NEW The purge of iles can purge only if older than a number of seconds
This commit is contained in:
parent
f60ea48d75
commit
26ad6199e5
@ -33,7 +33,7 @@ $langs->load("admin");
|
||||
$action = GETPOST('action', 'aZ09');
|
||||
$confirm = GETPOST('confirm', 'alpha');
|
||||
$choice = GETPOST('choice', 'aZ09');
|
||||
|
||||
$nbsecondsold = GETPOSTINT('nbsecondsold');
|
||||
|
||||
// Define filelog to discard it from purge
|
||||
$filelog = '';
|
||||
@ -42,6 +42,7 @@ if (!empty($conf->syslog->enabled)) {
|
||||
$filelog = preg_replace('/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog);
|
||||
}
|
||||
|
||||
// Security
|
||||
if (!$user->admin) {
|
||||
accessforbidden();
|
||||
}
|
||||
@ -64,7 +65,8 @@ if ($action == 'purge' && !preg_match('/^confirm/i', $choice) && ($choice != 'al
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
|
||||
$utils = new Utils($db);
|
||||
$result = $utils->purgeFiles($choice);
|
||||
|
||||
$result = $utils->purgeFiles($choice, $nbsecondsold);
|
||||
|
||||
$mesg = $utils->output;
|
||||
setEventMessages($mesg, null, 'mesgs');
|
||||
@ -114,8 +116,11 @@ print '> <label for="choicetempfiles">'.$langs->trans("PurgeDeleteTemporaryFiles
|
||||
|
||||
print '<input type="radio" name="choice" id="choiceallfiles" value="confirm_allfiles"';
|
||||
print ($choice && $choice == 'confirm_allfiles') ? ' checked' : '';
|
||||
print '> <label for="choiceallfiles">'.$langs->trans("PurgeDeleteAllFilesInDocumentsDir", $dolibarr_main_data_root).'</label><br>';
|
||||
|
||||
print '> <label for="choiceallfiles">'.$langs->trans("PurgeDeleteAllFilesInDocumentsDir", $dolibarr_main_data_root).'</label>';
|
||||
print '<br>';
|
||||
if (getDolGlobalInt('MAIN_PURGE_ACCEPT_NBSECONDSOLD')) {
|
||||
print 'NbSecondsOld = <input class="width50 right" type="text" name="nbsecondsold" value="'.$nbsecondsold.'">';
|
||||
}
|
||||
print '</td></tr></table>';
|
||||
|
||||
//if ($choice != 'confirm_allfiles')
|
||||
@ -129,7 +134,7 @@ print '</form>';
|
||||
if (preg_match('/^confirm/i', $choice)) {
|
||||
print '<br>';
|
||||
$formquestion = array();
|
||||
print $form->formconfirm($_SERVER["PHP_SELF"].'?choice=allfiles', $langs->trans('Purge'), $langs->trans('ConfirmPurge').img_warning().' ', 'purge', $formquestion, 'no', 2);
|
||||
print $form->formconfirm($_SERVER["PHP_SELF"].'?choice=allfiles&nbsecondsold='.$nbsecondsold, $langs->trans('Purge'), $langs->trans('ConfirmPurge').img_warning().' ', 'purge', $formquestion, 'no', 2);
|
||||
}
|
||||
|
||||
// End of page
|
||||
|
||||
@ -52,8 +52,8 @@ class Utils
|
||||
* Purge files into directory of data files.
|
||||
* CAN BE A CRON TASK
|
||||
*
|
||||
* @param string $choices Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than $nbsecondsold seconds, 'logfiles', or mix of this). Note 'allfiles' is possible too but very dangerous.
|
||||
* @param int $nbsecondsold Nb of seconds old to accept deletion of a directory if $choice is 'tempfilesold'
|
||||
* @param string $choices Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than $nbsecondsold seconds, 'logfiles', or mix of this). Note that 'allfiles' is also possible but very dangerous.
|
||||
* @param int $nbsecondsold Nb of seconds old to accept deletion of a directory if $choice is 'tempfilesold', or deletion of file if $choice is 'allfiles'
|
||||
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
|
||||
*/
|
||||
public function purgeFiles($choices = 'tempfilesold+logfiles', $nbsecondsold = 86400)
|
||||
@ -67,6 +67,9 @@ class Utils
|
||||
if (empty($choices)) {
|
||||
$choices = 'tempfilesold+logfiles';
|
||||
}
|
||||
if ($choices == 'allfiles' && $nbsecondsold > 0) {
|
||||
$choices = 'allfilesold';
|
||||
}
|
||||
|
||||
dol_syslog("Utils::purgeFiles choice=".$choices, LOG_DEBUG);
|
||||
|
||||
@ -77,6 +80,7 @@ class Utils
|
||||
|
||||
$choicesarray = preg_split('/[\+,]/', $choices);
|
||||
foreach ($choicesarray as $choice) {
|
||||
$now = dol_now();
|
||||
$filesarray = array();
|
||||
|
||||
if ($choice == 'tempfiles' || $choice == 'tempfilesold') {
|
||||
@ -85,7 +89,6 @@ class Utils
|
||||
$filesarray = dol_dir_list($dolibarr_main_data_root, "directories", 1, '^temp$', '', 'name', SORT_ASC, 2, 0, '', 1); // Do not follow symlinks
|
||||
|
||||
if ($choice == 'tempfilesold') {
|
||||
$now = dol_now();
|
||||
foreach ($filesarray as $key => $val) {
|
||||
if ($val['date'] > ($now - ($nbsecondsold))) {
|
||||
unset($filesarray[$key]); // Discard temp dir not older than $nbsecondsold
|
||||
@ -98,7 +101,14 @@ class Utils
|
||||
if ($choice == 'allfiles') {
|
||||
// Delete all files (except install.lock, do not follow symbolic links)
|
||||
if ($dolibarr_main_data_root) {
|
||||
$filesarray = dol_dir_list($dolibarr_main_data_root, "all", 0, '', 'install\.lock$', 'name', SORT_ASC, 0, 0, '', 1);
|
||||
$filesarray = dol_dir_list($dolibarr_main_data_root, "all", 0, '', 'install\.lock$', 'name', SORT_ASC, 0, 0, '', 1); // No need to use recursive, we will delete directory
|
||||
}
|
||||
}
|
||||
|
||||
if ($choice == 'allfilesold') {
|
||||
// Delete all files (except install.lock, do not follow symbolic links)
|
||||
if ($dolibarr_main_data_root) {
|
||||
$filesarray = dol_dir_list($dolibarr_main_data_root, "files", 1, '', 'install\.lock$', 'name', SORT_ASC, 0, 0, '', 1, $nbsecondsold); // No need to use recursive, we will delete directory
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,14 +148,16 @@ class Utils
|
||||
$countdeleted += $tmpcountdeleted;
|
||||
}
|
||||
} elseif ($filesarray[$key]['type'] == 'file') {
|
||||
// If (file that is not logfile) or (if mode is logfile)
|
||||
if ($filesarray[$key]['fullname'] != $filelog || $choice == 'logfile' || $choice == 'logfiles') {
|
||||
$result = dol_delete_file($filesarray[$key]['fullname'], 1, 1);
|
||||
if ($result) {
|
||||
$count++;
|
||||
$countdeleted++;
|
||||
} else {
|
||||
$counterror++;
|
||||
if ($choice != 'allfilesold' || $filesarray[$key]['date'] < ($now - $nbsecondsold)) {
|
||||
// If (file that is not logfile) or (if mode is logfile)
|
||||
if ($filesarray[$key]['fullname'] != $filelog || $choice == 'logfile' || $choice == 'logfiles') {
|
||||
$result = dol_delete_file($filesarray[$key]['fullname'], 1, 1);
|
||||
if ($result) {
|
||||
$count++;
|
||||
$countdeleted++;
|
||||
} else {
|
||||
$counterror++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,10 +54,11 @@ function dol_basename($pathfile)
|
||||
* @param int $nohook Disable all hooks
|
||||
* @param string $relativename For recursive purpose only. Must be "" at first call.
|
||||
* @param string $donotfollowsymlinks Do not follow symbolic links
|
||||
* @param string $nbsecondsold Only files older than $nbsecondsold
|
||||
* @return array Array of array('name'=>'xxx','fullname'=>'/abc/xxx','date'=>'yyy','size'=>99,'type'=>'dir|file',...)
|
||||
* @see dol_dir_list_in_database()
|
||||
*/
|
||||
function dol_dir_list($path, $types = "all", $recursive = 0, $filter = "", $excludefilter = null, $sortcriteria = "name", $sortorder = SORT_ASC, $mode = 0, $nohook = 0, $relativename = "", $donotfollowsymlinks = 0)
|
||||
function dol_dir_list($path, $types = "all", $recursive = 0, $filter = "", $excludefilter = null, $sortcriteria = "name", $sortorder = SORT_ASC, $mode = 0, $nohook = 0, $relativename = "", $donotfollowsymlinks = 0, $nbsecondsold = 0)
|
||||
{
|
||||
global $db, $hookmanager;
|
||||
global $object;
|
||||
@ -67,13 +68,14 @@ function dol_dir_list($path, $types = "all", $recursive = 0, $filter = "", $excl
|
||||
//print 'xxx'."files.lib.php::dol_dir_list path=".$path." types=".$types." recursive=".$recursive." filter=".$filter." excludefilter=".json_encode($excludefilter);
|
||||
}
|
||||
|
||||
$loaddate = ($mode == 1 || $mode == 2) ?true:false;
|
||||
$loadsize = ($mode == 1 || $mode == 3) ?true:false;
|
||||
$loadperm = ($mode == 1 || $mode == 4) ?true:false;
|
||||
$loaddate = ($mode == 1 || $mode == 2 || $nbsecondsold) ? true : false;
|
||||
$loadsize = ($mode == 1 || $mode == 3) ?true : false;
|
||||
$loadperm = ($mode == 1 || $mode == 4) ?true : false;
|
||||
|
||||
// Clean parameters
|
||||
$path = preg_replace('/([\\/]+)$/i', '', $path);
|
||||
$newpath = dol_osencode($path);
|
||||
$now = dol_now();
|
||||
|
||||
$reshook = 0;
|
||||
$file_list = array();
|
||||
@ -170,7 +172,7 @@ function dol_dir_list($path, $types = "all", $recursive = 0, $filter = "", $excl
|
||||
if ($recursive > 0) {
|
||||
if (empty($donotfollowsymlinks) || !is_link($path."/".$file)) {
|
||||
//var_dump('eee '. $path."/".$file. ' '.is_dir($path."/".$file).' '.is_link($path."/".$file));
|
||||
$file_list = array_merge($file_list, dol_dir_list($path."/".$file, $types, $recursive + 1, $filter, $excludefilter, $sortcriteria, $sortorder, $mode, $nohook, ($relativename != '' ? $relativename.'/' : '').$file, $donotfollowsymlinks));
|
||||
$file_list = array_merge($file_list, dol_dir_list($path."/".$file, $types, $recursive + 1, $filter, $excludefilter, $sortcriteria, $sortorder, $mode, $nohook, ($relativename != '' ? $relativename.'/' : '').$file, $donotfollowsymlinks, $nbsecondsold));
|
||||
}
|
||||
}
|
||||
} elseif (!$isdir && (($types == "files") || ($types == "all"))) {
|
||||
@ -183,18 +185,20 @@ function dol_dir_list($path, $types = "all", $recursive = 0, $filter = "", $excl
|
||||
}
|
||||
|
||||
if (!$filter || preg_match('/'.$filter.'/i', $file)) { // We do not search key $filter into $path, only into $file
|
||||
preg_match('/([^\/]+)\/[^\/]+$/', $path.'/'.$file, $reg);
|
||||
$level1name = (isset($reg[1]) ? $reg[1] : '');
|
||||
$file_list[] = array(
|
||||
"name" => $file,
|
||||
"path" => $path,
|
||||
"level1name" => $level1name,
|
||||
"relativename" => ($relativename ? $relativename.'/' : '').$file,
|
||||
"fullname" => $path.'/'.$file,
|
||||
"date" => $filedate,
|
||||
"size" => $filesize,
|
||||
"type" => 'file'
|
||||
);
|
||||
if (empty($nbsecondsold) || $filedate <= ($now - $nbsecondsold)) {
|
||||
preg_match('/([^\/]+)\/[^\/]+$/', $path.'/'.$file, $reg);
|
||||
$level1name = (isset($reg[1]) ? $reg[1] : '');
|
||||
$file_list[] = array(
|
||||
"name" => $file,
|
||||
"path" => $path,
|
||||
"level1name" => $level1name,
|
||||
"relativename" => ($relativename ? $relativename.'/' : '').$file,
|
||||
"fullname" => $path.'/'.$file,
|
||||
"date" => $filedate,
|
||||
"size" => $filesize,
|
||||
"type" => 'file'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user