Files
fusionpbx/resources/classes/settings.php
FusionPBX 7f9064fadb Update boolean toggle and the database boolean type (#7522)
* Change the data type to boolean

* Use boolean values in SQL and Conditional Statements

* Schema Data Types: Drop views so that alter tables statements complete

* Update settings.php

* Add extension for call_screen_enabled

* Gateway handle boolean
Add the boolean toggle
- distinct_to
- caller_id_in_from
- supress_cng
- extension_in_contact
- contact_in_ping

* Update gateways.php

* Update voicemail_edit.php

* Replace the ${caller_id_name} with the extensions caller id name

* Phrase - Edit: Update slide toggle with new select beneath.

* Update the input toggle style

* Set the input_toggle_style_switch as boolean

* Update the input toggle style

* Theme: Adjust CSS to wrap description text on form fields.

* Add domain_uuid and domain_name to the SQL SELECT

* Unset the parameters to fix the next query

* Gateway: Integrate new slide toggle switch.

* Dashboard: Update the input toggle style (#7499)

* Dashboard: Update the input toggle style

* Update dashboard_widget_edit.php

* Update dashboard_edit.php

* Inbound Route - Add: Integrate new slide toggle switch and boolean.

* User Settings: Integrate new slide toggle switch.

* Call Block Edit: Update the input toggle style (#7500)

* Bridge Edit: Update the input toggle style (#7501)

* Update phrases.php

* Update domain_edit.php

* Domain Settings Edit: Update the input toggle style

* User Edit: Update the input toggle style

* Update install.php

* Remove fields marked as deprecated

* Use boolean in the select statement

* Streams: New slide toggle and boolean.

* Streams: Remove unnecessary default.

* Queues (FIFO): New slide toggle.

* Call Detail Records: Update the input toggle style (#7503)

* Call Detail Records: Update the input toggle style

* Update xml_cdr_extension_summary.php

* Update xml_cdr_extension_summary.php

* Update xml_cdr_extension_summary.php

* Call Centers: Update the input toggle style (#7502)

* Call Centers: Update the input toggle style

* Update call_center_agent_edit.php

* Conference Centers: Update the input toggle style (#7506)

* Conference Centers: Update the input toggle style

* Update app_config.php

* Update conference_center_edit.php

* Use boolean data type (#7505)

* Call Flow Edit: Update the input toggle style (#7504)

* Devices: Update the input toggle style

* IVR Menus: New slide toggle.

* IVR Menu: Remove deprected file.

* SIP Profile - Edit: New slide toggle.

* Device Profiles: Update the input toggle style

* Ring Groups - List: Fix syntax.

* Theme: Fire onchange event when switching slide toggle control.

* Device Vendors: Update the input toggle style

* Update enabled boolean

* Update device_edit.php

* Ring Groups: New slide toggle.

* Update domain_edit.php

* Email Templates: Update the input toggle style

* Header: Correct default value on input toggle style.

* Default/Domain/User Settings: Adjust Select option label for better clarity.

* Use true boolean types for switch theme toggle or drop-down box (#7507)

* Groups: Update the input toggle style

* Conference Controls: Update the input toggle style (#7509)

* Conference Controls: Update the input toggle style

* Update conference_control_edit.php

* Update conference_control_details.php

* Update conference_control_detail_edit.php

* Conferences: Update the input toggle style (#7511)

* Use true boolean types for switch theme toggle or drop-down box (#7513)

* Dialplans: Update the input toggle style
Also updated outbound route add

* Ring Group: Only adjust ring group forward destination top margin when slide toggle enabled.

* Default/Domain/User Settings - List: Adjust for new 'Select Box' label on input_toggle_style value.

* Use true boolean types for switch theme toggle or drop-down box (#7514)

* Call Forward / Follow Me: Minor javascript adjustments to mirror previous behavior.

* Ring Groups [Dashboard]: New slide toggle.

* Get the contacts details with a seperate SQL Query

* Remove debug info

* Destinations: Update the input toggle style

* Conference Profiles: Update the input toggle style (#7515)

* Conference Profiles: Update the input toggle style

* Update conference_profiles.php

* Update conference_profile_params.php

* Update conference_profile_param_edit.php

* Update conference_profile_edit.php

* Extensions: Update the input toggle style

* Contacts: Update the input toggle style (#7517)

* Contacts: Update the input toggle style

* Update contact_address_edit.php

* Update contact_attachment_edit.php

* Update contact_edit.php

* Update contact_email_edit.php

* Update contact_phone_edit.php

* Update contact_relation_edit.php

* Update contact_setting_edit.php

* Update contact_url_edit.php

* Update contacts.php

* Voicemails: New slide toggle.

* Voicemail Greeting: New slide toggle.

* Modules: New slide toggle.

* Recordings: New slide toggle

* Variables: New slide toggle.

* Time Conditions: New slide toggle.

* Extensions - List: Remove invalid column name.

* Extension Settings: New slide toggle.

* Number Translations: New slide toggle and boolean.

* Sofia global settings: Update the input toggle style

* Update alter table convert to boolean

* Fix contact relation search (#7518)

* Sip Profile Edit: Fix setting enabled labels (#7519)

* Sip Profile Edit: Fix setting enabled labels

* Update sip_profile_edit.php

* Update dialplan_edit.php (#7520)

* Update SQL queries to use true/false for contact primary settings (#7521)

* Update SQL queries to use true/false for contact primary settings

* Update contacts_vcard.php

* Update email.php

* Update totp.php

* Update contact_url_edit.php

* Update contact_email_edit.php

* Update contact_address_edit.php

* Update contact_attachment_edit.php

* Update contact_phone_edit.php

* Add a database views method

* Add database view groups

* Add database users view

* Add database call_recordings view

* Update users.php

* Add  database view call block

* Update schema to use the database views method

* Update conference.conf.lua boolean

* Update directory.lua boolean

* Update callcenter.conf.lua agent_status boolean

* Update sofia.conf.lua boolean

* Update number translations lua boolean

* Update directory.lua boolean

* Update group_call.lua fix the cache

* Update reverse-auth-lookup.lua boolean

* Update directory.lua boolean

* Use concat instead of ||

* Update index.lua

---------

Co-authored-by: fusionate <nate@fusionpbx.com>
Co-authored-by: Alex <alex@fusionpbx.com>
Co-authored-by: frytimo <tim@fusionpbx.com>
2025-09-20 18:42:05 -06:00

471 lines
15 KiB
PHP

<?php
/**
* The settings class is used to load settings using hierarchical overriding
*
* The settings are loaded from the database tables default_settings, domain_settings, and user_settings in that order with
* Each setting overrides the setting from the previous table.
*
* @access public
* @author Mark Crane <mark@fusionpbx.com>
*/
class settings implements clear_cache {
/**
* Set in the constructor. String used to load a specific domain. Must be a value domain UUID before sending to the constructor.
* @var string
*/
private $domain_uuid;
/**
* Set in the constructor. String used to load a specific user. Must be a valid user UUID before sending to the constructor.
* @var string
*/
private $user_uuid;
/**
* Set in the constructor. String used for loading a specific device UUID
* @var string
*/
private $device_uuid;
/**
* Set in the constructor. String used for loading device profile
* @var string
*/
private $device_profile_uuid;
/**
* Set in the constructor. Current category set to load
* @var string
*/
private $category;
/**
* Internal array structure that is populated from the database
* @var array Array of settings loaded from Default Settings
*/
private $settings;
/**
* Set in the constructor. Must be a database object and cannot be null.
* @var database Database Object
*/
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.
*
* Valid values are: database, domain_uuid, user_uuid, device_uuid, device_profile_uuid, and category.
* @param array setting_array
* @depends database::new()
* @access public
*/
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();
} else {
$this->database = $setting_array['database'];
}
//trap passing a PDO object instead of the required database object
if (!($this->database instanceof database)) {
//should never happen but will trap it here just-in-case
throw new \InvalidArgumentException("Database object passed in settings class constructor is not a valid database object");
}
//set the values from the array
$this->domain_uuid = $setting_array['domain_uuid'] ?? null;
$this->user_uuid = $setting_array['user_uuid'] ?? null;
$this->device_profile_uuid = $setting_array['device_profile_uuid'] ?? null;
$this->device_uuid = $setting_array['device_uuid'] ?? null;
$this->category = $setting_array['category'] ?? null;
$this->reload();
}
/**
* Returns the database object used in the settings
* @return database Object
*/
public function database(): database {
return $this->database;
}
/**
* Reloads the settings from the database
*/
public function reload() {
$this->settings = [];
//set the default settings
$this->default_settings();
//set the domain settings
if (!empty($this->domain_uuid)) {
$this->domain_settings();
//set the user settings only when the domain_uuid was set
if (!empty($this->user_uuid)) {
$this->user_settings();
}
//set the device profile settings
if (!empty($this->device_profile_uuid)) {
$this->device_profile_settings();
}
//set the device settings
if (!empty($this->device_uuid)) {
$this->device_settings();
}
}
}
/**
* Get the value utilizing the hierarchical overriding technique
* @param string $category Returns all settings when empty or the default value if the settings array is null
* @param string $subcategory Returns the array of category items when empty or the default value if the category array is null
* @param mixed $default_value allows default value returned if category and subcategory not found
*/
public function get(string $category = null, string $subcategory = null, $default_value = null) {
//incremental refinement from all settings to a single setting
if (empty($category)) {
return $this->settings ?? $default_value;
}
elseif (empty($subcategory)) {
return $this->settings[$category] ?? $default_value;
}
else {
return $this->settings[$category][$subcategory] ?? $default_value;
}
}
/**
* Returns the domain_uuid in this object used to load the settings
* @return string UUID of the domain used to load the object or an empty string
*/
public function get_domain_uuid(): string {
if (!empty($this->domain_uuid)) {
return $this->domain_uuid;
}
return "";
}
/**
* Returns the user_uuid in this object used to load the settings
* @return string UUID of the user used to load the object or an empty string
*/
public function get_user_uuid(): string {
if (!empty($this->user_uuid)) {
return $this->user_uuid;
}
return "";
}
/**
* set the default, domain, user, device or device profile settings
* @param string $table_prefix prefix for the table.
* @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 $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 $value = "", string $type = 'text', bool $enabled = true, string $description = "") {
//set the table name
$table_name = $table_prefix.'_settings';
//init record as an array
$record = [];
if(!empty($this->domain_uuid)) {
$record[$table_name][0]['domain_uuid'] = $this->domain_uuid;
}
if(!empty($this->user_uuid)) {
$record[$table_name][0]['user_uuid'] = $this->user_uuid;
}
if(!empty($this->device_uuid)) {
$record[$table_name][0]['device_uuid'] = $this->device_uuid;
}
if(!empty($this->device_profile_uuid)) {
$record[$table_name][0]['device_profile_uuid'] = $this->device_profile_uuid;
}
if(!is_uuid($uuid)) {
$uuid = uuid();
}
//build the array
$record[$table_name][0][$table_prefix.'_setting_uuid' ] = $uuid;
$record[$table_name][0][$table_prefix.'_setting_category' ] = $category;
$record[$table_name][0][$table_prefix.'_setting_subcategory'] = $subcategory;
$record[$table_name][0][$table_prefix.'_setting_name' ] = $type;
$record[$table_name][0][$table_prefix.'_setting_value' ] = $value;
$record[$table_name][0][$table_prefix.'_setting_enabled' ] = $enabled;
$record[$table_name][0][$table_prefix.'_setting_description'] = $description;
//grant temporary permissions
$p = permissions::new();
$p->add($table_prefix.'_setting_add', 'temp');
$p->add($table_prefix.'_setting_edit', 'temp');
//execute insert
$this->database->app_name = $table_name;
$this->database->save($record);
//revoke temporary permissions
$p->delete($table_prefix.'_setting_add', 'temp');
$p->delete($table_prefix.'_setting_edit', 'temp');
}
/**
* Update the internal settings array with the default settings from the database
* @access private
*/
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 ";
if (!empty($this->category)) {
$sql .= "and default_setting_category = :default_setting_category ";
$parameters['default_setting_category'] = $this->category;
}
$sql .= "order by default_setting_order asc ";
$result = $this->database->select($sql, $parameters ?? null, 'all');
if (!empty($result)) {
foreach ($result as $row) {
$name = $row['default_setting_name'];
$category = $row['default_setting_category'];
$subcategory = $row['default_setting_subcategory'];
if (isset($row['default_setting_value']) && $row['default_setting_value'] !== '') {
if ($name == "boolean") {
$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])) {
$this->settings[$category][$subcategory] = array();
}
$this->settings[$category][$subcategory][] = $row['default_setting_value'];
}
else {
$this->settings[$category][$subcategory] = $row['default_setting_value'];
}
}
}
}
//if the apcu extension is loaded store the result
if ($this->apcu_enabled) {
apcu_store($key, $this->settings);
}
}
/**
* Update the internal settings array with the domain settings from the database
* @access private
*/
private function domain_settings() {
$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) {
$name = $row['domain_setting_name'];
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if ($name == "array") {
$this->settings[$category][$subcategory] = array();
}
}
//add the domain settings to the $this->settings array
foreach ($result as $row) {
$name = $row['domain_setting_name'];
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if (isset($row['domain_setting_value']) && $row['domain_setting_value'] !== '') {
if ($name == "boolean") {
$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])) {
$this->settings[$category][$subcategory] = array();
}
$this->settings[$category][$subcategory][] = $row['domain_setting_value'];
}
else {
$this->settings[$category][$subcategory] = $row['domain_setting_value'];
}
}
}
}
}
/**
* Update the internal settings array with the user settings from the database
* @access private
* @depends $this->domain_uuid
*/
private function user_settings() {
$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'];
$category = $row['user_setting_category'];
$subcategory = $row['user_setting_subcategory'];
if (isset($row['user_setting_value']) && $row['user_setting_value'] !== '') {
if ($name == "boolean") {
$this->settings[$category][$subcategory] = filter_var($row['user_setting_value'], FILTER_VALIDATE_BOOLEAN);
}
elseif ($name == "array") {
$this->settings[$category][$subcategory][] = $row['user_setting_value'];
}
else {
$this->settings[$category][$subcategory] = $row['user_setting_value'];
}
}
}
}
}
}
/**
* Update the internal settings array with the device profile settings from the database
* @access private
* @depends $this->domain_uuid
*/
private function device_profile_settings() {
//get the device profile settings
$sql = "select profile_setting_name, profile_setting_value from v_device_profile_settings ";
$sql .= "where device_profile_uuid = :device_profile_uuid ";
$sql .= "and domain_uuid = :domain_uuid ";
$sql .= "and profile_setting_enabled = true ";
$params = [];
$params['device_profile_uuid'] = $this->device_profile_uuid;
$params['domain_uuid'] = $this->domain_uuid;
$result = $this->database->select($sql, $params, 'all');
if (!empty($result)) {
foreach ($result as $row) {
$name = $row['profile_setting_name'];
$value = $row['profile_setting_value'];
$this->settings[$name] = $value;
}
}
}
/**
* Update the internal settings array with the device settings from the database
* @access private
* @depends $this->domain_uuid
*/
private function device_settings() {
//get the device settings
$sql = "select device_setting_subcategory, device_setting_value from v_device_settings ";
$sql .= "where device_setting_uuid = :device_uuid ";
$sql .= "and domain_uuid = :domain_uuid ";
$sql .= "and device_setting_enabled = true ";
$params = [];
$params['device_uuid'] = $this->device_uuid;
$params['domain_uuid'] = $this->domain_uuid;
$result = $this->database->select($sql, $params, 'all');
if (!empty($result)) {
foreach ($result as $row) {
$name = $row['device_setting_subcategory'];
$value = $row['device_setting_value'] ?? null;
$this->settings[$name] = $value;
}
}
}
public static function clear_cache() {
if (function_exists('apcu_enabled') && apcu_enabled()) {
$cache = apcu_cache_info(false);
if (!empty($cache['cache_list'])) {
//clear apcu cache
foreach ($cache['cache_list'] as $entry) {
$key = $entry['info'];
if (str_starts_with($key, 'settings_')) {
apcu_delete($key);
}
}
global $settings;
//check there is a settings object
if (!empty($settings) && ($settings instanceof settings)) {
$database = $settings->database();
$domain_uuid = $settings->get_domain_uuid();
$user_uuid = $settings->get_user_uuid();
//recreate the settings object to reload all settings from database
$settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid, 'user_uuid' => $user_uuid]);
}
}
}
}
}