Files
fusionpbx/resources/classes/config.php
FusionPBX dda18481ec Refactor PHP code (#7574)
* Use settings-get method
- get default, domain and user settings
- Replace _SESSION

* Simplify get domain paging

* Change isset using empty

* Fix token name and hash

* Add new default settings category: contact, name: default_sort_column, and default_sort_order

* Update app_config.php

* Update footer.php

* Fix require.php and core/dashboard/index.php (#7563)

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Fix require.php and core/dashboard/index.php

* Update destinations.php

* Update permission.php

* Update require.php

---------

Co-authored-by: FusionPBX <markjcrane@gmail.com>
Co-authored-by: FusionPBX <mark@fusionpbx.com>

* Update header.php

* Use settings get classes (#7567)

* access_control

* azure

* azure

* basic_operator_panel

* bridges

* call_block

* call_broadcast

* call_center

* call_flows

* call_forward

* call_recordings

* do_not_disturb

* feature_event_notify

* follow_me

* remove unused object properties

* fix esl command

* fix esl command

* conference_centers

* conference_centers - remove whitespace

* conference_controls

* conference_profiles

* conference_profiles

* Delete core/websockets/resources/classes/socket_exception.php.original~

* Delete core/websockets/resources/classes/websocket_server.php.original~

* conferences

* destinations

* device

* dialplan

* email_queue

* event_guard

* extension_settings

* extension

* fax

* fax_queue

* fifo

* gateways

* ivr_menu

* modules

* switch_music_on_hold

* number_translations

* phrases

* pin_numbers

* provision

* switch_recordings

* registrations

* ring_groups

* sip_profiles

* sofia_global_settings

* streams

* presence

* switch_files

* time_conditions

* vars

* voicemail_greetings

* voicemail

* ringbacks

* contacts

* xml_cdr

* authentication

* dashboard

* default_settings

* domain_settings

* email_templates

* permission

* user_logs

* user_settings

* users

* button

* cache

* captcha

* remove cli_option

* remove directory.php for switch_directory class

* email

* file

* groups

* event_socket use config object

* Use intval to give an integer port

* switch_settings

* tones

* fix object used before initialization

* menu

* fix copy paste error for switch_files.php

* always include require.php for framework files

* Fix missing properties

* set the action

* Use the $database object

* Add missing class properties

* Fix the domain_name

* Use public scope for domain_uuid and domain_name

* Add missing parameters

* Correct the user_uuid parameter

* Add json_validate and use it in the dashboard
update indentation on functions.php

* Intialize the active_registrations variable

* Define the $parameters

* Set a default value for user_setting_enabled

* Add condition domain_uuid not empty

* Add not empty condition for domain_uuid

* Declare the global variables

* Update how the defaults are set use ??

* Use ?? to set the default values

* Update call_center_queue_edit.php

* Prevent an error

* Add domain_name property

* Fix the null coalescing operator

* Removed domain_uuid from the URL

* Change condition to check sip_profile_domain_name

* Refactor domain_uuid declaration and comments

Removed redundant domain_uuid declaration and updated comments.

* Account for an empty value

* Refactor constructor to use settings array

Updated constructor to accept settings array for domain UUID and database initialization.

* Refactor xml_cdr.php to improve variable organization

Removed duplicate domain_uuid declaration and reorganized private variables for better structure.

* Enhance transcription button logic and icon display

Updated transcription button visibility conditions and improved application icon handling in the call flow summary.

* Refactor settings initialization in domains.php

Updated settings initialization to include domain_uuid and user_uuid.

* Modify domain change condition in require.php

Updated condition to check if 'domain_change' is not empty before proceeding.

* Set default_setting_enabled to true by default

* Enhance domain UUID check in access controls

* Enhance domain UUID check in settings list

* Refactor category display logic in vars.php

* Simplify list row URL generation

Removed domain UUID check from list row URL construction.

* Refactor module category display logic

* Fix SQL query by removing parameters variable

* Initialize result_count variable for call recordings

* Refactor leg variable usage in xml_cdr_details.php

* Update conference_room_edit.php

* Change GET to REQUEST for order and search variables

* Set timezone and SQL time format in recordings.php

Added timezone and SQL time format settings.

* Set default for ring group greeting

* Improve domain UUID check in stream listing

* Handle null voicemail_option_param safely

* Add file existence check for greeting files

Check if greeting file exists before getting size and date.

* Improve domain UUID check in email templates

* Update FIFO strategy dropdown and description text

* Add multilingual agent descriptions

Added multilingual descriptions for agents in the app_languages.php file.

* Add music on hold descriptions

* Add the chime list description

Updated copyright year from 2024 to 2025.

* Fix domain UUID check and handle email subject decoding

* Add null coalescing for $value in email_test.php

Ensure $value is not null by providing a default empty string.

* Handle undefined dialplan_uuid in input field

* Add translations for 'Status' label in multiple languages

* Fix typo in config instance check

---------

Co-authored-by: frytimo <tim@fusionpbx.com>
2025-10-15 21:24:59 -06:00

349 lines
11 KiB
PHP

<?php
/**
* config class loads configuration from the file system
* @param string $db_type Type of database
* @param string $db_driver Alias of type
* @param string $db_host Host to connect to
* @param string $db_path Path of the database if it is file system based
* @param string $db_file File name of the database if it is file system based
* @param string $db_port Port to connect to
* @param string $db_name Name of the database
* @param string $db_sslmode SSL Mode to use
* @param string $db_cert_authority The certificate authority
* @param string $db_secure If the database is using a secure connection
* @param string $db_username Username credentials to connect with
* @param string $db_password Password credentials to connect with
* @param string $config_path Configuration path currently in use
* @param string $config_file Configuration file currently in use
* @param string $config_path_and_filename Full path and configuration file currently in use
* @internal the @param statements are used because they match the magic __get function that allows those to be accessed publicly
*/
final class config {
// Full path and filename of config.conf
private $file;
// The internal array that holds the configuration in the config.conf file
private $configuration;
/**
* Configuration object used to hold a single instance
* @var array
*/
public static $config = null;
/**
* Loads the framework configuration file
*/
public function __construct(string $file = '') {
//initialize configuration array to be an empty array
$this->configuration = [];
//check if the config file was found
if (empty($file)) {
//locate the conf file
$file = self::find();
}
//remember the fullpath and filename
$this->file = $file;
//load the conf file
if (file_exists($file)) {
$this->read();
}
//set the server variables
$this->define_project_paths();
}
/**
* Magic method to allow backward compatibility for variables such as db_type.
* <p>This will allow using config object with the syntax of:<br>
* $config = new config();<br>
* $db_type = $config->db_type;<br></p>
* <p>Note:<br>
* The <i>InvalidArgumentException</i> is thrown if there is no such variable accessed such as:<br>
* $config = new config();<br>
* $db_function = $config->db_function();
* </p>
* <p>This is ensure that any invalid code is detected and fixed.</p>
* @param string $name Name of the object property
* @return string Returns the value as a string
*/
public function __get(string $name): string {
switch($name) {
case 'db_type':
case 'db_driver':
return $this->configuration['database.0.type'] ?? '';
case 'db_path':
case 'path':
return $this->configuration['database.0.path'] ?? '';
case 'db_host':
return $this->configuration['database.0.host'] ?? '';
case 'db_port':
return $this->configuration['database.0.port'] ?? '';
case 'db_name':
return $this->configuration['database.0.name'] ?? '';
case 'db_sslmode':
return $this->configuration['database.0.sslmode'] ?? 'prefer';
case 'db_cert_authority':
return $this->configuration['database.0.cert_authority'] ?? '';
case 'db_secure':
return $this->configuration['database.0.secure'] ?? 'false';
case 'db_username':
case 'username':
return $this->configuration['database.0.username'] ?? '';
case 'db_password':
case 'password':
return $this->configuration['database.0.password'] ?? '';
case 'db_file':
return $this->configuration['database.0.file'] ?? '';
case 'config_path':
return $this->path();
case 'config_filename':
return $this->filename();
case 'config_path_and_filename':
case 'config_file':
return $this->path_and_filename();
default:
if (property_exists($this, $name)) {
return $this->{$name};
}
elseif (array_key_exists($name, $this->configuration)) {
return $this->configuration[$name];
}
}
return "";
}
/**
* Returns the string representation of the configuration file
* @return string configuration
*/
public function __toString(): string {
$string_builder = "";
foreach ($this->configuration as $key => $value) {
$string_builder .= "$key = '$value'\n";
}
return $string_builder;
}
// loads the config.conf file
public function read() {
//check if include is needed
if (substr($this->file, 0, -4) === '.php') {
//allow global variables to be set in the old config.php file
global $db_type, $db_host, $db_port, $db_name, $db_username, $db_password, $db_path;
global $db_sslmode, $db_secure, $db_cert_authority;
//load the config.php file
require_once $this->file;
//convert the old properties to the new standard
if (isset($db_type)) {
$this->configuration['database.0.type'] = $db_type;
} else {
$this->configuration['database.0.type'] = 'pgsql';
}
if (isset($db_path)) {
$this->configuration['database.0.path'] = $db_path;
} else {
$this->configuration['database.0.path'] = '';
}
if (isset($db_host)) {
$this->configuration['database.0.host'] = $db_host;
}
if (isset($db_port)) {
$this->configuration['database.0.port'] = $db_port;
}
if (isset($db_name)) {
$this->configuration['database.0.name'] = $db_name;
}
if (isset($db_username)) {
$this->configuration['database.0.username'] = $db_username;
}
if (isset($db_password)) {
$this->configuration['database.0.password'] = $db_password;
}
if (isset($db_sslmode)) {
$this->configuration['database.0.sslmode'] = $db_sslmode;
} else {
$this->configuration['database.0.sslmode'] = 'prefer';
}
if (isset($db_secure)) {
$this->configuration['database.0.secure'] = $db_secure;
}
if (isset($db_cert_authority)) {
$this->configuration['database.0.cert_authority'] = $db_cert_authority;
}
//remove from the global namespace
unset($db_type, $db_host, $db_port, $db_name, $db_username, $db_password, $db_sslmode, $db_secure, $db_cert_authority);
}
else {
//save the loaded and parsed conf file to the object
$this->configuration = parse_ini_file($this->file);
}
}
// set project paths if not already defined
private function define_project_paths() {
// Load the document root
$doc_root = $this->get('document.root', '/var/www/fusionpbx');
$doc_path = $this->get('document.path', '');
//set the server variables and define project path constant
if (!empty($doc_path)) {
if (!defined('PROJECT_PATH')) { define("PROJECT_PATH", $doc_path); }
if (!defined('PROJECT_ROOT')) { define("PROJECT_ROOT", $doc_root.'/'.$doc_path); }
}
else {
if (!defined('PROJECT_PATH')) { define("PROJECT_PATH", ''); }
if (!defined('PROJECT_ROOT')) { define("PROJECT_ROOT", $doc_root); }
}
// internal definitions to the framework
$_SERVER["PROJECT_PATH"] = PROJECT_PATH;
$_SERVER["PROJECT_ROOT"] = PROJECT_ROOT;
// tell php where the framework is
$_SERVER["DOCUMENT_ROOT"] = PROJECT_ROOT;
// have php search for any libraries in the now defined root
set_include_path(PROJECT_ROOT);
}
/**
* Find the path to the config.conf file
* @var string $config_path - full path to the config.php file
*/
public static function find(): string {
//define the file variable
$file = "";
//find the file
if (file_exists("/etc/fusionpbx/config.conf")) {
$file = "/etc/fusionpbx/config.conf";
}
elseif (file_exists("/usr/local/etc/fusionpbx/config.conf")) {
$file = "/usr/local/etc/fusionpbx/config.conf";
}
elseif (file_exists("/etc/fusionpbx/config.php")) {
$file = "/etc/fusionpbx/config.php";
}
elseif (file_exists("/usr/local/etc/fusionpbx/config.php")) {
$file = "/usr/local/etc/fusionpbx/config.php";
}
elseif (file_exists(getenv('SystemDrive') . DIRECTORY_SEPARATOR . 'ProgramData' . DIRECTORY_SEPARATOR . 'fusionpbx' . DIRECTORY_SEPARATOR . 'config.conf')) {
$file = getenv('SystemDrive') . DIRECTORY_SEPARATOR . 'ProgramData' . DIRECTORY_SEPARATOR . 'fusionpbx' . DIRECTORY_SEPARATOR . 'config.conf';
}
elseif (file_exists(dirname(__DIR__, 2) . "/resources/config.php")) {
//use the current web directory to find it as a last resort
$file = "/var/www/fusionpbx/resources/config.php";
}
return $file;
}
/**
* Get a configuration value using a key in the configuration file
* @param string $key Match key on the left hand side of the '=' in the config file. If $key is null the default value is returned
* @param string|null $default_value if no matching key is found, then this value will be returned
* @return string|null returns a value in the config.conf file or an empty string
*/
public function get(string $key, ?string $default_value = ''): ?string {
if (!empty($this->__get($key))) {
return $this->__get($key);
}
return $default_value;
}
/**
* Returns the config path or an empty string
* @return string
*/
public function path(): string {
return dirname($this->file);
}
/**
* Returns the file name only of the configuration file
* @return string
*/
public function filename(): string {
return basename($this->file);
}
/**
* Returns the path and the file name
* @return string
*/
public function path_and_filename(): string {
return $this->file;
}
/**
* Returns if the config class has a loaded configuration or not
* @return bool True if configuration has loaded and false if it is empty
*/
public function is_empty(): bool {
return count($this->configuration) === 0;
}
/**
* Returns the array of configuration settings
* @return array
*/
public function configuration(): array {
return $this->configuration;
}
/**
* Ensures the configuration file is loaded only once
* @return config
*/
public static function load(string $file = ''): config {
if (self::$config === null) {
self::$config = new config($file);
}
return self::$config;
}
}
/*
//Examples:
//~~~~~~~~
$config = new config;
echo "Config path: " . $config->path() . "\n";
echo "Config file: " . $config->filename() . "\n";
echo "Full path and filename: " . $config->path_and_filename() . "\n";
// show old style configuration options
echo "db_type: ".$config->db_type."\n";
echo "db_name: ".$config->db_name."\n";
echo "db_username: ".$config->db_username."\n";
echo "db_password: ".$config->db_password."\n";
echo "db_host: ".$config->db_host."\n";
echo "db_path: ".$config->db_path."\n";
echo "db_port: ".$config->db_port."\n";
// use current style configuration options even on old config.php
echo "database.0.type: " . $config->get('database.0.type') . "\n";
// use a default value
echo "admin.name: " . $config->value('admin.name', 'admin') . "\n";
// get all configuration options by printing the object
echo "config settings: " . $config . "\n";
// save the configuration options to a file
file_put_contents('/etc/fusionpbx/config.conf', $config);
// get all configuration options as an array
var_dump($config->configuration());
//*/