NEW The purge of iles can purge only if older than a number of seconds

This commit is contained in:
Laurent Destailleur 2022-09-09 15:33:55 +02:00
parent f60ea48d75
commit 26ad6199e5
3 changed files with 55 additions and 34 deletions

View File

@ -33,7 +33,7 @@ $langs->load("admin");
$action = GETPOST('action', 'aZ09'); $action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha'); $confirm = GETPOST('confirm', 'alpha');
$choice = GETPOST('choice', 'aZ09'); $choice = GETPOST('choice', 'aZ09');
$nbsecondsold = GETPOSTINT('nbsecondsold');
// Define filelog to discard it from purge // Define filelog to discard it from purge
$filelog = ''; $filelog = '';
@ -42,6 +42,7 @@ if (!empty($conf->syslog->enabled)) {
$filelog = preg_replace('/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog); $filelog = preg_replace('/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog);
} }
// Security
if (!$user->admin) { if (!$user->admin) {
accessforbidden(); 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'; require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
$utils = new Utils($db); $utils = new Utils($db);
$result = $utils->purgeFiles($choice);
$result = $utils->purgeFiles($choice, $nbsecondsold);
$mesg = $utils->output; $mesg = $utils->output;
setEventMessages($mesg, null, 'mesgs'); 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 '<input type="radio" name="choice" id="choiceallfiles" value="confirm_allfiles"';
print ($choice && $choice == 'confirm_allfiles') ? ' checked' : ''; 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>'; print '</td></tr></table>';
//if ($choice != 'confirm_allfiles') //if ($choice != 'confirm_allfiles')
@ -129,7 +134,7 @@ print '</form>';
if (preg_match('/^confirm/i', $choice)) { if (preg_match('/^confirm/i', $choice)) {
print '<br>'; print '<br>';
$formquestion = array(); $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 // End of page

View File

@ -52,8 +52,8 @@ class Utils
* Purge files into directory of data files. * Purge files into directory of data files.
* CAN BE A CRON TASK * 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 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' * @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) * @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) public function purgeFiles($choices = 'tempfilesold+logfiles', $nbsecondsold = 86400)
@ -67,6 +67,9 @@ class Utils
if (empty($choices)) { if (empty($choices)) {
$choices = 'tempfilesold+logfiles'; $choices = 'tempfilesold+logfiles';
} }
if ($choices == 'allfiles' && $nbsecondsold > 0) {
$choices = 'allfilesold';
}
dol_syslog("Utils::purgeFiles choice=".$choices, LOG_DEBUG); dol_syslog("Utils::purgeFiles choice=".$choices, LOG_DEBUG);
@ -77,6 +80,7 @@ class Utils
$choicesarray = preg_split('/[\+,]/', $choices); $choicesarray = preg_split('/[\+,]/', $choices);
foreach ($choicesarray as $choice) { foreach ($choicesarray as $choice) {
$now = dol_now();
$filesarray = array(); $filesarray = array();
if ($choice == 'tempfiles' || $choice == 'tempfilesold') { 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 $filesarray = dol_dir_list($dolibarr_main_data_root, "directories", 1, '^temp$', '', 'name', SORT_ASC, 2, 0, '', 1); // Do not follow symlinks
if ($choice == 'tempfilesold') { if ($choice == 'tempfilesold') {
$now = dol_now();
foreach ($filesarray as $key => $val) { foreach ($filesarray as $key => $val) {
if ($val['date'] > ($now - ($nbsecondsold))) { if ($val['date'] > ($now - ($nbsecondsold))) {
unset($filesarray[$key]); // Discard temp dir not older than $nbsecondsold unset($filesarray[$key]); // Discard temp dir not older than $nbsecondsold
@ -98,7 +101,14 @@ class Utils
if ($choice == 'allfiles') { if ($choice == 'allfiles') {
// Delete all files (except install.lock, do not follow symbolic links) // Delete all files (except install.lock, do not follow symbolic links)
if ($dolibarr_main_data_root) { 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; $countdeleted += $tmpcountdeleted;
} }
} elseif ($filesarray[$key]['type'] == 'file') { } elseif ($filesarray[$key]['type'] == 'file') {
// If (file that is not logfile) or (if mode is logfile) if ($choice != 'allfilesold' || $filesarray[$key]['date'] < ($now - $nbsecondsold)) {
if ($filesarray[$key]['fullname'] != $filelog || $choice == 'logfile' || $choice == 'logfiles') { // If (file that is not logfile) or (if mode is logfile)
$result = dol_delete_file($filesarray[$key]['fullname'], 1, 1); if ($filesarray[$key]['fullname'] != $filelog || $choice == 'logfile' || $choice == 'logfiles') {
if ($result) { $result = dol_delete_file($filesarray[$key]['fullname'], 1, 1);
$count++; if ($result) {
$countdeleted++; $count++;
} else { $countdeleted++;
$counterror++; } else {
$counterror++;
}
} }
} }
} }

View File

@ -54,10 +54,11 @@ function dol_basename($pathfile)
* @param int $nohook Disable all hooks * @param int $nohook Disable all hooks
* @param string $relativename For recursive purpose only. Must be "" at first call. * @param string $relativename For recursive purpose only. Must be "" at first call.
* @param string $donotfollowsymlinks Do not follow symbolic links * @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',...) * @return array Array of array('name'=>'xxx','fullname'=>'/abc/xxx','date'=>'yyy','size'=>99,'type'=>'dir|file',...)
* @see dol_dir_list_in_database() * @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 $db, $hookmanager;
global $object; 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); //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; $loaddate = ($mode == 1 || $mode == 2 || $nbsecondsold) ? true : false;
$loadsize = ($mode == 1 || $mode == 3) ?true:false; $loadsize = ($mode == 1 || $mode == 3) ?true : false;
$loadperm = ($mode == 1 || $mode == 4) ?true:false; $loadperm = ($mode == 1 || $mode == 4) ?true : false;
// Clean parameters // Clean parameters
$path = preg_replace('/([\\/]+)$/i', '', $path); $path = preg_replace('/([\\/]+)$/i', '', $path);
$newpath = dol_osencode($path); $newpath = dol_osencode($path);
$now = dol_now();
$reshook = 0; $reshook = 0;
$file_list = array(); $file_list = array();
@ -170,7 +172,7 @@ function dol_dir_list($path, $types = "all", $recursive = 0, $filter = "", $excl
if ($recursive > 0) { if ($recursive > 0) {
if (empty($donotfollowsymlinks) || !is_link($path."/".$file)) { if (empty($donotfollowsymlinks) || !is_link($path."/".$file)) {
//var_dump('eee '. $path."/".$file. ' '.is_dir($path."/".$file).' '.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"))) { } 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 if (!$filter || preg_match('/'.$filter.'/i', $file)) { // We do not search key $filter into $path, only into $file
preg_match('/([^\/]+)\/[^\/]+$/', $path.'/'.$file, $reg); if (empty($nbsecondsold) || $filedate <= ($now - $nbsecondsold)) {
$level1name = (isset($reg[1]) ? $reg[1] : ''); preg_match('/([^\/]+)\/[^\/]+$/', $path.'/'.$file, $reg);
$file_list[] = array( $level1name = (isset($reg[1]) ? $reg[1] : '');
"name" => $file, $file_list[] = array(
"path" => $path, "name" => $file,
"level1name" => $level1name, "path" => $path,
"relativename" => ($relativename ? $relativename.'/' : '').$file, "level1name" => $level1name,
"fullname" => $path.'/'.$file, "relativename" => ($relativename ? $relativename.'/' : '').$file,
"date" => $filedate, "fullname" => $path.'/'.$file,
"size" => $filesize, "date" => $filedate,
"type" => 'file' "size" => $filesize,
); "type" => 'file'
);
}
} }
} }
} }