1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cGuiSourceEditor extends cGuiPage {
24:
25: 26: 27: 28: 29:
30: protected $_filename;
31:
32: 33: 34: 35: 36: 37: 38: 39:
40: protected $_versionfilename;
41:
42: 43: 44: 45: 46:
47: protected $_filepath;
48:
49: 50: 51: 52: 53:
54: protected $_filetype;
55:
56: 57: 58: 59: 60:
61: protected $_codeMirror;
62:
63: 64: 65: 66: 67:
68: protected $_readOnly;
69:
70: 71: 72: 73: 74:
75: protected $_versioning;
76:
77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 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:
107: parent::__construct("generic_source_editor");
108:
109:
110: if (!$perm->have_perm_area_action($area, $action)) {
111: $this->displayCriticalError(i18n('Permission denied'));
112: }
113:
114:
115: if (!(int) $client > 0) {
116: $this->abortRendering();
117: }
118:
119:
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:
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:
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:
155: $this->update($_REQUEST);
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165: 166: 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:
180: if (!$perm->have_perm_area_action($area, $action)) {
181: $this->displayCriticalError(i18n('Permission denied'));
182: }
183:
184:
185: if( ($this->_readOnly || ($req['status'] != 'send')) && $req['delfile'] == '') {
186: if($req['action'] == '') {
187: $this->abortRendering();
188: }
189: return;
190: }
191:
192:
193: if(ini_get('magic_quotes_gpc')) {
194: foreach($req as $key => $value) {
195: $req[$key] = stripslashes($value);
196: }
197: }
198:
199:
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:
214: if($req['delfile'] != '') {
215:
216: if(cFileHandler::exists($this->_filepath . $req['delfile'])) {
217:
218: $fileInfos = new cApiFileInformationCollection();
219: $fileInfos->select('filename = \'' . $req['delfile'] . '\'');
220: $fileInfo = $fileInfos->next();
221:
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:
231: cFileHandler::remove($this->_filepath . $req['delfile']);
232:
233:
234: $fileInfos->removeFileInformation(array(
235: 'filename' => $req['delfile']
236: ));
237:
238:
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:
249: $this->_versionfilename = $this->_filename;
250:
251:
252: if(is_dir($this->_filepath) && cFileHandler::writeable($this->_filepath)) {
253:
254: if(!cFileHandler::validateFilename($req['file'], false)) {
255: $this->displayError(i18n('Not a valid filename!'));
256: return;
257: }
258:
259: if(cFileHandler::exists($this->_filepath . '/' . $req['file'])) {
260: $this->displayError(i18n('A file with this name exists already'));
261: return;
262: }
263:
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:
276: $oldCode = cFileHandler::read($this->_filepath);
277: $oldName = $this->_filename;
278:
279:
280: $fileInfos = new cApiFileInformationCollection();
281: $fileInfos->select('filename = \'' . $this->_filename . '\'');
282: $fileInfo = $fileInfos->next();
283: $oldDesc = '';
284: if($fileInfo == null) {
285:
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:
295: if($req['file'] != $this->_filename) {
296:
297: if(!cFileHandler::validateFilename($req['file'], false)) {
298: $this->displayError(i18n('Not a valid filename!'));
299: } else {
300:
301: if(!cFileHandler::exists(dirname($this->_filepath) . '/' . $req['file'])) {
302:
303: cFileHandler::rename($this->_filepath, $req['file']);
304: $this->_filepath = dirname($this->_filepath) . '/' . $req['file'];
305: $this->_filename = $req['file'];
306:
307:
308: $fileInfo->set('filename', $req['file']);
309:
310:
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:
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:
327: $oVersion->createNewVersion();
328: }
329:
330:
331: if(cFileHandler::write($this->_filepath, $req['code'])) {
332:
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: 342: 343: 344: 345: 346: 347: 348: 349:
350: public function render($template = NULL, $return = false) {
351:
352: $cfg = cRegistry::getConfig();
353: $area = cRegistry::getArea();
354: $action = cRegistry::getAction();
355:
356:
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:
366: $this->set('s', 'DESCRIPTION', $desc);
367:
368:
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:
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:
388: parent::render();
389: }
390:
391: }
392: