PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Backend/Model/Config/Backend/File.php

https://github.com/jonathanselander/magento2
PHP | 251 lines | 128 code | 20 blank | 103 comment | 21 complexity | b522b2901d307960c9af2b7870b433bb MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Unlicense
  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 Magento
  22. * @package Magento_Backend
  23. * @copyright Copyright (c) 2013 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. namespace Magento\Backend\Model\Config\Backend;
  27. /**
  28. * System config file field backend model
  29. *
  30. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  31. */
  32. class File extends \Magento\Core\Model\Config\Value
  33. {
  34. /**
  35. * @var \Magento\Backend\Model\Config\Backend\File\RequestData\RequestDataInterface
  36. */
  37. protected $_requestData;
  38. /**
  39. * Upload max file size in kilobytes
  40. *
  41. * @var int
  42. */
  43. protected $_maxFileSize = 0;
  44. /**
  45. * @var \Magento\Filesystem
  46. */
  47. protected $_filesystem;
  48. /**
  49. * @var \Magento\Filesystem\Directory\WriteInterface
  50. */
  51. protected $_mediaDirectory;
  52. /**
  53. * @var \Magento\Core\Model\File\UploaderFactory
  54. */
  55. protected $_uploaderFactory;
  56. /**
  57. * @param \Magento\Core\Model\Context $context
  58. * @param \Magento\Core\Model\Registry $registry
  59. * @param \Magento\Core\Model\StoreManagerInterface $storeManager
  60. * @param \Magento\Core\Model\Config $config
  61. * @param \Magento\Core\Model\File\UploaderFactory $uploaderFactory
  62. * @param \Magento\Backend\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData
  63. * @param \Magento\Filesystem $filesystem
  64. * @param \Magento\Core\Model\Resource\AbstractResource $resource
  65. * @param \Magento\Data\Collection\Db $resourceCollection
  66. * @param array $data
  67. */
  68. public function __construct(
  69. \Magento\Core\Model\Context $context,
  70. \Magento\Core\Model\Registry $registry,
  71. \Magento\Core\Model\StoreManagerInterface $storeManager,
  72. \Magento\Core\Model\Config $config,
  73. \Magento\Core\Model\File\UploaderFactory $uploaderFactory,
  74. \Magento\Backend\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData,
  75. \Magento\Filesystem $filesystem,
  76. \Magento\Core\Model\Resource\AbstractResource $resource = null,
  77. \Magento\Data\Collection\Db $resourceCollection = null,
  78. array $data = array()
  79. ) {
  80. $this->_uploaderFactory = $uploaderFactory;
  81. $this->_requestData = $requestData;
  82. $this->_filesystem = $filesystem;
  83. $this->_mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Filesystem::MEDIA);
  84. parent::__construct($context, $registry, $storeManager, $config, $resource, $resourceCollection, $data);
  85. }
  86. /**
  87. * Save uploaded file before saving config value
  88. *
  89. * @return \Magento\Backend\Model\Config\Backend\File
  90. * @throws \Magento\Core\Exception
  91. */
  92. protected function _beforeSave()
  93. {
  94. $value = $this->getValue();
  95. $tmpName = $this->_requestData->getTmpName($this->getPath());
  96. $file = array();
  97. if ($tmpName) {
  98. $file['tmp_name'] = $tmpName;
  99. $file['name'] = $this->_requestData->getName($this->getPath());
  100. } elseif (!empty($value['tmp_name'])) {
  101. $file['tmp_name'] = $value['tmp_name'];
  102. $file['name'] = $value['value'];
  103. }
  104. if (!empty($file)) {
  105. $uploadDir = $this->_getUploadDir();
  106. try {
  107. $uploader = $this->_uploaderFactory->create(array('fileId' => $file));
  108. $uploader->setAllowedExtensions($this->_getAllowedExtensions());
  109. $uploader->setAllowRenameFiles(true);
  110. $uploader->addValidateCallback('size', $this, 'validateMaxSize');
  111. $result = $uploader->save($uploadDir);
  112. } catch (\Exception $e) {
  113. throw new \Magento\Core\Exception($e->getMessage());
  114. }
  115. $filename = $result['file'];
  116. if ($filename) {
  117. if ($this->_addWhetherScopeInfo()) {
  118. $filename = $this->_prependScopeInfo($filename);
  119. }
  120. $this->setValue($filename);
  121. }
  122. } else {
  123. if (is_array($value) && !empty($value['delete'])) {
  124. $this->setValue('');
  125. } else {
  126. $this->unsValue();
  127. }
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Validation callback for checking max file size
  133. *
  134. * @param string $filePath Path to temporary uploaded file
  135. * @throws \Magento\Core\Exception
  136. */
  137. public function validateMaxSize($filePath)
  138. {
  139. $directory = $this->_filesystem->getDirectoryRead(\Magento\Filesystem::SYS_TMP);
  140. if (
  141. $this->_maxFileSize > 0 &&
  142. $directory->stat($directory->getRelativePath($filePath))['size'] > ($this->_maxFileSize * 1024)
  143. ) {
  144. throw new \Magento\Core\Exception(
  145. __('The file you\'re uploading exceeds the server size limit of %1 kilobytes.', $this->_maxFileSize)
  146. );
  147. }
  148. }
  149. /**
  150. * Makes a decision about whether to add info about the scope.
  151. *
  152. * @return boolean
  153. */
  154. protected function _addWhetherScopeInfo()
  155. {
  156. $fieldConfig = $this->getFieldConfig();
  157. $dirParams = array_key_exists('upload_dir', $fieldConfig) ? $fieldConfig['upload_dir'] : array();
  158. return (is_array($dirParams) && array_key_exists('scope_info', $dirParams) && $dirParams['scope_info']);
  159. }
  160. /**
  161. * Return path to directory for upload file
  162. *
  163. * @return string
  164. * @throws \Magento\Core\Exception
  165. */
  166. protected function _getUploadDir()
  167. {
  168. $fieldConfig = $this->getFieldConfig();
  169. /* @var $fieldConfig \Magento\Simplexml\Element */
  170. if (!array_key_exists('upload_dir', $fieldConfig)) {
  171. throw new \Magento\Core\Exception(
  172. __('The base directory to upload file is not specified.')
  173. );
  174. }
  175. if (is_array($fieldConfig['upload_dir'])) {
  176. $uploadDir = $fieldConfig['upload_dir']['value'];
  177. if (array_key_exists('scope_info', $fieldConfig['upload_dir'])
  178. && $fieldConfig['upload_dir']['scope_info']
  179. ) {
  180. $uploadDir = $this->_appendScopeInfo($uploadDir);
  181. }
  182. if (array_key_exists('config', $fieldConfig['upload_dir'])) {
  183. $uploadDir = $this->_mediaDirectory->getAbsolutePath($uploadDir);
  184. }
  185. } else {
  186. $uploadDir = (string) $fieldConfig['upload_dir'];
  187. }
  188. return $uploadDir;
  189. }
  190. /**
  191. * Prepend path with scope info
  192. *
  193. * E.g. 'stores/2/path' , 'websites/3/path', 'default/path'
  194. *
  195. * @param string $path
  196. * @return string
  197. */
  198. protected function _prependScopeInfo($path)
  199. {
  200. $scopeInfo = $this->getScope();
  201. if ('default' != $this->getScope()) {
  202. $scopeInfo .= '/' . $this->getScopeId();
  203. }
  204. return $scopeInfo . '/' . $path;
  205. }
  206. /**
  207. * Add scope info to path
  208. *
  209. * E.g. 'path/stores/2' , 'path/websites/3', 'path/default'
  210. *
  211. * @param string $path
  212. * @return string
  213. */
  214. protected function _appendScopeInfo($path)
  215. {
  216. $path .= '/' . $this->getScope();
  217. if ('default' != $this->getScope()) {
  218. $path .= '/' . $this->getScopeId();
  219. }
  220. return $path;
  221. }
  222. /**
  223. * Getter for allowed extensions of uploaded files
  224. *
  225. * @return array
  226. */
  227. protected function _getAllowedExtensions()
  228. {
  229. return array();
  230. }
  231. }