extranet/library/Scores/Insee/AvisSituation.php

156 lines
4.0 KiB
PHP
Raw Normal View History

2011-05-19 07:08:47 +00:00
<?php
class Scores_Insee_AvisSituation
2011-05-19 07:08:47 +00:00
{
protected static $timeout = 10;
protected static $retryDelay = 300;
protected $fichierErreur;
2011-05-19 07:24:34 +00:00
protected $pathLog;
2011-05-25 09:26:15 +00:00
protected $pathAvisPdf;
2011-05-19 07:08:47 +00:00
protected $siret;
2012-05-20 16:31:28 +00:00
2011-05-19 07:08:47 +00:00
public function __construct($siret)
{
$c = Zend_Registry::get('config');
$this->pathAvisPdf = $c->profil->path->files;
$this->pathLog = realpath($c->profil->path->data).'/log';
2011-05-19 07:24:34 +00:00
$this->fichierErreur = $this->pathLog.'/aviserreur.lock';
2011-05-19 07:08:47 +00:00
$this->siret = $siret;
}
public function erreurcpt($action)
{
switch($action){
case 'plus':
if (file_exists($this->fichierErreur)){
$handle = fopen($this->fichierErreur, '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->fichierErreur, 'w');
fputcsv($handle, array($date_creation, $date_modification, $nb), ';');
fclose($handle);
break;
case 'raz':
$handle = fopen($this->fichierErreur, 'w');
$date_creation = time();
$date_modification = time();
$nb = 0;
fputcsv($handle, array($date_creation, $date_modification, $nb), ';');
fclose($handle);
break;
}
}
2012-05-20 16:31:28 +00:00
2011-05-19 07:08:47 +00:00
public function nberreur()
{
if (file_exists($this->fichierErreur)){
$handle = fopen($this->fichierErreur, 'r');
$data = fgetcsv($handle, '1000', ';');
$nb = $data[2];
fclose($handle);
} else {
$nb = 1;
}
return $nb;
}
2012-05-20 16:31:28 +00:00
2011-05-19 07:08:47 +00:00
public function erreur()
{
if (file_exists($this->fichierErreur))
{
$handle = fopen($this->fichierErreur, '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;
}
}
2012-05-20 16:31:28 +00:00
2011-05-19 07:08:47 +00:00
public function mailerreur()
{
2012-05-20 16:31:28 +00:00
$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.EOL.
2011-05-19 07:08:47 +00:00
'http://avis-situation-sirene.insee.fr'.EOL.
'pour login '.$user->getLogin().EOL;
2012-05-20 16:31:28 +00:00
$mail = new Scores_Mail_Method();
2015-06-11 15:29:14 +00:00
$mail->setFromKey('contact');
$mail->addToKey('support');
$mail->setSubject($objet);
$mail->setBodyText($texte);
2015-06-11 15:29:14 +00:00
$mail->execute();
2011-05-19 07:08:47 +00:00
}
2012-05-20 16:31:28 +00:00
2011-05-19 07:08:47 +00:00
public function erreurmsg(){
return "<h3>Le site partenaire n'a pas répondu correctement ou est indisponible. Merci d'essayer à nouveau ultérieurement.</h3>";
}
2012-05-20 16:31:28 +00:00
2011-05-19 07:08:47 +00:00
/**
* Récupére l'avis de situtation à partir du site au format pdf
* @param string $format Format 'pdf' ou 'array'
* @param boolean $force True aller obligatoirement le chercher à l'insee
* @return string Le PDF demandé
*/
public function get($format='pdf', $force=0)
{
$force=$force*1;
$date=date('Ymd');
$siren=trim(substr($this->siret,0,9));
$nic=trim(substr($this->siret,9,5));
2011-05-19 07:24:34 +00:00
$fichier = $this->pathAvisPdf.'/avis-'.$siren.'-'.$nic.'-'.$date.'.pdf';
2011-05-19 07:08:47 +00:00
if ($format!='pdf') return 'Format pdf uniquement';
2013-11-05 14:45:25 +00:00
// On délivre l'avis en base
if ($force==0 && file_exists($fichier)) {
2011-05-19 07:08:47 +00:00
return file_get_contents($fichier);
}
2013-11-05 14:45:25 +00:00
// On télécharge le fichier sur le site
else {
$url = 'http://avis-situation-sirene.insee.fr/avisitu/AvisPdf.do';
$post = array(
'siren' => $siren,
'nic' => $nic,
'bSubmit' => 'Avis+de+Situation'
);
2013-11-05 14:45:25 +00:00
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($fichier, $body);
return $body;
2013-11-05 14:45:25 +00:00
}
} catch (Zend_Http_Client_Exception $e) {
if (APPLICATION_ENV=='development') {
echo $e->getMessage();
}
2011-05-19 07:08:47 +00:00
}
2012-05-20 16:31:28 +00:00
return false;
2011-05-19 07:08:47 +00:00
} // Fin fichier disponible
}
2012-05-20 16:31:28 +00:00
2013-11-05 14:45:25 +00:00
}