Fix: Introduce error by default when function is used as a true delete

function.
This commit is contained in:
Laurent Destailleur 2015-05-29 17:11:42 +02:00
parent c8949d0532
commit 65c7ab7a93
9 changed files with 34 additions and 33 deletions

View File

@ -121,7 +121,7 @@ if (! empty($_POST['removedfile']) || ! empty($_POST['removedfilehtml']))
$pathtodelete=$listofpaths[$keytodelete];
$filetodelete=$listofnames[$keytodelete];
$result = dol_delete_file($pathtodelete,1);
if ($result >= 0)
if ($result)
{
setEventMessage($langs->trans("FileWasRemoved"), $filetodelete);

View File

@ -89,7 +89,7 @@ if ($action=='purge' && ! preg_match('/^confirm/i',$choice) && ($choice != 'allf
// 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']);
$count+=(dol_delete_file($filesarray[$key]['fullname'])?1:0);
}
}
}

View File

@ -1759,10 +1759,8 @@ if (empty($reshook))
$upload_dir = $conf->facture->dir_output;
$file = $upload_dir . '/' . GETPOST('file');
$ret = dol_delete_file($file, 0, 0, 0, $object);
if ($ret)
setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
else
setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
if ($ret) setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
else setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
$action = '';
}
}

View File

@ -823,10 +823,8 @@ else if ($action == 'remove_file' && $user->rights->contrat->creer) {
$upload_dir = $conf->contrat->dir_output;
$file = $upload_dir . '/' . GETPOST('file');
$ret = dol_delete_file($file, 0, 0, 0, $object);
if ($ret)
setEventMessage($langs->trans("FileWasRemoved", GETPOST('file')));
else
setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('file')), 'errors');
if ($ret) setEventMessage($langs->trans("FileWasRemoved", GETPOST('file')));
else setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('file')), 'errors');
}
}

View File

@ -116,9 +116,9 @@ class HookManager
* @param array $parameters Array of parameters
* @param Object $object Object to use hooks on
* @param string $action Action code on calling page ('create', 'edit', 'view', 'add', 'update', 'delete'...)
* @return mixed For doActions,formObjectOptions,pdf_xxx: Return 0 if we want to keep standard actions, >0 if we want to stop standard actions, <0 means KO.
* For printSearchForm,printLeftBlock,printTopRightMenu,formAddObjectLine,...: Return HTML string. TODO Deprecated. Must always return an int and things to print into ->resprints.
* Can also return some values into an array ->results.
* @return mixed For 'addreplace hooks (doActions,formObjectOptions,pdf_xxx,...): Return 0 if we want to keep standard actions, >0 if we want to stop standard actions, <0 if KO.
* For 'output' hooks (printLeftBlock, formAddObjectLine, formBuilddocOptions, ...): Return 0, <0 if KO. Things to print are returned into ->resprints and set into ->resPrint.
* All types can also return some values into an array ->results.
* $this->error or this->errors are also defined by class called by this function if error.
*/
function executeHooks($method, $parameters=false, &$object='', &$action='')
@ -135,7 +135,8 @@ class HookManager
array(
'addMoreActionsButtons',
'addStatisticLine',
'doActions',
'deleteFile',
'doActions',
'formCreateThirdpartyOptions',
'formObjectOptions',
'formattachOptions',
@ -147,7 +148,7 @@ class HookManager
'formatEvent'
)
)) $hooktype='addreplace';
// Deprecated hook types
// Deprecated hook types ('returnvalue')
if (preg_match('/^pdf_/',$method) && $method != 'pdf_writelinedesc') $hooktype='returnvalue'; // pdf_xxx except pdf_writelinedesc are 'returnvalue' hooks. When there is 2 hooks of this type, only last one win. TODO Move them into 'output' or 'addreplace' hooks.
if ($method == 'insertExtraFields')
{
@ -195,7 +196,7 @@ class HookManager
if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) $this->resArray =array_merge($this->resArray, $actionclassinstance->results);
if (! empty($actionclassinstance->resprints)) $this->resPrint.=$actionclassinstance->resprints;
}
// Generic hooks that return a string or array (printSearchForm, printLeftBlock, formAddObjectLine, formBuilddocOptions, ...)
// Generic hooks that return a string or array (printLeftBlock, formAddObjectLine, formBuilddocOptions, ...)
else
{
// TODO. this should be done into the method of hook by returning nothing

View File

@ -793,12 +793,12 @@ function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disable
/**
* Remove a file or several files with a mask
*
* @param string $file File to delete or mask of file to delete
* @param int $disableglob Disable usage of glob like *
* @param string $file File to delete or mask of files to delete
* @param int $disableglob Disable usage of glob like * so function is an exact delete function that will return error if no file found
* @param int $nophperrors Disable all PHP output errors
* @param int $nohook Disable all hooks
* @param object $object Current object in use
* @return boolean True if file is deleted (or if glob is used and there's nothing to delete), False if error
* @return boolean True if no error (file is deleted or if glob is used and there's nothing to delete), False if error
*/
function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=null)
{
@ -823,19 +823,20 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=n
$reshook=$hookmanager->executeHooks('deleteFile', $parameters, $object);
}
if (empty($nohook) && isset($reshook) && $reshook != '') // 0:not deleted, 1:deleted, null or '' for bypass
if (empty($nohook) && $reshook != 0) // reshook = 0 to do standard actions, 1 = ok, -1 = ko
{
return $reshook;
if ($reshook < 0) return false;
return true;
}
else
{
$error=0;
//print "x".$file." ".$disableglob;exit;
$ok=true;
$file_osencoded=dol_osencode($file); // New filename encoded in OS filesystem encoding charset
if (empty($disableglob) && ! empty($file_osencoded))
{
$ok=true;
$globencoded=str_replace('[','\[',$file_osencoded);
$globencoded=str_replace(']','\]',$globencoded);
$listofdir=glob($globencoded);
@ -853,6 +854,7 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=n
}
else
{
$ok=false;
if ($nophperrors) $ok=@unlink($file_osencoded);
else $ok=unlink($file_osencoded);
if ($ok) dol_syslog("Removed file ".$file_osencoded, LOG_DEBUG);
@ -958,9 +960,9 @@ function dol_delete_preview($object)
if (file_exists($file) && is_writable($file))
{
if ( ! dol_delete_file($file,1) )
if (! dol_delete_file($file,1))
{
$object->error=$langs->trans("ErrorFailedToOpenFile",$file);
$object->error=$langs->trans("ErrorFailedToDeleteFile",$file);
return 0;
}
}

View File

@ -59,11 +59,8 @@ if ($action == 'confirm_deletefile' && $confirm == 'yes')
if ($urlfile)
{
$ret = dol_delete_file($file, 0, 0, 0, $object);
if ($ret) {
setEventMessage($langs->trans("FileWasRemoved", $urlfile));
} else {
setEventMessage($langs->trans("ErrorFailToDeleteFile", $urlfile), 'errors');
}
if ($ret) setEventMessage($langs->trans("FileWasRemoved", $urlfile));
else setEventMessage($langs->trans("ErrorFailToDeleteFile", $urlfile), 'errors');
}
elseif ($linkid)
{

View File

@ -3680,7 +3680,7 @@ function migrate_delete_old_files($db,$langs,$conf)
print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
}
else
{
{
//print $langs->trans("FileWasRemoved",$filetodelete);
}
}

View File

@ -339,10 +339,15 @@ class FilesLibTest extends PHPUnit_Framework_TestCase
print __METHOD__." result=".$result."\n";
$this->assertTrue($result,'delete file');
// Again to test no error when deleteing a non existing file
$result=dol_delete_file($conf->admin->dir_temp.'/file2.csv');
// Again to test there is error when deleting a non existing file with option disableglob
$result=dol_delete_file($conf->admin->dir_temp.'/file2.csv',1,1);
print __METHOD__." result=".$result."\n";
$this->assertTrue($result,'delete file that does not exists');
$this->assertFalse($result,'delete file that does not exists with disableglo must return ko');
// Again to test there is no error when deleting a non existing file without option disableglob
$result=dol_delete_file($conf->admin->dir_temp.'/file2.csv',0,1);
print __METHOD__." result=".$result."\n";
$this->assertTrue($result,'delete file that does not exists without disabling glob must return ok');
// Test copy with special char / delete with blob
$result=dol_copy($file, $conf->admin->dir_temp.'/file with [x] and é.csv',0,1);