file_save.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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-2025
  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. echo "access denied";
  28. exit;
  29. }
  30. //add multi-lingual support
  31. $language = new text;
  32. $text = $language->get();
  33. //compare the tokens
  34. $key_name = '/app/edit/'.$_POST['mode'];
  35. $hash = hash_hmac('sha256', $key_name, $_SESSION['keys'][$key_name]);
  36. if (!hash_equals($hash, $_POST['token'])) {
  37. echo "access denied";
  38. exit;
  39. }
  40. //get the directory
  41. if (!isset($_SESSION)) { session_start(); }
  42. switch ($_SESSION["app"]["edit"]["dir"]) {
  43. case 'scripts':
  44. $edit_directory = $settings->get('switch','scripts');
  45. break;
  46. case 'php':
  47. $edit_directory = $_SERVER["DOCUMENT_ROOT"].'/'.PROJECT_PATH;
  48. break;
  49. case 'grammar':
  50. $edit_directory = $settings->get('switch','grammar');
  51. break;
  52. case 'provision':
  53. switch (PHP_OS) {
  54. case "Linux":
  55. if (file_exists('/usr/share/fusionpbx/templates/provision')) {
  56. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  57. }
  58. elseif (file_exists('/etc/fusionpbx/resources/templates/provision')) {
  59. $edit_directory = '/etc/fusionpbx/resources/templates/provision';
  60. }
  61. else {
  62. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  63. }
  64. break;
  65. case "FreeBSD":
  66. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  67. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  68. }
  69. elseif (file_exists('/usr/local/etc/fusionpbx/resources/templates/provision')) {
  70. $edit_directory = '/usr/local/etc/fusionpbx/resources/templates/provision';
  71. }
  72. else {
  73. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  74. }
  75. break;
  76. case "NetBSD":
  77. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  78. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  79. }
  80. else {
  81. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  82. }
  83. break;
  84. case "OpenBSD":
  85. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  86. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  87. }
  88. else {
  89. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  90. }
  91. break;
  92. default:
  93. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision/";
  94. }
  95. break;
  96. case 'xml':
  97. $edit_directory = $settings->get('switch','conf');
  98. break;
  99. }
  100. if (!isset($edit_directory) && is_array($_SESSION['editor']['path'])) {
  101. foreach ($_SESSION['editor']['path'] as $path) {
  102. if ($_SESSION["app"]["edit"]["dir"] == $path) {
  103. $edit_directory = $path;
  104. break;
  105. }
  106. }
  107. }
  108. //set the file variable
  109. $file_path = $_POST["filepath"];
  110. //remove attempts to change the directory
  111. $file_path = str_replace('..', '', $file_path);
  112. $file_path = str_replace ("\\", "/", $file_path);
  113. //break the path into an array
  114. $path_array = pathinfo($file_path);
  115. $path_prefix = substr($path_array['dirname'], 0, strlen($edit_directory));
  116. //validate the path
  117. if (realpath($path_prefix) == realpath($edit_directory)) {
  118. if ($file_path != '') {
  119. try {
  120. //save file content
  121. $file_path = realpath($file_path);
  122. $file_path = str_replace ('//', '/', $file_path);
  123. $file_path = str_replace ("\\", "/", $file_path);
  124. if (file_exists($file_path)) {
  125. //create a file handle
  126. $handle = fopen($file_path, 'wb');
  127. if (!$handle) {
  128. throw new Exception('Write Failed - Check File Owner & Permissions');
  129. }
  130. //build a array of the content
  131. $lines = explode("\n", str_replace ("\r\n", "\n", $_POST["content"]));
  132. $file_content = '';
  133. //remove trailing spaces on each line
  134. foreach ($lines as $line) {
  135. $file_content .= rtrim($line) . "\n";
  136. }
  137. //save the file with single empty line
  138. fwrite($handle, rtrim($file_content) . "\n");
  139. //close the file handle
  140. fclose($handle);
  141. }
  142. //set the reload_xml value to true
  143. $_SESSION["reload_xml"] = true;
  144. //alert user of success
  145. echo "Changes Saved";
  146. }
  147. catch(Exception $e) {
  148. //alert error
  149. echo $e->getMessage();
  150. }
  151. }
  152. }