Insee : Avis de situation changement d'url
This commit is contained in:
parent
304942f6eb
commit
fe876711c4
@ -1220,30 +1220,48 @@ class IdentiteController extends Zend_Controller_Action
|
||||
{
|
||||
$this->_helper->layout()->disableLayout();
|
||||
$this->_helper->viewRenderer->setNoRender(true);
|
||||
if (APPLICATION_ENV == 'staging' || true) {
|
||||
|
||||
// --- En environement de preprod, on envoi sur le site
|
||||
if (APPLICATION_ENV == 'staging') {
|
||||
$this->redirect('http://avis-situation-sirene.insee.fr/');
|
||||
} else {
|
||||
// ==> Start
|
||||
}
|
||||
// --- Téléchargement du fichier et distribution
|
||||
else {
|
||||
$avis = new Scores_Insee_AvisSituation($this->siret);
|
||||
if (!$avis->erreur()){
|
||||
if ( !$avis->erreur() ) {
|
||||
$avis->erreurcpt('raz');
|
||||
$body = $avis->get('pdf', 1);
|
||||
if($body !== false) {
|
||||
header("Pragma: public");
|
||||
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
|
||||
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
|
||||
header("Cache-Control: must-revalidate");
|
||||
header("Content-type: application/pdf");
|
||||
header("Content-Length: ".strlen($body));
|
||||
header('Content-disposition: inline; filename="avis-'.$this->siret.'.pdf"');
|
||||
header("Accept-Ranges: ".strlen($body));
|
||||
echo $body;
|
||||
$ws = new WsScores();
|
||||
$ws->setLog('avis_situation', $this->siret, 0, '');
|
||||
} else {
|
||||
$avis->erreurcpt('plus');
|
||||
echo $avis->erreurmsg();
|
||||
}
|
||||
$file = $avis->get();
|
||||
if ($file === false) {
|
||||
$avis->erreurcpt('plus');
|
||||
echo $avis->erreurmsg();
|
||||
} else {
|
||||
// --- Envoi du fichier sur la sortie standard
|
||||
$content_type = 'application/pdf';
|
||||
if ( file_exists($file) ) {
|
||||
$ws = new WsScores();
|
||||
$ws->setLog('avis_situation', $this->siret, 0, '');
|
||||
$modules = array(); //apache_get_modules();
|
||||
if (in_array('mod_xsendfile', $modules)) {
|
||||
header ('X-Sendfile: ' . $file);
|
||||
header ('Content-Type: ' . $content_type);
|
||||
header ('Content-Disposition: attachment; filename="' . $content_type . '"');
|
||||
exit;
|
||||
} else {
|
||||
ini_set('zlib.output_compression', '0');
|
||||
header('Content-Transfer-Encoding: none');
|
||||
header('Content-Type: ' . $content_type.'');
|
||||
header('Content-Length: ' . filesize($file));
|
||||
header('Content-MD5: ' . base64_encode(md5_file($file)));
|
||||
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
|
||||
header('Cache-Control: private, max-age=0, must-revalidate');
|
||||
header('Pragma: public');
|
||||
readfile($file);
|
||||
exit;
|
||||
}
|
||||
} else {
|
||||
echo "Impossible de charger le fichier";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($avis->nberreur()==5){
|
||||
$avis->mailerreur();
|
||||
|
@ -1,28 +1,29 @@
|
||||
<?php
|
||||
class Scores_Insee_AvisSituation
|
||||
{
|
||||
protected static $timeout = 10;
|
||||
protected static $retryDelay = 300;
|
||||
protected $fichierErreur;
|
||||
protected $retryDelay = 300;
|
||||
protected $maxTime = 120;
|
||||
protected $lock;
|
||||
protected $pathLog;
|
||||
protected $pathAvisPdf;
|
||||
protected $pathPdf;
|
||||
protected $siret;
|
||||
|
||||
public function __construct($siret)
|
||||
{
|
||||
$c = Zend_Registry::get('config');
|
||||
$this->pathAvisPdf = $c->profil->path->shared.'/temp/files';
|
||||
$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->fichierErreur = $this->pathLog.'/aviserreur.lock';
|
||||
$this->siret = $siret;
|
||||
$this->lock = $this->pathLog.'/aviserreur.lock';
|
||||
}
|
||||
|
||||
public function erreurcpt($action)
|
||||
{
|
||||
switch($action){
|
||||
case 'plus':
|
||||
if (file_exists($this->fichierErreur)){
|
||||
$handle = fopen($this->fichierErreur, 'r');
|
||||
if (file_exists($this->lock)){
|
||||
$handle = fopen($this->lock, 'r');
|
||||
$data = fgetcsv($handle, '1000', ';');
|
||||
$date_creation = $data[0];
|
||||
$date_modification = time();
|
||||
@ -34,12 +35,12 @@ class Scores_Insee_AvisSituation
|
||||
$nb = 0;
|
||||
}
|
||||
$nb++;
|
||||
$handle = fopen($this->fichierErreur, 'w');
|
||||
$handle = fopen($this->lock, 'w');
|
||||
fputcsv($handle, array($date_creation, $date_modification, $nb), ';');
|
||||
fclose($handle);
|
||||
break;
|
||||
case 'raz':
|
||||
$handle = fopen($this->fichierErreur, 'w');
|
||||
$handle = fopen($this->lock, 'w');
|
||||
$date_creation = time();
|
||||
$date_modification = time();
|
||||
$nb = 0;
|
||||
@ -51,8 +52,8 @@ class Scores_Insee_AvisSituation
|
||||
|
||||
public function nberreur()
|
||||
{
|
||||
if (file_exists($this->fichierErreur)){
|
||||
$handle = fopen($this->fichierErreur, 'r');
|
||||
if (file_exists($this->lock)){
|
||||
$handle = fopen($this->lock, 'r');
|
||||
$data = fgetcsv($handle, '1000', ';');
|
||||
$nb = $data[2];
|
||||
fclose($handle);
|
||||
@ -64,9 +65,9 @@ class Scores_Insee_AvisSituation
|
||||
|
||||
public function erreur()
|
||||
{
|
||||
if (file_exists($this->fichierErreur))
|
||||
if (file_exists($this->lock))
|
||||
{
|
||||
$handle = fopen($this->fichierErreur, 'r');
|
||||
$handle = fopen($this->lock, 'r');
|
||||
$data = fgetcsv($handle, '1000', ';');
|
||||
$date_creation = $data[0];
|
||||
$date_modification = $data[1];
|
||||
@ -101,37 +102,53 @@ class Scores_Insee_AvisSituation
|
||||
$mail->execute();
|
||||
}
|
||||
|
||||
public function erreurmsg(){
|
||||
public function erreurmsg()
|
||||
{
|
||||
return "<h3>Le site partenaire n'a pas répondu correctement ou est indisponible. Merci d'essayer à nouveau ultérieurement.</h3>";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* 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()
|
||||
{
|
||||
$force=$force*1;
|
||||
$date=date('Ymd');
|
||||
$siren=trim(substr($this->siret,0,9));
|
||||
$nic=trim(substr($this->siret,9,5));
|
||||
$fichier = $this->pathAvisPdf.'/avis-'.$siren.'-'.$nic.'-'.$date.'.pdf';
|
||||
if ($format!='pdf') return 'Format pdf uniquement';
|
||||
$file = $this->pathPdf.'/avis-'.$siren.'-'.$nic.'-'.$date.'.pdf';
|
||||
|
||||
// On délivre l'avis en base
|
||||
if ($force==0 && file_exists($fichier)) {
|
||||
return file_get_contents($fichier);
|
||||
}
|
||||
|
||||
// On télécharge le fichier sur le site
|
||||
// --- 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/avisitu/AvisPdf.do';
|
||||
$url = 'http://avis-situation-sirene.insee.fr/AvisPdf.action';
|
||||
$post = array(
|
||||
'siren' => $siren,
|
||||
'nic' => $nic,
|
||||
'bSubmit' => 'Avis+de+Situation'
|
||||
'form.siren' => $siren,
|
||||
'form.nic' => $nic,
|
||||
'boutonAide' => "Avis de Situation"
|
||||
);
|
||||
try {
|
||||
$client = new Zend_Http_Client($url);
|
||||
@ -140,8 +157,8 @@ class Scores_Insee_AvisSituation
|
||||
$response = $client->request('POST');
|
||||
if ( $response->isSuccessful() && substr($response->getBody(),0,4)=='%PDF' ) {
|
||||
$body = $response->getBody();
|
||||
file_put_contents($fichier, $body);
|
||||
return $body;
|
||||
file_put_contents($file, $body);
|
||||
return $file;
|
||||
}
|
||||
} catch (Zend_Http_Client_Exception $e) {
|
||||
if (APPLICATION_ENV=='development') {
|
||||
@ -150,7 +167,6 @@ class Scores_Insee_AvisSituation
|
||||
}
|
||||
|
||||
return false;
|
||||
} // Fin fichier disponible
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user