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