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