From a487492a7a3b6659ae127531b3e7c5fcf885cb5d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 12 Jul 2017 20:23:35 +0200 Subject: [PATCH] Fix purge of log files was missing some log files. --- htdocs/core/class/utils.class.php | 94 +++++++++++++++++-------------- htdocs/langs/en_US/admin.lang | 2 +- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/htdocs/core/class/utils.class.php b/htdocs/core/class/utils.class.php index 3a35bfb2cc2..9e9d66f3d69 100644 --- a/htdocs/core/class/utils.class.php +++ b/htdocs/core/class/utils.class.php @@ -28,10 +28,10 @@ class Utils { var $db; - + var $output; // Used by Cron method to return message var $result; // Used by Cron method to return data - + /** * Constructor * @@ -47,21 +47,21 @@ class Utils * Purge files into directory of data files. * CAN BE A CRON TASK * - * @param string $choice Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfiles') - * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) + * @param string $choice Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfile') + * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) */ function purgeFiles($choice='tempfilesold') { global $conf, $langs, $dolibarr_main_data_root; - + $langs->load("admin"); - + dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG); require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; - + $filesarray=array(); if (empty($choice)) $choice='tempfilesold'; - + if ($choice=='tempfiles' || $choice=='tempfilesold') { // Delete temporary files @@ -78,29 +78,39 @@ class Utils } } } - + if ($choice=='allfiles') { - // Delete all files + // Delete all files (except install.lock) if ($dolibarr_main_data_root) { $filesarray=dol_dir_list($dolibarr_main_data_root,"all",0,'','install\.lock$'); } } - + if ($choice=='logfile') { - // Define filelog to discard it from purge + // Define files log + if ($dolibarr_main_data_root) + { + $filesarray=dol_dir_list($dolibarr_main_data_root, "files", 0, '.*\.log$', 'install\.lock$'); + } + $filelog=''; if (! empty($conf->syslog->enabled)) { $filelog=$conf->global->SYSLOG_FILE; $filelog=preg_replace('/DOL_DATA_ROOT/i',DOL_DATA_ROOT,$filelog); - } - $filesarray[]=array('fullname'=>$filelog,'type'=>'file'); + $alreadyincluded=false; + foreach ($filesarray as $tmpcursor) + { + if ($tmpcursor['fullname'] == $filelog) { $alreadyincluded=true; } + } + if (! $alreadyincluded) $filesarray[]=array('fullname'=>$filelog,'type'=>'file'); + } } - + $count=0; if (count($filesarray)) { @@ -120,7 +130,7 @@ class Utils } } } - + // Update cachenbofdoc if (! empty($conf->ecm->enabled) && $choice=='allfiles') { @@ -129,14 +139,14 @@ class Utils $result = $ecmdirstatic->refreshcachenboffile(1); } } - + if ($count > 0) $this->output=$langs->trans("PurgeNDirectoriesDeleted", $count); else $this->output=$langs->trans("PurgeNothingToDelete"); //return $count; return 0; // This function can be called by cron so must return 0 if OK } - + /** * Make a backup of database @@ -147,16 +157,16 @@ class Utils * @param int $usedefault 1=Use default backup profile (Set this to 1 when used as cron) * @param string $file 'auto' or filename to build * @param int $keeplastnfiles Keep only last n files (not used yet) - * @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) */ function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0) { global $db, $conf, $langs, $dolibarr_main_data_root; global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass; - + $langs->load("admin"); - + dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG); require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; @@ -190,14 +200,14 @@ class Utils $outputdir = $conf->admin->dir_output.'/backup'; $result=dol_mkdir($outputdir); - + // MYSQL if ($type == 'mysql' || $type == 'mysqli') { $cmddump=$conf->global->SYSTEMTOOLS_MYSQLDUMP; - - + + $outputfile = $outputdir.'/'.$file; // for compression format, we add extension $compression=$compression ? $compression : 'none'; @@ -205,11 +215,11 @@ class Utils if ($compression == 'bz') $outputfile.='.bz2'; $outputerror = $outputfile.'.err'; dol_mkdir($conf->admin->dir_output.'/backup'); - + // Parameteres execution $command=$cmddump; if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command - + //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass); $param=$dolibarr_main_db_name." -h ".$dolibarr_main_db_host; $param.=" -u ".$dolibarr_main_db_user; @@ -250,16 +260,16 @@ class Utils $paramcrypted.=' -p"'.preg_replace('/./i','*',$dolibarr_main_db_pass).'"'; $paramclear.=' -p"'.str_replace(array('"','`'),array('\"','\`'),$dolibarr_main_db_pass).'"'; } - + $errormsg=''; - + // Debut appel methode execution $fullcommandcrypted=$command." ".$paramcrypted." 2>&1"; $fullcommandclear=$command." ".$paramclear." 2>&1"; if ($compression == 'none') $handle = fopen($outputfile, 'w'); if ($compression == 'gz') $handle = gzopen($outputfile, 'w'); if ($compression == 'bz') $handle = bzopen($outputfile, 'w'); - + if ($handle) { $ok=0; @@ -277,11 +287,11 @@ class Utils elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; } pclose($handlein); - + if ($compression == 'none') fclose($handle); if ($compression == 'gz') gzclose($handle); if ($compression == 'bz') bzclose($handle); - + if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK)); } @@ -291,7 +301,7 @@ class Utils dol_syslog("Failed to open file ".$outputfile,LOG_ERR); $errormsg=$langs->trans("ErrorFailedToWriteInDir"); } - + // Get errorstring if ($compression == 'none') $handle = fopen($outputfile, 'r'); if ($compression == 'gz') $handle = gzopen($outputfile, 'r'); @@ -320,13 +330,13 @@ class Utils } } // Fin execution commande - + $this->output = $errormsg; $this->error = $errormsg; $this->result = array("commandbackuplastdone" => $command." ".$paramcrypted, "commandbackuptorun" => ""); //if (empty($this->output)) $this->output=$this->result['commandbackuplastdone']; } - + // MYSQL NO BIN if ($type == 'mysqlnobin') { @@ -338,7 +348,7 @@ class Utils if ($compression == 'bz') $outputfile.='.bz2'; $outputerror = $outputfile.'.err'; dol_mkdir($conf->admin->dir_output.'/backup'); - + if ($compression == 'gz' or $compression == 'bz') { backup_tables($outputfiletemp); @@ -349,16 +359,16 @@ class Utils { backup_tables($outputfile); } - + $this->output = ""; $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => ""); } - + // POSTGRESQL if ($type == 'postgresql') { $cmddump=$conf->global->SYSTEMTOOLS_POSTGRESQLDUMP; - + $outputfile = $outputdir.'/'.$file; // for compression format, we add extension $compression=$compression ? $compression : 'none'; @@ -366,11 +376,11 @@ class Utils if ($compression == 'bz') $outputfile.='.bz2'; $outputerror = $outputfile.'.err'; dol_mkdir($conf->admin->dir_output.'/backup'); - + // Parameteres execution $command=$cmddump; if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command - + //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass); //$param="-F c"; $param="-F p"; @@ -419,8 +429,8 @@ class Utils dol_delete_file($val['fullname']); } } - - + + return 0; } } diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 5d25e654a06..1f384015839 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -140,7 +140,7 @@ SystemToolsArea=System tools area SystemToolsAreaDesc=This area provides administration features. Use the menu to choose the feature you're looking for. Purge=Purge PurgeAreaDesc=This page allows you to delete all files generated or stored by Dolibarr (temporary files or all files in %s directory). Using this feature is not necessary. It is provided as a workaround for users whose Dolibarr is hosted by a provider that does not offer permissions to delete files generated by the web server. -PurgeDeleteLogFile=Delete log file %s defined for Syslog module (no risk of losing data) +PurgeDeleteLogFile=Delete log files, including %s defined for Syslog module (no risk of losing data) PurgeDeleteTemporaryFiles=Delete all temporary files (no risk of losing data) PurgeDeleteTemporaryFilesShort=Delete temporary files PurgeDeleteAllFilesInDocumentsDir=Delete all files in directory %s. Temporary files but also database backup dumps, files attached to elements (third parties, invoices, ...) and uploaded into the ECM module will be deleted.