1<?php 2/** 3 * Copyright (C) 2017-2019 thirty bees 4 * Copyright (C) 2007-2016 PrestaShop SA 5 * 6 * thirty bees is an extension to the PrestaShop software by PrestaShop SA. 7 * 8 * NOTICE OF LICENSE 9 * 10 * This source file is subject to the Academic Free License (AFL 3.0) 11 * that is bundled with this package in the file LICENSE.md. 12 * It is also available through the world-wide-web at this URL: 13 * https://opensource.org/licenses/afl-3.0.php 14 * If you did not receive a copy of the license and are unable to 15 * obtain it through the world-wide-web, please send an email 16 * to license@thirtybees.com so we can send you a copy immediately. 17 * 18 * @author thirty bees <modules@thirtybees.com> 19 * @author PrestaShop SA <contact@prestashop.com> 20 * @copyright 2017-2019 thirty bees 21 * @copyright 2007-2016 PrestaShop SA 22 * @license Academic Free License (AFL 3.0) 23 * PrestaShop is an internationally registered trademark of PrestaShop SA. 24 */ 25 26/** 27 * Class AdminBlockCategoriesController 28 * 29 * @since 1.0.0 30 */ 31class AdminBlockCategoriesController extends ModuleAdminController 32{ 33 /** 34 * @since 1.0.0 35 */ 36 public function postProcess() 37 { 38 if (($idThumb = Tools::getValue('deleteThumb', false)) !== false) { 39 if (file_exists(_PS_CAT_IMG_DIR_.(int) Tools::getValue('id_category').'-'.(int) $idThumb.'_thumb.jpg') 40 && !unlink(_PS_CAT_IMG_DIR_.(int) Tools::getValue('id_category').'-'.(int) $idThumb.'_thumb.jpg') 41 ) { 42 $this->context->controller->errors[] = Tools::displayError('Error while delete'); 43 } 44 45 if (empty($this->context->controller->errors)) { 46 Tools::clearSmartyCache(); 47 } 48 49 Tools::redirectAdmin(Context::getContext()->link->getAdminLink('AdminCategories').'&id_category='.(int) Tools::getValue('id_category').'&updatecategory'); 50 } 51 52 parent::postProcess(); 53 } 54 55 /** 56 * @since 1.0.0 57 */ 58 public function ajaxProcessuploadThumbnailImages() 59 { 60 $category = new Category((int) Tools::getValue('id_category')); 61 62 if (isset($_FILES['thumbnail'])) { 63 //Get total of image already present in directory 64 $files = scandir(_PS_CAT_IMG_DIR_); 65 $assignedKeys = []; 66 $allowedKeys = [0, 1, 2]; 67 foreach ($files as $file) { 68 $matches = []; 69 if (preg_match('/^'.$category->id.'-([0-9])?_thumb.jpg/i', $file, $matches) === 1) { 70 $assignedKeys[] = (int) $matches[1]; 71 } 72 } 73 74 $availableKeys = array_diff($allowedKeys, $assignedKeys); 75 $helper = new HelperImageUploader('thumbnail'); 76 $files = $helper->process(); 77 $totalErrors = []; 78 79 if (count($availableKeys) < count($files)) { 80 $totalErrors['name'] = sprintf(Tools::displayError('An error occurred while uploading the image :')); 81 $totalErrors['error'] = sprintf(Tools::displayError('You cannot upload more files')); 82 die(json_encode(['thumbnail' => [$totalErrors]])); 83 } 84 85 foreach ($files as $key => &$file) { 86 $id = array_shift($availableKeys); 87 $errors = []; 88 // Evaluate the memory required to resize the image: if it's too much, you can't resize it. 89 if (isset($file['save_path']) && !ImageManager::checkImageMemoryLimit($file['save_path'])) { 90 $errors[] = Tools::displayError('Due to memory limit restrictions, this image cannot be loaded. Please increase your memory_limit value via your server\'s configuration settings. '); 91 } 92 // Copy new image 93 if (!isset($file['save_path']) || (empty($errors) && !ImageManager::resize($file['save_path'], _PS_CAT_IMG_DIR_.(int) Tools::getValue('id_category').'-'.$id.'_thumb.jpg'))) { 94 $errors[] = Tools::displayError('An error occurred while uploading the image.'); 95 } 96 97 if (count($errors)) { 98 $totalErrors = array_merge($totalErrors, $errors); 99 } 100 101 if (isset($file['save_path']) && is_file($file['save_path'])) { 102 unlink($file['save_path']); 103 } 104 //Necesary to prevent hacking 105 if (isset($file['save_path'])) { 106 unset($file['save_path']); 107 } 108 109 if (isset($file['tmp_name'])) { 110 unset($file['tmp_name']); 111 } 112 113 //Add image preview and delete url 114 $file['image'] = ImageManager::thumbnail(_PS_CAT_IMG_DIR_.(int) $category->id.'-'.$id.'_thumb.jpg', $this->context->controller->table.'_'.(int) $category->id.'-'.$id.'_thumb.jpg', 100, 'jpg', true, true); 115 $file['delete_url'] = Context::getContext()->link->getAdminLink('AdminBlockCategories').'&deleteThumb='.$id.'&id_category='.(int) $category->id.'&updatecategory'; 116 } 117 118 if (count($totalErrors)) { 119 $this->context->controller->errors = array_merge($this->context->controller->errors, $totalErrors); 120 } else { 121 Tools::clearSmartyCache(); 122 } 123 124 die(json_encode(['thumbnail' => $files])); 125 } 126 } 127} 128