00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 define('DS', DIRECTORY_SEPARATOR);
00028 define('PS', PATH_SEPARATOR);
00029 define('BP', dirname(dirname(__FILE__)));
00030
00031 Mage::register('original_include_path', get_include_path());
00032
00033 if (defined('COMPILER_INCLUDE_PATH')) {
00034 $app_path = COMPILER_INCLUDE_PATH;
00035 set_include_path($app_path . PS . Mage::registry('original_include_path'));
00036 include_once "Mage_Core_functions.php";
00037 include_once "Varien_Autoload.php";
00038 } else {
00039
00040
00041
00042 $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
00043 $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
00044 $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
00045 $paths[] = BP . DS . 'lib';
00046
00047 $app_path = implode(PS, $paths);
00048 set_include_path($app_path . PS . Mage::registry('original_include_path'));
00049 include_once "Mage/Core/functions.php";
00050 include_once "Varien/Autoload.php";
00051 }
00052
00053 Varien_Autoload::register();
00054
00055
00056
00057
00058
00059
00060 final class Mage {
00061
00062
00063
00064
00065
00066 static private $_registry = array();
00067
00068
00069
00070
00071
00072
00073 static private $_app;
00074
00075 static private $_useCache = array();
00076
00077 static private $_objects;
00078
00079 static private $_isDownloader = false;
00080
00081 static private $_isDeveloperMode = false;
00082
00083 public static $headersSentThrowsException = true;
00084
00085 public static function getVersion()
00086 {
00087 return '1.3.2.1';
00088 }
00089
00090
00091
00092
00093
00094 public static function reset()
00095 {
00096 self::$_registry = array();
00097 self::$_app = null;
00098 self::$_useCache = array();
00099 self::$_objects = null;
00100 self::$_isDownloader = false;
00101 self::$_isDeveloperMode = false;
00102
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112 public static function register($key, $value, $graceful = false)
00113 {
00114 if(isset(self::$_registry[$key])) {
00115 if ($graceful) {
00116 return;
00117 }
00118 Mage::throwException('Mage registry key "'.$key.'" already exists');
00119 }
00120 self::$_registry[$key] = $value;
00121 }
00122
00123 public static function unregister($key)
00124 {
00125 if (isset(self::$_registry[$key])) {
00126 if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key],'__destruct'))) {
00127 self::$_registry[$key]->__destruct();
00128 }
00129 unset(self::$_registry[$key]);
00130 }
00131 }
00132
00133
00134
00135
00136
00137
00138
00139 public static function registry($key)
00140 {
00141 if (isset(self::$_registry[$key])) {
00142 return self::$_registry[$key];
00143 }
00144 return null;
00145 }
00146
00147
00148
00149
00150
00151
00152 public static function setRoot($appRoot='')
00153 {
00154 if (self::registry('appRoot')) {
00155 return ;
00156 }
00157 if (''===$appRoot) {
00158
00159 $appRoot = dirname(__FILE__);
00160 }
00161
00162 $appRoot = realpath($appRoot);
00163
00164 if (is_dir($appRoot) and is_readable($appRoot)) {
00165 Mage::register('appRoot', $appRoot);
00166 } else {
00167 Mage::throwException($appRoot.' is not a directory or not readable by this user');
00168 }
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 public static function getRoot()
00178 {
00179 return Mage::registry('appRoot');
00180 }
00181
00182
00183
00184
00185
00186
00187
00188 public static function objects($key=null)
00189 {
00190 if (!self::$_objects) {
00191 self::$_objects = new Varien_Object_Cache;
00192 }
00193 if (is_null($key)) {
00194 return self::$_objects;
00195 } else {
00196 return self::$_objects->load($key);
00197 }
00198 }
00199
00200
00201
00202
00203
00204
00205 public static function getBaseDir($type='base')
00206 {
00207 return Mage::getConfig()->getOptions()->getDir($type);
00208 }
00209
00210 public static function getModuleDir($type, $moduleName)
00211 {
00212 return Mage::getConfig()->getModuleDir($type, $moduleName);
00213 }
00214
00215 public static function getStoreConfig($path, $id=null)
00216 {
00217 return self::app()->getStore($id)->getConfig($path);
00218 }
00219
00220 public static function getStoreConfigFlag($path, $id=null)
00221 {
00222 $flag = strtolower(Mage::getStoreConfig($path, $id));
00223 if (!empty($flag) && 'false'!==$flag && '0'!==$flag) {
00224 return true;
00225 } else {
00226 return false;
00227 }
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 public static function getBaseUrl($type=Mage_Core_Model_Store::URL_TYPE_LINK, $secure=null)
00237 {
00238 return Mage::app()->getStore()->getBaseUrl($type, $secure);
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248 public static function getUrl($route='', $params=array())
00249 {
00250 return Mage::getModel('core/url')->getUrl($route, $params);
00251 }
00252
00253
00254
00255
00256
00257
00258 public static function getDesign()
00259 {
00260 return Mage::getSingleton('core/design_package');
00261 }
00262
00263
00264
00265
00266
00267
00268 public static function getConfig()
00269 {
00270 return Mage::registry('config');
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 public static function addObserver($eventName, $callback, $data=array(), $observerName='', $observerClass='')
00282 {
00283 if ($observerClass=='') {
00284 $observerClass = 'Varien_Event_Observer';
00285 }
00286 $observer = new $observerClass();
00287 $observer->setName($observerName)->addData($data)->setEventName($eventName)->setCallback($callback);
00288 return Mage::registry('events')->addObserver($observer);
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 public static function dispatchEvent($name, array $data=array())
00301 {
00302 Varien_Profiler::start('DISPATCH EVENT:'.$name);
00303 $result = Mage::app()->dispatchEvent($name, $data);
00304 #$result = Mage::registry('events')->dispatch($name, $data);
00305 Varien_Profiler::stop('DISPATCH EVENT:'.$name);
00306 return $result;
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 public static function getModel($modelClass='', $arguments=array())
00318 {
00319 return Mage::getConfig()->getModelInstance($modelClass, $arguments);
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329 public static function getSingleton($modelClass='', array $arguments=array())
00330 {
00331 $registryKey = '_singleton/'.$modelClass;
00332 if (!Mage::registry($registryKey)) {
00333 Mage::register($registryKey, Mage::getModel($modelClass, $arguments));
00334 }
00335 return Mage::registry($registryKey);
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345 public static function getResourceModel($modelClass, $arguments=array())
00346 {
00347 return Mage::getConfig()->getResourceModelInstance($modelClass, $arguments);
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357 public static function getResourceSingleton($modelClass='', array $arguments=array())
00358 {
00359 $registryKey = '_resource_singleton/'.$modelClass;
00360 if (!Mage::registry($registryKey)) {
00361 Mage::register($registryKey, Mage::getResourceModel($modelClass, $arguments));
00362 }
00363 return Mage::registry($registryKey);
00364 }
00365
00366
00367
00368
00369
00370
00371
00372 public static function getBlockSingleton($type)
00373 {
00374 $action = Mage::app()->getFrontController()->getAction();
00375 return $action ? $action->getLayout()->getBlockSingleton($type) : false;
00376 }
00377
00378
00379
00380
00381
00382
00383
00384 public static function helper($name)
00385 {
00386 return Mage::app()->getHelper($name);
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396 public static function exception($module='Mage_Core', $message='', $code=0)
00397 {
00398 $className = $module.'_Exception';
00399 return new $className($message, $code);
00400 }
00401
00402 public static function throwException($message, $messageStorage=null)
00403 {
00404 if ($messageStorage && ($storage = Mage::getSingleton($messageStorage))) {
00405 $storage->addError($message);
00406 }
00407 throw new Mage_Core_Exception($message);
00408 }
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 public static function app($code = '', $type = 'store', $options=array())
00419 {
00420 if (null === self::$_app) {
00421 Varien_Profiler::start('mage::app::construct');
00422 self::$_app = new Mage_Core_Model_App();
00423 Varien_Profiler::stop('mage::app::construct');
00424
00425 Mage::setRoot();
00426 Mage::register('events', new Varien_Event_Collection());
00427
00428
00429 Varien_Profiler::start('mage::app::register_config');
00430 Mage::register('config', new Mage_Core_Model_Config());
00431 Varien_Profiler::stop('mage::app::register_config');
00432
00433 Varien_Profiler::start('mage::app::init');
00434 self::$_app->init($code, $type, $options);
00435 Varien_Profiler::stop('mage::app::init');
00436
00437 self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
00438 }
00439 return self::$_app;
00440 }
00441
00442
00443
00444
00445
00446
00447
00448
00449 public static function run($code = '', $type = 'store', $options=array())
00450 {
00451 try {
00452 Varien_Profiler::start('mage');
00453
00454 Varien_Profiler::start('mage::app');
00455 self::app($code, $type, $options);
00456 Varien_Profiler::stop('mage::app');
00457
00458 Varien_Profiler::start('mage::dispatch');
00459 self::app()->getFrontController()->dispatch();
00460 Varien_Profiler::stop('mage::dispatch');
00461
00462 Varien_Profiler::stop('mage');
00463 }
00464 catch (Mage_Core_Model_Session_Exception $e) {
00465 header('Location: ' . Mage::getBaseUrl());
00466 die();
00467 }
00468 catch (Mage_Core_Model_Store_Exception $e) {
00469 $baseUrl = self::getScriptSystemUrl('404');
00470 if (!headers_sent()) {
00471 header('Location: ' . rtrim($baseUrl, '/').'/404/');
00472 }
00473 else {
00474 print '<script type="text/javascript">';
00475 print "window.location.href = '{$baseUrl}';";
00476 print '</script>';
00477 }
00478 die();
00479 }
00480 catch (Exception $e) {
00481 if (self::isInstalled() || self::$_isDownloader) {
00482 self::printException($e);
00483 exit();
00484 }
00485 try {
00486 self::dispatchEvent('mage_run_exception', array('exception' => $e));
00487 if (!headers_sent()) {
00488 header('Location:'.self::getUrl('install'));
00489 }
00490 else {
00491 self::printException($e);
00492 }
00493 }
00494 catch (Exception $ne) {
00495 self::printException($ne, $e->getMessage());
00496 }
00497 }
00498 }
00499
00500
00501
00502
00503
00504
00505
00506 public static function isInstalled($options = array())
00507 {
00508 $isInstalled = self::registry('_is_installed');
00509 if ($isInstalled === null) {
00510 self::setRoot();
00511
00512 if (is_string($options)) {
00513 $options = array(
00514 'etc_dir' => $options
00515 );
00516 }
00517 $etcDir = 'etc';
00518 if (!empty($options['etc_dir'])) {
00519 $etcDir = $options['etc_dir'];
00520 }
00521 $localConfigFile = self::getRoot() . DS . $etcDir . DS . 'local.xml';
00522
00523 $isInstalled = false;
00524
00525 if (is_readable($localConfigFile)) {
00526 $localConfig = simplexml_load_file($localConfigFile);
00527 date_default_timezone_set('UTC');
00528 if (($date = $localConfig->global->install->date) && strtotime($date)) {
00529 $isInstalled = true;
00530 }
00531 }
00532 self::register('_is_installed', $isInstalled);
00533 }
00534 return $isInstalled;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544 public static function log($message, $level=null, $file = '')
00545 {
00546 if (!self::getConfig()) {
00547 return;
00548 }
00549 if (!Mage::getStoreConfig('dev/log/active')) {
00550 return;
00551 }
00552
00553 static $loggers = array();
00554
00555 $level = is_null($level) ? Zend_Log::DEBUG : $level;
00556 if (empty($file)) {
00557 $file = Mage::getStoreConfig('dev/log/file');
00558 $file = empty($file) ? 'system.log' : $file;
00559 }
00560
00561 try {
00562 if (!isset($loggers[$file])) {
00563 $logFile = Mage::getBaseDir('var').DS.'log'.DS.$file;
00564 $logDir = Mage::getBaseDir('var').DS.'log';
00565
00566 if (!is_dir(Mage::getBaseDir('var').DS.'log')) {
00567 mkdir(Mage::getBaseDir('var').DS.'log', 0777);
00568 }
00569
00570 if (!file_exists($logFile)) {
00571 file_put_contents($logFile,'');
00572 chmod($logFile, 0777);
00573 }
00574
00575 $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
00576 $formatter = new Zend_Log_Formatter_Simple($format);
00577 $writer = new Zend_Log_Writer_Stream($logFile);
00578 $writer->setFormatter($formatter);
00579 $loggers[$file] = new Zend_Log($writer);
00580 }
00581
00582 if (is_array($message) || is_object($message)) {
00583 $message = print_r($message, true);
00584 }
00585
00586 $loggers[$file]->log($message, $level);
00587 }
00588 catch (Exception $e){
00589
00590 }
00591 }
00592
00593 public static function logException(Exception $e)
00594 {
00595 if (!self::getConfig()) {
00596 return;
00597 }
00598 $file = Mage::getStoreConfig('dev/log/exception_file');
00599 self::log("\n".$e->getTraceAsString(), Zend_Log::ERR, $file);
00600 }
00601
00602
00603
00604
00605
00606
00607
00608 public static function setIsDeveloperMode($mode)
00609 {
00610 self::$_isDeveloperMode = (bool)$mode;
00611 return self::$_isDeveloperMode;
00612 }
00613
00614
00615
00616
00617
00618
00619 public static function getIsDeveloperMode()
00620 {
00621 return self::$_isDeveloperMode;
00622 }
00623
00624
00625
00626
00627
00628
00629 public static function printException(Exception $e, $extra = '')
00630 {
00631 if (self::$_isDeveloperMode) {
00632 print '<pre>';
00633
00634 if (!empty($extra)) {
00635 print $extra . "\n\n";
00636 }
00637
00638 print $e->getMessage() . "\n\n";
00639 print $e->getTraceAsString();
00640 print '</pre>';
00641 }
00642 else {
00643 self::getConfig()->createDirIfNotExists(self::getBaseDir('var') . DS . 'report');
00644 $reportId = intval(microtime(true) * rand(100, 1000));
00645 $reportFile = self::getBaseDir('var') . DS . 'report' . DS . $reportId;
00646 $reportData = array(
00647 !empty($extra) ? $extra . "\n\n" : '' . $e->getMessage(),
00648 $e->getTraceAsString()
00649 );
00650 $reportData = serialize($reportData);
00651
00652 file_put_contents($reportFile, $reportData);
00653 chmod($reportFile, 0777);
00654
00655 $storeCode = 'default';
00656 try {
00657 $storeCode = self::app()->getStore()->getCode();
00658 }
00659 catch (Exception $e) {}
00660
00661 $baseUrl = self::getScriptSystemUrl('report', true);
00662 $reportUrl = rtrim($baseUrl, '/') . '/report/?id='
00663 . $reportId . '&s=' . $storeCode;
00664
00665 if (!headers_sent()) {
00666 header('Location: ' . $reportUrl);
00667 }
00668 else {
00669 print '<script type="text/javascript">';
00670 print "window.location.href = '{$reportUrl}';";
00671 print '</script>';
00672 }
00673 }
00674
00675 die();
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 public static function getScriptSystemUrl($folder, $exitIfNot = false)
00687 {
00688 $runDirUrl = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
00689 $runDir = rtrim(dirname($_SERVER['SCRIPT_FILENAME']), DS);
00690
00691 $baseUrl = null;
00692 if (is_dir($runDir.'/'.$folder)) {
00693 $baseUrl = str_replace(DS, '/', $runDirUrl);
00694 } else {
00695 $runDirUrlArray = explode('/', $runDirUrl);
00696 $runDirArray = explode('/', $runDir);
00697 $count = count($runDirArray);
00698
00699 for ($i=0; $i < $count; $i++) {
00700 array_pop($runDirUrlArray);
00701 array_pop($runDirArray);
00702 $_runDir = implode('/', $runDirArray);
00703 if (!empty($_runDir)) {
00704 $_runDir .= '/';
00705 }
00706
00707 if (is_dir($_runDir.$folder)) {
00708 $_runDirUrl = implode('/', $runDirUrlArray);
00709 $baseUrl = str_replace(DS, '/', $_runDirUrl);
00710 break;
00711 }
00712 }
00713 }
00714
00715 if (is_null($baseUrl)) {
00716 $errorMessage = "Unable detect system directory: $folder";
00717 if ($exitIfNot) {
00718
00719 exit($errorMessage);
00720 } else {
00721 self::printException(new Exception(), $errorMessage);
00722 }
00723 }
00724
00725 return $baseUrl;
00726 }
00727
00728 public static function setIsDownloader($flag=true)
00729 {
00730 self::$_isDownloader = $flag;
00731 }
00732 }