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