1: <?php
2: /**
3: * This file contains the the registry class.
4: *
5: * @package Core
6: * @subpackage Backend
7: * @version SVN Revision $Rev:$
8: *
9: * @author Dominik Ziegler
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * This class contains functions for global interaction in CONTENIDO.
20: *
21: * @package Core
22: * @subpackage Backend
23: */
24: class cRegistry {
25:
26: /**
27: * Container for application variables.
28: * Meant to set and get application wide variables as an alternative to
29: * store them in global scope.
30: *
31: * @var array
32: */
33: protected static $_appVars = array();
34:
35: /**
36: * Container for information messages.
37: *
38: * @var array
39: */
40: protected static $_infoMessages = array();
41:
42: /**
43: * Container for error messages.
44: *
45: * @var array
46: */
47: protected static $_errMessages = array();
48:
49: /**
50: * Container for warning messages.
51: *
52: * @var array
53: */
54: protected static $_warnMessages = array();
55:
56: /**
57: * Function wich returns path after the last possible place changing via
58: * configuration file.
59: *
60: * @return string path
61: */
62: public static function getBackendPath() {
63: $cfg = self::getConfig();
64: return $cfg['path']['contenido'];
65: }
66:
67: /**
68: * Function wich returns the backend URL after the last possible place
69: * changing via configuration file.
70: *
71: * @return string URL
72: */
73: public static function getBackendUrl() {
74: $cfg = self::getConfig();
75: return $cfg['path']['contenido_fullhtml'];
76: }
77:
78: /**
79: * Function wich returns path after the last possible place changing via
80: * configuration file.
81: * The path point to the current client
82: *
83: * @return string path
84: */
85: public static function getFrontendPath() {
86: $cfgClient = self::getClientConfig();
87: $client = self::getClientId();
88: return $cfgClient[$client]['path']['frontend'];
89: }
90:
91: /**
92: * Function wich returns URL after the last possible place changing via
93: * configuration file.
94: * The path point to the current client
95: *
96: * @return string URL
97: */
98: public static function getFrontendUrl() {
99: $cfgClient = self::getClientConfig();
100: $client = self::getClientId();
101: return $cfgClient[$client]['path']['htmlpath'];
102: }
103:
104: /**
105: * Returns the CONTENIDO Session ID stored in the global variable
106: * "contenido".
107: *
108: * @return string
109: */
110: public static function getBackendSessionId() {
111: return self::_fetchGlobalVariable('contenido');
112: }
113:
114: /**
115: * Returns the CONTENIDO backend language stored in the global variable
116: * "belang"
117: *
118: * @return string
119: */
120: public static function getBackendLanguage() {
121: return self::_fetchGlobalVariable('belang');
122: }
123:
124: /**
125: * Checks if the edit mode in backend is active or not stored in the global
126: * variable "edit"
127: *
128: * @return boolean
129: */
130: public static function isBackendEditMode() {
131: return self::_fetchGlobalVariable('edit', false);
132: }
133:
134: /**
135: * Returns the current language ID stored in the global variable "lang".
136: *
137: * @return int
138: */
139: public static function getLanguageId() {
140: return self::_fetchGlobalVariable('lang', self::_fetchGlobalVariable('load_lang', 0));
141: }
142:
143: /**
144: * Returns the loaded cApiLanguage object for the current language.
145: *
146: * @return cApiLanguage
147: */
148: public static function getLanguage() {
149: return self::_fetchItemObject('cApiLanguage', self::getLanguageId());
150: }
151:
152: /**
153: * Returns the current client ID stored in the global variable "client".
154: *
155: * @return int
156: */
157: public static function getClientId() {
158: return self::_fetchGlobalVariable('client', self::_fetchGlobalVariable('load_client', 0));
159: }
160:
161: /**
162: * Returns the loaded cApiClient object for the current client.
163: *
164: * @return cApiClient
165: */
166: public static function getClient() {
167: return self::_fetchItemObject('cApiClient', self::getClientId());
168: }
169:
170: /**
171: * Returns the article id stored in the global variable "idart".
172: *
173: * @param bool $autoDetect If true, the value is tried to detected
174: * automatically. (default: false)
175: * @return int
176: */
177: public static function getArticleId($autoDetect = false) {
178: // TODO: autoDetect from front_content.php
179: return self::_fetchGlobalVariable('idart', 0);
180: }
181:
182: /**
183: * Returns the loaded cApiArticle object for the current article.
184: *
185: * @return cApiArticle
186: */
187: public static function getArticle() {
188: return self::_fetchItemObject('cApiArticle', self::getArticleId());
189: }
190:
191: /**
192: * Returns the article language id stored in the global variable
193: * "idartlang".
194: *
195: * @param bool $autoDetect If true, the value is tried to detected
196: * automatically. (default: false)
197: * @return int
198: */
199: public static function getArticleLanguageId($autoDetect = false) {
200: // TODO: autoDetect from front_content.php
201: return self::_fetchGlobalVariable('idartlang', 0);
202: }
203:
204: /**
205: * Returns the loaded cApiArticleLanguage object for the current article.
206: *
207: * @return cApiArticleLanguage
208: */
209: public static function getArticleLanguage() {
210: return self::_fetchItemObject('cApiArticleLanguage', self::getArticleLanguageId());
211: }
212:
213: /**
214: * Returns the category id stored in the global variable "idcat".
215: *
216: * @param bool $autoDetect If true, the value is tried to detected
217: * automatically. (default: false)
218: * @return int
219: */
220: public static function getCategoryId($autoDetect = false) {
221: // TODO: autoDetect from front_content.php
222: return self::_fetchGlobalVariable('idcat', 0);
223: }
224:
225: /**
226: * Returns the loaded cApiCategory object for the current category.
227: *
228: * @return cApiCategory
229: */
230: public static function getCategory() {
231: return self::_fetchItemObject('cApiCategory', self::getCategoryId());
232: }
233:
234: /**
235: * Returns the category language id stored in the global variable
236: * "idcatlang".
237: *
238: * @param bool $autoDetect If true, the value is tried to detected
239: * automatically. (default: false)
240: * @return int
241: */
242: public static function getCategoryLanguageId($autoDetect = false) {
243: // TODO: autoDetect from front_content.php
244: return self::_fetchGlobalVariable('idcatlang', 0);
245: }
246:
247: /**
248: * Returns the loaded cApiCategoryLanguage object for the current category.
249: *
250: * @return cApiCategoryLanguage
251: */
252: public static function getCategoryLanguage() {
253: return self::_fetchItemObject('cApiCategoryLanguage', self::getCategoryLanguageId());
254: }
255:
256: /**
257: * Returns the category/article relation id stored in the global variable
258: * "idcatart".
259: *
260: * @param bool $autoDetect If true, the value is tried to detected
261: * automatically. (default: false)
262: * @return int
263: */
264: public static function getCategoryArticleId($autoDetect = false) {
265: // TODO: autoDetect from front_content.php
266: return self::_fetchGlobalVariable('idcatart', 0);
267: }
268:
269: /**
270: * Returns the loaded cApiCategoryArticle object for the current
271: * category/article relation.
272: *
273: * @return cApiCategoryArticle
274: */
275: public static function getCategoryArticle() {
276: return self::_fetchItemObject('cApiCategoryArticle', self::getCategoryArticleId());
277: }
278:
279: /**
280: * Returns the current module ID.
281: * Note: This function will work only within module code.
282: *
283: * @return int
284: */
285: public static function getCurrentModuleId() {
286: return self::_fetchGlobalVariable('cCurrentModule', 0);
287: }
288:
289: /**
290: * Returns the current container ID.
291: * Note: This function will work only within module code.
292: *
293: * @return int
294: */
295: public static function getCurrentContainerId() {
296: return self::_fetchGlobalVariable('cCurrentContainer', 0);
297: }
298:
299: /**
300: * Returns the current frame id stored in the global variable "frame".
301: *
302: * @return string
303: */
304: public static function getFrame() {
305: return self::_fetchGlobalVariable('frame', '');
306: }
307:
308: /**
309: * Return the session object stored in the global variable "sess".
310: *
311: * @return cSession
312: */
313: public static function getSession() {
314: return self::_fetchGlobalVariable('sess');
315: }
316:
317: /**
318: * Returns the auth object stored in the global variable "auth".
319: *
320: * @return cAuth
321: */
322: public static function getAuth() {
323: return self::_fetchGlobalVariable('auth');
324: }
325:
326: /**
327: * Returns the area stored in the global variable "area".
328: *
329: * @return string
330: */
331: public static function getArea() {
332: return self::_fetchGlobalVariable('area');
333: }
334:
335: /**
336: * Returns the permission object stored in the global variable "perm".
337: *
338: * @return cPermission
339: */
340: public static function getPerm() {
341: return self::_fetchGlobalVariable('perm');
342: }
343:
344: /**
345: * Returns the configuration array stored in the global variable "cfg".
346: *
347: * @return array
348: */
349: public static function getConfig() {
350: return self::_fetchGlobalVariable('cfg', array());
351: }
352:
353: /**
354: * This function returns either a full configuration section or the value
355: * for a certain configuration option if a $optionName is given.
356: * In this case a $default value can be given which will be returned if this
357: * option is not defined.
358: *
359: * @param string $sectionName
360: * @param string $optionName optional
361: * @param string $defaultValue optional
362: * @return array string
363: */
364: public static function getConfigValue($sectionName = NULL, $optionName = NULL, $defaultValue = NULL) {
365: // get general configuration array
366: $cfg = self::getConfig();
367:
368: // determine configuration section
369: $section = array();
370: if (array_key_exists($sectionName, $cfg)) {
371: $section = $cfg[$sectionName];
372: }
373: if (NULL === $optionName) {
374: return $section;
375: }
376:
377: // determine configuration value for certain option name of
378: // configuration section
379: $value = $defaultValue;
380: if (is_array($cfg[$sectionName])) {
381: if (array_key_exists($optionName, $section)) {
382: $value = $section[$optionName];
383: }
384: }
385: return $value;
386: }
387:
388: /**
389: * Returns the client configuration array stored in the global variable
390: * "cfgClient".
391: * If no client ID is specified or is 0 the complete array is returned.
392: *
393: * @param int $clientId Client ID (optional)
394: * @return array
395: */
396: public static function getClientConfig($clientId = 0) {
397: $clientConfig = self::_fetchGlobalVariable('cfgClient', array());
398:
399: if ($clientId == 0) {
400: return $clientConfig;
401: }
402:
403: return (isset($clientConfig[$clientId]) ? $clientConfig[$clientId] : array());
404: }
405:
406: /**
407: * Returns a new CONTENIDO database object.
408: *
409: * @todo perhaps its better to instantiate only one object and reset it on
410: * call
411: * @return cDb
412: */
413: public static function getDb() {
414: try {
415: $db = new cDb();
416: } catch (Exception $e) {
417: die($e->getMessage());
418: }
419:
420: return $db;
421: }
422:
423: /**
424: * Fetches the database table name with its prefix.
425: *
426: * @param string $index name of the index
427: * @return string
428: */
429: public static function getDbTableName($index) {
430: $cfg = self::getConfig();
431:
432: if (!is_array($cfg['tab']) || !isset($cfg['tab'][$index])) {
433: return '';
434: }
435:
436: return $cfg['tab'][$index];
437: }
438:
439: /**
440: * Return the global CONTENIDO Execution Chain Registry.
441: *
442: * @return cApiCecRegistry
443: */
444: public static function getCecRegistry() {
445: return self::_fetchGlobalVariable('_cecRegistry');
446: }
447:
448: /**
449: * Setter for an application variable.
450: *
451: * @param string $key
452: * @param mixed $value
453: */
454: public static function setAppVar($key, $value) {
455: self::$_appVars[$key] = $value;
456: }
457:
458: /**
459: * Getter for an application variable.
460: *
461: * @param string $key
462: * @param mixed $default Default value to return, if the application
463: * variable doesn't exists
464: * @return mixed
465: */
466: public static function getAppVar($key, $default = NULL) {
467: return (isset(self::$_appVars[$key])) ? self::$_appVars[$key] : $default;
468: }
469:
470: /**
471: * Unsets an existing application variable.
472: *
473: * @param string $key
474: */
475: public static function unsetAppVar($key) {
476: if (isset(self::$_appVars[$key])) {
477: unset(self::$_appVars[$key]);
478: }
479: }
480:
481: /**
482: * Fetches the global variable requested.
483: * If variable is not set, the default value is returned.
484: *
485: * @param string $variableName name of the global variable
486: * @param mixed $defaultValue default value
487: * @return mixed
488: */
489: protected final static function _fetchGlobalVariable($variableName, $defaultValue = NULL) {
490: if (!isset($GLOBALS[$variableName])) {
491: return $defaultValue;
492: }
493:
494: return $GLOBALS[$variableName];
495: }
496:
497: /**
498: * Fetches the corresponding Item object for the specific class name and its
499: * primary key value.
500: *
501: * @param string $apiClassName name of the api class
502: * @param int $objectId primary key value
503: * @throws cInvalidArgumentException if the given objectId is not greater
504: * than 0 or the given class does not exist
505: * @return Item
506: */
507: protected final static function _fetchItemObject($apiClassName, $objectId) {
508: if ((int) $objectId <= 0) {
509: throw new cInvalidArgumentException('Object ID must be greater than 0.');
510: }
511:
512: if (!class_exists($apiClassName)) {
513: throw new cInvalidArgumentException('Requested API object was not found: \'' . $apiClassName . '\'');
514: }
515:
516: return new $apiClassName($objectId);
517: }
518:
519: /**
520: * Bootstraps the CONTENIDO framework and initializes the global variables
521: * sess, auth and perm.
522: *
523: * @param array $features array with class name definitions
524: */
525: public final static function bootstrap($features) {
526: $cfg = self::getConfig();
527:
528: $sessClass = $authClass = $permClass = NULL;
529:
530: $bootstrapFeatures = array(
531: 'sess',
532: 'auth',
533: 'perm'
534: );
535:
536: foreach ($bootstrapFeatures as $feature) {
537: $varFeatureClass = $feature . 'Class';
538: if (isset($cfg['bootstrap'][$feature]) && class_exists($cfg['bootstrap'][$feature])) {
539: $$varFeatureClass = $cfg['bootstrap'][$feature];
540: } elseif (isset($features[$feature]) && class_exists($features[$feature])) {
541: $$varFeatureClass = $features[$feature];
542: }
543: }
544:
545: if (isset($sessClass)) {
546: global $sess;
547:
548: $sess = new $sessClass();
549: $sess->start();
550: if (isset($authClass)) {
551: global $auth;
552: if (!isset($auth)) {
553: $auth = new $authClass();
554: }
555: $auth->start();
556:
557: if (isset($permClass)) {
558: global $perm;
559: if (!isset($perm)) {
560: $perm = new $permClass();
561: }
562: }
563: }
564: }
565: }
566:
567: /**
568: * Shutdowns the CONTENIDO framework on page close.
569: *
570: * @param bool $debugShowAll
571: */
572: public final static function shutdown($debugShowAll = true) {
573: if ($debugShowAll == true) {
574: cDebug::showAll();
575: }
576:
577: $sess = self::getSession();
578: if (isset($sess)) {
579: $sess->freeze();
580: }
581: }
582:
583: /**
584: * Stores an information massage in the cRegistry.
585: *
586: * @param string $message
587: */
588: public static function addInfoMessage($message) {
589: array_push(self::$_infoMessages, $message);
590: }
591:
592: /**
593: * Stores an error massage in the cRegistry.
594: *
595: * @param string $message
596: */
597: public static function addErrorMessage($message) {
598: array_push(self::$_errMessages, $message);
599: }
600:
601: /**
602: * Stores an warning massage in the cRegistry.
603: *
604: * @param string $message
605: */
606: public static function addWarningMessage($message) {
607: array_push(self::$_warnMessages, $message);
608: }
609:
610: /**
611: * Appends the last info message that will be outputted
612: *
613: * @param string $message
614: */
615: public static function appendLastInfoMessage($message) {
616: if(count(self::$_infoMessages) == 0) {
617: self::$_infoMessages[] = $message;
618: return;
619: }
620: end(self::$_infoMessages);
621: $lastKey = key(self::$_infoMessages);
622: self::$_infoMessages[$lastKey] .= "<br>" . $message;
623: reset(self::$_infoMessages);
624: }
625:
626: /**
627: * Appends the last error message that will be outputted
628: *
629: * @param string $message
630: */
631: public static function appendLastErrorMessage($message) {
632: if(count(self::$_errMessages) == 0) {
633: self::$_errMessages[] = $message;
634: return;
635: }
636: end(self::$_errMessages);
637: $lastKey = key(self::$_errMessages);
638: self::$_errMessages[$lastKey] .= "<br>" . $message;
639: reset(self::$_errMessages);
640: }
641:
642: /**
643: * Appends the last warning that will be outputted
644: *
645: * @param string $message
646: */
647: public static function appendLastWarningMessage($message) {
648: if(count(self::$_warnMessages) == 0) {
649: self::$_warnMessages[] = $message;
650: return;
651: }
652: end(self::$_warnMessages);
653: $lastKey = key(self::$_warnMessages);
654: self::$_warnMessages[$lastKey] .= "<br>" . $message;
655: reset(self::$_warnMessages);
656: }
657:
658: /**
659: * Returns an array with information messages.
660: *
661: * @return array
662: */
663: public static function getInfoMessages() {
664: return self::$_infoMessages;
665: }
666:
667: /**
668: * Returns an array with error messages.
669: *
670: * @return array
671: */
672: public static function getErrorMessages() {
673: return self::$_errMessages;
674: }
675:
676: /**
677: * Returns an array with warning messages.
678: *
679: * @return array
680: */
681: public static function getWarningMessages() {
682: return self::$_warnMessages;
683: }
684:
685: /**
686: * Returns true if the DNT header is set and equal to 1.
687: * Returns false if the DNT header is unset or not equal to 1.
688: *
689: * @return boolean whether tracking is allowed by the DNT header
690: */
691: public static function isTrackingAllowed() {
692: return (isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT'] != 1) || !isset($_SERVER['HTTP_DNT']);
693: }
694:
695: /**
696: * Returns the actual encoding (standard: utf-8)
697: *
698: * @return string name of encoding
699: * @return boolean false if no language founded
700: */
701: public static function getEncoding() {
702:
703: $apiLanguage = new cApiLanguage(self::getLanguageId());
704: if ($apiLanguage->isLoaded()) {
705: return trim($apiLanguage->get('encoding'));
706: }
707:
708: return false;
709: }
710: }
711: