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 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 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 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 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 integer
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 integer
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 boolean $autoDetect If true, the value is tried to detected
174: * automatically. (default: false)
175: * @return integer
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 boolean $autoDetect If true, the value is tried to detected
196: * automatically. (default: false)
197: * @return integer
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 boolean $autoDetect If true, the value is tried to detected
217: * automatically. (default: false)
218: * @return integer
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 boolean $autoDetect If true, the value is tried to detected
239: * automatically. (default: false)
240: * @return integer
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 boolean $autoDetect If true, the value is tried to detected
261: * automatically. (default: false)
262: * @return integer
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 integer
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 integer
294: */
295: public static function getCurrentContainerId() {
296: return self::_fetchGlobalVariable('cCurrentContainer', 0);
297: }
298:
299: /**
300: * Return the session object stored in the global variable "sess".
301: *
302: * @return cSession
303: */
304: public static function getSession() {
305: return self::_fetchGlobalVariable('sess');
306: }
307:
308: /**
309: * Returns the auth object stored in the global variable "auth".
310: *
311: * @return cAuth
312: */
313: public static function getAuth() {
314: return self::_fetchGlobalVariable('auth');
315: }
316:
317: /**
318: * Returns the permission object stored in the global variable "perm".
319: *
320: * @return cPermission
321: */
322: public static function getPerm() {
323: return self::_fetchGlobalVariable('perm');
324: }
325:
326: /**
327: * Returns the configuration array stored in the global variable "cfg".
328: *
329: * @return array
330: */
331: public static function getConfig() {
332: return self::_fetchGlobalVariable('cfg', array());
333: }
334:
335: /**
336: * This function returns either a full configuration section or the value
337: * for a certain configuration option if a $optionName is given.
338: * In this case a $default value can be given which will be returned if this
339: * option is not defined.
340: *
341: * @param string $sectionName
342: * @param string $optionName optional
343: * @param string $defaultValue optional
344: * @return array string
345: */
346: public static function getConfigValue($sectionName = NULL, $optionName = NULL, $defaultValue = NULL) {
347: // get general configuration array
348: $cfg = self::getConfig();
349:
350: // determine configuration section
351: $section = array();
352: if (array_key_exists($sectionName, $cfg)) {
353: $section = $cfg[$sectionName];
354: }
355: if (NULL === $optionName) {
356: return $section;
357: }
358:
359: // determine configuration value for certain option name of
360: // configuration section
361: $value = $defaultValue;
362: if (is_array($cfg[$sectionName])) {
363: if (array_key_exists($optionName, $section)) {
364: $value = $section[$optionName];
365: }
366: }
367: return $value;
368: }
369:
370: /**
371: * Returns the client configuration array stored in the global variable
372: * "cfgClient".
373: * If no client ID is specified or is 0 the complete array is returned.
374: *
375: * @param integer $clientId Client ID (optional)
376: * @return array
377: */
378: public static function getClientConfig($clientId = 0) {
379: $clientConfig = self::_fetchGlobalVariable('cfgClient', array());
380:
381: if ($clientId == 0) {
382: return $clientConfig;
383: }
384:
385: return (isset($clientConfig[$clientId])? $clientConfig[$clientId] : array());
386: }
387:
388: /**
389: * Returns a new CONTENIDO database object.
390: *
391: * @todo perhaps its better to instantiate only one object and reset it on
392: * call
393: * @return cDb
394: */
395: public static function getDb() {
396: try {
397: $db = new cDb();
398: } catch (Exception $e) {
399: die($e->getMessage());
400: }
401:
402: return $db;
403: }
404:
405: /**
406: * Fetches the database table name with its prefix.
407: *
408: * @param string $index name of the index
409: * @return string
410: */
411: public static function getDbTableName($index) {
412: $cfg = self::getConfig();
413:
414: if (!is_array($cfg['tab']) || !isset($cfg['tab'][$index])) {
415: return '';
416: }
417:
418: return $cfg['tab'][$index];
419: }
420:
421: /**
422: * Return the global CONTENIDO Execution Chain Registry.
423: *
424: * @return cApiCecRegistry
425: */
426: public static function getCecRegistry() {
427: return self::_fetchGlobalVariable('_cecRegistry');
428: }
429:
430: /**
431: * Setter for an application variable.
432: *
433: * @param string $key
434: * @param mixed $value
435: */
436: public static function setAppVar($key, $value) {
437: self::$_appVars[$key] = $value;
438: }
439:
440: /**
441: * Getter for an application variable.
442: *
443: * @param string $key
444: * @param mixed $default Default value to return, if the application
445: * variable doesn't exists
446: * @return mixed
447: */
448: public static function getAppVar($key, $default = null) {
449: return (isset(self::$_appVars[$key]))? self::$_appVars[$key] : $default;
450: }
451:
452: /**
453: * Unsets an existing application variable.
454: *
455: * @param string $key
456: */
457: public static function unsetAppVar($key) {
458: if (isset(self::$_appVars[$key])) {
459: unset(self::$_appVars[$key]);
460: }
461: }
462:
463: /**
464: * Fetches the global variable requested.
465: * If variable is not set, the default value is returned.
466: *
467: * @param string $variableName name of the global variable
468: * @param mixed $defaultValue default value
469: * @return mixed
470: */
471: protected final static function _fetchGlobalVariable($variableName, $defaultValue = NULL) {
472: if (!isset($GLOBALS[$variableName])) {
473: return $defaultValue;
474: }
475:
476: return $GLOBALS[$variableName];
477: }
478:
479: /**
480: * Fetches the corresponding Item object for the specific class name and its
481: * primary key value.
482: *
483: * @param string $apiClassName name of the api class
484: * @param integer $objectId primary key value
485: * @throws cInvalidArgumentException if the given objectId is not greater
486: * than 0 or the given class does not exist
487: * @return Item
488: */
489: protected final static function _fetchItemObject($apiClassName, $objectId) {
490: if ((int) $objectId <= 0) {
491: throw new cInvalidArgumentException('Object ID must be greater than 0.');
492: }
493:
494: if (!class_exists($apiClassName)) {
495: throw new cInvalidArgumentException('Requested API object was not found: \'' . $apiClassName . '\'');
496: }
497:
498: return new $apiClassName($objectId);
499: }
500:
501: /**
502: * Bootstraps the CONTENIDO framework and initializes the global variables
503: * sess, auth and perm.
504: *
505: * @param $features array array with class name definitions
506: */
507: public final static function bootstrap($features) {
508: $cfg = self::getConfig();
509:
510: $bootstrapFeatures = array(
511: 'sess',
512: 'auth',
513: 'perm'
514: );
515: foreach ($bootstrapFeatures as $feature) {
516: $varFeatureClass = $feature . 'Class';
517: if (isset($features[$feature])) {
518: $$varFeatureClass = $features[$feature];
519: } else {
520: $$varFeatureClass = $cfg['bootstrap'][$feature];
521: }
522: }
523:
524: if (isset($sessClass) && class_exists($sessClass)) {
525: global $sess;
526: $sess = new $sessClass();
527: $sess->start();
528: if (isset($authClass) && class_exists($authClass)) {
529: global $auth;
530: if (!isset($auth)) {
531: $auth = new $authClass();
532: }
533: $auth->start();
534:
535: if (isset($permClass) && class_exists($permClass)) {
536: global $perm;
537: if (!isset($perm)) {
538: $perm = new $permClass();
539: }
540: }
541: }
542: }
543: }
544:
545: /**
546: * Shutdowns the CONTENIDO framework on page close.
547: */
548: public final static function shutdown($debugShowAll = true) {
549: if ($debugShowAll == true) {
550: cDebug::showAll();
551: }
552:
553: $sess = self::getSession();
554: if (isset($sess)) {
555: $sess->freeze();
556: }
557: }
558:
559: /**
560: * Stores an information massage in the cRegistry.
561: *
562: * @param string message
563: */
564: public static function addInfoMessage($message) {
565: array_push(self::$_infoMessages, $message);
566: }
567:
568: /**
569: * Stores an error massage in the cRegistry.
570: *
571: * @param string message
572: */
573: public static function addErrorMessage($message) {
574: array_push(self::$_errMessages, $message);
575: }
576:
577: /**
578: * Stores an warning massage in the cRegistry.
579: *
580: * @param string message
581: */
582: public static function addWarningMessage($message) {
583: array_push(self::$_warnMessages, $message);
584: }
585:
586: /**
587: * Returns an array with information messages.
588: *
589: * @return array
590: */
591: public static function getInfoMessages() {
592: return self::$_infoMessages;
593: }
594:
595: /**
596: * Returns an array with error messages.
597: *
598: * @return array
599: */
600: public static function getErrorMessages() {
601: return self::$_errMessages;
602: }
603:
604: /**
605: * Returns an array with warning messages.
606: *
607: * @return array
608: */
609: public static function getWarningMessages() {
610: return self::$_warnMessages;
611: }
612:
613: /**
614: * Returns true if the DNT header is set and equal to 1.
615: * Returns false if the DNT header is unset or not equal to 1.
616: *
617: * @return boolean whether tracking is allowed by the DNT header
618: */
619: public static function isTrackingAllowed() {
620: return (isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT'] != 1) || !isset($_SERVER['HTTP_DNT']);
621: }
622:
623: }
624: