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