batch/scripts/sources/Dila.php
Michael RICOIS b7fa256a3e Script DILA
2015-07-07 13:39:24 +00:00

256 lines
8.8 KiB
PHP

<?php
/**
* Récupération des fichiers de la DILA pour mise a disposition et suivi
*
*/
error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
// --- Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// --- Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// --- Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
// --- Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/Zend/autoload_classmap.php',
__DIR__ . '/../../library/Application/autoload_classmap.php',
__DIR__ . '/../../library/Scores/autoload_classmap.php',
__DIR__ . '/../../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
'SdMetier' => __DIR__ . '/../../library/SdMetier',
'Metier' => __DIR__ . '/../../library/Metier',
),
'fallback_autoloader' => true
)
));
// --- 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 lookfs 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 de la DILA.\n";
echo $opts->getUsageMessage();
exit;
}
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
$STORAGEBASE = "/home/data/sources/dila";
if ($opts->from == 'restricted') {
$FTPSERVER = "ftp.journal-officiel.gouv.fr";
$FTPUSER = "SCORE";
$FTPPASS = "SD075";
} else {
$FTPSERVER = "echanges.dila.gouv.fr";
$FTPUSER = "anonymous";
$FTPPASS = "guest";
}
// --- Liste des Types
$types = array(
'ASSOCIATION' => array(
'FTPDIR' => "ASSOCIATIONS/ASS_{YEAR}",
'STORAGE' => "associations/{YEAR}",
'PATTERN' => array(),
),
'BALO' => array(
'FTPDIR' => "BALO/{YEAR}",
'STORAGE' => "balo/{YEAR}",
'PATTERN' => array(),
),
'BOAMP' => array(
'FTPDIR' => "BOAMP/{YEAR}",
'STORAGE' => "boamp/{YEAR}",
'PATTERN' => array(),
),
'BODACC' => array(
'FTPDIR' => "BODACC/{YEAR}",
'STORAGE' => "bodacc/{YEAR}",
'PATTERN' => array(),
),
'ASSOCIATIONCPT' => array(
'FTPDIR' => "COMPTES_DES_ASSOCATIONS/FLUX",
'STORAGE' => "associationcpt/{YEAR}",
'PATTERN' => array(),
),
);
$fileM = new Application_Model_Sdv1FluxDila();
$oneShot = false;
if ($opts->file) {
$oneShot = true;
}
if ($opts->type) {
if (array_key_exists(strtoupper($opts->type), $types)) {
$types = array( $opts->type => $types[strtoupper($opts->type)] );
$oneShot = true;
} else {
echo date('Y-m-d H:i:s') . " - Type inexistant\n";
exit;
}
}
if ($opts->cron || $oneShot) {
// --- Connexion au FTP
$conn = ftp_connect($FTPSERVER);
if ( false === $conn) {
echo date('Y-m-d H:i:s') . " - Impossible de se connecter à $FTPSERVER\n";
exit;
}
// --- Login
$login = ftp_login($conn, $FTPUSER, $FTPPASS);
if ( false === $login) {
echo date('Y-m-d H:i:s') . " - Erreur authentication à $FTPSERVER\n";
exit;
}
// --- Pour chaque type
foreach ($types as $type => $typeParams) {
// --- Spécification répertoires
$ftpDir = $typeParams['FTPDIR'];
$ftpDir = str_replace('{YEAR}', date('Y'), $ftpDir);
$storageDir = $typeParams['STORAGE'];
$storageDir = str_replace('{YEAR}', date('Y'), $storageDir);
$storageDir = $STORAGEBASE . '/' . $storageDir;
if (!file_exists($storageDir)) {
mkdir($storageDir, 0777, true);
}
// --- Liste des fichiers dans le répertoire
$contents = ftp_nlist($conn, $ftpDir);
print_r($contents); exit;
if ($contents === false) {
echo date('Y-m-d H:i:s') . " - Impossible de lister le répertoire $ftpDir\n";
continue;
}
if (count($contents) > 0) {
foreach ($contents as $file) {
$downloadFile = false;
$fileSql = $fileM->select()->where('file=?', $file)->order('dateInsert DESC')->limit(1);
$fileRow = $fileM->fetchRow($fileSql);
// --- Récupération des infos FTP
$ftpDate = 0;
$fileTime = ftp_mdtm($conn, $file);
if ($fileTime != -1) {
$ftpDate = date('YmdHis', $fileTime);
}
$ftpSize = ftp_size($conn, $file);
// -- Fichier non présent en base
if ($fileRow === null) {
$downloadFile = true;
// --- Le fichier existe déjà sur le système de fichier
if (file_exists($storageDir.'/'.$file)) {
echo date('Y-m-d H:i:s') . "Le fichier existe $file\n";
// --- Enregistrement dans la base de données
$data = array(
'type' => $type,
'file' => $file,
'directory' => $storageDir,
'ftpSize' => filesize($storageDir.'/'.$file),
'ftpDate' => date('YmdHis', filectime($storageDir.'/'.$file)),
'dateInsert'=> date('YmdHis'),
);
try {
$fileM->insert($data);
} catch(Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s') . " - Erreur insertion bdd $file = ".$e->getMessage()."\n";
}
$downloadFile = false;
}
}
// --- Fichier déjà présent en base
else {
if ($ftpSize != filesize($storageDir.'/'.$file) && $ftpSize != $fileRow->ftpSize) {
move($storageDir.'/'.$file, $storageDir.'/'.$file.'_'.date('YmdHis'));
$downloadFile = true;
}
/*if ($ftpDate != date('YmdHis', filectime($storageDir.'/'.$file))) {
move($storageDir.'/'.$file, $storageDir.'/'.$file.'_'.date('YmdHis'));
$downloadFile = true;
}*/
}
// --- Téléchargement du fichier
if ( $downloadFile === true) {
if (ftp_get($conn, $storageDir.'/'.$file, $ftpDir.'/'.$file, FTP_BINARY)) {
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 bdd $file = ".$e->getMessage()."\n";
}
} else {
echo date('Y-m-d H:i:s') . " - Impossible de télécharger $file\n";
}
}
}
}
}
// --- Fermeture du FTP
ftp_close($conn);
}