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: 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:
103: parent::__construct("generic_source_editor");
104:
105:
106: if (!$perm->have_perm_area_action($area, $action)) {
107: $this->displayCriticalError(i18n('Permission denied'));
108: }
109:
110:
111: if (!(int) $client > 0) {
112: $this->abortRendering();
113: }
114:
115:
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:
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:
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:
151: $this->update($_REQUEST);
152: }
153:
154: 155: 156: 157: 158: 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:
172: if (!$perm->have_perm_area_action($area, $action)) {
173: $this->displayCriticalError(i18n('Permission denied'));
174: }
175:
176:
177: if( ($this->_readOnly || ($req['status'] != 'send')) && $req['delfile'] == '') {
178: if($req['action'] == '') {
179: $this->abortRendering();
180: }
181: return;
182: }
183:
184:
185: if(ini_get('magic_quotes_gpc')) {
186: foreach($req as $key => $value) {
187: $req[$key] = stripslashes($value);
188: }
189: }
190:
191:
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:
206: if($req['delfile'] != '') {
207:
208: if(cFileHandler::exists($this->_filepath . $req['delfile'])) {
209:
210: $fileInfos = new cApiFileInformationCollection();
211: $fileInfos->select('filename = \'' . $req['delfile'] . '\'');
212: $fileInfo = $fileInfos->next();
213:
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:
223: cFileHandler::remove($this->_filepath . $req['delfile']);
224:
225:
226: $fileInfos->removeFileInformation(array(
227: 'filename' => $req['delfile']
228: ));
229:
230:
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:
241: $this->_versionfilename = $this->_filename;
242:
243:
244: if(is_dir($this->_filepath) && cFileHandler::writeable($this->_filepath)) {
245:
246: if(!cFileHandler::validateFilename($req['file'], false)) {
247: $this->displayError(i18n('Not a valid filename!'));
248: return;
249: }
250:
251: if(cFileHandler::exists($this->_filepath . '/' . $req['file'])) {
252: $this->displayError(i18n('A file with this name exists already'));
253: return;
254: }
255:
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:
268: $oldCode = cFileHandler::read($this->_filepath);
269: $oldName = $this->_filename;
270:
271:
272: $fileInfos = new cApiFileInformationCollection();
273: $fileInfos->select('filename = \'' . $this->_filename . '\'');
274: $fileInfo = $fileInfos->next();
275: $oldDesc = '';
276: if($fileInfo == null) {
277:
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:
287: if($req['file'] != $this->_filename) {
288:
289: if(!cFileHandler::validateFilename($req['file'], false)) {
290: $this->displayError(i18n('Not a valid filename!'));
291: } else {
292:
293: if(!cFileHandler::exists(dirname($this->_filepath) . '/' . $req['file'])) {
294:
295: cFileHandler::rename($this->_filepath, $req['file']);
296: $this->_filepath = dirname($this->_filepath) . '/' . $req['file'];
297: $this->_filename = $req['file'];
298:
299:
300: $fileInfo->set('filename', $req['file']);
301:
302:
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:
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:
319: $oVersion->createNewVersion();
320: }
321:
322:
323: if(cFileHandler::write($this->_filepath, $req['code'])) {
324:
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: 334: 335: 336: 337: 338: 339:
340: public function render($template = NULL, $return = false) {
341:
342: $cfg = cRegistry::getConfig();
343: $area = cRegistry::getArea();
344: $action = cRegistry::getAction();
345:
346:
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:
356: $this->set('s', 'DESCRIPTION', $desc);
357:
358:
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:
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:
378: parent::render();
379: }
380:
381: }
382: