index.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. session_start();
  3. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  4. $user = $_SESSION['user'];
  5. /*
  6. if (empty($panel)) {
  7. $command = VESTA_CMD."v-list-user '".$user."' 'json'";
  8. exec ($command, $output, $return_var);
  9. if ( $return_var > 0 ) {
  10. header("Location: /error/");
  11. exit;
  12. }
  13. $panel = json_decode(implode('', $output), true);
  14. }
  15. */
  16. /*
  17. // Check user session
  18. if ((!isset($_SESSION['user'])) && (!defined('NO_AUTH_REQUIRED'))) {
  19. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  20. header("Location: /login/");
  21. exit;
  22. }
  23. */
  24. ?>
  25. <title>Edit file <?= htmlspecialchars($_REQUEST['path']) ?></title>
  26. <meta charset="utf-8" />
  27. <link href="/css/file_manager_editor.css" type="text/css" rel="stylesheet">
  28. <script src="/js/cheef-editor/jquery/jquery-1.8.3.min.js"></script>
  29. <script src="/js/cheef-editor/ace/ace.js"></script>
  30. <script src="/js/cheef-editor/ace/theme-twilight.js"></script>
  31. <script src="/js/cheef-editor/ace/mode-ruby.js"></script>
  32. <script src="/js/cheef-editor/jquery-ace.min.js"></script>
  33. <div id="message" style="display:none; position: absoulte;background-color: green; color: white; padding: 10px;"></div>
  34. <div id="error-message" style="display:none; position: absoulte;background-color: red; color: white; padding: 10px;"></div>
  35. <?php
  36. if (!empty($_REQUEST['path'])) {
  37. $content = '';
  38. $path = $_REQUEST['path'];
  39. if (is_readable($path)) {
  40. $image = getimagesize($path) ? true : false;
  41. if ($image) {
  42. header('Location: /view/file/?path='.$path);
  43. exit;
  44. }
  45. if (!empty($_POST['save'])) {
  46. $fn = tempnam ('/tmp', 'vst-save-file-');
  47. if ($fn) {
  48. $f = fopen ($fn, 'w+');
  49. fwrite($f, $_POST['contents']);
  50. fclose($f);
  51. chmod($fn, 0644);
  52. if ($f) {
  53. //copy($fn, $path);
  54. exec (VESTA_CMD . "v-copy-fs-file {$user} {$fn} {$path}", $output, $return_var);
  55. $error = check_return_code($return_var, $output);
  56. if ($return_var != 0) {
  57. /*var_dump(VESTA_CMD . "v-copy-fs-file {$user} {$fn} {$path}");
  58. var_dump($path);
  59. var_dump($output);*/
  60. die('<p style="color: white">Error while saving file</p>');//echo '0';
  61. }
  62. }
  63. unlink($fn);
  64. }
  65. }
  66. // $content = file_get_contents($path);
  67. // v-open-fs-file
  68. //print file_get_contents($path);
  69. exec (VESTA_CMD . "v-check-fs-permission {$user} {$path}", $content, $return_var);
  70. if ($return_var != 0) {
  71. print 'Error while opening file'; // todo: handle this more styled
  72. exit;
  73. }
  74. /*exec (VESTA_CMD . "v-open-fs-file {$user} {$path}", $content, $return_var);
  75. if ($return_var != 0) {
  76. print 'Error while opening file'; // todo: handle this more styled
  77. exit;
  78. }
  79. $content = implode("\n", $content);*/
  80. $content = file_get_contents($path);
  81. }
  82. }
  83. else {
  84. $content = '';
  85. }
  86. ?>
  87. <form id="edit-file-form" method="post">
  88. <!-- input id="do-backup" type="button" onClick="javascript:void(0);" name="save" value="backup (ctrl+F2)" class="backup" / -->
  89. <input type="submit" name="save" value="Save" class="save" />
  90. <textarea name="contents" class="editor" id="editor" rows="4" style="display:none;width: 100%; height: 100%;"><?php echo $content ?></textarea>
  91. </form>
  92. <script type="text/javascript" src="/js/hotkeys.js"></script>
  93. <script type="text/javascript">
  94. $('.editor').ace({ theme: 'twilight', lang: 'ruby' });
  95. var dcrt = $('#editor').data('ace');
  96. var editor = dcrt.editor.ace;
  97. editor.gotoLine(0);
  98. editor.focus();
  99. var makeBackup = function() {
  100. var params = {
  101. action: 'backup',
  102. path: '<?= $path ?>'
  103. };
  104. $.ajax({url: "/file_manager/fm_api.php",
  105. method: "POST",
  106. data: params,
  107. dataType: 'JSON',
  108. success: function(reply) {
  109. var fadeTimeout = 3000;
  110. if (reply.result) {
  111. $('#message').text('File backed up as ' + reply.filename);
  112. clearTimeout(window.msg_tmt);
  113. $('#message').show();
  114. window.msg_tmt = setTimeout(function() {$('#message').fadeOut();}, fadeTimeout);
  115. }
  116. else {
  117. $('#error-message').text(reply.message);
  118. clearTimeout(window.errmsg_tmt);
  119. $('#error-message').show();
  120. window.errmsg_tmt = setTimeout(function() {$('#error-message').fadeOut();}, fadeTimeout);
  121. }
  122. }
  123. });
  124. }
  125. $('#do-backup').on('click', function(evt) {
  126. evt.preventDefault();
  127. makeBackup();
  128. });
  129. //
  130. // Shortcuts
  131. //
  132. shortcut.add("Ctrl+s",function() {
  133. var inp = $('<input>').attr({'type': 'hidden', 'name': 'save'}).val('Save');
  134. $('#edit-file-form').append(inp);
  135. $('#edit-file-form').submit();
  136. },{
  137. 'type': 'keydown',
  138. 'propagate': false,
  139. 'disable_in_input': false,
  140. 'target': document
  141. });
  142. shortcut.add("Ctrl+F2",function() {
  143. makeBackup();
  144. },{
  145. 'type': 'keydown',
  146. 'propagate': false,
  147. 'disable_in_input': false,
  148. 'target': document
  149. });
  150. </script>