From 315a3d322f37a57b05382474fcb5fc2ef7c34f68 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Wed, 26 Jun 2024 21:23:24 -0600 Subject: [PATCH] Provision class use settings and database object Credit for these changes go to frytimo changes curated by Mark --- app/provision/resources/classes/provision.php | 141 ++++++++++-------- 1 file changed, 77 insertions(+), 64 deletions(-) diff --git a/app/provision/resources/classes/provision.php b/app/provision/resources/classes/provision.php index 5e020d1a58..71192e000d 100644 --- a/app/provision/resources/classes/provision.php +++ b/app/provision/resources/classes/provision.php @@ -17,7 +17,7 @@ The Initial Developer of the Original Code is Mark J Crane - Copyright (C) 2014-2023 + Copyright (C) 2014-2024 All Rights Reserved. Contributor(s): @@ -33,8 +33,48 @@ public $template_dir; public $device_address; public $device_template; + private $settings; + private $database; public function __construct() { + + //preset the the values + $database = null; + $settings = null; + $domain_uuid = null; + + //use the parameters to set the values if they exist + if (isset($params['database'])) { + $database = $params['database']; + } + if (isset($params['settings'])) { + $settings = $params['settings']; + } + if (isset($params['domain_uuid'])) { + $domain_uuid = $params['domain_uuid']; + } + + //check if we can use the settings object to get the database object + if (!empty($settings) && empty($database)) { + $database = $settings->database(); + } + + //fill in missing + if (empty($database)) { + $database = database::new(); + } + if (empty($settings)) { + $settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid]); + } + + //assign to the object + $this->database = $database; + $this->settings = $settings; + $this->domain_uuid = $domain_uuid; + + //get the project root + $project_root = dirname(__DIR__, 4); + //set the default template directory if (PHP_OS == "Linux") { //set the default template dir @@ -117,37 +157,26 @@ switch (strtolower($vendor)) { case "algo": return strtoupper($device_address); - break; case "aastra": return strtoupper($device_address); - break; case "cisco": return strtoupper($device_address); - break; case "linksys": return strtolower($device_address); - break; case "mitel": return strtoupper($device_address); - break; case "polycom": return strtolower($device_address); - break; case "snom": return strtolower($device_address); - break; case "escene": return strtolower($device_address); - break; case "grandstream": return strtolower($device_address); - break; case "yealink": return strtolower($device_address); - break; case "gigaset": return strtoupper($device_address); - break; default: return strtolower($device_address); } @@ -256,6 +285,7 @@ //get the variables $domain_uuid = $this->domain_uuid; + $domain_name = $this->domain_name; $device_template = $this->device_template; $template_dir = $this->template_dir; $device_address = $this->device_address; @@ -287,27 +317,14 @@ } //build the provision array - $provision = array(); - if (is_array($_SESSION['provision'])) { - foreach ($_SESSION['provision'] as $key => $val) { - if (isset($val['var'])) { $value = $val['var']; } - elseif (isset($val['text'])) { $value = $val['text']; } - elseif (isset($val['boolean'])) { $value = $val['boolean']; } - elseif (isset($val['numeric'])) { $value = $val['numeric']; } - elseif (isset($val) && is_array($val) && empty($val['uuid'])) { $value = $val; } - if (isset($value)) { - $value = str_replace('${domain_name}', $domain_name, $value); - $value = str_replace('${mac_address}', $device_address, $value); - $value = str_replace('${device_address}', $device_address, $value); - $provision[$key] = $value; - } - unset($value); - } + $provision = $this->settings->get('provision', null, []); + foreach ($provision as $key => $value) { + $provision[$key] = $value; } //add the http auth password to the array - if (isset($_SESSION['provision']["http_auth_password"]) && is_array($_SESSION['provision']["http_auth_password"])) { - $provision["http_auth_password"] = $_SESSION['provision']["http_auth_password"][0]; + if (isset($provision["http_auth_password"]) && is_array($provision["http_auth_password"])) { + $provision["http_auth_password"] = $provision["http_auth_password"][0]; } //check to see if the device_address exists in devices @@ -317,7 +334,7 @@ //get the device_template $sql = "select * from v_devices "; $sql .= "where device_address = :device_address "; - if ($provision['http_domain_filter'] == "true") { + if ($this->settings->get('provision','http_domain_filter', 'false') === "true") { $sql .= "and domain_uuid=:domain_uuid "; $parameters['domain_uuid'] = $domain_uuid; } @@ -330,7 +347,7 @@ //checks either device enabled if ($row['device_enabled'] != 'true') { - if ($_SESSION['provision']['debug']['boolean'] == 'true') { + if ($this->settings->get('provision','debug', 'false') === 'true') { echo "
device disabled
"; } else { @@ -524,7 +541,7 @@ unset($templates); //device address does not exist in the table so add it - if ($_SESSION['provision']['auto_insert_enabled']['boolean'] == "true" && is_uuid($domain_uuid)) { + if ($this->settings->get('provision','auto_insert_enabled','false') === "true") { //get a new primary key $device_uuid = uuid(); @@ -655,12 +672,7 @@ //initialize a template object $view = new template(); - if (!empty($_SESSION['provision']['template_engine']['text'])) { - $view->engine = $_SESSION['provision']['template_engine']['text']; //raintpl, smarty, twig - } - else { - $view->engine = "smarty"; - } + view->engine = $this->settings->get('provision', 'template_engine', 'smarty'); $view->template_dir = $template_dir ."/".$device_template."/"; $view->cache_dir = sys_get_temp_dir(); $view->init(); @@ -887,14 +899,14 @@ //get the list of contact directly assigned to the user if (is_uuid($domain_uuid)) { - if ($_SESSION['contact']['permissions']['boolean'] == "true") { + if ($this->settings->get('contact','permissions','false') === "true") { //get the contacts assigned to the groups and add to the contacts array - if (is_uuid($device_user_uuid) && $_SESSION['provision']['contact_groups']['boolean'] == "true") { + if (is_uuid($device_user_uuid) && $this->settings->get('contact','contact_groups', 'false') === "true") { $this->contact_append($contacts, $line, $domain_uuid, $device_user_uuid, 'groups'); } //get the contacts assigned to the user and add to the contacts array - if (is_uuid($device_user_uuid) && $_SESSION['provision']['contact_users']['boolean'] == "true") { + if (is_uuid($device_user_uuid) && $this->settings->get('contact','contact_users', 'false') === "true") { $this->contact_append($contacts, $line, $domain_uuid, $device_user_uuid, 'users'); } } @@ -905,7 +917,7 @@ } //get the extensions and add them to the contacts array - if (is_uuid($device_uuid) && is_uuid($domain_uuid) && !empty($_SESSION['provision']['contact_extensions']['boolean']) && $_SESSION['provision']['contact_extensions']['boolean'] == "true") { + if (is_uuid($device_uuid) && is_uuid($domain_uuid) && $this->settings->get('provision','contact_extensions','false') === "true") { //get contacts from the database $sql = "select extension_uuid as contact_uuid, directory_first_name, directory_last_name, "; $sql .= "effective_caller_id_name, effective_caller_id_number, "; @@ -1119,13 +1131,8 @@ //set the device address in the correct format $device_address = $this->format_address($device_address, $device_vendor); - // set date/time for versioning provisioning templates - if (!empty($_SESSION['provision']['version_format']['text'])) { - $time = date($_SESSION['provision']['version_format']['text']); - } - else { - $time = date("dmyHi"); - } + //set date/time for versioning provisioning templates + $time = date($this->settings->get('provision','version_format', "dmyHi")); //replace the variables in the template in the future loop through all the line numbers to do a replace for each possible line number $view->assign("device_address", $device_address); @@ -1153,13 +1160,13 @@ $parameters['device_user_uuid'] = $device_user_uuid; $database = new database; $contact_uuid = $database->select($sql, $parameters, 'column'); - $view->assign("ldap_username","uid=".$contact_uuid.",".$_SESSION['provision']['grandstream_ldap_user_base']['text']); + $view->assign("ldap_username", "uid=" . $contact_uuid . "," . $this->settings->get('provision','grandstream_ldap_user_base', '')); $view->assign("ldap_password",md5($laddr_salt.$device_user_uuid)); unset($parameters); } //get the time zone - $time_zone_name = $_SESSION['domain']['time_zone']['name']; + $time_zone_name = $this->settings->get('domain','time_zone', ''); if (!empty($time_zone_name)) { $time_zone_offset_raw = get_time_zone_offset($time_zone_name)/3600; $time_zone_offset_hours = floor($time_zone_offset_raw); @@ -1220,7 +1227,7 @@ //make sure the file exists if (!file_exists($template_dir."/".$device_template ."/".$file)) { $this->http_error('404'); - if ($_SESSION['provision']['debug']['boolean'] == 'true') { + if ($this->settings->get('provision','debug','false') === 'true') { echo ":$template_dir/$device_template/$file
"; echo "template_dir: $template_dir
"; echo "device_template: $device_template
"; @@ -1234,7 +1241,7 @@ $file_contents = $view->render($file); //log file for testing - if (!empty($_SESSION['provision']['debug']['boolean']) && $_SESSION['provision']['debug']['boolean'] == 'true') { + if ($this->settings->get('provision','debug','false') === 'true') { $tmp_file = "/tmp/provisioning_log.txt"; $fh = fopen($tmp_file, 'w') or die("can't open file"); $tmp_string = $device_address."\n"; @@ -1250,17 +1257,23 @@ function write() { //build the provision array - $provision = array(); - if (is_array($_SESSION['provision'])) { - foreach ($_SESSION['provision'] as $key => $val) { - if (isset($val['var'])) { $value = $val['var']; } - elseif (isset($val['text'])) { $value = $val['text']; } - elseif (isset($val['boolean'])) { $value = $val['boolean']; } - elseif (isset($val['numeric'])) { $value = $val['numeric']; } - elseif (is_array($val) && !is_uuid($val['uuid'])) { $value = $val; } - if (isset($value)) { $provision[$key] = $value; } - unset($value); + $provision = $this->settings->get('provision', null, []); + foreach ($provision as $key => $val) { + if (isset($val['var'])) { + $value = $val['var']; + } elseif (isset($val['text'])) { + $value = $val['text']; + } elseif (isset($val['boolean'])) { + $value = $val['boolean']; + } elseif (isset($val['numeric'])) { + $value = $val['numeric']; + } elseif (is_array($val) && !is_uuid($val['uuid'])) { + $value = $val; } + if (isset($value)) { + $provision[$key] = $value; + } + unset($value); } //check either we have destination path to write files