diff --git a/resources/classes/database.php b/resources/classes/database.php
index 07990355bd..b109362e3f 100644
--- a/resources/classes/database.php
+++ b/resources/classes/database.php
@@ -53,7 +53,7 @@ include "root.php";
public function connect() {
- if (strlen($this->type) == 0 && strlen($this->db_name) == 0) {
+ if (strlen($this->db_name) == 0) {
//include config.php
include "root.php";
if (file_exists($_SERVER["PROJECT_ROOT"]."/resources/config.php")) {
@@ -63,7 +63,7 @@ include "root.php";
} elseif (file_exists("/etc/fusionpbx/config.php")){
//linux
include "/etc/fusionpbx/config.php";
- } elseif (file_exists("/usr/local/etc/fusionpbx/config.php")){
+ } elseif (file_exists("/usr/local/etc/fusionpbx/config.php")) {
//bsd
include "/usr/local/etc/fusionpbx/config.php";
}
@@ -92,6 +92,9 @@ include "root.php";
$this->driver = $this->type;
}
+ //sanitize the database name
+ $this->db_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->db_name);
+
if ($this->driver == "sqlite") {
if (strlen($this->db_name) == 0) {
$server_name = $_SERVER["SERVER_NAME"];
@@ -233,6 +236,9 @@ include "root.php";
if (!$this->db) {
$this->connect();
}
+ //sanitize the names
+ $this->table = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->table);
+ $this->db_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->db_name);
//get the table info
if (strlen($this->table) == 0) { return false; }
if ($this->type == "sqlite") {
@@ -322,45 +328,97 @@ include "root.php";
if (!$this->db) {
$this->connect();
}
+ //sanitize the name
+ $this->table = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->table);
//get data from the database
$sql = "select * from ".$this->table." ";
if ($this->where) {
$i = 0;
if (is_array($this->where)) {
foreach($this->where as $row) {
+ //sanitize the name
+ $array['name'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array['name']);
+
+ //validate the operator
+ switch ($row['operator']) {
+ case "<": break;
+ case ">": break;
+ case "<=": break;
+ case ">=": break;
+ case "=": break;
+ case ">=": break;
+ case "<>": break;
+ case "!=": break;
+ default:
+ //invalid operator
+ return false;
+ }
+
+ //build the sql
if ($i == 0) {
- $sql .= 'where '.$row['name']." ".$row['operator']." '".$row['value']."' ";
+ //$sql .= 'where '.$row['name']." ".$row['operator']." '".$row['value']."' ";
+ $sql .= 'where '.$row['name']." ".$row['operator']." :".$row['name']." ";
}
else {
- $sql .= "and ".$row['name']." ".$row['operator']." '".$row['value']."' ";
+ //$sql .= "and ".$row['name']." ".$row['operator']." '".$row['value']."' ";
+ $sql .= "and ".$row['name']." ".$row['operator']." :".$row['name']." ";
}
+
+ //add the name and value to the params array
+ $params[$row['name']] = $row['value'];
+
+ //increment $i
$i++;
}
}
}
- if (count($this->order_by) > 0) {
+ if (is_array($this->order_by)) {
$sql .= "order by ";
$i = 1;
if (is_array($this->order_by)) {
foreach($this->order_by as $row) {
+ //sanitize the name
+ $row['name'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $row['name']);
+
+ //sanitize the order
+ switch ($row['order']) {
+ case "asc":
+ break;
+ case "desc":
+ break;
+ default:
+ $row['order'] = '';
+ }
+
+ //build the sql
if (count($this->order_by) == $i) {
$sql .= $row['name']." ".$row['order']." ";
}
else {
$sql .= $row['name']." ".$row['order'].", ";
}
+
+ //increment $i
$i++;
}
}
}
- if ($this->limit) {
- $sql .= " limit ".$this->limit." offset ".$this->offset." ";
+
+ //limit
+ if (isset($this->limit) && is_numeric($this->limit)) {
+ $sql .= "limit ".$this->limit." ";
}
- //echo $sql;
+ //offset
+ if (isset($this->offset) && is_numeric($this->offset)) {
+ $sql .= "offset ".$this->offset." ";
+ }
+
$prep_statement = $this->db->prepare($sql);
if ($prep_statement) {
- $prep_statement->execute();
- return $prep_statement->fetchAll(PDO::FETCH_ASSOC);
+ $prep_statement->execute($params);
+ $array = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
+ unset($prep_statement);
+ return $array;
}
else {
return false;
@@ -386,145 +444,60 @@ include "root.php";
}
}
- public function add() {
- //connect to the database if needed
- if (!$this->db) {
- $this->connect();
- }
- //add data to the database
- $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." ";
- }
- else {
- $sql .= $name.", ";
- }
- $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) {
- $sql .= "'".$value."' ";
- }
- else {
- $sql .= "'".$value."' ";
- }
- }
- else {
- if (strlen($value) > 0) {
- $sql .= "'".$value."', ";
- }
- else {
- $sql .= "null, ";
- }
- }
- $i++;
- }
- }
- $sql .= ")";
- //execute the query, show exceptions
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- try {
- $this->sql = $sql;
- $this->db->exec($sql);
- }
- catch(PDOException $e) {
- echo "Error:
\n";
- echo "
| \n"; - echo $e->getMessage(); - echo " | \n"; - echo "