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 * @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(''); $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->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 = ''. ''. ''. ''. ''.$req.''. ''. ''. ''; $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("", '', $response); $response = str_replace('','', $response); if ($this->debug) { file_put_contents($this->type_document.'-'.$this->siren.'-'.$this->mode_diffusion.'.response', $response); } return $response; } }