mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-28 16:24:54 +02:00
2011-02-14 Miguel de Dios <miguel.dedios@artica.es>
* include/db/postgresql.php, include/db/mysql.php, include/functions_db.php: initial division of the DB core in several engines, at the moment mysql and postgreSQL (not complete). * include/functions_config.php, include/config_process.php: change source code to use new functions. * install.php: mark the mysql engine (in future select). git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3826 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
parent
3bc407be06
commit
26dc52933e
@ -1,3 +1,14 @@
|
|||||||
|
2011-02-14 Miguel de Dios <miguel.dedios@artica.es>
|
||||||
|
|
||||||
|
* include/db/postgresql.php, include/db/mysql.php,
|
||||||
|
include/functions_db.php: initial division of the DB core in several
|
||||||
|
engines, at the moment mysql and postgreSQL (not complete).
|
||||||
|
|
||||||
|
* include/functions_config.php, include/config_process.php: change source
|
||||||
|
code to use new functions.
|
||||||
|
|
||||||
|
* install.php: mark the mysql engine (in future select).
|
||||||
|
|
||||||
2011-02-14 Miguel de Dios <miguel.dedios@artica.es>
|
2011-02-14 Miguel de Dios <miguel.dedios@artica.es>
|
||||||
|
|
||||||
* pandoradb.postgreSQL.sql: added "block_size" and "flash_chart" columns on
|
* pandoradb.postgreSQL.sql: added "block_size" and "flash_chart" columns on
|
||||||
|
@ -59,28 +59,24 @@ else {
|
|||||||
|
|
||||||
$config['start_time'] = microtime (true);
|
$config['start_time'] = microtime (true);
|
||||||
|
|
||||||
// Non-persistent connection: This will help to avoid mysql errors like "has gone away" or locking problems
|
|
||||||
// If you want persistent connections change it to mysql_pconnect().
|
|
||||||
$config['dbconnection'] = mysql_connect ($config["dbhost"], $config["dbuser"], $config["dbpass"]);
|
|
||||||
if (! $config['dbconnection']) {
|
|
||||||
include ($config["homedir"]."/general/error_authconfig.php");
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
$ownDir = dirname(__FILE__) . '/';
|
$ownDir = dirname(__FILE__) . '/';
|
||||||
|
|
||||||
|
require_once ($ownDir . 'functions_db.php');
|
||||||
|
require_once ($ownDir . 'functions.php');
|
||||||
|
|
||||||
|
select_db_engine();
|
||||||
|
connect_db();
|
||||||
|
|
||||||
|
|
||||||
if (! defined ('EXTENSIONS_DIR'))
|
if (! defined ('EXTENSIONS_DIR'))
|
||||||
define ('EXTENSIONS_DIR', 'extensions');
|
define ('EXTENSIONS_DIR', 'extensions');
|
||||||
|
|
||||||
if (! defined ('ENTERPRISE_DIR'))
|
if (! defined ('ENTERPRISE_DIR'))
|
||||||
define ('ENTERPRISE_DIR', 'enterprise');
|
define ('ENTERPRISE_DIR', 'enterprise');
|
||||||
|
|
||||||
mysql_select_db ($config["dbname"]);
|
|
||||||
require_once ($ownDir . 'functions.php');
|
|
||||||
require_once ($ownDir . 'functions_db.php');
|
|
||||||
require_once ($ownDir. 'functions_config.php');
|
require_once ($ownDir. 'functions_config.php');
|
||||||
|
|
||||||
process_config ();
|
process_config();
|
||||||
|
|
||||||
require_once ($ownDir . 'streams.php');
|
require_once ($ownDir . 'streams.php');
|
||||||
require_once ($ownDir . 'gettext.php');
|
require_once ($ownDir . 'gettext.php');
|
||||||
@ -141,12 +137,13 @@ else {
|
|||||||
// Connect to the history DB
|
// Connect to the history DB
|
||||||
if (isset($config['history_db_enabled'])) {
|
if (isset($config['history_db_enabled'])) {
|
||||||
if ($config['history_db_enabled']) {
|
if ($config['history_db_enabled']) {
|
||||||
$config['history_db_connection'] = mysql_connect ($config['history_db_host'] . ':' . $config['history_db_port'], $config['history_db_user'], $config['history_db_pass']);
|
$config['history_db_connection'] = connect_db(
|
||||||
mysql_select_db ($config['history_db_name'], $config['history_db_connection']);
|
$config['history_db_host'] . ':' . $config['history_db_port'],
|
||||||
|
$config['history_db_user'],
|
||||||
|
$config['history_db_pass']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make dbconnection the default connection again (the link identifier of the already opened link will be returned)
|
// Make dbconnection the default connection again (the link identifier of the already opened link will be returned)
|
||||||
$config['dbconnection'] = mysql_connect ($config["dbhost"], $config["dbuser"], $config["dbpass"]);
|
connect_db();
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
307
pandora_console/include/db/mysql.php
Normal file
307
pandora_console/include/db/mysql.php
Normal file
@ -0,0 +1,307 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// Pandora FMS - http://pandorafms.com
|
||||||
|
// ==================================================
|
||||||
|
// Copyright (c) 2005-2011 Artica Soluciones Tecnologicas
|
||||||
|
// Please see http://pandorafms.org for full contribution list
|
||||||
|
|
||||||
|
// This program is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public License
|
||||||
|
// as published by the Free Software Foundation; version 2
|
||||||
|
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
|
||||||
|
function mysql_connect_db($host = null, $db = null, $user = null, $pass = null) {
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
if ($host === null)
|
||||||
|
$host = $config["dbhost"];
|
||||||
|
if ($db === null)
|
||||||
|
$db = $config["dbname"];
|
||||||
|
if ($user === null)
|
||||||
|
$user = $config["dbuser"];
|
||||||
|
if ($pass === null)
|
||||||
|
$pass = $config["dbpass"];
|
||||||
|
|
||||||
|
// Non-persistent connection: This will help to avoid mysql errors like "has gone away" or locking problems
|
||||||
|
// If you want persistent connections change it to mysql_pconnect().
|
||||||
|
$config['dbconnection'] = mysql_connect($host, $user, $pass);
|
||||||
|
mysql_select_db($db, $config['dbconnection']);
|
||||||
|
|
||||||
|
if (! $config['dbconnection']) {
|
||||||
|
include ($config["homedir"]."/general/error_authconfig.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $config['dbconnection'];
|
||||||
|
}
|
||||||
|
|
||||||
|
function mysql_get_db_all_rows_sql ($sql, $search_history_db = false, $cache = true) {
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
$history = array ();
|
||||||
|
|
||||||
|
// To disable globally SQL cache depending on global variable.
|
||||||
|
// Used in several critical places like Metaconsole trans-server queries
|
||||||
|
if (isset($config["dbcache"]))
|
||||||
|
$cache = $config["dbcache"];
|
||||||
|
|
||||||
|
// Read from the history DB if necessary
|
||||||
|
if ($search_history_db) {
|
||||||
|
$cache = false;
|
||||||
|
$history = false;
|
||||||
|
|
||||||
|
if (isset($config['history_db_connection']))
|
||||||
|
$history = mysql_process_sql ($sql, 'affected_rows', $config['history_db_connection'], false);
|
||||||
|
|
||||||
|
if ($history === false) {
|
||||||
|
$history = array ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$return = mysql_process_sql ($sql, 'affected_rows', $config['dbconnection'], $cache);
|
||||||
|
if ($return === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append result to the history DB data
|
||||||
|
if (! empty ($return)) {
|
||||||
|
foreach ($return as $row) {
|
||||||
|
array_push ($history, $row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty ($history))
|
||||||
|
return $history;
|
||||||
|
//Return false, check with === or !==
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mysql_process_sql ($sql, $rettype = "affected_rows", $dbconnection = '', $cache = true) {
|
||||||
|
global $config;
|
||||||
|
global $sql_cache;
|
||||||
|
|
||||||
|
$retval = array();
|
||||||
|
|
||||||
|
if ($sql == '')
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if ($cache && ! empty ($sql_cache[$sql])) {
|
||||||
|
$retval = $sql_cache[$sql];
|
||||||
|
$sql_cache['saved']++;
|
||||||
|
add_database_debug_trace ($sql);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$start = microtime (true);
|
||||||
|
if ($dbconnection == '') {
|
||||||
|
$result = mysql_query ($sql);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$result = mysql_query ($sql, $dbconnection);
|
||||||
|
}
|
||||||
|
$time = microtime (true) - $start;
|
||||||
|
if ($result === false) {
|
||||||
|
$backtrace = debug_backtrace ();
|
||||||
|
$error = sprintf ('%s (\'%s\') in <strong>%s</strong> on line %d',
|
||||||
|
mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']);
|
||||||
|
add_database_debug_trace ($sql, mysql_error ());
|
||||||
|
set_error_handler ('sql_error_handler');
|
||||||
|
trigger_error ($error);
|
||||||
|
restore_error_handler ();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
elseif ($result === true) {
|
||||||
|
if ($rettype == "insert_id") {
|
||||||
|
$result = mysql_insert_id ();
|
||||||
|
}
|
||||||
|
elseif ($rettype == "info") {
|
||||||
|
$result = mysql_info ();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$result = mysql_affected_rows ();
|
||||||
|
}
|
||||||
|
|
||||||
|
add_database_debug_trace ($sql, $result, mysql_affected_rows (),
|
||||||
|
array ('time' => $time));
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
add_database_debug_trace ($sql, 0, mysql_affected_rows (),
|
||||||
|
array ('time' => $time));
|
||||||
|
while ($row = mysql_fetch_assoc ($result)) {
|
||||||
|
array_push ($retval, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($cache === true)
|
||||||
|
$sql_cache[$sql] = $retval;
|
||||||
|
mysql_free_result ($result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty ($retval))
|
||||||
|
return $retval;
|
||||||
|
//Return false, check with === or !==
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the rows in a table of the database.
|
||||||
|
*
|
||||||
|
* @param string Database table name.
|
||||||
|
* @param string Field to order by.
|
||||||
|
* @param string $order The type of order, by default 'ASC'.
|
||||||
|
*
|
||||||
|
* @return mixed A matrix with all the values in the table
|
||||||
|
*/
|
||||||
|
function mysql_get_db_all_rows_in_table($table, $order_field = "", $order = 'ASC') {
|
||||||
|
if ($order_field != "") {
|
||||||
|
return get_db_all_rows_sql ("SELECT * FROM `".$table."` ORDER BY ".$order_field . " " . $order);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return get_db_all_rows_sql ("SELECT * FROM `".$table."`");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts strings into database
|
||||||
|
*
|
||||||
|
* The number of values should be the same or a positive integer multiple as the number of rows
|
||||||
|
* If you have an associate array (eg. array ("row1" => "value1")) you can use this function with ($table, array_keys ($array), $array) in it's options
|
||||||
|
* All arrays and values should have been cleaned before passing. It's not neccessary to add quotes.
|
||||||
|
*
|
||||||
|
* @param string Table to insert into
|
||||||
|
* @param mixed A single value or array of values to insert (can be a multiple amount of rows)
|
||||||
|
*
|
||||||
|
* @return mixed False in case of error or invalid values passed. Affected rows otherwise
|
||||||
|
*/
|
||||||
|
function mysql_process_sql_insert($table, $values) {
|
||||||
|
//Empty rows or values not processed
|
||||||
|
if (empty ($values))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
$values = (array) $values;
|
||||||
|
|
||||||
|
$query = sprintf ("INSERT INTO `%s` ", $table);
|
||||||
|
$fields = array ();
|
||||||
|
$values_str = '';
|
||||||
|
$i = 1;
|
||||||
|
$max = count ($values);
|
||||||
|
foreach ($values as $field => $value) { //Add the correct escaping to values
|
||||||
|
if ($field[0] != "`") {
|
||||||
|
$field = "`".$field."`";
|
||||||
|
}
|
||||||
|
|
||||||
|
array_push ($fields, $field);
|
||||||
|
|
||||||
|
if (is_null ($value)) {
|
||||||
|
$values_str .= "NULL";
|
||||||
|
}
|
||||||
|
elseif (is_int ($value) || is_bool ($value)) {
|
||||||
|
$values_str .= sprintf ("%d", $value);
|
||||||
|
}
|
||||||
|
else if (is_float ($value) || is_double ($value)) {
|
||||||
|
$values_str .= sprintf ("%f", $value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$values_str .= sprintf ("'%s'", $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($i < $max) {
|
||||||
|
$values_str .= ",";
|
||||||
|
}
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$query .= '('.implode (', ', $fields).')';
|
||||||
|
|
||||||
|
$query .= ' VALUES ('.$values_str.')';
|
||||||
|
|
||||||
|
return process_sql ($query, 'insert_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function comes back with an array in case of SELECT
|
||||||
|
* in case of UPDATE, DELETE etc. with affected rows
|
||||||
|
* an empty array in case of SELECT without results
|
||||||
|
* Queries that return data will be cached so queries don't get repeated
|
||||||
|
*
|
||||||
|
* @param string SQL statement to execute
|
||||||
|
*
|
||||||
|
* @param string What type of info to return in case of INSERT/UPDATE.
|
||||||
|
* 'affected_rows' will return mysql_affected_rows (default value)
|
||||||
|
* 'insert_id' will return the ID of an autoincrement value
|
||||||
|
* 'info' will return the full (debug) information of a query
|
||||||
|
*
|
||||||
|
* @return mixed An array with the rows, columns and values in a multidimensional array or false in error
|
||||||
|
*/
|
||||||
|
function mysql_process_sql($sql, $rettype = "affected_rows", $dbconnection = '', $cache = true) {
|
||||||
|
global $config;
|
||||||
|
global $sql_cache;
|
||||||
|
|
||||||
|
$retval = array();
|
||||||
|
|
||||||
|
if ($sql == '')
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if ($cache && ! empty ($sql_cache[$sql])) {
|
||||||
|
$retval = $sql_cache[$sql];
|
||||||
|
$sql_cache['saved']++;
|
||||||
|
add_database_debug_trace ($sql);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$start = microtime (true);
|
||||||
|
if ($dbconnection == '') {
|
||||||
|
$result = mysql_query ($sql);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$result = mysql_query ($sql, $dbconnection);
|
||||||
|
}
|
||||||
|
$time = microtime (true) - $start;
|
||||||
|
if ($result === false) {
|
||||||
|
$backtrace = debug_backtrace ();
|
||||||
|
$error = sprintf ('%s (\'%s\') in <strong>%s</strong> on line %d',
|
||||||
|
mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']);
|
||||||
|
add_database_debug_trace ($sql, mysql_error ());
|
||||||
|
set_error_handler ('sql_error_handler');
|
||||||
|
trigger_error ($error);
|
||||||
|
restore_error_handler ();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
elseif ($result === true) {
|
||||||
|
if ($rettype == "insert_id") {
|
||||||
|
$result = mysql_insert_id ();
|
||||||
|
}
|
||||||
|
elseif ($rettype == "info") {
|
||||||
|
$result = mysql_info ();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$result = mysql_affected_rows ();
|
||||||
|
}
|
||||||
|
|
||||||
|
add_database_debug_trace ($sql, $result, mysql_affected_rows (),
|
||||||
|
array ('time' => $time));
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
add_database_debug_trace ($sql, 0, mysql_affected_rows (),
|
||||||
|
array ('time' => $time));
|
||||||
|
while ($row = mysql_fetch_assoc ($result)) {
|
||||||
|
array_push ($retval, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($cache === true)
|
||||||
|
$sql_cache[$sql] = $retval;
|
||||||
|
mysql_free_result ($result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty ($retval))
|
||||||
|
return $retval;
|
||||||
|
//Return false, check with === or !==
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
?>
|
257
pandora_console/include/db/postgresql.php
Normal file
257
pandora_console/include/db/postgresql.php
Normal file
@ -0,0 +1,257 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// Pandora FMS - http://pandorafms.com
|
||||||
|
// ==================================================
|
||||||
|
// Copyright (c) 2005-2011 Artica Soluciones Tecnologicas
|
||||||
|
// Please see http://pandorafms.org for full contribution list
|
||||||
|
|
||||||
|
// This program is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public License
|
||||||
|
// as published by the Free Software Foundation; version 2
|
||||||
|
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
|
||||||
|
function postgresql_connect_db($host = null, $db = null, $user = null, $pass = null) {
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
if ($host === null)
|
||||||
|
$host = $config["dbhost"];
|
||||||
|
if ($db === null)
|
||||||
|
$db = $config["dbname"];
|
||||||
|
if ($user === null)
|
||||||
|
$user = $config["dbuser"];
|
||||||
|
if ($pass === null)
|
||||||
|
$pass = $config["dbpass"];
|
||||||
|
|
||||||
|
$config['dbconnection'] = pg_connect("host=" . $host .
|
||||||
|
" dbname=" . $db .
|
||||||
|
" user=" . $user .
|
||||||
|
" password=" . $pass);
|
||||||
|
|
||||||
|
if (! $config['dbconnection']) {
|
||||||
|
include ($config["homedir"]."/general/error_authconfig.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $config['dbconnection'];
|
||||||
|
}
|
||||||
|
|
||||||
|
function postgresql_get_db_all_rows_sql ($sql, $search_history_db = false, $cache = true) {
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
$history = array ();
|
||||||
|
|
||||||
|
// To disable globally SQL cache depending on global variable.
|
||||||
|
// Used in several critical places like Metaconsole trans-server queries
|
||||||
|
if (isset($config["dbcache"]))
|
||||||
|
$cache = $config["dbcache"];
|
||||||
|
|
||||||
|
// Read from the history DB if necessary
|
||||||
|
if ($search_history_db) {
|
||||||
|
$cache = false;
|
||||||
|
$history = false;
|
||||||
|
|
||||||
|
if (isset($config['history_db_connection']))
|
||||||
|
$history = postgresql_process_sql ($sql, 'affected_rows', $config['history_db_connection'], false);
|
||||||
|
|
||||||
|
if ($history === false) {
|
||||||
|
$history = array ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$return = postgresql_process_sql ($sql, 'affected_rows', $config['dbconnection'], $cache);
|
||||||
|
if ($return === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append result to the history DB data
|
||||||
|
if (! empty ($return)) {
|
||||||
|
foreach ($return as $row) {
|
||||||
|
array_push ($history, $row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty ($history))
|
||||||
|
return $history;
|
||||||
|
//Return false, check with === or !==
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function postgresql_insert_id($dbconnection = '') {
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
if ($dbconnection !== '') {
|
||||||
|
$insert_query = pg_query($dbconnection, "SELECT lastval();");
|
||||||
|
$insert_id = pg_fetch_row($insert_query);
|
||||||
|
$result = $insert_row[0];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$insert_query = pg_query($config['dbconnection'], "SELECT lastval();");
|
||||||
|
$insert_id = pg_fetch_row($insert_query);
|
||||||
|
$result = $insert_row[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function postgresql_process_sql($sql, $rettype = "affected_rows", $dbconnection = '', $cache = true) {
|
||||||
|
global $config;
|
||||||
|
global $sql_cache;
|
||||||
|
|
||||||
|
$retval = array();
|
||||||
|
|
||||||
|
if ($sql == '')
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if ($cache && ! empty ($sql_cache[$sql])) {
|
||||||
|
$retval = $sql_cache[$sql];
|
||||||
|
$sql_cache['saved']++;
|
||||||
|
add_database_debug_trace ($sql);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$start = microtime (true);
|
||||||
|
if ($dbconnection !== '') {
|
||||||
|
pg_send_query($dbconnection, $sql);
|
||||||
|
$result = pg_get_result($dbconnection);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pg_send_query($config['dbconnection'], $sql);
|
||||||
|
$result = pg_get_result($config['dbconnection']);
|
||||||
|
|
||||||
|
debugPrint($sql);
|
||||||
|
$insert_query = pg_query($config['dbconnection'], "SELECT LASTVAL();");
|
||||||
|
$insert_id = pg_fetch_row($insert_query);
|
||||||
|
debugPrint($insert_row[0]);
|
||||||
|
}
|
||||||
|
$time = microtime (true) - $start;
|
||||||
|
if ($result === false) {
|
||||||
|
$backtrace = debug_backtrace ();
|
||||||
|
$error = sprintf ('%s (\'%s\') in <strong>%s</strong> on line %d',
|
||||||
|
mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']);
|
||||||
|
add_database_debug_trace ($sql, mysql_error ());
|
||||||
|
set_error_handler ('sql_error_handler');
|
||||||
|
trigger_error ($error);
|
||||||
|
restore_error_handler ();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$status = pg_result_status($result);
|
||||||
|
$rows = pg_affected_rows($result);
|
||||||
|
|
||||||
|
if ($status !== 2) { //The query NOT IS a select
|
||||||
|
if ($rettype == "insert_id") {
|
||||||
|
$result = postgresql_insert_id($dbconnection);
|
||||||
|
}
|
||||||
|
elseif ($rettype == "info") {
|
||||||
|
$result = pg_result_status($result, PGSQL_STATUS_STRING);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$rows = pg_affected_rows($result);
|
||||||
|
$result = $rows;
|
||||||
|
}
|
||||||
|
add_database_debug_trace ($sql, $result, $rows,
|
||||||
|
array ('time' => $time));
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
else { //The query IS a select.
|
||||||
|
add_database_debug_trace ($sql, 0, $rows, array ('time' => $time));
|
||||||
|
while ($row = pg_fetch_assoc($result)) {
|
||||||
|
array_push ($retval, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($cache === true)
|
||||||
|
$sql_cache[$sql] = $retval;
|
||||||
|
pg_free_result ($result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty ($retval)) {
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Return false, check with === or !==
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the rows in a table of the database.
|
||||||
|
*
|
||||||
|
* @param string Database table name.
|
||||||
|
* @param string Field to order by.
|
||||||
|
* @param string $order The type of order, by default 'ASC'.
|
||||||
|
*
|
||||||
|
* @return mixed A matrix with all the values in the table
|
||||||
|
*/
|
||||||
|
function postgresql_get_db_all_rows_in_table($table, $order_field = "", $order = 'ASC') {
|
||||||
|
if ($order_field != "") {
|
||||||
|
return get_db_all_rows_sql ('SELECT * FROM "'.$table.'" ORDER BY "'.$order_field . ' ' . $order);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return get_db_all_rows_sql ('SELECT * FROM "'.$table.'"');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts strings into database
|
||||||
|
*
|
||||||
|
* The number of values should be the same or a positive integer multiple as the number of rows
|
||||||
|
* If you have an associate array (eg. array ("row1" => "value1")) you can use this function with ($table, array_keys ($array), $array) in it's options
|
||||||
|
* All arrays and values should have been cleaned before passing. It's not neccessary to add quotes.
|
||||||
|
*
|
||||||
|
* @param string Table to insert into
|
||||||
|
* @param mixed A single value or array of values to insert (can be a multiple amount of rows)
|
||||||
|
*
|
||||||
|
* @return mixed False in case of error or invalid values passed. Affected rows otherwise
|
||||||
|
*/
|
||||||
|
function postgresql_process_sql_insert($table, $values) {
|
||||||
|
//Empty rows or values not processed
|
||||||
|
if (empty ($values))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
$values = (array) $values;
|
||||||
|
|
||||||
|
$query = sprintf ('INSERT INTO "%s" ', $table);
|
||||||
|
$fields = array ();
|
||||||
|
$values_str = '';
|
||||||
|
$i = 1;
|
||||||
|
$max = count ($values);
|
||||||
|
foreach ($values as $field => $value) {
|
||||||
|
//Add the correct escaping to values
|
||||||
|
if ($field[0] != '"') {
|
||||||
|
$field = '"' . $field . '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
array_push ($fields, $field);
|
||||||
|
|
||||||
|
if (is_null ($value)) {
|
||||||
|
$values_str .= "NULL";
|
||||||
|
}
|
||||||
|
elseif (is_int ($value) || is_bool ($value)) {
|
||||||
|
$values_str .= sprintf("%d", $value);
|
||||||
|
}
|
||||||
|
else if (is_float ($value) || is_double ($value)) {
|
||||||
|
$values_str .= sprintf("%f", $value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$values_str .= sprintf("'%s'", $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($i < $max) {
|
||||||
|
$values_str .= ",";
|
||||||
|
}
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$query .= '(' . implode(', ', $fields) . ')';
|
||||||
|
|
||||||
|
$query .= ' VALUES (' . $values_str . ')';
|
||||||
|
|
||||||
|
return process_sql($query, 'insert_id');
|
||||||
|
}
|
||||||
|
?>
|
@ -46,9 +46,18 @@ function update_config_value ($token, $value) {
|
|||||||
|
|
||||||
switch ($token) {
|
switch ($token) {
|
||||||
case 'list_ACL_IPs_for_API':
|
case 'list_ACL_IPs_for_API':
|
||||||
$rows = get_db_all_rows_sql('SELECT id_config
|
switch ($config["dbtype"]) {
|
||||||
FROM tconfig
|
case "mysql":
|
||||||
WHERE token LIKE "%list_ACL_IPs_for_API_%"');
|
$rows = get_db_all_rows_sql('SELECT id_config
|
||||||
|
FROM tconfig
|
||||||
|
WHERE token LIKE "%list_ACL_IPs_for_API_%"');
|
||||||
|
break;
|
||||||
|
case "postgresql":
|
||||||
|
$rows = get_db_all_rows_sql("SELECT id_config
|
||||||
|
FROM tconfig
|
||||||
|
WHERE token LIKE '%list_ACL_IPs_for_API_%'");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if ($rows !== false) {
|
if ($rows !== false) {
|
||||||
foreach ($rows as $row)
|
foreach ($rows as $row)
|
||||||
|
@ -2168,44 +2168,17 @@ function get_db_sql ($sql, $field = 0, $search_history_db = false) {
|
|||||||
* @return mixed A matrix with all the values returned from the SQL statement or
|
* @return mixed A matrix with all the values returned from the SQL statement or
|
||||||
* false in case of empty result
|
* false in case of empty result
|
||||||
*/
|
*/
|
||||||
function get_db_all_rows_sql ($sql, $search_history_db = false, $cache = true) {
|
function get_db_all_rows_sql($sql, $search_history_db = false, $cache = true) {
|
||||||
global $config;
|
global $config;
|
||||||
$history = array ();
|
|
||||||
|
switch ($config["dbtype"]) {
|
||||||
// To disable globally SQL cache depending on global variable.
|
case "mysql":
|
||||||
// Used in several critical places like Metaconsole trans-server queries
|
return mysql_get_db_all_rows_sql($sql, $search_history_db, $cache);
|
||||||
if (isset($config["dbcache"]))
|
break;
|
||||||
$cache = $config["dbcache"];
|
case "postgresql":
|
||||||
|
return postgresql_get_db_all_rows_sql($sql, $search_history_db, $cache);
|
||||||
// Read from the history DB if necessary
|
break;
|
||||||
if ($search_history_db) {
|
|
||||||
$cache = false;
|
|
||||||
$history = false;
|
|
||||||
|
|
||||||
if (isset($config['history_db_connection']))
|
|
||||||
$history = process_sql ($sql, 'affected_rows', $config['history_db_connection'], false);
|
|
||||||
|
|
||||||
if ($history === false) {
|
|
||||||
$history = array ();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$return = process_sql ($sql, 'affected_rows', $config['dbconnection'], $cache);
|
|
||||||
if ($return === false) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append result to the history DB data
|
|
||||||
if (! empty ($return)) {
|
|
||||||
foreach ($return as $row) {
|
|
||||||
array_push ($history, $row);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! empty ($history))
|
|
||||||
return $history;
|
|
||||||
//Return false, check with === or !==
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2374,71 +2347,17 @@ function clean_cache() {
|
|||||||
*
|
*
|
||||||
* @return mixed An array with the rows, columns and values in a multidimensional array or false in error
|
* @return mixed An array with the rows, columns and values in a multidimensional array or false in error
|
||||||
*/
|
*/
|
||||||
function process_sql ($sql, $rettype = "affected_rows", $dbconnection = '', $cache = true) {
|
function process_sql($sql, $rettype = "affected_rows", $dbconnection = '', $cache = true) {
|
||||||
global $config;
|
global $config;
|
||||||
global $sql_cache;
|
|
||||||
|
|
||||||
$retval = array();
|
switch ($config["dbtype"]) {
|
||||||
|
case "mysql":
|
||||||
if ($sql == '')
|
return mysql_process_sql($sql, $rettype, $dbconnection, $cache);
|
||||||
return false;
|
break;
|
||||||
|
case "postgresql":
|
||||||
if ($cache && ! empty ($sql_cache[$sql])) {
|
return postgresql_process_sql($sql, $rettype, $dbconnection, $cache);
|
||||||
$retval = $sql_cache[$sql];
|
break;
|
||||||
$sql_cache['saved']++;
|
|
||||||
add_database_debug_trace ($sql);
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
$start = microtime (true);
|
|
||||||
if ($dbconnection == '') {
|
|
||||||
$result = mysql_query ($sql);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$result = mysql_query ($sql, $dbconnection);
|
|
||||||
}
|
|
||||||
$time = microtime (true) - $start;
|
|
||||||
if ($result === false) {
|
|
||||||
$backtrace = debug_backtrace ();
|
|
||||||
$error = sprintf ('%s (\'%s\') in <strong>%s</strong> on line %d',
|
|
||||||
mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']);
|
|
||||||
add_database_debug_trace ($sql, mysql_error ());
|
|
||||||
set_error_handler ('sql_error_handler');
|
|
||||||
trigger_error ($error);
|
|
||||||
restore_error_handler ();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
elseif ($result === true) {
|
|
||||||
if ($rettype == "insert_id") {
|
|
||||||
$result = mysql_insert_id ();
|
|
||||||
}
|
|
||||||
elseif ($rettype == "info") {
|
|
||||||
$result = mysql_info ();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$result = mysql_affected_rows ();
|
|
||||||
}
|
|
||||||
|
|
||||||
add_database_debug_trace ($sql, $result, mysql_affected_rows (),
|
|
||||||
array ('time' => $time));
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
add_database_debug_trace ($sql, 0, mysql_affected_rows (),
|
|
||||||
array ('time' => $time));
|
|
||||||
while ($row = mysql_fetch_assoc ($result)) {
|
|
||||||
array_push ($retval, $row);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($cache === true)
|
|
||||||
$sql_cache[$sql] = $retval;
|
|
||||||
mysql_free_result ($result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! empty ($retval))
|
|
||||||
return $retval;
|
|
||||||
//Return false, check with === or !==
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2451,10 +2370,15 @@ function process_sql ($sql, $rettype = "affected_rows", $dbconnection = '', $cac
|
|||||||
* @return mixed A matrix with all the values in the table
|
* @return mixed A matrix with all the values in the table
|
||||||
*/
|
*/
|
||||||
function get_db_all_rows_in_table ($table, $order_field = "", $order = 'ASC') {
|
function get_db_all_rows_in_table ($table, $order_field = "", $order = 'ASC') {
|
||||||
if ($order_field != "") {
|
global $config;
|
||||||
return get_db_all_rows_sql ("SELECT * FROM `".$table."` ORDER BY ".$order_field . " " . $order);
|
|
||||||
} else {
|
switch ($config["dbtype"]) {
|
||||||
return get_db_all_rows_sql ("SELECT * FROM `".$table."`");
|
case "mysql":
|
||||||
|
return mysql_get_db_all_rows_in_table($table, $order_field, $order);
|
||||||
|
break;
|
||||||
|
case "postgresql":
|
||||||
|
return postgresql_get_db_all_rows_in_table($table, $order_field, $order);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3470,48 +3394,16 @@ function get_modulegroup_name ($modulegroup_id) {
|
|||||||
* @return mixed False in case of error or invalid values passed. Affected rows otherwise
|
* @return mixed False in case of error or invalid values passed. Affected rows otherwise
|
||||||
*/
|
*/
|
||||||
function process_sql_insert ($table, $values) {
|
function process_sql_insert ($table, $values) {
|
||||||
//Empty rows or values not processed
|
global $config;
|
||||||
if (empty ($values))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
$values = (array) $values;
|
switch ($config["dbtype"]) {
|
||||||
|
case "mysql":
|
||||||
$query = sprintf ("INSERT INTO `%s` ", $table);
|
return mysql_process_sql_insert ($table, $values);
|
||||||
$fields = array ();
|
break;
|
||||||
$values_str = '';
|
case "postgresql":
|
||||||
$i = 1;
|
return postgresql_process_sql_insert ($table, $values);
|
||||||
$max = count ($values);
|
break;
|
||||||
foreach ($values as $field => $value) { //Add the correct escaping to values
|
|
||||||
if ($field[0] != "`") {
|
|
||||||
$field = "`".$field."`";
|
|
||||||
}
|
|
||||||
|
|
||||||
array_push ($fields, $field);
|
|
||||||
|
|
||||||
if (is_null ($value)) {
|
|
||||||
$values_str .= "NULL";
|
|
||||||
}
|
|
||||||
elseif (is_int ($value) || is_bool ($value)) {
|
|
||||||
$values_str .= sprintf ("%d", $value);
|
|
||||||
}
|
|
||||||
else if (is_float ($value) || is_double ($value)) {
|
|
||||||
$values_str .= sprintf ("%f", $value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$values_str .= sprintf ("'%s'", $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($i < $max) {
|
|
||||||
$values_str .= ",";
|
|
||||||
}
|
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$query .= '('.implode (', ', $fields).')';
|
|
||||||
|
|
||||||
$query .= ' VALUES ('.$values_str.')';
|
|
||||||
|
|
||||||
return process_sql ($query, 'insert_id');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -495,8 +495,10 @@ function install_step4() {
|
|||||||
$cfgin = fopen ("include/config.inc.php","r");
|
$cfgin = fopen ("include/config.inc.php","r");
|
||||||
$cfgout = fopen ($pandora_config,"w");
|
$cfgout = fopen ($pandora_config,"w");
|
||||||
$config_contents = fread ($cfgin, filesize("include/config.inc.php"));
|
$config_contents = fread ($cfgin, filesize("include/config.inc.php"));
|
||||||
|
$dbtype = 'mysql'; //TODO set other types
|
||||||
$config_new = '<?php
|
$config_new = '<?php
|
||||||
// Begin of automatic config file
|
// Begin of automatic config file
|
||||||
|
$config["dbtype"] = "' . $dbtype . '"; //DB type (mysql, postgres)
|
||||||
$config["dbname"]="'.$dbname.'"; // MySQL DataBase name
|
$config["dbname"]="'.$dbname.'"; // MySQL DataBase name
|
||||||
$config["dbuser"]="pandora"; // DB User
|
$config["dbuser"]="pandora"; // DB User
|
||||||
$config["dbpass"]="'.$random_password.'"; // DB Password
|
$config["dbpass"]="'.$random_password.'"; // DB Password
|
||||||
|
Loading…
x
Reference in New Issue
Block a user