index.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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($dir === $this->base) {
  116. throw new Exception('Cannot rename root');
  117. }
  118. if(preg_match('([^ a-zа-я-_0-9.]+)ui', $name) || !strlen($name)) {
  119. throw new Exception('Invalid name: ' . $name);
  120. }
  121. $new = explode(DIRECTORY_SEPARATOR, $dir);
  122. array_pop($new);
  123. array_push($new, $name);
  124. $new = implode(DIRECTORY_SEPARATOR, $new);
  125. if($dir !== $new) {
  126. if(is_file($new) || is_dir($new)) { throw new Exception('Path already exists: ' . $new); }
  127. rename($dir, $new);
  128. }
  129. return array('id' => $this->id($new));
  130. }
  131. public function remove($id) {
  132. $dir = $this->path($id);
  133. if($dir === $this->base) {
  134. throw new Exception('Cannot remove root');
  135. }
  136. if(is_dir($dir)) {
  137. foreach(array_diff(scandir($dir), array(".", "..")) as $f) {
  138. $this->remove($this->id($dir . DIRECTORY_SEPARATOR . $f));
  139. }
  140. rmdir($dir);
  141. }
  142. if(is_file($dir)) {
  143. unlink($dir);
  144. }
  145. return array('status' => 'OK');
  146. }
  147. public function move($id, $par) {
  148. $dir = $this->path($id);
  149. $par = $this->path($par);
  150. $new = explode(DIRECTORY_SEPARATOR, $dir);
  151. $new = array_pop($new);
  152. $new = $par . DIRECTORY_SEPARATOR . $new;
  153. rename($dir, $new);
  154. return array('id' => $this->id($new));
  155. }
  156. public function copy($id, $par) {
  157. $dir = $this->path($id);
  158. $par = $this->path($par);
  159. $new = explode(DIRECTORY_SEPARATOR, $dir);
  160. $new = array_pop($new);
  161. $new = $par . DIRECTORY_SEPARATOR . $new;
  162. if(is_file($new) || is_dir($new)) { throw new Exception('Path already exists: ' . $new); }
  163. if(is_dir($dir)) {
  164. mkdir($new);
  165. foreach(array_diff(scandir($dir), array(".", "..")) as $f) {
  166. $this->copy($this->id($dir . DIRECTORY_SEPARATOR . $f), $this->id($new));
  167. }
  168. }
  169. if(is_file($dir)) {
  170. copy($dir, $new);
  171. }
  172. return array('id' => $this->id($new));
  173. }
  174. }
  175. if(isset($_GET['operation'])) {
  176. $fs = new fs(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'root' . DIRECTORY_SEPARATOR);
  177. try {
  178. $rslt = null;
  179. switch($_GET['operation']) {
  180. case 'get_node':
  181. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
  182. $rslt = $fs->lst($node, (isset($_GET['id']) && $_GET['id'] === '#'));
  183. break;
  184. case "get_content":
  185. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
  186. $rslt = $fs->data($node);
  187. break;
  188. case 'create_node':
  189. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
  190. $rslt = $fs->create($node, isset($_GET['text']) ? $_GET['text'] : '', (!isset($_GET['type']) || $_GET['type'] !== 'file'));
  191. break;
  192. case 'rename_node':
  193. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
  194. $rslt = $fs->rename($node, isset($_GET['text']) ? $_GET['text'] : '');
  195. break;
  196. case 'delete_node':
  197. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
  198. $rslt = $fs->remove($node);
  199. break;
  200. case 'move_node':
  201. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
  202. $parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? $_GET['parent'] : '/';
  203. $rslt = $fs->move($node, $parn);
  204. break;
  205. case 'copy_node':
  206. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : '/';
  207. $parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? $_GET['parent'] : '/';
  208. $rslt = $fs->copy($node, $parn);
  209. break;
  210. default:
  211. throw new Exception('Unsupported operation: ' . $_GET['operation']);
  212. break;
  213. }
  214. header('Content-Type: application/json; charset=utf-8');
  215. echo json_encode($rslt);
  216. }
  217. catch (Exception $e) {
  218. header($_SERVER["SERVER_PROTOCOL"] . ' 500 Server Error');
  219. header('Status: 500 Server Error');
  220. echo $e->getMessage();
  221. }
  222. die();
  223. }
  224. ?>
  225. <!DOCTYPE html>
  226. <html>
  227. <head>
  228. <meta charset="utf-8">
  229. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  230. <title>Title</title>
  231. <meta name="viewport" content="width=device-width" />
  232. <link rel="stylesheet" href="./../../dist/themes/default/style.min.css" />
  233. <style>
  234. html, body { background:#ebebeb; font-size:10px; font-family:Verdana; margin:0; padding:0; }
  235. #container { min-width:320px; margin:0px auto 0 auto; background:white; border-radius:0px; padding:0px; overflow:hidden; }
  236. #tree { float:left; min-width:319px; border-right:1px solid silver; overflow:auto; padding:0px 0; }
  237. #data { margin-left:320px; }
  238. #data textarea { margin:0; padding:0; height:100%; width:100%; border:0; background:white; display:block; line-height:18px; resize:none; }
  239. #data, #code { font: normal normal normal 12px/18px 'Consolas', monospace !important; }
  240. #tree .folder { background:url('./file_sprite.png') right bottom no-repeat; }
  241. #tree .file { background:url('./file_sprite.png') 0 0 no-repeat; }
  242. #tree .file-pdf { background-position: -32px 0 }
  243. #tree .file-as { background-position: -36px 0 }
  244. #tree .file-c { background-position: -72px -0px }
  245. #tree .file-iso { background-position: -108px -0px }
  246. #tree .file-htm, #tree .file-html, #tree .file-xml, #tree .file-xsl { background-position: -126px -0px }
  247. #tree .file-cf { background-position: -162px -0px }
  248. #tree .file-cpp { background-position: -216px -0px }
  249. #tree .file-cs { background-position: -236px -0px }
  250. #tree .file-sql { background-position: -272px -0px }
  251. #tree .file-xls, #tree .file-xlsx { background-position: -362px -0px }
  252. #tree .file-h { background-position: -488px -0px }
  253. #tree .file-crt, #tree .file-pem, #tree .file-cer { background-position: -452px -18px }
  254. #tree .file-php { background-position: -108px -18px }
  255. #tree .file-jpg, #tree .file-jpeg, #tree .file-png, #tree .file-gif, #tree .file-bmp { background-position: -126px -18px }
  256. #tree .file-ppt, #tree .file-pptx { background-position: -144px -18px }
  257. #tree .file-rb { background-position: -180px -18px }
  258. #tree .file-text, #tree .file-txt, #tree .file-md, #tree .file-log, #tree .file-htaccess { background-position: -254px -18px }
  259. #tree .file-doc, #tree .file-docx { background-position: -362px -18px }
  260. #tree .file-zip, #tree .file-gz, #tree .file-tar, #tree .file-rar { background-position: -416px -18px }
  261. #tree .file-js { background-position: -434px -18px }
  262. #tree .file-css { background-position: -144px -0px }
  263. #tree .file-fla { background-position: -398px -0px }
  264. </style>
  265. </head>
  266. <body>
  267. <div id="container" role="main">
  268. <div id="tree"></div>
  269. <div id="data">
  270. <div class="content code" style="display:none;"><textarea id="code" readonly="readonly"></textarea></div>
  271. <div class="content folder" style="display:none;"></div>
  272. <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>
  273. <div class="content default" style="text-align:center;">Select a file from the tree.</div>
  274. </div>
  275. </div>
  276. <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  277. <script src="./../../dist/jstree.min.js"></script>
  278. <script>
  279. $(function () {
  280. $(window).resize(function () {
  281. var h = Math.max($(window).height() - 0, 420);
  282. $('#container, #data, #tree, #data .content').height(h).filter('.default').css('lineHeight', h + 'px');
  283. }).resize();
  284. $('#tree')
  285. .jstree({
  286. 'core' : {
  287. 'data' : {
  288. 'url' : '?operation=get_node',
  289. 'data' : function (node) {
  290. return { 'id' : node.id };
  291. }
  292. },
  293. 'check_callback' : function(o, n, p, i, m) {
  294. if(m && m.dnd && m.pos !== 'i') { return false; }
  295. if(o === "move_node" || o === "copy_node") {
  296. if(this.get_node(n).parent === this.get_node(p).id) { return false; }
  297. }
  298. return true;
  299. },
  300. 'force_text' : true,
  301. 'themes' : {
  302. 'responsive' : false,
  303. 'variant' : 'small',
  304. 'stripes' : true
  305. }
  306. },
  307. 'sort' : function(a, b) {
  308. 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);
  309. },
  310. 'contextmenu' : {
  311. 'items' : function(node) {
  312. var tmp = $.jstree.defaults.contextmenu.items();
  313. delete tmp.create.action;
  314. tmp.create.label = "New";
  315. tmp.create.submenu = {
  316. "create_folder" : {
  317. "separator_after" : true,
  318. "label" : "Folder",
  319. "action" : function (data) {
  320. var inst = $.jstree.reference(data.reference),
  321. obj = inst.get_node(data.reference);
  322. inst.create_node(obj, { type : "default" }, "last", function (new_node) {
  323. setTimeout(function () { inst.edit(new_node); },0);
  324. });
  325. }
  326. },
  327. "create_file" : {
  328. "label" : "File",
  329. "action" : function (data) {
  330. var inst = $.jstree.reference(data.reference),
  331. obj = inst.get_node(data.reference);
  332. inst.create_node(obj, { type : "file" }, "last", function (new_node) {
  333. setTimeout(function () { inst.edit(new_node); },0);
  334. });
  335. }
  336. }
  337. };
  338. if(this.get_type(node) === "file") {
  339. delete tmp.create;
  340. }
  341. return tmp;
  342. }
  343. },
  344. 'types' : {
  345. 'default' : { 'icon' : 'folder' },
  346. 'file' : { 'valid_children' : [], 'icon' : 'file' }
  347. },
  348. 'unique' : {
  349. 'duplicate' : function (name, counter) {
  350. return name + ' ' + counter;
  351. }
  352. },
  353. 'plugins' : ['state','dnd','sort','types','contextmenu','unique']
  354. })
  355. .on('delete_node.jstree', function (e, data) {
  356. $.get('?operation=delete_node', { 'id' : data.node.id })
  357. .fail(function () {
  358. data.instance.refresh();
  359. });
  360. })
  361. .on('create_node.jstree', function (e, data) {
  362. $.get('?operation=create_node', { 'type' : data.node.type, 'id' : data.node.parent, 'text' : data.node.text })
  363. .done(function (d) {
  364. data.instance.set_id(data.node, d.id);
  365. })
  366. .fail(function () {
  367. data.instance.refresh();
  368. });
  369. })
  370. .on('rename_node.jstree', function (e, data) {
  371. $.get('?operation=rename_node', { 'id' : data.node.id, 'text' : data.text })
  372. .done(function (d) {
  373. data.instance.set_id(data.node, d.id);
  374. })
  375. .fail(function () {
  376. data.instance.refresh();
  377. });
  378. })
  379. .on('move_node.jstree', function (e, data) {
  380. $.get('?operation=move_node', { 'id' : data.node.id, 'parent' : data.parent })
  381. .done(function (d) {
  382. //data.instance.load_node(data.parent);
  383. data.instance.refresh();
  384. })
  385. .fail(function () {
  386. data.instance.refresh();
  387. });
  388. })
  389. .on('copy_node.jstree', function (e, data) {
  390. $.get('?operation=copy_node', { 'id' : data.original.id, 'parent' : data.parent })
  391. .done(function (d) {
  392. //data.instance.load_node(data.parent);
  393. data.instance.refresh();
  394. })
  395. .fail(function () {
  396. data.instance.refresh();
  397. });
  398. })
  399. .on('changed.jstree', function (e, data) {
  400. if(data && data.selected && data.selected.length) {
  401. $.get('?operation=get_content&id=' + data.selected.join(':'), function (d) {
  402. if(d && typeof d.type !== 'undefined') {
  403. $('#data .content').hide();
  404. switch(d.type) {
  405. case 'text':
  406. case 'txt':
  407. case 'md':
  408. case 'htaccess':
  409. case 'log':
  410. case 'sql':
  411. case 'php':
  412. case 'js':
  413. case 'json':
  414. case 'css':
  415. case 'html':
  416. $('#data .code').show();
  417. $('#code').val(d.content);
  418. break;
  419. case 'png':
  420. case 'jpg':
  421. case 'jpeg':
  422. case 'bmp':
  423. case 'gif':
  424. $('#data .image img').one('load', function () { $(this).css({'marginTop':'-' + $(this).height()/2 + 'px','marginLeft':'-' + $(this).width()/2 + 'px'}); }).attr('src',d.content);
  425. $('#data .image').show();
  426. break;
  427. default:
  428. $('#data .default').html(d.content).show();
  429. break;
  430. }
  431. }
  432. });
  433. }
  434. else {
  435. $('#data .content').hide();
  436. $('#data .default').html('Select a file from the tree.').show();
  437. }
  438. });
  439. });
  440. </script>
  441. </body>
  442. </html>