125 lines
4.5 KiB
PHP
125 lines
4.5 KiB
PHP
|
<?php
|
||
|
/**
|
||
|
* Récupération des fichiers INSEE pour mise a disposition et suivi
|
||
|
*
|
||
|
*/
|
||
|
// --- Define path to application directory
|
||
|
defined('APPLICATION_PATH')
|
||
|
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../../application'));
|
||
|
|
||
|
// --- Define application environment
|
||
|
defined('APPLICATION_ENV')
|
||
|
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
|
||
|
|
||
|
// --- Composer autoload
|
||
|
require_once realpath(__DIR__ . '/../../vendor/autoload.php');
|
||
|
|
||
|
// --- Create application, bootstrap, and run
|
||
|
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
|
||
|
|
||
|
try {
|
||
|
$opts = new Zend_Console_Getopt(
|
||
|
//Options
|
||
|
array(
|
||
|
'help|?' => "Displays usage information.",
|
||
|
'file-w' => "Télécharger à nouveau le fichier en mode manuel pour écrasement de l'ancien",
|
||
|
'type=w' => "Only look for one type of file",
|
||
|
'cron' => "Mandatory option for launch the cli in cron",
|
||
|
'from=w' => "restricted not opendata"
|
||
|
)
|
||
|
);
|
||
|
$opts->parse();
|
||
|
} catch (Zend_Console_Getopt_Exception $e) {
|
||
|
echo $e->getUsageMessage();
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
// --- Usage
|
||
|
if( isset($opts->help) || count($opts->getOptions())==0 )
|
||
|
{
|
||
|
echo "Recuperation des fichiers INSEE.\n";
|
||
|
echo $opts->getUsageMessage();
|
||
|
exit;
|
||
|
}
|
||
|
$BASEPATH = realpath(APPLICATION_PATH . '/../');
|
||
|
$STORAGEBASE = "/home/data/sources/insee";
|
||
|
$FTP_URL = "ftp://81.255.68.110:21/download/";
|
||
|
$FTP_OPTION = "-k --ssl --disable-epsv --user client_syracuse23:cLiEnT_SYRAcuse23*37654 --cacert $BASEPATH/certs/ca.pem --cert $BASEPATH/certs/client.pem --key $BASEPATH/certs/key.pem";
|
||
|
|
||
|
echo date('Y-m-d H:i:s') . " - Debut execution\n";
|
||
|
|
||
|
$c = new Zend_Config($application->getOptions());
|
||
|
$db = Zend_Db::factory($c->profil->db->metier);
|
||
|
Zend_Db_Table_Abstract::setDefaultAdapter($db);
|
||
|
|
||
|
$storageDir = $STORAGEBASE . '/' . $date('Y');
|
||
|
if (!file_exists($storageDir)) {
|
||
|
mkdir($storageDir, 0777, true);
|
||
|
}
|
||
|
|
||
|
$fileM = new Application_Model_Sdv1FluxInsee();
|
||
|
|
||
|
$oneShot = false;
|
||
|
if ($opts->file) {
|
||
|
$oneShot = true;
|
||
|
}
|
||
|
|
||
|
if ($opts->cron || $oneShot) {
|
||
|
|
||
|
// --- Liste des fichiers
|
||
|
$command = "-s $FTP_OPTION $FTP_URL | grep -e '^-' | awk '{ print $9 }'";
|
||
|
exec($command, $contents);
|
||
|
if (count($contents) == 0) {
|
||
|
echo date('Y-m-d H:i:s') . " - Impossible de lister le répertoire $ftpDir\n";
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// --- Traitement pour chaque fichier
|
||
|
if (count($contents) > 0) {
|
||
|
foreach ($contents as $file) {
|
||
|
echo date('Y-m-d H:i:s') . " - Lecture $file\n";
|
||
|
$fileSql = $fileM->select()->where('file=?', $file)->order('dateInsert DESC')->limit(1); //@todo : Marquer invalide ?
|
||
|
$fileRow = $fileM->fetchRow($fileSql);
|
||
|
// -- Fichier non présent en base
|
||
|
if ($fileRow === null) {
|
||
|
// --- Téléchargement du fichier
|
||
|
echo date('Y-m-d H:i:s') . " - Telechargement $file\n";
|
||
|
$command = "cd $storageDir; curl -s -S $FTP_OPTION -O " . $FTP_URL . $file;
|
||
|
exec($command);
|
||
|
if (file_exists($storageDir.'/'.$file)) {
|
||
|
$type = 'insee';
|
||
|
$ftpSize = filesize($storageDir.'/'.$file);
|
||
|
$ftpDate = filectime($storageDir.'/'.$file);
|
||
|
|
||
|
echo date('Y-m-d H:i:s') . " - Ecriture du fichier $storageDir/$file\n";
|
||
|
// --- Enregistrement dans la base de données
|
||
|
$data = array(
|
||
|
'type' => $type,
|
||
|
'file' => $file,
|
||
|
'directory' => $storageDir,
|
||
|
'ftpSize' => $ftpSize,
|
||
|
'ftpDate' => $ftpDate,
|
||
|
'dateInsert'=> date('YmdHis'),
|
||
|
);
|
||
|
try {
|
||
|
$fileM->insert($data);
|
||
|
} catch(Zend_Db_Exception $e) {
|
||
|
echo date('Y-m-d H:i:s') . " - Erreur insertion $file = ".$e->getMessage()."\n";
|
||
|
}
|
||
|
} else {
|
||
|
echo date('Y-m-d H:i:s') . " - Impossible de télécharger $file\n";
|
||
|
}
|
||
|
}
|
||
|
// --- Fichier déjà présent en base
|
||
|
else {
|
||
|
|
||
|
// @todo : Vérifier la présence réelle du fichier
|
||
|
|
||
|
echo date('Y-m-d H:i:s') . " - Fichier $file en base\n";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
echo date('Y-m-d H:i:s') . " - Fin execution\n";
|