file_save.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2023
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. James Rose <[email protected]>
  21. */
  22. //includes files
  23. require_once dirname(__DIR__, 2) . "/resources/require.php";
  24. require_once "resources/check_auth.php";
  25. //check permissions
  26. if (permission_exists('edit_save')) {
  27. //access granted
  28. }
  29. else {
  30. echo "access denied";
  31. exit;
  32. }
  33. //add multi-lingual support
  34. $language = new text;
  35. $text = $language->get();
  36. //compare the tokens
  37. $key_name = '/app/edit/'.$_POST['mode'];
  38. $hash = hash_hmac('sha256', $key_name, $_SESSION['keys'][$key_name]);
  39. if (!hash_equals($hash, $_POST['token'])) {
  40. echo "access denied";
  41. exit;
  42. }
  43. //get the directory
  44. if (!isset($_SESSION)) { session_start(); }
  45. switch ($_SESSION["app"]["edit"]["dir"]) {
  46. case 'scripts':
  47. $edit_directory = $_SESSION['switch']['scripts']['dir'];
  48. break;
  49. case 'php':
  50. $edit_directory = $_SERVER["DOCUMENT_ROOT"].'/'.PROJECT_PATH;
  51. break;
  52. case 'grammar':
  53. $edit_directory = $_SESSION['switch']['grammar']['dir'];
  54. break;
  55. case 'provision':
  56. switch (PHP_OS) {
  57. case "Linux":
  58. if (file_exists('/usr/share/fusionpbx/templates/provision')) {
  59. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  60. }
  61. elseif (file_exists('/etc/fusionpbx/resources/templates/provision')) {
  62. $edit_directory = '/etc/fusionpbx/resources/templates/provision';
  63. }
  64. else {
  65. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  66. }
  67. break;
  68. case "FreeBSD":
  69. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  70. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  71. }
  72. elseif (file_exists('/usr/local/etc/fusionpbx/resources/templates/provision')) {
  73. $edit_directory = '/usr/local/etc/fusionpbx/resources/templates/provision';
  74. }
  75. else {
  76. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  77. }
  78. break;
  79. case "NetBSD":
  80. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  81. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  82. }
  83. else {
  84. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  85. }
  86. break;
  87. case "OpenBSD":
  88. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  89. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  90. }
  91. else {
  92. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  93. }
  94. break;
  95. default:
  96. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision/";
  97. }
  98. break;
  99. case 'xml':
  100. $edit_directory = $_SESSION['switch']['conf']['dir'];
  101. break;
  102. }
  103. if (!isset($edit_directory) && is_array($_SESSION['editor']['path'])) {
  104. foreach ($_SESSION['editor']['path'] as $path) {
  105. if ($_SESSION["app"]["edit"]["dir"] == $path) {
  106. $edit_directory = $path;
  107. break;
  108. }
  109. }
  110. }
  111. //set the file variable
  112. $file_path = $_POST["filepath"];
  113. //remove attempts to change the directory
  114. $file_path = str_replace('..', '', $file_path);
  115. $file_path = str_replace ("\\", "/", $file_path);
  116. //break the path into an array
  117. $path_array = pathinfo($file_path);
  118. $path_prefix = substr($path_array['dirname'], 0, strlen($edit_directory));
  119. //validate the path
  120. if (realpath($path_prefix) == realpath($edit_directory)) {
  121. if ($file_path != '') {
  122. try {
  123. //save file content
  124. $file_path = realpath($file_path);
  125. $file_path = str_replace ('//', '/', $file_path);
  126. $file_path = str_replace ("\\", "/", $file_path);
  127. if (file_exists($file_path)) {
  128. //create a file handle
  129. $handle = fopen($file_path, 'wb');
  130. if (!$handle) {
  131. throw new Exception('Write Failed - Check File Owner & Permissions');
  132. }
  133. //build a array of the content
  134. $lines = explode("\n", str_replace ("\r\n", "\n", $_POST["content"]));
  135. //go in reverse order to remove blank trailing lines at the end of the file
  136. for ($i = count($lines) - 1; $i > 0; $i--) {
  137. //find the last empty line
  138. if (!empty($lines[$i])) {
  139. //exit for loop
  140. break;
  141. }
  142. //remember the last line index
  143. $last_line = $i;
  144. }
  145. $file_content = '';
  146. //remove trailing spaces on each line
  147. for ($i = 0; $i < $last_line; $i++) {
  148. $file_content .= rtrim($lines[$i]) . "\n";
  149. }
  150. //save the file
  151. fwrite($handle, $file_content);
  152. //close the file handle
  153. fclose($handle);
  154. }
  155. //set the reload_xml value to true
  156. $_SESSION["reload_xml"] = true;
  157. //alert user of success
  158. echo "Changes Saved";
  159. }
  160. catch(Exception $e) {
  161. //alert error
  162. echo $e->getMessage();
  163. }
  164. }
  165. }