PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Mage.php

https://bitbucket.org/mengqing/magento-mirror
PHP | 989 lines | 517 code | 93 blank | 379 comment | 79 complexity | 3764ef2d38069630afc512623993ae45 MD5 | raw file
  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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. define('DS', DIRECTORY_SEPARATOR);
  27. define('PS', PATH_SEPARATOR);
  28. define('BP', dirname(dirname(__FILE__)));
  29. Mage::register('original_include_path', get_include_path());
  30. if (defined('COMPILER_INCLUDE_PATH')) {
  31. $appPath = COMPILER_INCLUDE_PATH;
  32. set_include_path($appPath . PS . Mage::registry('original_include_path'));
  33. include_once COMPILER_INCLUDE_PATH . DS . "Mage_Core_functions.php";
  34. include_once COMPILER_INCLUDE_PATH . DS . "Varien_Autoload.php";
  35. } else {
  36. /**
  37. * Set include path
  38. */
  39. $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
  40. $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
  41. $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
  42. $paths[] = BP . DS . 'lib';
  43. $appPath = implode(PS, $paths);
  44. set_include_path($appPath . PS . Mage::registry('original_include_path'));
  45. include_once "Mage/Core/functions.php";
  46. include_once "Varien/Autoload.php";
  47. }
  48. Varien_Autoload::register();
  49. /**
  50. * Main Mage hub class
  51. *
  52. * @author Magento Core Team <core@magentocommerce.com>
  53. */
  54. final class Mage
  55. {
  56. /**
  57. * Registry collection
  58. *
  59. * @var array
  60. */
  61. static private $_registry = array();
  62. /**
  63. * Application root absolute path
  64. *
  65. * @var string
  66. */
  67. static private $_appRoot;
  68. /**
  69. * Application model
  70. *
  71. * @var Mage_Core_Model_App
  72. */
  73. static private $_app;
  74. /**
  75. * Config Model
  76. *
  77. * @var Mage_Core_Model_Config
  78. */
  79. static private $_config;
  80. /**
  81. * Event Collection Object
  82. *
  83. * @var Varien_Event_Collection
  84. */
  85. static private $_events;
  86. /**
  87. * Object cache instance
  88. *
  89. * @var Varien_Object_Cache
  90. */
  91. static private $_objects;
  92. /**
  93. * Is downloader flag
  94. *
  95. * @var bool
  96. */
  97. static private $_isDownloader = false;
  98. /**
  99. * Is developer mode flag
  100. *
  101. * @var bool
  102. */
  103. static private $_isDeveloperMode = false;
  104. /**
  105. * Is allow throw Exception about headers already sent
  106. *
  107. * @var bool
  108. */
  109. public static $headersSentThrowsException = true;
  110. /**
  111. * Is installed flag
  112. *
  113. * @var bool
  114. */
  115. static private $_isInstalled;
  116. /**
  117. * Magento edition constants
  118. */
  119. const EDITION_COMMUNITY = 'Community';
  120. const EDITION_ENTERPRISE = 'Enterprise';
  121. const EDITION_PROFESSIONAL = 'Professional';
  122. const EDITION_GO = 'Go';
  123. /**
  124. * Current Magento edition.
  125. *
  126. * @var string
  127. * @static
  128. */
  129. static private $_currentEdition = self::EDITION_COMMUNITY;
  130. /**
  131. * Gets the current Magento version string
  132. * @link http://www.magentocommerce.com/blog/new-community-edition-release-process/
  133. *
  134. * @return string
  135. */
  136. public static function getVersion()
  137. {
  138. $i = self::getVersionInfo();
  139. return trim("{$i['major']}.{$i['minor']}.{$i['revision']}" . ($i['patch'] != '' ? ".{$i['patch']}" : "")
  140. . "-{$i['stability']}{$i['number']}", '.-');
  141. }
  142. /**
  143. * Gets the detailed Magento version information
  144. * @link http://www.magentocommerce.com/blog/new-community-edition-release-process/
  145. *
  146. * @return array
  147. */
  148. public static function getVersionInfo()
  149. {
  150. return array(
  151. 'major' => '1',
  152. 'minor' => '7',
  153. 'revision' => '0',
  154. 'patch' => '2',
  155. 'stability' => '',
  156. 'number' => '',
  157. );
  158. }
  159. /**
  160. * Get current Magento edition
  161. *
  162. * @static
  163. * @return string
  164. */
  165. public static function getEdition()
  166. {
  167. return self::$_currentEdition;
  168. }
  169. /**
  170. * Set all my static data to defaults
  171. *
  172. */
  173. public static function reset()
  174. {
  175. self::$_registry = array();
  176. self::$_appRoot = null;
  177. self::$_app = null;
  178. self::$_config = null;
  179. self::$_events = null;
  180. self::$_objects = null;
  181. self::$_isDownloader = false;
  182. self::$_isDeveloperMode = false;
  183. self::$_isInstalled = null;
  184. // do not reset $headersSentThrowsException
  185. }
  186. /**
  187. * Register a new variable
  188. *
  189. * @param string $key
  190. * @param mixed $value
  191. * @param bool $graceful
  192. * @throws Mage_Core_Exception
  193. */
  194. public static function register($key, $value, $graceful = false)
  195. {
  196. if (isset(self::$_registry[$key])) {
  197. if ($graceful) {
  198. return;
  199. }
  200. self::throwException('Mage registry key "'.$key.'" already exists');
  201. }
  202. self::$_registry[$key] = $value;
  203. }
  204. /**
  205. * Unregister a variable from register by key
  206. *
  207. * @param string $key
  208. */
  209. public static function unregister($key)
  210. {
  211. if (isset(self::$_registry[$key])) {
  212. if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key], '__destruct'))) {
  213. self::$_registry[$key]->__destruct();
  214. }
  215. unset(self::$_registry[$key]);
  216. }
  217. }
  218. /**
  219. * Retrieve a value from registry by a key
  220. *
  221. * @param string $key
  222. * @return mixed
  223. */
  224. public static function registry($key)
  225. {
  226. if (isset(self::$_registry[$key])) {
  227. return self::$_registry[$key];
  228. }
  229. return null;
  230. }
  231. /**
  232. * Set application root absolute path
  233. *
  234. * @param string $appRoot
  235. * @throws Mage_Core_Exception
  236. */
  237. public static function setRoot($appRoot = '')
  238. {
  239. if (self::$_appRoot) {
  240. return ;
  241. }
  242. if ('' === $appRoot) {
  243. // automagically find application root by dirname of Mage.php
  244. $appRoot = dirname(__FILE__);
  245. }
  246. $appRoot = realpath($appRoot);
  247. if (is_dir($appRoot) and is_readable($appRoot)) {
  248. self::$_appRoot = $appRoot;
  249. } else {
  250. self::throwException($appRoot . ' is not a directory or not readable by this user');
  251. }
  252. }
  253. /**
  254. * Retrieve application root absolute path
  255. *
  256. * @return string
  257. */
  258. public static function getRoot()
  259. {
  260. return self::$_appRoot;
  261. }
  262. /**
  263. * Retrieve Events Collection
  264. *
  265. * @return Varien_Event_Collection $collection
  266. */
  267. public static function getEvents()
  268. {
  269. return self::$_events;
  270. }
  271. /**
  272. * Varien Objects Cache
  273. *
  274. * @param string $key optional, if specified will load this key
  275. * @return Varien_Object_Cache
  276. */
  277. public static function objects($key = null)
  278. {
  279. if (!self::$_objects) {
  280. self::$_objects = new Varien_Object_Cache;
  281. }
  282. if (is_null($key)) {
  283. return self::$_objects;
  284. } else {
  285. return self::$_objects->load($key);
  286. }
  287. }
  288. /**
  289. * Retrieve application root absolute path
  290. *
  291. * @param string $type
  292. * @return string
  293. */
  294. public static function getBaseDir($type = 'base')
  295. {
  296. return self::getConfig()->getOptions()->getDir($type);
  297. }
  298. /**
  299. * Retrieve module absolute path by directory type
  300. *
  301. * @param string $type
  302. * @param string $moduleName
  303. * @return string
  304. */
  305. public static function getModuleDir($type, $moduleName)
  306. {
  307. return self::getConfig()->getModuleDir($type, $moduleName);
  308. }
  309. /**
  310. * Retrieve config value for store by path
  311. *
  312. * @param string $path
  313. * @param mixed $store
  314. * @return mixed
  315. */
  316. public static function getStoreConfig($path, $store = null)
  317. {
  318. return self::app()->getStore($store)->getConfig($path);
  319. }
  320. /**
  321. * Retrieve config flag for store by path
  322. *
  323. * @param string $path
  324. * @param mixed $store
  325. * @return bool
  326. */
  327. public static function getStoreConfigFlag($path, $store = null)
  328. {
  329. $flag = strtolower(self::getStoreConfig($path, $store));
  330. if (!empty($flag) && 'false' !== $flag) {
  331. return true;
  332. } else {
  333. return false;
  334. }
  335. }
  336. /**
  337. * Get base URL path by type
  338. *
  339. * @param string $type
  340. * @param null|bool $secure
  341. * @return string
  342. */
  343. public static function getBaseUrl($type = Mage_Core_Model_Store::URL_TYPE_LINK, $secure = null)
  344. {
  345. return self::app()->getStore()->getBaseUrl($type, $secure);
  346. }
  347. /**
  348. * Generate url by route and parameters
  349. *
  350. * @param string $route
  351. * @param array $params
  352. * @return string
  353. */
  354. public static function getUrl($route = '', $params = array())
  355. {
  356. return self::getModel('core/url')->getUrl($route, $params);
  357. }
  358. /**
  359. * Get design package singleton
  360. *
  361. * @return Mage_Core_Model_Design_Package
  362. */
  363. public static function getDesign()
  364. {
  365. return self::getSingleton('core/design_package');
  366. }
  367. /**
  368. * Retrieve a config instance
  369. *
  370. * @return Mage_Core_Model_Config
  371. */
  372. public static function getConfig()
  373. {
  374. return self::$_config;
  375. }
  376. /**
  377. * Add observer to even object
  378. *
  379. * @param string $eventName
  380. * @param callback $callback
  381. * @param array $arguments
  382. * @param string $observerName
  383. */
  384. public static function addObserver($eventName, $callback, $data = array(), $observerName = '', $observerClass = '')
  385. {
  386. if ($observerClass == '') {
  387. $observerClass = 'Varien_Event_Observer';
  388. }
  389. $observer = new $observerClass();
  390. $observer->setName($observerName)->addData($data)->setEventName($eventName)->setCallback($callback);
  391. return self::getEvents()->addObserver($observer);
  392. }
  393. /**
  394. * Dispatch event
  395. *
  396. * Calls all observer callbacks registered for this event
  397. * and multiple observers matching event name pattern
  398. *
  399. * @param string $name
  400. * @param array $data
  401. * @return Mage_Core_Model_App
  402. */
  403. public static function dispatchEvent($name, array $data = array())
  404. {
  405. Varien_Profiler::start('DISPATCH EVENT:'.$name);
  406. $result = self::app()->dispatchEvent($name, $data);
  407. Varien_Profiler::stop('DISPATCH EVENT:'.$name);
  408. return $result;
  409. }
  410. /**
  411. * Retrieve model object
  412. *
  413. * @link Mage_Core_Model_Config::getModelInstance
  414. * @param string $modelClass
  415. * @param array|object $arguments
  416. * @return Mage_Core_Model_Abstract|false
  417. */
  418. public static function getModel($modelClass = '', $arguments = array())
  419. {
  420. return self::getConfig()->getModelInstance($modelClass, $arguments);
  421. }
  422. /**
  423. * Retrieve model object singleton
  424. *
  425. * @param string $modelClass
  426. * @param array $arguments
  427. * @return Mage_Core_Model_Abstract
  428. */
  429. public static function getSingleton($modelClass='', array $arguments=array())
  430. {
  431. $registryKey = '_singleton/'.$modelClass;
  432. if (!self::registry($registryKey)) {
  433. self::register($registryKey, self::getModel($modelClass, $arguments));
  434. }
  435. return self::registry($registryKey);
  436. }
  437. /**
  438. * Retrieve object of resource model
  439. *
  440. * @param string $modelClass
  441. * @param array $arguments
  442. * @return Object
  443. */
  444. public static function getResourceModel($modelClass, $arguments = array())
  445. {
  446. return self::getConfig()->getResourceModelInstance($modelClass, $arguments);
  447. }
  448. /**
  449. * Retrieve Controller instance by ClassName
  450. *
  451. * @param string $class
  452. * @param Mage_Core_Controller_Request_Http $request
  453. * @param Mage_Core_Controller_Response_Http $response
  454. * @param array $invokeArgs
  455. * @return Mage_Core_Controller_Front_Action
  456. */
  457. public static function getControllerInstance($class, $request, $response, array $invokeArgs = array())
  458. {
  459. return new $class($request, $response, $invokeArgs);
  460. }
  461. /**
  462. * Retrieve resource vodel object singleton
  463. *
  464. * @param string $modelClass
  465. * @param array $arguments
  466. * @return object
  467. */
  468. public static function getResourceSingleton($modelClass = '', array $arguments = array())
  469. {
  470. $registryKey = '_resource_singleton/'.$modelClass;
  471. if (!self::registry($registryKey)) {
  472. self::register($registryKey, self::getResourceModel($modelClass, $arguments));
  473. }
  474. return self::registry($registryKey);
  475. }
  476. /**
  477. * Deprecated, use self::helper()
  478. *
  479. * @param string $type
  480. * @return object
  481. */
  482. public static function getBlockSingleton($type)
  483. {
  484. $action = self::app()->getFrontController()->getAction();
  485. return $action ? $action->getLayout()->getBlockSingleton($type) : false;
  486. }
  487. /**
  488. * Retrieve helper object
  489. *
  490. * @param string $name the helper name
  491. * @return Mage_Core_Helper_Abstract
  492. */
  493. public static function helper($name)
  494. {
  495. $registryKey = '_helper/' . $name;
  496. if (!self::registry($registryKey)) {
  497. $helperClass = self::getConfig()->getHelperClassName($name);
  498. self::register($registryKey, new $helperClass);
  499. }
  500. return self::registry($registryKey);
  501. }
  502. /**
  503. * Retrieve resource helper object
  504. *
  505. * @param string $moduleName
  506. * @return Mage_Core_Model_Resource_Helper_Abstract
  507. */
  508. public static function getResourceHelper($moduleName)
  509. {
  510. $registryKey = '_resource_helper/' . $moduleName;
  511. if (!self::registry($registryKey)) {
  512. $helperClass = self::getConfig()->getResourceHelper($moduleName);
  513. self::register($registryKey, $helperClass);
  514. }
  515. return self::registry($registryKey);
  516. }
  517. /**
  518. * Return new exception by module to be thrown
  519. *
  520. * @param string $module
  521. * @param string $message
  522. * @param integer $code
  523. * @return Mage_Core_Exception
  524. */
  525. public static function exception($module = 'Mage_Core', $message = '', $code = 0)
  526. {
  527. $className = $module . '_Exception';
  528. return new $className($message, $code);
  529. }
  530. /**
  531. * Throw Exception
  532. *
  533. * @param string $message
  534. * @param string $messageStorage
  535. * @throws Mage_Core_Exception
  536. */
  537. public static function throwException($message, $messageStorage = null)
  538. {
  539. if ($messageStorage && ($storage = self::getSingleton($messageStorage))) {
  540. $storage->addError($message);
  541. }
  542. throw new Mage_Core_Exception($message);
  543. }
  544. /**
  545. * Get initialized application object.
  546. *
  547. * @param string $code
  548. * @param string $type
  549. * @param string|array $options
  550. * @return Mage_Core_Model_App
  551. */
  552. public static function app($code = '', $type = 'store', $options = array())
  553. {
  554. if (null === self::$_app) {
  555. self::$_app = new Mage_Core_Model_App();
  556. self::setRoot();
  557. self::$_events = new Varien_Event_Collection();
  558. self::_setIsInstalled($options);
  559. self::_setConfigModel($options);
  560. Varien_Profiler::start('self::app::init');
  561. self::$_app->init($code, $type, $options);
  562. Varien_Profiler::stop('self::app::init');
  563. self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
  564. }
  565. return self::$_app;
  566. }
  567. /**
  568. * @static
  569. * @param string $code
  570. * @param string $type
  571. * @param array $options
  572. * @param string|array $modules
  573. */
  574. public static function init($code = '', $type = 'store', $options = array(), $modules = array())
  575. {
  576. try {
  577. self::setRoot();
  578. self::$_app = new Mage_Core_Model_App();
  579. self::_setIsInstalled($options);
  580. self::_setConfigModel($options);
  581. if (!empty($modules)) {
  582. self::$_app->initSpecified($code, $type, $options, $modules);
  583. } else {
  584. self::$_app->init($code, $type, $options);
  585. }
  586. } catch (Mage_Core_Model_Session_Exception $e) {
  587. header('Location: ' . self::getBaseUrl());
  588. die;
  589. } catch (Mage_Core_Model_Store_Exception $e) {
  590. require_once(self::getBaseDir() . DS . 'errors' . DS . '404.php');
  591. die;
  592. } catch (Exception $e) {
  593. self::printException($e);
  594. die;
  595. }
  596. }
  597. /**
  598. * Front end main entry point
  599. *
  600. * @param string $code
  601. * @param string $type
  602. * @param string|array $options
  603. */
  604. public static function run($code = '', $type = 'store', $options = array())
  605. {
  606. try {
  607. Varien_Profiler::start('mage');
  608. self::setRoot();
  609. if (isset($options['edition'])) {
  610. self::$_currentEdition = $options['edition'];
  611. }
  612. self::$_app = new Mage_Core_Model_App();
  613. if (isset($options['request'])) {
  614. self::$_app->setRequest($options['request']);
  615. }
  616. if (isset($options['response'])) {
  617. self::$_app->setResponse($options['response']);
  618. }
  619. self::$_events = new Varien_Event_Collection();
  620. self::_setIsInstalled($options);
  621. self::_setConfigModel($options);
  622. self::$_app->run(array(
  623. 'scope_code' => $code,
  624. 'scope_type' => $type,
  625. 'options' => $options,
  626. ));
  627. Varien_Profiler::stop('mage');
  628. } catch (Mage_Core_Model_Session_Exception $e) {
  629. header('Location: ' . self::getBaseUrl());
  630. die();
  631. } catch (Mage_Core_Model_Store_Exception $e) {
  632. require_once(self::getBaseDir() . DS . 'errors' . DS . '404.php');
  633. die();
  634. } catch (Exception $e) {
  635. if (self::isInstalled() || self::$_isDownloader) {
  636. self::printException($e);
  637. exit();
  638. }
  639. try {
  640. self::dispatchEvent('mage_run_exception', array('exception' => $e));
  641. if (!headers_sent()) {
  642. header('Location:' . self::getUrl('install'));
  643. } else {
  644. self::printException($e);
  645. }
  646. } catch (Exception $ne) {
  647. self::printException($ne, $e->getMessage());
  648. }
  649. }
  650. }
  651. /**
  652. * Set application isInstalled flag based on given options
  653. *
  654. * @param array $options
  655. */
  656. protected static function _setIsInstalled($options = array())
  657. {
  658. if (isset($options['is_installed']) && $options['is_installed']) {
  659. self::$_isInstalled = true;
  660. }
  661. }
  662. /**
  663. * Set application Config model
  664. *
  665. * @param array $options
  666. */
  667. protected static function _setConfigModel($options = array())
  668. {
  669. if (isset($options['config_model']) && class_exists($options['config_model'])) {
  670. $alternativeConfigModelName = $options['config_model'];
  671. unset($options['config_model']);
  672. $alternativeConfigModel = new $alternativeConfigModelName($options);
  673. } else {
  674. $alternativeConfigModel = null;
  675. }
  676. if (!is_null($alternativeConfigModel) && ($alternativeConfigModel instanceof Mage_Core_Model_Config)) {
  677. self::$_config = $alternativeConfigModel;
  678. } else {
  679. self::$_config = new Mage_Core_Model_Config($options);
  680. }
  681. }
  682. /**
  683. * Retrieve application installation flag
  684. *
  685. * @param string|array $options
  686. * @return bool
  687. */
  688. public static function isInstalled($options = array())
  689. {
  690. if (self::$_isInstalled === null) {
  691. self::setRoot();
  692. if (is_string($options)) {
  693. $options = array('etc_dir' => $options);
  694. }
  695. $etcDir = self::getRoot() . DS . 'etc';
  696. if (!empty($options['etc_dir'])) {
  697. $etcDir = $options['etc_dir'];
  698. }
  699. $localConfigFile = $etcDir . DS . 'local.xml';
  700. self::$_isInstalled = false;
  701. if (is_readable($localConfigFile)) {
  702. $localConfig = simplexml_load_file($localConfigFile);
  703. date_default_timezone_set('UTC');
  704. if (($date = $localConfig->global->install->date) && strtotime($date)) {
  705. self::$_isInstalled = true;
  706. }
  707. }
  708. }
  709. return self::$_isInstalled;
  710. }
  711. /**
  712. * log facility (??)
  713. *
  714. * @param string $message
  715. * @param integer $level
  716. * @param string $file
  717. * @param bool $forceLog
  718. */
  719. public static function log($message, $level = null, $file = '', $forceLog = false)
  720. {
  721. if (!self::getConfig()) {
  722. return;
  723. }
  724. try {
  725. $logActive = self::getStoreConfig('dev/log/active');
  726. if (empty($file)) {
  727. $file = self::getStoreConfig('dev/log/file');
  728. }
  729. }
  730. catch (Exception $e) {
  731. $logActive = true;
  732. }
  733. if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
  734. return;
  735. }
  736. static $loggers = array();
  737. $level = is_null($level) ? Zend_Log::DEBUG : $level;
  738. $file = empty($file) ? 'system.log' : $file;
  739. try {
  740. if (!isset($loggers[$file])) {
  741. $logDir = self::getBaseDir('var') . DS . 'log';
  742. $logFile = $logDir . DS . $file;
  743. if (!is_dir($logDir)) {
  744. mkdir($logDir);
  745. chmod($logDir, 0777);
  746. }
  747. if (!file_exists($logFile)) {
  748. file_put_contents($logFile, '');
  749. chmod($logFile, 0777);
  750. }
  751. $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
  752. $formatter = new Zend_Log_Formatter_Simple($format);
  753. $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
  754. if (!self::$_app || !$writerModel) {
  755. $writer = new Zend_Log_Writer_Stream($logFile);
  756. }
  757. else {
  758. $writer = new $writerModel($logFile);
  759. }
  760. $writer->setFormatter($formatter);
  761. $loggers[$file] = new Zend_Log($writer);
  762. }
  763. if (is_array($message) || is_object($message)) {
  764. $message = print_r($message, true);
  765. }
  766. $loggers[$file]->log($message, $level);
  767. }
  768. catch (Exception $e) {
  769. }
  770. }
  771. /**
  772. * Write exception to log
  773. *
  774. * @param Exception $e
  775. */
  776. public static function logException(Exception $e)
  777. {
  778. if (!self::getConfig()) {
  779. return;
  780. }
  781. $file = self::getStoreConfig('dev/log/exception_file');
  782. self::log("\n" . $e->__toString(), Zend_Log::ERR, $file);
  783. }
  784. /**
  785. * Set enabled developer mode
  786. *
  787. * @param bool $mode
  788. * @return bool
  789. */
  790. public static function setIsDeveloperMode($mode)
  791. {
  792. self::$_isDeveloperMode = (bool)$mode;
  793. return self::$_isDeveloperMode;
  794. }
  795. /**
  796. * Retrieve enabled developer mode
  797. *
  798. * @return bool
  799. */
  800. public static function getIsDeveloperMode()
  801. {
  802. return self::$_isDeveloperMode;
  803. }
  804. /**
  805. * Display exception
  806. *
  807. * @param Exception $e
  808. */
  809. public static function printException(Exception $e, $extra = '')
  810. {
  811. if (self::$_isDeveloperMode) {
  812. print '<pre>';
  813. if (!empty($extra)) {
  814. print $extra . "\n\n";
  815. }
  816. print $e->getMessage() . "\n\n";
  817. print $e->getTraceAsString();
  818. print '</pre>';
  819. } else {
  820. $reportData = array(
  821. !empty($extra) ? $extra . "\n\n" : '' . $e->getMessage(),
  822. $e->getTraceAsString()
  823. );
  824. // retrieve server data
  825. if (isset($_SERVER)) {
  826. if (isset($_SERVER['REQUEST_URI'])) {
  827. $reportData['url'] = $_SERVER['REQUEST_URI'];
  828. }
  829. if (isset($_SERVER['SCRIPT_NAME'])) {
  830. $reportData['script_name'] = $_SERVER['SCRIPT_NAME'];
  831. }
  832. }
  833. // attempt to specify store as a skin
  834. try {
  835. $storeCode = self::app()->getStore()->getCode();
  836. $reportData['skin'] = $storeCode;
  837. }
  838. catch (Exception $e) {}
  839. require_once(self::getBaseDir() . DS . 'errors' . DS . 'report.php');
  840. }
  841. die();
  842. }
  843. /**
  844. * Define system folder directory url by virtue of running script directory name
  845. * Try to find requested folder by shifting to domain root directory
  846. *
  847. * @param string $folder
  848. * @param boolean $exitIfNot
  849. * @return string
  850. */
  851. public static function getScriptSystemUrl($folder, $exitIfNot = false)
  852. {
  853. $runDirUrl = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
  854. $runDir = rtrim(dirname($_SERVER['SCRIPT_FILENAME']), DS);
  855. $baseUrl = null;
  856. if (is_dir($runDir.'/'.$folder)) {
  857. $baseUrl = str_replace(DS, '/', $runDirUrl);
  858. } else {
  859. $runDirUrlArray = explode('/', $runDirUrl);
  860. $runDirArray = explode('/', $runDir);
  861. $count = count($runDirArray);
  862. for ($i=0; $i < $count; $i++) {
  863. array_pop($runDirUrlArray);
  864. array_pop($runDirArray);
  865. $_runDir = implode('/', $runDirArray);
  866. if (!empty($_runDir)) {
  867. $_runDir .= '/';
  868. }
  869. if (is_dir($_runDir.$folder)) {
  870. $_runDirUrl = implode('/', $runDirUrlArray);
  871. $baseUrl = str_replace(DS, '/', $_runDirUrl);
  872. break;
  873. }
  874. }
  875. }
  876. if (is_null($baseUrl)) {
  877. $errorMessage = "Unable detect system directory: $folder";
  878. if ($exitIfNot) {
  879. // exit because of infinity loop
  880. exit($errorMessage);
  881. } else {
  882. self::printException(new Exception(), $errorMessage);
  883. }
  884. }
  885. return $baseUrl;
  886. }
  887. /**
  888. * Set is downloader flag
  889. *
  890. * @param bool $flag
  891. */
  892. public static function setIsDownloader($flag = true)
  893. {
  894. self::$_isDownloader = $flag;
  895. }
  896. }