Update database.php

An improvement to make the database class more robust.
This commit is contained in:
FusionPBX 2016-11-27 00:16:18 -07:00 committed by GitHub
parent 8c138e3ae4
commit de3f56fc20
1 changed files with 448 additions and 389 deletions

View File

@ -204,16 +204,20 @@ include "root.php";
$prep_statement->execute();
$tmp = $prep_statement->fetchAll(PDO::FETCH_NAMED);
if ($this->type == "pgsql" || $this->type == "sqlite" || $this->type == "mssql") {
if (is_array($tmp)) {
foreach ($tmp as &$row) {
$result[]['name'] = $row['name'];
}
}
}
if ($this->type == "mysql") {
if (is_array($tmp)) {
foreach ($tmp as &$row) {
$table_array = array_values($row);
$result[]['name'] = $table_array[0];
}
}
}
return $result;
}
@ -268,25 +272,33 @@ include "root.php";
//set the list of fields
if ($this->type == "sqlite") {
if (is_array($table_info)) {
foreach($table_info as $row) {
$result[]['name'] = $row['name'];
}
}
}
if ($this->type == "pgsql") {
if (is_array($table_info)) {
foreach($table_info as $row) {
$result[]['name'] = $row['column_name'];
}
}
}
if ($this->type == "mysql") {
if (is_array($table_info)) {
foreach($table_info as $row) {
$result[]['name'] = $row['Field'];
}
}
}
if ($this->type == "mssql") {
if (is_array($table_info)) {
foreach($table_info as $row) {
$result[]['name'] = $row['COLUMN_NAME'];
}
}
}
//return the result array
return $result;
@ -312,6 +324,7 @@ include "root.php";
$sql = "select * from ".$this->table." ";
if ($this->where) {
$i = 0;
if (is_array($this->where)) {
foreach($this->where as $row) {
if ($i == 0) {
$sql .= 'where '.$row['name']." ".$row['operator']." '".$row['value']."' ";
@ -322,9 +335,11 @@ include "root.php";
$i++;
}
}
}
if (count($this->order_by) > 0) {
$sql .= "order by ";
$i = 1;
if (is_array($this->order_by)) {
foreach($this->order_by as $row) {
if (count($this->order_by) == $i) {
$sql .= $row['name']." ".$row['order']." ";
@ -335,6 +350,7 @@ include "root.php";
$i++;
}
}
}
if ($this->limit) {
$sql .= " limit ".$this->limit." offset ".$this->offset." ";
}
@ -377,6 +393,7 @@ include "root.php";
$sql = "insert into ".$this->table;
$sql .= " (";
$i = 1;
if (is_array($this->fields)) {
foreach($this->fields as $name => $value) {
if (count($this->fields) == $i) {
$sql .= $name." ";
@ -386,10 +403,12 @@ include "root.php";
}
$i++;
}
}
$sql .= ") ";
$sql .= "values ";
$sql .= "(";
$i = 1;
if (is_array($this->fields)) {
foreach($this->fields as $name => $value) {
if (count($this->fields) == $i) {
if (strlen($value) > 0) {
@ -409,6 +428,7 @@ include "root.php";
}
$i++;
}
}
$sql .= ")";
//execute the query, show exceptions
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@ -438,6 +458,7 @@ include "root.php";
//udate the database
$sql = "update ".$this->table." set ";
$i = 1;
if (is_array($this->fields)) {
foreach($this->fields as $name => $value) {
if (count($this->fields) == $i) {
if (strlen($name) > 0 && $value == null) {
@ -457,7 +478,9 @@ include "root.php";
}
$i++;
}
}
$i = 0;
if (is_array($this->where)) {
foreach($this->where as $row) {
if ($i == 0) {
$sql .= 'where '.$row['name']." ".$row['operator']." '".$row['value']."' ";
@ -467,6 +490,7 @@ include "root.php";
}
$i++;
}
}
$this->db->exec(check_sql($sql));
unset($this->fields);
unset($this->where);
@ -482,8 +506,8 @@ include "root.php";
//delete from the database
if (isset($this->table) && isset($this->where)) {
$sql = "delete from ".$this->table." ";
if ($this->where) {
$i = 0;
if (is_array($this->where)) {
foreach($this->where as $row) {
if ($i == 0) {
$sql .= "where ".$row['name']." ".$row['operator']." '".$row['value']."' ";
@ -534,11 +558,14 @@ include "root.php";
//$config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/$schema_name/app_config.php");
/*
$x = 0;
if (is_array($config_list)) {
foreach ($config_list as &$config_path) {
include($config_path);
$x++;
}
}
$tables = $apps[0]['db'];
if (is_array($tables)) {
foreach ($tables as &$row) {
//print_r($row);
$table = $row['table'];
@ -549,13 +576,16 @@ include "root.php";
}
}
}
}
*/
//-------------------
//loop through the array
if (is_array($new_array)) {
foreach ($new_array as $schema_name => $schema_array) {
$this->name = $schema_name;
if (is_array($schema_array)) {
foreach ($schema_array as $schema_id => $array) {
//set the variables
@ -582,11 +612,13 @@ include "root.php";
//get the parent field names
$parent_field_names = array();
if (is_array($array)) {
foreach ($array as $key => $value) {
if (!is_array($value)) {
$parent_field_names[] = $key;
}
}
}
//get the data before the delete
if ($parent_key_exists) {
@ -684,11 +716,11 @@ include "root.php";
unset($sql, $action);
//child data
if (is_array($array)) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$table_name = "v_".$key;
foreach ($value as $id => $row) {
//prepare the variables
$child_name = $this->singular($key);
@ -702,6 +734,7 @@ include "root.php";
//determine if the uuid exists
$uuid_exists = false;
if (is_array($row)) {
foreach ($row as $k => $v) {
if ($child_key_name == $k) {
if (strlen($v) > 0) {
@ -714,14 +747,17 @@ include "root.php";
$uuid_exists = false;
}
}
}
//get the child field names
$child_field_names = array();
if (is_array($row)) {
foreach ($row as $k => $v) {
if (!is_array($v)) {
$child_field_names[] = $k;
}
}
}
//get the child data
if ($uuid_exists) {
@ -811,12 +847,16 @@ include "root.php";
} //is array
} //foreach array
} //is_array array
} // foreach schema_array
} //is_array $schema_array
} // foreach main array
}
//return the before and after data
//log this in the future
if (is_array($old_array)) {
//if (is_array($old_array)) {
//normalize the array structure
//$old_array = $this->normalize_array($old_array, $this->name);
@ -825,7 +865,7 @@ include "root.php";
//print_r($old_array);
//echo "</pre>\n";
//exit;
}
//}
//$message["new"] = $new_array;
//$message["new"]["md5"] = md5(json_encode($new_array));
$this->message = $message;
@ -886,6 +926,7 @@ include "root.php";
$sql = "select count(*) as num_rows from ".$this->table." ";
if ($this->where) {
$i = 0;
if (is_array($this->where)) {
foreach($this->where as $row) {
if ($i == 0) {
$sql .= "where ".$row['name']." ".$row['operator']." '".$row['value']."' ";
@ -896,6 +937,7 @@ include "root.php";
$i++;
}
}
}
unset($this->where);
$prep_statement = $this->db->prepare(check_sql($sql));
if ($prep_statement) {
@ -972,8 +1014,8 @@ include "root.php";
}
else {
//where
if (is_array($array['where'])) {
$i = 0;
if (is_array($array)) {
foreach($array['where'] as $row) {
if ($i == 0) {
$sql .= "WHERE ".$row['name']." ".$row['operator']." '".$row['value']."' ";
@ -1099,12 +1141,12 @@ include "root.php";
//print_r($new_array);
//echo "</pre>\n";
//exit;
//------------------------------------------
//loop through the array
foreach ($new_array as $schema_name => $schema_array) {
if (is_array($new_array)) foreach ($new_array as $schema_name => $schema_array) {
$this->name = $schema_name;
foreach ($schema_array as $schema_id => $array) {
if (is_array($schema_array)) foreach ($schema_array as $schema_id => $array) {
//set the variables
$table_name = "v_".$this->name;
@ -1130,7 +1172,7 @@ include "root.php";
//get the parent field names
$parent_field_names = array();
foreach ($array as $key => $value) {
if (is_array($array)) foreach ($array as $key => $value) {
if (!is_array($value)) {
$parent_field_names[] = $key;
}
@ -1182,7 +1224,7 @@ include "root.php";
//foreach ($parent_field_names as $field_name) {
// $sql .= check_str($field_name).", ";
//}
foreach ($array as $array_key => $array_value) {
if (is_array($array)) foreach ($array as $array_key => $array_value) {
if (!is_array($array_value)) {
$sql .= check_str($array_key).", ";
}
@ -1193,7 +1235,7 @@ include "root.php";
if (!$parent_key_exists) {
$sql .= "'".$parent_key_value."', ";
}
foreach ($array as $array_key => $array_value) {
if (is_array($array)) foreach ($array as $array_key => $array_value) {
if (!is_array($array_value)) {
if (strlen($array_value) == 0) {
$sql .= "null, ";
@ -1251,6 +1293,7 @@ include "root.php";
//parent data
$sql = "UPDATE v_".$this->name." SET ";
if (is_array($array)) {
foreach ($array as $array_key => $array_value) {
if (!is_array($array_value) && $array_key != $parent_key_name) {
if (strlen($array_value) == 0) {
@ -1261,6 +1304,7 @@ include "root.php";
}
}
}
}
$sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' ";
$sql = str_replace(", WHERE", " WHERE", $sql);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@ -1307,11 +1351,10 @@ include "root.php";
unset($sql, $action);
//child data
foreach ($array as $key => $value) {
if (is_array($array)) foreach ($array as $key => $value) {
if (is_array($value)) {
$table_name = "v_".$key;
foreach ($value as $id => $row) {
//prepare the variables
$child_name = $this->singular($key);
@ -1325,7 +1368,7 @@ include "root.php";
//determine if the uuid exists
$uuid_exists = false;
foreach ($row as $k => $v) {
if (is_array($row)) foreach ($row as $k => $v) {
if ($child_key_name == $k) {
if (strlen($v) > 0) {
$child_key_value = $v;
@ -1340,7 +1383,7 @@ include "root.php";
//get the child field names
$child_field_names = array();
foreach ($row as $k => $v) {
if (is_array($row)) foreach ($row as $k => $v) {
if (!is_array($v)) {
$child_field_names[] = $k;
}
@ -1377,6 +1420,7 @@ include "root.php";
if ($action == "update") {
if (permission_exists($child_name.'_edit')) {
$sql = "UPDATE ".$table_name." SET ";
if (is_array($row)) {
foreach ($row as $k => $v) {
//if (!is_array($v) && $k != $child_key_name) { //original
if (!is_array($v) && ($k != $parent_key_name || $k != $child_key_name)) {
@ -1388,6 +1432,7 @@ include "root.php";
}
}
}
}
$sql .= "WHERE ".$parent_key_name." = '".$this->uuid."' ";
$sql .= "AND ".$child_key_name." = '".$child_key_value."' ";
$sql = str_replace(", WHERE", " WHERE", $sql);
@ -1436,6 +1481,7 @@ include "root.php";
$child_key_name = $this->singular($child_name).'_uuid';
$parent_key_exists = false;
$child_key_exists = false;
if (is_array($row)) {
foreach ($row as $k => $v) {
if ($k == $parent_key_name) {
$parent_key_exists = true;
@ -1445,6 +1491,7 @@ include "root.php";
$child_key_value = $v;
}
}
}
if (!$child_key_value) {
$child_key_value = uuid();
}
@ -1457,11 +1504,13 @@ include "root.php";
if (!$child_key_exists) {
$sql .= $this->singular($child_key_name).", ";
}
if (is_array($row)) {
foreach ($row as $k => $v) {
if (!is_array($v)) {
$sql .= check_str($k).", ";
}
}
}
$sql .= ") ";
$sql .= "VALUES ";
$sql .= "(";
@ -1471,6 +1520,7 @@ include "root.php";
if (!$child_key_exists) {
$sql .= "'".$child_key_value."', ";
}
if (is_array($row)) {
foreach ($row as $k => $v) {
if (!is_array($v)) {
if (strlen($v) == 0) {
@ -1481,6 +1531,7 @@ include "root.php";
}
}
}
}
$sql .= ");";
$sql = str_replace(", )", ")", $sql);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@ -1533,7 +1584,7 @@ include "root.php";
//return the before and after data
//log this in the future
if (is_array($old_array)) {
//if (is_array($old_array)) {
//normalize the array structure
//$old_array = $this->normalize_array($old_array, $this->name);
@ -1542,7 +1593,7 @@ include "root.php";
//print_r($old_array);
//echo "</pre>\n";
//exit;
}
//}
//$message["new"] = $new_array;
//$message["new"]["md5"] = md5(json_encode($new_array));
$this->message = $message;
@ -1626,10 +1677,12 @@ include "root.php";
//get the $apps array from the installed apps from the core and mod directories
$config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
$x = 0;
if (is_array($config_list)) {
foreach ($config_list as &$config_path) {
include($config_path);
$x++;
}
}
$_SESSION['apps'] = $apps;
}
@ -1654,17 +1707,24 @@ include "root.php";
$this->get_apps();
}
//search through all fields to see if domain_uuid exists
foreach ($_SESSION['apps'] as $x => &$app) {
$apps = $_SESSION['apps'];
if (is_array($apps)) {
foreach ($apps as $x => &$app) {
if (is_array($app['db'])) {
foreach ($app['db'] as $y => &$row) {
if ($row['table'] == $name) {
if (is_array($row['fields'])) {
foreach ($row['fields'] as $z => $field) {
if ($field['name'] == "domain_uuid") {
return true;
}
} //foreach
} //is array
}
}
}
}
} //foreach
} //is array
} //foreach
} //is array
//not found
return false;
}
@ -1703,7 +1763,6 @@ include "root.php";
}
}
/*
//example usage
//find