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