index.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $return_var = v_exec('v-copy-fs-file', [$user, $fn, $path]);
  40. if ($return_var != 0) {
  41. print('<p style="color: white">Error while saving file</p>');
  42. exit;
  43. }
  44. }
  45. unlink($fn);
  46. }
  47. }
  48. $return_var = v_exec('v-open-fs-file', [$user, $path], false, $content);
  49. if ($return_var != 0) {
  50. print 'Error while opening file'; // todo: handle this more styled
  51. exit;
  52. }
  53. $content = $content . "\n";
  54. } else {
  55. $content = '';
  56. }
  57. ?>
  58. <form id="edit-file-form" method="post">
  59. <!-- input id="do-backup" type="button" onClick="javascript:void(0);" name="save" value="backup (ctrl+F2)" class="backup" / -->
  60. <input type="submit" name="save" value="Save" class="save" />
  61. <textarea name="contents" class="editor" id="editor" rows="4" style="display:none;width: 100%; height: 100%;"><?php echo $content ?></textarea>
  62. </form>
  63. <script type="text/javascript" src="/js/hotkeys.js"></script>
  64. <script type="text/javascript">
  65. $('.editor').ace({ theme: 'twilight', lang: 'ruby' });
  66. var dcrt = $('#editor').data('ace');
  67. dcrt.editor.ace.getSession().setNewLineMode('unix');
  68. var aceInstance = dcrt.editor.ace;
  69. aceInstance.gotoLine(0);
  70. aceInstance.focus();
  71. var makeBackup = function() {
  72. var params = {
  73. action: 'backup',
  74. path: '<?= $path ?>'
  75. };
  76. $.ajax({url: "/file_manager/fm_api.php",
  77. method: "POST",
  78. data: params,
  79. dataType: 'JSON',
  80. success: function(reply) {
  81. var fadeTimeout = 3000;
  82. if (reply.result) {
  83. $('#message').text('File backed up as ' + reply.filename);
  84. clearTimeout(window.msg_tmt);
  85. $('#message').show();
  86. window.msg_tmt = setTimeout(function() {$('#message').fadeOut();}, fadeTimeout);
  87. }
  88. else {
  89. $('#error-message').text(reply.message);
  90. clearTimeout(window.errmsg_tmt);
  91. $('#error-message').show();
  92. window.errmsg_tmt = setTimeout(function() {$('#error-message').fadeOut();}, fadeTimeout);
  93. }
  94. }
  95. });
  96. }
  97. $('#do-backup').on('click', function(evt) {
  98. evt.preventDefault();
  99. makeBackup();
  100. });
  101. //
  102. // Shortcuts
  103. //
  104. shortcut.add("Ctrl+s",function() {
  105. var inp = $('<input>').attr({'type': 'hidden', 'name': 'save'}).val('Save');
  106. $('#edit-file-form').append(inp);
  107. $('#edit-file-form').submit();
  108. },{
  109. 'type': 'keydown',
  110. 'propagate': false,
  111. 'disable_in_input': false,
  112. 'target': document
  113. });
  114. shortcut.add("Ctrl+F2",function() {
  115. makeBackup();
  116. },{
  117. 'type': 'keydown',
  118. 'propagate': false,
  119. 'disable_in_input': false,
  120. 'target': document
  121. });
  122. </script>