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