421 lines
12 KiB
PHP
Raw Normal View History

2013-11-05 11:18:30 +00:00
<?php
/**
* Infogreffe provider
*/
class Metier_Infogreffe
{
/**
* 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;
2014-01-16 10:47:25 +00:00
/**
*
* @var unknown
*/
public $type_acte;
2013-11-05 11:18:30 +00:00
/**
* AC : Numéro de l'acte
* @var string
*/
public $num;
/**
* SIREN
* @var string
*/
public $siren;
/**
* Request XML
* @var string
*/
protected $xml = '';
2014-02-25 16:33:07 +00:00
/**
* Cache delay
* @var unknown
*/
protected $cacheFiletime;
2013-11-05 11:18:30 +00:00
/**
* Initialize configuration
*/
public function __construct()
{
//Load configuration
$c = Zend_Registry::get('config');
$this->config = $c->profil->infogreffe;
if ( null === $this->config ) {
throw new Exception('Unable to load configuration file.');
}
2014-02-25 16:33:07 +00:00
$this->cacheFiletime = $c->profil->infogreffe->cache->time;
2013-11-05 11:18:30 +00:00
}
public function callRequest()
{
$fromCache = false;
2014-02-25 16:33:07 +00:00
if ( $this->mode_diffusion == 'XL' && $this->fileIsCache() ){
2013-11-05 11:18:30 +00:00
$fromCache = true;
}
if ($fromCache) {
$xml = $this->fileFromCache();
} else {
$xml = $this->getProduitsXML();
}
2013-12-26 14:25:59 +00:00
$this->error($xml);
2013-11-05 11:18:30 +00:00
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
*/
2014-02-25 16:33:07 +00:00
protected function fileIsCache()
2013-11-05 11:18:30 +00:00
{
$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)
{
2014-02-07 16:14:36 +00:00
if ( !empty($xml) ) {
2013-11-05 11:18:30 +00:00
$doc = new DOMDocument();
$load = $doc->loadXML($xml, LIBXML_NOERROR | LIBXML_NOWARNING);
2014-02-07 16:14:36 +00:00
if ( !$load ) {
2013-11-05 11:18:30 +00:00
$tmp = explode('-', $xml);
$errNum = intval($tmp[0]);
$errMsg = $tmp[1];
2014-02-07 16:14:36 +00:00
throw new Exception($errMsg, $errNum);
2013-11-05 11:18:30 +00:00
}
2014-02-07 16:14:36 +00:00
2013-11-05 11:18:30 +00:00
}
2014-02-07 16:14:36 +00:00
else {
throw new Exception('XML content is empty.');
2013-11-05 11:18:30 +00:00
}
}
/**
* Download file from URL
* @param string $url
* @param string $filename
* @throws Exception
* @return string
*/
protected function download($url, $filename)
{
2014-02-07 16:14:36 +00:00
$file = $this->config->storage->path . '/' . $filename;
2014-02-25 16:33:07 +00:00
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() );
2013-11-05 11:18:30 +00:00
}
}
/**
* 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') )
{
2014-02-07 16:14:36 +00:00
$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);
2013-11-05 11:18:30 +00:00
$commande->addChild('num_depot',$this->num_depot);
2014-01-16 10:47:25 +00:00
//Date de cloture au format dd/MM/yyyy
2013-11-05 11:18:30 +00:00
$commande->addChild('date_cloture', $this->date_cloture);
}
// Commande de documents : actes
elseif ( $this->type_document=='AC' )
{
2014-02-07 16:14:36 +00:00
$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);
2013-11-05 11:18:30 +00:00
$commande->addChild('num_depot',$this->num_depot);
$liste_actes = $commande->addChild('liste_actes');
$liste_actes->addChild('acte')->addAttribute('num', $this->num);
}
}
2014-02-07 16:14:36 +00:00
//Set Command ID
$commande->addChild('reference_client', $this->reference_client);
2013-11-05 11:18:30 +00:00
$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();
2014-02-07 16:14:36 +00:00
$req = $this->xml;
2013-11-05 11:18:30 +00:00
if ($this->debug) {
2014-01-16 10:47:25 +00:00
file_put_contents($this->type_document.'-'.$this->siren.'-'.$this->mode_diffusion.'.query', $this->xml);
2013-11-05 11:18:30 +00:00
}
//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);
2014-02-07 16:14:36 +00:00
if ($this->debug) {
file_put_contents($this->type_document.'-'.$this->siren.'-'.$this->mode_diffusion.'.response', $response);
}
2013-11-05 11:18:30 +00:00
return $response;
}
}