Better error management and better error message for reporting upload or

error of images.
This commit is contained in:
Laurent Destailleur 2014-10-18 14:56:06 +02:00
parent 97f98d7e16
commit 90d47cffae
7 changed files with 58 additions and 35 deletions

View File

@ -117,7 +117,7 @@ if ( ($action == 'update' && empty($_POST["cancel"]))
} }
else dol_syslog($imgThumbMini); else dol_syslog($imgThumbMini);
} }
else dol_syslog($langs->trans("ErrorImageFormatNotSupported"),LOG_WARNING); else dol_syslog("ErrorImageFormatNotSupported",LOG_WARNING);
} }
else if (preg_match('/^ErrorFileIsInfectedWithAVirus/',$result)) else if (preg_match('/^ErrorFileIsInfectedWithAVirus/',$result))
{ {
@ -135,7 +135,8 @@ if ( ($action == 'update' && empty($_POST["cancel"]))
else else
{ {
$error++; $error++;
setEventMessage($langs->trans("ErrorOnlyPngJpgSupported"),'errors'); $langs->load("errors");
setEventMessage($langs->trans("ErrorBadImageFormat"),'errors');
} }
} }
} }
@ -226,13 +227,15 @@ if ($action == 'addthumb')
else else
{ {
$error++; $error++;
setEventMessage($langs->trans("ErrorImageFormatNotSupported"),'errors'); $langs->load("errors");
dol_syslog($langs->transnoentities("ErrorImageFormatNotSupported"),LOG_WARNING); setEventMessage($langs->trans("ErrorBadImageFormat"),'errors');
dol_syslog($langs->transnoentities("ErrorBadImageFormat"),LOG_WARNING);
} }
} }
else else
{ {
$error++; $error++;
$langs->load("errors");
setEventMessage($langs->trans("ErrorFileDoesNotExists",$_GET["file"]),'errors'); setEventMessage($langs->trans("ErrorFileDoesNotExists",$_GET["file"]),'errors');
dol_syslog($langs->transnoentities("ErrorFileDoesNotExists",$_GET["file"]),LOG_WARNING); dol_syslog($langs->transnoentities("ErrorFileDoesNotExists",$_GET["file"]),LOG_WARNING);
} }

View File

@ -37,7 +37,7 @@ ErrorSupplierCodeRequired=Supplier code required
ErrorSupplierCodeAlreadyUsed=Supplier code already used ErrorSupplierCodeAlreadyUsed=Supplier code already used
ErrorBadParameters=Bad parameters ErrorBadParameters=Bad parameters
ErrorBadValueForParameter=Wrong value '%s' for parameter incorrect '%s' ErrorBadValueForParameter=Wrong value '%s' for parameter incorrect '%s'
ErrorBadImageFormat=Image file has not a supported format ErrorBadImageFormat=Image file has not a supported format (Your PHP does not support functions to convert images of this format)
ErrorBadDateFormat=Value '%s' has wrong date format ErrorBadDateFormat=Value '%s' has wrong date format
ErrorWrongDate=Date is not correct! ErrorWrongDate=Date is not correct!
ErrorFailedToWriteInDir=Failed to write in directory %s ErrorFailedToWriteInDir=Failed to write in directory %s

View File

@ -58,12 +58,12 @@ ErrorCantLoadUserFromDolibarrDatabase=Failed to find user <b>%s</b> in Dolibarr
ErrorNoVATRateDefinedForSellerCountry=Error, no vat rates defined for country '%s'. ErrorNoVATRateDefinedForSellerCountry=Error, no vat rates defined for country '%s'.
ErrorNoSocialContributionForSellerCountry=Error, no social contribution type defined for country '%s'. ErrorNoSocialContributionForSellerCountry=Error, no social contribution type defined for country '%s'.
ErrorFailedToSaveFile=Error, failed to save file. ErrorFailedToSaveFile=Error, failed to save file.
ErrorOnlyPngJpgSupported=Error, only .png and .jpg image format file are supported.
ErrorImageFormatNotSupported=Your PHP does not support functions to convert images of this format.
SetDate=Set date SetDate=Set date
SelectDate=Select a date SelectDate=Select a date
SeeAlso=See also %s SeeAlso=See also %s
BackgroundColorByDefault=Default background color BackgroundColorByDefault=Default background color
FileNotUploaded=The file was not uploaded
FileUploaded=The file was successfully uploaded
FileWasNotUploaded=A file is selected for attachment but was not yet uploaded. Click on "Attach file" for this. FileWasNotUploaded=A file is selected for attachment but was not yet uploaded. Click on "Attach file" for this.
NbOfEntries=Nb of entries NbOfEntries=Nb of entries
GoToWikiHelpPage=Read online help (need Internet access) GoToWikiHelpPage=Read online help (need Internet access)

View File

@ -55,8 +55,6 @@ NoOpenedPropals=No opened commercial proposals
NoOtherOpenedPropals=No other opened commercial proposals NoOtherOpenedPropals=No other opened commercial proposals
RefProposal=Commercial proposal ref RefProposal=Commercial proposal ref
SendPropalByMail=Send commercial proposal by mail SendPropalByMail=Send commercial proposal by mail
FileNotUploaded=The file was not uploaded
FileUploaded=The file was successfully uploaded
AssociatedDocuments=Documents associated with the proposal: AssociatedDocuments=Documents associated with the proposal:
ErrorCantOpenDir=Can't open directory ErrorCantOpenDir=Can't open directory
DatePropal=Date of proposal DatePropal=Date of proposal

View File

@ -46,6 +46,8 @@ class Product extends CommonObject
protected $isnolinkedbythird = 1; // No field fk_soc protected $isnolinkedbythird = 1; // No field fk_soc
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
var $regeximgext='\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff';
//! Identifiant unique //! Identifiant unique
var $id ; var $id ;
//! Ref //! Ref
@ -3068,18 +3070,19 @@ class Product extends CommonObject
} }
/** /**
* Deplace fichier uploade sous le nom $files dans le repertoire sdir * Move an uploaded file described into $file array into target directory $sdir.
* *
* @param string $sdir Repertoire destination finale * @param string $sdir Target directory
* @param string $file Nom du fichier uploade * @param string $file Array of file info of file to upload: array('name'=>..., 'tmp_name'=>...)
* @param int $maxWidth Largeur maximum que dois faire la miniature (160 par defaut) * @param int $maxWidth Largeur maximum que dois faire la miniature (160 by defaut)
* @param int $maxHeight Hauteur maximum que dois faire la miniature (120 par defaut) * @param int $maxHeight Hauteur maximum que dois faire la miniature (120 by defaut)
* @return void * @return int <0 if KO, >0 if OK
*/ */
function add_photo($sdir, $file, $maxWidth = 160, $maxHeight = 120) function add_photo($sdir, $file, $maxWidth = 160, $maxHeight = 120)
{ {
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$result = 0;
$dir = $sdir .'/'. get_exdir($this->id,2) . $this->id ."/photos"; $dir = $sdir .'/'. get_exdir($this->id,2) . $this->id ."/photos";
dol_mkdir($dir); dol_mkdir($dir);
@ -3098,6 +3101,9 @@ class Product extends CommonObject
$this->add_thumb($originImage,$maxWidth,$maxHeight); $this->add_thumb($originImage,$maxWidth,$maxHeight);
} }
} }
if (is_numeric($result) && $result > 0) return 1;
else return -1;
} }
/** /**
@ -3192,7 +3198,7 @@ class Product extends CommonObject
if (! utf8_check($file)) $file=utf8_encode($file); // To be sure file is stored in UTF8 in memory if (! utf8_check($file)) $file=utf8_encode($file); // To be sure file is stored in UTF8 in memory
if (dol_is_file($dir.$file) && preg_match('/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $dir.$file)) if (dol_is_file($dir.$file) && preg_match('/('.$this->regeximgext.')$/i', $dir.$file))
{ {
$nbphoto++; $nbphoto++;
$photo = $file; $photo = $file;
@ -3227,11 +3233,11 @@ class Product extends CommonObject
$alt.=' - '.$langs->transnoentitiesnoconv('Size').': '.$imgarray['width'].'x'.$imgarray['height']; $alt.=' - '.$langs->transnoentitiesnoconv('Size').': '.$imgarray['width'].'x'.$imgarray['height'];
if ($photo_vignette && $imgarray['height'] > $maxHeight) { if ($photo_vignette && $imgarray['height'] > $maxHeight) {
$return.= '<!-- Show thumb -->'; $return.= '<!-- Show thumb -->';
$return.= '<img class="photo" border="0" height="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdirthumb.$photo_vignette).'" title="'.dol_escape_htmltag($alt).'">'; $return.= '<img class="photo photowithmargin" border="0" height="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdirthumb.$photo_vignette).'" title="'.dol_escape_htmltag($alt).'">';
} }
else { else {
$return.= '<!-- Show original file -->'; $return.= '<!-- Show original file -->';
$return.= '<img class="photo" border="0" height="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'" title="'.dol_escape_htmltag($alt).'">'; $return.= '<img class="photo photowithmargin" border="0" height="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'" title="'.dol_escape_htmltag($alt).'">';
} }
$return.= '</a>'."\n"; $return.= '</a>'."\n";
@ -3241,7 +3247,7 @@ class Product extends CommonObject
{ {
$return.= '<br>'; $return.= '<br>';
// On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites // On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
if ($photo_vignette && preg_match('/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i', $photo) && ($product->imgWidth > $maxWidth || $product->imgHeight > $maxHeight)) if ($photo_vignette && preg_match('/('.$this->regeximgext.')$/i', $photo) && ($product->imgWidth > $maxWidth || $product->imgHeight > $maxHeight))
{ {
$return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=addthumb&amp;file='.urlencode($pdir.$viewfilename).'">'.img_picto($langs->trans('GenerateThumb'),'refresh').'&nbsp;&nbsp;</a>'; $return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=addthumb&amp;file='.urlencode($pdir.$viewfilename).'">'.img_picto($langs->trans('GenerateThumb'),'refresh').'&nbsp;&nbsp;</a>';
} }
@ -3266,7 +3272,7 @@ class Product extends CommonObject
} }
if ($size == 0) { // Format origine if ($size == 0) { // Format origine
$return.= '<img class="photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'">'; $return.= '<img class="photo photowithmargin" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'">';
if ($showfilename) $return.= '<br>'.$viewfilename; if ($showfilename) $return.= '<br>'.$viewfilename;
if ($showaction) if ($showaction)
@ -3334,14 +3340,14 @@ class Product extends CommonObject
while (($file = readdir($handle)) != false) while (($file = readdir($handle)) != false)
{ {
if (! utf8_check($file)) $file=utf8_encode($file); // readdir returns ISO if (! utf8_check($file)) $file=utf8_encode($file); // readdir returns ISO
if (dol_is_file($dir.$file) && preg_match('/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $dir.$file)) if (dol_is_file($dir.$file) && preg_match('/('.$this->regeximgext.')$/i', $dir.$file))
{ {
$nbphoto++; $nbphoto++;
// On determine nom du fichier vignette // On determine nom du fichier vignette
$photo=$file; $photo=$file;
$photo_vignette=''; $photo_vignette='';
if (preg_match('/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $photo, $regs)) if (preg_match('/('.$this->regeximgext.')$/i', $photo, $regs))
{ {
$photo_vignette=preg_replace('/'.$regs[0].'/i', '', $photo).'_small'.$regs[0]; $photo_vignette=preg_replace('/'.$regs[0].'/i', '', $photo).'_small'.$regs[0];
} }
@ -3385,7 +3391,7 @@ class Product extends CommonObject
dol_delete_file($file); dol_delete_file($file);
// Si elle existe, on efface la vignette // Si elle existe, on efface la vignette
if (preg_match('/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i',$filename,$regs)) if (preg_match('/('.$this->regeximgext.')$/i',$filename,$regs))
{ {
$photo_vignette=preg_replace('/'.$regs[0].'/i','',$filename).'_small'.$regs[0]; $photo_vignette=preg_replace('/'.$regs[0].'/i','',$filename).'_small'.$regs[0];
if (file_exists(dol_osencode($dirthumb.$photo_vignette))) if (file_exists(dol_osencode($dirthumb.$photo_vignette)))

View File

@ -26,6 +26,7 @@
require '../main.inc.php'; require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
@ -59,7 +60,20 @@ if ($id > 0 || ! empty($ref))
if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0 && GETPOST('sendit') && ! empty($conf->global->MAIN_UPLOAD_DOC)) if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0 && GETPOST('sendit') && ! empty($conf->global->MAIN_UPLOAD_DOC))
{ {
if ($object->id) $result = $object->add_photo($dir, $_FILES['userfile']); if ($object->id)
{
if (image_format_supported($_FILES['userfile']['name']) >= 1)
{
$result = $object->add_photo($dir, $_FILES['userfile']);
if ($result > 0) setEventMessage($langs->trans("FileUploaded"));
else setEventMessage($langs->trans("FileNotUploaded"), 'errors');
}
else
{
$langs->load("errors");
setEventMessage($langs->trans("ErrorBadImageFormat"), 'errors');
}
}
} }
if ($action == 'confirm_delete' && $_GET["file"] && $confirm == 'yes' && ($user->rights->produit->creer || $user->rights->service->creer)) if ($action == 'confirm_delete' && $_GET["file"] && $confirm == 'yes' && ($user->rights->produit->creer || $user->rights->service->creer))

View File

@ -2121,8 +2121,10 @@ div.dolgraph div.legend table tbody tr { height: auto; }
.photo { .photo {
border: 0px; border: 0px;
/* filter:alpha(opacity=55); */ }
/* opacity:.55; */ .photowithmargin {
margin-bottom: 2px;
margin-top: 2px;
} }
.logo_setup .logo_setup