Update database.php

This commit is contained in:
FusionPBX
2019-08-03 16:16:30 -06:00
committed by GitHub
parent c0f129d5de
commit 54396d3fe9
+227 -217
View File
@@ -1192,9 +1192,11 @@ 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) { if (is_array($array)) {
if (!is_array($value)) { foreach ($array as $key => $value) {
$parent_field_names[] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $key); if (!is_array($value)) {
$parent_field_names[] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $key);
}
} }
} }
@@ -1245,10 +1247,12 @@ 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).", ";
//} //}
if (is_array($array)) foreach ($array as $array_key => $array_value) { if (is_array($array)) {
if (!is_array($array_value)) { foreach ($array as $array_key => $array_value) {
$array_key = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array_key); if (!is_array($array_value)) {
$sql .= $array_key.", "; $array_key = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array_key);
$sql .= $array_key.", ";
}
} }
} }
$sql .= ") "; $sql .= ") ";
@@ -1257,18 +1261,20 @@ include "root.php";
if (!$parent_key_exists) { if (!$parent_key_exists) {
$sql .= "'".$parent_key_value."', "; $sql .= "'".$parent_key_value."', ";
} }
if (is_array($array)) foreach ($array as $array_key => $array_value) { if (is_array($array)) {
if (!is_array($array_value)) { foreach ($array as $array_key => $array_value) {
if (strlen($array_value) == 0) { if (!is_array($array_value)) {
$sql .= "null, "; if (strlen($array_value) == 0) {
} $sql .= "null, ";
elseif ($array_value === "now()") { }
$sql .= "now(), "; elseif ($array_value === "now()") {
} $sql .= "now(), ";
else { }
//$sql .= "'".check_str($array_value)."', "; else {
$sql .= ':'.$array_key.", "; //$sql .= "'".check_str($array_value)."', ";
$params[$array_key] = $array_value; $sql .= ':'.$array_key.", ";
$params[$array_key] = $array_value;
}
} }
} }
} }
@@ -1409,105 +1415,224 @@ include "root.php";
unset($sql, $action); unset($sql, $action);
//child data //child data
if (is_array($array)) 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;
$table_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $table_name); $table_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $table_name);
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);
$child_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $child_name); $child_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $child_name);
$child_key_name = $child_name."_uuid"; $child_key_name = $child_name."_uuid";
//determine if the parent key exists in the child array //determine if the parent key exists in the child array
$parent_key_exists = false; $parent_key_exists = false;
if (!isset($array[$parent_key_name])) { if (!isset($array[$parent_key_name])) {
$parent_key_exists = true; $parent_key_exists = true;
} }
//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) { 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;
$uuid_exists = true; $uuid_exists = true;
break; break;
}
}
else {
$uuid_exists = false;
} }
} }
//allow characters found in the uuid only.
$child_key_value = preg_replace('#[^a-zA-Z0-9_\-]#', '', $child_key_value);
//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[] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
}
}
}
//determine sql update or delete and get the original data
if ($uuid_exists) {
$sql = "SELECT ". implode(", ", $child_field_names)." FROM ".$table_name." ";
$sql .= "WHERE ".$child_key_name." = '".$child_key_value."' ";
$prep_statement = $this->db->prepare($sql);
if ($prep_statement) {
//get the data
$prep_statement->execute();
$child_array = $prep_statement->fetch(PDO::FETCH_ASSOC);
//set the action
if (is_array($child_array)) {
$action = "update";
}
else {
$action = "add";
}
//add to the parent array
if (is_array($child_array)) {
$old_array[$schema_name][$schema_id][$key][] = $child_array;
}
}
unset($prep_statement);
}
else { else {
$uuid_exists = false; $action = "add";
} }
}
//allow characters found in the uuid only. //update the child data
$child_key_value = preg_replace('#[^a-zA-Z0-9_\-]#', '', $child_key_value); if ($action == "update") {
if (permission_exists($child_name.'_edit')) {
//get the child field names $sql = "UPDATE ".$table_name." SET ";
$child_field_names = array(); if (is_array($row)) {
if (is_array($row)) foreach ($row as $k => $v) { foreach ($row as $k => $v) {
if (!is_array($v)) { if (!is_array($v) && ($k != $parent_key_name || $k != $child_key_name)) {
$child_field_names[] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k); $k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
} if (strlen($v) == 0) {
} $sql .= $k." = null, ";
}
//determine sql update or delete and get the original data elseif ($v === "now()") {
if ($uuid_exists) { $sql .= $k." = now(), ";
$sql = "SELECT ". implode(", ", $child_field_names)." FROM ".$table_name." "; }
$sql .= "WHERE ".$child_key_name." = '".$child_key_value."' "; else {
$prep_statement = $this->db->prepare($sql); //$sql .= "$k = '".check_str($v)."', ";
if ($prep_statement) { $sql .= $k." = :".$k.", ";
//get the data $params[$k] = $v;
$prep_statement->execute(); }
$child_array = $prep_statement->fetch(PDO::FETCH_ASSOC); }
//set the action }
if (is_array($child_array)) {
$action = "update";
} }
else { $sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' ";
$action = "add"; $sql .= "AND ".$child_key_name." = '".$child_key_value."' ";
} $sql = str_replace(", WHERE", " WHERE", $sql);
//add to the parent array $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (is_array($child_array)) {
$old_array[$schema_name][$schema_id][$key][] = $child_array;
}
}
unset($prep_statement);
}
else {
$action = "add";
}
//update the data //$prep_statement->bindParam(':domain_uuid', $this->domain_uuid );
if ($action == "update") {
if (permission_exists($child_name.'_edit')) { try {
$sql = "UPDATE ".$table_name." SET "; //$this->db->query(check_sql($sql));
$prep_statement = $this->db->prepare($sql);
$prep_statement->execute($params);
unset($prep_statement);
$message["details"][$m]["name"] = $key;
$message["details"][$m]["message"] = "OK";
$message["details"][$m]["code"] = "200";
$message["details"][$m]["uuid"] = $child_key_value;
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
if (is_array($params)) {
$message["details"][$m]["params"] = $params;
unset($params);
}
}
$this->message = $message;
$m++;
}
catch(PDOException $e) {
if ($message["code"] = "200") {
$message["message"] = "Bad Request";
$message["code"] = "400";
}
$message["details"][$m]["name"] = $key;
$message["details"][$m]["message"] = $e->getMessage();
$message["details"][$m]["code"] = "400";
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
if (is_array($params)) {
$message["details"][$m]["params"] = $params;
unset($params);
}
}
$this->message = $message;
$m++;
}
}
else {
$message["name"] = $child_name;
$message["message"] = "Forbidden, does not have '${child_name}_edit'";
$message["code"] = "403";
$message["line"] = __line__;
$this->message = $message;
$m++;
}
} //action update
//add the child data
if ($action == "add") {
if (permission_exists($child_name.'_add')) {
//determine if child or parent key exists
$child_key_name = $child_name.'_uuid';
$parent_key_exists = false;
$child_key_exists = false;
if (is_array($row)) { if (is_array($row)) {
foreach ($row as $k => $v) { foreach ($row as $k => $v) {
if (!is_array($v) && ($k != $parent_key_name || $k != $child_key_name)) { if ($k == $parent_key_name) {
$parent_key_exists = true;
}
if ($k == $child_key_name) {
$child_key_exists = true;
$child_key_value = $v;
}
}
}
if (!$child_key_value) {
$child_key_value = uuid();
}
//build the insert
$sql = "INSERT INTO ".$table_name." ";
$sql .= "(";
if (!$parent_key_exists) {
$sql .= $this->singular($parent_key_name).", ";
}
if (!$child_key_exists) {
$sql .= $this->singular($child_key_name).", ";
}
if (is_array($row)) {
foreach ($row as $k => $v) {
if (!is_array($v)) {
$k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k); $k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
$sql .= $k.", ";
}
}
}
$sql .= ") ";
$sql .= "VALUES ";
$sql .= "(";
if (!$parent_key_exists) {
$sql .= "'".$parent_key_value."', ";
}
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) { if (strlen($v) == 0) {
$sql .= $k." = null, "; $sql .= "null, ";
} }
elseif ($v === "now()") { elseif ($v === "now()") {
$sql .= $k." = now(), "; $sql .= "now(), ";
} }
else { else {
//$sql .= "$k = '".check_str($v)."', "; $k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
$sql .= $k." = :".$k.", "; //$sql .= "'".check_str($v)."', ";
$sql .= ':'.$k.", ";
$params[$k] = $v; $params[$k] = $v;
} }
} }
} }
} }
$sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' "; $sql .= ");";
$sql .= "AND ".$child_key_name." = '".$child_key_value."' "; $sql = str_replace(", )", ")", $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);
//$prep_statement->bindParam(':domain_uuid', $this->domain_uuid );
try { try {
//$this->db->query(check_sql($sql)); //$this->db->query(check_sql($sql));
$prep_statement = $this->db->prepare($sql); $prep_statement = $this->db->prepare($sql);
@@ -1548,136 +1673,21 @@ include "root.php";
} }
else { else {
$message["name"] = $child_name; $message["name"] = $child_name;
$message["message"] = "Forbidden, does not have '${child_name}_edit'"; $message["message"] = "Forbidden, does not have '${child_name}_add'";
$message["code"] = "403"; $message["code"] = "403";
$message["line"] = __line__; $message["line"] = __line__;
$this->message = $message; $this->message = $message;
$m++; $m++;
} }
} //action update } //action add
//add the data //unset the variables
if ($action == "add") { unset($sql, $action, $child_key_name, $child_key_value);
if (permission_exists($child_name.'_add')) { } // foreach value
//determine if child or parent key exists
$child_key_name = $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;
}
if ($k == $child_key_name) {
$child_key_exists = true;
$child_key_value = $v;
}
}
}
if (!$child_key_value) {
$child_key_value = uuid();
}
//build the insert
$sql = "INSERT INTO ".$table_name." ";
$sql .= "(";
if (!$parent_key_exists) {
$sql .= $this->singular($parent_key_name).", ";
}
if (!$child_key_exists) {
$sql .= $this->singular($child_key_name).", ";
}
if (is_array($row)) {
foreach ($row as $k => $v) {
if (!is_array($v)) {
$k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
$sql .= $k.", ";
}
}
}
$sql .= ") ";
$sql .= "VALUES ";
$sql .= "(";
if (!$parent_key_exists) {
$sql .= "'".$parent_key_value."', ";
}
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) {
$sql .= "null, ";
}
elseif ($v === "now()") {
$sql .= "now(), ";
}
else {
$k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
//$sql .= "'".check_str($v)."', ";
$sql .= ':'.$k.", ";
$params[$k] = $v;
}
}
}
}
$sql .= ");";
$sql = str_replace(", )", ")", $sql);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
//$this->db->query(check_sql($sql));
$prep_statement = $this->db->prepare($sql);
$prep_statement->execute($params);
unset($prep_statement);
$message["details"][$m]["name"] = $key;
$message["details"][$m]["message"] = "OK";
$message["details"][$m]["code"] = "200";
$message["details"][$m]["uuid"] = $child_key_value;
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
if (is_array($params)) {
$message["details"][$m]["params"] = $params;
unset($params);
}
}
$this->message = $message;
$m++;
}
catch(PDOException $e) {
if ($message["code"] = "200") {
$message["message"] = "Bad Request";
$message["code"] = "400";
}
$message["details"][$m]["name"] = $key;
$message["details"][$m]["message"] = $e->getMessage();
$message["details"][$m]["code"] = "400";
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
if (is_array($params)) {
$message["details"][$m]["params"] = $params;
unset($params);
}
}
$this->message = $message;
$m++;
}
}
else {
$message["name"] = $child_name;
$message["message"] = "Forbidden, does not have '${child_name}_add'";
$message["code"] = "403";
$message["line"] = __line__;
$this->message = $message;
$m++;
}
} //action add
//unset the variables } //is array
unset($sql, $action, $child_key_name, $child_key_value); } //foreach array
} // foreach value }
} //is array
} //foreach array
} // foreach schema_array } // foreach schema_array
} // foreach main array } // foreach main array