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