index.php 15KB

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