From 1854f07e5c585af108e53b01efad29ae5e35fc02 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 30 Jan 2016 00:12:58 +0100 Subject: [PATCH] Move code to purge files into a dedicated utils class (cron job need class) --- htdocs/admin/tools/purge.php | 58 +-------------- htdocs/core/class/utils.class.php | 117 ++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 54 deletions(-) create mode 100644 htdocs/core/class/utils.class.php diff --git a/htdocs/admin/tools/purge.php b/htdocs/admin/tools/purge.php index 9249186f574..1bc3fea3909 100644 --- a/htdocs/admin/tools/purge.php +++ b/htdocs/admin/tools/purge.php @@ -48,60 +48,10 @@ if (! empty($conf->syslog->enabled)) */ if ($action=='purge' && ! preg_match('/^confirm/i',$choice) && ($choice != 'allfiles' || $confirm == 'yes') ) { - $filesarray=array(); + require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php'; + $utils = new Utils($db); + $count = $utils->purgeFiles($choice); - if ($choice=='tempfiles') - { - // Delete temporary files - if ($dolibarr_main_data_root) - { - $filesarray=dol_dir_list($dolibarr_main_data_root,"directories",1,'^temp$'); - } - } - - if ($choice=='allfiles') - { - // Delete all files - if ($dolibarr_main_data_root) - { - $filesarray=dol_dir_list($dolibarr_main_data_root,"all",0,'','install\.lock$'); - } - } - - if ($choice=='logfile') - { - $filesarray[]=array('fullname'=>$filelog,'type'=>'file'); - } - - $count=0; - if (count($filesarray)) - { - foreach($filesarray as $key => $value) - { - //print "x ".$filesarray[$key]['fullname']."
\n"; - if ($filesarray[$key]['type'] == 'dir') - { - $count+=dol_delete_dir_recursive($filesarray[$key]['fullname']); - } - elseif ($filesarray[$key]['type'] == 'file') - { - // If (file that is not logfile) or (if logfile with option logfile) - if ($filesarray[$key]['fullname'] != $filelog || $choice=='logfile') - { - $count+=(dol_delete_file($filesarray[$key]['fullname'])?1:0); - } - } - } - - // Update cachenbofdoc - if (! empty($conf->ecm->enabled) && $choice=='allfiles') - { - require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php'; - $ecmdirstatic = new EcmDirectory($db); - $result = $ecmdirstatic->refreshcachenboffile(1); - } - } - if ($count) $mesg=$langs->trans("PurgeNDirectoriesDeleted", $count); else $mesg=$langs->trans("PurgeNothingToDelete"); setEventMessages($mesg, null, 'mesgs'); @@ -159,7 +109,7 @@ if (preg_match('/^confirm/i',$choice)) { print '
'; $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', $langs->trans('Purge'), $langs->trans('ConfirmPurge').img_warning().' ', 'purge', $formquestion, 'no', 2); } diff --git a/htdocs/core/class/utils.class.php b/htdocs/core/class/utils.class.php new file mode 100644 index 00000000000..23827a31b94 --- /dev/null +++ b/htdocs/core/class/utils.class.php @@ -0,0 +1,117 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/class/utils.class.php + * \ingroup core + * \brief File for Utils class + */ + + +/** + * Class to manage utility methods + */ +class Utils +{ + var $db; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + function __construct($db) + { + $this->db = $db; + } + + + /** + * Purge files into directory of data files. + * + * @param string $choice Choice of purge mode ('tempfiles', 'allfiles', 'logfiles') + * @return int Nb of files deleted + */ + function purgeFiles($choice) + { + global $conf, $dolibarr_main_data_root; + + $filesarray=array(); + + if ($choice=='tempfiles') + { + // Delete temporary files + if ($dolibarr_main_data_root) + { + $filesarray=dol_dir_list($dolibarr_main_data_root,"directories",1,'^temp$'); + } + } + + if ($choice=='allfiles') + { + // Delete all files + 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 + $filelog=''; + if (! empty($conf->syslog->enabled)) + { + $filelog=SYSLOG_FILE; + $filelog=preg_replace('/DOL_DATA_ROOT/i',DOL_DATA_ROOT,$filelog); + } + + $filesarray[]=array('fullname'=>$filelog,'type'=>'file'); + } + + $count=0; + if (count($filesarray)) + { + foreach($filesarray as $key => $value) + { + //print "x ".$filesarray[$key]['fullname']."
\n"; + if ($filesarray[$key]['type'] == 'dir') + { + $count+=dol_delete_dir_recursive($filesarray[$key]['fullname']); + } + elseif ($filesarray[$key]['type'] == 'file') + { + // If (file that is not logfile) or (if logfile with option logfile) + if ($filesarray[$key]['fullname'] != $filelog || $choice=='logfile') + { + $count+=(dol_delete_file($filesarray[$key]['fullname'])?1:0); + } + } + } + + // Update cachenbofdoc + if (! empty($conf->ecm->enabled) && $choice=='allfiles') + { + require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php'; + $ecmdirstatic = new EcmDirectory($this->db); + $result = $ecmdirstatic->refreshcachenboffile(1); + } + } + + return $count; + } +}