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_Adminhtml
23: * @copyright Copyright (c) 2012 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: /**
28: * System config file field backend model
29: *
30: * @category Mage
31: * @package Mage_Adminhtml
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Adminhtml_Model_System_Config_Backend_File extends Mage_Core_Model_Config_Data
35: {
36: /**
37: * Upload max file size in kilobytes
38: *
39: * @var int
40: */
41: protected $_maxFileSize = 0;
42:
43: /**
44: * Save uploaded file before saving config value
45: *
46: * @return Mage_Adminhtml_Model_System_Config_Backend_File
47: */
48: protected function _beforeSave()
49: {
50: $value = $this->getValue();
51: if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']){
52:
53: $uploadDir = $this->_getUploadDir();
54:
55: try {
56: $file = array();
57: $tmpName = $_FILES['groups']['tmp_name'];
58: $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value'];
59: $name = $_FILES['groups']['name'];
60: $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value'];
61: $uploader = new Mage_Core_Model_File_Uploader($file);
62: $uploader->setAllowedExtensions($this->_getAllowedExtensions());
63: $uploader->setAllowRenameFiles(true);
64: $uploader->addValidateCallback('size', $this, 'validateMaxSize');
65: $result = $uploader->save($uploadDir);
66:
67: } catch (Exception $e) {
68: Mage::throwException($e->getMessage());
69: return $this;
70: }
71:
72: $filename = $result['file'];
73: if ($filename) {
74: if ($this->_addWhetherScopeInfo()) {
75: $filename = $this->_prependScopeInfo($filename);
76: }
77: $this->setValue($filename);
78: }
79: } else {
80: if (is_array($value) && !empty($value['delete'])) {
81: $this->setValue('');
82: } else {
83: $this->unsValue();
84: }
85: }
86:
87: return $this;
88: }
89:
90: /**
91: * Validation callback for checking max file size
92: *
93: * @param string $filePath Path to temporary uploaded file
94: * @throws Mage_Core_Exception
95: */
96: public function validateMaxSize($filePath)
97: {
98: if ($this->_maxFileSize > 0 && filesize($filePath) > ($this->_maxFileSize * 1024)) {
99: throw Mage::exception('Mage_Core', Mage::helper('adminhtml')->__('Uploaded file is larger than %.2f kilobytes allowed by server', $this->_maxFileSize));
100: }
101: }
102:
103: /**
104: * Makes a decision about whether to add info about the scope.
105: *
106: * @return boolean
107: */
108: protected function _addWhetherScopeInfo()
109: {
110: $fieldConfig = $this->getFieldConfig();
111: $el = $fieldConfig->descend('upload_dir');
112: return (!empty($el['scope_info']));
113: }
114:
115: /**
116: * Return path to directory for upload file
117: *
118: * @return string
119: * @throw Mage_Core_Exception
120: */
121: protected function _getUploadDir()
122: {
123: $fieldConfig = $this->getFieldConfig();
124: /* @var $fieldConfig Varien_Simplexml_Element */
125:
126: if (empty($fieldConfig->upload_dir)) {
127: Mage::throwException(Mage::helper('catalog')->__('The base directory to upload file is not specified.'));
128: }
129:
130: $uploadDir = (string)$fieldConfig->upload_dir;
131:
132: $el = $fieldConfig->descend('upload_dir');
133:
134: /**
135: * Add scope info
136: */
137: if (!empty($el['scope_info'])) {
138: $uploadDir = $this->_appendScopeInfo($uploadDir);
139: }
140:
141: /**
142: * Take root from config
143: */
144: if (!empty($el['config'])) {
145: $uploadRoot = $this->_getUploadRoot((string)$el['config']);
146: $uploadDir = $uploadRoot . '/' . $uploadDir;
147: }
148: return $uploadDir;
149: }
150:
151: /**
152: * Return the root part of directory path for uploading
153: *
154: * @var string
155: * @return string
156: */
157: protected function _getUploadRoot($token)
158: {
159: return Mage::getBaseDir('media');
160: }
161:
162: /**
163: * Prepend path with scope info
164: *
165: * E.g. 'stores/2/path' , 'websites/3/path', 'default/path'
166: *
167: * @param string $path
168: * @return string
169: */
170: protected function _prependScopeInfo($path)
171: {
172: $scopeInfo = $this->getScope();
173: if ('default' != $this->getScope()) {
174: $scopeInfo .= '/' . $this->getScopeId();
175: }
176: return $scopeInfo . '/' . $path;
177: }
178:
179: /**
180: * Add scope info to path
181: *
182: * E.g. 'path/stores/2' , 'path/websites/3', 'path/default'
183: *
184: * @param string $path
185: * @return string
186: */
187: protected function _appendScopeInfo($path)
188: {
189: $path .= '/' . $this->getScope();
190: if ('default' != $this->getScope()) {
191: $path .= '/' . $this->getScopeId();
192: }
193: return $path;
194: }
195:
196: /**
197: * Getter for allowed extensions of uploaded files
198: *
199: * @return array
200: */
201: protected function _getAllowedExtensions()
202: {
203: return array();
204: }
205: }
206: