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) && cString::findFirstPos(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 (cString::findFirstPos(trim($script), 'http') === 0 || cString::findFirstPos(trim($script), '<script') === 0 || cString::findFirstPos(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 (cString::findFirstPos($stylesheet, 'http') === 0 || cString::findFirstPos($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: 655:
656: public function render($template = NULL, $return = false) {
657:
658: if ($template == NULL) {
659: $template = $this->_contentTemplate;
660: }
661:
662:
663: $this->_renderMetaTags();
664: $this->_renderScripts();
665: $this->_renderStyles();
666:
667:
668: $this->_pageTemplate->set('s', 'PAGECLASS', implode(' ', $this->_bodyClassNames));
669:
670:
671: $text = $this->_renderContentMessages();
672: if (cString::getStringLength(trim($text)) > 0) {
673: $this->_skipTemplateCheck = true;
674: }
675:
676: if (!$this->_abort) {
677: if (count($this->_objects) == 0) {
678: $output = $this->_renderTemplate($template);
679: } else {
680: $output = $this->_renderObjects();
681: }
682: $this->_pageTemplate->set('s', 'CONTENT', $text . $output);
683: } else {
684: $this->_pageTemplate->set('s', 'CONTENT', $text);
685: }
686:
687: return $this->_pageTemplate->generate($this->_pageBase, $return);
688: }
689:
690: 691: 692:
693: protected function _renderMetaTags() {
694:
695:
696:
697: $produceXhtml = false;
698: $meta = '';
699: foreach ($this->_metaTags as $metaTag) {
700: $tag = '<meta';
701: foreach ($metaTag as $key => $value) {
702: $tag .= ' ' . $key . '="' . $value . '"';
703: }
704: if ($produceXhtml) {
705: $tag .= ' /';
706: }
707: $tag .= ">\n";
708: $meta .= $tag;
709: }
710: if (!empty($meta)) {
711: $this->_pageTemplate->set('s', 'META', $meta);
712: } else {
713: $this->_pageTemplate->set('s', 'META', '');
714: }
715: }
716:
717: 718: 719:
720: protected function _renderScripts() {
721: $strscript = $this->_subnav . "\n" . $this->_markScript . "\n";
722: foreach ($this->_scripts as $script) {
723: if (cString::findFirstPos($script, 'http') === 0 || cString::findFirstPos($script, '//') === 0) {
724: $strscript .= '<script type="text/javascript" src="' . $script . '"></script>' . "\n";
725: } else if (cString::findFirstPos($script, '<script') === false) {
726: $strscript .= '<script type="text/javascript" src="scripts/' . $script . '"></script>' . "\n";
727: } else {
728: $strscript .= $script;
729: }
730: }
731: $this->_pageTemplate->set('s', 'SCRIPTS', $strscript);
732: }
733:
734: 735: 736:
737: protected function _renderStyles() {
738: $strstyle = '';
739: foreach ($this->_styles as $style) {
740: if (cString::findFirstPos($style, 'http') === 0 || cString::findFirstPos($style, '//') === 0) {
741: $strstyle .= '<link href="' . $style . '" type="text/css" rel="stylesheet">' . "\n";
742: } else {
743: $strstyle .= '<link href="styles/' . $style . '" type="text/css" rel="stylesheet">' . "\n";
744: }
745: }
746: $this->_pageTemplate->set('s', 'STYLES', $strstyle);
747: }
748:
749: 750: 751: 752: 753: 754:
755: protected function _renderContentMessages() {
756: global $notification;
757:
758:
759: $okMessages = cRegistry::getOkMessages();
760: foreach ($okMessages as $message) {
761: $this->displayOk($message);
762: }
763:
764: $infoMessages = cRegistry::getInfoMessages();
765: foreach ($infoMessages as $message) {
766: $this->displayInfo($message);
767: }
768:
769: $errorMessages = cRegistry::getErrorMessages();
770: foreach ($errorMessages as $message) {
771: $this->displayError($message);
772: }
773:
774: $warningMessages = cRegistry::getWarningMessages();
775: foreach ($warningMessages as $message) {
776: $this->displayWarning($message);
777: }
778:
779: $text = '';
780: if ($this->_ok != '') {
781: $text .= $notification->returnNotification('ok', $this->_ok) . '<br>';
782: }
783: if ($this->_info != '') {
784: $text .= $notification->returnNotification('info', $this->_info) . '<br>';
785: }
786: if ($this->_warning != '') {
787: $text .= $notification->returnNotification('warning', $this->_warning) . '<br>';
788: }
789: if ($this->_error != '') {
790: $text .= $notification->returnNotification('error', $this->_error) . '<br>';
791: }
792:
793: return $text;
794: }
795:
796: 797: 798: 799: 800: 801:
802: protected function _renderObjects() {
803: $output = '';
804:
805: foreach ($this->_objects as $obj) {
806: if (is_string($obj)) {
807: $output .= $obj;
808: }
809:
810: if (!method_exists($obj, 'render')) {
811: continue;
812: }
813:
814:
815:
816:
817: $oldOutput = $output;
818:
819:
820:
821: ob_start();
822: $output .= $obj->render(false);
823:
824:
825: $output .= ob_get_contents();
826: if ($oldOutput == $output) {
827: cWarning(__FILE__, __LINE__, "Rendering this object (" . print_r($obj, true) . ") doesn't seem to have any effect.");
828: }
829: ob_end_clean();
830: }
831:
832: return $output;
833: }
834:
835: 836: 837: 838: 839: 840: 841:
842: protected function _renderTemplate($template) {
843: global $perm, $currentuser, $notification;
844:
845: $cfg = cRegistry::getConfig();
846:
847: $file = '';
848: if ($this->_pluginName == '') {
849: $file = $cfg['path']['templates'] . 'template.' . $this->_pageName . '.html';
850: } else {
851: $file = $cfg['path']['plugins'] . $this->_pluginName . '/templates/template.' . $this->_pageName . '.html';
852: }
853:
854: $output = '';
855:
856: if (!$this->_skipTemplateCheck && $perm->isSysadmin($currentuser) && !cFileHandler::exists($file)) {
857: $output .= $notification->returnNotification('warning', i18n("The requested resource") . " <strong>template." . $this->_pageName . ".html</strong> " . i18n("was not found")) . '<br>';
858: }
859:
860: if (cFileHandler::exists($file)) {
861: $output .= $template->generate($file, true);
862: } else {
863: $output .= '';
864: }
865:
866: return $output;
867: }
868:
869: 870: 871: 872: 873: 874: 875: 876: 877: 878:
879: protected function _getRealFilePathName($file) {
880: $tmp = explode('?', $file);
881: return $tmp[0];
882: }
883:
884: }
885: