2011-05-19 07:08:47 +00:00
|
|
|
<?php
|
|
|
|
class AvisSituation
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2012-11-05 15:33:01 +00:00
|
|
|
$c = Zend_Registry::get('config');
|
2012-11-16 14:12:03 +00:00
|
|
|
$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();
|
2011-05-19 08:45:12 +00:00
|
|
|
|
|
|
|
$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.
|
2011-05-19 08:45:12 +00:00
|
|
|
'pour login '.$user->getLogin().EOL;
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-25 06:45:25 +00:00
|
|
|
$mail = new Scores_Mail();
|
2011-05-19 08:45:12 +00:00
|
|
|
$mail->setFrom('contact');
|
|
|
|
$mail->addToKey('support');
|
|
|
|
$mail->setSubject($objet);
|
|
|
|
$mail->setBodyText($texte);
|
|
|
|
$mail->send();
|
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-10-30 21:19:27 +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-10-30 21:19:27 +00:00
|
|
|
|
|
|
|
// On télécharge le fichier sur le site
|
|
|
|
else {
|
|
|
|
|
|
|
|
$body = false;
|
|
|
|
$cookie = false;
|
|
|
|
|
|
|
|
//Initialisation de la session sur le site de l'Insee
|
2011-05-19 07:08:47 +00:00
|
|
|
$url = 'http://avis-situation-sirene.insee.fr/avisitu/jsp/avis.jsp';
|
2013-10-30 21:19:27 +00:00
|
|
|
try {
|
|
|
|
$client = new Zend_Http_Client($url);
|
|
|
|
$client->setCookieJar();
|
|
|
|
$response = $client->request('GET');
|
|
|
|
if ( $response->isSuccessful() ) {
|
|
|
|
$body = $response->getBody();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Zend_Http_Client_Exception $e) {
|
|
|
|
if (APPLICATION_ENV=='development') {
|
|
|
|
echo $e->getMessage();
|
|
|
|
}
|
2011-05-19 07:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-30 21:19:27 +00:00
|
|
|
$cookie = $client->getCookieJar();
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match("/<form name=\"demForm\" method=\"post\" action=\"\/avisitu\/IdentificationListeSiret.do/Uis", $body, $matches)) {
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
$dep=$depActif='';
|
|
|
|
if ($nic=='') $crit='S'; // l'établissement siège
|
|
|
|
else $crit=''; // établissement particulier, saisissez le NIC
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
//Post du formulaire - Liste
|
|
|
|
$url = 'http://avis-situation-sirene.insee.fr/avisitu/IdentificationListeSiret.do';
|
|
|
|
$post = array(
|
|
|
|
'siren' => $siren,
|
|
|
|
'critere' => $crit, // S pour le siège ou vide avec un NIC !!!
|
|
|
|
'nic' => $nic,
|
|
|
|
'departement' => $dep,
|
|
|
|
'departement_actif' => $depActif,
|
|
|
|
'bSubmit' => 'Valider'
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
$client = new Zend_Http_Client($url);
|
|
|
|
$client->setCookieJar($cookie);
|
|
|
|
$client->setParameterPost($post);
|
|
|
|
$response = $client->request('POST');
|
|
|
|
if ( $response->isSuccessful() ) {
|
|
|
|
$body = $response->getBody();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Zend_Http_Client_Exception $e) {
|
|
|
|
if (APPLICATION_ENV=='development') {
|
|
|
|
echo $e->getMessage();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$cookie = $client->getCookieJar();
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match("/<h3>Fiche établissement<\/h3>/Uis", $body, $matches))//<li class="ongletActif">établissement</li>
|
2011-05-19 07:08:47 +00:00
|
|
|
$tabInfos['fiche']='etab';
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match('/<div class="TitreGauche">(.*)<br\/>/Uis', $body, $matches)) {
|
|
|
|
$tabInfos['raiSoc']=trim($matches[1]);
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match("/Dernière mise à jour : (.*)<\/div>/Uis", $body, $matches))
|
|
|
|
$tabInfos['dateMaj']=trim($matches[1]);
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
$s1=substr($siren,0,3);
|
|
|
|
$s2=substr($siren,3,3);
|
|
|
|
$s3=substr($siren,6,3);
|
|
|
|
if (preg_match('/<div class="TitreDroite">(?:.*)('.$s1.'(?:.*)'.$s2.'(?:.*)'.$s3.')(?:.*)('.$nic.')(?:.*)<\/div>/Uis', $body, $matches)) {
|
|
|
|
$tabInfos['siren'] = trim($matches[1]);
|
|
|
|
$tabInfos['nic'] = trim($matches[2]);
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match('/<label id="labelFiche">Etat : <\/label>(.*)depuis le(.*)<\/p>/Uis', $body, $matches)) {
|
|
|
|
$tabInfos['etat'] = trim($matches[1]);
|
|
|
|
$tabInfos['dateEtat']= trim($matches[2]);
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match('/<label id="labelFiche">Catégorie d\'établissement : <\/label>(.*)<\/p>/Uis', $body, $matches)) {
|
|
|
|
$tabInfos['typeEtab']= trim($matches[1]);
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
$tabAdresse=array();
|
|
|
|
if (preg_match('/<label id="labelFiche">Adresse d\'implantation : <\/label>(?:.*)<ul id="adresse">(.*)<\/ul>/Uis', $body, $matches)) {
|
|
|
|
$strTmp=trim($matches[1]);
|
|
|
|
$tabTmp=explode('</li>', $strTmp);
|
|
|
|
foreach ($tabTmp as $i=>$strTmp)
|
|
|
|
$tabAdresse[$i]=trim(str_replace('<li>','',$strTmp));
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match('/<label id="labelFiche">(?:.*)Catégorie juridique :(?:.*)<\/label>(.*) - (.*)<\/p>/Uis', $body, $matches)) {
|
|
|
|
$tabInfos['fjCod']= trim($matches[1]);
|
|
|
|
$tabInfos['fjLib']= trim($matches[2]);
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match('/<label id="labelFiche">Activité principale exercée :(?:.*)<\/label>(.*) - (.*)<\/p>/Uis', $body, $matches)) {
|
|
|
|
$tabInfos['nafCod']=trim($matches[1]);
|
|
|
|
$tabInfos['nafLib']=trim($matches[2]);
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
if (preg_match('/<label id="labelFiche">(?:.*)Tranche d'effectif(.*)<\/label>(.*)<\/p>/Uis', $body, $matches)) {
|
|
|
|
$tabInfos['effPeriode']=trim($matches[1]);
|
|
|
|
$tabInfos['effTranche']=trim($matches[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$strCsv=$siren.';'.$nic.';'.$tabInfos['fiche'].';'.$tabInfos['dateMaj'].';'.
|
|
|
|
$tabInfos['siren'].';'.$tabInfos['nic'].';'.$tabInfos['raiSoc'].';'.
|
|
|
|
$tabInfos['etat'].';'.$tabInfos['dateEtat'].';'.$tabInfos['fjCod'].';'.$tabInfos['fjLib'].';'.
|
|
|
|
$tabInfos['nafCod'].';'.$tabInfos['nafLib'].';'.$tabInfos['effPeriode'].';'.
|
|
|
|
$tabInfos['effTranche'].';'.
|
|
|
|
$tabInfos['typeEtab'].';'.@implode(';',@$tabAdresse).
|
|
|
|
";\n";
|
|
|
|
$fp=fopen($this->pathLog."/avis.csv", "a");
|
|
|
|
fwrite($fp, $strCsv);
|
|
|
|
fclose($fp);
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
// $body contient l'avis de situation au format html
|
|
|
|
$tabErreurs=array();
|
|
|
|
if (preg_match('/name="erreurs" value="(.*)" class="erreurText" readonly/Ui', $body, $matches1)
|
|
|
|
|| preg_match('/name="erreurs_bis" value="(.*)" class="erreurTextBis" readonly/Ui', $body, $matches2)) {
|
|
|
|
$tabErreurs[]=@$matches1[1];
|
|
|
|
$tabErreurs[]=@$matches2[1];
|
|
|
|
die('<font color="red">ERREUR '.utf8_encode(implode(' ', $tabErreurs)).'</font>'); // Gérer le retour d'une erreur
|
|
|
|
}
|
|
|
|
usleep(round(rand(500000,1000000)));
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
// Get format PDF
|
|
|
|
if ( $format == 'pdf' ) {
|
|
|
|
$referer = $url;
|
|
|
|
$url = 'http://avis-situation-sirene.insee.fr/avisitu/AvisPdf.do';
|
|
|
|
$post = array(
|
|
|
|
'siren' => $siren,
|
|
|
|
'nic' => $nic,
|
|
|
|
'bSubmit' => 'Avis+de+Situation'
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
$client = new Zend_Http_Client($url);
|
|
|
|
$client->setCookieJar($cookie);
|
|
|
|
$client->setParameterPost($post);
|
|
|
|
$response = $client->request('POST');
|
|
|
|
if ( $response->isSuccessful() ) {
|
|
|
|
$body = $response->getBody();
|
|
|
|
file_put_contents($fichier, $body);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Zend_Http_Client_Exception $e) {
|
|
|
|
if (APPLICATION_ENV=='development') {
|
|
|
|
echo $e->getMessage();
|
|
|
|
}
|
|
|
|
return false;
|
2011-05-19 07:08:47 +00:00
|
|
|
}
|
2013-10-30 21:19:27 +00:00
|
|
|
$cookie = $client->getCookieJar();
|
|
|
|
} // Fin format PDF
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
return $body;
|
2011-05-19 07:08:47 +00:00
|
|
|
} // Fin fichier disponible
|
|
|
|
}
|
2012-05-20 16:31:28 +00:00
|
|
|
|
2013-10-30 21:19:27 +00:00
|
|
|
}
|