file_save.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. //remove trailing spaces
  136. $file_content = '';
  137. foreach ($lines as $line) {
  138. $file_content .= rtrim($line) . "\n";
  139. }
  140. //save the file
  141. fwrite($handle, $file_content);
  142. //close the file handle
  143. fclose($handle);
  144. }
  145. //set the reload_xml value to true
  146. $_SESSION["reload_xml"] = true;
  147. //alert user of success
  148. echo "Changes Saved";
  149. }
  150. catch(Exception $e) {
  151. //alert error
  152. echo $e->getMessage();
  153. }
  154. }
  155. }