Merge branch 'master' into use_javascript_for_phrase_edit

This commit is contained in:
FusionPBX
2025-03-13 14:20:09 -06:00
committed by GitHub
403 changed files with 15352 additions and 4178 deletions
+100 -53
View File
@@ -53,6 +53,12 @@ class settings {
*/
private $database;
/**
* Tracks if the APCu extension is loaded for database queries
* @var bool
*/
private $apcu_enabled;
/**
* Create a settings object using key/value pairs in the $setting_array.
*
@@ -63,6 +69,16 @@ class settings {
*/
public function __construct($setting_array = []) {
//try to use RAM cache by default
if (!isset($setting_array['allow_caching'])) {
$setting_array['allow_caching'] = true;
}
//track ram caching
if ($setting_array['allow_caching']) {
$this->apcu_enabled = function_exists('apcu_enabled') && apcu_enabled();
}
//open a database connection
if (empty($setting_array['database'])) {
$this->database = database::new();
@@ -166,12 +182,12 @@ class settings {
* @param string $uuid uuid of the setting if available. If set to an empty string then a new uuid will be created.
* @param string $category Category of the setting.
* @param string $subcategory Subcategory of the setting.
* @param string $type Type of the setting (array, numeric, text, etc)
* @param string $value (optional) Value to set. Default is empty string.
* @param string $type Type of the setting (array, numeric, text, etc)
* @param bool $enabled (optional) True or False. Default is True.
* @param string $description (optional) Description. Default is empty string.
*/
public function set(string $table_prefix, string $uuid, string $category, string $subcategory, string $type = 'text', string $value = "", bool $enabled = true, string $description = "") {
public function set(string $table_prefix, string $uuid, string $category, string $subcategory, string $value = "", string $type = 'text', bool $enabled = true, string $description = "") {
//set the table name
$table_name = $table_prefix.'_settings';
@@ -223,6 +239,15 @@ class settings {
*/
private function default_settings() {
//set the key for global defaults
$key = 'settings_global_' . $this->category;
//if the apcu extension is loaded get the already parsed array
if ($this->apcu_enabled && apcu_exists($key)) {
$this->settings = apcu_fetch($key);
return;
}
//get the default settings
$sql = "select * from v_default_settings ";
$sql .= "where default_setting_enabled = 'true' ";
@@ -239,17 +264,7 @@ class settings {
$subcategory = $row['default_setting_subcategory'];
if (isset($row['default_setting_value']) && $row['default_setting_value'] !== '') {
if ($name == "boolean") {
if (gettype($row['default_setting_value']) === 'string') {
if ($row['default_setting_value'] === 'true') {
$this->settings[$category][$subcategory] = true;
}
else {
$this->settings[$category][$subcategory] = false;
}
}
elseif (gettype($row['default_setting_value']) === 'boolean') {
$this->settings[$category][$subcategory] = $row['default_setting_value'];
}
$this->settings[$category][$subcategory] = filter_var($row['default_setting_value'], FILTER_VALIDATE_BOOLEAN);
}
elseif ($name == "array") {
if (!isset($this->settings[$category][$subcategory]) || !is_array($this->settings[$category][$subcategory])) {
@@ -263,7 +278,11 @@ class settings {
}
}
}
unset($sql, $result, $row);
//if the apcu extension is loaded store the result
if ($this->apcu_enabled) {
apcu_store($key, $this->settings);
}
}
/**
@@ -271,13 +290,22 @@ class settings {
* @access private
*/
private function domain_settings() {
$sql = "select * from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and domain_setting_enabled = 'true' ";
$parameters['domain_uuid'] = $this->domain_uuid;
$result = $this->database->select($sql, $parameters, 'all');
unset($sql, $parameters);
$key = 'settings_domain_'.$this->domain_uuid;
$result = '';
//if the apcu extension is loaded get the cached database result
if ($this->apcu_enabled && apcu_exists($key)) {
$result = apcu_fetch($key);
} else {
$sql = "select * from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and domain_setting_enabled = 'true' ";
$parameters['domain_uuid'] = $this->domain_uuid;
$result = $this->database->select($sql, $parameters, 'all');
//if the apcu extension is loaded store the result
if ($this->apcu_enabled) {
apcu_store($key, $result);
}
}
if (!empty($result)) {
//domain setting array types override the default settings set as type array
foreach ($result as $row) {
@@ -296,17 +324,7 @@ class settings {
$subcategory = $row['domain_setting_subcategory'];
if (isset($row['domain_setting_value']) && $row['domain_setting_value'] !== '') {
if ($name == "boolean") {
if (gettype($row['domain_setting_value']) === 'string') {
if ($row['domain_setting_value'] === 'true') {
$this->settings[$category][$subcategory] = true;
}
else {
$this->settings[$category][$subcategory] = false;
}
}
elseif (gettype($row['domain_setting_value']) === 'boolean') {
$this->settings[$category][$subcategory] = $row['domain_setting_value'];
}
$this->settings[$category][$subcategory] = filter_var($row['domain_setting_value'], FILTER_VALIDATE_BOOLEAN);
}
if ($name == "array") {
if (!isset($this->settings[$category][$subcategory]) || !is_array($this->settings[$category][$subcategory])) {
@@ -321,7 +339,6 @@ class settings {
}
}
unset($result, $row);
}
/**
@@ -330,15 +347,25 @@ class settings {
* @depends $this->domain_uuid
*/
private function user_settings() {
$sql = "select * from v_user_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and user_uuid = :user_uuid ";
$sql .= " order by user_setting_order asc ";
$parameters['domain_uuid'] = $this->domain_uuid;
$parameters['user_uuid'] = $this->user_uuid;
$result = $this->database->select($sql, $parameters, 'all');
if (is_array($result)) {
$key = 'settings_user_'.$this->user_uuid;
$result = '';
//if the apcu extension is loaded get the cached database result
if ($this->apcu_enabled && apcu_exists($key)) {
$result = apcu_fetch($key);
} else {
$sql = "select * from v_user_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and user_uuid = :user_uuid ";
$sql .= " order by user_setting_order asc ";
$parameters['domain_uuid'] = $this->domain_uuid;
$parameters['user_uuid'] = $this->user_uuid;
$result = $this->database->select($sql, $parameters, 'all');
//if the apcu extension is loaded store the result
if ($this->apcu_enabled) {
apcu_store($key, $result);
}
}
if (!empty($result)) {
foreach ($result as $row) {
if ($row['user_setting_enabled'] == 'true') {
$name = $row['user_setting_name'];
@@ -346,17 +373,7 @@ class settings {
$subcategory = $row['user_setting_subcategory'];
if (isset($row['user_setting_value']) && $row['user_setting_value'] !== '') {
if ($name == "boolean") {
if (gettype($row['user_setting_value']) === 'string') {
if ($row['user_setting_value'] === 'true') {
$this->settings[$category][$subcategory] = true;
}
else {
$this->settings[$category][$subcategory] = false;
}
}
elseif (gettype($row['user_setting_value']) === 'boolean') {
$this->settings[$category][$subcategory] = $row['user_setting_value'];
}
$this->settings[$category][$subcategory] = filter_var($row['user_setting_value'], FILTER_VALIDATE_BOOLEAN);
}
elseif ($name == "array") {
$this->settings[$category][$subcategory][] = $row['user_setting_value'];
@@ -422,4 +439,34 @@ class settings {
}
}
}
/**
* Clears the settings cache
* @param string $type Empty clears all settings. Values can be global, domain, or user.
*/
public static function clear_cache(string $type = '') {
if (function_exists('apcu_enabled') && apcu_enabled()) {
switch ($type) {
case 'all':
case '':
default:
$type = "";
break;
case 'global':
case 'domain':
case 'user':
$type .= "_";
break;
}
$cache = apcu_cache_info(false);
if (!empty($cache['cache_list'])) {
foreach ($cache['cache_list'] as $entry) {
$key = $entry['info'];
if (str_starts_with($key, 'settings_' . $type)) {
apcu_delete($key);
}
}
}
}
}
}