webservice/library/Metier/Infogreffe/Service.php
Michael RICOIS 53e213202b CS PSR-2
2016-11-24 16:02:49 +01:00

420 lines
12 KiB
PHP
Executable File

<?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
if (Zend_Registry::isRegistered('config')) {
$c = Zend_Registry::get('config');
} else {
$c = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
}
$this->config = $c->profil->infogreffe;
if (null === $this->config) {
throw new Exception('Unable to load configuration file.');
}
$this->cacheFiletime = $c->profil->infogreffe->cache->time;
}
public function callRequest()
{
$fromCache = false;
if ($this->mode_diffusion == 'XL' && $this->fileIsCache()) {
$fromCache = true;
}
if ($fromCache) {
$xml = $this->fileFromCache();
} else {
$xml = $this->getProduitsXML();
}
$this->error($xml);
if ($this->mode_diffusion == 'XL') {
$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;
if (!file_exists($file)) {
return false;
}
$dateFile = filemtime($file);
$now = mktime(date('G'), date('i'), date('s'), date('m'), date('d'), date('Y'));
$maxTime = mktime(
date('G', $dateFile)+$this->cacheFiletime,
date('i', $dateFile),
date('s', $dateFile),
date("m", $dateFile),
date("d", $dateFile),
date("Y", $dateFile)
);
if ($now>$maxTime) {
return true;
}
return false;
}
/**
* Detect error
* @param string $xml
* @throws Exception
*/
protected function error($xml)
{
if (!empty($xml)) {
$doc = new DOMDocument();
$load = $doc->loadXML($xml, LIBXML_NOERROR | LIBXML_NOWARNING);
if (!$load) {
$tmp = explode('-', $xml);
$errNum = intval($tmp[0]);
$errMsg = $tmp[1];
throw new Exception($errMsg, $errNum);
}
} else {
throw new Exception('XML content is empty.');
}
}
/**
* Download file from URL
* @todo : Use Guzzle
* @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');
if ($response->isSuccessful() && substr($response->getBody(), 0, 4)=='%PDF') {
if (copy($response->getStreamName(), $file)) {
return $file;
} else {
throw new Exception("Erreur lors de l'ecriture du fichier");
}
} else {
throw new Exception("Fichier non PDF");
}
} catch (Zend_Http_Client_Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Pdf information
* @param string $pdf
* @return array
* pages => number of pages
* version => pdf version
* size => pdf filesize
*/
public function pdfInfos($pdf)
{
if (false !== ($file = file_get_contents($pdf))) {
//Number of page
$pages = preg_match_all("/\/Page\W/", $file, $matches);
//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');
if ($this->mode_diffusion=='XL') {
//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);
if ($this->mode_diffusion!='XL') {
// Commande de documents : bilan saisie ou bilan complet
if (($this->type_document=='BS' || $this->type_document=='BI')) {
$num_gest = $commande->addChild('num_gest');
$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);
//Date de cloture au format dd/MM/yyyy
$commande->addChild('date_cloture', $this->date_cloture);
}
// Commande de documents : actes
elseif ($this->type_document=='AC') {
$num_gest = $commande->addChild('num_gest');
$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);
$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);
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);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
//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);
$response = str_replace('</return></ns0:getProduitsWebServicesXMLResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>', '', $response);
if ($this->debug) {
file_put_contents($this->type_document.'-'.$this->siren.'-'.$this->mode_diffusion.'.response', $response);
}
return $response;
}
}