PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/ProductDownload.php

https://github.com/netplayer/PrestaShop
PHP | 310 lines | 151 code | 32 blank | 127 comment | 12 complexity | 3e0ec3f69b235c849e27764fd0181093 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, 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 ProductDownloadCore extends ObjectModel
  27. {
  28. /** @var integer Product id which download belongs */
  29. public $id_product;
  30. /** @var string DisplayFilename the name which appear */
  31. public $display_filename;
  32. /** @var string PhysicallyFilename the name of the file on hard disk */
  33. public $filename;
  34. /** @var string DateDeposit when the file is upload */
  35. public $date_add;
  36. /** @var string DateExpiration deadline of the file */
  37. public $date_expiration;
  38. /** @var string NbDaysAccessible how many days the customer can access to file */
  39. public $nb_days_accessible;
  40. /** @var string NbDownloadable how many time the customer can download the file */
  41. public $nb_downloadable;
  42. /** @var boolean Active if file is accessible or not */
  43. public $active = 1;
  44. /** @var boolean is_shareable indicates whether the product can be shared */
  45. public $is_shareable = 0;
  46. protected static $_productIds = array();
  47. /**
  48. * @see ObjectModel::$definition
  49. */
  50. public static $definition = array(
  51. 'table' => 'product_download',
  52. 'primary' => 'id_product_download',
  53. 'fields' => array(
  54. 'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  55. 'display_filename' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
  56. 'filename' => array('type' => self::TYPE_STRING, 'validate' => 'isSha1', 'size' => 255),
  57. 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  58. 'date_expiration' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  59. 'nb_days_accessible' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'size' => 10),
  60. 'nb_downloadable' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'size' => 10),
  61. 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  62. 'is_shareable' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  63. ),
  64. );
  65. /**
  66. * Build a virtual product
  67. *
  68. * @param integer $id_product_download Existing productDownload id in order to load object (optional)
  69. */
  70. public function __construct($id_product_download = null)
  71. {
  72. parent::__construct($id_product_download);
  73. // @TODO check if the file is present on hard drive
  74. }
  75. /**
  76. * @see ObjectModel::getFields()
  77. * @return array
  78. */
  79. public function getFields()
  80. {
  81. $fields = parent::getFields();
  82. if (!$fields['date_expiration'])
  83. $fields['date_expiration'] = '0000-00-00 00:00:00';
  84. return $fields;
  85. }
  86. public function add($autodate = true, $null_values = false)
  87. {
  88. return (bool)parent::add($autodate, $null_values);
  89. }
  90. public function update($null_values = false)
  91. {
  92. if (parent::update($null_values))
  93. {
  94. // Refresh cache of feature detachable because the row can be deactive
  95. Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', ProductDownload::isCurrentlyUsed($this->def['table'], true));
  96. return true;
  97. }
  98. return false;
  99. }
  100. public function delete($delete_file = false)
  101. {
  102. $result = parent::delete();
  103. if ($result && $delete_file)
  104. return $this->deleteFile();
  105. return $result;
  106. }
  107. /**
  108. * Delete the file
  109. * @param int $id_product_download : if we need to delete a specific product attribute file
  110. *
  111. * @return boolean
  112. */
  113. public function deleteFile($id_product_download = null)
  114. {
  115. if (!$this->checkFile())
  116. return false;
  117. return unlink(_PS_DOWNLOAD_DIR_.$this->filename)
  118. && Db::getInstance()->delete('product_download', 'id_product_download = '.(int)$id_product_download);
  119. }
  120. /**
  121. * Check if file exists
  122. *
  123. * @return boolean
  124. */
  125. public function checkFile()
  126. {
  127. if (!$this->filename) return false;
  128. return file_exists(_PS_DOWNLOAD_DIR_.$this->filename);
  129. }
  130. /**
  131. * Check if download repository is writable
  132. *
  133. * @return boolean
  134. */
  135. public static function checkWritableDir()
  136. {
  137. return is_writable(_PS_DOWNLOAD_DIR_);
  138. }
  139. /**
  140. * Return the id_product_download from an id_product
  141. *
  142. * @param int $id_product Product the id
  143. * @return integer Product the id for this virtual product
  144. */
  145. public static function getIdFromIdProduct($id_product)
  146. {
  147. if (!ProductDownload::isFeatureActive())
  148. return false;
  149. if (array_key_exists((int)$id_product, self::$_productIds))
  150. return self::$_productIds[$id_product];
  151. self::$_productIds[$id_product] = (int)Db::getInstance()->getValue('
  152. SELECT `id_product_download`
  153. FROM `'._DB_PREFIX_.'product_download`
  154. WHERE `id_product` = '.(int)$id_product.'
  155. AND `active` = 1
  156. ORDER BY `id_product_download` DESC');
  157. return self::$_productIds[$id_product];
  158. }
  159. /**
  160. * Return the display filename from a physical filename
  161. *
  162. * @since 1.5.0.1
  163. *
  164. * @param string $filename Filename physically
  165. * @return integer Product the id for this virtual product
  166. *
  167. */
  168. public static function getIdFromFilename($filename)
  169. {
  170. return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  171. SELECT `id_product_download`
  172. FROM `'._DB_PREFIX_.'product_download`
  173. WHERE `filename` = \''.pSQL($filename).'\'');
  174. }
  175. /**
  176. * Return the filename from an id_product
  177. *
  178. * @param int $id_product Product the id
  179. * @return string Filename the filename for this virtual product
  180. */
  181. public static function getFilenameFromIdProduct($id_product)
  182. {
  183. return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  184. SELECT `filename`
  185. FROM `'._DB_PREFIX_.'product_download`
  186. WHERE `id_product` = '.(int)$id_product.'
  187. AND `active` = 1
  188. ');
  189. }
  190. /**
  191. * Return the display filename from a physical filename
  192. *
  193. * @param string $filename Filename physically
  194. * @return string Filename the display filename for this virtual product
  195. */
  196. public static function getFilenameFromFilename($filename)
  197. {
  198. return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  199. SELECT `display_filename`
  200. FROM `'._DB_PREFIX_.'product_download`
  201. WHERE `filename` = \''.pSQL($filename).'\'');
  202. }
  203. /**
  204. * Return html link
  205. *
  206. * @param string $class CSS selector (optionnal)
  207. * @param bool $admin specific to backend (optionnal)
  208. * @param string $hash hash code in table order detail (optionnal)
  209. * @return string Html all the code for print a link to the file
  210. */
  211. public function getTextLink($admin = true, $hash = false)
  212. {
  213. $key = $this->filename.'-'.($hash ? $hash : 'orderdetail');
  214. $link = ($admin) ? 'get-file-admin.php?' : _PS_BASE_URL_.__PS_BASE_URI__.'index.php?controller=get-file&';
  215. $link .= ($admin) ? 'file='.$this->filename : 'key='.$key;
  216. return $link;
  217. }
  218. /**
  219. * Return html link
  220. *
  221. * @param string $class CSS selector
  222. * @param bool $admin specific to backend
  223. * @param bool $hash hash code in table order detail
  224. * @return string Html all the code for print a link to the file
  225. */
  226. public function getHtmlLink($class = false, $admin = true, $hash = false)
  227. {
  228. $link = $this->getTextLink($admin, $hash);
  229. $html = '<a href="'.$link.'" title=""';
  230. if ($class)
  231. $html .= ' class="'.$class.'"';
  232. $html .= '>'.$this->display_filename.'</a>';
  233. return $html;
  234. }
  235. /**
  236. * Return a deadline
  237. *
  238. * @return string Datetime in SQL format
  239. */
  240. public function getDeadline()
  241. {
  242. if (!(int)$this->nb_days_accessible)
  243. return '0000-00-00 00:00:00';
  244. $timestamp = strtotime('+'.(int)$this->nb_days_accessible.' day');
  245. return date('Y-m-d H:i:s', $timestamp);
  246. }
  247. /**
  248. * Return a hash for control download access
  249. *
  250. * @return string Hash ready to insert in database
  251. */
  252. public function getHash()
  253. {
  254. // TODO check if this hash not already in database
  255. return sha1(microtime().$this->id);
  256. }
  257. /**
  258. * Return a sha1 filename
  259. *
  260. * @return string Sha1 unique filename
  261. */
  262. public static function getNewFilename()
  263. {
  264. do {
  265. $filename = sha1(microtime());
  266. } while (file_exists(_PS_DOWNLOAD_DIR_.$filename));
  267. return $filename;
  268. }
  269. /**
  270. * This method is allow to know if a feature is used or active
  271. * @since 1.5.0.1
  272. * @return bool
  273. */
  274. public static function isFeatureActive()
  275. {
  276. return Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE');
  277. }
  278. }