config = $c->profil->infogreffe; if ( null === $this->config ) { throw new Exception('Unable to load configuration file.'); } } public function callRequest() { $fromCache = false; if ( $this->mode_diffusion == 'XL' && $this->fileIsCache($name) ){ $fromCache = true; } if ($fromCache) { $xml = $this->fileFromCache(); } else { $xml = $this->getProduitsXML(); $this->error($xml); } if ( $this->mode_diffusion == 'XL' ){ $this->fileTocache($xml); } $this->error($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); } /** * * @param string $xml * @return boolean */ protected function fileIsCache($xml) { $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]; if( $errNum == '5' ){ $errMsg = 'Service partenaire indisponible.'; } throw new Exception($errNum . '-' . $errMsg); } } else { throw new Exception('Fichier vide'); } } /** * Download file from URL * @param string $url * @param string $filename * @throws Exception * @return string */ protected function download($url, $filename) { $file = $file = $this->config->storage->path . DIRECTORY_SEPARATOR . $filename; $fp = fopen($file, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); if( curl_errno($ch) ) { throw new Exception( curl_error($ch) ); } curl_close($ch); fclose($fp); return $file; } /** * 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); //Set Command ID $emetteur->addChild('reference_client', $this->reference_client); $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') ) { $commande->addChild('greffe',$this->greffe); $commande->addChild('dossier_millesime',$this->dossier_millesime); $commande->addChild('dossier_statut',$this->dossier_statut); $commande->addChild('dossier_chrono',$this->dossier_chrono); $commande->addChild('num_depot',$this->num_depot); $commande->addChild('date_cloture', $this->date_cloture); } // Commande de documents : actes elseif ( $this->type_document=='AC' ) { $commande->addChild('greffe',$this->greffe); $commande->addChild('dossier_millesime',$this->dossier_millesime); $commande->addChild('dossier_statut',$this->dossier_statut); $commande->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); } } $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(); //Be sure it's in UTF-8 $req = utf8_encode($this->xml); if ($this->debug) { file_put_contents($this->type_document.'-'.$this->siren.'-'.$this->mode_diffusion.'.query'); } //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); return $response; } }