diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php index 74f190074b6..c1e3338bb2a 100644 --- a/htdocs/core/class/html.formfile.class.php +++ b/htdocs/core/class/html.formfile.class.php @@ -551,83 +551,115 @@ class FormFile * @param string $url Full url to use for click links ('' = autodetect) * @return int <0 if KO, nb of files shown if OK */ - function list_of_documents($filearray,$object,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$title='',$url='') - { - global $user, $conf, $langs; - global $bc; - global $sortfield, $sortorder, $maxheightmini; + function list_of_documents($filearray,$object,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$title='',$url='') + { + global $user, $conf, $langs; + global $bc, $hookmanager; + global $sortfield, $sortorder, $maxheightmini; - // Show list of existing files - if (empty($useinecm)) print_titre($title?$title:$langs->trans("AttachedFiles")); - if (empty($url)) $url=$_SERVER["PHP_SELF"]; - print ''; - print ''; - print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder); - if (empty($useinecm)) print_liste_field_titre('',$url,"","",$param,'align="center"'); - print_liste_field_titre('','',''); - print ''; + if (! is_object($hookmanager)) + { + if (! class_exists('HookManager')) { + // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array + require DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; + $hookmanager=new HookManager($db); + } + } + $hookmanager->initHooks(array('formfile')); - $nboffiles=count($filearray); + $parameters=array( + 'filearray' => $filearray, + 'modulepart'=> $modulepart, + 'param' => $param, + 'forcedownload' => $forcedownload, + 'relativepath' => $relativepath, + 'permtodelete' => $permtodelete, + 'useinecm' => $useinecm, + 'textifempty' => $textifempty, + 'maxlength' => $maxlength, + 'title' => $title, + 'url' => $url + ); + $reshook=$hookmanager->executeHooks('showNodesList', $parameters, $object); - if ($nboffiles > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php'; + if (isset($reshook) && $reshook != '') // 0:not deleted, 1:deleted, null or '' for bypass + { + return $reshook; + } + else + { + // Show list of existing files + if (empty($useinecm)) print_titre($title?$title:$langs->trans("AttachedFiles")); + if (empty($url)) $url=$_SERVER["PHP_SELF"]; + print '
'; + print ''; + print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder); + if (empty($useinecm)) print_liste_field_titre('',$url,"","",$param,'align="center"'); + print_liste_field_titre('','',''); + print ''; - $var=true; - foreach($filearray as $key => $file) // filearray must be only files here - { - if ($file['name'] != '.' - && $file['name'] != '..' - && ! preg_match('/\.meta$/i',$file['name'])) - { - // Define relative path used to store the file - if (empty($relativepath)) - $relativepath=(! empty($object->ref)?dol_sanitizeFileName($object->ref):'').'/'; + $nboffiles=count($filearray); - $var=!$var; - print ''; - print '\n"; - print ''; - print ''; - // Preview - if (empty($useinecm)) - { - print ''; - } - // Delete or view link - print '"; - print "\n"; - } - } - if ($nboffiles == 0) - { - print ''; - } - print "
'; - //print "XX".$file['name']; //$file['name'] must be utf8 - print ''; - print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' '; - print dol_trunc($file['name'],$maxlength,'middle'); - print ''; - print "'.dol_print_size($file['size'],1,1).''.dol_print_date($file['date'],"dayhour").''; - $tmp=explode('.',$file['name']); - $minifile=$tmp[0].'_mini.'.$tmp[1]; - if (image_format_supported($file['name']) > 0) print ''; - else print ' '; - print ''; - if ($useinecm) print ''.img_view().'   '; - if ($permtodelete) print ''.img_delete().''; - else print ' '; - print "
'; - if (empty($textifempty)) print $langs->trans("NoFileFound"); - else print $textifempty; - print '
"; + if ($nboffiles > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php'; - return $nboffiles; - } + $var=true; + foreach($filearray as $key => $file) // filearray must be only files here + { + if ($file['name'] != '.' + && $file['name'] != '..' + && ! preg_match('/\.meta$/i',$file['name'])) + { + // Define relative path used to store the file + if (empty($relativepath)) + $relativepath=(! empty($object->ref)?dol_sanitizeFileName($object->ref):'').'/'; + + $var=!$var; + print ''; + print ''; + //print "XX".$file['name']; //$file['name'] must be utf8 + print ''; + print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' '; + print dol_trunc($file['name'],$maxlength,'middle'); + print ''; + print "\n"; + print ''.dol_print_size($file['size'],1,1).''; + print ''.dol_print_date($file['date'],"dayhour").''; + // Preview + if (empty($useinecm)) + { + print ''; + $tmp=explode('.',$file['name']); + $minifile=$tmp[0].'_mini.'.$tmp[1]; + if (image_format_supported($file['name']) > 0) print ''; + else print ' '; + print ''; + } + // Delete or view link + print ''; + if ($useinecm) print ''.img_view().'   '; + if ($permtodelete) print ''.img_delete().''; + else print ' '; + print ""; + print "\n"; + } + } + if ($nboffiles == 0) + { + print ''; + if (empty($textifempty)) print $langs->trans("NoFileFound"); + else print $textifempty; + print ''; + } + print ""; + + return $nboffiles; + } + } /** diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index af964d92ff1..787a0a0815a 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -52,6 +52,7 @@ function dol_basename($pathfile) function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter="", $sortcriteria="name", $sortorder=SORT_ASC, $mode=0) { global $db, $hookmanager; + global $object; dol_syslog("files.lib.php::dol_dir_list path=".$path." types=".$types." recursive=".$recursive." filter=".$filter." excludefilter=".json_encode($excludefilter)); @@ -83,7 +84,7 @@ function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefil 'loaddate' => $loaddate, 'loadsize' => $loadsize ); - $reshook=$hookmanager->executeHooks('getNodesList', $parameters); + $reshook=$hookmanager->executeHooks('getNodesList', $parameters, $object); // $reshook may contain returns stacked by other modules // $reshook is always empty with an array for can not lose returns stacked with other modules @@ -764,61 +765,91 @@ function dolMoveUploadedFile($src_file, $dest_file, $allowoverwrite, $notrigger= * @param int $disableglob Disable usage of glob like * * @param int $nophperrors Disable all PHP output errors * @param int $notrigger Disable all triggers - * @param Object $object Object + * @param object $object Current object in use * @return boolean True if file is deleted, False if error */ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$notrigger=0,$object=null) { global $db, $conf, $user, $langs; + global $hookmanager; $langs->load("other"); $langs->load("errors"); - $error=0; + if (! is_object($hookmanager)) + { + if (! class_exists('HookManager')) { + // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array + require DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; + $hookmanager=new HookManager($db); + } + } + $hookmanager->initHooks(array('fileslib')); - //print "x".$file." ".$disableglob; - $ok=true; - $file_osencoded=dol_osencode($file); // New filename encoded in OS filesystem encoding charset - if (empty($disableglob) && ! empty($file_osencoded)) - { - foreach (glob($file_osencoded) as $filename) - { - if ($nophperrors) $ok=@unlink($filename); // The unlink encapsulated by dolibarr - else $ok=unlink($filename); // The unlink encapsulated by dolibarr - if ($ok) - { - dol_syslog("Removed file ".$filename, LOG_DEBUG); - if (! $notrigger) - { - if (! is_object($object)) $object=(object) 'dummy'; - $object->src_file=$file; + $parameters=array( + 'file' => $file, + 'disableglob'=> $disableglob, + 'nophperrors' => $nophperrors, + 'notrigger' => $notrigger + ); + $reshook=$hookmanager->executeHooks('deleteFile', $parameters, $object); - // TODO Replace trigger by a hook. Triggers must be used for business events only. - // Appel des triggers - include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; - $interface=new Interfaces($db); - $result=$interface->run_triggers('FILE_DELETE',$object,$user,$langs,$conf); - if ($result < 0) { $error++; $errors=$interface->errors; } - // Fin appel triggers - } - } - else { - dol_syslog("Failed to remove file ".$filename, LOG_WARNING); - } - } - } - else - { - if ($nophperrors) $ok=@unlink($file_osencoded); // The unlink encapsulated by dolibarr - else $ok=unlink($file_osencoded); // The unlink encapsulated by dolibarr - if ($ok) { - dol_syslog("Removed file ".$file_osencoded, LOG_DEBUG); - } - else { - dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING); - } - } - return $ok; + if (isset($reshook) && $reshook != '') // 0:not deleted, 1:deleted, null or '' for bypass + { + return $reshook; + } + else + { + $error=0; + + //print "x".$file." ".$disableglob; + $ok=true; + $file_osencoded=dol_osencode($file); // New filename encoded in OS filesystem encoding charset + if (empty($disableglob) && ! empty($file_osencoded)) + { + foreach (glob($file_osencoded) as $filename) + { + if ($nophperrors) $ok=@unlink($filename); // The unlink encapsulated by dolibarr + else $ok=unlink($filename); // The unlink encapsulated by dolibarr + if ($ok) + { + dol_syslog("Removed file ".$filename, LOG_DEBUG); + if (! $notrigger) + { + if (! is_object($object)) $object=(object) 'dummy'; + $object->src_file=$file; + + // TODO Replace trigger by a hook. Triggers must be used for business events only. + // REGIS just after of hook testing + // Appel des triggers + include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; + $interface=new Interfaces($db); + $result=$interface->run_triggers('FILE_DELETE',$object,$user,$langs,$conf); + if ($result < 0) { + $error++; $errors=$interface->errors; + } + // Fin appel triggers + } + } + else { + dol_syslog("Failed to remove file ".$filename, LOG_WARNING); + } + } + } + else + { + if ($nophperrors) $ok=@unlink($file_osencoded); // The unlink encapsulated by dolibarr + else $ok=unlink($file_osencoded); // The unlink encapsulated by dolibarr + if ($ok) { + dol_syslog("Removed file ".$file_osencoded, LOG_DEBUG); + } + else { + dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING); + } + } + + return $ok; + } } /**