index.php 6.0 KB

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