auto_loader.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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-2021
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. class auto_loader {
  22. public function __construct() {
  23. spl_autoload_register(array($this, 'loader'));
  24. }
  25. private function loader($class_name) {
  26. //set the default value
  27. $class_found = false;
  28. //sanitize the class name
  29. $class_name = preg_replace('[^a-zA-Z0-9_]', '', $class_name);
  30. //save the log to the syslog server
  31. if (isset($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
  32. openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
  33. }
  34. //find the most relevant class name
  35. if (!$class_found && file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/classes/".$class_name.".php")) {
  36. //first priority
  37. $path = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/classes/".$class_name.".php";
  38. $class_found = true;
  39. if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
  40. syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$path.", line: ".__line__);
  41. }
  42. include $path;
  43. }
  44. elseif (!$class_found && file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/core/".$class_name."/resources/classes/".$class_name.".php")) {
  45. //second priority
  46. $path = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/core/".$class_name."/resources/classes/".$class_name.".php";
  47. $class_found = true;
  48. if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
  49. syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$path.", line: ".__line__);
  50. }
  51. include $path;
  52. }
  53. elseif (!$class_found && file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/app/".$class_name."/resources/classes/".$class_name.".php")) {
  54. //third priority
  55. $path = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/app/".$class_name."/resources/classes/".$class_name.".php";
  56. $class_found = true;
  57. if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
  58. syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$path.", line: ".__line__);
  59. }
  60. include $path;
  61. }
  62. //use glob for a more exensive search for the classes (note: GLOB_BRACE doesn't work on some systems)
  63. if (!$class_found && !class_exists($class_name)) {
  64. //fourth priority
  65. $results_1 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/resources/classes/".$class_name.".php");
  66. $results_2 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/classes/".$class_name.".php");
  67. $results = array_merge((array)$results_1,(array)$results_2);
  68. unset($results_1, $results_2);
  69. foreach ($results as &$class_file) {
  70. if (!$class_found) {
  71. $class_found = true;
  72. if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
  73. syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$class_file.", line: ".__line__);
  74. }
  75. include $class_file;
  76. break;
  77. }
  78. }
  79. unset($results);
  80. }
  81. //save the log to the syslog server
  82. if (isset($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
  83. closelog();
  84. }
  85. }
  86. }
  87. ?>