Improved database connection handling

This commit is contained in:
FusionPBX
2024-08-29 04:04:04 -06:00
committed by GitHub
parent 273a45bbb6
commit f6ce98ad45
+75 -76
View File
@@ -17,7 +17,7 @@
The Initial Developer of the Original Code is The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
Copyright (C) 2010 - 2019 Copyright (C) 2010 - 2024
All Rights Reserved. All Rights Reserved.
Contributor(s): Contributor(s):
@@ -45,11 +45,32 @@
private $toggle_field; private $toggle_field;
private $toggle_values; private $toggle_values;
public function __construct() { /**
* Set in the constructor. Must be a database object and cannot be null.
* @var database Database Object
*/
private $database;
/**
* Create a settings object using key/value pairs in the $setting_array.
*
* Valid values are: database.
* @param array setting_array
* @depends database::new()
* @access public
*/
public function __construct($setting_array = []) {
//open a database connection
if (empty($setting_array['database'])) {
$this->database = database::new();
} else {
$this->database = $setting_array['database'];
}
//assign private variables //assign private variables
$this->app_name = 'devices'; $this->app_name = 'devices';
$this->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e'; $this->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e';
} }
@@ -477,8 +498,7 @@
if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) { if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
$sql = "update v_devices set device_uuid_alternate = null where device_uuid_alternate = :device_uuid_alternate; "; $sql = "update v_devices set device_uuid_alternate = null where device_uuid_alternate = :device_uuid_alternate; ";
$parameters['device_uuid_alternate'] = $record['uuid']; $parameters['device_uuid_alternate'] = $record['uuid'];
$database = new database; $this->database->execute($sql, $parameters);
$database->execute($sql, $parameters);
unset($sql, $parameters); unset($sql, $parameters);
$array[$this->table][$x][$this->uuid_prefix.'uuid'] = $record['uuid']; $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $record['uuid'];
@@ -498,10 +518,9 @@
$p->add('device_key_delete', 'temp'); $p->add('device_key_delete', 'temp');
//execute delete //execute delete
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
//revoke temporary permissions //revoke temporary permissions
@@ -561,10 +580,9 @@
//delete the checked rows //delete the checked rows
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
//execute delete //execute delete
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
} }
unset($records); unset($records);
@@ -608,10 +626,9 @@
//delete the checked rows //delete the checked rows
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
//execute delete //execute delete
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
} }
unset($records); unset($records);
@@ -655,10 +672,9 @@
//delete the checked rows //delete the checked rows
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
//execute delete //execute delete
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
} }
unset($records); unset($records);
@@ -711,10 +727,9 @@
$p->add('device_vendor_function_group_delete', 'temp'); $p->add('device_vendor_function_group_delete', 'temp');
//execute delete //execute delete
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
//revoke temporary permissions //revoke temporary permissions
@@ -773,10 +788,9 @@
$p->add('device_vendor_function_group_delete', 'temp'); $p->add('device_vendor_function_group_delete', 'temp');
//execute delete //execute delete
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
//revoke temporary permissions //revoke temporary permissions
@@ -836,10 +850,9 @@
$p->add('device_profile_setting_delete', 'temp'); $p->add('device_profile_setting_delete', 'temp');
//execute delete //execute delete
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
//revoke temporary permissions //revoke temporary permissions
@@ -889,10 +902,9 @@
//execute delete //execute delete
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
} }
unset($records); unset($records);
@@ -935,10 +947,9 @@
//execute delete //execute delete
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->delete($array);
$database->delete($array);
unset($array); unset($array);
} }
unset($records); unset($records);
@@ -988,8 +999,7 @@
$sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) "; $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
$sql .= "and ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") "; $sql .= "and ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") ";
$parameters['domain_uuid'] = $_SESSION['domain_uuid']; $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
$database = new database; $rows = $this->database->select($sql, $parameters, 'all');
$rows = $database->select($sql, $parameters, 'all');
if (is_array($rows) && @sizeof($rows) != 0) { if (is_array($rows) && @sizeof($rows) != 0) {
foreach ($rows as $row) { foreach ($rows as $row) {
$states[$row['uuid']] = $row['toggle']; $states[$row['uuid']] = $row['toggle'];
@@ -1010,10 +1020,9 @@
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
//save the array //save the array
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->save($array);
$database->save($array);
unset($array); unset($array);
//write the provision files //write the provision files
@@ -1068,8 +1077,7 @@
if (is_array($uuids) && @sizeof($uuids) != 0) { if (is_array($uuids) && @sizeof($uuids) != 0) {
$sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." "; $sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
$sql .= "where ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") "; $sql .= "where ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") ";
$database = new database; $rows = $this->database->select($sql, $parameters ?? null, 'all');
$rows = $database->select($sql, $parameters ?? null, 'all');
if (is_array($rows) && @sizeof($rows) != 0) { if (is_array($rows) && @sizeof($rows) != 0) {
foreach ($rows as $row) { foreach ($rows as $row) {
$states[$row['uuid']] = $row['toggle']; $states[$row['uuid']] = $row['toggle'];
@@ -1090,10 +1098,9 @@
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
//save the array //save the array
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->save($array);
$database->save($array);
unset($array); unset($array);
//set message //set message
@@ -1141,8 +1148,7 @@
if (is_array($uuids) && @sizeof($uuids) != 0) { if (is_array($uuids) && @sizeof($uuids) != 0) {
$sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." "; $sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
$sql .= "where ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") "; $sql .= "where ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") ";
$database = new database; $rows = $this->database->select($sql, $parameters ?? null, 'all');
$rows = $database->select($sql, $parameters ?? null, 'all');
if (is_array($rows) && @sizeof($rows) != 0) { if (is_array($rows) && @sizeof($rows) != 0) {
foreach ($rows as $row) { foreach ($rows as $row) {
$states[$row['uuid']] = $row['toggle']; $states[$row['uuid']] = $row['toggle'];
@@ -1163,10 +1169,9 @@
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
//save the array //save the array
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->save($array);
$database->save($array);
unset($array); unset($array);
//set message //set message
@@ -1214,8 +1219,7 @@
if (!empty($uuids) && is_array($uuids) && @sizeof($uuids) != 0) { if (!empty($uuids) && is_array($uuids) && @sizeof($uuids) != 0) {
$sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." "; $sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
$sql .= "where ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") "; $sql .= "where ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") ";
$database = new database; $rows = $this->database->select($sql, $parameters ?? null, 'all');
$rows = $database->select($sql, $parameters ?? null, 'all');
if (is_array($rows) && @sizeof($rows) != 0) { if (is_array($rows) && @sizeof($rows) != 0) {
foreach ($rows as $row) { foreach ($rows as $row) {
$states[$row['uuid']] = $row['toggle']; $states[$row['uuid']] = $row['toggle'];
@@ -1238,10 +1242,9 @@
if (!empty($array) && is_array($array) && @sizeof($array) != 0) { if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
//save the array //save the array
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->database->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->save($array);
$database->save($array);
unset($array); unset($array);
//set message //set message
@@ -1294,8 +1297,7 @@
$sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) "; $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
$sql .= "and ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") "; $sql .= "and ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") ";
$parameters['domain_uuid'] = $_SESSION['domain_uuid']; $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
$database = new database; $rows = $this->database->select($sql, $parameters, 'all');
$rows = $database->select($sql, $parameters, 'all');
if (is_array($rows) && @sizeof($rows) != 0) { if (is_array($rows) && @sizeof($rows) != 0) {
$y = $z = 0; $y = $z = 0;
foreach ($rows as $x => $row) { foreach ($rows as $x => $row) {
@@ -1320,8 +1322,7 @@
$sql_2 .= "else 100 end, "; $sql_2 .= "else 100 end, ";
$sql_2 .= "profile_key_id asc "; $sql_2 .= "profile_key_id asc ";
$parameters_2['device_profile_uuid'] = $row['device_profile_uuid']; $parameters_2['device_profile_uuid'] = $row['device_profile_uuid'];
$database = new database; $rows_2 = $this->database->select($sql_2, $parameters_2, 'all');
$rows_2 = $database->select($sql_2, $parameters_2, 'all');
if (is_array($rows_2) && @sizeof($rows_2) != 0) { if (is_array($rows_2) && @sizeof($rows_2) != 0) {
foreach ($rows_2 as $row_2) { foreach ($rows_2 as $row_2) {
@@ -1342,8 +1343,7 @@
//settings sub table //settings sub table
$sql_3 = "select * from v_device_profile_settings where device_profile_uuid = :device_profile_uuid"; $sql_3 = "select * from v_device_profile_settings where device_profile_uuid = :device_profile_uuid";
$parameters_3['device_profile_uuid'] = $row['device_profile_uuid']; $parameters_3['device_profile_uuid'] = $row['device_profile_uuid'];
$database = new database; $rows_3 = $this->database->select($sql_3, $parameters_3, 'all');
$rows_3 = $database->select($sql_3, $parameters_3, 'all');
if (is_array($rows_3) && @sizeof($rows_3) != 0) { if (is_array($rows_3) && @sizeof($rows_3) != 0) {
foreach ($rows_3 as $row_3) { foreach ($rows_3 as $row_3) {
@@ -1375,10 +1375,9 @@
$p->add('device_profile_setting_add', 'temp'); $p->add('device_profile_setting_add', 'temp');
//save the array //save the array
$database = new database; $this->database->app_name = $this->app_name;
$database->app_name = $this->app_name; $this->databasee->app_uuid = $this->app_uuid;
$database->app_uuid = $this->app_uuid; $this->database->save($array);
$database->save($array);
unset($array); unset($array);
//revoke temporary permissions //revoke temporary permissions