dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxEditor.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxEditor;
3
4
5
6class dbxEditor {
7
8 public function run() {
9
10 $modul = dbx()->get_system_var('dbx_modul');
11 $run1 = dbx()->get_modul_var('dbx_run1','dbx');
12 $file = dbx()->get_modul_var('file','none','*');
14 $ajax = dbx()->get_system_var('dbx_ajax',0,'int');
15 $xedit = dbx()->get_system_var('dbx_edit',0,'int');
16 dbx()->set_system_var('dbx_edit',0);
17
18 $content='';
19
20 switch ($run1) {
21
22 case 'tree':
23 $content = $this->render_tree($base);
24 break;
25
26 case 'edit':
27
28 // 🔒 Sicherheit
29 if (!$this->is_valid_file($file)) {
30 return "invalid file";
31 }
32
33 $full = $base.$file;
34
35 if (!file_exists($full)) {
36 return "file not found ($file)";
37 }
38
39 $txt = file_get_contents($full);
40 $data['file']=$file;
41 $data['txt'] = dbx()->norep(htmlspecialchars($txt, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'));
42
43 $oTPL = dbx()->get_system_obj('dbxTPL');
44 $content = $oTPL->get_tpl('dbxEditor|editor', $data);
45
46 break;
47
48
49 case 'save':
50
51 if (!$this->is_valid_file($file)) {
52 header('Content-Type: application/json');
53 echo json_encode(['ok'=>0]);
54 exit;
55 }
56
57 $full = $base.$file;
58 $data = $_POST['content'] ?? '';
59
60 file_put_contents($full, $data);
61 $this->clear_dd_file_cache($file);
62
63 header('Content-Type: application/json');
64 echo json_encode(['ok'=>1]);
65 exit;
66
67 case 'delete':
68 $content="delete ($base) ($file) ajax=($ajax)";
69 dbx()->debug($content);
70 header('Content-Type: application/json');
71
72 if (!$this->is_valid_file($file)) {
73 echo json_encode(['ok'=>0, 'msg'=>'invalid file']);
74 exit;
75 }
76
77 $full = $base.$file;
78
79 if (!file_exists($full)) {
80 echo json_encode(['ok'=>0, 'msg'=>'file not found']);
81 exit;
82 }
83
84 if (!unlink($full)) {
85 echo json_encode(['ok'=>0, 'msg'=>'delete failed']);
86 exit;
87 }
88
89 $this->clear_dd_file_cache($file);
90 echo json_encode(['ok'=>1]);
91 exit;
92
93
94 break;
95
96 case 'rename':
97
98 header('Content-Type: application/json');
99
100 $old = dbx()->get_modul_var('old','','alphanum+/');
101 $new = dbx()->get_modul_var('new','','alphanum+/');
102
103 if (!$this->is_valid_file($old) || !$this->is_valid_file($new)) {
104 echo json_encode(['ok'=>0, 'msg'=>'invalid file']);
105 exit;
106 }
107
108 $fullOld = $base.$old;
109 $fullNew = $base.$new;
110
111 if (!file_exists($fullOld)) {
112 echo json_encode(['ok'=>0, 'msg'=>'source not found']);
113 exit;
114 }
115
116 if (file_exists($fullNew)) {
117 echo json_encode(['ok'=>0, 'msg'=>'target exists']);
118 exit;
119 }
120
121 if (!rename($fullOld, $fullNew)) {
122 echo json_encode(['ok'=>0, 'msg'=>'rename failed']);
123 exit;
124 }
125 $this->clear_dd_file_cache($old);
126 $this->clear_dd_file_cache($new);
127 echo json_encode(['ok'=>1]);
128 exit;
129
130 break;
131
132 case 'copy':
133
134 header('Content-Type: application/json');
135
136 $old = dbx()->get_modul_var('old','','alphanum+/.');
137 $new = dbx()->get_modul_var('new','','alphanum+/.');
138
139 if (!$this->is_valid_file($old) || !$this->is_valid_file($new)) {
140 echo json_encode(['ok'=>0, 'msg'=>'invalid file']);
141 exit;
142 }
143
144 $fullOld = $base . $old;
145 $fullNew = $base . $new;
146
147 if (!file_exists($fullOld)) {
148 echo json_encode(['ok'=>0, 'msg'=>'source not found']);
149 exit;
150 }
151
152 if (file_exists($fullNew)) {
153 echo json_encode(['ok'=>0, 'msg'=>'target exists']);
154 exit;
155 }
156
157 if (!copy($fullOld, $fullNew)) {
158 echo json_encode(['ok'=>0, 'msg'=>'copy failed']);
159 exit;
160 }
161
162 $this->clear_dd_file_cache($new);
163 echo json_encode(['ok'=>1]);
164 exit;
165
166 break;
167
168
169 default:
170 $oTPL=dbx()->get_system_obj('dbxTPL');
171 $msg['msg']="Modul=($modul) Action=($run1) is undef.";
172 $content=$oTPL->get_tpl('dbx|alert-warning',$msg);
173 } // switch
174 dbx()->set_system_var('dbx_edit',$xedit);
175 return $content;
176 }
177
178 private function clear_dd_file_cache($file) {
179 $file = str_replace('\\', '/', (string)$file);
180
181 if (preg_match('#(?:^|/)dbx/modules/([^/]+)/dd/([^/]+)\.dd\.php$#', $file, $match)) {
182 $oDD = dbx()->get_system_obj('dbxDD');
183 $oDD->clear_dd_cache($match[1] . '|' . $match[2]);
184 }
185 }
186
187 private function is_valid_file($file) {
188 if (strpos($file,'..') !== false) return false;
189
190 $ext = pathinfo($file, PATHINFO_EXTENSION);
191 return in_array($ext, ['css','htm','js','php']);
192 }
193
194 private function render_tree($dir, $base = null) {
195
196 if ($base === null) $base = $dir;
197
198 $html = '<ul>';
199
200 $items = scandir($dir);
201
202 foreach ($items as $item) {
203
204 if ($item === '.' || $item === '..') continue;
205
206 $full = $dir . '/' . $item;
207 $rel = str_replace($base . '/', '', $full);
208
209 if (is_dir($full)) {
210
211 $html .= '<li>';
212 $html .= '<span>' . $item . '</span>';
213 $html .= $this->render_tree($full, $base);
214 $html .= '</li>';
215
216 } else {
217
218 if (!$this->is_valid_file($item)) continue;
219
220 $html .= '<li>';
221 $html .= '<a href="?dbx_modul=dbxEditor&dbx_run1=edit&file=' . urlencode($rel) . '">';
222 $html .= $item;
223 $html .= '</a>';
224 $html .= '</li>';
225 }
226 }
227
228 $html .= '</ul>';
229
230 return $html;
231 }
232
233
234
235
236}
exit
Definition index.php:532
dbx_get_base_dir($cut_Data=0)
Definition index.php:157
DBX schema administration.