webfiler/scripts/jobs/integrate.php
2015-09-29 16:59:45 +00:00

150 lines
5.6 KiB
PHP

<?php
// --- 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|?' => "Affiche l'aide.",
'cron' => "Use in crontab",
'user=s' => "Regen for user et toutes ses prestations",
'presta=s' => "Regen for a prestation",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(count($opts->getOptions())==0 || isset($opts->help))
{
echo "Integrate file in webfiler.";
echo "\n\n";
echo $opts->getUsageMessage();
echo "\n";
exit;
}
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->resources->db);
$prestaSql = $db->select()
->from(array('p'=>'clients_presta'), array(), 'sdv1')
->join(array('c'=>'clients'), 'c.id = p.clientId ', 'sdv1')
->where('p.actif = ?', 1)
->where('c.actif = ?', 'Oui');
$prestaList = $db->fetchAll($prestaSql, Zend_Db::FETCH_OBJ);
foreach ($prestaList as $p) {
echo date('Y-m-d H:i:s')." - Recherche des paramètres pour la prestation ".$p->code."\n";
$params = array();
$paramSql = $db->select()->from('prestadetail', array(), 'webfiler')->where('prestaCode = ?', $p->code);
$paramResult = $db->fetchAll($paramSql, Zend_Db::FETCH_OBJ);
if (count($paramsResult) > 0) {
foreach ($params as $item) {
$params[$item->name] = $item->value;
}
} else {
echo date('Y-m-d H:i:s')." - Aucun paramètre.\n";
continue;
}
// --- Traitement
if (count($params) > 0) {
switch ($params['type']) {
case 'db':
// --- Lecture de la base de données pour obtenir la liste des fichiers
$stmt = $db->query($sql);
$rows = $stmt->fetchAll(Zend_Db::FETCH_OBJ);
if (count($rows) > 0) {
foreach ($rows as $f) {
$directory = $f->directory;
if (substr($f->directory, -1) == '/') {
$directory = substr($f->directory, 0, strlen($f->directory)-1);
}
if (substr($directory, 0, 1) == '/') {
$filename = $directory.'/'.$f->file;
} else {
$filename = $c->profil->path->secure.'/'.$directory.'/'.$f->file;
}
if (file_exists($filename)) {
$fileDate = filectime($filename);
$fileSize = filesize($filename);
$fileMd5 = md5_file($filename);
$fileSha1 = sha1_file($filename);
// --- Enregistrer dans la base
}
}
}
break;
case 'file':
// --- Lecture du repertoire pour obtenir la liste des fichiers
$directory = $params['directory'];
if (substr($params['directory'], -1) == '/') {
$directory = substr($params['directory'], 0, strlen($params['directory'])-1);
}
if (substr($directory, 0, 1) != '/') {
$directory = $c->profil->path->secure.'/'.$directory;
}
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
// --- Masque de fichier
if (array_key_exists('filemask', $params)) {
$pattern = '/'.$params['filemask'].'/';
if (!preg_match($pattern, $file)) {
continue;
}
}
$filename = $directory.'/'.$file;
$fileDate = filectime($filename);
// --- Calcul du nombre de jours
$dateNow = new Zend_Date();
$dateFile = new Zend_Date($fileDate, Zend_Date::TIMESTAMP);
$difference = $dateNow->sub($dateFile);
$measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::DAY);
$measure->convertTo(Zend_Measure_Time::DAY);
$nbJour = $measure->getValue(0);
if ($nbJour > $params['maxkeepday']) {
continue;
}
$fileSize = filesize($filename);
$fileMd5 = md5_file($filename);
$fileSha1 = sha1_file($filename);
// --- Enregistrer dans la base
}
}
closedir($handle);
}
break;
default:
echo date('Y-m-d H:i:s')." - Type inconnu.\n";
break;
}
}
}