PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Image.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 711 lines | 447 code | 80 blank | 184 comment | 90 complexity | f399f6badd7e69f301848d240957bd1e MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class ImageCore extends ObjectModel
  27. {
  28. public $id;
  29. /** @var integer Image ID */
  30. public $id_image;
  31. /** @var integer Product ID */
  32. public $id_product;
  33. /** @var integer Position used to order images of the same product */
  34. public $position;
  35. /** @var boolean Image is cover */
  36. public $cover;
  37. /** @var string Legend */
  38. public $legend;
  39. /** @var string image extension */
  40. public $image_format = 'jpg';
  41. /** @var string path to index.php file to be copied to new image folders */
  42. public $source_index;
  43. /** @var string image folder */
  44. protected $folder;
  45. /** @var string image path without extension */
  46. protected $existing_path;
  47. /** @var int access rights of created folders (octal) */
  48. protected static $access_rights = 0775;
  49. /**
  50. * @see ObjectModel::$definition
  51. */
  52. public static $definition = array(
  53. 'table' => 'image',
  54. 'primary' => 'id_image',
  55. 'multilang' => true,
  56. 'fields' => array(
  57. 'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  58. 'position' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  59. 'cover' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'shop' => true),
  60. 'legend' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
  61. ),
  62. );
  63. protected static $_cacheGetSize = array();
  64. public function __construct($id = null, $id_lang = null)
  65. {
  66. parent::__construct($id, $id_lang);
  67. $this->image_dir = _PS_PROD_IMG_DIR_;
  68. $this->source_index = _PS_PROD_IMG_DIR_.'index.php';
  69. }
  70. public function add($autodate = true, $null_values = false)
  71. {
  72. if ($this->position <= 0)
  73. $this->position = Image::getHighestPosition($this->id_product) + 1;
  74. return parent::add($autodate, $null_values);
  75. }
  76. public function delete()
  77. {
  78. if (!parent::delete())
  79. return false;
  80. if ($this->hasMultishopEntries())
  81. return true;
  82. if (!$this->deleteProductAttributeImage() || !$this->deleteImage())
  83. return false;
  84. // update positions
  85. $result = Db::getInstance()->executeS('
  86. SELECT *
  87. FROM `'._DB_PREFIX_.'image`
  88. WHERE `id_product` = '.(int)$this->id_product.'
  89. ORDER BY `position`
  90. ');
  91. $i = 1;
  92. if ($result)
  93. foreach ($result as $row)
  94. {
  95. $row['position'] = $i++;
  96. Db::getInstance()->update($this->def['table'], $row, '`id_image` = '.(int)$row['id_image'], 1);
  97. }
  98. return true;
  99. }
  100. /**
  101. * Return available images for a product
  102. *
  103. * @param integer $id_lang Language ID
  104. * @param integer $id_product Product ID
  105. * @param integer $id_product_attribute Product Attribute ID
  106. * @return array Images
  107. */
  108. public static function getImages($id_lang, $id_product, $id_product_attribute = NULL)
  109. {
  110. $attribute_filter = ($id_product_attribute ? ' AND ai.`id_product_attribute` = '.(int)$id_product_attribute : '');
  111. $sql = 'SELECT *
  112. FROM `'._DB_PREFIX_.'image` i
  113. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image`)';
  114. if ($id_product_attribute)
  115. $sql .= ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_image` ai ON (i.`id_image` = ai.`id_image`)';
  116. $sql .= ' WHERE i.`id_product` = '.(int)$id_product.' AND il.`id_lang` = '.(int)$id_lang . $attribute_filter.'
  117. ORDER BY i.`position` ASC';
  118. return Db::getInstance()->executeS($sql);
  119. }
  120. /**
  121. * Return Images
  122. *
  123. * @return array Images
  124. */
  125. public static function getAllImages()
  126. {
  127. return Db::getInstance()->executeS('
  128. SELECT `id_image`, `id_product`
  129. FROM `'._DB_PREFIX_.'image`
  130. ORDER BY `id_image` ASC');
  131. }
  132. /**
  133. * Return number of images for a product
  134. *
  135. * @param integer $id_product Product ID
  136. * @return integer number of images
  137. */
  138. public static function getImagesTotal($id_product)
  139. {
  140. $result = Db::getInstance()->getRow('
  141. SELECT COUNT(`id_image`) AS total
  142. FROM `'._DB_PREFIX_.'image`
  143. WHERE `id_product` = '.(int)$id_product);
  144. return $result['total'];
  145. }
  146. /**
  147. * Return highest position of images for a product
  148. *
  149. * @param integer $id_product Product ID
  150. * @return integer highest position of images
  151. */
  152. public static function getHighestPosition($id_product)
  153. {
  154. $result = Db::getInstance()->getRow('
  155. SELECT MAX(`position`) AS max
  156. FROM `'._DB_PREFIX_.'image`
  157. WHERE `id_product` = '.(int)$id_product);
  158. return $result['max'];
  159. }
  160. /**
  161. * Delete product cover
  162. *
  163. * @param integer $id_product Product ID
  164. * @return boolean result
  165. */
  166. public static function deleteCover($id_product)
  167. {
  168. if (!Validate::isUnsignedId($id_product))
  169. die(Tools::displayError());
  170. if (file_exists(_PS_TMP_IMG_DIR_.'product_'.$id_product.'.jpg'))
  171. unlink(_PS_TMP_IMG_DIR_.'product_'.$id_product.'.jpg');
  172. return (Db::getInstance()->execute('
  173. UPDATE `'._DB_PREFIX_.'image`
  174. SET `cover` = 0
  175. WHERE `id_product` = '.(int)$id_product
  176. ) &&
  177. Db::getInstance()->execute('
  178. UPDATE `'._DB_PREFIX_.'image` i, `'._DB_PREFIX_.'image_shop` image_shop
  179. SET image_shop.`cover` = 0
  180. WHERE image_shop.id_shop IN ('.implode(',', array_map('intval', Shop::getContextListShopID())).') AND image_shop.id_image = i.id_image AND i.`id_product` = '.(int)$id_product
  181. ));
  182. }
  183. /**
  184. *Get product cover
  185. *
  186. * @param integer $id_product Product ID
  187. * @return boolean result
  188. */
  189. public static function getCover($id_product)
  190. {
  191. return Db::getInstance()->getRow('
  192. SELECT * FROM `'._DB_PREFIX_.'image` i'.
  193. Shop::addSqlAssociation('image', 'i').'
  194. WHERE `id_product` = '.(int)$id_product.'
  195. AND image_shop.`cover`= 1');
  196. }
  197. /**
  198. * Copy images from a product to another
  199. *
  200. * @param integer $id_product_old Source product ID
  201. * @param boolean $id_product_new Destination product ID
  202. */
  203. public static function duplicateProductImages($id_product_old, $id_product_new, $combination_images)
  204. {
  205. $images_types = ImageType::getImagesTypes('products');
  206. $result = Db::getInstance()->executeS('
  207. SELECT `id_image`
  208. FROM `'._DB_PREFIX_.'image`
  209. WHERE `id_product` = '.(int)$id_product_old);
  210. foreach ($result as $row)
  211. {
  212. $image_old = new Image($row['id_image']);
  213. $image_new = clone $image_old;
  214. unset($image_new->id);
  215. $image_new->id_product = (int)$id_product_new;
  216. // A new id is generated for the cloned image when calling add()
  217. if ($image_new->add())
  218. {
  219. $new_path = $image_new->getPathForCreation();
  220. foreach ($images_types as $image_type)
  221. {
  222. if (file_exists(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'-'.$image_type['name'].'.jpg'))
  223. {
  224. if (!Configuration::get('PS_LEGACY_IMAGES'))
  225. $image_new->createImgFolder();
  226. copy(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'-'.$image_type['name'].'.jpg',
  227. $new_path.'-'.$image_type['name'].'.jpg');
  228. if (Configuration::get('WATERMARK_HASH'))
  229. copy(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'-'.$image_type['name'].'-'.Configuration::get('WATERMARK_HASH').'.jpg',
  230. $new_path.'-'.$image_type['name'].'-'.Configuration::get('WATERMARK_HASH').'.jpg');
  231. }
  232. }
  233. if (file_exists(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'.jpg'))
  234. copy(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'.jpg', $new_path.'.jpg');
  235. Image::replaceAttributeImageAssociationId($combination_images, (int)$image_old->id, (int)$image_new->id);
  236. // Duplicate shop associations for images
  237. $image_new->duplicateShops($id_product_old);
  238. }
  239. else
  240. return false;
  241. }
  242. return Image::duplicateAttributeImageAssociations($combination_images);
  243. }
  244. protected static function replaceAttributeImageAssociationId(&$combination_images, $saved_id, $id_image)
  245. {
  246. if (!isset($combination_images['new']) || !is_array($combination_images['new']))
  247. return;
  248. foreach ($combination_images['new'] as $id_product_attribute => $image_ids)
  249. foreach ($image_ids as $key => $image_id)
  250. if ((int)$image_id == (int)$saved_id)
  251. $combination_images['new'][$id_product_attribute][$key] = (int)$id_image;
  252. }
  253. /**
  254. * Duplicate product attribute image associations
  255. * @param integer $id_product_attribute_old
  256. * @return boolean
  257. */
  258. public static function duplicateAttributeImageAssociations($combination_images)
  259. {
  260. if (!isset($combination_images['new']) || !is_array($combination_images['new']))
  261. return true;
  262. $query = 'INSERT INTO `'._DB_PREFIX_.'product_attribute_image` (`id_product_attribute`, `id_image`) VALUES ';
  263. foreach ($combination_images['new'] as $id_product_attribute => $image_ids)
  264. foreach ($image_ids as $image_id)
  265. $query .= '('.(int)$id_product_attribute.', '.(int)$image_id.'), ';
  266. $query = rtrim($query, ', ');
  267. return DB::getInstance()->execute($query);
  268. }
  269. /**
  270. * Reposition image
  271. *
  272. * @param integer $position Position
  273. * @param boolean $direction Direction
  274. * @deprecated since version 1.5.0.1 use Image::updatePosition() instead
  275. */
  276. public function positionImage($position, $direction)
  277. {
  278. Tools::displayAsDeprecated();
  279. $position = (int)$position;
  280. $direction = (int)$direction;
  281. // temporary position
  282. $high_position = Image::getHighestPosition($this->id_product) + 1;
  283. Db::getInstance()->execute('
  284. UPDATE `'._DB_PREFIX_.'image`
  285. SET `position` = '.(int)$high_position.'
  286. WHERE `id_product` = '.(int)$this->id_product.'
  287. AND `position` = '.($direction ? $position - 1 : $position + 1));
  288. Db::getInstance()->execute('
  289. UPDATE `'._DB_PREFIX_.'image`
  290. SET `position` = `position`'.($direction ? '-1' : '+1').'
  291. WHERE `id_image` = '.(int)$this->id);
  292. Db::getInstance()->execute('
  293. UPDATE `'._DB_PREFIX_.'image`
  294. SET `position` = '.$this->position.'
  295. WHERE `id_product` = '.(int)$this->id_product.'
  296. AND `position` = '.(int)$high_position);
  297. }
  298. /**
  299. * Change an image position and update relative positions
  300. *
  301. * @param int $way position is moved up if 0, moved down if 1
  302. * @param int $position new position of the moved image
  303. * @return int success
  304. */
  305. public function updatePosition($way, $position)
  306. {
  307. if (!isset($this->id) || !$position)
  308. return false;
  309. // < and > statements rather than BETWEEN operator
  310. // since BETWEEN is treated differently according to databases
  311. $result = (Db::getInstance()->execute('
  312. UPDATE `'._DB_PREFIX_.'image`
  313. SET `position`= `position` '.($way ? '- 1' : '+ 1').'
  314. WHERE `position`
  315. '.($way
  316. ? '> '.(int)$this->position.' AND `position` <= '.(int)$position
  317. : '< '.(int)$this->position.' AND `position` >= '.(int)$position).'
  318. AND `id_product`='.(int)$this->id_product)
  319. && Db::getInstance()->execute('
  320. UPDATE `'._DB_PREFIX_.'image`
  321. SET `position` = '.(int)$position.'
  322. WHERE `id_image` = '.(int)$this->id_image));
  323. return $result;
  324. }
  325. public static function getSize($type)
  326. {
  327. if (!isset(self::$_cacheGetSize[$type]) || self::$_cacheGetSize[$type] === null)
  328. self::$_cacheGetSize[$type] = Db::getInstance()->getRow('
  329. SELECT `width`, `height`
  330. FROM '._DB_PREFIX_.'image_type
  331. WHERE `name` = \''.pSQL($type).'\'
  332. ');
  333. return self::$_cacheGetSize[$type];
  334. }
  335. public static function getWidth($params, &$smarty)
  336. {
  337. $result = self::getSize($params['type']);
  338. return $result['width'];
  339. }
  340. public static function getHeight($params, &$smarty)
  341. {
  342. $result = self::getSize($params['type']);
  343. return $result['height'];
  344. }
  345. /**
  346. * Clear all images in tmp dir
  347. */
  348. public static function clearTmpDir()
  349. {
  350. foreach (scandir(_PS_TMP_IMG_DIR_) as $d)
  351. if (preg_match('/(.*)\.jpg$/', $d))
  352. unlink(_PS_TMP_IMG_DIR_.$d);
  353. }
  354. /**
  355. * Delete Image - Product attribute associations for this image
  356. */
  357. public function deleteProductAttributeImage()
  358. {
  359. return Db::getInstance()->execute('
  360. DELETE
  361. FROM `'._DB_PREFIX_.'product_attribute_image`
  362. WHERE `id_image` = '.(int)$this->id
  363. );
  364. }
  365. /**
  366. * Delete the product image from disk and remove the containing folder if empty
  367. * Handles both legacy and new image filesystems
  368. */
  369. public function deleteImage($force_delete = false)
  370. {
  371. if (!$this->id)
  372. return false;
  373. // Delete base image
  374. if (file_exists($this->image_dir.$this->getExistingImgPath().'.'.$this->image_format))
  375. unlink($this->image_dir.$this->getExistingImgPath().'.'.$this->image_format);
  376. else
  377. return false;
  378. $files_to_delete = array();
  379. // Delete auto-generated images
  380. $image_types = ImageType::getImagesTypes();
  381. foreach ($image_types as $image_type)
  382. {
  383. $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-'.$image_type['name'].'.'.$this->image_format;
  384. if (Configuration::get('WATERMARK_HASH'))
  385. $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-'.$image_type['name'].'-'.Configuration::get('WATERMARK_HASH').'.'.$this->image_format;
  386. }
  387. // Delete watermark image
  388. $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-watermark.'.$this->image_format;
  389. // delete index.php
  390. $files_to_delete[] = $this->image_dir.$this->getImgFolder().'index.php';
  391. // Delete tmp images
  392. $files_to_delete[] = _PS_TMP_IMG_DIR_.'product_'.$this->id_product.'.'.$this->image_format;
  393. $files_to_delete[] = _PS_TMP_IMG_DIR_.'product_mini_'.$this->id_product.'.'.$this->image_format;
  394. foreach ($files_to_delete as $file)
  395. if (file_exists($file) && !@unlink($file))
  396. return false;
  397. // Can we delete the image folder?
  398. if (is_dir($this->image_dir.$this->getImgFolder()))
  399. {
  400. $delete_folder = true;
  401. foreach (scandir($this->image_dir.$this->getImgFolder()) as $file)
  402. if (($file != '.' && $file != '..'))
  403. {
  404. $delete_folder = false;
  405. break;
  406. }
  407. }
  408. if (isset($delete_folder) && $delete_folder)
  409. @rmdir($this->image_dir.$this->getImgFolder());
  410. return true;
  411. }
  412. /**
  413. * Recursively deletes all product images in the given folder tree and removes empty folders.
  414. *
  415. * @param string $path folder containing the product images to delete
  416. * @param string $format image format
  417. * @return bool success
  418. */
  419. public static function deleteAllImages($path, $format = 'jpg')
  420. {
  421. if (!$path || !$format || !is_dir($path))
  422. return false;
  423. foreach (scandir($path) as $file)
  424. {
  425. if (preg_match('/^[0-9]+(\-(.*))?\.'.$format.'$/', $file))
  426. unlink($path.$file);
  427. elseif (is_dir($path.$file) && (preg_match('/^[0-9]$/', $file)))
  428. Image::deleteAllImages($path.$file.'/', $format);
  429. }
  430. // Can we remove the image folder?
  431. if (is_numeric(basename($path)))
  432. {
  433. $remove_folder = true;
  434. foreach (scandir($path) as $file)
  435. if (($file != '.' && $file != '..' && $file != 'index.php'))
  436. {
  437. $remove_folder = false;
  438. break;
  439. }
  440. if ($remove_folder)
  441. {
  442. // we're only removing index.php if it's a folder we want to delete
  443. if (file_exists($path.'index.php'))
  444. @unlink ($path.'index.php');
  445. @rmdir($path);
  446. }
  447. }
  448. return true;
  449. }
  450. /**
  451. * Returns image path in the old or in the new filesystem
  452. *
  453. * @ returns string image path
  454. */
  455. public function getExistingImgPath()
  456. {
  457. if (!$this->id)
  458. return false;
  459. if (!$this->existing_path)
  460. {
  461. if (Configuration::get('PS_LEGACY_IMAGES') && file_exists(_PS_PROD_IMG_DIR_.$this->id_product.'-'.$this->id.'.'.$this->image_format))
  462. $this->existing_path = $this->id_product.'-'.$this->id;
  463. else
  464. $this->existing_path = $this->getImgPath();
  465. }
  466. return $this->existing_path;
  467. }
  468. /**
  469. * Returns the path to the folder containing the image in the new filesystem
  470. *
  471. * @return string path to folder
  472. */
  473. public function getImgFolder()
  474. {
  475. if (!$this->id)
  476. return false;
  477. if (!$this->folder)
  478. $this->folder = Image::getImgFolderStatic($this->id);
  479. return $this->folder;
  480. }
  481. /**
  482. * Create parent folders for the image in the new filesystem
  483. *
  484. * @return bool success
  485. */
  486. public function createImgFolder()
  487. {
  488. if (!$this->id)
  489. return false;
  490. if (!file_exists(_PS_PROD_IMG_DIR_.$this->getImgFolder()))
  491. {
  492. // Apparently sometimes mkdir cannot set the rights, and sometimes chmod can't. Trying both.
  493. $success = @mkdir(_PS_PROD_IMG_DIR_.$this->getImgFolder(), self::$access_rights, true);
  494. $chmod = @chmod(_PS_PROD_IMG_DIR_.$this->getImgFolder(), self::$access_rights);
  495. // Create an index.php file in the new folder
  496. if (($success || $chmod)
  497. && !file_exists(_PS_PROD_IMG_DIR_.$this->getImgFolder().'index.php')
  498. && file_exists($this->source_index))
  499. return @copy($this->source_index, _PS_PROD_IMG_DIR_.$this->getImgFolder().'index.php');
  500. }
  501. return true;
  502. }
  503. /**
  504. * Returns the path to the image without file extension
  505. *
  506. * @return string path
  507. */
  508. public function getImgPath()
  509. {
  510. if (!$this->id)
  511. return false;
  512. $path = $this->getImgFolder().$this->id;
  513. return $path;
  514. }
  515. /**
  516. * Returns the path to the folder containing the image in the new filesystem
  517. *
  518. * @param mixed $id_image
  519. * @return string path to folder
  520. */
  521. public static function getImgFolderStatic($id_image)
  522. {
  523. if (!is_numeric($id_image))
  524. return false;
  525. $folders = str_split((string)$id_image);
  526. return implode('/', $folders).'/';
  527. }
  528. /**
  529. * Move all legacy product image files from the image folder root to their subfolder in the new filesystem.
  530. * If max_execution_time is provided, stops before timeout and returns string "timeout".
  531. * If any image cannot be moved, stops and returns "false"
  532. *
  533. * @param int max_execution_time
  534. * @return mixed success or timeout
  535. */
  536. public static function moveToNewFileSystem($max_execution_time = 0)
  537. {
  538. $start_time = time();
  539. $image = null;
  540. $tmp_folder = 'duplicates/';
  541. foreach (scandir(_PS_PROD_IMG_DIR_) as $file)
  542. {
  543. // matches the base product image or the thumbnails
  544. if (preg_match('/^([0-9]+\-)([0-9]+)(\-(.*))?\.jpg$/', $file, $matches))
  545. {
  546. // don't recreate an image object for each image type
  547. if (!$image || $image->id !== (int)$matches[2])
  548. $image = new Image((int)$matches[2]);
  549. // image exists in DB and with the correct product?
  550. if (Validate::isLoadedObject($image) && $image->id_product == (int)rtrim($matches[1], '-'))
  551. {
  552. // create the new folder if it does not exist
  553. if (!$image->createImgFolder())
  554. return false;
  555. // if there's already a file at the new image path, move it to a dump folder
  556. // most likely the preexisting image is a demo image not linked to a product and it's ok to replace it
  557. $new_path = _PS_PROD_IMG_DIR_.$image->getImgPath().(isset($matches[3]) ? $matches[3] : '').'.jpg';
  558. if (file_exists($new_path))
  559. {
  560. if (!file_exists(_PS_PROD_IMG_DIR_.$tmp_folder))
  561. {
  562. @mkdir(_PS_PROD_IMG_DIR_.$tmp_folder, self::$access_rights);
  563. @chmod(_PS_PROD_IMG_DIR_.$tmp_folder, self::$access_rights);
  564. }
  565. $tmp_path = _PS_PROD_IMG_DIR_.$tmp_folder.basename($file);
  566. if (!@rename($new_path, $tmp_path) || !file_exists($tmp_path))
  567. return false;
  568. }
  569. // move the image
  570. if (!@rename(_PS_PROD_IMG_DIR_.$file, $new_path) || !file_exists($new_path))
  571. return false;
  572. }
  573. }
  574. if ((int)$max_execution_time != 0 && (time() - $start_time > (int)$max_execution_time - 4))
  575. return 'timeout';
  576. }
  577. return true;
  578. }
  579. /**
  580. * Try to create and delete some folders to check if moving images to new file system will be possible
  581. *
  582. * @return boolean success
  583. */
  584. public static function testFileSystem()
  585. {
  586. $safe_mode = Tools::getSafeModeStatus();
  587. if ($safe_mode)
  588. return false;
  589. $folder1 = _PS_PROD_IMG_DIR_.'testfilesystem/';
  590. $test_folder = $folder1.'testsubfolder/';
  591. // check if folders are already existing from previous failed test
  592. if (file_exists($test_folder))
  593. {
  594. @rmdir($test_folder);
  595. @rmdir($folder1);
  596. }
  597. if (file_exists($test_folder))
  598. return false;
  599. @mkdir($test_folder, self::$access_rights, true);
  600. @chmod($test_folder, self::$access_rights);
  601. if (!is_writeable($test_folder))
  602. return false;
  603. @rmdir($test_folder);
  604. @rmdir($folder1);
  605. if (file_exists($folder1))
  606. return false;
  607. return true;
  608. }
  609. /**
  610. * Returns the path where a product image should be created (without file format)
  611. *
  612. * @return string path
  613. */
  614. public function getPathForCreation()
  615. {
  616. if (!$this->id)
  617. return false;
  618. if (Configuration::get('PS_LEGACY_IMAGES'))
  619. {
  620. if (!$this->id_product)
  621. return false;
  622. $path = $this->id_product.'-'.$this->id;
  623. }
  624. else
  625. {
  626. $path = $this->getImgPath();
  627. $this->createImgFolder();
  628. }
  629. return _PS_PROD_IMG_DIR_.$path;
  630. }
  631. }