file_read.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. //check permissions
  25. if (permission_exists('edit_view')) {
  26. //access granted
  27. }
  28. else {
  29. echo "access denied";
  30. exit;
  31. }
  32. //get the directory
  33. if (!isset($_SESSION)) { session_start(); }
  34. switch ($_SESSION["app"]["edit"]["dir"]) {
  35. case 'scripts':
  36. $edit_directory = $_SESSION['switch']['scripts']['dir'];
  37. break;
  38. case 'php':
  39. $edit_directory = $_SERVER["DOCUMENT_ROOT"].'/'.PROJECT_PATH;
  40. break;
  41. case 'grammar':
  42. $edit_directory = $_SESSION['switch']['grammar']['dir'];
  43. break;
  44. case 'provision':
  45. switch (PHP_OS) {
  46. case "Linux":
  47. if (file_exists('/usr/share/fusionpbx/templates/provision')) {
  48. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  49. }
  50. elseif (file_exists('/etc/fusionpbx/resources/templates/provision')) {
  51. $edit_directory = '/etc/fusionpbx/resources/templates/provision';
  52. }
  53. else {
  54. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  55. }
  56. break;
  57. case "FreeBSD":
  58. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  59. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  60. }
  61. elseif (file_exists('/usr/local/etc/fusionpbx/resources/templates/provision')) {
  62. $edit_directory = '/usr/local/etc/fusionpbx/resources/templates/provision';
  63. }
  64. else {
  65. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  66. }
  67. break;
  68. case "NetBSD":
  69. if (file_exists('/usr/local/share/fusionpbx/templates/provision')) {
  70. $edit_directory = '/usr/share/fusionpbx/templates/provision';
  71. }
  72. else {
  73. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
  74. }
  75. break;
  76. case "OpenBSD":
  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. default:
  85. $edit_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision/";
  86. }
  87. break;
  88. case 'xml':
  89. $edit_directory = $_SESSION['switch']['conf']['dir'];
  90. break;
  91. }
  92. if (!isset($edit_directory) && is_array($_SESSION['editor']['path'])) {
  93. foreach ($_SESSION['editor']['path'] as $path) {
  94. if ($_SESSION["app"]["edit"]["dir"] == $path) {
  95. $edit_directory = $path;
  96. break;
  97. }
  98. }
  99. }
  100. //set the file variable
  101. $file_name = $_POST["file"];
  102. //remove attempts to change the directory
  103. $file_name = str_replace('..', '', $file_name);
  104. $file_name = str_replace ("\\", "/", $file_name);
  105. //break the path into an array
  106. $path_array = pathinfo($file_name);
  107. $path_prefix = substr($path_array['dirname'], 0, strlen($edit_directory));
  108. //validate the path
  109. if (realpath($path_prefix) == realpath($edit_directory)) {
  110. //clear the output
  111. ob_clean();
  112. //send the content of the file
  113. echo file_get_contents($file_name);
  114. //get the contents of the file
  115. /*
  116. $handle = fopen($file_name, "r");
  117. if ($handle) {
  118. while (!feof($handle)) {
  119. $buffer = fgets($handle, 4096);
  120. echo $buffer;
  121. }
  122. fclose($handle);
  123. }
  124. */
  125. }