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