index.php 4.8 KB

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