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