maruto 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,989 @@
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
+
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
+ * Set include path
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
+ * Main Mage hub class
57
+ *
58
+ * @author Magento Core Team <core@magentocommerce.com>
59
+ */
60
+ final class Mage
61
+ {
62
+ /**
63
+ * Registry collection
64
+ *
65
+ * @var array
66
+ */
67
+ static private $_registry = array();
68
+
69
+ /**
70
+ * Application root absolute path
71
+ *
72
+ * @var string
73
+ */
74
+ static private $_appRoot;
75
+
76
+ /**
77
+ * Application model
78
+ *
79
+ * @var Mage_Core_Model_App
80
+ */
81
+ static private $_app;
82
+
83
+ /**
84
+ * Config Model
85
+ *
86
+ * @var Mage_Core_Model_Config
87
+ */
88
+ static private $_config;
89
+
90
+ /**
91
+ * Event Collection Object
92
+ *
93
+ * @var Varien_Event_Collection
94
+ */
95
+ static private $_events;
96
+
97
+ /**
98
+ * Object cache instance
99
+ *
100
+ * @var Varien_Object_Cache
101
+ */
102
+ static private $_objects;
103
+
104
+ /**
105
+ * Is downloader flag
106
+ *
107
+ * @var bool
108
+ */
109
+ static private $_isDownloader = false;
110
+
111
+ /**
112
+ * Is developer mode flag
113
+ *
114
+ * @var bool
115
+ */
116
+ static private $_isDeveloperMode = false;
117
+
118
+ /**
119
+ * Is allow throw Exception about headers already sent
120
+ *
121
+ * @var bool
122
+ */
123
+ public static $headersSentThrowsException = true;
124
+
125
+ /**
126
+ * Is installed flag
127
+ *
128
+ * @var bool
129
+ */
130
+ static private $_isInstalled;
131
+
132
+ /**
133
+ * Magento edition constants
134
+ */
135
+ const EDITION_COMMUNITY = 'Community';
136
+ const EDITION_ENTERPRISE = 'Enterprise';
137
+ const EDITION_PROFESSIONAL = 'Professional';
138
+ const EDITION_GO = 'Go';
139
+
140
+ /**
141
+ * Current Magento edition.
142
+ *
143
+ * @var string
144
+ * @static
145
+ */
146
+ static private $_currentEdition = self::EDITION_COMMUNITY;
147
+
148
+ /**
149
+ * Gets the current Magento version string
150
+ * @link http://www.magentocommerce.com/blog/new-community-edition-release-process/
151
+ *
152
+ * @return string
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
+ * Gets the detailed Magento version information
163
+ * @link http://www.magentocommerce.com/blog/new-community-edition-release-process/
164
+ *
165
+ * @return array
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
+ * Get current Magento edition
181
+ *
182
+ * @static
183
+ * @return string
184
+ */
185
+ public static function getEdition()
186
+ {
187
+ return self::$_currentEdition;
188
+ }
189
+
190
+ /**
191
+ * Set all my static data to defaults
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
+ // do not reset $headersSentThrowsException
206
+ }
207
+
208
+ /**
209
+ * Register a new variable
210
+ *
211
+ * @param string $key
212
+ * @param mixed $value
213
+ * @param bool $graceful
214
+ * @throws Mage_Core_Exception
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
+ * Unregister a variable from register by key
229
+ *
230
+ * @param string $key
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
+ * Retrieve a value from registry by a key
244
+ *
245
+ * @param string $key
246
+ * @return mixed
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
+ * Set application root absolute path
258
+ *
259
+ * @param string $appRoot
260
+ * @throws Mage_Core_Exception
261
+ */
262
+ public static function setRoot($appRoot = '')
263
+ {
264
+ if (self::$_appRoot) {
265
+ return ;
266
+ }
267
+
268
+ if ('' === $appRoot) {
269
+ // automagically find application root by dirname of Mage.php
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
+ * Retrieve application root absolute path
284
+ *
285
+ * @return string
286
+ */
287
+ public static function getRoot()
288
+ {
289
+ return self::$_appRoot;
290
+ }
291
+
292
+ /**
293
+ * Retrieve Events Collection
294
+ *
295
+ * @return Varien_Event_Collection $collection
296
+ */
297
+ public static function getEvents()
298
+ {
299
+ return self::$_events;
300
+ }
301
+
302
+ /**
303
+ * Varien Objects Cache
304
+ *
305
+ * @param string $key optional, if specified will load this key
306
+ * @return Varien_Object_Cache
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
+ * Retrieve application root absolute path
322
+ *
323
+ * @param string $type
324
+ * @return string
325
+ */
326
+ public static function getBaseDir($type = 'base')
327
+ {
328
+ return self::getConfig()->getOptions()->getDir($type);
329
+ }
330
+
331
+ /**
332
+ * Retrieve module absolute path by directory type
333
+ *
334
+ * @param string $type
335
+ * @param string $moduleName
336
+ * @return string
337
+ */
338
+ public static function getModuleDir($type, $moduleName)
339
+ {
340
+ return self::getConfig()->getModuleDir($type, $moduleName);
341
+ }
342
+
343
+ /**
344
+ * Retrieve config value for store by path
345
+ *
346
+ * @param string $path
347
+ * @param mixed $store
348
+ * @return mixed
349
+ */
350
+ public static function getStoreConfig($path, $store = null)
351
+ {
352
+ return self::app()->getStore($store)->getConfig($path);
353
+ }
354
+
355
+ /**
356
+ * Retrieve config flag for store by path
357
+ *
358
+ * @param string $path
359
+ * @param mixed $store
360
+ * @return bool
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
+ * Get base URL path by type
374
+ *
375
+ * @param string $type
376
+ * @param null|bool $secure
377
+ * @return string
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
+ * Generate url by route and parameters
386
+ *
387
+ * @param string $route
388
+ * @param array $params
389
+ * @return string
390
+ */
391
+ public static function getUrl($route = '', $params = array())
392
+ {
393
+ return self::getModel('core/url')->getUrl($route, $params);
394
+ }
395
+
396
+ /**
397
+ * Get design package singleton
398
+ *
399
+ * @return Mage_Core_Model_Design_Package
400
+ */
401
+ public static function getDesign()
402
+ {
403
+ return self::getSingleton('core/design_package');
404
+ }
405
+
406
+ /**
407
+ * Retrieve a config instance
408
+ *
409
+ * @return Mage_Core_Model_Config
410
+ */
411
+ public static function getConfig()
412
+ {
413
+ return self::$_config;
414
+ }
415
+
416
+ /**
417
+ * Add observer to even object
418
+ *
419
+ * @param string $eventName
420
+ * @param callback $callback
421
+ * @param array $arguments
422
+ * @param string $observerName
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
+ * Dispatch event
436
+ *
437
+ * Calls all observer callbacks registered for this event
438
+ * and multiple observers matching event name pattern
439
+ *
440
+ * @param string $name
441
+ * @param array $data
442
+ * @return Mage_Core_Model_App
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
+ * Retrieve model object
454
+ *
455
+ * @link Mage_Core_Model_Config::getModelInstance
456
+ * @param string $modelClass
457
+ * @param array|object $arguments
458
+ * @return Mage_Core_Model_Abstract|false
459
+ */
460
+ public static function getModel($modelClass = '', $arguments = array())
461
+ {
462
+ return self::getConfig()->getModelInstance($modelClass, $arguments);
463
+ }
464
+
465
+ /**
466
+ * Retrieve model object singleton
467
+ *
468
+ * @param string $modelClass
469
+ * @param array $arguments
470
+ * @return Mage_Core_Model_Abstract
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
+ * Retrieve object of resource model
483
+ *
484
+ * @param string $modelClass
485
+ * @param array $arguments
486
+ * @return Object
487
+ */
488
+ public static function getResourceModel($modelClass, $arguments = array())
489
+ {
490
+ return self::getConfig()->getResourceModelInstance($modelClass, $arguments);
491
+ }
492
+
493
+ /**
494
+ * Retrieve Controller instance by ClassName
495
+ *
496
+ * @param string $class
497
+ * @param Mage_Core_Controller_Request_Http $request
498
+ * @param Mage_Core_Controller_Response_Http $response
499
+ * @param array $invokeArgs
500
+ * @return Mage_Core_Controller_Front_Action
501
+ */
502
+ public static function getControllerInstance($class, $request, $response, array $invokeArgs = array())
503
+ {
504
+ return new $class($request, $response, $invokeArgs);
505
+ }
506
+
507
+ /**
508
+ * Retrieve resource vodel object singleton
509
+ *
510
+ * @param string $modelClass
511
+ * @param array $arguments
512
+ * @return object
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
+ * Deprecated, use self::helper()
525
+ *
526
+ * @param string $type
527
+ * @return object
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
+ * Retrieve helper object
537
+ *
538
+ * @param string $name the helper name
539
+ * @return Mage_Core_Helper_Abstract
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
+ * Retrieve resource helper object
553
+ *
554
+ * @param string $moduleName
555
+ * @return Mage_Core_Model_Resource_Helper_Abstract
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
+ * Return new exception by module to be thrown
570
+ *
571
+ * @param string $module
572
+ * @param string $message
573
+ * @param integer $code
574
+ * @return Mage_Core_Exception
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
+ * Throw Exception
584
+ *
585
+ * @param string $message
586
+ * @param string $messageStorage
587
+ * @throws Mage_Core_Exception
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
+ * Get initialized application object.
599
+ *
600
+ * @param string $code
601
+ * @param string $type
602
+ * @param string|array $options
603
+ * @return Mage_Core_Model_App
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
+ * @static
624
+ * @param string $code
625
+ * @param string $type
626
+ * @param array $options
627
+ * @param string|array $modules
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
+ * Front end main entry point
656
+ *
657
+ * @param string $code
658
+ * @param string $type
659
+ * @param string|array $options
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
+ * Set application isInstalled flag based on given options
711
+ *
712
+ * @param array $options
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
+ * Set application Config model
723
+ *
724
+ * @param array $options
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
+ * Retrieve application installation flag
745
+ *
746
+ * @param string|array $options
747
+ * @return bool
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
+ * log facility (??)
778
+ *
779
+ * @param string $message
780
+ * @param integer $level
781
+ * @param string $file
782
+ * @param bool $forceLog
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
+ * Write exception to log
849
+ *
850
+ * @param Exception $e
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
+ * Set enabled developer mode
863
+ *
864
+ * @param bool $mode
865
+ * @return bool
866
+ */
867
+ public static function setIsDeveloperMode($mode)
868
+ {
869
+ self::$_isDeveloperMode = (bool)$mode;
870
+ return self::$_isDeveloperMode;
871
+ }
872
+
873
+ /**
874
+ * Retrieve enabled developer mode
875
+ *
876
+ * @return bool
877
+ */
878
+ public static function getIsDeveloperMode()
879
+ {
880
+ return self::$_isDeveloperMode;
881
+ }
882
+
883
+ /**
884
+ * Display exception
885
+ *
886
+ * @param Exception $e
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
+ // retrieve server data
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
+ // attempt to specify store as a skin
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
+ * Define system folder directory url by virtue of running script directory name
932
+ * Try to find requested folder by shifting to domain root directory
933
+ *
934
+ * @param string $folder
935
+ * @param boolean $exitIfNot
936
+ * @return string
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
+ // exit because of infinity loop
971
+ exit($errorMessage);
972
+ } else {
973
+ self::printException(new Exception(), $errorMessage);
974
+ }
975
+ }
976
+
977
+ return $baseUrl;
978
+ }
979
+
980
+ /**
981
+ * Set is downloader flag
982
+ *
983
+ * @param bool $flag
984
+ */
985
+ public static function setIsDownloader($flag = true)
986
+ {
987
+ self::$_isDownloader = $flag;
988
+ }
989
+ }