1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23:
24: class cGuiPage {
25:
26: 27: 28: 29: 30: 31:
32: protected $_pageName;
33:
34: 35: 36: 37: 38:
39: protected $_pluginName;
40:
41: 42: 43: 44: 45:
46: protected $_pageTemplate;
47:
48: 49: 50: 51:
52: protected $_pageBase;
53: 54: 55: 56: 57: 58:
59: protected $_contentTemplate;
60:
61: 62: 63: 64: 65: 66:
67: protected $_scripts;
68:
69: 70: 71: 72: 73: 74:
75: protected $_styles;
76:
77: 78: 79: 80: 81: 82:
83: protected $_subnav;
84:
85: 86: 87: 88: 89: 90:
91: protected $_markScript;
92:
93: 94: 95: 96: 97: 98:
99: protected $_error;
100:
101: 102: 103: 104: 105: 106:
107: protected $_warning;
108:
109: 110: 111: 112: 113: 114:
115: protected $_info;
116:
117: 118: 119: 120: 121:
122: protected $_abort;
123:
124: 125: 126: 127: 128: 129:
130: protected $_objects;
131:
132: 133: 134: 135: 136:
137: protected $_metaTags;
138:
139: 140: 141: 142: 143:
144: protected $_bodyClassNames;
145:
146:
147: 148: 149: 150: 151:
152: protected $_filesDirectory;
153:
154: 155: 156: 157: 158:
159: protected $_skipTemplateCheck = false;
160:
161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174:
175: public function __construct($pageName, $pluginName = '', $subMenu = '') {
176: global $lang, $cfg, $sess;
177:
178: $this->_pageName = $pageName;
179: $this->_pluginName = $pluginName;
180: $this->_pageTemplate = new cTemplate();
181: $this->_contentTemplate = new cTemplate();
182: $this->_scripts = array();
183: $this->_styles = array();
184: $this->_subnav = '';
185: $this->_markScript = '';
186: $this->_error = '';
187: $this->_warning = '';
188: $this->_info = '';
189: $this->_abort = false;
190: $this->_objects = array();
191: $this->_metaTags = array();
192: $this->_bodyClassNames = array();
193:
194:
195: $clang = new cApiLanguage($lang);
196:
197: if ($clang->isLoaded()) {
198: $this->setEncoding($clang->get('encoding'));
199: }
200:
201:
202: $this->setPageBase();
203:
204: $this->_pageTemplate->set('s', 'SUBMENU', $subMenu);
205: $this->_pageTemplate->set('s', 'PAGENAME', $pageName);
206: $pageid = str_replace('.', '_', $pageName);
207: $this->_pageTemplate->set('s', 'PAGENAME', $pageName);
208: $this->_pageTemplate->set('s', 'PAGEID', $pageid);
209:
210: $this->addBodyClassName('page_generic');
211: $this->addBodyClassName('page_' . $pageid);
212:
213: $scriptDir = '';
214: $styleDir = '';
215: if($pluginName != '') {
216: $this->_filesDirectory = '';
217: $scriptDir = cRegistry::getBackendPath() . $cfg['path']['plugins'] . $pluginName . '/' . $cfg['path']['scripts'];
218: $styleDir = cRegistry::getBackendPath() . $cfg['path']['plugins'] . $pluginName . '/' . $cfg['path']['styles'];
219: } else {
220: $this->_filesDirectory = 'includes/';
221: $scriptDir = $cfg['path']['scripts_includes'];
222: $styleDir = $cfg['path']['styles_includes'];
223: }
224:
225: if (cFileHandler::exists($styleDir . $pageName . '.css')) {
226: $this->addStyle($this->_filesDirectory . $pageName . '.css');
227: }
228:
229: 230: 231: 232:
233: if(cFileHandler::exists($styleDir)) {
234: foreach (new DirectoryIterator($styleDir) as $stylefile) {
235: if (cString::endsWith($stylefile->getFilename(), '.' . $pageName . '.css')) {
236: $this->addStyle($this->_filesDirectory . $stylefile->getFilename());
237: }
238: }
239: }
240:
241: if (cFileHandler::exists($scriptDir . $pageName . '.js')) {
242: $this->addScript($this->_filesDirectory . $pageName . '.js');
243: }
244:
245: 246: 247: 248:
249: if(cFileHandler::exists($scriptDir)) {
250: foreach (new DirectoryIterator($scriptDir) as $scriptfile) {
251: if (cString::endsWith($scriptfile->getFilename(), '.' . $pageName . '.js')) {
252: $this->addScript($this->_filesDirectory . $scriptfile->getFilename());
253: }
254: }
255: }
256: }
257:
258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271:
272: public function addScript($script) {
273: global $perm, $currentuser;
274:
275: $script = trim($script);
276: if (empty($script)) {
277: return;
278: }
279:
280: $cfg = cRegistry::getConfig();
281: $backendUrl = cRegistry::getBackendUrl();
282: $backendPath = cRegistry::getBackendPath();
283: $filePathName = $this->_getRealFilePathName($script);
284:
285:
286: if($perm->isSysadmin($currentuser) && strpos(trim($script), '<script') === false &&
287: ((!empty($this->_pluginName) && !cFileHandler::exists($backendPath . $cfg['path']['plugins'] . $this->_pluginName . '/' . $cfg['path']['scripts'] . $script)) &&
288: (!cFileHandler::exists($backendPath . $cfg['path']['scripts'] . $filePathName)))) {
289: $this->displayWarning(i18n("The requested resource") . " <strong>" . $filePathName . "</strong> " . i18n("was not found"));
290: }
291:
292: if (strpos(trim($script), 'http') === 0 || strpos(trim($script), '<script') === 0 || strpos(trim($script), '//') === 0) {
293:
294:
295:
296:
297: if(!in_array($script, $this->_scripts)) {
298: $this->_scripts[] = $script;
299: }
300: } else if (!empty($this->_pluginName) && cFileHandler::exists($backendPath . $cfg['path']['plugins'] . $this->_pluginName . '/' . $cfg['path']['scripts'] . $filePathName)) {
301:
302: $fullpath = $backendUrl . $cfg['path']['plugins'] . $this->_pluginName . '/' . $cfg['path']['scripts'] . $script;
303: if(!in_array($fullpath, $this->_scripts)) {
304: $this->_scripts[] = $fullpath;
305: }
306: } else if (cFileHandler::exists($backendPath . $cfg['path']['scripts'] . $filePathName)) {
307:
308: $fullpath = $backendUrl . $cfg['path']['scripts'] . $script;
309:
310: if(!in_array($fullpath, $this->_scripts)) {
311: $this->_scripts[] = $fullpath;
312: }
313: }
314: }
315:
316: 317: 318: 319: 320: 321: 322: 323:
324: public function addStyle($stylesheet) {
325: global $perm, $currentuser;
326:
327: $stylesheet = trim($stylesheet);
328: if (empty($stylesheet)) {
329: return;
330: }
331:
332: $cfg = cRegistry::getConfig();
333: $backendUrl = cRegistry::getBackendUrl();
334: $backendPath = cRegistry::getBackendPath();
335: $filePathName = $this->_getRealFilePathName($stylesheet);
336:
337:
338: if($perm->isSysadmin($currentuser) && ((!empty($this->_pluginName) && !cFileHandler::exists($backendPath . $cfg['path']['plugins'] . $this->_pluginName . '/' . $cfg['path']['styles'] . $stylesheet))) ||
339: (empty($this->_pluginName) && !cFileHandler::exists($backendPath . $cfg['path']['styles'] . $filePathName))) {
340: $this->displayWarning(i18n("The requested resource") . " <strong>" . $filePathName . "</strong> " . i18n("was not found"));
341: }
342:
343: if (strpos($stylesheet, 'http') === 0 || strpos($stylesheet, '//') === 0) {
344:
345: if(!in_array($stylesheet, $this->_styles)) {
346: $this->_styles[] = $stylesheet;
347: }
348: } else if (!empty($this->_pluginName) && cFileHandler::exists($backendPath . $cfg['path']['plugins'] . $this->_pluginName . '/' . $cfg['path']['styles'] . $filePathName)) {
349:
350:
351: $fullpath = $backendUrl . $cfg['path']['plugins'] . $this->_pluginName . '/' . $cfg['path']['styles'] . $stylesheet;
352: if(!in_array($fullpath, $this->_styles)) {
353: $this->_styles[] = $fullpath;
354: }
355: } else if (cFileHandler::exists($backendPath . $cfg['path']['styles'] . $filePathName)) {
356:
357:
358: $fullpath = $backendUrl . $cfg['path']['styles'] . $stylesheet;
359: if(!in_array($fullpath, $this->_styles)) {
360: $this->_styles[] = $fullpath;
361: }
362: }
363: }
364:
365: 366: 367: 368: 369: 370: 371:
372: public function addMeta(array $meta) {
373: $allowedAttributes = array(
374: 'charset',
375: 'content',
376: 'http-equiv',
377: 'name',
378: 'itemprop'
379: );
380: foreach ($meta as $key => $value) {
381: if (!in_array($key, $allowedAttributes)) {
382: throw new cInvalidArgumentException('Unallowed attribute for meta tag given - meta tag will be ignored!');
383: }
384: }
385: $this->_metaTags[] = $meta;
386: }
387:
388: 389: 390: 391: 392:
393: public function addBodyClassName($className) {
394: if (!in_array($className, $this->_bodyClassNames)) {
395: $this->_bodyClassNames[] = $className;
396: }
397: }
398:
399: 400: 401: 402: 403: 404: 405: 406:
407: public function setSubnav($additional = '', $aarea = '') {
408: global $area, $sess;
409:
410: if ($aarea == '') {
411: $aarea = $area;
412: }
413:
414: $this->_subnav = '
415: <script type="text/javascript">
416: Con.getFrame("right_top").location.href = "' . $sess->url("main.php?area={$aarea}&frame=3&{$additional}") . '";
417: </script>
418: ';
419: }
420:
421: 422: 423:
424: public function setReload() {
425: $this->_scripts[] = 'reload.js';
426: }
427:
428: 429: 430: 431: 432: 433:
434: public function reloadFrame($frameName, $updatedParameters = null) {
435: if(is_array($updatedParameters)) {
436: $updateString = '';
437: foreach($updatedParameters as $key => $value) {
438: $updateString .= $key . ': "' . $value . '"';
439: }
440: $this->_scripts[] = '<script type="text/javascript">
441: (function(Con, $) {
442: var frame = Con.getFrame(\'' . $frameName . '\');
443: if (frame) {
444: frame.location.href = Con.UtilUrl.replaceParams(frame.location.href, {' . $updateString . '});
445: }
446: })(Con, Con.$);
447: </script>';
448: } else {
449: $this->_scripts[] = '<script type="text/javascript">
450: (function(Con, $) {
451: var frame = Con.getFrame("' . $frameName . '");
452: if (frame) {
453: frame.location.href = "' . $updatedParameters .'";
454: }
455: })(Con, Con.$);
456: </script>';
457: }
458: }
459:
460: 461: 462: 463: 464:
465: public function setMarkScript($item) {
466: $this->_markScript = markSubMenuItem($item, true);
467: }
468:
469: 470: 471: 472: 473: 474:
475: public function setEncoding($encoding) {
476: if (empty($encoding)) {
477: return;
478: }
479: $this->_metaTags[] = array(
480: 'http-equiv' => 'Content-type',
481: 'content' => 'text/html;charset=' . $encoding
482: );
483: }
484:
485: 486: 487: 488: 489: 490: 491: 492:
493: public function set($type, $key, $value) {
494: $this->_contentTemplate->set($type, $key, $value);
495: }
496:
497: 498: 499: 500:
501: public function setPageBase($filename = '') {
502: if ('' === $filename) {
503: $cfg = cRegistry::getConfig();
504: $this->_pageBase = $cfg['path']['templates'] . $cfg['templates']['generic_page'];
505: } else {
506: $this->_pageBase = $filename;
507: }
508: }
509:
510: 511: 512: 513: 514:
515: public function next() {
516: $this->_contentTemplate->next();
517: }
518:
519: 520: 521: 522: 523:
524: public function abortRendering() {
525: $this->_abort = true;
526: }
527:
528: 529: 530: 531: 532: 533:
534: public function displayCriticalError($msg) {
535: $this->_error = $msg;
536: $this->_abort = true;
537: }
538:
539: 540: 541: 542: 543: 544:
545: public function displayError($msg) {
546: $this->_error .= $msg . '<br>';
547: }
548:
549: 550: 551: 552: 553:
554: public function displayWarning($msg) {
555: $this->_warning .= $msg . '<br>';
556: }
557:
558: 559: 560: 561: 562:
563: public function displayInfo($msg) {
564: $this->_info .= $msg . '<br>';
565: }
566:
567: 568: 569: 570: 571: 572: 573: 574:
575: public function setContent($objects) {
576: if (!is_array($objects)) {
577: $objects = array(
578: $objects
579: );
580: }
581: $this->_objects = $objects;
582: }
583:
584: 585: 586: 587: 588: 589: 590: 591: 592:
593: public function appendContent($objects) {
594: if (!is_array($objects)) {
595: $this->_objects[] = $objects;
596: } else {
597: $this->_objects = array_merge($this->_objects, $objects);
598: }
599: }
600:
601: 602: 603: 604: 605: 606: 607: 608:
609: public function render($template = NULL, $return = false) {
610: global $cfg;
611:
612: if ($template == NULL) {
613: $template = $this->_contentTemplate;
614: }
615:
616:
617: $this->_renderMetaTags();
618: $this->_renderScripts();
619: $this->_renderStyles();
620:
621:
622: $this->_pageTemplate->set('s', 'PAGECLASS', implode(' ', $this->_bodyClassNames));
623:
624:
625: $text = $this->_renderContentMessages();
626: if (strlen(trim($text)) > 0) {
627: $this->_skipTemplateCheck = true;
628: }
629:
630: if (!$this->_abort) {
631: if (count($this->_objects) == 0) {
632: $output = $this->_renderTemplate($template);
633: } else {
634: $output = $this->_renderObjects();
635: }
636: $this->_pageTemplate->set('s', 'CONTENT', $text . $output);
637: } else {
638: $this->_pageTemplate->set('s', 'CONTENT', $text);
639: }
640:
641: return $this->_pageTemplate->generate($this->_pageBase, $return);
642: }
643:
644: 645: 646:
647: protected function _renderMetaTags() {
648:
649:
650:
651: $produceXhtml = false;
652: $meta = '';
653: foreach ($this->_metaTags as $metaTag) {
654: $tag = '<meta';
655: foreach ($metaTag as $key => $value) {
656: $tag .= ' ' . $key . '="' . $value . '"';
657: }
658: if ($produceXhtml) {
659: $tag .= ' /';
660: }
661: $tag .= ">\n";
662: $meta .= $tag;
663: }
664: if (!empty($meta)) {
665: $this->_pageTemplate->set('s', 'META', $meta);
666: } else {
667: $this->_pageTemplate->set('s', 'META', '');
668: }
669: }
670:
671: 672: 673:
674: protected function _renderScripts() {
675: $strscript = $this->_subnav . "\n" . $this->_markScript . "\n";
676: foreach ($this->_scripts as $script) {
677: if (strpos($script, 'http') === 0 || strpos($script, '//') === 0) {
678: $strscript .= '<script type="text/javascript" src="' . $script . '"></script>' . "\n";
679: } else if (strpos($script, '<script') === false) {
680: $strscript .= '<script type="text/javascript" src="scripts/' . $script . '"></script>' . "\n";
681: } else {
682: $strscript .= $script;
683: }
684: }
685: $this->_pageTemplate->set('s', 'SCRIPTS', $strscript);
686: }
687:
688: 689: 690:
691: protected function _renderStyles() {
692: $strstyle = '';
693: foreach ($this->_styles as $style) {
694: if (strpos($style, 'http') === 0 || strpos($style, '//') === 0) {
695: $strstyle .= '<link href="' . $style . '" type="text/css" rel="stylesheet">' . "\n";
696: } else {
697: $strstyle .= '<link href="styles/' . $style . '" type="text/css" rel="stylesheet">' . "\n";
698: }
699: }
700: $this->_pageTemplate->set('s', 'STYLES', $strstyle);
701: }
702:
703: 704: 705: 706: 707: 708: 709:
710: protected function _renderContentMessages() {
711: global $notification;
712:
713:
714: $infoMessages = cRegistry::getInfoMessages();
715: foreach ($infoMessages as $message) {
716: $this->displayInfo($message);
717: }
718:
719: $errorMessages = cRegistry::getErrorMessages();
720: foreach ($errorMessages as $message) {
721: $this->displayError($message);
722: }
723:
724: $warningMessages = cRegistry::getWarningMessages();
725: foreach ($warningMessages as $message) {
726: $this->displayWarning($message);
727: }
728:
729: $text = '';
730: if ($this->_info != '') {
731: $text .= $notification->returnNotification('info', $this->_info) . '<br>';
732: }
733: if ($this->_warning != '') {
734: $text .= $notification->returnNotification('warning', $this->_warning) . '<br>';
735: }
736: if ($this->_error != '') {
737: $text .= $notification->returnNotification('error', $this->_error) . '<br>';
738: }
739:
740: return $text;
741: }
742:
743: 744: 745: 746: 747: 748:
749: protected function _renderObjects() {
750: $output = '';
751:
752: foreach ($this->_objects as $obj) {
753: if (!method_exists($obj, 'render')) {
754: continue;
755: }
756:
757:
758:
759:
760: $oldOutput = $output;
761: ob_start();
762:
763:
764: $output .= $obj->render(false);
765:
766: $output .= ob_get_contents();
767: if ($oldOutput == $output) {
768: cWarning(__FILE__, __LINE__, "Rendering this object (" . print_r($obj, true) . ") doesn't seem to have any effect.");
769: }
770: ob_end_clean();
771: }
772:
773: return $output;
774: }
775:
776: 777: 778: 779: 780: 781:
782: protected function _renderTemplate($template) {
783: global $perm, $currentuser, $notification;
784:
785: $cfg = cRegistry::getConfig();
786:
787: $file = '';
788: if ($this->_pluginName == '') {
789: $file = $cfg['path']['templates'] . 'template.' . $this->_pageName . '.html';
790: } else {
791: $file = $cfg['path']['plugins'] . $this->_pluginName . '/templates/template.' . $this->_pageName . '.html';
792: }
793:
794: $output = '';
795:
796: if (!$this->_skipTemplateCheck && $perm->isSysadmin($currentuser) && !cFileHandler::exists($file)) {
797: $output .= $notification->returnNotification('warning', i18n("The requested resource") . " <strong>template." . $this->_pageName . ".html</strong> " . i18n("was not found")) . '<br>';
798: }
799:
800: if (cFileHandler::exists($file)) {
801: $output .= $template->generate($file, true);
802: } else {
803: $output .= '';
804: }
805:
806: return $output;
807: }
808:
809: 810: 811: 812: 813: 814: 815: 816: 817: 818:
819: protected function _getRealFilePathName($file) {
820: $tmp = explode('?', $file);
821: return $tmp[0];
822: }
823:
824: }