2021-11-26 21:11:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
/*
|
|
|
|
|
FusionPBX
|
|
|
|
|
Version: MPL 1.1
|
|
|
|
|
|
|
|
|
|
The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
http://www.mozilla.org/MPL/
|
|
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
for the specific language governing rights and limitations under the
|
|
|
|
|
License.
|
|
|
|
|
|
|
|
|
|
The Original Code is FusionPBX
|
|
|
|
|
|
|
|
|
|
The Initial Developer of the Original Code is
|
|
|
|
|
Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
Portions created by the Initial Developer are Copyright (C) 2008-2024
|
|
|
|
|
the Initial Developer. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Contributor(s):
|
|
|
|
|
Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Auto Loader class
|
|
|
|
|
* Searches for project files when a class is required. Debugging mode can be set using:
|
|
|
|
|
* - export DEBUG=1
|
|
|
|
|
* OR
|
|
|
|
|
* - debug=true is appended to the url
|
|
|
|
|
*/
|
2021-11-26 21:11:20 +01:00
|
|
|
class auto_loader {
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
const FILE = 'autoloader_cache.php';
|
2025-02-26 01:21:41 +01:00
|
|
|
const CACHE_KEY = 'autoloader_classes';
|
2025-02-13 22:48:40 +01:00
|
|
|
|
|
|
|
|
private $classes;
|
|
|
|
|
|
2025-02-26 01:21:41 +01:00
|
|
|
/**
|
|
|
|
|
* Tracks the APCu extension for caching to RAM drive across requests
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $apcu_enabled;
|
|
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
/**
|
|
|
|
|
* Cache path and file name
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private static $cache_file = null;
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
public function __construct($project_path = '') {
|
2025-02-26 01:21:41 +01:00
|
|
|
|
|
|
|
|
//set if we can use RAM cache
|
|
|
|
|
$this->apcu_enabled = function_exists('apcu_enabled') && apcu_enabled();
|
|
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
//set cache location
|
|
|
|
|
self::$cache_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::FILE;
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//classes must be loaded before this object is registered
|
|
|
|
|
if (!$this->load_cache()) {
|
2025-02-15 18:44:46 +01:00
|
|
|
//cache miss so load them
|
2025-02-13 22:48:40 +01:00
|
|
|
$this->reload_classes($project_path);
|
2025-02-15 18:44:46 +01:00
|
|
|
//update the cache after loading classes array
|
|
|
|
|
$this->update_cache();
|
2025-02-13 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
//register this object to load any unknown classes
|
2021-11-26 21:11:20 +01:00
|
|
|
spl_autoload_register(array($this, 'loader'));
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
public function update_cache(string $file = ''): bool {
|
2025-02-26 01:21:41 +01:00
|
|
|
//guard against writing an empty file
|
2025-02-28 05:09:31 +01:00
|
|
|
if (empty($this->classes)) {
|
2025-02-26 01:21:41 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//update RAM cache when available
|
|
|
|
|
if ($this->apcu_enabled) {
|
2025-03-12 20:55:47 +01:00
|
|
|
$success = apcu_store(self::CACHE_KEY, $this->classes);
|
|
|
|
|
//do not save to drive when we are using apcu
|
|
|
|
|
if ($success) return true;
|
2025-02-26 01:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//ensure we have somewhere to put the file
|
|
|
|
|
if (empty($file)) {
|
2025-03-12 20:55:47 +01:00
|
|
|
$file = self::$cache_file;
|
2024-03-21 17:33:05 +01:00
|
|
|
}
|
2025-02-13 22:48:40 +01:00
|
|
|
|
2025-02-26 01:21:41 +01:00
|
|
|
//export the classes array using PHP engine
|
|
|
|
|
$data = var_export($this->classes, true);
|
2025-02-13 22:48:40 +01:00
|
|
|
|
2025-02-26 01:21:41 +01:00
|
|
|
//put the array in a form that it can be loaded directly to an array
|
|
|
|
|
$result = file_put_contents($file, "<?php\n return " . $data . ";\n");
|
|
|
|
|
if ($result !== false) {
|
|
|
|
|
return true;
|
2024-03-21 17:33:05 +01:00
|
|
|
}
|
2025-03-12 20:55:47 +01:00
|
|
|
|
|
|
|
|
//file failed to save - send error to syslog when debugging
|
2025-02-26 01:21:41 +01:00
|
|
|
$error_array = error_get_last();
|
2025-03-12 20:55:47 +01:00
|
|
|
self::log(LOG_WARNING, $error_array['message'] ?? '');
|
2025-02-26 01:21:41 +01:00
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function load_cache(string $file = ''): bool {
|
|
|
|
|
$this->classes = [];
|
2025-02-26 01:21:41 +01:00
|
|
|
|
|
|
|
|
//use apcu when available
|
|
|
|
|
if ($this->apcu_enabled && apcu_exists(self::CACHE_KEY)) {
|
|
|
|
|
$this->classes = apcu_fetch(self::CACHE_KEY, $success);
|
|
|
|
|
if ($success) return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//use a standard file
|
|
|
|
|
if (empty($file)) {
|
2025-03-12 20:55:47 +01:00
|
|
|
$file = self::$cache_file;
|
2025-02-13 22:48:40 +01:00
|
|
|
}
|
2025-03-12 20:55:47 +01:00
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//use PHP engine to parse it
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
|
$this->classes = include $file;
|
|
|
|
|
}
|
2025-03-12 20:55:47 +01:00
|
|
|
|
|
|
|
|
//catch edge case of first time using apcu cache
|
|
|
|
|
if ($this->apcu_enabled) {
|
|
|
|
|
apcu_store(self::CACHE_KEY, $this->classes);
|
2025-02-13 22:48:40 +01:00
|
|
|
}
|
2025-03-12 20:55:47 +01:00
|
|
|
|
|
|
|
|
//return true when we have classes and false if the array is still empty
|
|
|
|
|
return !empty($this->classes);
|
2025-02-13 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function reload_classes($project_path = '') {
|
|
|
|
|
//set project path using magic dir constant
|
|
|
|
|
if (empty($project_path)) {
|
|
|
|
|
$project_path = dirname(__DIR__, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
//build the array of all locations for classes in specific order
|
|
|
|
|
$search_path = [
|
|
|
|
|
$project_path . '/resources/interfaces/*.php',
|
|
|
|
|
$project_path . '/resources/traits/*.php',
|
|
|
|
|
$project_path . '/resources/classes/*.php',
|
|
|
|
|
$project_path . '/*/*/resources/interfaces/*.php',
|
|
|
|
|
$project_path . '/*/*/resources/traits/*.php',
|
|
|
|
|
$project_path . '/*/*/resources/classes/*.php',
|
|
|
|
|
$project_path . '/core/authentication/resources/classes/plugins/*.php',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
//get all php files for each path
|
|
|
|
|
$files = [];
|
|
|
|
|
foreach ($search_path as $path) {
|
|
|
|
|
$files = array_merge($files, glob($path));
|
|
|
|
|
}
|
2025-02-13 22:48:40 +01:00
|
|
|
|
|
|
|
|
//reset the current array
|
|
|
|
|
$this->classes = [];
|
|
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
//store PHP language declared classes, interfaces, and traits
|
|
|
|
|
$curr_classes = get_declared_classes();
|
|
|
|
|
$curr_interfaces = get_declared_interfaces();
|
|
|
|
|
$curr_traits = get_declared_traits();
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//store the class name (key) and the path (value)
|
2025-03-12 20:55:47 +01:00
|
|
|
foreach ($files as $file) {
|
|
|
|
|
|
|
|
|
|
//include the new class
|
|
|
|
|
try {
|
|
|
|
|
include_once $file;
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
//report the error
|
|
|
|
|
self::log(LOG_ERR, "Exception while trying to include file '$file': " . $e->getMessage());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//get the new classes
|
|
|
|
|
$new_classes = get_declared_classes();
|
|
|
|
|
$new_interfaces = get_declared_interfaces();
|
|
|
|
|
$new_traits = get_declared_traits();
|
|
|
|
|
|
|
|
|
|
//check for a new class
|
|
|
|
|
$classes = array_diff($new_classes, $curr_classes);
|
|
|
|
|
if (!empty($classes)) {
|
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
|
$this->classes[$class] = $file;
|
|
|
|
|
}
|
|
|
|
|
//overwrite previous array with new values
|
|
|
|
|
$curr_classes = $new_classes;
|
|
|
|
|
}
|
2025-02-13 23:05:27 +01:00
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
//check for a new interface
|
|
|
|
|
$interfaces = array_diff($new_interfaces, $curr_interfaces);
|
|
|
|
|
if (!empty($interfaces)) {
|
|
|
|
|
foreach ($interfaces as $interface) {
|
|
|
|
|
$this->classes[$interface] = $file;
|
|
|
|
|
}
|
|
|
|
|
//overwrite previous array with new values
|
|
|
|
|
$curr_interfaces = $new_interfaces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//check for a new trait
|
|
|
|
|
$traits = array_diff($new_traits, $curr_traits);
|
|
|
|
|
if (!empty($traits)) {
|
|
|
|
|
foreach ($traits as $trait) {
|
|
|
|
|
$this->classes[$trait] = $file;
|
|
|
|
|
}
|
|
|
|
|
//overwrite previous array with new values
|
|
|
|
|
$curr_traits = $new_traits;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-21 17:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
/**
|
|
|
|
|
* The loader is set to private because only the PHP engine should be calling this method
|
|
|
|
|
* @param string $class_name The class name that needs to be loaded
|
|
|
|
|
* @return bool True if the class is loaded or false when the class is not found
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
private function loader($class_name): bool {
|
2021-11-26 21:11:20 +01:00
|
|
|
|
|
|
|
|
//sanitize the class name
|
2024-03-21 17:33:05 +01:00
|
|
|
$class_name = preg_replace('[^a-zA-Z0-9_]', '', $class_name);
|
2021-11-26 21:11:20 +01:00
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//find the path using the class_name as the key in the classes array
|
|
|
|
|
if (isset($this->classes[$class_name])) {
|
|
|
|
|
//include the class or interface
|
|
|
|
|
include_once $this->classes[$class_name];
|
2024-04-26 21:49:56 +02:00
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//return boolean
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 01:21:41 +01:00
|
|
|
//Smarty has it's own autoloader so reject the request
|
|
|
|
|
if ($class_name === 'Smarty_Autoloader') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 19:24:39 +01:00
|
|
|
//cache miss
|
2025-03-12 20:55:47 +01:00
|
|
|
self::log(LOG_WARNING, "class '$class_name' not found in cache");
|
2025-02-20 19:24:39 +01:00
|
|
|
|
|
|
|
|
//set project path using magic dir constant
|
|
|
|
|
$project_path = dirname(__DIR__, 2);
|
|
|
|
|
|
|
|
|
|
//build the search path array
|
2025-03-12 20:55:47 +01:00
|
|
|
$search_path[] = glob($project_path . "/resources/interfaces/" . $class_name . ".php");
|
|
|
|
|
$search_path[] = glob($project_path . "/resources/traits/" . $class_name . ".php");
|
|
|
|
|
$search_path[] = glob($project_path . "/resources/classes/" . $class_name . ".php");
|
|
|
|
|
$search_path[] = glob($project_path . "/*/*/resources/interfaces/" . $class_name . ".php");
|
|
|
|
|
$search_path[] = glob($project_path . "/*/*/resources/traits/" . $class_name . ".php");
|
|
|
|
|
$search_path[] = glob($project_path . "/*/*/resources/classes/" . $class_name . ".php");
|
2025-02-20 19:24:39 +01:00
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
//collapse all entries to only the matched entry
|
|
|
|
|
$matches = array_filter($search_path);
|
|
|
|
|
if (!empty($matches)) {
|
|
|
|
|
$path = array_pop($matches)[0];
|
2025-02-20 19:24:39 +01:00
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
//include the class, interface, or trait
|
|
|
|
|
include_once $path;
|
2025-02-20 19:24:39 +01:00
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
//inject the class in to the array
|
|
|
|
|
$this->classes[$class_name] = $path;
|
2025-02-20 19:24:39 +01:00
|
|
|
|
|
|
|
|
//update the cache with new classes
|
|
|
|
|
$this->update_cache();
|
|
|
|
|
|
|
|
|
|
//return boolean
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 22:48:40 +01:00
|
|
|
//send to syslog when debugging
|
2025-03-12 20:55:47 +01:00
|
|
|
self::log(LOG_ERR, "class '$class_name' not found name");
|
2021-11-26 21:11:20 +01:00
|
|
|
|
2024-03-21 17:33:05 +01:00
|
|
|
//return boolean
|
|
|
|
|
return false;
|
2021-11-26 21:11:20 +01:00
|
|
|
}
|
2025-02-20 19:24:39 +01:00
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
/**
|
|
|
|
|
* Returns a list of classes loaded by the auto_loader. If no classes have been loaded an empty array is returned.
|
|
|
|
|
* @return array List of classes loaded by the auto_loader or empty array
|
|
|
|
|
*/
|
|
|
|
|
public function get_class_list(): array {
|
|
|
|
|
if (!empty($this->classes)) {
|
|
|
|
|
return $this->classes;
|
2025-02-20 19:24:39 +01:00
|
|
|
}
|
2025-03-12 20:55:47 +01:00
|
|
|
return [];
|
2025-02-20 19:24:39 +01:00
|
|
|
}
|
2025-02-26 01:21:41 +01:00
|
|
|
|
2025-03-12 20:55:47 +01:00
|
|
|
public static function clear_cache(string $file = '') {
|
|
|
|
|
|
|
|
|
|
//check for apcu cache
|
2025-02-26 01:21:41 +01:00
|
|
|
if (function_exists('apcu_enabled') && apcu_enabled()) {
|
|
|
|
|
apcu_delete(self::CACHE_KEY);
|
|
|
|
|
}
|
2025-03-12 20:55:47 +01:00
|
|
|
|
|
|
|
|
//set default file
|
|
|
|
|
if (empty(self::$cache_file)) {
|
|
|
|
|
self::$cache_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::FILE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//set file to clear
|
|
|
|
|
if (empty($file)) {
|
|
|
|
|
$file = self::$cache_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//remove the file when it exists
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
|
@unlink($file);
|
|
|
|
|
$error_array = error_get_last();
|
|
|
|
|
//send to syslog when debugging with either environment variable or debug in the url
|
|
|
|
|
self::log(LOG_WARNING, $error_array['message'] ?? '');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function log(int $level, string $message): void {
|
|
|
|
|
if (filter_var($_REQUEST['debug'] ?? false, FILTER_VALIDATE_BOOL) || filter_var(getenv('DEBUG') ?? false, FILTER_VALIDATE_BOOL)) {
|
|
|
|
|
openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
|
|
|
|
|
syslog($level, "[auto_loader] " . $message);
|
|
|
|
|
closelog();
|
|
|
|
|
}
|
2025-02-26 01:21:41 +01:00
|
|
|
}
|
2021-11-26 21:11:20 +01:00
|
|
|
}
|