root.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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) 2018
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. // make sure the PATH_SEPARATOR is defined
  22. umask(2);
  23. if (!defined("PATH_SEPARATOR")) {
  24. if (strpos($_ENV["OS"], "Win") !== false) {
  25. define("PATH_SEPARATOR", ";");
  26. } else {
  27. define("PATH_SEPARATOR", ":");
  28. }
  29. }
  30. if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html';
  31. // make sure the document_root is set
  32. $_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]);
  33. if(PHP_SAPI == 'cli'){
  34. chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME));
  35. $script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]);
  36. $dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME));
  37. if (file_exists('/project_root.php')) {
  38. $path = '/';
  39. } else {
  40. $i = 1;
  41. $path = '';
  42. while ($i < count($dirs)) {
  43. $path .= '/' . $dirs[$i];
  44. if (file_exists($path. '/project_root.php')) {
  45. break;
  46. }
  47. $i++;
  48. }
  49. }
  50. $_SERVER["DOCUMENT_ROOT"] = $path;
  51. }else{
  52. $_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]);
  53. }
  54. $_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]);
  55. // try to detect if a project path is being used
  56. if (!defined('PROJECT_PATH')) {
  57. if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) {
  58. define('PROJECT_PATH', '/fusionpbx');
  59. } elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) {
  60. define('PROJECT_PATH', '');
  61. } else {
  62. $dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME)));
  63. $i = 1;
  64. $path = $_SERVER["DOCUMENT_ROOT"];
  65. while ($i < count($dirs)) {
  66. $path .= '/' . $dirs[$i];
  67. if (file_exists($path. '/project_root.php')) {
  68. break;
  69. }
  70. $i++;
  71. }
  72. if(!file_exists($path. '/project_root.php')){
  73. die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance");
  74. }
  75. $project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path);
  76. define('PROJECT_PATH', $project_path);
  77. }
  78. $_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH);
  79. set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]);
  80. }
  81. ?>