diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 29f9ab00dd..3bd161cc28 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,9 @@ +2011-02-24 Miguel de Dios + + * include/db/postgresql.php, include/db/mysql.php,include/functions_db.php: + created the function "postgresql_get_db_row" in to the engines mysql and + postgreSQL. + 2011-02-24 Miguel de Dios * include/db/postgresql.php, include/db/mysql.php, include/functions_db.php: diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index cabf951400..d29e94968b 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -115,6 +115,50 @@ function mysql_get_db_value ($field, $table, $field_search = 1, $condition = 1, return $result[0][$field]; } +/** + * Get the first row of a database query into a table. + * + * The SQL statement executed would be something like: + * "SELECT (*||$fields) FROM $table WHERE $field_search = $condition" + * + * @param string Table to get the row + * @param string Field to filter elements + * @param string Condition the field must have. + * @param mixed Fields to select (array or string or false/empty for *) + * + * @return mixed The first row of a database query or false. + */ +function mysql_get_db_row ($table, $field_search, $condition, $fields = false) { + if (empty ($fields)) { + $fields = '*'; + } + else { + if (is_array ($fields)) + $fields = implode (',', $fields); + else if (! is_string ($fields)) + return false; + } + + if (is_int ($condition)) { + $sql = sprintf ("SELECT %s FROM `%s` WHERE `%s` = %d LIMIT 1", + $fields, $table, $field_search, $condition); + } + else if (is_float ($condition) || is_double ($condition)) { + $sql = sprintf ("SELECT %s FROM `%s` WHERE `%s` = %f LIMIT 1", + $fields, $table, $field_search, $condition); + } + else { + $sql = sprintf ("SELECT %s FROM `%s` WHERE `%s` = '%s' LIMIT 1", + $fields, $table, $field_search, $condition); + } + $result = get_db_all_rows_sql ($sql); + + if ($result === false) + return false; + + return $result[0]; +} + /** * Get all the rows in a table of the database. * diff --git a/pandora_console/include/db/postgresql.php b/pandora_console/include/db/postgresql.php index bf39040fda..161d6c0bc4 100644 --- a/pandora_console/include/db/postgresql.php +++ b/pandora_console/include/db/postgresql.php @@ -78,6 +78,50 @@ function postgresql_get_db_value ($field, $table, $field_search = 1, $condition } } +/** + * Get the first row of a database query into a table. + * + * The SQL statement executed would be something like: + * "SELECT (*||$fields) FROM $table WHERE $field_search = $condition" + * + * @param string Table to get the row + * @param string Field to filter elements + * @param string Condition the field must have. + * @param mixed Fields to select (array or string or false/empty for *) + * + * @return mixed The first row of a database query or false. + */ +function postgresql_get_db_row ($table, $field_search, $condition, $fields = false) { + if (empty ($fields)) { + $fields = '*'; + } + else { + if (is_array ($fields)) + $fields = implode (',', $fields); + else if (! is_string ($fields)) + return false; + } + + if (is_int ($condition)) { + $sql = sprintf ('SELECT %s FROM "%s" WHERE \'%s\' = %d LIMIT 1', + $fields, $table, $field_search, $condition); + } + else if (is_float ($condition) || is_double ($condition)) { + $sql = sprintf ("SELECT %s FROM \"%s\" WHERE '%s' = %f LIMIT 1", + $fields, $table, $field_search, $condition); + } + else { + $sql = sprintf ("SELECT %s FROM \"%s\" WHERE '%s' = '%s' LIMIT 1", + $fields, $table, $field_search, $condition); + } + $result = get_db_all_rows_sql ($sql); + + if ($result === false) + return false; + + return $result[0]; +} + function postgresql_get_db_all_rows_sql ($sql, $search_history_db = false, $cache = true) { global $config; diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 66914b8dfe..a5c0141e20 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -69,6 +69,7 @@ function check_login () { include("general/noaccess.php"); exit; } + if ((isset($_SESSION["id_usuario"])) AND ($_SESSION["id_usuario"] != "")) { if (is_user ($_SESSION["id_usuario"])) { return 0; @@ -86,6 +87,7 @@ function check_login () { } } } + pandora_audit("No session", "Trying to access without a valid session", "N/A"); include ($config["homedir"]."/general/noaccess.php"); exit; @@ -2162,31 +2164,16 @@ function get_db_row_sql ($sql, $search_history_db = false) { * @return mixed The first row of a database query or false. */ function get_db_row ($table, $field_search, $condition, $fields = false) { - if (empty ($fields)) { - $fields = '*'; - } else { - if (is_array ($fields)) - $fields = implode (',', $fields); - else if (! is_string ($fields)) - return false; - } - - if (is_int ($condition)) { - $sql = sprintf ("SELECT %s FROM `%s` WHERE `%s` = %d LIMIT 1", - $fields, $table, $field_search, $condition); - } else if (is_float ($condition) || is_double ($condition)) { - $sql = sprintf ("SELECT %s FROM `%s` WHERE `%s` = %f LIMIT 1", - $fields, $table, $field_search, $condition); - } else { - $sql = sprintf ("SELECT %s FROM `%s` WHERE `%s` = '%s' LIMIT 1", - $fields, $table, $field_search, $condition); - } - $result = get_db_all_rows_sql ($sql); - - if ($result === false) - return false; + global $config; - return $result[0]; + switch ($config["dbtype"]) { + case "mysql": + return mysql_get_db_row($table, $field_search, $condition, $fields); + break; + case "postgresql": + return postgresql_get_db_row($table, $field_search, $condition, $fields); + break; + } } /**