Sépration du code récupération des données à distances

This commit is contained in:
Michael RICOIS 2016-11-16 17:49:21 +01:00
parent 8f6cf506de
commit e6a84a4664

View File

@ -11,263 +11,300 @@ class Metier_Partenaires_MArtisanat
public $libErreur='';
public $cookie='';
public $iDb;
public $enCache=false;
function __construct() {
$this->iDb=new Metier_Util_Db();
protected $remote = false;
public function __construct()
{
$this->iDb = new Metier_Util_Db();
}
function getIdentite($siren, $refresh=false) {
$siren=$siren*1;
$res=$this->iDb->select('jo.artisanat', 'id, siren, actif, numRM, denomination, sigle, nomCommercial, enseigne, fj, effectif, aprm, debutActivite, activite, adresse, cp, ville, cessation, radiation, nbInscriptions, nom, prenom, nomUsage, dateNaiss, lieuNaiss, natio, qualite, qualif, dateQualif, dateFctDeb, dateFctFin, IF(dateInsert>dateUpdate,dateInsert,dateUpdate) AS dateUpdate', "siren=$siren", false, MYSQL_ASSOC);
if (count($res)>0 && !$refresh) {
$this->enCache=true;
$tabInsert=$res[0];
/**
* Active la récupération des données distantes
*/
public function setRemote()
{
$this->remote = true;
}
public function getIdentite($siren)
{
$siren = $siren * 1;
$res = $this->iDb->select('jo.artisanat', 'id, siren, actif, numRM, denomination, sigle, nomCommercial, enseigne, fj, effectif, aprm, debutActivite, activite, adresse, cp, ville, cessation, radiation, nbInscriptions, nom, prenom, nomUsage, dateNaiss, lieuNaiss, natio, qualite, qualif, dateQualif, dateFctDeb, dateFctFin, IF(dateInsert>dateUpdate,dateInsert,dateUpdate) AS dateUpdate', "siren=$siren", false, MYSQL_ASSOC);
if (count($res)>0 && !$this->remote) {
$tabInsert = $res[0];
} elseif (ARTISANAT_DISPO_WEB) {
$this->enCache=false;
$url='http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=recherche';
$page=getUrl($url, '', '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200a !';
return false;
}
$this->cookie=$page['header']['Set-Cookie'];
$this->referer=$url;
$url='http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=cherche';
$postData=array( 'siren_op'=>'%3D',
'siren'=>$siren,
'den_rent_op'=>'%25',
'den_rent'=>'',
'adresse_op'=>'contient',
'adresse'=>'',
'bde_rent_op'=>'%25',
'bde_rent'=>'',
'nom_rdir_op'=>'%25',
'nom_rdir'=>'',
'pren_rdir_op'=>'%25',
'pren_rdir'=>'',
'table'=>'rentreprise',//'rentrad',
'x'=>56,
'y'=>14,
);
sleep(rand(1, 2));
$page=getUrl($url, $this->cookie, $postData, $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200b !';
return false;
}
$this->referer=$url;
if (preg_match('/<span.class="rouge">aucune.entreprise.ne.correspond(?:.*)vos.crit(?:.*)de.recherche<\/span>/Uis', $this->body, $matches)) {
$url='http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=cherche';
$postData=array( 'siren_op'=>'%3D',
'siren'=>$siren,
'den_rent_op'=>'%25',
'den_rent'=>'',
'adresse_op'=>'contient',
'adresse'=>'',
'bde_rent_op'=>'%25',
'bde_rent'=>'',
'nom_rdir_op'=>'%25',
'nom_rdir'=>'',
'pren_rdir_op'=>'%25',
'pren_rdir'=>'',
'table'=>'rentrad',
'x'=>56,
'y'=>14,
);
sleep(rand(1, 2));
$page=getUrl($url, $this->cookie, $postData, $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200c !';
return false;
}
$this->referer=$url;
}
/** Gestion des multi-inscriptions au RM **/
if (preg_match('/<b class="gris">(.*)entreprises.correspondent(?:.*)vos.crit(?:.*)de.recherche<\/span>/Uis', $this->body, $matches))
{
$nbRep=trim($matches[1])*1;
$iRadMax=-1;
if (preg_match_all('/<a class="turquoise" href="(rm\.php\?dom=Gerer&amp;fonction=dossier&amp;(.*))">/Uis', $this->body, $matches)) {
$tabUrls=array_unique($matches[1]);
$anRadMax=0;
// Recherche de la dernière inscription
foreach ($tabUrls as $iUrl=>$url) {
// On prend la dernière inscription
$tabTmp=explode('&amp;',$url);
if (substr($tabTmp[3],0,8)=='an_rera=') {
$anRad=substr($tabTmp[3],8);
if ($anRad>$anRadMax) {
$anRadMax=$anRad;
$iRadMax=$iUrl;
}
}
}
}
if ($iRadMax==-1) die('$iRadMax==-1 Cas impossible !'.PHP_EOL.print_r($tabUrls,true).PHP_EOL.print_r($matches,true));
// Accès à la dernière fiche
$url='http://www.cma-paris.fr/CMP/'.strtr(trim($tabUrls[$iRadMax]),array('&amp;'=>'&'));
sleep(rand(1, 2));
$page=getUrl($url, $this->cookie, '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200c !';
return false;
}
$this->referer=$url;
}
$tabInsert=array();
if (preg_match('/n&deg; d\'identification \(SIREN\)<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)&nbsp;<\/td>((?:.*)n&deg; de gestion rm<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>)?/Uis', $this->body, $matches)) {
$sirenLu=$matches[1]*1;
if ($siren==$sirenLu) {
$tabInsert['siren']=$matches[1];
$tabInsert['numRM']=@$matches[2];
} else {
$this->libErreur='Erreur Siren $sirenLu<>$siren !';
return false;
}
} elseif (preg_match('/<span class="rouge">aucune entreprise ne correspond &agrave; vos crit&egrave;res de recherche<\/span>/Uis', $this->body, $matches)) {
$this->libErreur='Erreur Artisan absent de la base Artisanat !';
// die($this->body);
return false;
} else {
$this->libErreur='Erreur Siren absent dans la page !';
return false;
}
$actif=null;
if (preg_match("/Renseignements.relatif(?:.*)entreprise(.*)<\/b>/Uis", $this->body, $matches)) {
switch (trim(strtr($matches[1],array(chr(160)=>'')))) {
case 'en activit&eacute;': $actif=1; break;
case 'radi&eacute;e': $actif=0; break;
default: print_r($matches);die(); break;
}
//die(PHP_EOL.'$actif='.$actif.PHP_EOL.print_r($matches));
}
$tabInsert['actif']=$actif;
if ($actif===null) {
//die("siren=$siren".PHP_EOL."sirenLu=$sirenLu".PHP_EOL.$this->body);
$this->libErreur='Erreur Actif/Radié non trouvé !';
return false;
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;nomination<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['denomination']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;nomination<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['denomination']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">sigle<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['sigle']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;nom&nbsp;commercial<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['nomCommercial']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">enseigne<\/span><\/td>(?:.*)<td align="left" colspan="4">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['enseigne']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">forme&nbsp;juridique<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['fj']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">effectif<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['effectif']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">code APRM<\/span><\/td>(?:.*)<td align="left">(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['aprm']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;but&nbsp;d\'activit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['debutActivite']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;activit&eacute;\(s\) exerc&eacute;e\(s\) donnant lieu &agrave; immatriculation<\/span>(?:.*)<td colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['activite']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">adresse&nbsp;de&nbsp;l\'entreprise<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['adresse']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">code postal<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['cp']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">ville<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['ville']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">cessation d\'activit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['cessation']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">radiation du RM<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['radiation']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
$tabEtabs=array();
if (preg_match('/<!--deb Etablissements secondaires-->(?:.*)<b class="gris">Aucune inscription compl&eacute;mentaire<\/b>(?:.*)<!--fin Etablissements secondaires-->/Uis', $this->body, $matches))
$tabInsert['nbInscriptions']=0;
elseif (preg_match('/<!--deb Etablissements secondaires-->(?:.*)<b class="gris">Inscription compl&eacute;mentaire<\/b>(.*)<!--fin Etablissements secondaires-->/Uis', $this->body, $matches)) {
$tabInsert['nbInscriptions']=0;
if (preg_match_all('/<tr>(?:.*)<td><img alt="" src="inter\/pixtrans\.gif" width="30" height="1"><\/td>(?:.*)<td nowrap>(?:.*)<!-- adresse -->(.*)<\/td>(?:.*)<td nowrap>(?:.*)<!-- code postal et ville -->(.*)<\/td>(?:.*)<td nowrap>(?:.*)<!-- enseigne -->(.*)<\/td>(?:.*)<td>(?:.*)<!-- debut activite -->(.*)<\/td>(?:.*)<td>(?:.*)<!-- fin activite -->(.*)<\/td>(?:.*)<\/tr>/Uis', $matches[1], $matches2)) {
foreach ($matches2[1] as $i=>$adresse) {
$tabEtabs[$i]['siren'] =$siren;
$tabEtabs[$i]['num'] =$i;
$tabEtabs[$i]['adresse'] =trim(strtr(html_entity_decode($adresse),chr(160),' '));
$tabEtabs[$i]['cpVille'] =trim(strtr(html_entity_decode($matches2[2][$i]),chr(160),' '));
$tabEtabs[$i]['enseigne']=trim(strtr(html_entity_decode($matches2[3][$i]),chr(160),' '));
$tabEtabs[$i]['actDeb'] =Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches2[4][$i]),chr(160),' ')));
$tabEtabs[$i]['actFin'] =Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches2[5][$i]),chr(160),' ')));
$tabInsert['nbInscriptions']++;
}
}
}
/** Informations sur la Personne Physique **/
if (preg_match('/<a class="turquoise" href="(rm\.php\?dom=Gerer&amp;fonction=dirigeant&amp;dept_re(.*))" onclick="window\.open\(this\.href/Uis', $this->body, $matches)) {
$urlDir='http://www.cma-paris.fr/CMP/'.strtr(trim($matches[1]),array('&amp;'=>'&'));
//die($urlDir);
sleep(rand(1, 2));
$page=getUrl($urlDir, $this->cookie, '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200d !';
return false;
}
$this->referer=$url;
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nom<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['nom']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">pr&eacute;nom<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['prenom']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nom d\'usage<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['nomUsage']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"(?:.*)<span class="turquoise">&nbsp;date&nbsp;de&nbsp;naissance<\/span><\/td>(?:.*)-->(?:.*)<td(?: +)align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['dateNaiss']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
// if (preg_match('/date&nbsp;de&nbsp;naissance(.*)<td background="inter\/pix3_turquoise\.gif" align="right"/Uis', $this->body, $matches))
// die(print_r($matches));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;lieu&nbsp;de&nbsp;naissance<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['lieuNaiss']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nationalit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['natio']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">qualit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['qualite']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right" nowrap><span class="turquoise">qualification&nbsp;artisanale<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['qualif']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">date&nbsp;d\'obtention<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['datequalif']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">prise de fonction<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['dateFctDeb']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">fin de fonction<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['dateFctFin']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
// Insertion en base de données
$tabInsert['dateInsert']=date('Y-m-d H:i:s');
$id=$this->iDb->insert('jo.artisanat', $tabInsert, true);
$tabInsert['id']=$id;
foreach ($tabEtabs as $tabInsert2) {
$tabInsert2['dateInsert']=$tabInsert['dateInsert'];
$id2=$this->iDb->insert('jo.artisanat_etab', $tabInsert2, true);
}
$tabInsert['dateUpdate']=substr($tabInsert['dateInsert'],0,10);
unset($tabInsert['dateInsert']);
$tabInsert = $this->getRemoteIdentite();
}
return $tabInsert;
}
}
?>
public function getRemoteIdentite()
{
$url = 'http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=recherche';
$page = getUrl($url, '', '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body = $page['body'];
$this->codeRetour = $page['code'];
if ($this->codeRetour != '200') {
$this->libErreur = 'Erreur Artisanat 200a !';
return false;
}
$this->cookie = $page['header']['Set-Cookie'];
$this->referer = $url;
$url = 'http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=cherche';
$postData = array(
'siren_op' => '%3D',
'siren' => $siren,
'den_rent_op' => '%25',
'den_rent' => '',
'adresse_op' => 'contient',
'adresse' => '',
'bde_rent_op' => '%25',
'bde_rent' => '',
'nom_rdir_op' => '%25',
'nom_rdir' => '',
'pren_rdir_op' => '%25',
'pren_rdir' => '',
'table' => 'rentreprise',//'rentrad',
'x' => 56,
'y' => 14,
);
sleep(rand(1, 2));
$page = getUrl($url, $this->cookie, $postData, $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body = $page['body'];
$this->codeRetour = $page['code'];
if ($this->codeRetour != '200') {
$this->libErreur = 'Erreur Artisanat 200b !';
return false;
}
$this->referer = $url;
if (preg_match('/<span.class="rouge">aucune.entreprise.ne.correspond(?:.*)vos.crit(?:.*)de.recherche<\/span>/Uis', $this->body, $matches)) {
$url = 'http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=cherche';
$postData = array(
'siren_op' => '%3D',
'siren' => $siren,
'den_rent_op' => '%25',
'den_rent' => '',
'adresse_op' => 'contient',
'adresse' => '',
'bde_rent_op' => '%25',
'bde_rent' => '',
'nom_rdir_op' => '%25',
'nom_rdir' => '',
'pren_rdir_op' => '%25',
'pren_rdir' => '',
'table' => 'rentrad',
'x' => 56,
'y' => 14,
);
sleep(rand(1, 2));
$page=getUrl($url, $this->cookie, $postData, $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body = $page['body'];
$this->codeRetour = $page['code'];
if ($this->codeRetour != '200') {
$this->libErreur='Erreur Artisanat 200c !';
return false;
}
$this->referer=$url;
}
/** Gestion des multi-inscriptions au RM **/
if (preg_match('/<b class="gris">(.*)entreprises.correspondent(?:.*)vos.crit(?:.*)de.recherche<\/span>/Uis', $this->body, $matches)) {
$nbRep = trim($matches[1])*1;
$iRadMax = -1;
if (preg_match_all('/<a class="turquoise" href="(rm\.php\?dom=Gerer&amp;fonction=dossier&amp;(.*))">/Uis', $this->body, $matches)) {
$tabUrls = array_unique($matches[1]);
$anRadMax = 0;
// Recherche de la dernière inscription
foreach ($tabUrls as $iUrl => $url) {
// On prend la dernière inscription
$tabTmp = explode('&amp;',$url);
if (substr($tabTmp[3],0,8) == 'an_rera=') {
$anRad=substr($tabTmp[3],8);
if ($anRad > $anRadMax) {
$anRadMax = $anRad;
$iRadMax = $iUrl;
}
}
}
}
if ($iRadMax==-1) die('$iRadMax==-1 Cas impossible !'.PHP_EOL.print_r($tabUrls,true).PHP_EOL.print_r($matches,true));
// Accès à la dernière fiche
$url = 'http://www.cma-paris.fr/CMP/'.strtr(trim($tabUrls[$iRadMax]),array('&amp;'=>'&'));
sleep(rand(1, 2));
$page = getUrl($url, $this->cookie, '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body = $page['body'];
$this->codeRetour = $page['code'];
if ($this->codeRetour != '200') {
$this->libErreur = 'Erreur Artisanat 200c !';
return false;
}
$this->referer = $url;
}
$tabInsert = array();
if (preg_match('/n&deg; d\'identification \(SIREN\)<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)&nbsp;<\/td>((?:.*)n&deg; de gestion rm<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>)?/Uis', $this->body, $matches)) {
$sirenLu = $matches[1]*1;
if ($siren == $sirenLu) {
$tabInsert['siren'] = $matches[1];
$tabInsert['numRM'] = @$matches[2];
}
else {
$this->libErreur = 'Erreur Siren $sirenLu<>$siren !';
return false;
}
}
elseif (preg_match('/<span class="rouge">aucune entreprise ne correspond &agrave; vos crit&egrave;res de recherche<\/span>/Uis', $this->body, $matches)) {
$this->libErreur = 'Erreur Artisan absent de la base Artisanat !';
return false;
}
else {
$this->libErreur = 'Erreur Siren absent dans la page !';
return false;
}
$actif = null;
if (preg_match("/Renseignements.relatif(?:.*)entreprise(.*)<\/b>/Uis", $this->body, $matches)) {
switch (trim(strtr($matches[1],array(chr(160)=>'')))) {
case 'en activit&eacute;': $actif = 1; break;
case 'radi&eacute;e': $actif = 0; break;
default: print_r($matches); die(); break;
}
}
$tabInsert['actif'] = $actif;
if ($actif === null) {
$this->libErreur = 'Erreur Actif/Radié non trouvé !';
return false;
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;nomination<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['denomination'] = $matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;nomination<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['denomination']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">sigle<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['sigle']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;nom&nbsp;commercial<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['nomCommercial']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">enseigne<\/span><\/td>(?:.*)<td align="left" colspan="4">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['enseigne']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">forme&nbsp;juridique<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['fj']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">effectif<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['effectif']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">code APRM<\/span><\/td>(?:.*)<td align="left">(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['aprm']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;but&nbsp;d\'activit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['debutActivite']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;activit&eacute;\(s\) exerc&eacute;e\(s\) donnant lieu &agrave; immatriculation<\/span>(?:.*)<td colspan="5">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['activite']=trim($matches[1]);
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">adresse&nbsp;de&nbsp;l\'entreprise<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['adresse']=trim($matches[1]);
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">code postal<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['cp']=trim($matches[1]);
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">ville<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['ville']=trim($matches[1]);
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">cessation d\'activit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['cessation']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">radiation du RM<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['radiation']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
$tabEtabs=array();
if (preg_match('/<!--deb Etablissements secondaires-->(?:.*)<b class="gris">Aucune inscription compl&eacute;mentaire<\/b>(?:.*)<!--fin Etablissements secondaires-->/Uis', $this->body, $matches)) {
$tabInsert['nbInscriptions']=0;
}
elseif (preg_match('/<!--deb Etablissements secondaires-->(?:.*)<b class="gris">Inscription compl&eacute;mentaire<\/b>(.*)<!--fin Etablissements secondaires-->/Uis', $this->body, $matches)) {
$tabInsert['nbInscriptions']=0;
if (preg_match_all('/<tr>(?:.*)<td><img alt="" src="inter\/pixtrans\.gif" width="30" height="1"><\/td>(?:.*)<td nowrap>(?:.*)<!-- adresse -->(.*)<\/td>(?:.*)<td nowrap>(?:.*)<!-- code postal et ville -->(.*)<\/td>(?:.*)<td nowrap>(?:.*)<!-- enseigne -->(.*)<\/td>(?:.*)<td>(?:.*)<!-- debut activite -->(.*)<\/td>(?:.*)<td>(?:.*)<!-- fin activite -->(.*)<\/td>(?:.*)<\/tr>/Uis', $matches[1], $matches2)) {
foreach ($matches2[1] as $i=>$adresse) {
$tabEtabs[$i]['siren'] = $siren;
$tabEtabs[$i]['num'] = $i;
$tabEtabs[$i]['adresse'] = trim(strtr(html_entity_decode($adresse),chr(160),' '));
$tabEtabs[$i]['cpVille'] = trim(strtr(html_entity_decode($matches2[2][$i]),chr(160),' '));
$tabEtabs[$i]['enseigne']= trim(strtr(html_entity_decode($matches2[3][$i]),chr(160),' '));
$tabEtabs[$i]['actDeb'] = Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches2[4][$i]),chr(160),' ')));
$tabEtabs[$i]['actFin'] = Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches2[5][$i]),chr(160),' ')));
$tabInsert['nbInscriptions']++;
}
}
}
/** Informations sur la Personne Physique **/
if (preg_match('/<a class="turquoise" href="(rm\.php\?dom=Gerer&amp;fonction=dirigeant&amp;dept_re(.*))" onclick="window\.open\(this\.href/Uis', $this->body, $matches)) {
$urlDir='http://www.cma-paris.fr/CMP/'.strtr(trim($matches[1]),array('&amp;'=>'&'));
//die($urlDir);
sleep(rand(1, 2));
$page = getUrl($urlDir, $this->cookie, '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body = $page['body'];
$this->codeRetour = $page['code'];
if ($this->codeRetour != '200') {
$this->libErreur = 'Erreur Artisanat 200d !';
return false;
}
$this->referer = $url;
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nom<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['nom']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">pr&eacute;nom<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['prenom']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nom d\'usage<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['nomUsage']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"(?:.*)<span class="turquoise">&nbsp;date&nbsp;de&nbsp;naissance<\/span><\/td>(?:.*)-->(?:.*)<td(?: +)align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['dateNaiss']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;lieu&nbsp;de&nbsp;naissance<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['lieuNaiss']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nationalit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['natio']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">qualit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['qualite']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right" nowrap><span class="turquoise">qualification&nbsp;artisanale<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['qualif']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">date&nbsp;d\'obtention<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['datequalif']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">prise de fonction<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['dateFctDeb']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">fin de fonction<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['dateFctFin']=Metier_Util_Date::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
}
// Insertion en base de données
$tabInsert['dateInsert'] = date('Y-m-d H:i:s');
$id = $this->iDb->insert('jo.artisanat', $tabInsert, true);
$tabInsert['id'] = $id;
foreach ($tabEtabs as $tabInsert2) {
$tabInsert2['dateInsert'] = $tabInsert['dateInsert'];
$id2 = $this->iDb->insert('jo.artisanat_etab', $tabInsert2, true);
}
$tabInsert['dateUpdate'] = substr($tabInsert['dateInsert'],0,10);
unset($tabInsert['dateInsert']);
return $tabInsert;
}
}