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: cInclude('external', 'codemirror/class.codemirror.php');
20: cInclude('includes', 'functions.file.php');
21:
22: 23: 24: 25: 26: 27:
28: class cModuleTemplateHandler extends cModuleHandler {
29:
30: 31: 32: 33: 34:
35: private $_code;
36:
37: 38: 39: 40:
41: private $_file;
42:
43: 44: 45: 46:
47: private $_tmpFile;
48:
49: 50: 51: 52:
53: private $_area;
54:
55: 56: 57: 58:
59: private $_frame;
60:
61: 62: 63: 64:
65: private $_status;
66:
67: 68: 69: 70:
71: private $_action;
72:
73: 74: 75: 76:
77: private $_new;
78:
79: 80: 81: 82:
83: private $_delete;
84:
85: 86: 87: 88:
89: private $_selectedFile;
90:
91: 92: 93: 94:
95: private $_page = NULL;
96:
97: 98: 99: 100:
101: private $_notification = NULL;
102:
103: 104: 105: 106: 107:
108: private $_templateFileEnding = 'html';
109:
110: 111: 112: 113: 114:
115: private $_newFileName = 'newfilename';
116:
117: 118: 119: 120: 121:
122: private $_actionCreate = 'htmltpl_create';
123:
124: 125: 126: 127: 128:
129: private $_actionEdit = 'htmltpl_edit';
130:
131: 132: 133: 134: 135:
136: private $_actionDelete = 'htmltpl_delete';
137:
138: 139: 140: 141: 142:
143: private $_testArea = 'htmltpl';
144:
145: 146: 147: 148: 149: 150:
151: public function __construct($idmod, $page) {
152: parent::__construct($idmod);
153: $this->_page = $page;
154: $this->_notification = new cGuiNotification();
155: }
156:
157: 158: 159: 160: 161: 162: 163:
164: public function setNewDelete($new, $delete) {
165: $this->_new = $new;
166: $this->_delete = $delete;
167: }
168:
169: 170: 171: 172: 173:
174: public function setCode($code) {
175: $this->_code = stripslashes($code);
176: }
177:
178: 179: 180: 181: 182:
183: public function setSelectedFile($selectedFile) {
184: $this->_selectedFile = $selectedFile;
185: }
186:
187: 188: 189: 190: 191: 192: 193:
194: public function setFiles($file, $tmpFile) {
195: $this->_file = $file;
196: $this->_tmpFile = $tmpFile;
197: }
198:
199: 200: 201: 202: 203:
204: public function setStatus($status) {
205: $this->_status = $status;
206: }
207:
208: 209: 210: 211: 212: 213: 214:
215: public function setFrameIdmodArea($frame, $idmod, $area) {
216: $this->_frame = $frame;
217: $this->_idmod = $idmod;
218: $this->_area = $area;
219: }
220:
221: 222: 223: 224: 225:
226: public function setAction($action) {
227: $this->_action = $action;
228: }
229:
230: 231: 232: 233: 234:
235: public function checkWritePermissions() {
236: if ($this->moduleWriteable('template') == true && cFileHandler::exists(parent::getModulePath() . $this->_directories['template'])) {
237: return $this->_notification->displayNotification(cGuiNotification::LEVEL_WARNING, sprintf(i18n("You have no write permissions for this module: %s"), parent::getModuleName()));
238: } else {
239: return true;
240: }
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250: 251:
252: private function _getAction() {
253: global $newModTpl, $deleteModTpl;
254:
255: if (isset($this->_status)) {
256:
257: if (isset($newModTpl)) {
258: return 'new';
259: }
260:
261: if (isset($deleteModTpl)) {
262: return 'delete';
263: }
264:
265: if (isset($this->_file) && isset($this->_tmpFile)) {
266: if ($this->_file == $this->_tmpFile) {
267:
268:
269: if (empty($this->_file)) {
270: return 'empty';
271: } else {
272: return 'save';
273: }
274: }
275:
276: if ($this->_file != $this->_tmpFile) {
277: return 'rename';
278: }
279: } else {
280:
281: throw new cException(i18n('Field of the file name is empty!'));
282: }
283: } else {
284: return 'default';
285: }
286: }
287:
288: 289: 290: 291: 292:
293: private function _hasSelectedFileChanged() {
294: if ($this->_file != $this->_selectedFile) {
295: return true;
296: } else {
297: return false;
298: }
299: }
300:
301: 302: 303:
304: private function _save() {
305:
306: if ($this->_hasSelectedFileChanged()) {
307: $this->_file = $this->_selectedFile;
308: $this->_tmpFile = $this->_selectedFile;
309: }
310:
311: if (isset($this->_code)) {
312:
313:
314: if (class_exists('cSmartyFrontend')) {
315: $tpl = cSmartyFrontend::getInstance();
316: $tpl->clearCache($this->getTemplatePath($this->_file));
317: }
318:
319:
320: $ret = $this->createModuleFile('template', $this->_file, $this->_code);
321:
322: if (true === $ret) {
323: $this->_notification->displayNotification(cGuiNotification::LEVEL_OK, i18n('Saved changes successfully!'));
324: }
325: }
326: }
327:
328: 329: 330: 331: 332:
333: private function _rename() {
334:
335:
336: if (class_exists('cSmartyFrontend')) {
337: $tpl = cSmartyFrontend::getInstance();
338: $tpl->clearCache($this->getTemplatePath($this->_tmpFile));
339: $tpl->clearCache($this->getTemplatePath($this->_file));
340: }
341:
342: if ($this->renameModuleFile('template', $this->_tmpFile, $this->_file) == false) {
343: throw new cException(i18n('Rename of the file failed!'));
344: } else {
345: $this->createModuleFile('template', $this->_file, $this->_code);
346: $this->_notification->displayNotification(cGuiNotification::LEVEL_OK, i18n('Renamed the template file successfully!'));
347: $this->_tmpFile = $this->_file;
348: }
349: }
350:
351: 352: 353:
354: private function _new() {
355: $fileName = $this->_newFileName;
356:
357:
358: $fileName = $this->_newFileName . '.' . $this->_templateFileEnding;
359: while ($this->existFile('template', $fileName)) {
360: $fileName = $this->_newFileName . $this->getRandomCharacters(5) . '.' . $this->_templateFileEnding;
361: }
362: $this->createModuleFile('template', $fileName, '');
363: $this->_notification->displayNotification(cGuiNotification::LEVEL_OK, i18n('Created a new template file successfully!'));
364:
365:
366:
367: if (class_exists('cSmartyFrontend')) {
368: $tpl = cSmartyFrontend::getInstance();
369: $tpl->clearCache($this->getTemplatePath($fileName));
370: }
371:
372:
373: $this->_file = $fileName;
374: $this->_tmpFile = $fileName;
375: }
376:
377: 378: 379:
380: private function _delete() {
381:
382:
383: if (class_exists('cSmartyFrontend')) {
384: $tpl = cSmartyFrontend::getInstance();
385: $tpl->clearCache($this->getTemplatePath($this->_tmpFile));
386: }
387:
388: $ret = $this->deleteFile('template', $this->_tmpFile);
389: if ($ret == true) {
390: $this->_notification->displayNotification(cGuiNotification::LEVEL_OK, i18n('Deleted the template file successfully!'));
391: }
392: $files = $this->getAllFilesFromDirectory('template');
393:
394: if (is_array($files)) {
395: if (!array_key_exists('0', $files)) {
396: $this->_file = '';
397: $this->_tmpFile = '';
398: } else {
399: $this->_file = $files[0];
400: $this->_tmpFile = $files[0];
401: }
402: }
403: }
404:
405: 406: 407:
408: public function _default() {
409: $files = $this->getAllFilesFromDirectory('template');
410:
411:
412: if (count($files) > 0) {
413: $this->_tmpFile = $files[0];
414: $this->_file = $files[0];
415: } else {
416:
417: $this->_file = '';
418: $this->_tmpFile = '';
419: }
420: }
421:
422: 423: 424: 425: 426: 427: 428: 429: 430:
431: private function _havePermission($perm, $notification, $action) {
432: switch ($action) {
433: case 'new':
434: if (!$perm->have_perm_area_action($this->_testArea, $this->_actionCreate)) {
435: $notification->displayNotification('error', i18n('Permission denied'));
436: return -1;
437: }
438: break;
439: case 'save':
440: case 'rename':
441: if (!$perm->have_perm_area_action($this->_testArea, $this->_actionEdit)) {
442: $notification->displayNotification('error', i18n('Permission denied'));
443: return -1;
444: }
445: break;
446: case 'delete':
447: if (!$perm->have_perm_area_action($this->_testArea, $this->_actionDelete)) {
448: $notification->displayNotification('error', i18n('Permission denied'));
449: return -1;
450: }
451: break;
452: default:
453: return true;
454: break;
455: }
456: }
457:
458: 459: 460: 461: 462: 463:
464: private function _validateHTML($notification) {
465:
466: if (getEffectiveSetting('layout', 'htmlvalidator', 'true') == 'true' && $this->_code !== '') {
467: $v = new cHTMLValidator();
468: $v->validate($this->_code);
469: $msg = '';
470:
471: foreach ($v->missingNodes as $value) {
472: $idQualifier = '';
473:
474: $attr = array();
475:
476: if ($value['name'] != '') {
477: $attr['name'] = "name '" . $value['name'] . "'";
478: }
479:
480: if ($value['id'] != '') {
481: $attr['id'] = "id '" . $value['id'] . "'";
482: }
483:
484: $idQualifier = implode(', ', $attr);
485:
486: if ($idQualifier != '') {
487: $idQualifier = "($idQualifier)";
488: }
489: $msg .= sprintf(i18n("Tag '%s' %s has no end tag (start tag is on line %s char %s)"), $value['tag'], $idQualifier, $value['line'], $value['char']) . '<br>';
490: }
491:
492: if ($msg != '') {
493: $notification->displayNotification('warning', $msg) . '<br>';
494: }
495: }
496: }
497:
498: 499: 500: 501: 502:
503: private function _makeFormular($belang, $readOnly) {
504: $fileForm = new cGuiTableForm("file__chooser");
505: $fileForm->addHeader(i18n('Choose file'));
506: $fileForm->setTableID('choose_mod_template_file');
507: $fileForm->setVar('area', $this->_area);
508: $fileForm->setVar('action', $this->_action);
509: $fileForm->setVar('frame', $this->_frame);
510: $fileForm->setVar('status', 'send');
511: $fileForm->setVar('tmp_file', conHtmlSpecialChars($this->_tmpFile));
512: $fileForm->setVar('idmod', $this->_idmod);
513: $fileForm->setVar('file', conHtmlSpecialChars($this->_file));
514:
515: $form = new cGuiTableForm('file_editor');
516: $form->setTableID('mod_template');
517: $form->addHeader(i18n('Edit file'));
518: $form->setVar('area', $this->_area);
519: $form->setVar('action', $this->_action);
520: $form->setVar('frame', $this->_frame);
521: $form->setVar('status', 'send');
522: $form->setVar('tmp_file', conHtmlSpecialChars($this->_tmpFile));
523: $form->setVar('idmod', $this->_idmod);
524: $form->setVar('file', conHtmlSpecialChars($this->_file));
525: $form->setVar('selectedFile', conHtmlSpecialChars($this->_file));
526:
527: $selectFile = new cHTMLSelectElement('selectedFile');
528: $selectFile->setClass("fileChooser");
529:
530: $filesArray = $this->getAllFilesFromDirectory('template');
531:
532: if (true === is_array($filesArray)) {
533:
534:
535: foreach ($filesArray as $key => $file) {
536:
537:
538: if (is_dir($file)) {
539: continue;
540: }
541:
542:
543: $optionField = new cHTMLOptionElement(conHtmlSpecialChars($file), conHtmlSpecialChars($file));
544:
545:
546: if ($file == $this->_file) {
547: $optionField->setAttribute('selected', 'selected');
548: }
549:
550: $selectFile->addOptionElement($key, $optionField);
551: }
552:
553: }
554:
555: $aDelete = new cHTMLLink('main.php');
556: $aDelete->setID("deleteLink");
557: $aDelete->setContent(i18n("Delete HTML-template"));
558: $aDelete->setClass('deletefunction');
559: $aDelete->setCustom("deleteModTpl", "1");
560: $aDelete->setCustom('area', $this->_area);
561: $aDelete->setCustom('action', $this->_actionDelete);
562: $aDelete->setCustom('frame', $this->_frame);
563: $aDelete->setCustom('status', 'send');
564: $aDelete->setCustom('idmod', $this->_idmod);
565: $aDelete->setCustom('file', urlencode($this->_file));
566: $aDelete->setCustom('tmp_file', urlencode($this->_tmpFile));
567:
568: $aAdd = new cHTMLLink('main.php');
569: $aAdd->setContent(i18n('New HTML-template'));
570: $aAdd->setClass('addfunction');
571: $aAdd->setCustom("newModTpl", "1");
572: $aAdd->setCustom('area', $this->_area);
573: $aAdd->setCustom('action', $this->_actionCreate);
574: $aAdd->setCustom('frame', $this->_frame);
575: $aAdd->setCustom('status', 'send');
576: $aAdd->setCustom('tmp_file', urlencode($this->_tmpFile));
577: $aAdd->setCustom('idmod', $this->_idmod);
578: $aAdd->setCustom('file', urlencode($this->_file));
579:
580:
581: $oName = new cHTMLTextbox('file', conHtmlSpecialChars($this->_file), 60);
582:
583: $oCode = new cHTMLTextarea('code', conHtmlSpecialChars($this->_code), 100, 35, 'code');
584:
585: $oCode->setStyle('font-family: monospace;width: 100%;');
586:
587: $oCode->updateAttributes(array(
588: 'wrap' => getEffectiveSetting('html_editor', 'wrap', 'off')
589: ));
590:
591: $fileForm->add(i18n('Action'), $aAdd->toHtml());
592:
593: if ($this->_file) {
594: $fileForm->add(i18n('Action'), $aDelete->toHtml());
595: $fileForm->add(i18n('File'), $selectFile);
596: }
597:
598: if($readOnly) {
599: $oName->setDisabled('disabled');
600: }
601:
602:
603: if ($this->_file) {
604: $form->add(i18n('Name'), $oName);
605: $form->add(i18n('Code'), $oCode);
606: }
607: $this->_page->setContent(array(
608: $fileForm
609: ));
610: if ($this->_file) {
611: $this->_page->appendContent($form);
612: }
613:
614: $oCodeMirror = new CodeMirror('code', 'html', substr(strtolower($belang), 0, 2), true, $this->_cfg);
615: if($readOnly) {
616: $oCodeMirror->setProperty("readOnly", "true");
617:
618: $form->setActionButton('submit', cRegistry::getBackendUrl() . 'images/but_ok_off.gif', i18n('Overwriting files is disabled'), 's');
619: }
620: $this->_page->addScript($oCodeMirror->renderScript());
621:
622:
623: }
624:
625: 626: 627: 628: 629: 630: 631: 632: 633: 634:
635: public function display($perm, $notification, $belang, $readOnly) {
636: $myAction = $this->_getAction();
637:
638:
639: if ($this->_havePermission($perm, $notification, $myAction) === -1) {
640: return;
641: }
642:
643: try {
644: switch ($myAction) {
645: case 'save':
646: if(!$readOnly) {
647: $this->_save();
648: }
649: break;
650: case 'rename':
651: if(!$readOnly) {
652: $this->_rename();
653: }
654: break;
655: case 'new':
656: if(!$readOnly) {
657: $this->_new();
658: }
659: break;
660: case 'delete':
661: if(!$readOnly) {
662: $this->_delete();
663: }
664: break;
665: default:
666: $this->_default();
667: break;
668: }
669:
670: $this->_code = $this->getFilesContent('template', '', $this->_file);
671: $this->_validateHTML($notification);
672: $this->_makeFormular($belang, $readOnly);
673: } catch (Exception $e) {
674: $this->_page->displayError(i18n($e->getMessage()));
675: }
676: }
677:
678: }
679: