1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: 20: 21: 22: 23:
24: class cGuiSourceEditor extends cGuiPage {
25:
26: 27: 28: 29:
30: protected $filename;
31:
32: 33: 34: 35:
36: protected $filepath;
37:
38: 39: 40: 41:
42: protected $filetype;
43:
44: 45: 46: 47:
48: protected $codeMirror;
49:
50: 51: 52: 53:
54: protected $readOnly;
55:
56: 57: 58: 59:
60: protected $versioning;
61:
62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74:
75: public function __construct($filename, $versioning = true, $filetype = '', $filepath = '') {
76: global $cfg, $cfgClient, $client, $perm, $area, $action, $belang;
77:
78:
79: parent::__construct("generic_source_editor");
80:
81:
82: if (!$perm->have_perm_area_action($area, $action)) {
83: $this->displayCriticalError(i18n('Permission denied'));
84: }
85:
86:
87: if (!(int) $client > 0) {
88: $this->abortRendering();
89: }
90:
91:
92: if($filetype == '') {
93: switch($_REQUEST['area']) {
94: case 'style':
95: $filepath = $cfgClient[$client]['css']['path'] . $filename;
96: $filetype = 'css';
97: break;
98: case 'js':
99: $filepath = $cfgClient[$client]['js']['path'] . $filename;
100: $filetype = 'js';
101: break;
102: case 'htmltpl':
103: $filepath = $cfgClient[$client]['tpl']['path'] . $filename;
104: $filetype = 'html';
105: break;
106: }
107: }
108:
109:
110: $this->filetype = $filetype;
111: $this->filepath = $filepath;
112:
113: $this->readOnly = (getEffectiveSetting("client", "readonly", "false") == "true");
114: if($this->readOnly) {
115: cRegistry::addWarningMessage(i18n("This area is read only! The administrator disabled edits!"));
116: }
117:
118: $this->filename = $filename;
119:
120:
121: cInclude('external', 'codemirror/class.codemirror.php');
122: $this->codeMirror = new CodeMirror('code', $this->filetype, substr(strtolower($belang), 0, 2), true, $cfg, !$this->readOnly);
123:
124: $this->versioning = $versioning;
125:
126:
127: $this->update($_REQUEST);
128: }
129:
130: 131: 132: 133: 134: 135:
136: protected function update($req) {
137: global $cfg, $cfgClient, $db, $client, $area, $frame, $perm, $action;
138:
139:
140: if (!$perm->have_perm_area_action($area, $action)) {
141: $this->displayCriticalError(i18n('Permission denied'));
142: }
143:
144:
145: if( ($this->readOnly || ($req['status'] != 'send')) && $req['delfile'] == '') {
146: if($req['action'] == '') {
147: $this->abortRendering();
148: }
149: return;
150: }
151:
152:
153: if(ini_get('magic_quotes_gpc')) {
154: foreach($req as $key => $value) {
155: $req[$key] = stripslashes($value);
156: }
157: }
158:
159:
160: $dbFileType = '';
161: switch($req['area']) {
162: case 'style':
163: $dbFileType = 'css';
164: break;
165: case 'js':
166: $dbFileType = 'js';
167: break;
168: case 'htmltpl':
169: $dbFileType = 'templates';
170: break;
171: }
172:
173:
174: if($req['delfile'] != '') {
175:
176: if(cFileHandler::exists($this->filepath . $req['delfile'])) {
177:
178: $fileInfos = new cApiFileInformationCollection();
179: $fileInfos->select('filename = \'' . $req['delfile'] . '\'');
180: $fileInfo = $fileInfos->next();
181:
182: if($fileInfo != null) {
183: $idsfi = $fileInfo->get('idsfi');
184:
185: if (cSecurity::isInteger($idsfi) && is_dir($cfgClient[$client]['version']['path'] . "$dbFileType/$idsfi")) {
186: cDirHandler::recursiveRmdir($cfgClient[$client]['version']['path'] . "$dbFileType/$idsfi");
187: }
188: }
189:
190:
191: cFileHandler::remove($this->filepath . $req['delfile']);
192:
193:
194: $fileInfos->removeFileInformation(array(
195: 'filename' => $req['delfile']
196: ));
197:
198:
199: $this->displayOk(i18n('File deleted successfully!'));
200: $this->abortRendering();
201:
202: $this->reloadFrame('left_bottom', array());
203: $this->reloadFrame('right_top', "main.php?area=$area&frame=3");
204: }
205: return;
206: }
207:
208:
209: if(is_dir($this->filepath) && cFileHandler::writeable($this->filepath)) {
210:
211: if(!cFileHandler::validateFilename($req['file'], false)) {
212: $this->displayError(i18n('Not a valid filename!'));
213: return;
214: }
215:
216: if(cFileHandler::exists($this->filepath . '/' . $req['file'])) {
217: $this->displayError(i18n('A file with this name exists already'));
218: return;
219: }
220:
221: $this->filepath = $this->filepath . '/' . $req['file'];
222: $this->filename = $req['file'];
223:
224: cFileHandler::write($this->filepath, '');
225:
226: $this->reloadFrame('left_bottom', array(
227: 'file' => $req['file']
228: ));
229: $this->reloadFrame('right_top', "main.php?area=$area&frame=3&file={$req['file']}");
230: }
231:
232:
233: $oldCode = cFileHandler::read($this->filepath);
234: $oldName = $this->filename;
235:
236:
237: $fileInfos = new cApiFileInformationCollection();
238: $fileInfos->select('filename = \'' . $this->filename . '\'');
239: $fileInfo = $fileInfos->next();
240: $oldDesc = '';
241: if($fileInfo == null) {
242:
243: $fileInfo = $fileInfos->create($dbFileType, $this->filename, $req['description']);
244: } else {
245: $oldDesc = $fileInfo->get('description');
246: if($oldDesc != $req['description']) {
247: $fileInfo->set('description', $req['description']);
248: }
249: }
250:
251:
252: if($req['file'] != $this->filename) {
253:
254: if(!cFileHandler::validateFilename($req['file'], false)) {
255: $this->displayError(i18n('Not a valid filename!'));
256: } else {
257:
258: if(!cFileHandler::exists(dirname($this->filepath) . '/' . $req['file'])) {
259:
260: cFileHandler::rename($this->filepath, $req['file']);
261: $this->filepath = dirname($this->filepath) . '/' . $req['file'];
262: $this->filename = $req['file'];
263:
264:
265: $fileInfo->set('filename', $req['file']);
266:
267:
268: $this->reloadFrame('left_bottom', array(
269: 'file' => $req['file']
270: ));
271: $this->reloadFrame('right_top', "main.php?area=$area&frame=3&file={$req['file']}");
272: } else {
273: $this->displayError(i18n('Couldn\'t rename file. Does it exist already?'));
274: return;
275: }
276: }
277: }
278:
279:
280: if($this->versioning && $oldCode != $req['code']) {
281: $fileInfoArray = $fileInfos->getFileInformation($this->filename, $dbFileType);
282: $oVersion = new cVersionFile($fileInfo->get('idsfi'), $fileInfoArray, $req['file'], $dbFileType, $cfg, $cfgClient, $db, $client, $area, $frame, $this->filename);
283:
284: $oVersion->createNewVersion();
285: }
286:
287:
288: if(cFileHandler::write($this->filepath, $req['code'])) {
289:
290: $fileInfo->store();
291: $this->displayOk(i18n('Changes saved successfully!'));
292: } else {
293: $this->displayError(i18n('Couldn\'t save the changes! Check the file system permissions.'));
294: }
295: }
296:
297: 298: 299: 300:
301: public function render() {
302: global $area, $action, $cfg;
303:
304:
305: $fileInfos = new cApiFileInformationCollection();
306: $fileInfos->select('filename = \'' . $this->filename . '\'');
307: $fileInfo = $fileInfos->next();
308: $desc = '';
309: if($fileInfo != null) {
310: $desc = $fileInfo->get('description');
311: }
312:
313:
314: $this->set('s', 'DESCRIPTION', $desc);
315:
316:
317: $this->set('s', 'CODEMIRROR_SCRIPT', $this->codeMirror->renderScript());
318: $this->set('s', 'AREA', $area);
319: $this->set('s', 'ACTION', $action);
320: $this->set('s', 'FILENAME', $this->filename);
321: if(cFileHandler::readable($this->filepath) && $this->filename != '') {
322: $this->set('s', 'SOURCE', conHtmlentities(cFileHandler::read($this->filepath)));
323: } else {
324: $this->set('s', 'SOURCE', '');
325: }
326: if($this->readOnly) {
327:
328: $this->set('s', 'SAVE_BUTTON_IMAGE', $cfg['path']['images'] . 'but_ok_off.gif');
329: $this->set('s', 'SAVE_BUTTON_DESC', i18n('The administratos has disabled edits'));
330: } else {
331: $this->set('s', 'SAVE_BUTTON_IMAGE', $cfg['path']['images'] . 'but_ok.gif');
332: $this->set('s', 'SAVE_BUTTON_DESC', i18n('Save changes'));
333: }
334:
335:
336: parent::render();
337: }
338: }
339: