#!/usr/bin/php -c/var/www/batch/config/php_batch_sd.ini
<?php
include_once(FWK_PATH.'common/chiffres.php');
include_once(FWK_PATH.'common/dates.php');
include_once(FWK_PATH.'common/curl.php');
include_once(INCLUDE_PATH.'insee/classMInsee.php');
include_once(FWK_PATH.'mail/sendMail.php');

global $iDb;
$iDb=new WDB('sdv1');
$iDb2=new WDB('sdv1');

$iInsee=new MInsee();

//include_once(INCLUDE_PATH.'insee/classMSigVille.php');
$cookie=$post=$referer='';
$timeout=300;
$url="http://www.aspic.interieur.gouv.fr/Acces_ASPIC/asAcces-000-Portail.php?mod=CONS";
$page=getUrl($url, $cookie, $post, $referer, false, 'www.aspic.interieur.gouv.fr', '', $timeout);
$cookie=$page['header']['Set-Cookie'];
$body=$page['body'];
$referer=$url;

if ($page['code']<>200)	die('Code retour != de 200'.EOL);

if (!preg_match_all('/<li><a HREF="(.*)">(.*)\((.*)\)<\/a><\/li>/Uim', $body, $matches))
	die();

foreach ($matches[3] as $i=>$dep) {
	$urlDep=$matches[1][$i];
	$nomDep=$matches[2][$i];
	echo "$dep: $nomDep ($urlDep)".EOL;
	$url="http://www.aspic.interieur.gouv.fr/Acces_ASPIC/".$urlDep;
	$page=getUrl($url, $cookie, $post, $referer, false, 'www.aspic.interieur.gouv.fr', '', $timeout);
	$referer=$url;
	randsleep(1,2);
	$url='http://www.aspic.interieur.gouv.fr/AspicExtract/asListeRequetes.php';
	$page=getUrl($url, $cookie, $post, $referer, false, 'www.aspic.interieur.gouv.fr', '', $timeout);
	$referer=$url;
	$body=$page['body'];
	
	if (preg_match_all("/<LI><a HREF='javascript\:SoumetFormulaire\((.*)\)'>(.*)<\/A>/Uim", $body, $matches2)) {
		foreach ($matches2[1] as $j=>$reqId) {
			$reqNom=$matches2[2][$j];
			randsleep(1,2);
			$post=array('Id'=>$reqId,
						'cboFormat'=>'text/plain');
			$url='http://www.aspic.interieur.gouv.fr/AspicExtract/asRequete.php';
			$page=getUrl($url, $cookie, $post, $referer, false, 'www.aspic.interieur.gouv.fr', '', $timeout);
			file_put_contents("/root/aspic/$dep-$reqId-$reqNom.txt",$page['body']);
			$size=round(strlen($page['body'])/1024,1);
			echo "$dep: $nomDep $reqId-$reqNom $size Ko".EOL;
		}
		$post='';
	}
}

die();
for ($depIn=1; $depIn<96; $depIn++) {
	if ($depIn*1<10)	$depIn='0'.$depIn*1;
	die();
	$body=$page['body'];
}
/*	if (preg_match_all('/<li id="(.*)">(.*) \((.*)\)<\/li>/Uim', $body, $matches)) {
		foreach ($matches[1] as $j=>$cp) {
			$ville=strip_tags(utf8_decode($matches[2][$j]));
			echo $cp.'='.$ville.EOL;
			$iDb->insert('zonesVilles',	array(	'cp'=>$cp,
												'commune'=>$ville));

	randsleep(7,21);
*/
 ?>