Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * This file contains the module template handler class.
  4:  * TODO: Rework comments of this class.
  5:  *
  6:  * @package Core
  7:  * @subpackage Backend
  8:  * @version SVN Revision $Rev:$
  9:  *
 10:  * @author Rusmir Jusufovic
 11:  * @copyright four for business AG <www.4fb.de>
 12:  * @license http://www.contenido.org/license/LIZENZ.txt
 13:  * @link http://www.4fb.de
 14:  * @link http://www.contenido.org
 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:  * Class handels the view, creation, edit, delete of modul templates.
 24:  *
 25:  * @package Core
 26:  * @subpackage Backend
 27:  */
 28: class cModuleTemplateHandler extends cModuleHandler {
 29: 
 30:     // Form fields
 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:      * The file end of template files.
 59:      *
 60:      * @var string
 61:      */
 62:     private $_templateFileEnding = 'html';
 63: 
 64:     /**
 65:      * The name of the new file.
 66:      *
 67:      * @var string
 68:      */
 69:     private $_newFileName = 'newfilename';
 70: 
 71:     /**
 72:      * Action name for create htmltpl
 73:      *
 74:      * @var string
 75:      */
 76:     private $_actionCreate = 'htmltpl_create';
 77: 
 78:     /**
 79:      * Action name for edit htmltpl
 80:      *
 81:      * @var string
 82:      */
 83:     private $_actionEdit = 'htmltpl_edit';
 84: 
 85:     /**
 86:      * Action name for delete htmltpl_edit
 87:      *
 88:      * @var string
 89:      */
 90:     private $_actionDelete = 'htmltpl_delete';
 91: 
 92:     /**
 93:      * In template we test if we have premission for htmltpl.
 94:      *
 95:      * @var string
 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:      * Set the new delete from Form.
107:      * This are set if user had push the delete or new button.
108:      *
109:      * @param string $new
110:      * @param string $delete
111:      */
112:     public function setNewDelete($new, $delete) {
113:         $this->_new = $new;
114:         $this->_delete = $delete;
115:     }
116: 
117:     /**
118:      * Set the code from Form!
119:      *
120:      * @param string $code
121:      */
122:     public function setCode($code) {
123:         $this->_code = stripslashes($code);
124:     }
125: 
126:     /**
127:      * Set the selected file from Form.
128:      *
129:      * @param string $selectedFile
130:      */
131:     public function setSelectedFile($selectedFile) {
132:         $this->_selectedFile = $selectedFile;
133:     }
134: 
135:     /**
136:      * Set the file and tmpFile from Form.
137:      * (get it with $_Request...)
138:      *
139:      * @param string $file
140:      * @param string $tmpFile
141:      */
142:     public function setFiles($file, $tmpFile) {
143:         $this->_file = $file;
144:         $this->_tmpFile = $tmpFile;
145:     }
146: 
147:     /**
148:      * Set the status it can be send or empty ''
149:      *
150:      * @param string $status
151:      */
152:     public function setStatus($status) {
153:         $this->_status = $status;
154:     }
155: 
156:     /**
157:      * Set $frame and idmod and are.
158:      *
159:      * @param int $frame
160:      * @param int $idmod
161:      * @param int $area
162:      */
163:     public function setFrameIdmodArea($frame, $idmod, $area) {
164:         $this->_frame = $frame;
165:         $this->_idmod = $idmod;
166:         $this->_area = $area;
167:     }
168: 
169:     /**
170:      * We have two actions wich could send from form.
171:      *
172:      * @param string $action
173:      */
174:     public function setAction($action) {
175:         $this->_action = $action;
176:     }
177: 
178:     /**
179:      * Checks write permissions for module template
180:      *
181:      * @return $this warning notification
182:      * @return boolean true
183:      */
184:     public function checkWritePermissions() {
185:         if ($this->moduleWriteable('template') == false && cFileHandler::exists(parent::getModulePath() . $this->_directories['template'])) {
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:      * The method decide what action is send from
194:      * user (form).
195:      *
196:      * @throws cException if one of the filenames is not set
197:      * @return string [new, delete,empty,save,rename, default]
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:                     // file ist empty also no file in template
215:                     // directory
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:                 // one of files (file or tmp_file) is not set
228:                 throw new cException(i18n('Field of the file name is empty!'));
229:             }
230:         } else {
231:             return 'default';
232:         }
233:     }
234: 
235:     /**
236:      * Has the selected file changed.
237:      *
238:      * @return boolean is the filename changed
239:      */
240:     private function _hasSelectedFileChanged() {
241:         if ($this->_file != $this->_selectedFile) {
242:             return true;
243:         } else {
244:             return false;
245:         }
246:     }
247: 
248:     /**
249:      * Save the code in the file
250:      */
251:     private function _save() {
252:         // if user selected other file display it
253:         if ($this->_hasSelectedFileChanged()) {
254:             $this->_file = $this->_selectedFile;
255:             $this->_tmpFile = $this->_selectedFile;
256:         }
257: 
258:         if (isset($this->_code)) {
259:             // trigger a smarty cache rebuild for template if changes were saved
260:             $tpl = cSmartyFrontend::getInstance();
261:             $tpl->clearCache($this->getTemplatePath($this->_file));
262: 
263:             // save the contents of file
264:             $ret = $this->createModuleFile('template', $this->_file, $this->_code);
265:             // show message
266:             if (true === $ret) {
267:                 $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Saved changes successfully!'));
268:             }
269:         }
270:     }
271: 
272:     /**
273:      * rename a file in template directory
274:      *
275:      * @throws cException if rename was not successfull
276:      */
277:     private function _rename() {
278:         // trigger a smarty cache rebuild for old and new template file name
279:         $tpl = cSmartyFrontend::getInstance();
280:         $tpl->clearCache($this->getTemplatePath($this->_tmpFile));
281:         $tpl->clearCache($this->getTemplatePath($this->_file));
282: 
283:         if ($this->renameModuleFile('template', $this->_tmpFile, $this->_file) == false) {
284:             throw new cException(i18n('Rename of the file failed!'));
285:         } else {
286:             $this->createModuleFile('template', $this->_file, $this->_code);
287:             $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Renamed the template file successfully!'));
288:             $this->_tmpFile = $this->_file;
289:         }
290:     }
291: 
292:     /**
293:      * Make new file
294:      */
295:     private function _new() {
296:         $fileName = $this->_newFileName;
297: 
298:         // if target filename already exists insert few random characters into target filename
299:         if ($this->existFile('template', $this->_newFileName . '.' . $this->_templateFileEnding)) {
300:             $fileName = $this->_newFileName . $this->getRandomCharacters(5);
301:         }
302:         $fileName = $fileName . '.' . $this->_templateFileEnding;
303:         $this->createModuleFile('template', $fileName, '');
304:         $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Created a new template file successfully!'));
305: 
306:         // trigger a smarty cache rebuild for new template file
307:         $tpl = cSmartyFrontend::getInstance();
308:         $tpl->clearCache($this->getTemplatePath($fileName));
309: 
310:         // set to new fileName
311:         $this->_file = $fileName;
312:         $this->_tmpFile = $fileName;
313:     }
314: 
315:     /**
316:      * Delete a file
317:      */
318:     private function _delete() {
319:         // trigger a smarty cache rebuild for template that should be deleted
320:         $tpl = cSmartyFrontend::getInstance();
321:         $tpl->clearCache($this->getTemplatePath($this->_tmpFile));
322: 
323:         $ret = $this->deleteFile('template', $this->_tmpFile);
324:         if ($ret == true) {
325:             $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Deleted the template file successfully!'));
326:         }
327:         $files = $this->getAllFilesFromDirectory('template');
328: 
329:         if (is_array($files)) {
330:             if (!array_key_exists('0', $files)) {
331:                 $this->_file = '';
332:                 $this->_tmpFile = '';
333:             } else {
334:                 $this->_file = $files[0];
335:                 $this->_tmpFile = $files[0];
336:             }
337:         }
338:     }
339: 
340:     /**
341:      * Default case
342:      */
343:     public function _default() {
344:         $files = $this->getAllFilesFromDirectory('template');
345: 
346:         // one or more templates files are in template direcotry
347:         if (count($files) > 0) {
348:             $this->_tmpFile = $files[0];
349:             $this->_file = $files[0];
350:         } else {
351:             // template directory is empty
352:             $this->_file = '';
353:             $this->_tmpFile = '';
354:         }
355:     }
356: 
357:     /**
358:      * Have the user premissions for the actions.
359:      *
360:      * @param cPermission $perm
361:      * @param cGuiNotification $notification
362:      * @param string $action
363:      *
364:      * @return int if user dont have permission return -1
365:      */
366:     private function _havePremission($perm, $notification, $action) {
367:         switch ($action) {
368:             case 'new':
369:                 if (!$perm->have_perm_area_action($this->_testArea, $this->_actionCreate)) {
370:                     $notification->displayNotification('error', i18n('Permission denied'));
371:                     return -1;
372:                 }
373:                 break;
374:             case 'save':
375:             case 'rename':
376:                 if (!$perm->have_perm_area_action($this->_testArea, $this->_actionEdit)) {
377:                     $notification->displayNotification('error', i18n('Permission denied'));
378:                     return -1;
379:                 }
380:                 break;
381:             case 'delete':
382:                 if (!$perm->have_perm_area_action($this->_testArea, $this->_actionDelete)) {
383:                     $notification->displayNotification('error', i18n('Permission denied'));
384:                     return -1;
385:                 }
386:                 break;
387:             default:
388:                 return true;
389:                 break;
390:         }
391:     }
392: 
393:     /**
394:      * This method test the code if the client setting htmlvalidator
395:      * is not set to false.
396:      * @param {cGuiNotification} $notification
397:      */
398:     private function _validateHTML($notification) {
399:         // Try to validate html
400:         if (getEffectiveSetting('layout', 'htmlvalidator', 'true') == 'true' && $this->_code !== '') {
401:             $v = new cHTMLValidator();
402:             $v->validate($this->_code);
403:             $msg = '';
404: 
405:             foreach ($v->missingNodes as $value) {
406:                 $idQualifier = '';
407: 
408:                 $attr = array();
409: 
410:                 if ($value['name'] != '') {
411:                     $attr['name'] = "name '" . $value['name'] . "'";
412:                 }
413: 
414:                 if ($value['id'] != '') {
415:                     $attr['id'] = "id '" . $value['id'] . "'";
416:                 }
417: 
418:                 $idQualifier = implode(', ', $attr);
419: 
420:                 if ($idQualifier != '') {
421:                     $idQualifier = "($idQualifier)";
422:                 }
423:                 $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>';
424:             }
425: 
426:             if ($msg != '') {
427:                 $notification->displayNotification('warning', $msg) . '<br>';
428:             }
429:         }
430:     }
431: 
432:     private function _makeFormular($belang, $readOnly) {
433:         $fileForm = new cGuiTableForm("file__chooser");
434:         $fileForm->addHeader(i18n('Choose file'));
435:         $fileForm->setTableid('choose_mod_template_file');
436:         $fileForm->setVar('area', $this->_area);
437:         $fileForm->setVar('action', $this->_action);
438:         $fileForm->setVar('frame', $this->_frame);
439:         $fileForm->setVar('status', 'send');
440:         $fileForm->setVar('tmp_file', $this->_tmpFile);
441:         $fileForm->setVar('idmod', $this->_idmod);
442:         $fileForm->setVar('file', $this->_file);
443: 
444:         $form = new cGuiTableForm('file_editor');
445:         $form->setTableid('mod_template');
446:         $form->addHeader(i18n('Edit file'));
447:         $form->setVar('area', $this->_area);
448:         $form->setVar('action', $this->_action);
449:         $form->setVar('frame', $this->_frame);
450:         $form->setVar('status', 'send');
451:         $form->setVar('tmp_file', $this->_tmpFile);
452:         $form->setVar('idmod', $this->_idmod);
453:         $form->setVar('file', $this->_file);
454:         $form->setVar('selectedFile', $this->_file);
455: 
456:         $selectFile = new cHTMLSelectElement('selectedFile');
457:         $selectFile->setClass("fileChooser");
458:         // array with all files in template directory
459:         $filesArray = $this->getAllFilesFromDirectory('template');
460: 
461:         if (true === is_array($filesArray)) {
462: 
463:             // make options fields
464:             foreach ($filesArray as $key => $file) {
465: 
466:                 // ignore dirs
467:                 if (is_dir($file)) {
468:                     continue;
469:                 }
470: 
471:                 $optionField = new cHTMLOptionElement($file, $file);
472: 
473:                 // select the current file
474:                 if ($file == $this->_file) {
475:                     $optionField->setAttribute('selected', 'selected');
476:                 }
477: 
478:                 $selectFile->addOptionElement($key, $optionField);
479:             }
480: 
481:         }
482: 
483:         $aDelete = new cHTMLLink('main.php');
484:         $aDelete->setId("deleteLink");
485:         $aDelete->setContent(i18n("Delete HTML-template"));
486:         $aDelete->setClass('deletefunction');
487:         $aDelete->setCustom("deleteModTpl", "1");
488:         $aDelete->setCustom('area', $this->_area);
489:         $aDelete->setCustom('action', $this->_actionDelete);
490:         $aDelete->setCustom('frame', $this->_frame);
491:         $aDelete->setCustom('status', 'send');
492:         $aDelete->setCustom('idmod', $this->_idmod);
493:         $aDelete->setCustom('file', $this->_file);
494:         $aDelete->setCustom('tmp_file', $this->_tmpFile);
495: 
496:         $aAdd = new cHTMLLink('main.php');
497:         $aAdd->setContent(i18n('New HTML-template'));
498:         $aAdd->setClass('addfunction');
499:         $aAdd->setCustom("newModTpl", "1");
500:         $aAdd->setCustom('area', $this->_area);
501:         $aAdd->setCustom('action', $this->_actionCreate);
502:         $aAdd->setCustom('frame', $this->_frame);
503:         $aAdd->setCustom('status', 'send');
504:         $aAdd->setCustom('tmp_file', $this->_tmpFile);
505:         $aAdd->setCustom('idmod', $this->_idmod);
506:         $aAdd->setCustom('file', $this->_file);
507: 
508:         // $oName = new cHTMLLabel($sFilename, '');
509:         $oName = new cHTMLTextbox('file', $this->_file, 60);
510: 
511:         $oCode = new cHTMLTextarea('code', conHtmlSpecialChars($this->_code), 100, 35, 'code');
512: 
513:         $oCode->setStyle('font-family: monospace;width: 100%;');
514: 
515:         $oCode->updateAttributes(array(
516:             'wrap' => getEffectiveSetting('html_editor', 'wrap', 'off')
517:         ));
518: 
519:         $fileForm->add(i18n('Action'), $aAdd->toHTML());
520:         // show only if file exists
521:         if ($this->_file) {
522:             $fileForm->add(i18n('Action'), $aDelete->toHTML());
523:             $fileForm->add(i18n('File'), $selectFile);
524:         }
525: 
526:         if($readOnly) {
527:             $oName->setDisabled('disabled');
528:         }
529: 
530:         // add fields only if template file exists
531:         if ($this->_file) {
532:             $form->add(i18n('Name'), $oName);
533:             $form->add(i18n('Code'), $oCode);
534:         }
535:         $this->_page->setContent(array(
536:             $fileForm
537:         ));
538:         if ($this->_file) {
539:             $this->_page->appendContent($form);
540:         }
541: 
542:         $oCodeMirror = new CodeMirror('code', 'html', substr(strtolower($belang), 0, 2), true, $this->_cfg);
543:         if($readOnly) {
544:             $oCodeMirror->setProperty("readOnly", "true");
545: 
546:             $form->setActionButton('submit', cRegistry::getBackendUrl() . 'images/but_ok_off.gif', i18n('Overwriting files is disabled'), 's');
547:         }
548:         $this->_page->addScript($oCodeMirror->renderScript());
549: 
550:         // $this->_page->addScript('reload', $this->_reloadScript);
551:     }
552: 
553:     /**
554:      * Display the form and evaluate the action and excute the action.
555:      *
556:      * @param cPermission $perm
557:      * @param cGuiNotification $notification
558:      * @param string Backend language (not sure about this...)
559:      * @param bool render in read only mode
560:      */
561:     public function display($perm, $notification, $belang, $readOnly) {
562:         $myAction = $this->_getAction();
563: 
564:         // if the user doesn't have permissions
565:         if ($this->_havePremission($perm, $notification, $myAction) === -1) {
566:             return;
567:         }
568: 
569:         try {
570:             switch ($myAction) {
571:                 case 'save':
572:                     if(!$readOnly) {
573:                         $this->_save();
574:                     }
575:                     break;
576:                 case 'rename':
577:                     if(!$readOnly) {
578:                         $this->_rename();
579:                     }
580:                     break;
581:                 case 'new':
582:                     if(!$readOnly) {
583:                         $this->_new();
584:                     }
585:                     break;
586:                 case 'delete':
587:                     if(!$readOnly) {
588:                         $this->_delete();
589:                     }
590:                     break;
591:                 default:
592:                     $this->_default();
593:                     break;
594:             }
595: 
596:             $this->_code = $this->getFilesContent('template', '', $this->_file);
597:             $this->_validateHTML($notification);
598:             $this->_makeFormular($belang, $readOnly);
599:         } catch (Exception $e) {
600:             $this->_page->displayError(i18n($e->getMessage()));
601:         }
602:     }
603: 
604: }
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen