Cleaning whitespace in database*.lib.php files.

skala
Ivan Tcholakov 15 years ago
parent 2a48943013
commit 17f0c58dbd
  1. 160
      main/inc/lib/database.lib.php
  2. 164
      main/inc/lib/database.mysqli.lib.php

@ -1206,7 +1206,7 @@ class Database {
* for querying. The database parameter is considered not glued, * for querying. The database parameter is considered not glued,
* just plain e.g. COURSE001 * just plain e.g. COURSE001
*/ */
private static function format_table_name($database, $table) { private static function format_table_name($database, $table) {
global $_configuration; global $_configuration;
return '`'.$database.$_configuration['db_glue'].$table.'`'; return '`'.$database.$_configuration['db_glue'].$table.'`';
} }
@ -1313,90 +1313,90 @@ class Database {
/* /*
New useful DB functions New useful DB functions
*/ */
/** /**
* Experimental useful database insert * Experimental useful database insert
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public static function insert($table_name, $attributes) { public static function insert($table_name, $attributes) {
if (empty($attributes) || empty($table_name)) { if (empty($attributes) || empty($table_name)) {
return false; return false;
} }
$filtred_attributes = array(); $filtred_attributes = array();
foreach($attributes as $key => $value) { foreach($attributes as $key => $value) {
$filtred_attributes[$key] = "'".self::escape_string($value)."'"; $filtred_attributes[$key] = "'".self::escape_string($value)."'";
} }
$params = array_keys($filtred_attributes); //@todo check if the field exists in the table we should use a describe of that table $params = array_keys($filtred_attributes); //@todo check if the field exists in the table we should use a describe of that table
$values = array_values($filtred_attributes); $values = array_values($filtred_attributes);
if (!empty($params) && !empty($values)) { if (!empty($params) && !empty($values)) {
$sql = 'INSERT INTO '.$table_name.' ('.implode(',',$params).') VALUES ('.implode(',',$values).')'; $sql = 'INSERT INTO '.$table_name.' ('.implode(',',$params).') VALUES ('.implode(',',$values).')';
$result = self::query($sql); $result = self::query($sql);
return self::get_last_insert_id(); return self::get_last_insert_id();
} }
return false; return false;
} }
/** /**
* Experimental useful database finder * Experimental useful database finder
* @todo lot of stuff to do here * @todo lot of stuff to do here
* @todo known issues, it doesn't work when using LIKE conditions example: array('where'=>array('course_code LIKE "?%"')) * @todo known issues, it doesn't work when using LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
*/ */
public static function select($columns = '*' , $table_name, $conditions = array(), $type_result = 'all', $option = 'ASSOC') { public static function select($columns = '*' , $table_name, $conditions = array(), $type_result = 'all', $option = 'ASSOC') {
$conditions = self::parse_conditions($conditions); $conditions = self::parse_conditions($conditions);
//@todo we could do a describe here to check the columns ... //@todo we could do a describe here to check the columns ...
$clean_columns = ''; $clean_columns = '';
if (is_array($columns)) { if (is_array($columns)) {
$clean_columns = implode(',', $columns); $clean_columns = implode(',', $columns);
} else { } else {
if ($columns == '*') { if ($columns == '*') {
$clean_columns = '*'; $clean_columns = '*';
} else { } else {
$clean_columns = (string)$columns; $clean_columns = (string)$columns;
} }
} }
$sql = "SELECT $clean_columns FROM $table_name $conditions"; $sql = "SELECT $clean_columns FROM $table_name $conditions";
$result = self::query($sql); $result = self::query($sql);
$array = array(); $array = array();
//if (self::num_rows($result) > 0 ) { //if (self::num_rows($result) > 0 ) {
if ($type_result == 'all') { if ($type_result == 'all') {
while ($row = self::fetch_array($result, $option)) { while ($row = self::fetch_array($result, $option)) {
if (isset($row['id'])) { if (isset($row['id'])) {
$array[$row['id']] = $row; $array[$row['id']] = $row;
} else { } else {
$array[] = $row; $array[] = $row;
} }
} }
} else { } else {
$array = self::fetch_array($result, $option); $array = self::fetch_array($result, $option);
} }
return $array; return $array;
} }
/** /**
* Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC')) * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC'))
* @todo known issues, it doesn't work when using LIKE conditions example: array('where'=>array('course_code LIKE "?%"')) * @todo known issues, it doesn't work when using LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
* @param array * @param array
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public function parse_conditions($conditions) { public function parse_conditions($conditions) {
if (empty($conditions)) { if (empty($conditions)) {
return ''; return '';
} }
$return_value = ''; $return_value = '';
foreach ($conditions as $type_condition => $condition_data) { foreach ($conditions as $type_condition => $condition_data) {
$type_condition = strtolower($type_condition); $type_condition = strtolower($type_condition);
switch($type_condition) { switch($type_condition) {
case 'where': case 'where':
foreach ($condition_data as $condition => $value_array) { foreach ($condition_data as $condition => $value_array) {
if (is_array($value_array)) { if (is_array($value_array)) {
$clean_values = array(); $clean_values = array();
foreach($value_array as $item) { foreach($value_array as $item) {
$item = Database::escape_string($item); $item = Database::escape_string($item);
$clean_values[]= "'$item'"; $clean_values[]= "'$item'";
@ -1406,29 +1406,29 @@ class Database {
$clean_values = "'$value_array'"; $clean_values = "'$value_array'";
} }
if (!empty($condition) && !empty($clean_values)) { if (!empty($condition) && !empty($clean_values)) {
//$condition = str_replace('%','@percentage@', $condition); //$condition = str_replace('%','@percentage@', $condition);
$condition = str_replace('?','%s', $condition); //we treat everything as string $condition = str_replace('?','%s', $condition); //we treat everything as string
$condition = vsprintf($condition, $clean_values); $condition = vsprintf($condition, $clean_values);
//$condition = str_replace('@percentage@','%', $condition); //$condition = str_replace('@percentage@','%', $condition);
$where_return .= $condition; $where_return .= $condition;
} }
} }
if (!empty($where_return)) { if (!empty($where_return)) {
$return_value = " WHERE $where_return" ; $return_value = " WHERE $where_return" ;
} }
break; break;
case 'order': case 'order':
$order_array = explode(' ', $condition_data); $order_array = explode(' ', $condition_data);
if (!empty($order_array)) { if (!empty($order_array)) {
if (count($order_array) > 1) { if (count($order_array) > 1) {
$order_array[0] = self::escape_string($order_array[0]); $order_array[0] = self::escape_string($order_array[0]);
if (!empty($order_array[1])) { if (!empty($order_array[1])) {
$order_array[1] = strtolower($order_array[1]); $order_array[1] = strtolower($order_array[1]);
$order = 'desc'; $order = 'desc';
if (in_array($order_array[1], array('desc', 'asc'))) { if (in_array($order_array[1], array('desc', 'asc'))) {
$order = $order_array[1]; $order = $order_array[1];
} }
} }
$return_value .= ' ORDER BY '.$order_array[0].' '.$order; $return_value .= ' ORDER BY '.$order_array[0].' '.$order;
} else { } else {
@ -1436,9 +1436,9 @@ class Database {
} }
} }
break; break;
case 'limit': case 'limit':
$limit_array = explode(',', $condition_data); $limit_array = explode(',', $condition_data);
if (!empty($limit_array)) { if (!empty($limit_array)) {
if (count($limit_array) > 1) { if (count($limit_array) > 1) {
$return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]); $return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]);
@ -1447,62 +1447,62 @@ class Database {
} }
} }
break; break;
} }
} }
return $return_value; return $return_value;
} }
private function parse_where_conditions($coditions){ private function parse_where_conditions($coditions){
return self::parse_conditions(array('where'=>$coditions)); return self::parse_conditions(array('where'=>$coditions));
} }
/** /**
* Experimental useful database update * Experimental useful database update
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public static function delete($table_name, $where_conditions) { public static function delete($table_name, $where_conditions) {
$result = false; $result = false;
$where_return = self::parse_where_conditions($where_conditions); $where_return = self::parse_where_conditions($where_conditions);
$sql = "DELETE FROM $table_name $where_return "; $sql = "DELETE FROM $table_name $where_return ";
$result = self::query($sql); $result = self::query($sql);
$affected_rows = self::affected_rows(); $affected_rows = self::affected_rows();
//@todo should return affected_rows for //@todo should return affected_rows for
return $affected_rows; return $affected_rows;
} }
/** /**
* Experimental useful database update * Experimental useful database update
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public static function update($table_name, $attributes, $where_conditions = array()) { public static function update($table_name, $attributes, $where_conditions = array()) {
if (!empty($table_name) && !empty($attributes)) { if (!empty($table_name) && !empty($attributes)) {
$update_sql = ''; $update_sql = '';
//Cleaning attributes //Cleaning attributes
$count = 1; $count = 1;
foreach ($attributes as $key=>$value) { foreach ($attributes as $key=>$value) {
$value = self::escape_string($value); $value = self::escape_string($value);
$update_sql .= "$key = '$value' "; $update_sql .= "$key = '$value' ";
if ($count < count($attributes)) { if ($count < count($attributes)) {
$update_sql.=', '; $update_sql.=', ';
} }
$count++; $count++;
} }
if (!empty($update_sql)) { if (!empty($update_sql)) {
//Parsing and cleaning the where conditions //Parsing and cleaning the where conditions
$where_return = self::parse_where_conditions($where_conditions); $where_return = self::parse_where_conditions($where_conditions);
$sql = "UPDATE $table_name SET $update_sql $where_return "; $sql = "UPDATE $table_name SET $update_sql $where_return ";
//echo $sql; exit; //echo $sql; exit;
$result = self::query($sql); $result = self::query($sql);
$affected_rows = self::affected_rows(); $affected_rows = self::affected_rows();
return $affected_rows; return $affected_rows;
} }
} }
return false; return false;
} }
/* /*
DEPRECATED METHODS DEPRECATED METHODS
*/ */
@ -1520,6 +1520,6 @@ class Database {
public static function get_last_insert_id() { public static function get_last_insert_id() {
return mysql_insert_id(); return mysql_insert_id();
} }
} }
//end class Database //end class Database

@ -691,10 +691,10 @@ class Database {
? new mysqli('p:'.$parameters['server'], $parameters['username'], $parameters['password']) ? new mysqli('p:'.$parameters['server'], $parameters['username'], $parameters['password'])
: new mysqli($parameters['server'], $parameters['username'], $parameters['password']); : new mysqli($parameters['server'], $parameters['username'], $parameters['password']);
if ($database_connection->connect_errno) { if ($database_connection->connect_errno) {
error_log($database_connection->connect_errno()); error_log($database_connection->connect_errno());
return false; return false;
} else { } else {
return true; return true;
} }
} }
@ -923,7 +923,7 @@ class Database {
* @result mixed One cell of the result, or FALSE on error * @result mixed One cell of the result, or FALSE on error
*/ */
public static function result(&$resource, $row, $field = '') { public static function result(&$resource, $row, $field = '') {
if (self::num_rows($resource) > 0) { if (self::num_rows($resource) > 0) {
if (!empty($field)) { if (!empty($field)) {
$r = mysqli_data_seek($resource, $row); $r = mysqli_data_seek($resource, $row);
return $r[$field]; return $r[$field];
@ -1317,88 +1317,88 @@ class Database {
/* /*
New useful DB functions New useful DB functions
*/ */
/** /**
* Experimental useful database insert * Experimental useful database insert
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public static function insert($table_name, $attributes) { public static function insert($table_name, $attributes) {
if (empty($attributes) || empty($table_name)) { if (empty($attributes) || empty($table_name)) {
return false; return false;
} }
$filtred_attributes = array(); $filtred_attributes = array();
foreach($attributes as $key => $value) { foreach($attributes as $key => $value) {
$filtred_attributes[$key] = "'".self::escape_string($value)."'"; $filtred_attributes[$key] = "'".self::escape_string($value)."'";
} }
$params = array_keys($filtred_attributes); //@todo check if the field exists in the table we should use a describe of that table $params = array_keys($filtred_attributes); //@todo check if the field exists in the table we should use a describe of that table
$values = array_values($filtred_attributes); $values = array_values($filtred_attributes);
if (!empty($params) && !empty($values)) { if (!empty($params) && !empty($values)) {
$sql = 'INSERT INTO '.$table_name.' ('.implode(',',$params).') VALUES ('.implode(',',$values).')'; $sql = 'INSERT INTO '.$table_name.' ('.implode(',',$params).') VALUES ('.implode(',',$values).')';
$result = self::query($sql); $result = self::query($sql);
return self::get_last_insert_id(); return self::get_last_insert_id();
} }
return false; return false;
} }
/** /**
* Experimental useful database finder * Experimental useful database finder
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public static function select($columns = '*' , $table_name, $conditions = array(), $type_result = 'all', $option = 'ASSOC') { public static function select($columns = '*' , $table_name, $conditions = array(), $type_result = 'all', $option = 'ASSOC') {
$conditions = self::parse_conditions($conditions); $conditions = self::parse_conditions($conditions);
//@todo we could do a describe here to check the columns ... //@todo we could do a describe here to check the columns ...
$clean_columns = ''; $clean_columns = '';
if (is_array($columns)) { if (is_array($columns)) {
$clean_columns = implode(',', $columns); $clean_columns = implode(',', $columns);
} else { } else {
if ($columns == '*') { if ($columns == '*') {
$clean_columns = '*'; $clean_columns = '*';
} else { } else {
$clean_columns = (string)$columns; $clean_columns = (string)$columns;
} }
} }
$sql = "SELECT $clean_columns FROM $table_name $conditions"; $sql = "SELECT $clean_columns FROM $table_name $conditions";
$result = self::query($sql); $result = self::query($sql);
$array = array(); $array = array();
//if (self::num_rows($result) > 0 ) { //if (self::num_rows($result) > 0 ) {
if ($type_result == 'all') { if ($type_result == 'all') {
while ($row = self::fetch_array($result, $option)) { while ($row = self::fetch_array($result, $option)) {
if (isset($row['id'])) { if (isset($row['id'])) {
$array[$row['id']] = $row; $array[$row['id']] = $row;
} else { } else {
$array[] = $row; $array[] = $row;
} }
} }
} else { } else {
$array = self::fetch_array($result, $option); $array = self::fetch_array($result, $option);
} }
return $array; return $array;
} }
/** /**
* Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC')) * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC'))
* @param array * @param array
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
private function parse_conditions($conditions) { private function parse_conditions($conditions) {
if (empty($conditions)) { if (empty($conditions)) {
return ''; return '';
} }
$return_value = ''; $return_value = '';
foreach ($conditions as $type_condition => $condition_data) { foreach ($conditions as $type_condition => $condition_data) {
$type_condition = strtolower($type_condition); $type_condition = strtolower($type_condition);
switch($type_condition) { switch($type_condition) {
case 'where': case 'where':
foreach ($condition_data as $condition => $value_array) { foreach ($condition_data as $condition => $value_array) {
if (is_array($value_array)) { if (is_array($value_array)) {
$clean_values = array(); $clean_values = array();
foreach($value_array as $item) { foreach($value_array as $item) {
$item = Database::escape_string($item); $item = Database::escape_string($item);
$clean_values[]= "'$item'"; $clean_values[]= "'$item'";
@ -1407,28 +1407,28 @@ class Database {
$value_array = Database::escape_string($value_array); $value_array = Database::escape_string($value_array);
$clean_values = "'$value_array'"; $clean_values = "'$value_array'";
} }
if (!empty($condition) && !empty($clean_values)) { if (!empty($condition) && !empty($clean_values)) {
$condition = str_replace('?','%s', $condition); //we treat everything as string $condition = str_replace('?','%s', $condition); //we treat everything as string
$condition = vsprintf($condition, $clean_values); $condition = vsprintf($condition, $clean_values);
$where_return .= $condition; $where_return .= $condition;
} }
} }
if (!empty($where_return)) { if (!empty($where_return)) {
$return_value = " WHERE $where_return" ; $return_value = " WHERE $where_return" ;
} }
break; break;
case 'order': case 'order':
$order_array = explode(' ', $condition_data); $order_array = explode(' ', $condition_data);
if (!empty($order_array)) { if (!empty($order_array)) {
if (count($order_array) > 1) { if (count($order_array) > 1) {
$order_array[0] = self::escape_string($order_array[0]); $order_array[0] = self::escape_string($order_array[0]);
if (!empty($order_array[1])) { if (!empty($order_array[1])) {
$order_array[1] = strtolower($order_array[1]); $order_array[1] = strtolower($order_array[1]);
$order = 'desc'; $order = 'desc';
if (in_array($order_array[1], array('desc', 'asc'))) { if (in_array($order_array[1], array('desc', 'asc'))) {
$order = $order_array[1]; $order = $order_array[1];
} }
} }
$return_value .= ' ORDER BY '.$order_array[0].' '.$order; $return_value .= ' ORDER BY '.$order_array[0].' '.$order;
} else { } else {
@ -1436,9 +1436,9 @@ class Database {
} }
} }
break; break;
case 'limit': case 'limit':
$limit_array = explode(',', $condition_data); $limit_array = explode(',', $condition_data);
if (!empty($limit_array)) { if (!empty($limit_array)) {
if (count($limit_array) > 1) { if (count($limit_array) > 1) {
$return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]); $return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]);
@ -1447,62 +1447,62 @@ class Database {
} }
} }
break; break;
} }
} }
return $return_value; return $return_value;
} }
private function parse_where_conditions($coditions){ private function parse_where_conditions($coditions){
return self::parse_conditions(array('where'=>$coditions)); return self::parse_conditions(array('where'=>$coditions));
} }
/** /**
* Experimental useful database update * Experimental useful database update
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public static function delete($table_name, $where_conditions) { public static function delete($table_name, $where_conditions) {
$result = false; $result = false;
$where_return = self::parse_where_conditions($where_conditions); $where_return = self::parse_where_conditions($where_conditions);
$sql = "DELETE FROM $table_name $where_return "; $sql = "DELETE FROM $table_name $where_return ";
$result = self::query($sql); $result = self::query($sql);
$affected_rows = self::affected_rows(); $affected_rows = self::affected_rows();
//@todo should return affected_rows for //@todo should return affected_rows for
return $affected_rows; return $affected_rows;
} }
/** /**
* Experimental useful database update * Experimental useful database update
* @todo lot of stuff to do here * @todo lot of stuff to do here
*/ */
public static function update($table_name, $attributes, $where_conditions = array()) { public static function update($table_name, $attributes, $where_conditions = array()) {
if (!empty($table_name) && !empty($attributes)) { if (!empty($table_name) && !empty($attributes)) {
$update_sql = ''; $update_sql = '';
//Cleaning attributes //Cleaning attributes
$count = 1; $count = 1;
foreach ($attributes as $key=>$value) { foreach ($attributes as $key=>$value) {
$value = self::escape_string($value); $value = self::escape_string($value);
$update_sql .= "$key = '$value' "; $update_sql .= "$key = '$value' ";
if ($count < count($attributes)) { if ($count < count($attributes)) {
$update_sql.=', '; $update_sql.=', ';
} }
$count++; $count++;
} }
if (!empty($update_sql)) { if (!empty($update_sql)) {
//Parsing and cleaning the where conditions //Parsing and cleaning the where conditions
$where_return = self::parse_where_conditions($where_conditions); $where_return = self::parse_where_conditions($where_conditions);
$sql = "UPDATE $table_name SET $update_sql $where_return "; $sql = "UPDATE $table_name SET $update_sql $where_return ";
//echo $sql; exit; //echo $sql; exit;
$result = self::query($sql); $result = self::query($sql);
$affected_rows = self::affected_rows(); $affected_rows = self::affected_rows();
return $affected_rows; return $affected_rows;
} }
} }
return false; return false;
} }
/* /*
DEPRECATED METHODS DEPRECATED METHODS
*/ */
@ -1521,6 +1521,6 @@ class Database {
global $database_connection; global $database_connection;
return $database_connection->insert_id($database_connection); return $database_connection->insert_id($database_connection);
} }
} }
//end class Database //end class Database
Loading…
Cancel
Save