auto_loader.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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-2024
  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. public static function autoload_search($array) : string {
  26. if (!is_array($array) && count($path) != 0) {
  27. return '';
  28. }
  29. foreach($array as $path) {
  30. if (is_array($path) && count($path) != 0) {
  31. foreach($path as $sub_path) {
  32. if (!empty($sub_path) && file_exists($sub_path)) {
  33. return $sub_path;
  34. }
  35. }
  36. }
  37. elseif (!empty($path) && file_exists($path)) {
  38. return $path;
  39. }
  40. }
  41. return '';
  42. }
  43. private function loader($class_name) : bool {
  44. //sanitize the class name
  45. $class_name = preg_replace('[^a-zA-Z0-9_]', '', $class_name);
  46. //use glob for a more extensive search for the classes (note: GLOB_BRACE doesn't work on some systems)
  47. if (!class_exists($class_name)) {
  48. //set project path using magic dir constant
  49. $project_path = dirname(__DIR__, 2);
  50. //build the search path array
  51. $search_path[] = glob($project_path . "/resources/classes/".$class_name.".php");
  52. $search_path[] = glob($project_path . "/resources/interfaces/".$class_name.".php");
  53. $search_path[] = glob($project_path . "/resources/traits/".$class_name.".php");
  54. $search_path[] = glob($project_path . "/*/*/resources/classes/".$class_name.".php");
  55. $search_path[] = glob($project_path . "/*/*/resources/interfaces/".$class_name.".php");
  56. $search_path[] = glob($project_path . "/*/*/resources/traits/".$class_name.".php");
  57. //find the path
  58. $path = self::autoload_search($search_path);
  59. if (!empty($path)) {
  60. //send to syslog
  61. if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
  62. openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
  63. syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$path.", line: ".__line__);
  64. closelog();
  65. }
  66. //include the class or interface
  67. include $path;
  68. //return boolean
  69. return true;
  70. }
  71. }
  72. //return boolean
  73. return false;
  74. }
  75. }
  76. ?>