2016-08-30 10:24:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Infogreffe provider
|
|
|
|
*/
|
|
|
|
class Metier_Infogreffe_Service
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate debug mode
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $debug = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config definition
|
|
|
|
* @var Zend_Config
|
|
|
|
*/
|
|
|
|
public $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference client - customer reference
|
|
|
|
* G[Number]
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $reference_client;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type de document
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $type_document;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mode de diffusion
|
|
|
|
* XL : XML
|
|
|
|
* M : Mail
|
|
|
|
* C : Courrier
|
|
|
|
* T : Téléchargement
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $mode_diffusion;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $greffe;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $dossier_millesime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $dossier_statut;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $dossier_chrono;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var unknown
|
|
|
|
*/
|
|
|
|
public $date_depot;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $num_depot;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BI : Date de cloture
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $date_cloture;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var unknown
|
|
|
|
*/
|
|
|
|
public $date_acte;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var unknown
|
|
|
|
*/
|
|
|
|
public $type_acte;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AC : Numéro de l'acte
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $num;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SIREN
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $siren;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request XML
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $xml = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache delay
|
|
|
|
* @var unknown
|
|
|
|
*/
|
|
|
|
protected $cacheFiletime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize configuration
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//Load configuration
|
2016-11-24 16:02:49 +01:00
|
|
|
if (Zend_Registry::isRegistered('config')) {
|
2016-08-30 10:24:06 +02:00
|
|
|
$c = Zend_Registry::get('config');
|
|
|
|
} else {
|
|
|
|
$c = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
|
|
|
|
}
|
|
|
|
$this->config = $c->profil->infogreffe;
|
2016-11-24 16:02:49 +01:00
|
|
|
if (null === $this->config) {
|
2016-08-30 10:24:06 +02:00
|
|
|
throw new Exception('Unable to load configuration file.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cacheFiletime = $c->profil->infogreffe->cache->time;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function callRequest()
|
|
|
|
{
|
|
|
|
$fromCache = false;
|
2016-11-24 16:02:49 +01:00
|
|
|
if ($this->mode_diffusion == 'XL' && $this->fileIsCache()) {
|
2016-08-30 10:24:06 +02:00
|
|
|
$fromCache = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fromCache) {
|
|
|
|
$xml = $this->fileFromCache();
|
|
|
|
} else {
|
|
|
|
$xml = $this->getProduitsXML();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->error($xml);
|
|
|
|
|
2016-11-24 16:02:49 +01:00
|
|
|
if ($this->mode_diffusion == 'XL') {
|
2016-08-30 10:24:06 +02:00
|
|
|
$this->fileTocache($xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save data in cache
|
|
|
|
* @param string $xml
|
|
|
|
*/
|
|
|
|
protected function fileTocache($xml)
|
|
|
|
{
|
|
|
|
$filename = $this->type_document . '-' . $this->siren . '.xml';
|
|
|
|
$file = $this->config->cache->path . DIRECTORY_SEPARATOR . $filename;
|
|
|
|
file_put_contents($file, $xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $xml
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function fileFromCache()
|
|
|
|
{
|
|
|
|
$filename = $this->type_document . '-' . $this->siren . '.xml';
|
|
|
|
$file = $file = $this->config->cache->path . DIRECTORY_SEPARATOR . $filename;
|
|
|
|
return file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function fileIsCache()
|
|
|
|
{
|
|
|
|
$filename = $this->type_document . '-' . $this->siren . '.xml';
|
|
|
|
$file = $file = $this->config->cache->path . DIRECTORY_SEPARATOR . $filename;
|
2016-11-24 16:02:49 +01:00
|
|
|
if (!file_exists($file)) {
|
2016-08-30 10:24:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dateFile = filemtime($file);
|
2016-11-24 16:02:49 +01:00
|
|
|
$now = mktime(date('G'), date('i'), date('s'), date('m'), date('d'), date('Y'));
|
2016-08-30 10:24:06 +02:00
|
|
|
$maxTime = mktime(
|
2016-11-24 16:02:49 +01:00
|
|
|
date('G', $dateFile)+$this->cacheFiletime,
|
|
|
|
date('i', $dateFile),
|
|
|
|
date('s', $dateFile),
|
|
|
|
date("m", $dateFile),
|
|
|
|
date("d", $dateFile),
|
|
|
|
date("Y", $dateFile)
|
2016-08-30 10:24:06 +02:00
|
|
|
);
|
|
|
|
|
2016-11-24 16:02:49 +01:00
|
|
|
if ($now>$maxTime) {
|
2016-08-30 10:24:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect error
|
|
|
|
* @param string $xml
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function error($xml)
|
|
|
|
{
|
2016-11-24 16:02:49 +01:00
|
|
|
if (!empty($xml)) {
|
2016-08-30 10:24:06 +02:00
|
|
|
$doc = new DOMDocument();
|
|
|
|
$load = $doc->loadXML($xml, LIBXML_NOERROR | LIBXML_NOWARNING);
|
2016-11-24 16:02:49 +01:00
|
|
|
if (!$load) {
|
2016-08-30 10:24:06 +02:00
|
|
|
$tmp = explode('-', $xml);
|
|
|
|
$errNum = intval($tmp[0]);
|
|
|
|
$errMsg = $tmp[1];
|
|
|
|
throw new Exception($errMsg, $errNum);
|
|
|
|
}
|
2016-11-24 16:02:49 +01:00
|
|
|
} else {
|
2016-08-30 10:24:06 +02:00
|
|
|
throw new Exception('XML content is empty.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download file from URL
|
2016-10-12 11:04:27 +02:00
|
|
|
* @todo : Use Guzzle
|
2016-08-30 10:24:06 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param string $filename
|
|
|
|
* @throws Exception
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function download($url, $filename)
|
|
|
|
{
|
|
|
|
$file = $this->config->storage->path . '/' . $filename;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$client = new Zend_Http_Client($url);
|
|
|
|
$client->setStream();
|
|
|
|
$response = $client->request('GET');
|
2016-11-24 16:02:49 +01:00
|
|
|
if ($response->isSuccessful() && substr($response->getBody(), 0, 4)=='%PDF') {
|
|
|
|
if (copy($response->getStreamName(), $file)) {
|
2016-08-30 10:24:06 +02:00
|
|
|
return $file;
|
|
|
|
} else {
|
2016-11-24 16:02:49 +01:00
|
|
|
throw new Exception("Erreur lors de l'ecriture du fichier");
|
2016-08-30 10:24:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-24 16:02:49 +01:00
|
|
|
throw new Exception("Fichier non PDF");
|
2016-08-30 10:24:06 +02:00
|
|
|
}
|
|
|
|
} catch (Zend_Http_Client_Exception $e) {
|
2016-11-24 16:02:49 +01:00
|
|
|
throw new Exception($e->getMessage());
|
2016-08-30 10:24:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pdf information
|
|
|
|
* @param string $pdf
|
|
|
|
* @return array
|
|
|
|
* pages => number of pages
|
|
|
|
* version => pdf version
|
|
|
|
* size => pdf filesize
|
|
|
|
*/
|
|
|
|
public function pdfInfos($pdf)
|
|
|
|
{
|
2016-11-24 16:02:49 +01:00
|
|
|
if (false !== ($file = file_get_contents($pdf))) {
|
2016-08-30 10:24:06 +02:00
|
|
|
//Number of page
|
2016-11-24 16:02:49 +01:00
|
|
|
$pages = preg_match_all("/\/Page\W/", $file, $matches);
|
2016-08-30 10:24:06 +02:00
|
|
|
|
|
|
|
//Pdf Version
|
|
|
|
preg_match("/^\%PDF\-(.*)\s/U", $file, $matches);
|
|
|
|
$version = $matches[1];
|
|
|
|
|
|
|
|
//Pdf size
|
|
|
|
$size = filesize($pdf);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'pages' => $pages,
|
|
|
|
'version' => $version,
|
|
|
|
'size' => $size,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define XML for the request
|
|
|
|
*/
|
|
|
|
protected function setXML()
|
|
|
|
{
|
|
|
|
//Construct the request
|
|
|
|
$xml = new SimpleXMLElement('<demande></demande>');
|
|
|
|
$emetteur = $xml->addChild('emetteur');
|
|
|
|
$emetteur->addChild('code_abonne', $this->config->user);
|
|
|
|
$emetteur->addChild('mot_passe', $this->config->password);
|
|
|
|
|
|
|
|
$code_requete = $emetteur->addChild('code_requete');
|
|
|
|
$code_requete->addChild('type_profil', 'A');
|
|
|
|
$code_requete->addChild('origine_emetteur', 'IC');
|
|
|
|
|
|
|
|
// C = Commande de documents
|
|
|
|
$code_requete->addChild('nature_requete', 'C');
|
|
|
|
|
|
|
|
$code_requete->addChild('type_document', $this->type_document);
|
|
|
|
$code_requete->addChild('type_requete', 'S'); // S = Simple
|
|
|
|
|
|
|
|
// Mode de diffusion : C = Courrier, T = Téléchargement, M = Mail, XL = XML
|
|
|
|
$mode_diffusion = $code_requete->addChild('mode_diffusion');
|
2016-11-24 16:02:49 +01:00
|
|
|
if ($this->mode_diffusion=='XL') {
|
2016-08-30 10:24:06 +02:00
|
|
|
//On ajoute tout les types de diffusions pour XL
|
|
|
|
$mode_diffusion->addChild('mode')->addAttribute('type', 'C');
|
|
|
|
$mode_diffusion->addChild('mode')->addAttribute('type', 'T');
|
|
|
|
}
|
|
|
|
$mode_diffusion->addChild('mode')->addAttribute('type', $this->mode_diffusion);
|
|
|
|
|
|
|
|
$code_requete->addChild('media', 'WS');
|
|
|
|
|
|
|
|
$commande = $xml->addChild('commande');
|
|
|
|
$commande->addChild('num_siren', $this->siren);
|
|
|
|
|
2016-11-24 16:02:49 +01:00
|
|
|
if ($this->mode_diffusion!='XL') {
|
2016-08-30 10:24:06 +02:00
|
|
|
// Commande de documents : bilan saisie ou bilan complet
|
2016-11-24 16:02:49 +01:00
|
|
|
if (($this->type_document=='BS' || $this->type_document=='BI')) {
|
2016-08-30 10:24:06 +02:00
|
|
|
$num_gest = $commande->addChild('num_gest');
|
2016-11-24 16:02:49 +01:00
|
|
|
$num_gest->addChild('greffe', $this->greffe);
|
|
|
|
$num_gest->addChild('dossier_millesime', $this->dossier_millesime);
|
|
|
|
$num_gest->addChild('dossier_statut', $this->dossier_statut);
|
|
|
|
$num_gest->addChild('dossier_chrono', $this->dossier_chrono);
|
|
|
|
$commande->addChild('num_depot', $this->num_depot);
|
2016-08-30 10:24:06 +02:00
|
|
|
//Date de cloture au format dd/MM/yyyy
|
|
|
|
$commande->addChild('date_cloture', $this->date_cloture);
|
|
|
|
}
|
|
|
|
// Commande de documents : actes
|
2016-11-24 16:02:49 +01:00
|
|
|
elseif ($this->type_document=='AC') {
|
2016-08-30 10:24:06 +02:00
|
|
|
$num_gest = $commande->addChild('num_gest');
|
2016-11-24 16:02:49 +01:00
|
|
|
$num_gest->addChild('greffe', $this->greffe);
|
|
|
|
$num_gest->addChild('dossier_millesime', $this->dossier_millesime);
|
|
|
|
$num_gest->addChild('dossier_statut', $this->dossier_statut);
|
|
|
|
$num_gest->addChild('dossier_chrono', $this->dossier_chrono);
|
|
|
|
$commande->addChild('num_depot', $this->num_depot);
|
2016-08-30 10:24:06 +02:00
|
|
|
$liste_actes = $commande->addChild('liste_actes');
|
|
|
|
$liste_actes->addChild('acte')->addAttribute('num', $this->num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set Command ID
|
|
|
|
$commande->addChild('reference_client', $this->reference_client);
|
|
|
|
|
|
|
|
$xml = str_replace('<?xml version="1.0"?>', '', $xml->asXML());
|
|
|
|
|
|
|
|
$this->xml = $xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send XML Request
|
|
|
|
* We have some problem to use SOAP so we use CURL
|
|
|
|
* @throws Exception
|
|
|
|
* @return string XML Response
|
|
|
|
*/
|
|
|
|
protected function getProduitsXML()
|
|
|
|
{
|
|
|
|
$this->setXML();
|
|
|
|
|
|
|
|
$req = $this->xml;
|
|
|
|
|
|
|
|
if ($this->debug) {
|
|
|
|
file_put_contents($this->type_document.'-'.$this->siren.'-'.$this->mode_diffusion.'.query', $this->xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create XML request
|
|
|
|
$post = '<?xml version="1.0" encoding="UTF-8"?>'.
|
|
|
|
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '.
|
|
|
|
'xmlns:ns1="https://webservices.infogreffe.fr/" '.
|
|
|
|
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '.
|
|
|
|
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '.
|
|
|
|
'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '.
|
|
|
|
'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'.
|
|
|
|
'<SOAP-ENV:Body>'.
|
|
|
|
'<ns1:getProduitsWebServicesXML>'.
|
|
|
|
'<param0 xsi:type="xsd:string">'.$req.'</param0>'.
|
|
|
|
'</ns1:getProduitsWebServicesXML>'.
|
|
|
|
'</SOAP-ENV:Body>'.
|
|
|
|
'</SOAP-ENV:Envelope>';
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->config->url);
|
2016-11-24 16:02:49 +01:00
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, true);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
2016-08-30 10:24:06 +02:00
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
|
2016-11-24 16:02:49 +01:00
|
|
|
if (curl_errno($ch)) {
|
|
|
|
throw new Exception(curl_error($ch));
|
2016-08-30 10:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Remove SOAP part of XML
|
|
|
|
$response = str_replace("<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><SOAP-ENV:Body><ns0:getProduitsWebServicesXMLResponse xmlns:ns0='urn:local' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><return xsi:type='xsd:string'>", '', $response);
|
2016-11-24 16:02:49 +01:00
|
|
|
$response = str_replace('</return></ns0:getProduitsWebServicesXMLResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>', '', $response);
|
2016-08-30 10:24:06 +02:00
|
|
|
|
|
|
|
if ($this->debug) {
|
|
|
|
file_put_contents($this->type_document.'-'.$this->siren.'-'.$this->mode_diffusion.'.response', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2016-11-24 16:02:49 +01:00
|
|
|
}
|