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