172 lines
4.2 KiB
PHP
172 lines
4.2 KiB
PHP
<?php
|
|
class Scores_Insee_AvisSituation
|
|
{
|
|
protected $retryDelay = 300;
|
|
protected $maxTime = 120;
|
|
protected $lock;
|
|
protected $pathLog;
|
|
protected $pathPdf;
|
|
protected $siret;
|
|
|
|
public function __construct($siret)
|
|
{
|
|
$this->siret = $siret;
|
|
|
|
$c = Zend_Registry::get('config');
|
|
$this->pathPdf = $c->profil->path->shared.'/temp/files';
|
|
$this->pathLog = $c->profil->path->shared.'/persist/log';
|
|
$this->lock = $this->pathLog.'/aviserreur.lock';
|
|
}
|
|
|
|
public function erreurcpt($action)
|
|
{
|
|
switch($action){
|
|
case 'plus':
|
|
if (file_exists($this->lock)){
|
|
$handle = fopen($this->lock, 'r');
|
|
$data = fgetcsv($handle, '1000', ';');
|
|
$date_creation = $data[0];
|
|
$date_modification = time();
|
|
$nb = $data[2];
|
|
fclose($handle);
|
|
} else {
|
|
$date_creation = time();
|
|
$date_modification = time();
|
|
$nb = 0;
|
|
}
|
|
$nb++;
|
|
$handle = fopen($this->lock, 'w');
|
|
fputcsv($handle, array($date_creation, $date_modification, $nb), ';');
|
|
fclose($handle);
|
|
break;
|
|
case 'raz':
|
|
$handle = fopen($this->lock, 'w');
|
|
$date_creation = time();
|
|
$date_modification = time();
|
|
$nb = 0;
|
|
fputcsv($handle, array($date_creation, $date_modification, $nb), ';');
|
|
fclose($handle);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function nberreur()
|
|
{
|
|
if (file_exists($this->lock)){
|
|
$handle = fopen($this->lock, 'r');
|
|
$data = fgetcsv($handle, '1000', ';');
|
|
$nb = $data[2];
|
|
fclose($handle);
|
|
} else {
|
|
$nb = 1;
|
|
}
|
|
return $nb;
|
|
}
|
|
|
|
public function erreur()
|
|
{
|
|
if (file_exists($this->lock))
|
|
{
|
|
$handle = fopen($this->lock, 'r');
|
|
$data = fgetcsv($handle, '1000', ';');
|
|
$date_creation = $data[0];
|
|
$date_modification = $data[1];
|
|
$nb = $data[2];
|
|
fclose($handle);
|
|
} else {
|
|
$date_creation = 0;
|
|
$date_modification = 0;
|
|
}
|
|
if ($nb > 0 && $date_modification < $date_creation + $this->retryDelay) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function mailerreur()
|
|
{
|
|
$user = new Scores_Utilisateur();
|
|
|
|
$objet = "AVIS INSEE - (Date :".date("d")."/".date("m")."/".date("Y").")";
|
|
$texte = "Accès impossible au site de situation INSEE : ".
|
|
$this->siret."\n".
|
|
"http://avis-situation-sirene.insee.fr"."\n".
|
|
"pour login ".$user->getLogin()."\n";
|
|
|
|
$mail = new Scores_Mail_Method();
|
|
$mail->setFromKey('contact');
|
|
$mail->addToKey('support');
|
|
$mail->setSubject($objet);
|
|
$mail->setBodyText($texte);
|
|
$mail->execute();
|
|
}
|
|
|
|
public function erreurmsg()
|
|
{
|
|
return "<h3>Le site partenaire n'a pas répondu correctement ou est indisponible. Merci d'essayer à nouveau ultérieurement.</h3>";
|
|
}
|
|
|
|
/**
|
|
* Détermine suivant la date de fichier si celui-ci est périmé
|
|
*/
|
|
protected function timeover($file)
|
|
{
|
|
$dateFile = filemtime($file);
|
|
$now = mktime(date('G'), date('i'), date('s'), date("m") , date("d"), date("Y"));
|
|
$maxTime = mktime(
|
|
date('G',$dateFile)+$this->maxTime,
|
|
date('i',$dateFile),
|
|
date('s',$dateFile),
|
|
date("m",$dateFile),
|
|
date("d",$dateFile),
|
|
date("Y",$dateFile));
|
|
if( $now>$maxTime ) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Récupére l'avis de situtation à partir du site au format pdf
|
|
* @return string Nom du fichier avec chemin
|
|
*/
|
|
public function get()
|
|
{
|
|
$date=date('Ymd');
|
|
$siren=trim(substr($this->siret,0,9));
|
|
$nic=trim(substr($this->siret,9,5));
|
|
$file = $this->pathPdf.'/avis-'.$siren.'-'.$nic.'-'.$date.'.pdf';
|
|
|
|
// --- Le fichier existe
|
|
if (file_exists($file) && $this->timeover($file) === false) {
|
|
return $file;
|
|
}
|
|
// ---- On télécharge le fichier sur le site
|
|
else {
|
|
$url = 'http://avis-situation-sirene.insee.fr/AvisPdf.action';
|
|
$post = array(
|
|
'form.siren' => $siren,
|
|
'form.nic' => $nic,
|
|
'boutonAide' => "Avis de Situation"
|
|
);
|
|
try {
|
|
$client = new Zend_Http_Client($url);
|
|
$client->setStream();
|
|
$client->setParameterPost($post);
|
|
$response = $client->request('POST');
|
|
if ( $response->isSuccessful() && substr($response->getBody(),0,4)=='%PDF' ) {
|
|
$body = $response->getBody();
|
|
file_put_contents($file, $body);
|
|
return $file;
|
|
}
|
|
} catch (Zend_Http_Client_Exception $e) {
|
|
if (APPLICATION_ENV=='development') {
|
|
echo $e->getMessage();
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
} |