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