index.php 4.3 KB

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