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

Classes

  • cGuiBackendHelpbox
  • cGuiFileOverview
  • cGuiFoldingRow
  • cGuiList
  • cGuiMenu
  • cGuiNavigation
  • cGuiNotification
  • cGuiObjectPager
  • cGuiPage
  • cGuiScrollList
  • cGuiSourceEditor
  • cGuiTableForm
  • cGuiTree
  • cPager
  • cTemplate
  • cTree
  • cTreeItem
  • NoteLink
  • NoteList
  • NoteListItem
  • NoteView
  • TODOLink
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains the generic source editor class. It is used for editing HTML templates, JS files and CSS files
  5:  *
  6:  * @package Core
  7:  * @subpackage GUI
  8:  * @author Mischa Holz
  9:  * @copyright four for business AG <www.4fb.de>
 10:  * @license http://www.contenido.org/license/LIZENZ.txt
 11:  * @link http://www.4fb.de
 12:  * @link http://www.contenido.org
 13:  */
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * Source editor class.
 19:  *
 20:  * @package Core
 21:  * @subpackage GUI
 22:  */
 23: class cGuiSourceEditor extends cGuiPage {
 24: 
 25:     /**
 26:      * Name of the file that is being edited.
 27:      *
 28:      * @var string
 29:      */
 30:     protected $_filename;
 31: 
 32:     /**
 33:      * Name of the file that is being edited.
 34:      *
 35:      * This variable is different to $filename only if you rename your
 36:      * file.
 37:      *
 38:      * @var string
 39:      */
 40:     protected $_versionfilename;
 41: 
 42:     /**
 43:      * Full path to the file that is being edited.
 44:      *
 45:      * @var string
 46:      */
 47:     protected $_filepath;
 48: 
 49:     /**
 50:      * CodeMirror type of the file that is being edited.
 51:      *
 52:      * @var string
 53:      */
 54:     protected $_filetype;
 55: 
 56:     /**
 57:      * CodeMirror instance.
 58:      *
 59:      * @var object
 60:      */
 61:     protected $_codeMirror;
 62: 
 63:     /**
 64:      * Read-only mode or not.
 65:      *
 66:      * @var bool
 67:      */
 68:     protected $_readOnly;
 69: 
 70:     /**
 71:      * Versioning or not.
 72:      *
 73:      * @var bool
 74:      */
 75:     protected $_versioning;
 76: 
 77:     /**
 78:      * Constructor to create an instance of this class.
 79:      *
 80:      * Initializes the class and its parent.
 81:      *
 82:      * @param string $filename
 83:      *         Name of the edited file
 84:      * @param bool $versioning [optional]
 85:      *         Is versioning activated or not. Defaults to true
 86:      * @param string $filetype [optional]
 87:      *         The type of the file. If ommited the class tries to determine
 88:      *         the type from the area
 89:      * @param string $filepath [optional]
 90:      *         Path to the file. If ommited the class tries to determine the
 91:      *         path from the type and the area
 92:      */
 93:     public function __construct($filename, $versioning = true, $filetype = '', $filepath = '') {
 94:         global $belang, $cfgClient;
 95: 
 96:         $cfg = cRegistry::getConfig();
 97:         $client = cRegistry::getClientId();
 98:         $perm = cRegistry::getPerm();
 99:         $area = cRegistry::getArea();
100:         $action = cRegistry::getAction();
101: 
102:         // call parent constructor
103:         parent::__construct("generic_source_editor");
104: 
105:         // check permissions
106:         if (!$perm->have_perm_area_action($area, $action)) {
107:             $this->displayCriticalError(i18n('Permission denied'));
108:         }
109: 
110:         // display empty page if no client is selected
111:         if (!(int) $client > 0) {
112:             $this->abortRendering();
113:         }
114: 
115:         // determine the filetype and path by using the area
116:         if($filetype == '') {
117:             switch($_REQUEST['area']) {
118:                 case 'style':
119:                     $filepath = $cfgClient[$client]['css']['path'] . $filename;
120:                     $filetype = 'css';
121:                     break;
122:                 case 'js':
123:                     $filepath = $cfgClient[$client]['js']['path'] . $filename;
124:                     $filetype = 'js';
125:                     break;
126:                 case 'htmltpl':
127:                     $filepath = $cfgClient[$client]['tpl']['path'] . $filename;
128:                     $filetype = 'html';
129:                     break;
130:             }
131:         }
132: 
133:         // assign variables
134:         $this->_filetype = $filetype;
135:         $this->_filepath = $filepath;
136: 
137:         $this->_readOnly = (getEffectiveSetting("client", "readonly", "false") == "true");
138:         if($this->_readOnly) {
139:             cRegistry::addWarningMessage(i18n("This area is read only! The administrator disabled edits!"));
140:         }
141: 
142:         $this->_filename = $filename;
143: 
144:         // include the class and create the codemirror instance
145:         cInclude('external', 'codemirror/class.codemirror.php');
146:         $this->_codeMirror = new CodeMirror('code', $this->_filetype, substr(strtolower($belang), 0, 2), true, $cfg, !$this->_readOnly);
147: 
148:         $this->_versioning = $versioning;
149: 
150:         // update the edited file by using the super global _REQUEST
151:         $this->update($_REQUEST);
152:     }
153: 
154:     /**
155:      * Updates the file according to the options in the array.
156:      *
157:      * @param array $req
158:      *         Request array. Usually _REQUEST
159:      */
160:     protected function update($req) {
161:         global $cfgClient;
162: 
163:         $cfg = cRegistry::getConfig();
164:         $client = cRegistry::getClientId();
165:         $db = cRegistry::getDb();
166:         $frame = cRegistry::getFrame();
167:         $perm = cRegistry::getPerm();
168:         $area = cRegistry::getArea();
169:         $action = cRegistry::getAction();
170: 
171:         // check permissions
172:         if (!$perm->have_perm_area_action($area, $action)) {
173:             $this->displayCriticalError(i18n('Permission denied'));
174:         }
175: 
176:         // if read only is activated or no data has been sent, skip the update step
177:         if( ($this->_readOnly || ($req['status'] != 'send')) && $req['delfile'] == '') {
178:             if($req['action'] == '') {
179:                $this->abortRendering();
180:             }
181:             return;
182:         }
183: 
184:         // if magic quotes are on, strip slashes from the array
185:         if(ini_get('magic_quotes_gpc')) {
186:             foreach($req as $key => $value) {
187:                 $req[$key] = stripslashes($value);
188:             }
189:         }
190: 
191:         // determine the file type for the file information table
192:         $dbFileType = '';
193:         switch($req['area']) {
194:             case 'style':
195:                 $dbFileType = 'css';
196:                 break;
197:             case 'js':
198:                 $dbFileType = 'js';
199:                 break;
200:             case 'htmltpl':
201:                 $dbFileType = 'templates';
202:                 break;
203:         }
204: 
205:         // delete the specified file
206:         if($req['delfile'] != '') {
207:             // check if it exists
208:             if(cFileHandler::exists($this->_filepath . $req['delfile'])) {
209:                 // load information
210:                 $fileInfos = new cApiFileInformationCollection();
211:                 $fileInfos->select('filename = \'' . $req['delfile'] . '\'');
212:                 $fileInfo = $fileInfos->next();
213:                 // if there is information and if there are versioning files, delete them
214:                 if($fileInfo != null) {
215:                     $idsfi = $fileInfo->get('idsfi');
216: 
217:                     if (cSecurity::isInteger($idsfi) && is_dir($cfgClient[$client]['version']['path'] . "$dbFileType/$idsfi")) {
218:                         cDirHandler::recursiveRmdir($cfgClient[$client]['version']['path'] . "$dbFileType/$idsfi");
219:                     }
220:                 }
221: 
222:                 // remove the file
223:                 cFileHandler::remove($this->_filepath . $req['delfile']);
224: 
225:                 // remove the file information
226:                 $fileInfos->removeFileInformation(array(
227:                         'filename' => $req['delfile']
228:                 ));
229: 
230:                 // display the information and reload the frame
231:                 $this->displayOk(i18n('File deleted successfully!'));
232:                 $this->abortRendering();
233: 
234:                 $this->reloadFrame('left_bottom', array());
235:                 $this->reloadFrame('right_top', "main.php?area=$area&frame=3");
236:             }
237:             return;
238:         }
239: 
240:         // Set version filename
241:         $this->_versionfilename = $this->_filename;
242: 
243:         // if the filename is empty, display an empty editor and create a new file
244:         if(is_dir($this->_filepath) && cFileHandler::writeable($this->_filepath)) {
245:             // validate the file name
246:             if(!cFileHandler::validateFilename($req['file'], false)) {
247:                 $this->displayError(i18n('Not a valid filename!'));
248:                 return;
249:             }
250:             // check if the file exists already
251:             if(cFileHandler::exists($this->_filepath . '/' . $req['file'])) {
252:                 $this->displayError(i18n('A file with this name exists already'));
253:                 return;
254:             }
255:             // set the variables and create the file. Reload frames
256:             $this->_filepath = $this->_filepath . '/' . $req['file'];
257:             $this->_filename = $req['file'];
258: 
259:             cFileHandler::write($this->_filepath, '');
260: 
261:             $this->reloadFrame('left_bottom', array(
262:                     'file' => $req['file']
263:             ));
264:             $this->reloadFrame('right_top', "main.php?area=$area&frame=3&file={$req['file']}");
265:         }
266: 
267:         // save the old code and the old name
268:         $oldCode = cFileHandler::read($this->_filepath);
269:         $oldName = $this->_filename;
270: 
271:         // load the file information and update the description
272:         $fileInfos = new cApiFileInformationCollection();
273:         $fileInfos->select('filename = \'' . $this->_filename . '\'');
274:         $fileInfo = $fileInfos->next();
275:         $oldDesc = '';
276:         if($fileInfo == null) {
277:             // file information does not exist yet. Create the row
278:             $fileInfo = $fileInfos->create($dbFileType, $this->_filename, $req['description']);
279:         } else {
280:             $oldDesc = $fileInfo->get('description');
281:             if($oldDesc != $req['description']) {
282:                 $fileInfo->set('description', $req['description']);
283:             }
284:         }
285: 
286:         // rename the file
287:         if($req['file'] != $this->_filename) {
288:             // validate the file name
289:             if(!cFileHandler::validateFilename($req['file'], false)) {
290:                 $this->displayError(i18n('Not a valid filename!'));
291:             } else {
292:                 // check if a file with that name exists already
293:                 if(!cFileHandler::exists(dirname($this->_filepath) . '/' . $req['file'])) {
294:                     // rename the file and set the variables accordingly
295:                     cFileHandler::rename($this->_filepath, $req['file']);
296:                     $this->_filepath = dirname($this->_filepath) . '/' . $req['file'];
297:                     $this->_filename = $req['file'];
298: 
299:                     // update the file information
300:                     $fileInfo->set('filename', $req['file']);
301: 
302:                     // reload frames
303:                     $this->reloadFrame('left_bottom', array(
304:                             'file' => $req['file']
305:                     ));
306:                     $this->reloadFrame('right_top', "main.php?area=$area&frame=3&file={$req['file']}");
307:                 } else {
308:                     $this->displayError(i18n('Couldn\'t rename file. Does it exist already?'));
309:                     return;
310:                 }
311:             }
312:         }
313: 
314:         // if the versioning should be updated and the code changed, create a versioning instance and update it
315:         if($this->_versioning && $oldCode != $req['code']) {
316:             $fileInfoArray = $fileInfos->getFileInformation($this->_versionfilename, $dbFileType);
317:             $oVersion = new cVersionFile($fileInfo->get('idsfi'), $fileInfoArray, $req['file'], $dbFileType, $cfg, $cfgClient, $db, $client, $area, $frame, $this->_versionfilename);
318:             // Create new Layout Version in cms/version/css/ folder
319:             $oVersion->createNewVersion();
320:         }
321: 
322:         // write the code changes and display an error message or success message
323:         if(cFileHandler::write($this->_filepath, $req['code'])) {
324:             // store the file information
325:             $fileInfo->store();
326:             $this->displayOk(i18n('Changes saved successfully!'));
327:         } else {
328:             $this->displayError(i18n('Couldn\'t save the changes! Check the file system permissions.'));
329:         }
330:     }
331: 
332:     /**
333:      * Renders the page.
334:      *
335:      * @see cGuiPage::render()
336:      * @param cTemplate|null $template
337:      * @param bool $return
338:      * @throws cInvalidArgumentException
339:      */
340:     public function render($template = NULL, $return = false) {
341: 
342:         $cfg = cRegistry::getConfig();
343:         $area = cRegistry::getArea();
344:         $action = cRegistry::getAction();
345: 
346:         // load the file information
347:         $fileInfos = new cApiFileInformationCollection();
348:         $fileInfos->select('filename = \'' . $this->_filename . '\'');
349:         $fileInfo = $fileInfos->next();
350:         $desc = '';
351:         if($fileInfo != null) {
352:             $desc = $fileInfo->get('description');
353:         }
354: 
355:         // assign description
356:         $this->set('s', 'DESCRIPTION', $desc);
357: 
358:         // assign the codemirror script, and other variables
359:         $this->set('s', 'CODEMIRROR_SCRIPT', $this->_codeMirror->renderScript());
360:         $this->set('s', 'AREA', $area);
361:         $this->set('s', 'ACTION', $action);
362:         $this->set('s', 'FILENAME', $this->_filename);
363:         if(cFileHandler::readable($this->_filepath) && $this->_filename != '') {
364:             $this->set('s', 'SOURCE', conHtmlentities(cFileHandler::read($this->_filepath)));
365:         } else {
366:             $this->set('s', 'SOURCE', '');
367:         }
368:         if($this->_readOnly) {
369:             // if the read only mode is activated, display a greyed out icon
370:             $this->set('s', 'SAVE_BUTTON_IMAGE', $cfg['path']['images'] . 'but_ok_off.gif');
371:             $this->set('s', 'SAVE_BUTTON_DESC', i18n('The administratos has disabled edits'));
372:         } else {
373:             $this->set('s', 'SAVE_BUTTON_IMAGE', $cfg['path']['images'] . 'but_ok.gif');
374:             $this->set('s', 'SAVE_BUTTON_DESC', i18n('Save changes'));
375:         }
376: 
377:         // call the render method of cGuiPage
378:         parent::render();
379:     }
380: 
381: }
382: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0