From 5c2922a17b2a8be46caa7a4857b872e470f2525f Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Wed, 30 May 2012 00:09:38 +0200 Subject: [PATCH] Fix: big problem with files upload and more compatible with external modules Conflicts: htdocs/compta/facture.php --- htdocs/comm/propal.php | 1 + htdocs/comm/propal/class/propal.class.php | 2 +- htdocs/commande/class/commande.class.php | 4 +- htdocs/commande/document.php | 2 +- htdocs/commande/fiche.php | 1 + htdocs/compta/facture.php | 21 ++++---- htdocs/compta/facture/class/facture.class.php | 29 +++++++++++ htdocs/core/ajax/fileupload.php | 48 ++++++++++++++----- htdocs/core/class/commonobject.class.php | 2 +- htdocs/core/class/html.formfile.class.php | 3 +- htdocs/core/lib/files.lib.php | 27 +++++++---- htdocs/user/class/usergroup.class.php | 2 +- 12 files changed, 103 insertions(+), 39 deletions(-) diff --git a/htdocs/comm/propal.php b/htdocs/comm/propal.php index 31af8ad691c..3fae4333d37 100644 --- a/htdocs/comm/propal.php +++ b/htdocs/comm/propal.php @@ -136,6 +136,7 @@ if ($action == 'confirm_clone' && $confirm == 'yes') else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->propale->supprimer) { $object->fetch($id); + $object->fetch_thirdparty(); $result=$object->delete($user); if ($result > 0) { diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 6977109d1f5..aab35e04585 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -1810,7 +1810,7 @@ class Propal extends CommonObject { dol_delete_preview($this); - if (!dol_delete_file($file)) + if (! dol_delete_file($file,0,0,0,$this)) // For triggers { $this->error='ErrorFailToDeleteFile'; $this->db->rollback(); diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 76a27ab2769..7bd1c812e00 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -2304,7 +2304,7 @@ class Commande extends CommonObject { dol_delete_preview($this); - if (!dol_delete_file($file)) + if (! dol_delete_file($file,0,0,0,$this)) // For triggers { $this->error=$langs->trans("ErrorCanNotDeleteFile",$file); $this->db->rollback(); @@ -2313,7 +2313,7 @@ class Commande extends CommonObject } if (file_exists($dir)) { - if (!dol_delete_dir($dir)) + if (! dol_delete_dir($dir)) { $this->error=$langs->trans("ErrorCanNotDeleteDir",$dir); $this->db->rollback(); diff --git a/htdocs/commande/document.php b/htdocs/commande/document.php index 58cbe37ede0..322b32c5094 100644 --- a/htdocs/commande/document.php +++ b/htdocs/commande/document.php @@ -166,7 +166,7 @@ if ($id > 0 || ! empty($ref)) */ if ($action == 'delete') { - $ret=$form->form_confirm($_SERVER["PHP_SELF"].'?id='.$id.'&urlfile='.urldecode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 0, 1); + $ret=$form->form_confirm($_SERVER["PHP_SELF"].'?id='.$id.'&urlfile='.urlencode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 0, 1); if ($ret == 'html') print '
'; } diff --git a/htdocs/commande/fiche.php b/htdocs/commande/fiche.php index 8dd59e31e15..27f7a09e0d6 100644 --- a/htdocs/commande/fiche.php +++ b/htdocs/commande/fiche.php @@ -134,6 +134,7 @@ else if ($action == 'confirm_delete' && $confirm == 'yes') if ($user->rights->commande->supprimer) { $object->fetch($id); + $object->fetch_thirdparty(); $result=$object->delete($user); if ($result > 0) { diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 72e1cef02d0..49483ba8081 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -149,16 +149,17 @@ else if ($action == 'reopen' && $user->rights->facture->creer) // Delete invoice else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->facture->supprimer) { - $result = $object->fetch($id); - $result = $object->delete(); - if ($result > 0) - { - Header('Location: '.$_SERVER["PHP_SELF"]); - exit; - } - else - { - $mesg='
'.$object->error.'
'; + $result = $object->fetch($id); + $object->fetch_thirdparty(); + $result = $object->delete(); + if ($result > 0) + { + Header('Location: '.$_SERVER["PHP_SELF"]); + exit; + } + else + { + $mesg='
'.$object->error.'
'; } } diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 64dc2ec393e..72698d39326 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -1166,6 +1166,7 @@ class Facture extends CommonObject function delete($rowid=0, $notrigger=0) { global $user,$langs,$conf; + require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php"); if (! $rowid) $rowid=$this->id; @@ -1236,6 +1237,34 @@ class Facture extends CommonObject $resql=$this->db->query($sql); if ($resql) { + // On efface le repertoire de pdf provisoire + $ref = dol_sanitizeFileName($this->ref); + if ($conf->facture->dir_output) + { + $dir = $conf->facture->dir_output . "/" . $ref; + $file = $conf->facture->dir_output . "/" . $ref . "/" . $ref . ".pdf"; + if (file_exists($file)) // We must delete all files before deleting directory + { + $ret=dol_delete_preview($this); + + if (! dol_delete_file($file,0,0,0,$this)) // For triggers + { + $this->error=$langs->trans("ErrorCanNotDeleteFile",$file); + $this->db->rollback(); + return 0; + } + } + if (file_exists($dir)) + { + if (! dol_delete_dir_recursive($dir)) // For remove dir and meta + { + $this->error=$langs->trans("ErrorCanNotDeleteDir",$dir); + $this->db->rollback(); + return 0; + } + } + } + $this->db->commit(); return 1; } diff --git a/htdocs/core/ajax/fileupload.php b/htdocs/core/ajax/fileupload.php index ca06febbf13..fef19981fdf 100644 --- a/htdocs/core/ajax/fileupload.php +++ b/htdocs/core/ajax/fileupload.php @@ -23,7 +23,7 @@ //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); -if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); @@ -45,7 +45,6 @@ error_reporting(E_ALL | E_STRICT); $fk_element = GETPOST('fk_element','int'); $element = GETPOST('element','alpha'); -$element_ref=GETPOST('element_ref','alpha'); /** @@ -68,19 +67,45 @@ class UploadHandler * @param string $element element * @param string $element_ref element ref */ - function __construct($options=null,$fk_element=null,$element=null,$element_ref=null) + function __construct($options=null,$fk_element=null,$element=null) { - global $conf; + global $db, $conf; + global $object; $this->_fk_element=$fk_element; $this->_element=$element; - $this->_element_ref=$element_ref; + + $pathname=$filename=$element; + if (preg_match('/^([^_]+)_([^_]+)/i',$element,$regs)) + { + $pathname = $regs[1]; + $filename = $regs[2]; + } + + // For compatibility + if ($element == 'propal') { + $pathname = 'comm/propal'; $filename = 'propal'; + } + if ($element == 'commande') { + $pathname = $filename = 'commande'; + } + if ($element == 'facture') { + $pathname = 'compta/facture'; $filename = 'facture'; + } + + dol_include_once('/'.$pathname.'/class/'.$filename.'.class.php'); + + $classname = ucfirst($filename); + $object = new $classname($db); + + $object->fetch($fk_element); + $object->fetch_thirdparty(); $this->_options = array( 'script_url' => $_SERVER['PHP_SELF'], - 'upload_dir' => $conf->$element->dir_output . '/' . $element_ref . '/', - 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$element_ref.'/', + 'upload_dir' => $conf->$element->dir_output . '/' . $object->ref . '/', + 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object->ref.'/', 'param_name' => 'files', // The php.ini settings upload_max_filesize and post_max_size // take precedence over the following max_file_size setting: @@ -100,8 +125,8 @@ class UploadHandler ), */ 'thumbs' => array( - 'upload_dir' => $conf->$element->dir_output . '/' . $element_ref . '/thumbs/', - 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$element_ref.'/thumbs/' + 'upload_dir' => $conf->$element->dir_output . '/' . $object->ref . '/thumbs/', + 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object->ref.'/thumbs/' ) ) ); @@ -255,8 +280,7 @@ class UploadHandler FILE_APPEND ); } else { - // FIXME problem with trigger - dol_move_uploaded_file($uploaded_file, $file_path, 1, 0, 0, 1); + dol_move_uploaded_file($uploaded_file, $file_path, 1); } } else { // Non-multipart uploads (PUT method support) @@ -384,7 +408,7 @@ class UploadHandler * View */ -$upload_handler = new UploadHandler(null,$fk_element,$element,$element_ref); +$upload_handler = new UploadHandler(null,$fk_element,$element); header('Pragma: no-cache'); header('Cache-Control: private, no-cache'); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index f301395706e..96f4b820190 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -544,7 +544,7 @@ abstract class CommonObject $this->thirdparty = $thirdparty; // Use first price level if level not defined for third party - if ($conf->global->PRODUIT_MULTIPRICES && empty($this->thirdparty->price_level)) + if (! empty($conf->global->PRODUIT_MULTIPRICES) && empty($this->thirdparty->price_level)) { $this->client->price_level=1; // deprecated $this->thirdparty->price_level=1; diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php index 4bb658654b5..34ecea9488b 100644 --- a/htdocs/core/class/html.formfile.class.php +++ b/htdocs/core/class/html.formfile.class.php @@ -839,7 +839,7 @@ class FormFile // Load existing files: // TODO do not delete if (1 == 2) { - $.getJSON($("#fileupload form").prop("action"), { fk_element: "'.$object->id.'", element: "'.$object->element.'", element_ref: "'.$object->ref.'"}, function (files) { + $.getJSON($("#fileupload form").prop("action"), { fk_element: "'.$object->id.'", element: "'.$object->element.'"}, function (files) { var fu = $("#fileupload").data("fileupload"); fu._adjustMaxNumberOfFiles(-files.length); fu._renderDownload(files) @@ -867,7 +867,6 @@ class FormFile print '
'; print ''; print ''; - print ''; print '
'; print ''; print '