Provision class use settings and database object

Credit for these changes go to frytimo changes curated by Mark
This commit is contained in:
FusionPBX
2024-06-26 21:23:24 -06:00
committed by GitHub
parent 71334c9655
commit 315a3d322f

View File

@@ -17,7 +17,7 @@
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
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 "<br/>device disabled<br/>";
}
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<br/>";
echo "template_dir: $template_dir<br/>";
echo "device_template: $device_template<br/>";
@@ -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