This commit is contained in:
Michael RICOIS 2016-11-29 16:00:07 +01:00
parent 525d25eccc
commit 7bdb5814f7

View File

@ -8,20 +8,31 @@ class Metier_Partenaires_MOrias
private $header = '';
private $cookie = '';
private $codeRetour = 0;
private $accesDist=true;
private $iDb;
public $enCache=false;
public $force=false;
public $annee=0;
public $erreur='';
/**
* Remote Flag
* @var boolean
*/
protected $remote = false;
public function __construct($accesDist = true, $db = null)
{
$this->accesDist = $accesDist;
/**
* SIREN
* @var string
*/
protected $siren;
/**
* Num ORIAS
* @var integer
*/
protected $num;
public function __construct($db = null)
{
if ($db === null) {
$this->iDb = new Metier_Util_Db();
} else {
@ -37,6 +48,12 @@ class Metier_Partenaires_MOrias
$this->remote = true;
}
/**
* Numero ORIAS
* @param unknown $annee
* @return string
* Retourne le numéro ORIAS
*/
public function getMaxOrias($annee = 0)
{
if ($annee*1 > 0 && $annee*1 < 100) {
@ -57,240 +74,263 @@ class Metier_Partenaires_MOrias
return sprintf('%08d', $ret[0]['numOrias']);
}
public function getInfosOrias($siren, $numOrias=0)
/**
* Infos ORIAS
* @param string $siren
* @param int $num
*/
public function getInfosOrias($siren, $num=0)
{
if ($siren*1 > 1000) {
$strWhere = "siren=$siren";
} elseif ($numOrias*1 > 0) {
$strWhere = "numOrias=$numOrias";
$this->siren = $siren;
$this->num = $num;
// Remote
if ($this->remote) {
$result = $this->getRemoteInfosOrias();
if ($result !== false && count($result) > 0) {
return $result;
}
}
// Database
if ($this->siren * 1 > 1000) {
$strWhere = "siren=$this->siren";
} elseif ($this->num * 1 > 0) {
$strWhere = "numOrias=$this->num";
} else {
return false;
}
$ret = $this->iDb->select('sdv1.orias', 'siren, id, nom, cp, ville, adresse, numOrias, numOriasAttribue',
$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);
if (count($ret) > 0) {
$tabRet = $ret[0];
$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;
$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;
}
return $tabRet;
}
protected function getRemoteInfosOrias()
{
// Initialisation Cookies
if ($this->num > 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',
if ($this->num > 0) {
$url = 'http://www.orias.fr/orias/public/index.html';
$post = array(
'menu%3AiasSearchForm'=>'menu%3AiasSearchForm',
'menu%3AiasSearchForm%3AiasInputText'=>sprintf('%08s', $this->num),
'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' => $this->num,
'numOriasAttribue' => 0,
'dateInsert' => date('YmdHis')
);
} 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);
}
$this->erreur = 'Numéro Orias inexistant';
// On enregistre ce numéro comme non attribué si < au dernier numéro attribué
if ($this->num < $this->getMaxOrias()) {
$this->iDb->insert('sdv1.orias', $tabInsert);
}
return false;
}
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));
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));
}
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;
}
// 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;
}
// 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);
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'] = $this->num;
$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;
}
}