index.php 4.8 KB

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