PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Customer/Model/Attribute/Data/File.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 285 lines | 174 code | 32 blank | 79 comment | 43 complexity | 2b42ac42a98bdb9cc7cc4efb046e1fd2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  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@magentocommerce.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 Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Customer
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Customer Attribute File Data Model
  28. *
  29. * @category Mage
  30. * @package Mage_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Customer_Model_Attribute_Data_File extends Mage_Customer_Model_Attribute_Data_Abstract
  34. {
  35. /**
  36. * Validator for check not protected extensions
  37. *
  38. * @var Mage_Core_Model_File_Validator_NotProtectedExtension
  39. */
  40. protected $_validatorNotProtectedExtensions;
  41. /**
  42. * Extract data from request and return value
  43. *
  44. * @param Zend_Controller_Request_Http $request
  45. * @return array|string
  46. */
  47. public function extractValue(Zend_Controller_Request_Http $request)
  48. {
  49. if ($this->getIsAjaxRequest()) {
  50. return false;
  51. }
  52. $extend = $this->_getRequestValue($request);
  53. $attrCode = $this->getAttribute()->getAttributeCode();
  54. if ($this->_requestScope) {
  55. $value = array();
  56. if (strpos($this->_requestScope, '/') !== false) {
  57. $scopes = explode('/', $this->_requestScope);
  58. $mainScope = array_shift($scopes);
  59. } else {
  60. $mainScope = $this->_requestScope;
  61. $scopes = array();
  62. }
  63. if (!empty($_FILES[$mainScope])) {
  64. foreach ($_FILES[$mainScope] as $fileKey => $scopeData) {
  65. foreach ($scopes as $scopeName) {
  66. if (isset($scopeData[$scopeName])) {
  67. $scopeData = $scopeData[$scopeName];
  68. } else {
  69. $scopeData[$scopeName] = array();
  70. }
  71. }
  72. if (isset($scopeData[$attrCode])) {
  73. $value[$fileKey] = $scopeData[$attrCode];
  74. }
  75. }
  76. } else {
  77. $value = array();
  78. }
  79. } else {
  80. if (isset($_FILES[$attrCode])) {
  81. $value = $_FILES[$attrCode];
  82. } else {
  83. $value = array();
  84. }
  85. }
  86. if (!empty($extend['delete'])) {
  87. $value['delete'] = true;
  88. }
  89. return $value;
  90. }
  91. /**
  92. * Validate file by attribute validate rules
  93. * Return array of errors
  94. *
  95. * @param array $value
  96. * @return array
  97. */
  98. protected function _validateByRules($value)
  99. {
  100. $label = Mage::helper('customer')->__($this->getAttribute()->getStoreLabel());
  101. $rules = $this->getAttribute()->getValidateRules();
  102. $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
  103. if (!empty($rules['file_extensions'])) {
  104. $extensions = explode(',', $rules['file_extensions']);
  105. $extensions = array_map('trim', $extensions);
  106. if (!in_array($extension, $extensions)) {
  107. return array(
  108. Mage::helper('customer')->__('"%s" is not a valid file extension.', $label)
  109. );
  110. }
  111. }
  112. /**
  113. * Check protected file extension
  114. */
  115. /** @var $validator Mage_Core_Model_File_Validator_NotProtectedExtension */
  116. $validator = Mage::getSingleton('core/file_validator_notProtectedExtension');
  117. if (!$validator->isValid($extension)) {
  118. return $validator->getMessages();
  119. }
  120. if (!is_uploaded_file($value['tmp_name'])) {
  121. return array(
  122. Mage::helper('customer')->__('"%s" is not a valid file.', $label)
  123. );
  124. }
  125. if (!empty($rules['max_file_size'])) {
  126. $size = $value['size'];
  127. if ($rules['max_file_size'] < $size) {
  128. return array(
  129. Mage::helper('customer')->__('"%s" exceeds the allowed file size.', $label)
  130. );
  131. };
  132. }
  133. return array();
  134. }
  135. /**
  136. * Validate data
  137. *
  138. * @param array|string $value
  139. * @throws Mage_Core_Exception
  140. * @return boolean
  141. */
  142. public function validateValue($value)
  143. {
  144. $errors = array();
  145. $attribute = $this->getAttribute();
  146. $label = Mage::helper('customer')->__($attribute->getStoreLabel());
  147. if (is_array($value)) {
  148. $toDelete = !empty($value['delete']) ? true : false;
  149. $toUpload = !empty($value['tmp_name']) ? true : false;
  150. if (!$toUpload && !$toDelete && $this->getEntity()->getData($attribute->getAttributeCode())) {
  151. return true;
  152. }
  153. if (!$attribute->getIsRequired() && !$toUpload) {
  154. return true;
  155. }
  156. if ($attribute->getIsRequired() && !$toUpload) {
  157. $errors[] = Mage::helper('customer')->__('"%s" is a required value.', $label);
  158. }
  159. if ($toUpload) {
  160. $errors = array_merge($errors, $this->_validateByRules($value));
  161. }
  162. } else {
  163. $filePath = Mage::getBaseDir('media') . DS . 'customer' . $value;
  164. if ($attribute->getIsRequired() && !file_exists($filePath)) {
  165. $errors[] = Mage::helper('customer')->__('"%s" is a required value.', $label);
  166. }
  167. }
  168. if (count($errors) == 0) {
  169. return true;
  170. }
  171. return $errors;
  172. }
  173. /**
  174. * Export attribute value to entity model
  175. *
  176. * @param Mage_Core_Model_Abstract $entity
  177. * @param array|string $value
  178. * @return Mage_Customer_Model_Attribute_Data_File
  179. */
  180. public function compactValue($value)
  181. {
  182. if ($this->getIsAjaxRequest()) {
  183. return $this;
  184. }
  185. $attribute = $this->getAttribute();
  186. $original = $this->getEntity()->getData($attribute->getAttributeCode());
  187. $toDelete = false;
  188. if ($original) {
  189. if (!$attribute->getIsRequired() && !empty($value['delete'])) {
  190. $toDelete = true;
  191. }
  192. if (!empty($value['tmp_name'])) {
  193. $toDelete = true;
  194. }
  195. }
  196. $ioFile = new Varien_Io_File();
  197. $path = Mage::getBaseDir('media') . DS . 'customer';
  198. $ioFile->open(array('path' => $path));
  199. // unlink entity file
  200. if ($toDelete) {
  201. $this->getEntity()->setData($attribute->getAttributeCode(), '');
  202. $file = $path . $original;
  203. if ($ioFile->fileExists($file)) {
  204. $ioFile->rm($file);
  205. }
  206. }
  207. if (!empty($value['tmp_name'])) {
  208. try {
  209. $uploader = new Mage_Core_Model_File_Uploader($value);
  210. $uploader->setFilesDispersion(true);
  211. $uploader->setFilenamesCaseSensitivity(false);
  212. $uploader->setAllowRenameFiles(true);
  213. $uploader->save($path, $value['name']);
  214. $fileName = $uploader->getUploadedFileName();
  215. $this->getEntity()->setData($attribute->getAttributeCode(), $fileName);
  216. } catch (Exception $e) {
  217. Mage::logException($e);
  218. }
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Restore attribute value from SESSION to entity model
  224. *
  225. * @param array|string $value
  226. * @return Mage_Customer_Model_Attribute_Data_Abstract
  227. */
  228. public function restoreValue($value)
  229. {
  230. return $this;
  231. }
  232. /**
  233. * Return formated attribute value from entity model
  234. *
  235. * @return string|array
  236. */
  237. public function outputValue($format = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
  238. {
  239. $output = '';
  240. $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  241. if ($value) {
  242. switch ($format) {
  243. case Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_JSON:
  244. $output = array(
  245. 'value' => $value,
  246. 'url_key' => Mage::helper('core')->urlEncode($value)
  247. );
  248. break;
  249. }
  250. }
  251. return $output;
  252. }
  253. }