extranet/scripts/jobs/removeTempFile.php
2017-03-07 14:50:28 +01:00

110 lines
2.7 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|?' => "Display help.",
'options|o' => " all: All files, [directory_name]: Files in this directory",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(count($opts->getOptions())==0 || isset($opts->help))
{
echo "Delete temporary files";
echo "\n\n";
echo $opts->getUsageMessage();
echo "\n";
exit;
}
$c = new Zend_Config($application->getOptions());
//Liste des Repertoires
$tempDirs = array(
'cache' => array(
'path' => $c->profil->path->shared.'/cache',
'files' => array('*.tpl')
),
'files' => array(
'path' => $c->profil->path->shared.'/files',
'files' => array('*.pdf', '*.jpeg', '*.jpg', '*.csv', '*.csv.bz2', '*.xls','*.xml')
),
'pages' => array(
'path' => $c->profil->path->shared.'/pages',
'files' => array('*.html')
),
'imgcache' => array(
'path' => $c->profil->path->shared . '/pages/imgcache',
'files' => array('*.png', '*.gif', '*.jpeg', '*.jpg')
),
'sessions' => array(
'path' => $c->profil->path->shared . '/sessions',
'files' => array('sess_*')
),
);
/**
* Suppression des fichiers dans un repertoire
* @param string $path
* @param string $filePattern
*/
function removeFile($path, $filePattern)
{
$dir = realpath($path);
if (is_dir($dir)) {
if (stristr(PHP_OS, 'WIN')) {
$cmd = 'del '.$dir.'/'.$filePattern;
} else {
/**
* To avoid too args error from rm command
*/
$cmd = 'find '.$dir.'/ -name "'.$filePattern.'" -exec rm {} \;';
}
return shell_exec($cmd);
}
return false;
}
function removePatternInDir($path, $tabPattern)
{
foreach($tabPattern as $pattern) {
echo "Suppression fichiers ".$path." : ";
$result = removeFile($path, $pattern);
if ($result === false){
echo "Erreur";
} else {
echo $result;
}
echo "\n";
}
}
foreach($tempDirs as $dir => $options) {
if ($opts->options == 'all') {
removePatternInDir($options['path'], $options['files']);
} elseif ($opts->options == $dir) {
removePatternInDir($options['path'], $options['files']);
break;
}
}