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
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • 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:      * @throws cDbException
 94:      * @throws cException
 95:      * @throws cInvalidArgumentException
 96:      */
 97:     public function __construct($filename, $versioning = true, $filetype = '', $filepath = '') {
 98:         global $belang, $cfgClient;
 99: 
100:         $cfg = cRegistry::getConfig();
101:         $client = cRegistry::getClientId();
102:         $perm = cRegistry::getPerm();
103:         $area = cRegistry::getArea();
104:         $action = cRegistry::getAction();
105: 
106:         // call parent constructor
107:         parent::__construct("generic_source_editor");
108: 
109:         // check permissions
110:         if (!$perm->have_perm_area_action($area, $action)) {
111:             $this->displayCriticalError(i18n('Permission denied'));
112:         }
113: 
114:         // display empty page if no client is selected
115:         if (!(int) $client > 0) {
116:             $this->abortRendering();
117:         }
118: 
119:         // determine the filetype and path by using the area
120:         if($filetype == '') {
121:             switch($_REQUEST['area']) {
122:                 case 'style':
123:                     $filepath = $cfgClient[$client]['css']['path'] . $filename;
124:                     $filetype = 'css';
125:                     break;
126:                 case 'js':
127:                     $filepath = $cfgClient[$client]['js']['path'] . $filename;
128:                     $filetype = 'js';
129:                     break;
130:                 case 'htmltpl':
131:                     $filepath = $cfgClient[$client]['tpl']['path'] . $filename;
132:                     $filetype = 'html';
133:                     break;
134:             }
135:         }
136: 
137:         // assign variables
138:         $this->_filetype = $filetype;
139:         $this->_filepath = $filepath;
140: 
141:         $this->_readOnly = (getEffectiveSetting("client", "readonly", "false") == "true");
142:         if($this->_readOnly) {
143:             cRegistry::addWarningMessage(i18n("This area is read only! The administrator disabled edits!"));
144:         }
145: 
146:         $this->_filename = $filename;
147: 
148:         // include the class and create the codemirror instance
149:         cInclude('external', 'codemirror/class.codemirror.php');
150:         $this->_codeMirror = new CodeMirror('code', $this->_filetype, cString::getPartOfString(cString::toLowerCase($belang), 0, 2), true, $cfg, !$this->_readOnly);
151: 
152:         $this->_versioning = $versioning;
153: 
154:         // update the edited file by using the super global _REQUEST
155:         $this->update($_REQUEST);
156:     }
157: 
158:     /**
159:      * Updates the file according to the options in the array.
160:      *
161:      * @param array $req
162:      *         Request array. Usually _REQUEST
163:      *
164:      * @throws cDbException
165:      * @throws cException
166:      * @throws cInvalidArgumentException
167:      */
168:     protected function update($req) {
169:         global $cfgClient;
170: 
171:         $cfg = cRegistry::getConfig();
172:         $client = cRegistry::getClientId();
173:         $db = cRegistry::getDb();
174:         $frame = cRegistry::getFrame();
175:         $perm = cRegistry::getPerm();
176:         $area = cRegistry::getArea();
177:         $action = cRegistry::getAction();
178: 
179:         // check permissions
180:         if (!$perm->have_perm_area_action($area, $action)) {
181:             $this->displayCriticalError(i18n('Permission denied'));
182:         }
183: 
184:         // if read only is activated or no data has been sent, skip the update step
185:         if( ($this->_readOnly || ($req['status'] != 'send')) && $req['delfile'] == '') {
186:             if($req['action'] == '') {
187:                $this->abortRendering();
188:             }
189:             return;
190:         }
191: 
192:         // if magic quotes are on, strip slashes from the array
193:         if(ini_get('magic_quotes_gpc')) {
194:             foreach($req as $key => $value) {
195:                 $req[$key] = stripslashes($value);
196:             }
197:         }
198: 
199:         // determine the file type for the file information table
200:         $dbFileType = '';
201:         switch($req['area']) {
202:             case 'style':
203:                 $dbFileType = 'css';
204:                 break;
205:             case 'js':
206:                 $dbFileType = 'js';
207:                 break;
208:             case 'htmltpl':
209:                 $dbFileType = 'templates';
210:                 break;
211:         }
212: 
213:         // delete the specified file
214:         if($req['delfile'] != '') {
215:             // check if it exists
216:             if(cFileHandler::exists($this->_filepath . $req['delfile'])) {
217:                 // load information
218:                 $fileInfos = new cApiFileInformationCollection();
219:                 $fileInfos->select('filename = \'' . $req['delfile'] . '\'');
220:                 $fileInfo = $fileInfos->next();
221:                 // if there is information and if there are versioning files, delete them
222:                 if($fileInfo != null) {
223:                     $idsfi = $fileInfo->get('idsfi');
224: 
225:                     if (cSecurity::isInteger($idsfi) && is_dir($cfgClient[$client]['version']['path'] . "$dbFileType/$idsfi")) {
226:                         cDirHandler::recursiveRmdir($cfgClient[$client]['version']['path'] . "$dbFileType/$idsfi");
227:                     }
228:                 }
229: 
230:                 // remove the file
231:                 cFileHandler::remove($this->_filepath . $req['delfile']);
232: 
233:                 // remove the file information
234:                 $fileInfos->removeFileInformation(array(
235:                         'filename' => $req['delfile']
236:                 ));
237: 
238:                 // display the information and reload the frame
239:                 $this->displayOk(i18n('File deleted successfully!'));
240:                 $this->abortRendering();
241: 
242:                 $this->reloadFrame('left_bottom', array());
243:                 $this->reloadFrame('right_top', "main.php?area=$area&frame=3");
244:             }
245:             return;
246:         }
247: 
248:         // Set version filename
249:         $this->_versionfilename = $this->_filename;
250: 
251:         // if the filename is empty, display an empty editor and create a new file
252:         if(is_dir($this->_filepath) && cFileHandler::writeable($this->_filepath)) {
253:             // validate the file name
254:             if(!cFileHandler::validateFilename($req['file'], false)) {
255:                 $this->displayError(i18n('Not a valid filename!'));
256:                 return;
257:             }
258:             // check if the file exists already
259:             if(cFileHandler::exists($this->_filepath . '/' . $req['file'])) {
260:                 $this->displayError(i18n('A file with this name exists already'));
261:                 return;
262:             }
263:             // set the variables and create the file. Reload frames
264:             $this->_filepath = $this->_filepath . '/' . $req['file'];
265:             $this->_filename = $req['file'];
266: 
267:             cFileHandler::write($this->_filepath, '');
268: 
269:             $this->reloadFrame('left_bottom', array(
270:                     'file' => $req['file']
271:             ));
272:             $this->reloadFrame('right_top', "main.php?area=$area&frame=3&file={$req['file']}");
273:         }
274: 
275:         // save the old code and the old name
276:         $oldCode = cFileHandler::read($this->_filepath);
277:         $oldName = $this->_filename;
278: 
279:         // load the file information and update the description
280:         $fileInfos = new cApiFileInformationCollection();
281:         $fileInfos->select('filename = \'' . $this->_filename . '\'');
282:         $fileInfo = $fileInfos->next();
283:         $oldDesc = '';
284:         if($fileInfo == null) {
285:             // file information does not exist yet. Create the row
286:             $fileInfo = $fileInfos->create($dbFileType, $this->_filename, $req['description']);
287:         } else {
288:             $oldDesc = $fileInfo->get('description');
289:             if($oldDesc != $req['description']) {
290:                 $fileInfo->set('description', $req['description']);
291:             }
292:         }
293: 
294:         // rename the file
295:         if($req['file'] != $this->_filename) {
296:             // validate the file name
297:             if(!cFileHandler::validateFilename($req['file'], false)) {
298:                 $this->displayError(i18n('Not a valid filename!'));
299:             } else {
300:                 // check if a file with that name exists already
301:                 if(!cFileHandler::exists(dirname($this->_filepath) . '/' . $req['file'])) {
302:                     // rename the file and set the variables accordingly
303:                     cFileHandler::rename($this->_filepath, $req['file']);
304:                     $this->_filepath = dirname($this->_filepath) . '/' . $req['file'];
305:                     $this->_filename = $req['file'];
306: 
307:                     // update the file information
308:                     $fileInfo->set('filename', $req['file']);
309: 
310:                     // reload frames
311:                     $this->reloadFrame('left_bottom', array(
312:                             'file' => $req['file']
313:                     ));
314:                     $this->reloadFrame('right_top', "main.php?area=$area&frame=3&file={$req['file']}");
315:                 } else {
316:                     $this->displayError(i18n('Couldn\'t rename file. Does it exist already?'));
317:                     return;
318:                 }
319:             }
320:         }
321: 
322:         // if the versioning should be updated and the code changed, create a versioning instance and update it
323:         if($this->_versioning && $oldCode != $req['code']) {
324:             $fileInfoArray = $fileInfos->getFileInformation($this->_versionfilename, $dbFileType);
325:             $oVersion = new cVersionFile($fileInfo->get('idsfi'), $fileInfoArray, $req['file'], $dbFileType, $cfg, $cfgClient, $db, $client, $area, $frame, $this->_versionfilename);
326:             // Create new Layout Version in cms/version/css/ folder
327:             $oVersion->createNewVersion();
328:         }
329: 
330:         // write the code changes and display an error message or success message
331:         if(cFileHandler::write($this->_filepath, $req['code'])) {
332:             // store the file information
333:             $fileInfo->store();
334:             $this->displayOk(i18n('Changes saved successfully!'));
335:         } else {
336:             $this->displayError(i18n('Couldn\'t save the changes! Check the file system permissions.'));
337:         }
338:     }
339: 
340:     /**
341:      * Renders the page.
342:      *
343:      * @see cGuiPage::render()
344:      * @param cTemplate|null $template
345:      * @param bool           $return
346:      * @throws cDbException
347:      * @throws cException
348:      * @throws cInvalidArgumentException
349:      */
350:     public function render($template = NULL, $return = false) {
351: 
352:         $cfg = cRegistry::getConfig();
353:         $area = cRegistry::getArea();
354:         $action = cRegistry::getAction();
355: 
356:         // load the file information
357:         $fileInfos = new cApiFileInformationCollection();
358:         $fileInfos->select('filename = \'' . $this->_filename . '\'');
359:         $fileInfo = $fileInfos->next();
360:         $desc = '';
361:         if($fileInfo != null) {
362:             $desc = $fileInfo->get('description');
363:         }
364: 
365:         // assign description
366:         $this->set('s', 'DESCRIPTION', $desc);
367: 
368:         // assign the codemirror script, and other variables
369:         $this->set('s', 'CODEMIRROR_SCRIPT', $this->_codeMirror->renderScript());
370:         $this->set('s', 'AREA', $area);
371:         $this->set('s', 'ACTION', $action);
372:         $this->set('s', 'FILENAME', $this->_filename);
373:         if(cFileHandler::readable($this->_filepath) && $this->_filename != '') {
374:             $this->set('s', 'SOURCE', conHtmlentities(cFileHandler::read($this->_filepath)));
375:         } else {
376:             $this->set('s', 'SOURCE', '');
377:         }
378:         if($this->_readOnly) {
379:             // if the read only mode is activated, display a greyed out icon
380:             $this->set('s', 'SAVE_BUTTON_IMAGE', $cfg['path']['images'] . 'but_ok_off.gif');
381:             $this->set('s', 'SAVE_BUTTON_DESC', i18n('The administratos has disabled edits'));
382:         } else {
383:             $this->set('s', 'SAVE_BUTTON_IMAGE', $cfg['path']['images'] . 'but_ok.gif');
384:             $this->set('s', 'SAVE_BUTTON_DESC', i18n('Save changes'));
385:         }
386: 
387:         // call the render method of cGuiPage
388:         parent::render();
389:     }
390: 
391: }
392: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0