2015-01-20 11:07:27 +00:00
< ? php
2016-07-15 19:54:06 +02:00
require_once 'framework/common/curl.php' ;
2016-01-27 21:10:45 +00:00
class Metier_Partenaires_MOrias
2015-01-20 11:07:27 +00:00
{
2013-11-05 11:18:30 +00:00
private $referer = '' ;
private $body = '' ;
private $header = '' ;
private $cookie = '' ;
private $codeRetour = 0 ;
private $accesDist = true ;
private $iDb ;
2016-01-22 09:45:16 +00:00
2013-11-05 11:18:30 +00:00
public $enCache = false ;
public $force = false ;
public $annee = 0 ;
public $erreur = '' ;
2016-08-08 12:08:50 +02:00
public function __construct ( $accesDist = true , $db = null )
2015-01-20 11:07:27 +00:00
{
$this -> accesDist = $accesDist ;
2016-01-22 09:45:16 +00:00
2015-01-20 11:07:27 +00:00
if ( $db === null ) {
2016-07-18 15:21:48 +02:00
$this -> iDb = new Metier_Util_Db ();
2015-01-20 11:07:27 +00:00
} else {
$this -> iDb = $db ;
}
2013-11-05 11:18:30 +00:00
}
2016-08-08 12:08:50 +02:00
public function getMaxOrias ( $annee = 0 )
2015-01-20 11:07:27 +00:00
{
2016-08-08 12:08:50 +02:00
if ( $annee * 1 > 0 && $annee * 1 < 100 ) {
$this -> annee = '20' . $annee ;
2015-01-20 11:07:27 +00:00
$annee ++ ;
2016-08-08 12:08:50 +02:00
$strWhere = " (numOrias/1000000)<= $annee " ;
} elseif ( $annee * 1 > 1900 && $annee * 1 < 9999 ) {
$this -> annee = $annee ;
$annee = substr ( '' . $annee , 2 , 2 ) * 1 + 1 ;
$strWhere = " (numOrias/1000000)<= $annee " ;
2013-11-05 11:18:30 +00:00
} else {
2016-08-08 12:08:50 +02:00
$strWhere = '1' ;
$this -> annee = date ( 'Y' ) * 1 ;
2013-11-05 11:18:30 +00:00
}
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
$ret = $this -> iDb -> select ( 'sdv1.orias' , 'MAX(numOrias) AS numOrias' , $strWhere , false , MYSQL_ASSOC );
2013-11-05 11:18:30 +00:00
return sprintf ( '%08d' , $ret [ 0 ][ 'numOrias' ]);
}
2016-01-22 09:45:16 +00:00
2015-01-20 11:07:27 +00:00
public function getInfosOrias ( $siren , $numOrias = 0 )
{
2016-08-08 12:08:50 +02:00
if ( $siren * 1 > 1000 ) {
$strWhere = " siren= $siren " ;
}
elseif ( $numOrias * 1 > 0 ) {
$strWhere = " numOrias= $numOrias " ;
} else {
return false ;
}
$ret = $this -> iDb -> select ( 'sdv1.orias' , 'siren, id, nom, cp, ville, adresse, numOrias, numOriasAttribue' ,
$strWhere , false , MYSQL_ASSOC );
if ( ! $this -> force && count ( $ret ) > 0 ) {
$this -> enCache = true ;
$tabRet = $ret [ 0 ]; //array();
$ret = $this -> iDb -> select ( 'sdv1.orias' , 'categorie, cat, dateInscription, typeStatut, rcsVille, rcsCode, rcsSiren, contact, encaissement, nature, paysPresta, paysEtab' , $strWhere , false , MYSQL_ASSOC );
foreach ( $ret as $i => $tabTmp ) {
$tabRet [ 'categories' ][] = $tabTmp ;
}
}
elseif ( $this -> accesDist == true ) {
$this -> enCache = false ;
2013-11-05 11:18:30 +00:00
// Initialisation Cookies
2016-08-08 12:08:50 +02:00
if ( $numOrias > 0 ) {
$this -> referer = 'http://www.orias.fr/orias/public/index.jsp' ;
}
else {
$this -> referer = 'http://www.orias.fr/orias/public/rechercheavance.html' ;
}
$page = getUrl ( $this -> referer , '' , '' , '' , false , '' , '' , 15 );
if ( $page [ 'code' ] == 200 ) {
$this -> cookie = str_replace ( ' Path=/orias' , '' , $page [ 'header' ][ 'Set-Cookie' ]);
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/id="javax\.faces\.ViewState" value="(.*)"/Ui' , $page [ 'body' ], $matches ))
2016-08-08 12:08:50 +02:00
$this -> codeRetour = $matches [ 1 ];
2013-11-05 11:18:30 +00:00
else {
2016-07-12 16:45:26 +02:00
echo 'Erreur Url=' . $this -> referer . PHP_EOL ;
2013-11-05 11:18:30 +00:00
print_r ( $page [ 'body' ]);
return false ;
}
2016-08-08 12:08:50 +02:00
} else {
return false ;
}
2016-01-22 09:45:16 +00:00
2013-11-05 11:18:30 +00:00
if ( $numOrias > 0 ) {
2016-08-08 12:08:50 +02:00
$url = 'http://www.orias.fr/orias/public/index.html' ;
$post = array (
'menu%3AiasSearchForm' => 'menu%3AiasSearchForm' ,
'menu%3AiasSearchForm%3AiasInputText' => sprintf ( '%08s' , $numOrias ),
'javax.faces.ViewState' => $this -> codeRetour ,
'menu%3AiasSearchForm%3AiasSearch' => 'menu%3AiasSearchForm%3AiasSearch' ,
);
2013-11-05 11:18:30 +00:00
} else {
2015-01-20 11:07:27 +00:00
// Recherche avec Siren
2016-08-08 12:08:50 +02:00
$url = 'http://www.orias.fr/orias/public/rechercheavance.html' ;
$post = array (
'j_id_id45' => 'j_id_id45' ,
'j_id_id45%3Aj_id_id52' => '' ,
'j_id_id45%3AtagRCS' => '' ,
'j_id_id45%3AtagSIREN' => $siren , //441921574,
'j_id_id45%3Aj_id_id160' => '' ,
'j_id_id45%3Aj_id_id167' => '' ,
'j_id_id45%3Aj_id_id175' => '' ,
'javax.faces.ViewState' => $this -> codeRetour ,
'j_id_id45%3Aj_id_id203' => 'j_id_id45%3Aj_id_id203'
);
$this -> referer = 'http://www.orias.fr/orias/public/list.html' ;
2013-11-05 11:18:30 +00:00
}
2016-07-15 19:54:06 +02:00
sleep ( rand ( 1 , 2 ));
2016-08-08 12:08:50 +02:00
$page = getUrl ( $url , $this -> cookie , $post , $this -> referer , false , '' , '' , 15 );
$this -> referer = $url ;
if ( $page [ 'code' ] == 302 ) {
$url = $page [ 'header' ][ 'Location' ];
}
2013-11-05 11:18:30 +00:00
else {
if ( preg_match ( " /Le numéro d'immatriculation n'existe pas/ " , $page [ 'body' ])) {
2016-08-08 12:08:50 +02:00
$tabInsert = array (
'actif' => 0 ,
'numOrias' => $numOrias ,
'numOriasAttribue' => 0 ,
'dateInsert' => date ( 'YmdHis' )
);
$this -> erreur = 'Numéro Orias inexistant' ;
2013-11-05 11:18:30 +00:00
// On enregistre ce numéro comme non attribué si < au dernier numéro attribué
2016-08-08 12:08:50 +02:00
if ( $numOrias < $this -> getMaxOrias ()) {
$this -> iDb -> insert ( 'sdv1.orias' , $tabInsert );
}
2013-11-05 11:18:30 +00:00
}
return false ;
}
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
if ( $url == 'http://www.orias.fr/orias/public/intermediaire.html' ) {
$page = getUrl ( $url , $this -> cookie , '' , $this -> referer , false , '' , '' , 24 );
$this -> referer = $url ;
$this -> body = $page [ 'body' ];
2016-07-12 16:45:26 +02:00
echo " Recherche par Orias : url= $url " . PHP_EOL ;
2015-01-20 11:07:27 +00:00
die ( print_r ( $page ));
2016-08-08 12:08:50 +02:00
}
2016-01-22 09:45:16 +00:00
2013-11-05 11:18:30 +00:00
// Liste contenant l'entreprise
2016-08-08 12:08:50 +02:00
$url = 'http://www.orias.fr/orias/public/list.html' ;
2016-07-15 19:54:06 +02:00
sleep ( rand ( 1 , 2 ));
2016-08-08 12:08:50 +02:00
$page = getUrl ( $url , $this -> cookie , '' , $this -> referer , false , '' , '' , 24 );
$this -> referer = $url ;
$this -> body = $page [ 'body' ];
2016-01-22 09:45:16 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<td>R\é\;sultat\(s\) de votre recherche \:(?:.*)<strong>(.*)<(?:.*)Interm\é\;diaire\(s\)/Uis' , $this -> body , $matches )) {
2016-08-08 12:08:50 +02:00
$nbInter = trim ( $matches [ 1 ]) * 1 ;
if ( $nbInter == 0 ) {
return false ;
}
if ( $nbInter > 1 ) {
2013-11-05 11:18:30 +00:00
return false ;
}
}
2016-01-22 09:45:16 +00:00
2013-11-05 11:18:30 +00:00
// Initalisation du tableau retour
2016-08-08 12:08:50 +02:00
$tabRet = array ( 'siren' => $siren );
2016-01-22 09:45:16 +00:00
2013-11-05 11:18:30 +00:00
//id="formResult:intermediariesList:13910
2016-01-22 09:45:16 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<td class="dr-table-cell rich-table-cell haut " id="formResult\:intermediariesList\:(.*)\:j_id_id117">(?:.*)<center>(.*)<\/center>/Uis' , $this -> body , $matches )) {
2016-08-08 12:08:50 +02:00
$tabRet [ 'id' ] = trim ( $matches [ 1 ]);
$tabRet [ 'nom' ] = html_entity_decode ( trim ( $matches [ 2 ]));
2013-11-05 11:18:30 +00:00
}
if ( preg_match ( '/<td class="dr-table-cell rich-table-cell r.cp_ville " id="formResult\:intermediariesList\:(?:.*)\:j_id_id124">(?:.*)<center>(.*)<\/center>/Uis' , $this -> body , $matches )) {
2016-08-08 12:08:50 +02:00
$tabTmp = explode ( ' ' , $matches [ 1 ]);
$tabRet [ 'cp' ] = trim ( $tabTmp [ 0 ]);
$tabRet [ 'ville' ] = html_entity_decode ( trim ( $tabTmp [ 1 ]));
2013-11-05 11:18:30 +00:00
}
if ( preg_match ( '/<td class="dr-table-cell rich-table-cell r.adresse " id="formResult\:intermediariesList\:(?:.*)\:j_id_id133">(.*)<\/td>/Uis' , $this -> body , $matches ))
2016-08-08 12:08:50 +02:00
$tabRet [ 'adresse' ] = html_entity_decode ( trim ( $matches [ 1 ]));
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
$tabRet [ 'actif' ] = 1 ;
$tabRet [ 'numOriasAttribue' ] = 1 ;
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<td class="dr-table-cell rich-table-cell r.immat " id="formResult\:intermediariesList\:(?:.*)\:j_id_id138">(?:.*|)(<span style="white-space\: no-wrap;">|Radi\é\; le \:)(.*)(?:<\/span>|<\/td>)/Uis' , $this -> body , $matches )) {
2016-08-08 12:08:50 +02:00
$str = trim ( strtr ( $matches [ 2 ], array ( ' ' => '' , ' ' => '' )));
if ( html_entity_decode ( trim ( str_replace ( ':' , '' , $matches [ 1 ]))) == 'Radié le' ) {
$tabRet [ 'actif' ] = 0 ;
$tabRet [ 'numOrias' ] = $numOrias ;
$tabRet [ 'dateRadiation' ] = $str ;
}
else {
$tabRet [ 'numOrias' ] = $str ;
2013-11-05 11:18:30 +00:00
}
}
2016-08-08 12:08:50 +02:00
if ( preg_match ( '/ id="javax\.faces\.ViewState" value="(.*)" /Uis' , $this -> body , $matches )) {
$this -> codeRetour = $matches [ 1 ];
}
2013-11-05 11:18:30 +00:00
2016-08-08 12:08:50 +02:00
$tabTmp = explode ( " <a href= \" # \" onclick= \" if(typeof jsfcljs == 'function') { jsfcljs(document.getElementById('formResult'), { ' " , $this -> body );
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
foreach ( $tabTmp as $i => $strCat ) {
if ( $i == 0 ) {
if ( count ( $tabTmp ) == 1 ) {
$tabInsert = array_merge ( $tabRet , array ( 'dateInsert' => date ( 'YmdHis' )));
2015-01-20 11:07:27 +00:00
$this -> iDb -> insert ( 'sdv1.orias' , $tabInsert );
2013-11-05 11:18:30 +00:00
}
2016-08-08 12:08:50 +02:00
$tabRet [ 'categories' ] = array ();
2013-11-05 11:18:30 +00:00
continue ;
}
2016-08-08 12:08:50 +02:00
$tabCat = array ();
if ( preg_match ( '/<span style="text-align\: left">(.*)<\/span>/Ui' , $strCat , $matches )) {
$tabCat [ 'categorie' ] = html_entity_decode ( trim ( $matches [ 1 ]));
}
if ( preg_match ( '/intermediaryRoleIdParam\'\:\'(.*)\'/Uis' , $strCat , $matches )) {
$tabCat [ 'cat' ] = $matches [ 1 ];
}
// Tout est dans la même page Html
if ( $i == 1 ) {
2013-11-05 11:18:30 +00:00
// Construction du lien vers la fiche d'identité de l'entreprise
2016-08-08 12:08:50 +02:00
$url = 'http://www.orias.fr/orias/public/list.html' ;
$post = array (
'formResult' => 'formResult' ,
'formResult%3AajaxLoadingModalBoxOpenedState' => '' ,
'javax.faces.ViewState' => $this -> codeRetour ,
'intermediaryIdParam' => $tabRet [ 'id' ],
'intermediaryRoleIdParam' => $tabCat [ 'cat' ],
);
if ( preg_match ( '/(formResult\:intermediariesList\:(?:.*)),intermediaryIdParam,/Ui' , $strCat , $matches )) {
$tabTmp2 = explode ( ',' , $matches [ 1 ]);
$post [ urlencode ( $tabTmp2 [ 0 ])] = urlencode ( $tabTmp2 [ 1 ]);
2013-11-05 11:18:30 +00:00
}
2016-07-15 19:54:06 +02:00
sleep ( rand ( 1 , 2 ));
2016-08-08 12:08:50 +02:00
$page = getUrl ( $url , $this -> cookie , $post , $this -> referer , false , '' , '' , 24 );
$tmpCat = explode ( 'class="dr-tbpnl-cntnt-pstn rich-tabpanel-content-position"' , $page [ 'body' ]);
2013-11-05 11:18:30 +00:00
}
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
if ( preg_match ( '/<dd class="col3">Date d\'inscription \: <strong>(.*)<\/strong><\/dd>/Uis' , $tmpCat [ $i ], $matches )) {
$tabCat [ 'dateInscription' ] = trim ( $matches [ 1 ]);
}
if ( preg_match ( '/<td align="right"(?: |)>Statut \:<\/td>(.*)<\/strong>/Uis' , $tmpCat [ $i ], $matches )) {
$tabCat [ 'typeStatut' ] = trim ( strip_tags ( $matches [ 1 ]));
}
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<td align="right">RCS \:<\/td>(?:.*)<td width="5%"><\/td>(?:.*)<td width="70%"><strong>(.*)<\/strong>(.*)SIREN \:(?:.*)<strong>(.*)<\/strong>/Uis' , $tmpCat [ $i ], $matches )) {
2016-08-08 12:08:50 +02:00
$tabCat [ 'rcsVille' ] = trim ( $matches [ 1 ]);
$tabCat [ 'rcsCode' ] = trim ( strtr ( strip_tags ( $matches [ 2 ]), array ( ' ' => '' , ' ' => '' , chr ( 160 ) => '' , " \r " => '' , " \n " => '' )));
$tabCat [ 'rcsSiren' ] = trim ( $matches [ 3 ]);
if ( $siren == 0 ) {
$tabRet [ 'siren' ] = $tabCat [ 'rcsSiren' ];
}
2015-01-20 11:07:27 +00:00
}
2016-08-08 12:08:50 +02:00
if ( preg_match ( '/<td align="right">Contact<\/td>(.*)<\/tr>/Uis' , $tmpCat [ $i ], $matches )) {
$tabCat [ 'contact' ] = html_entity_decode ( trim ( strip_tags ( $matches [ 1 ])));
2013-11-05 11:18:30 +00:00
}
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
if ( preg_match ( " /<strong>(.*)Cet intermédiaire n'est pas autorisé à encaisser les primes ou cotisations d'assurances(.*)< \ /strong>/Uis " , $tmpCat [ $i ])) {
$tabCat [ 'encaissement' ] = 0 ;
}
else {
$tabCat [ 'encaissement' ] = 1 ;
}
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
if ( preg_match ( '/<span class="txt_ssTitre">(?:.*)Nature de l\'activit\é\; d\'interm\é\;diation(.*)<\/dd>/Uis' , $tmpCat [ $i ], $matches )) {
$tabCat [ 'nature' ] = html_entity_decode ( trim ( strip_tags ( $matches [ 1 ])));
}
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
if ( preg_match ( '/<td width="50%" valign="top" align="left">\- Etats dans lesquels cet interm\é\;diaire exerce en libre prestation de service(.*)<\/table>(.*)<\/tr>/Uis' , $tmpCat [ $i ], $matches )) {
$tabCat [ 'paysPresta' ] = trim ( strip_tags ( $matches [ 1 ]));
}
if ( preg_match ( '/<td width="222" align="left">\- Etats dans lesquels cet interm\é\;diaire exerce en libert\é\; d\'\é\;tablissement(.*)<\/table>(.*)<\/tr>/Uis' , $tmpCat [ $i ], $matches )) {
$tabCat [ 'paysEtab' ] = trim ( strip_tags ( $matches [ 1 ]));
}
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
$tabRet [ 'categories' ][] = $tabCat ;
2016-01-22 09:45:16 +00:00
2016-08-08 12:08:50 +02:00
$tabInsert = $tabRet ;
2013-11-05 11:18:30 +00:00
unset ( $tabInsert [ 'categories' ]);
2016-08-08 12:08:50 +02:00
$tabInsert = array_merge ( $tabInsert , $tabCat , array ( 'dateInsert' => date ( 'YmdHis' )));
2015-01-20 11:07:27 +00:00
$this -> iDb -> insert ( 'sdv1.orias' , $tabInsert );
2013-11-05 11:18:30 +00:00
}
2015-01-20 11:07:27 +00:00
}
2013-11-05 11:18:30 +00:00
return $tabRet ;
}
}