backoffice/includes/partenaires/classMOrias.php
2011-06-21 13:28:10 +00:00

278 lines
12 KiB
PHP
Raw Permalink Blame History

<?
class MOrias {
private $referer ='';
private $body = '';
private $header = '';
private $cookie = '';
private $codeRetour = 0;
private $accesDist=true;
private $iDb;
public $enCache=false;
public $force=false;
public $annee=0;
public $erreur='';
function __construct($accesDist=true) {// $adresse, $cp, $ville, $pays='France') {
$this->accesDist=$accesDist;
$this->iDb=new WDb('sdv1');
}
public function getMaxOrias($annee=0) {
if ($annee*1>0 && $annee*1<100) {
$this->annee='20'.$annee;
$annee++;
$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";
} else {
$strWhere='1';
$this->annee=date('Y')*1;
}
$ret=$this->iDb->select('orias','MAX(numOrias) AS numOrias', $strWhere, false, MYSQL_ASSOC);
//die($strWhere);
return sprintf('%08d',$ret[0]['numOrias']);
}
public function getInfosOrias($siren, $numOrias=0) {
//die('numOrias 1:"'.$numOrias.'"');
if ($siren*1>1000)
$strWhere="siren=$siren";
elseif ($numOrias*1>0) {
// $numOrias=preg_replace('/[^0-9]/','',$numOrias);
// $numOrias=substr($numOrias,0,2).' '.substr($numOrias,2,3).' '.substr($numOrias,5,3);
$strWhere="numOrias=$numOrias";
// die($strWhere);
//die('numOrias 2:"'.$numOrias.'"');
} else return false;
/* echo "=================================================================================================".EOL;
echo "==== SELECT siren, id, nom, cp, ville, adresse, numOrias FROM orias WHERE $strWhere;".EOL;
echo "=================================================================================================".EOL;
*/
$ret=$this->iDb->select('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('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;
//die('numOrias 3:"'.$numOrias.'"');
} elseif ($this->accesDist==true) {
$this->enCache=false;
// Initialisation Cookies
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);
//print_r($page);
if ($page['code']==200) {
$this->cookie=str_replace(' Path=/orias', '',$page['header']['Set-Cookie']);
if(preg_match('/id="javax\.faces\.ViewState" value="(.*)"/Ui', $page['body'], $matches))
$this->codeRetour=$matches[1];
else {
echo 'Erreur Url='.$this->referer.EOL;
print_r($page['body']);
return false;
}
} else return false;
if ($numOrias>0) {
$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',
);
} else {
// Recherche avec Siren
$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';
}
randsleep(1,2);
$page=getUrl($url, $this->cookie, $post, $this->referer, false, '', '', 15);
$this->referer=$url;
if ($page['code']==302) $url=$page['header']['Location'];
else {
if (preg_match("/Le num&eacute;ro d'immatriculation n'existe pas/", $page['body'])) {
$tabInsert=array( 'actif'=>0,
'numOrias'=>$numOrias,
'numOriasAttribue'=>0,
'dateInsert'=>date('YmdHis'));
$this->erreur='Num<75>ro Orias inexistant';
// On enregistre ce num<75>ro comme non attribu<62> si < au dernier num<75>ro attribu<62>
if ($numOrias<$this->getMaxOrias())
$this->iDb->insert('orias', $tabInsert);
}
return false;
}
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'];
echo "Recherche par Orias : url=$url".EOL;
die(print_r($page));
} /* else { */
// Liste contenant l'entreprise
$url='http://www.orias.fr/orias/public/list.html';
randsleep(1,2);
$page=getUrl($url, $this->cookie, '', $this->referer, false, '', '', 24);
$this->referer=$url;
$this->body=$page['body'];
if (preg_match('/<td>R\&eacute\;sultat\(s\) de votre recherche \:(?:.*)<strong>(.*)<(?:.*)Interm\&eacute\;diaire\(s\)/Uis',$this->body, $matches)) {
$nbInter=trim($matches[1])*1;
if ($nbInter==0) return false;
if ($nbInter>1) {
@sendMail('production@scores-decisions.com', 'ylenaour@scores-decisions.com', "classMOrias::getInfosOrias sur $siren", "Plus de 1 interm<72>diaire recens<6E>... Merci de g<>rer le cas !");
return false;
}
}
// Initalisation du tableau retour
$tabRet=array('siren'=>$siren);
//id="formResult:intermediariesList:13910
if (preg_match('/<td class="dr-table-cell rich-table-cell haut " id="formResult\:intermediariesList\:(.*)\:j_id_id117">(?:.*)<center>(.*)<\/center>/Uis',$this->body, $matches)) {
$tabRet['id']=trim($matches[1]);
$tabRet['nom']=html_entity_decode(trim($matches[2]));
}
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)) {
$tabTmp=explode('&nbsp;',$matches[1]);
$tabRet['cp']=trim($tabTmp[0]);
$tabRet['ville']=html_entity_decode(trim($tabTmp[1]));
//$tabRet['cpVille']=explotrim(str_replace('&nbsp;',' ',$matches[1]));
}
if (preg_match('/<td class="dr-table-cell rich-table-cell r.adresse " id="formResult\:intermediariesList\:(?:.*)\:j_id_id133">(.*)<\/td>/Uis',$this->body, $matches))
$tabRet['adresse']=html_entity_decode(trim($matches[1]));
$tabRet['actif']=1;
$tabRet['numOriasAttribue']=1;
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\&eacute\; le \:)(.*)(?:<\/span>|<\/td>)/Uis',$this->body, $matches)) {
$str=trim(strtr($matches[2],array('&nbsp;'=>'',' '=>'')));
if (html_entity_decode(trim(str_replace(':','', $matches[1])))=='Radi<64> le') {
$tabRet['actif']=0;
$tabRet['numOrias']=$numOrias;
$tabRet['dateRadiation']=$str;
}
else
$tabRet['numOrias']=$str;
}
if (preg_match('/ id="javax\.faces\.ViewState" value="(.*)" /Uis',$this->body, $matches))
$this->codeRetour=$matches[1];
$tabTmp=explode("<a href=\"#\" onclick=\"if(typeof jsfcljs == 'function'){jsfcljs(document.forms['formResult'],'", $this->body);
foreach ($tabTmp as $i=>$strCat) {
if ($i==0) {
if (count($tabTmp)==1) {
$tabInsert=array_merge($tabRet, array('dateInsert'=>date('YmdHis')));
//print_r($tabInsert);
$this->iDb->insert('orias', $tabInsert);
}
$tabRet['categories']=array();
continue;
}
$tabCat=array();
//echo "$i:$strCat";
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];
//print_r($tabCat);
if ($i==1) { // Tout est dans la m<>me page Html
// Construction du lien vers la fiche d'identit<69> de l'entreprise
$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]);
/* echo "$i";
print_r($tabCat);
print_r($matches);*/
}
randsleep(1,2);
$page=getUrl($url, $this->cookie, $post, $this->referer, false, '', '', 24);
//$this->referer=$url;
$tmpCat=explode('class="dr-tbpnl-cntnt-pstn rich-tabpanel-content-position"', $page['body']);
//die($this->body);
}
if (preg_match('/<dd class="col3">Date d\'inscription \: <strong>(.*)<\/strong><\/dd>/Uis',$tmpCat[$i], $matches))
$tabCat['dateInscription']=/*Wdate::dateT('d/m/y', 'Y-m-d', */trim($matches[1]);//);
if (preg_match('/<td align="right"(?: |)>Statut \:<\/td>(.*)<\/strong>/Uis',$tmpCat[$i], $matches))
$tabCat['typeStatut']=trim(strip_tags($matches[1]));
if (preg_match('/<td align="right">RCS \:<\/td>(?:.*)<td width="5%"><\/td>(?:.*)<td width="70%"><strong>(.*)<\/strong>(.*)SIREN \:(?:.*)<strong>(.*)<\/strong>/Uis',$tmpCat[$i], $matches)) {
$tabCat['rcsVille']=trim($matches[1]);
$tabCat['rcsCode']=trim(strtr(strip_tags($matches[2]), array(' '=>'','&nbsp;'=>'',chr(160)=>'',"\r"=>'',"\n"=>'')));
$tabCat['rcsSiren']=trim($matches[3]);
if ($siren==0) $tabRet['siren']=$tabCat['rcsSiren'];
}
/*<td align="right">Contact</td>
<td></td>
<td><strong> Fran&ccedil;ois Meunier
</strong></td>
*/
if (preg_match('/<td align="right">Contact<\/td>(.*)<\/tr>/Uis',$tmpCat[$i], $matches)) {
$tabCat['contact']=html_entity_decode(trim(strip_tags($matches[1])));
/* <td></td>
<td>
<strong>
JEAN-PIERRE CHAUSSAT
</strong>
</td>
</table>*/
}
//print_r($tmpCat[$i]);
if (preg_match("/<strong>(.*)Cet interm&eacute;diaire n'est pas autoris&eacute; &agrave; encaisser les primes ou cotisations d'assurances(.*)<\/strong>/Uis", $tmpCat[$i]))
$tabCat['encaissement']=0;
else
$tabCat['encaissement']=1;
if (preg_match('/<span class="txt_ssTitre">(?:.*)Nature de l\'activit\&eacute\; d\'interm\&eacute\;diation(.*)<\/dd>/Uis',$tmpCat[$i], $matches))
$tabCat['nature']=html_entity_decode(trim(strip_tags($matches[1])));
if (preg_match('/<td width="50%" valign="top" align="left">\- Etats dans lesquels cet interm\&eacute\;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\&eacute\;diaire exerce en libert\&eacute\; d\'\&eacute\;tablissement(.*)<\/table>(.*)<\/tr>/Uis',$tmpCat[$i], $matches))
$tabCat['paysEtab']=trim(strip_tags($matches[1]));
$tabRet['categories'][]=$tabCat;
$tabInsert=$tabRet;
unset($tabInsert['categories']);
$tabInsert=array_merge($tabInsert, $tabCat, array('dateInsert'=>date('YmdHis')));
//print_r($tabInsert);
$this->iDb->insert('orias', $tabInsert);
//echo $tmpCat[$i];
}
}
return $tabRet;
}
}
?>