Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
875 lars 1
<?php
2
ini_set('open_basedir', dirname(__FILE__) . DIRECTORY_SEPARATOR);
3
 
4
class fs
5
{
6
	protected $base = null;
7
 
8
	protected function real($path) {
9
		$temp = realpath($path);
10
		if(!$temp) { throw new Exception('Path does not exist: ' . $path); }
11
		if($this->base && strlen($this->base)) {
12
			if(strpos($temp, $this->base) !== 0) { throw new Exception('Path is not inside base ('.$this->base.'): ' . $temp); }
13
		}
14
		return $temp;
15
	}
16
	protected function path($id) {
17
		$id = str_replace('/', DIRECTORY_SEPARATOR, $id);
18
		$id = trim($id, DIRECTORY_SEPARATOR);
19
		$id = $this->real($this->base . DIRECTORY_SEPARATOR . $id);
20
		return $id;
21
	}
22
	protected function id($path) {
23
		$path = $this->real($path);
24
		$path = substr($path, strlen($this->base));
25
		$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
26
		$path = trim($path, '/');
27
		return strlen($path) ? $path : '/';
28
	}
29
 
30
	public function __construct($base) {
31
		$this->base = $this->real($base);
32
		if(!$this->base) { throw new Exception('Base directory does not exist'); }
33
	}
34
	public function lst($id, $with_root = false) {
35
		$dir = $this->path($id);
36
		$lst = @scandir($dir);
37
		if(!$lst) { throw new Exception('Could not list path: ' . $dir); }
38
		$res = array();
39
		foreach($lst as $item) {
40
			if($item == '.' || $item == '..' || $item === null) { continue; }
41
			$tmp = preg_match('([^ a-zа-я-_0-9.]+)ui', $item);
42
			if($tmp === false || $tmp === 1) { continue; }
43
			if(is_dir($dir . DIRECTORY_SEPARATOR . $item)) {
44
				$res[] = array('text' => $item, 'children' => true,  'id' => $this->id($dir . DIRECTORY_SEPARATOR . $item), 'icon' => 'folder');
45
			}
46
			else {
47
				$res[] = array('text' => $item, 'children' => false, 'id' => $this->id($dir . DIRECTORY_SEPARATOR . $item), 'type' => 'file', 'icon' => 'file file-'.substr($item, strrpos($item,'.') + 1));
48
			}
49
		}
50
		if($with_root && $this->id($dir) === '/') {
51
			$res = array(array('text' => basename($this->base), 'children' => $res, 'id' => '/', 'icon'=>'folder', 'state' => array('opened' => true, 'disabled' => true)));
52
		}
53
		return $res;
54
	}
55
	public function data($id) {
56
		if(strpos($id, ":")) {
57
			$id = array_map(array($this, 'id'), explode(':', $id));
58
			return array('type'=>'multiple', 'content'=> 'Multiple selected: ' . implode(' ', $id));
59
		}
60
		$dir = $this->path($id);
61
		if(is_dir($dir)) {
62
			return array('type'=>'folder', 'content'=> $id);
63
		}
64
		if(is_file($dir)) {
65
			$ext = strpos($dir, '.') !== FALSE ? substr($dir, strrpos($dir, '.') + 1) : '';
66
			$dat = array('type' => $ext, 'content' => '');
67
			switch($ext) {
68
				case 'txt':
69
				case 'text':
70
				case 'md':
71
				case 'js':
72
				case 'json':
73
				case 'css':
74
				case 'html':
75
				case 'htm':
76
				case 'xml':
77
				case 'c':
78
				case 'cpp':
79
				case 'h':
80
				case 'sql':
81
				case 'log':
82
				case 'py':
83
				case 'rb':
84
				case 'htaccess':
85
				case 'php':
86
					$dat['content'] = file_get_contents($dir);
87
					break;
88
				case 'jpg':
89
				case 'jpeg':
90
				case 'gif':
91
				case 'png':
92
				case 'bmp':
93
					$dat['content'] = 'data:'.finfo_file(finfo_open(FILEINFO_MIME_TYPE), $dir).';base64,'.base64_encode(file_get_contents($dir));
94
					break;
95
				default:
96
					$dat['content'] = 'File not recognized: '.$this->id($dir);
97
					break;
98
			}
99
			return $dat;
100
		}
101
		throw new Exception('Not a valid selection: ' . $dir);
102
	}
103
	public function create($id, $name, $mkdir = false) {
104
		$dir = $this->path($id);
105
		if(preg_match('([^ a-zа-я-_0-9.]+)ui', $name) || !strlen($name)) {
106
			throw new Exception('Invalid name: ' . $name);
107
		}
108
		if($mkdir) {
109
			mkdir($dir . DIRECTORY_SEPARATOR . $name);
110
		}
111
		else {
112
			file_put_contents($dir . DIRECTORY_SEPARATOR . $name, '');
113
		}
114
		return array('id' => $this->id($dir . DIRECTORY_SEPARATOR . $name));
115
	}
116
	public function rename($id, $name) {
117
		$dir = $this->path($id);
118
		if($dir === $this->base) {
119
			throw new Exception('Cannot rename root');
120
		}
121
		if(preg_match('([^ a-zа-я-_0-9.]+)ui', $name) || !strlen($name)) {
122
			throw new Exception('Invalid name: ' . $name);
123
		}
124
		$new = explode(DIRECTORY_SEPARATOR, $dir);
125
		array_pop($new);
126
		array_push($new, $name);
127
		$new = implode(DIRECTORY_SEPARATOR, $new);
128
		if($dir !== $new) {
129
			if(is_file($new) || is_dir($new)) { throw new Exception('Path already exists: ' . $new); }
130
			rename($dir, $new);
131
		}
132
		return array('id' => $this->id($new));
133
	}
134
	public function remove($id) {
135
		$dir = $this->path($id);
136
		if($dir === $this->base) {
137
			throw new Exception('Cannot remove root');
138
		}
139
		if(is_dir($dir)) {
140
			foreach(array_diff(scandir($dir), array(".", "..")) as $f) {
141
				$this->remove($this->id($dir . DIRECTORY_SEPARATOR . $f));
142
			}
143
			rmdir($dir);
144
		}
145
		if(is_file($dir)) {
146
			unlink($dir);
147
		}
148
		return array('status' => 'OK');
149
	}
150
	public function move($id, $par) {
151
		$dir = $this->path($id);
152
		$par = $this->path($par);
153
		$new = explode(DIRECTORY_SEPARATOR, $dir);
154
		$new = array_pop($new);
155
		$new = $par . DIRECTORY_SEPARATOR . $new;
156
		rename($dir, $new);
157
		return array('id' => $this->id($new));
158
	}
159
	public function copy($id, $par) {
160
		$dir = $this->path($id);
161
		$par = $this->path($par);
162
		$new = explode(DIRECTORY_SEPARATOR, $dir);
163
		$new = array_pop($new);
164
		$new = $par . DIRECTORY_SEPARATOR . $new;
165
		if(is_file($new) || is_dir($new)) { throw new Exception('Path already exists: ' . $new); }
166
 
167
		if(is_dir($dir)) {
168
			mkdir($new);
169
			foreach(array_diff(scandir($dir), array(".", "..")) as $f) {
170
				$this->copy($this->id($dir . DIRECTORY_SEPARATOR . $f), $this->id($new));
171
			}
172
		}
173
		if(is_file($dir)) {
174
			copy($dir, $new);
175
		}
176
		return array('id' => $this->id($new));
177
	}
178
}
179
 
180
if(isset($_GET['operation'])) {
181
	$fs = new fs(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'root' . DIRECTORY_SEPARATOR);
182
	try {
183
		$rslt = null;
184
		switch($_GET['operation']) {
185
			case 'get_node':
186
				$node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
187
				$rslt = $fs->lst($node, (isset($_GET['id']) && $_GET['id'] === '#'));
188
				break;
189
			case "get_content":
190
				$node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
191
				$rslt = $fs->data($node);
192
				break;
193
			case 'create_node':
194
				$node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
195
				$rslt = $fs->create($node, isset($_GET['text']) ? $_GET['text'] : '', (!isset($_GET['type']) || $_GET['type'] !== 'file'));
196
				break;
197
			case 'rename_node':
198
				$node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
199
				$rslt = $fs->rename($node, isset($_GET['text']) ? $_GET['text'] : '');
200
				break;
201
			case 'delete_node':
202
				$node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
203
				$rslt = $fs->remove($node);
204
				break;
205
			case 'move_node':
206
				$node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
207
				$parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? $_GET['parent'] : '/';
208
				$rslt = $fs->move($node, $parn);
209
				break;
210
			case 'copy_node':
211
				$node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
212
				$parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? $_GET['parent'] : '/';
213
				$rslt = $fs->copy($node, $parn);
214
				break;
215
			default:
216
				throw new Exception('Unsupported operation: ' . $_GET['operation']);
217
				break;
218
		}
219
		header('Content-Type: application/json; charset=utf-8');
220
		echo json_encode($rslt);
221
	}
222
	catch (Exception $e) {
223
		header($_SERVER["SERVER_PROTOCOL"] . ' 500 Server Error');
224
		header('Status:  500 Server Error');
225
		echo $e->getMessage();
226
	}
227
	die();
228
}
229
?>
230
<!DOCTYPE html>
231
<html>
232
	<head>
233
		<meta charset="utf-8">
234
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
235
		<title>Title</title>
236
		<meta name="viewport" content="width=device-width" />
237
		<link rel="stylesheet" href="./../../dist/themes/default/style.min.css" />
238
		<style>
239
		html, body { background:#ebebeb; font-size:10px; font-family:Verdana; margin:0; padding:0; }
240
		#container { min-width:320px; margin:0px auto 0 auto; background:white; border-radius:0px; padding:0px; overflow:hidden; }
241
		#tree { float:left; min-width:319px; border-right:1px solid silver; overflow:auto; padding:0px 0; }
242
		#data { margin-left:320px; }
243
		#data textarea { margin:0; padding:0; height:100%; width:100%; border:0; background:white; display:block; line-height:18px; resize:none; }
244
		#data, #code { font: normal normal normal 12px/18px 'Consolas', monospace !important; }
245
 
246
		#tree .folder { background:url('./file_sprite.png') right bottom no-repeat; }
247
		#tree .file { background:url('./file_sprite.png') 0 0 no-repeat; }
248
		#tree .file-pdf { background-position: -32px 0 }
249
		#tree .file-as { background-position: -36px 0 }
250
		#tree .file-c { background-position: -72px -0px }
251
		#tree .file-iso { background-position: -108px -0px }
252
		#tree .file-htm, #tree .file-html, #tree .file-xml, #tree .file-xsl { background-position: -126px -0px }
253
		#tree .file-cf { background-position: -162px -0px }
254
		#tree .file-cpp { background-position: -216px -0px }
255
		#tree .file-cs { background-position: -236px -0px }
256
		#tree .file-sql { background-position: -272px -0px }
257
		#tree .file-xls, #tree .file-xlsx { background-position: -362px -0px }
258
		#tree .file-h { background-position: -488px -0px }
259
		#tree .file-crt, #tree .file-pem, #tree .file-cer { background-position: -452px -18px }
260
		#tree .file-php { background-position: -108px -18px }
261
		#tree .file-jpg, #tree .file-jpeg, #tree .file-png, #tree .file-gif, #tree .file-bmp { background-position: -126px -18px }
262
		#tree .file-ppt, #tree .file-pptx { background-position: -144px -18px }
263
		#tree .file-rb { background-position: -180px -18px }
264
		#tree .file-text, #tree .file-txt, #tree .file-md, #tree .file-log, #tree .file-htaccess { background-position: -254px -18px }
265
		#tree .file-doc, #tree .file-docx { background-position: -362px -18px }
266
		#tree .file-zip, #tree .file-gz, #tree .file-tar, #tree .file-rar { background-position: -416px -18px }
267
		#tree .file-js { background-position: -434px -18px }
268
		#tree .file-css { background-position: -144px -0px }
269
		#tree .file-fla { background-position: -398px -0px }
270
		</style>
271
	</head>
272
	<body>
273
		<div id="container" role="main">
274
			<div id="tree"></div>
275
			<div id="data">
276
				<div class="content code" style="display:none;"><textarea id="code" readonly="readonly"></textarea></div>
277
				<div class="content folder" style="display:none;"></div>
278
				<div class="content image" style="display:none; position:relative;"><img src="" alt="" style="display:block; position:absolute; left:50%; top:50%; padding:0; max-height:90%; max-width:90%;" /></div>
279
				<div class="content default" style="text-align:center;">Select a file from the tree.</div>
280
			</div>
281
		</div>
282
 
283
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
284
		<script src="./../../dist/jstree.min.js"></script>
285
		<script>
286
		$(function () {
287
			$(window).resize(function () {
288
				var h = Math.max($(window).height() - 0, 420);
289
				$('#container, #data, #tree, #data .content').height(h).filter('.default').css('lineHeight', h + 'px');
290
			}).resize();
291
 
292
			$('#tree')
293
				.jstree({
294
					'core' : {
295
						'data' : {
296
							'url' : '?operation=get_node',
297
							'data' : function (node) {
298
								return { 'id' : node.id };
299
							}
300
						},
301
						'check_callback' : function(o, n, p, i, m) {
302
							if(m && m.dnd && m.pos !== 'i') { return false; }
303
							if(o === "move_node" || o === "copy_node") {
304
								if(this.get_node(n).parent === this.get_node(p).id) { return false; }
305
							}
306
							return true;
307
						},
308
						'force_text' : true,
309
						'themes' : {
310
							'responsive' : false,
311
							'variant' : 'small',
312
							'stripes' : true
313
						}
314
					},
315
					'sort' : function(a, b) {
316
						return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : (this.get_type(a) >= this.get_type(b) ? 1 : -1);
317
					},
318
					'contextmenu' : {
319
						'items' : function(node) {
320
							var tmp = $.jstree.defaults.contextmenu.items();
321
							delete tmp.create.action;
322
							tmp.create.label = "New";
323
							tmp.create.submenu = {
324
								"create_folder" : {
325
									"separator_after"	: true,
326
									"label"				: "Folder",
327
									"action"			: function (data) {
328
										var inst = $.jstree.reference(data.reference),
329
											obj = inst.get_node(data.reference);
330
										inst.create_node(obj, { type : "default" }, "last", function (new_node) {
331
											setTimeout(function () { inst.edit(new_node); },0);
332
										});
333
									}
334
								},
335
								"create_file" : {
336
									"label"				: "File",
337
									"action"			: function (data) {
338
										var inst = $.jstree.reference(data.reference),
339
											obj = inst.get_node(data.reference);
340
										inst.create_node(obj, { type : "file" }, "last", function (new_node) {
341
											setTimeout(function () { inst.edit(new_node); },0);
342
										});
343
									}
344
								}
345
							};
346
							if(this.get_type(node) === "file") {
347
								delete tmp.create;
348
							}
349
							return tmp;
350
						}
351
					},
352
					'types' : {
353
						'default' : { 'icon' : 'folder' },
354
						'file' : { 'valid_children' : [], 'icon' : 'file' }
355
					},
356
					'unique' : {
357
						'duplicate' : function (name, counter) {
358
							return name + ' ' + counter;
359
						}
360
					},
361
					'plugins' : ['state','dnd','sort','types','contextmenu','unique']
362
				})
363
				.on('delete_node.jstree', function (e, data) {
364
					$.get('?operation=delete_node', { 'id' : data.node.id })
365
						.fail(function () {
366
							data.instance.refresh();
367
						});
368
				})
369
				.on('create_node.jstree', function (e, data) {
370
					$.get('?operation=create_node', { 'type' : data.node.type, 'id' : data.node.parent, 'text' : data.node.text })
371
						.done(function (d) {
372
							data.instance.set_id(data.node, d.id);
373
						})
374
						.fail(function () {
375
							data.instance.refresh();
376
						});
377
				})
378
				.on('rename_node.jstree', function (e, data) {
379
					$.get('?operation=rename_node', { 'id' : data.node.id, 'text' : data.text })
380
						.done(function (d) {
381
							data.instance.set_id(data.node, d.id);
382
						})
383
						.fail(function () {
384
							data.instance.refresh();
385
						});
386
				})
387
				.on('move_node.jstree', function (e, data) {
388
					$.get('?operation=move_node', { 'id' : data.node.id, 'parent' : data.parent })
389
						.done(function (d) {
390
							//data.instance.load_node(data.parent);
391
							data.instance.refresh();
392
						})
393
						.fail(function () {
394
							data.instance.refresh();
395
						});
396
				})
397
				.on('copy_node.jstree', function (e, data) {
398
					$.get('?operation=copy_node', { 'id' : data.original.id, 'parent' : data.parent })
399
						.done(function (d) {
400
							//data.instance.load_node(data.parent);
401
							data.instance.refresh();
402
						})
403
						.fail(function () {
404
							data.instance.refresh();
405
						});
406
				})
407
				.on('changed.jstree', function (e, data) {
408
					if(data && data.selected && data.selected.length) {
409
						$.get('?operation=get_content&id=' + data.selected.join(':'), function (d) {
410
							if(d && typeof d.type !== 'undefined') {
411
								$('#data .content').hide();
412
								switch(d.type) {
413
									case 'text':
414
									case 'txt':
415
									case 'md':
416
									case 'htaccess':
417
									case 'log':
418
									case 'sql':
419
									case 'php':
420
									case 'js':
421
									case 'json':
422
									case 'css':
423
									case 'html':
424
										$('#data .code').show();
425
										$('#code').val(d.content);
426
										break;
427
									case 'png':
428
									case 'jpg':
429
									case 'jpeg':
430
									case 'bmp':
431
									case 'gif':
432
										$('#data .image img').one('load', function () { $(this).css({'marginTop':'-' + $(this).height()/2 + 'px','marginLeft':'-' + $(this).width()/2 + 'px'}); }).attr('src',d.content);
433
										$('#data .image').show();
434
										break;
435
									default:
436
										$('#data .default').html(d.content).show();
437
										break;
438
								}
439
							}
440
						});
441
					}
442
					else {
443
						$('#data .content').hide();
444
						$('#data .default').html('Select a file from the tree.').show();
445
					}
446
				});
447
		});
448
		</script>
449
	</body>
450
</html>