Fix: big problem with files upload and more compatible with external

modules

Conflicts:
	htdocs/compta/facture.php
This commit is contained in:
Regis Houssin 2012-05-30 00:09:38 +02:00
parent f11430dec8
commit 5c2922a17b
12 changed files with 103 additions and 39 deletions

View File

@ -136,6 +136,7 @@ if ($action == 'confirm_clone' && $confirm == 'yes')
else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->propale->supprimer) else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->propale->supprimer)
{ {
$object->fetch($id); $object->fetch($id);
$object->fetch_thirdparty();
$result=$object->delete($user); $result=$object->delete($user);
if ($result > 0) if ($result > 0)
{ {

View File

@ -1810,7 +1810,7 @@ class Propal extends CommonObject
{ {
dol_delete_preview($this); dol_delete_preview($this);
if (!dol_delete_file($file)) if (! dol_delete_file($file,0,0,0,$this)) // For triggers
{ {
$this->error='ErrorFailToDeleteFile'; $this->error='ErrorFailToDeleteFile';
$this->db->rollback(); $this->db->rollback();

View File

@ -2304,7 +2304,7 @@ class Commande extends CommonObject
{ {
dol_delete_preview($this); 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->error=$langs->trans("ErrorCanNotDeleteFile",$file);
$this->db->rollback(); $this->db->rollback();
@ -2313,7 +2313,7 @@ class Commande extends CommonObject
} }
if (file_exists($dir)) if (file_exists($dir))
{ {
if (!dol_delete_dir($dir)) if (! dol_delete_dir($dir))
{ {
$this->error=$langs->trans("ErrorCanNotDeleteDir",$dir); $this->error=$langs->trans("ErrorCanNotDeleteDir",$dir);
$this->db->rollback(); $this->db->rollback();

View File

@ -166,7 +166,7 @@ if ($id > 0 || ! empty($ref))
*/ */
if ($action == 'delete') 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 '<br>'; if ($ret == 'html') print '<br>';
} }

View File

@ -134,6 +134,7 @@ else if ($action == 'confirm_delete' && $confirm == 'yes')
if ($user->rights->commande->supprimer) if ($user->rights->commande->supprimer)
{ {
$object->fetch($id); $object->fetch($id);
$object->fetch_thirdparty();
$result=$object->delete($user); $result=$object->delete($user);
if ($result > 0) if ($result > 0)
{ {

View File

@ -150,6 +150,7 @@ else if ($action == 'reopen' && $user->rights->facture->creer)
else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->facture->supprimer) else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->facture->supprimer)
{ {
$result = $object->fetch($id); $result = $object->fetch($id);
$object->fetch_thirdparty();
$result = $object->delete(); $result = $object->delete();
if ($result > 0) if ($result > 0)
{ {

View File

@ -1166,6 +1166,7 @@ class Facture extends CommonObject
function delete($rowid=0, $notrigger=0) function delete($rowid=0, $notrigger=0)
{ {
global $user,$langs,$conf; global $user,$langs,$conf;
require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
if (! $rowid) $rowid=$this->id; if (! $rowid) $rowid=$this->id;
@ -1236,6 +1237,34 @@ class Facture extends CommonObject
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) 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(); $this->db->commit();
return 1; return 1;
} }

View File

@ -23,7 +23,7 @@
//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','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('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
@ -45,7 +45,6 @@ error_reporting(E_ALL | E_STRICT);
$fk_element = GETPOST('fk_element','int'); $fk_element = GETPOST('fk_element','int');
$element = GETPOST('element','alpha'); $element = GETPOST('element','alpha');
$element_ref=GETPOST('element_ref','alpha');
/** /**
@ -68,19 +67,45 @@ class UploadHandler
* @param string $element element * @param string $element element
* @param string $element_ref element ref * @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->_fk_element=$fk_element;
$this->_element=$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( $this->_options = array(
'script_url' => $_SERVER['PHP_SELF'], 'script_url' => $_SERVER['PHP_SELF'],
'upload_dir' => $conf->$element->dir_output . '/' . $element_ref . '/', 'upload_dir' => $conf->$element->dir_output . '/' . $object->ref . '/',
'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$element_ref.'/', 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object->ref.'/',
'param_name' => 'files', 'param_name' => 'files',
// The php.ini settings upload_max_filesize and post_max_size // The php.ini settings upload_max_filesize and post_max_size
// take precedence over the following max_file_size setting: // take precedence over the following max_file_size setting:
@ -100,8 +125,8 @@ class UploadHandler
), ),
*/ */
'thumbs' => array( 'thumbs' => array(
'upload_dir' => $conf->$element->dir_output . '/' . $element_ref . '/thumbs/', 'upload_dir' => $conf->$element->dir_output . '/' . $object->ref . '/thumbs/',
'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$element_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 FILE_APPEND
); );
} else { } else {
// FIXME problem with trigger dol_move_uploaded_file($uploaded_file, $file_path, 1);
dol_move_uploaded_file($uploaded_file, $file_path, 1, 0, 0, 1);
} }
} else { } else {
// Non-multipart uploads (PUT method support) // Non-multipart uploads (PUT method support)
@ -384,7 +408,7 @@ class UploadHandler
* View * View
*/ */
$upload_handler = new UploadHandler(null,$fk_element,$element,$element_ref); $upload_handler = new UploadHandler(null,$fk_element,$element);
header('Pragma: no-cache'); header('Pragma: no-cache');
header('Cache-Control: private, no-cache'); header('Cache-Control: private, no-cache');

View File

@ -544,7 +544,7 @@ abstract class CommonObject
$this->thirdparty = $thirdparty; $this->thirdparty = $thirdparty;
// Use first price level if level not defined for third party // 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->client->price_level=1; // deprecated
$this->thirdparty->price_level=1; $this->thirdparty->price_level=1;

View File

@ -839,7 +839,7 @@ class FormFile
// Load existing files: // Load existing files:
// TODO do not delete // TODO do not delete
if (1 == 2) { 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"); var fu = $("#fileupload").data("fileupload");
fu._adjustMaxNumberOfFiles(-files.length); fu._adjustMaxNumberOfFiles(-files.length);
fu._renderDownload(files) fu._renderDownload(files)
@ -867,7 +867,6 @@ class FormFile
print '<form action="'.DOL_URL_ROOT.'/core/ajax/fileupload.php" method="POST" enctype="multipart/form-data">'; print '<form action="'.DOL_URL_ROOT.'/core/ajax/fileupload.php" method="POST" enctype="multipart/form-data">';
print '<input type="hidden" name="fk_element" value="'.$object->id.'">'; print '<input type="hidden" name="fk_element" value="'.$object->id.'">';
print '<input type="hidden" name="element" value="'.$object->element.'">'; print '<input type="hidden" name="element" value="'.$object->element.'">';
print '<input type="hidden" name="element_ref" value="'.$object->ref.'">';
print '<div class="fileupload-buttonbar">'; print '<div class="fileupload-buttonbar">';
print '<input type="hidden" name="protocol" value="http">'; print '<input type="hidden" name="protocol" value="http">';
print '<label class="fileinput-button">'; print '<label class="fileinput-button">';

View File

@ -596,16 +596,25 @@ function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disable
if (! empty($conf->global->MAIN_UMASK)) @chmod($file_name_osencoded, octdec($conf->global->MAIN_UMASK)); if (! empty($conf->global->MAIN_UMASK)) @chmod($file_name_osencoded, octdec($conf->global->MAIN_UMASK));
dol_syslog("Files.lib::dol_move_uploaded_file Success to move ".$src_file." to ".$file_name." - Umask=".$conf->global->MAIN_UMASK, LOG_DEBUG); dol_syslog("Files.lib::dol_move_uploaded_file Success to move ".$src_file." to ".$file_name." - Umask=".$conf->global->MAIN_UMASK, LOG_DEBUG);
if (! $notrigger && is_object($object)) if (! $notrigger)
{ {
$object->src_file=$dest_file; if (is_object($object))
{
$object->src_file=$dest_file;
// Appel des triggers // Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($db); $interface=new Interfaces($db);
$result=$interface->run_triggers('FILE_UPLOAD',$object,$user,$langs,$conf); $result=$interface->run_triggers('FILE_UPLOAD',$object,$user,$langs,$conf);
if ($result < 0) { $error++; $errors=$interface->errors; } if ($result < 0) {
// Fin appel triggers $error++; $errors=$interface->errors;
}
// Fin appel triggers
}
else
{
dol_syslog("Files.lib::dol_move_uploaded_file Object not find", LOG_WARNING);
}
} }
return 1; // Success return 1; // Success

View File

@ -23,7 +23,7 @@
*/ */
require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php"); require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
if ($conf->ldap->enabled) require_once (DOL_DOCUMENT_ROOT."/core/class/ldap.class.php"); if (! empty($conf->ldap->enabled)) require_once (DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
/** /**