diff --git a/classes/Image.php b/classes/Image.php index 2b95039bc..330c1cda1 100644 --- a/classes/Image.php +++ b/classes/Image.php @@ -1,6 +1,6 @@ 'isUnsignedId', 'position' => 'isUnsignedInt', 'cover' => 'isBool'); protected $fieldsRequiredLang = array('legend'); protected $fieldsSizeLang = array('legend' => 128); protected $fieldsValidateLang = array('legend' => 'isGenericName'); - + protected $table = 'image'; - protected $identifier = 'id_image'; - + protected $identifier = 'id_image'; + protected static $_cacheGetSize = array(); - + public function __construct($id = NULL, $id_lang = NULL) { parent::__construct($id, $id_lang); $this->image_dir = _PS_PROD_IMG_DIR_; $this->source_index = _PS_PROD_IMG_DIR_.'index.php'; } - + public function getFields() { $this->validateFields(); @@ -84,20 +87,20 @@ class ImageCore extends ObjectModel $fields['cover'] = (int)($this->cover); return $fields; } - + public function getTranslationsFieldsChild() { $this->validateFieldsLang(); return $this->getTranslationsFields(array('legend')); } - + public function delete() { - if (!parent::delete() || - !$this->deleteProductAttributeImage() || + if (!parent::delete() || + !$this->deleteProductAttributeImage() || !$this->deleteImage()) return false; - + // update positions $result = Db::getInstance()->ExecuteS(' SELECT * @@ -105,16 +108,16 @@ class ImageCore extends ObjectModel WHERE `id_product` = '.(int)$this->id_product.' ORDER BY `position`'); $i = 1; - if ($result) + if ($result) foreach ($result AS $row) { $row['position'] = $i++; Db::getInstance()->AutoExecute(_DB_PREFIX_.$this->table, $row, 'UPDATE', '`id_image` = '.(int)($row['id_image']), 1); } - + return true; } - + /** * Return available images for a product * @@ -131,7 +134,7 @@ class ImageCore extends ObjectModel WHERE i.`id_product` = '.(int)$id_product.' AND il.`id_lang` = '.(int)$id_lang.' ORDER BY i.`position` ASC'); } - + /** * Return Images * @@ -144,7 +147,7 @@ class ImageCore extends ObjectModel FROM `'._DB_PREFIX_.'image` ORDER BY `id_image` ASC'); } - + /** * Return number of images for a product * @@ -159,7 +162,7 @@ class ImageCore extends ObjectModel WHERE `id_product` = '.(int)($id_product)); return $result['total']; } - + /** * Return highest position of images for a product * @@ -174,7 +177,7 @@ class ImageCore extends ObjectModel WHERE `id_product` = '.(int)($id_product)); return $result['max']; } - + /** * Delete product cover * @@ -185,15 +188,15 @@ class ImageCore extends ObjectModel { if (!Validate::isUnsignedId($id_product)) die(Tools::displayError()); - + if (file_exists(_PS_TMP_IMG_DIR_.'product_'.$id_product.'.jpg')) unlink(_PS_TMP_IMG_DIR_.'product_'.$id_product.'.jpg'); return Db::getInstance()->Execute(' - UPDATE `'._DB_PREFIX_.'image` - SET `cover` = 0 + UPDATE `'._DB_PREFIX_.'image` + SET `cover` = 0 WHERE `id_product` = '.(int)($id_product)); } - + /** *Get product cover * @@ -203,11 +206,11 @@ class ImageCore extends ObjectModel public static function getCover($id_product) { return Db::getInstance()->getRow(' - SELECT * FROM `'._DB_PREFIX_.'image` + SELECT * FROM `'._DB_PREFIX_.'image` WHERE `id_product` = '.(int)($id_product).' AND `cover`= 1'); } - + /** * Copy images from a product to another * @@ -288,34 +291,34 @@ class ImageCore extends ObjectModel public function positionImage($position, $direction) { Tools::displayAsDeprecated(); - + $position = (int)($position); $direction = (int)($direction); - + // temporary position $high_position = Image::getHighestPosition($this->id_product) + 1; - + Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'image` SET `position` = '.(int)($high_position).' WHERE `id_product` = '.(int)($this->id_product).' AND `position` = '.($direction ? $position - 1 : $position + 1)); - + Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'image` SET `position` = `position`'.($direction ? '-1' : '+1').' WHERE `id_image` = '.(int)($this->id)); - + Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'image` SET `position` = '.$this->position.' WHERE `id_product` = '.(int)($this->id_product).' AND `position` = '.(int)($high_position)); } - + /** * Change an image position and update relative positions - * + * * @param int $way position is moved up if 0, moved down if 1 * @param int $position new position of the moved image * @return int success @@ -324,7 +327,7 @@ class ImageCore extends ObjectModel { if (!isset($this->id) || !$position) return false; - + // < and > statements rather than BETWEEN operator // since BETWEEN is treated differently according to databases $result = (Db::getInstance()->Execute(' @@ -342,14 +345,14 @@ class ImageCore extends ObjectModel return $result; } - + public static function getSize($type) { if (!isset(self::$_cacheGetSize[$type]) OR self::$_cacheGetSize[$type] === NULL) self::$_cacheGetSize[$type] = Db::getInstance()->getRow('SELECT `width`, `height` FROM '._DB_PREFIX_.'image_type WHERE `name` = \''.pSQL($type).'\''); return self::$_cacheGetSize[$type]; } - + /** * Clear all images in tmp dir */ @@ -370,7 +373,7 @@ class ImageCore extends ObjectModel WHERE `id_image` = '.(int)($this->id) ); } - + /** * Delete the product image from disk and remove the containing folder if empty * Handles both legacy and new image filesystems @@ -379,20 +382,20 @@ class ImageCore extends ObjectModel { if (!$this->id) return false; - + // Delete base image if (file_exists($this->image_dir.$this->getExistingImgPath().'.'.$this->image_format)) unlink($this->image_dir.$this->getExistingImgPath().'.'.$this->image_format); else return false; - + $files_to_delete = array(); - + // Delete auto-generated images $imageTypes = ImageType::getImagesTypes(); foreach ($imageTypes AS $imageType) $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-'.$imageType['name'].'.'.$this->image_format; - + // Delete watermark image $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-watermark.'.$this->image_format; // delete index.php @@ -400,11 +403,11 @@ class ImageCore extends ObjectModel // Delete tmp images $files_to_delete[] = _PS_TMP_IMG_DIR_.'product_'.$this->id_product.'.'.$this->image_format; $files_to_delete[] = _PS_TMP_IMG_DIR_.'product_mini_'.$this->id_product.'.'.$this->image_format; - + foreach ($files_to_delete as $file) if (file_exists($file) && !@unlink($file)) return false; - + // Can we delete the image folder? if (is_dir($this->image_dir.$this->getImgFolder())) { @@ -415,16 +418,16 @@ class ImageCore extends ObjectModel $delete_folder = false; break; } - } + } if (isset($delete_folder) && $delete_folder) @rmdir($this->image_dir.$this->getImgFolder()); return true; } - + /** * Recursively deletes all product images in the given folder tree and removes empty folders. - * + * * @param string $path folder containing the product images to delete * @param string $format image format * @return bool success @@ -436,11 +439,11 @@ class ImageCore extends ObjectModel foreach (scandir($path) as $file) { if (preg_match('/^[0-9]+(\-(.*))?\.'.$format.'$/', $file)) - unlink($path.$file); + unlink($path.$file); else if (is_dir($path.$file) && (preg_match('/^[0-9]$/', $file))) self::deleteAllImages($path.$file.'/', $format); } - + // Can we remove the image folder? $remove_folder = true; foreach (scandir($path) as $file) @@ -455,22 +458,22 @@ class ImageCore extends ObjectModel // we're only removing index.php if it's a folder we want to delete if (file_exists($path.'index.php')) @unlink ($path.'index.php'); - @rmdir($path); + @rmdir($path); } - + return true; } - + /** * Returns image path in the old or in the new filesystem - * + * * @ returns string image path */ public function getExistingImgPath() { if (!$this->id) return false; - + if (!$this->existing_path) { if (Configuration::get('PS_LEGACY_IMAGES') && file_exists(_PS_PROD_IMG_DIR_.$this->id_product.'-'.$this->id.'.'.$this->image_format)) @@ -481,62 +484,62 @@ class ImageCore extends ObjectModel return $this->existing_path; } - + /** * Returns the path to the folder containing the image in the new filesystem - * + * * @return string path to folder */ public function getImgFolder() { if (!$this->id) return false; - - if (!$this->folder) + + if (!$this->folder) $this->folder = self::getImgFolderStatic($this->id); return $this->folder; } - + /** * Create parent folders for the image in the new filesystem - * + * * @return bool success */ public function createImgFolder() { if (!$this->id) return false; - + if (!file_exists(_PS_PROD_IMG_DIR_.$this->getImgFolder())) { // Apparently sometimes mkdir cannot set the rights, and sometimes chmod can't. Trying both. - $success = @mkdir(_PS_PROD_IMG_DIR_.$this->getImgFolder(), 0755, true) - || @chmod(_PS_PROD_IMG_DIR_.$this->getImgFolder(), 0755); - + $success = @mkdir(_PS_PROD_IMG_DIR_.$this->getImgFolder(), $access_rights, true) + || @chmod(_PS_PROD_IMG_DIR_.$this->getImgFolder(), $access_rights); + // Create an index.php file in the new folder - if ($success + if ($success && !file_exists(_PS_PROD_IMG_DIR_.$this->getImgFolder().'index.php') && file_exists($this->source_index)) - return @copy($this->source_index, _PS_PROD_IMG_DIR_.$this->getImgFolder().'index.php'); + return @copy($this->source_index, _PS_PROD_IMG_DIR_.$this->getImgFolder().'index.php'); } return true; } - + /** * Returns the path to the image without file extension - * + * * @return string path */ public function getImgPath() { if (!$this->id) return false; - + $path = $this->getImgFolder().$this->id; return $path; } - + /** * Returns the path to the folder containing the image in the new filesystem * @@ -548,7 +551,7 @@ class ImageCore extends ObjectModel if (!is_numeric($id_image)) return false; $folders = str_split((string)$id_image); - return implode('/', $folders).'/'; + return implode('/', $folders).'/'; } /** @@ -578,7 +581,7 @@ class ImageCore extends ObjectModel // create the new folder if it does not exist if (!$image->createImgFolder()) return false; - + // if there's already a file at the new image path, move it to a dump folder // most likely the preexisting image is a demo image not linked to a product and it's ok to replace it $new_path = _PS_PROD_IMG_DIR_.$image->getImgPath().(isset($matches[3]) ? $matches[3] : '').'.jpg'; @@ -586,8 +589,8 @@ class ImageCore extends ObjectModel { if(!file_exists(_PS_PROD_IMG_DIR_.$tmp_folder)) { - @mkdir(_PS_PROD_IMG_DIR_.$tmp_folder, 0755); - @chmod(_PS_PROD_IMG_DIR_.$tmp_folder, 0755); + @mkdir(_PS_PROD_IMG_DIR_.$tmp_folder, $access_rights); + @chmod(_PS_PROD_IMG_DIR_.$tmp_folder, $access_rights); } $tmp_path = _PS_PROD_IMG_DIR_.$tmp_folder.basename($file); if (!@rename($new_path, $tmp_path) || !file_exists($tmp_path)) @@ -603,7 +606,7 @@ class ImageCore extends ObjectModel } return true; } - + public static function testFileSystem() { $safe_mode = ini_get('safe_mode'); @@ -618,8 +621,8 @@ class ImageCore extends ObjectModel } if (file_exists($test_folder)) return false; - @mkdir($test_folder, 0755, true); - @chmod($test_folder, 0755); + @mkdir($test_folder, $access_rights, true); + @chmod($test_folder, $access_rights); if (!is_writeable($test_folder)) return false; @rmdir($test_folder); @@ -628,11 +631,11 @@ class ImageCore extends ObjectModel return false; return true; } - + /** * Returns the path where a product image should be created (without file format) - * - * @return string path + * + * @return string path */ public function getPathForCreation() {