diff --git a/modules/qlohotelreview/classes/QhrHotelReview.php b/modules/qlohotelreview/classes/QhrHotelReview.php index 8768061d2..70ef425ed 100644 --- a/modules/qlohotelreview/classes/QhrHotelReview.php +++ b/modules/qlohotelreview/classes/QhrHotelReview.php @@ -290,9 +290,13 @@ class QhrHotelReview extends ObjectModel public function saveReviewImages() { $files = QhrHotelReviewHelper::fileAttachmentMultiple('images'); + $allowedExtensions = array('jpg', 'png', 'jpeg',); if (is_array($files) && count($files)) { foreach ($files as $key => $file) { $ext = pathinfo($file['rename'], PATHINFO_EXTENSION); + if (!in_array($ext, $allowedExtensions, true)) { + continue; + } $dir = _PS_MODULE_DIR_.'qlohotelreview/views/img/review/'.Image::getImgFolderStatic($this->id); QhrHotelReviewHelper::createDirectory($dir); $useSameExt = false; diff --git a/modules/qlohotelreview/controllers/front/default.php b/modules/qlohotelreview/controllers/front/default.php index 0156e3112..0d2ca4f45 100644 --- a/modules/qlohotelreview/controllers/front/default.php +++ b/modules/qlohotelreview/controllers/front/default.php @@ -85,6 +85,17 @@ class QloHotelReviewDefaultModuleFrontController extends ModuleFrontController } } + if (is_array($_FILES) && array_key_exists('images', $_FILES) ) { + $allowedExtensions = array('jpg', 'png', 'jpeg',); + foreach ($_FILES['images']['name'] as $key => $imageName) { + $extension = Tools::strtolower(pathinfo($imageName, PATHINFO_EXTENSION)); + if (!in_array($extension, $allowedExtensions)) { + $errors['general'][] = $objModule->l('Only images with the following extensions are allowed: jpg, jpeg, png','default'); + } + } + } + + if (!count($errors['by_key']) && !count($errors['general'])) { $customerReview = QhrHotelReview::getByCustomer( $objOrder->id_customer, diff --git a/modules/qlohotelreview/qlohotelreview.php b/modules/qlohotelreview/qlohotelreview.php index 405f12ff4..086265f15 100644 --- a/modules/qlohotelreview/qlohotelreview.php +++ b/modules/qlohotelreview/qlohotelreview.php @@ -182,6 +182,7 @@ class QloHotelReview extends Module $this->l('You can upload a maximum of %d images.', false, true), (int) Configuration::get('QHR_MAX_IMAGES_PER_REVIEW') ), + 'allowed_extensions' => $this->l('Only images with the following extensions are allowed: jpg, jpeg, png', false, true), ), ))); diff --git a/modules/qlohotelreview/views/css/front/review.css b/modules/qlohotelreview/views/css/front/review.css index 0c7346dce..539d10199 100644 --- a/modules/qlohotelreview/views/css/front/review.css +++ b/modules/qlohotelreview/views/css/front/review.css @@ -52,6 +52,10 @@ margin-bottom: 14px; } +#add-review-popup .hint { + margin-top: 6px; +} + #add-review-popup .review-error { margin-top: 2px; color: #E85757; diff --git a/modules/qlohotelreview/views/js/hook/review.js b/modules/qlohotelreview/views/js/hook/review.js index 57227636a..2db6b37b9 100644 --- a/modules/qlohotelreview/views/js/hook/review.js +++ b/modules/qlohotelreview/views/js/hook/review.js @@ -132,6 +132,29 @@ var QhrReviewImages = { init: function() { QhrReviewImages.inputHtml = ''; }, + validateFiles: function () { + $('#review-general-errors').hide(); + let isValid = true; + $('.images-field input.input-images').each(function (iInput, input) { + let files = input.files; + let dataTransfer = new DataTransfer(); + $.each(files, function (iFile, file) { + if (file.type.startsWith('image/')) { + dataTransfer.items.add(file); + } else { + isValid = false; + } + }); + input.files = dataTransfer.files; + if (!input.files.length) { + $(input).remove(); + } + }); + if (!isValid) { + QhrReviewForm.showGeneralErrors(qlo_hotel_review_js_vars.texts.allowed_extensions); + } + return isValid; + }, getFilesCount: function() { var count = 0; $('.images-field input.input-images').each(function(i, input) { @@ -156,6 +179,9 @@ var QhrReviewImages = { } }); input.files = dataTransfer.files; + if (input.files.length === 0) { + $(input).remove(); + } }, resetPreviews: function() { $('.previews-wrap').html(''); @@ -241,8 +267,9 @@ $(document).on('change', 'input.input-images', function(e) { var filesCount = QhrReviewImages.getFilesCount(); if (filesCount > qlo_hotel_review_js_vars.num_images_max) { QhrReviewImages.removeLastInput(); - alert(qlo_hotel_review_js_vars.texts.num_files); + QhrReviewForm.showGeneralErrors(qlo_hotel_review_js_vars.texts.num_files); } else { + QhrReviewImages.validateFiles(); QhrReviewImages.updatePreviews(); if (filesCount == qlo_hotel_review_js_vars.num_images_max) { diff --git a/modules/qlohotelreview/views/templates/hook/add-review-popup.tpl b/modules/qlohotelreview/views/templates/hook/add-review-popup.tpl index a838127d8..1ba6f6936 100644 --- a/modules/qlohotelreview/views/templates/hook/add-review-popup.tpl +++ b/modules/qlohotelreview/views/templates/hook/add-review-popup.tpl @@ -40,6 +40,7 @@
{l s='Allowed formats are: .jpg, .jpeg, .png' mod='qlohotelreview'}