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_Core
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: * Configuration options storage and logic
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Core_Model_Config_Options extends Varien_Object
35: {
36: /**
37: * Var directory
38: *
39: * @var string
40: */
41: const VAR_DIRECTORY = 'var';
42: /**
43: * Flag cache for existing or already created directories
44: *
45: * @var array
46: */
47: protected $_dirExists = array();
48:
49: /**
50: * Initialize default values of the options
51: */
52: protected function _construct()
53: {
54: $appRoot= Mage::getRoot();
55: $root = dirname($appRoot);
56:
57: $this->_data['app_dir'] = $appRoot;
58: $this->_data['base_dir'] = $root;
59: $this->_data['code_dir'] = $appRoot.DS.'code';
60: $this->_data['design_dir'] = $appRoot.DS.'design';
61: $this->_data['etc_dir'] = $appRoot.DS.'etc';
62: $this->_data['lib_dir'] = $root.DS.'lib';
63: $this->_data['locale_dir'] = $appRoot.DS.'locale';
64: $this->_data['media_dir'] = $root.DS.'media';
65: $this->_data['skin_dir'] = $root.DS.'skin';
66: $this->_data['var_dir'] = $this->getVarDir();
67: $this->_data['tmp_dir'] = $this->_data['var_dir'].DS.'tmp';
68: $this->_data['cache_dir'] = $this->_data['var_dir'].DS.'cache';
69: $this->_data['log_dir'] = $this->_data['var_dir'].DS.'log';
70: $this->_data['session_dir'] = $this->_data['var_dir'].DS.'session';
71: $this->_data['upload_dir'] = $this->_data['media_dir'].DS.'upload';
72: $this->_data['export_dir'] = $this->_data['var_dir'].DS.'export';
73: }
74:
75: public function getDir($type)
76: {
77: $method = 'get'.ucwords($type).'Dir';
78: $dir = $this->$method();
79: if (!$dir) {
80: throw Mage::exception('Mage_Core', 'Invalid dir type requested: '.$type);
81: }
82: return $dir;
83: }
84:
85: public function getAppDir()
86: {
87: //return $this->getDataSetDefault('app_dir', Mage::getRoot());
88: return $this->_data['app_dir'];
89: }
90:
91: public function getBaseDir()
92: {
93: //return $this->getDataSetDefault('base_dir', dirname($this->getAppDir()));
94: return $this->_data['base_dir'];
95: }
96:
97: public function getCodeDir()
98: {
99: //return $this->getDataSetDefault('code_dir', $this->getAppDir().DS.'code');
100: return $this->_data['code_dir'];
101: }
102:
103: public function getDesignDir()
104: {
105: //return $this->getDataSetDefault('design_dir', $this->getAppDir().DS.'design');
106: return $this->_data['design_dir'];
107: }
108:
109: public function getEtcDir()
110: {
111: //return $this->getDataSetDefault('etc_dir', $this->getAppDir().DS.'etc');
112: return $this->_data['etc_dir'];
113: }
114:
115: public function getLibDir()
116: {
117: //return $this->getDataSetDefault('lib_dir', $this->getBaseDir().DS.'lib');
118: return $this->_data['lib_dir'];
119: }
120:
121: public function getLocaleDir()
122: {
123: //return $this->getDataSetDefault('locale_dir', $this->getAppDir().DS.'locale');
124: return $this->_data['locale_dir'];
125: }
126:
127: public function getMediaDir()
128: {
129: //return $this->getDataSetDefault('media_dir', $this->getBaseDir().DS.'media');
130: return $this->_data['media_dir'];
131: }
132:
133: public function getSkinDir()
134: {
135: //return $this->getDataSetDefault('skin_dir', $this->getBaseDir().DS.'skin');
136: return $this->_data['skin_dir'];
137: }
138:
139: public function getSysTmpDir()
140: {
141: return sys_get_temp_dir();
142: }
143:
144: public function getVarDir()
145: {
146: //$dir = $this->getDataSetDefault('var_dir', $this->getBaseDir().DS.'var');
147: $dir = isset($this->_data['var_dir']) ? $this->_data['var_dir']
148: : $this->_data['base_dir'] . DS . self::VAR_DIRECTORY;
149: if (!$this->createDirIfNotExists($dir)) {
150: $dir = $this->getSysTmpDir().DS.'magento'.DS.'var';
151: if (!$this->createDirIfNotExists($dir)) {
152: throw new Mage_Core_Exception('Unable to find writable var_dir');
153: }
154: }
155: return $dir;
156: }
157:
158: public function getTmpDir()
159: {
160: //$dir = $this->getDataSetDefault('tmp_dir', $this->getVarDir().DS.'tmp');
161: $dir = $this->_data['tmp_dir'];
162: if (!$this->createDirIfNotExists($dir)) {
163: $dir = $this->getSysTmpDir().DS.'magento'.DS.'tmp';
164: if (!$this->createDirIfNotExists($dir)) {
165: throw new Mage_Core_Exception('Unable to find writable tmp_dir');
166: }
167: }
168: return $dir;
169: }
170:
171: public function getCacheDir()
172: {
173: //$dir = $this->getDataSetDefault('cache_dir', $this->getVarDir().DS.'cache');
174: $dir = $this->_data['cache_dir'];
175: $this->createDirIfNotExists($dir);
176: return $dir;
177: }
178:
179: public function getLogDir()
180: {
181: //$dir = $this->getDataSetDefault('log_dir', $this->getVarDir().DS.'log');
182: $dir = $this->_data['log_dir'];
183: $this->createDirIfNotExists($dir);
184: return $dir;
185: }
186:
187: public function getSessionDir()
188: {
189: //$dir = $this->getDataSetDefault('session_dir', $this->getVarDir().DS.'session');
190: $dir = $this->_data['session_dir'];
191: $this->createDirIfNotExists($dir);
192: return $dir;
193: }
194:
195: public function getUploadDir()
196: {
197: //$dir = $this->getDataSetDefault('upload_dir', $this->getMediaDir().DS.'upload');
198: $dir = $this->_data['upload_dir'];
199: $this->createDirIfNotExists($dir);
200: return $dir;
201: }
202:
203: public function getExportDir()
204: {
205: //$dir = $this->getDataSetDefault('export_dir', $this->getVarDir().DS.'export');
206: $dir = $this->_data['export_dir'];
207: $this->createDirIfNotExists($dir);
208: return $dir;
209: }
210:
211: public function createDirIfNotExists($dir)
212: {
213: if (!empty($this->_dirExists[$dir])) {
214: return true;
215: }
216: if (file_exists($dir)) {
217: if (!is_dir($dir)) {
218: return false;
219: }
220: if (!is_dir_writeable($dir)) {
221: return false;
222: }
223: } else {
224: $oldUmask = umask(0);
225: if (!@mkdir($dir, 0777, true)) {
226: return false;
227: }
228: umask($oldUmask);
229: }
230: $this->_dirExists[$dir] = true;
231: return true;
232: }
233: }
234: