diff --git a/datamodels/2.x/itop-attachments/en.dict.itop-attachments.php b/datamodels/2.x/itop-attachments/en.dict.itop-attachments.php index 863aafbd8..9050799dd 100755 --- a/datamodels/2.x/itop-attachments/en.dict.itop-attachments.php +++ b/datamodels/2.x/itop-attachments/en.dict.itop-attachments.php @@ -35,6 +35,7 @@ Dict::Add('EN US', 'English', 'English', array( 'Attachment:Max_Ko' => '(Maximum file size: %1$s Ko)', 'Attachments:NoAttachment' => 'No attachment. ', 'Attachments:PreviewNotAvailable' => 'Preview not available for this type of attachment.', + 'Attachments:Error:FileTooLarge' => 'File is too large to be uploaded. %1$s', )); // diff --git a/datamodels/2.x/itop-attachments/fr.dict.itop-attachments.php b/datamodels/2.x/itop-attachments/fr.dict.itop-attachments.php index baca0b159..1ec402e00 100755 --- a/datamodels/2.x/itop-attachments/fr.dict.itop-attachments.php +++ b/datamodels/2.x/itop-attachments/fr.dict.itop-attachments.php @@ -35,6 +35,7 @@ Dict::Add('FR FR', 'French', 'Français', array( 'Attachment:Max_Ko' => '(Taille de fichier max.: %1$s Kb)', 'Attachments:NoAttachment' => 'Aucune pièce jointe.', 'Attachments:PreviewNotAvailable' => 'Pas d\'aperçu pour ce type de pièce jointe.', + 'Attachments:Error:FileTooLarge' => 'Le fichier est trop gros pour être chargé. %1$s', )); // diff --git a/datamodels/2.x/itop-attachments/main.attachments.php b/datamodels/2.x/itop-attachments/main.attachments.php index cc4e33609..df994e368 100755 --- a/datamodels/2.x/itop-attachments/main.attachments.php +++ b/datamodels/2.x/itop-attachments/main.attachments.php @@ -54,16 +54,42 @@ class AttachmentPlugIn implements iApplicationUIExtension, iApplicationObjectExt } } - protected function GetMaxUpload() + /** + * Returns the value of "upload_max_filesize" in bytes if upload allowed, false otherwise. + * + * @since 2.6.1 + * + * @return number|boolean + */ + public static function GetMaxUploadSize() { - $iMaxUpload = ini_get('upload_max_filesize'); + $sMaxUpload = ini_get('upload_max_filesize'); + if (!$sMaxUpload) + { + $result = false; + } + else + { + $result = utils::ConvertToBytes($sMaxUpload); + } + + return $result; + } + + /** + * Returns the max. file upload size allowed as a dictionary entry + * + * @return string + */ + public static function GetMaxUpload() + { + $iMaxUpload = static::GetMaxUploadSize(); if (!$iMaxUpload) { $sRet = Dict::S('Attachments:UploadNotAllowedOnThisSystem'); } else { - $iMaxUpload = utils::ConvertToBytes($iMaxUpload); if ($iMaxUpload > 1024*1024*1024) { $sRet = Dict::Format('Attachment:Max_Go', sprintf('%0.2f', $iMaxUpload/(1024*1024*1024))); @@ -327,13 +353,16 @@ EOF } } - $oPage->add(''); - $oPage->add('
'); - $sMaxUpload = $this->GetMaxUpload(); - $oPage->p(Dict::S('Attachments:AddAttachment').' '.$sMaxUpload); + $oPage->add(''); + $oPage->add('
'); + $iMaxUploadInBytes = $this->GetMaxUploadSize(); + $sMaxUploadLabel = $this->GetMaxUpload(); + $sFileTooBigLabel = Dict::Format('Attachments:Error:FileTooLarge', $sMaxUploadLabel); + $sFileTooBigLabelForJS = addslashes($sFileTooBigLabel); + $oPage->p(Dict::S('Attachments:AddAttachment').' '.$sMaxUploadLabel); $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.iframe-transport.js'); - $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.fileupload.js'); + $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.fileupload.js'); $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL; $oPage->add_ready_script( @@ -362,6 +391,21 @@ EOF } } }, + send: function(e, data){ + // Don't send attachment if size is greater than PHP post_max_size, otherwise it will break the request and all its parameters (\$_REQUEST, \$_POST, ...) + // Note: We loop on the files as the data structures is an array but in this case, we only upload 1 file at a time. + var iTotalSizeInBytes = 0; + for(var i = 0; i < data.files.length; i++) + { + iTotalSizeInBytes += data.files[i].size; + } + + if(iTotalSizeInBytes > $iMaxUploadInBytes) + { + alert('$sFileTooBigLabelForJS'); + return false; + } + }, start: function() { $('#attachment_loading').show(); }, diff --git a/datamodels/2.x/itop-portal-base/en.dict.itop-portal-base.php b/datamodels/2.x/itop-portal-base/en.dict.itop-portal-base.php index 7c71b90e9..04a841502 100644 --- a/datamodels/2.x/itop-portal-base/en.dict.itop-portal-base.php +++ b/datamodels/2.x/itop-portal-base/en.dict.itop-portal-base.php @@ -38,6 +38,7 @@ Dict::Add('EN US', 'English', 'English', array( 'Portal:Button:Delete' => 'Delete', 'Portal:EnvironmentBanner:Title' => 'You are currently in %1$s mode', 'Portal:EnvironmentBanner:GoToProduction' => 'Go back to PRODUCTION mode', + 'Error:HTTP:400' => 'Bad request', 'Error:HTTP:401' => 'Authentication', 'Error:HTTP:404' => 'Page not found', 'Error:HTTP:500' => 'Oops! An error has occured.', diff --git a/datamodels/2.x/itop-portal-base/fr.dict.itop-portal-base.php b/datamodels/2.x/itop-portal-base/fr.dict.itop-portal-base.php index 1f261d196..7a437ed7d 100644 --- a/datamodels/2.x/itop-portal-base/fr.dict.itop-portal-base.php +++ b/datamodels/2.x/itop-portal-base/fr.dict.itop-portal-base.php @@ -36,6 +36,7 @@ Dict::Add('FR FR', 'French', 'Français', array( 'Portal:Button:Delete' => 'Supprimer', 'Portal:EnvironmentBanner:Title' => 'Vous êtes dans le mode %1$s', 'Portal:EnvironmentBanner:GoToProduction' => 'Retourner au mode PRODUCTION', + 'Error:HTTP:400' => 'Requête incorrecte', 'Error:HTTP:401' => 'Authentification', 'Error:HTTP:404' => 'Page non trouvée', 'Error:HTTP:500' => 'Oups ! Une erreur est survenue.', diff --git a/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php b/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php index 9764fb36b..461d0e853 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php +++ b/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php @@ -1383,7 +1383,7 @@ class ObjectController extends AbstractController break; default: - $oApp->abort(403); + $oApp->abort(403, Dict::S('Error:HTTP:400')); break; } diff --git a/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php b/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php index 22c34a6f0..423f5b94a 100644 --- a/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php +++ b/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php @@ -24,6 +24,7 @@ use Dict; use InlineImage; use DBObjectSet; use DBObjectSearch; +use AttachmentPlugIn; use Combodo\iTop\Renderer\FieldRenderer; use Combodo\iTop\Renderer\RenderingOutput; @@ -89,6 +90,10 @@ class BsFileUploadFieldRenderer extends FieldRenderer $oOutput->AddHtml(''); // JS for file upload + $iMaxUploadInBytes = AttachmentPlugIn::GetMaxUploadSize(); + $sMaxUploadLabel = AttachmentPlugIn::GetMaxUpload(); + $sFileTooBigLabel = Dict::Format('Attachments:Error:FileTooLarge', $sMaxUploadLabel); + $sFileTooBigLabelForJS = addslashes($sFileTooBigLabel); // Note : This is based on itop-attachement/main.attachments.php $oOutput->AddJs( << $iMaxUploadInBytes) + { + alert('$sFileTooBigLabelForJS'); + return false; + } + }, start: function() { // Scrolling to dropzone so the user can see that attachments are uploaded $(this)[0].scrollIntoView();