Michael RICOIS bff74bde19 Code format
2016-08-08 12:08:50 +02:00

292 lines
11 KiB
PHP

<?php
require_once 'framework/common/curl.php';
class Metier_Partenaires_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='';
public function __construct($accesDist = true, $db = null)
{
$this->accesDist = $accesDist;
if ( $db === null ) {
$this->iDb = new Metier_Util_Db();
} else {
$this->iDb = $db;
}
}
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('sdv1.orias','MAX(numOrias) AS numOrias', $strWhere, false, MYSQL_ASSOC);
return sprintf('%08d',$ret[0]['numOrias']);
}
public function getInfosOrias($siren, $numOrias=0)
{
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;
// 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);
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.PHP_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';
}
sleep(rand(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éro Orias inexistant';
// On enregistre ce numéro comme non attribué si < au dernier numéro attribué
if ($numOrias < $this->getMaxOrias()) {
$this->iDb->insert('sdv1.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".PHP_EOL;
die(print_r($page));
}
// Liste contenant l'entreprise
$url = 'http://www.orias.fr/orias/public/list.html';
sleep(rand(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) {
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]));
}
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é 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.getElementById('formResult'),{'", $this->body);
foreach ($tabTmp as $i => $strCat) {
if ($i == 0) {
if (count($tabTmp) == 1) {
$tabInsert = array_merge($tabRet, array('dateInsert'=>date('YmdHis')));
$this->iDb->insert('sdv1.orias', $tabInsert);
}
$tabRet['categories'] = array();
continue;
}
$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) {
// Construction du lien vers la fiche d'identité 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]);
}
sleep(rand(1, 2));
$page = getUrl($url, $this->cookie, $post, $this->referer, false, '', '', 24);
$tmpCat = explode('class="dr-tbpnl-cntnt-pstn rich-tabpanel-content-position"', $page['body']);
}
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]));
}
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'];
}
}
if (preg_match('/<td align="right">Contact<\/td>(.*)<\/tr>/Uis', $tmpCat[$i], $matches)) {
$tabCat['contact'] = html_entity_decode(trim(strip_tags($matches[1])));
}
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')));
$this->iDb->insert('sdv1.orias', $tabInsert);
}
}
return $tabRet;
}
}