extranet/scripts/jobs/removeTempFile.php

110 lines
2.7 KiB
PHP
Raw Normal View History

2012-11-16 09:34:42 +00:00
<?php
2015-09-25 12:38:03 +00:00
// --- Define path to application directory
2012-11-16 09:34:42 +00:00
defined('APPLICATION_PATH')
2015-09-25 12:38:03 +00:00
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../../application'));
2012-11-16 09:34:42 +00:00
2015-09-25 12:38:03 +00:00
// --- Define application environment
2012-11-16 09:34:42 +00:00
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
2015-09-25 12:38:03 +00:00
// --- Composer autoload
require_once realpath(__DIR__ . '/../../vendor/autoload.php');
2012-11-16 09:34:42 +00:00
2015-09-25 12:38:03 +00:00
// --- Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
2012-11-16 09:34:42 +00:00
try {
$opts = new Zend_Console_Getopt(
2014-03-14 10:42:09 +00:00
//Options
array(
'help|?' => "Display help.",
2014-12-17 10:01:13 +00:00
'options|o' => " all: All files, [directory_name]: Files in this directory",
2014-03-14 10:42:09 +00:00
)
2012-11-16 09:34:42 +00:00
);
$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(
2016-02-12 14:25:27 +00:00
'path' => $c->profil->path->shared.'/temp/cache',
'files' => array('*.tpl')
2012-11-16 09:34:42 +00:00
),
'files' => array(
2016-02-12 14:25:27 +00:00
'path' => $c->profil->path->shared.'/temp/files',
2013-05-15 09:37:27 +00:00
'files' => array('*.pdf', '*.jpeg', '*.jpg', '*.csv', '*.csv.bz2', '*.xls','*.xml')
2012-11-16 09:34:42 +00:00
),
'pages' => array(
2016-02-12 14:25:27 +00:00
'path' => $c->profil->path->shared.'/temp/pages',
2012-11-16 09:34:42 +00:00
'files' => array('*.html')
),
'imgcache' => array(
2016-02-12 14:25:27 +00:00
'path' => $c->profil->path->shared . '/temp/pages/imgcache',
'files' => array('*.png', '*.gif', '*.jpeg', '*.jpg')
2012-11-16 09:34:42 +00:00
),
'sessions' => array(
2016-02-12 14:25:27 +00:00
'path' => $c->profil->path->shared . '/temp/sessions',
2012-11-16 09:34:42 +00:00
'files' => array('sess_*')
),
);
/**
* Suppression des fichiers dans un repertoire
* @param string $path
* @param string $filePattern
*/
function removeFile($path, $filePattern)
{
$dir = realpath($path);
2012-11-16 09:34:42 +00:00
if (is_dir($dir)) {
if (stristr(PHP_OS, 'WIN')) {
2014-12-17 10:01:13 +00:00
$cmd = 'del '.$dir.'/'.$filePattern;
2012-11-16 09:34:42 +00:00
} 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;
}
}