Commit version 24.12.13800

This commit is contained in:
2025-01-06 17:35:06 -05:00
parent b7f6a79c2c
commit 55d9218816
6133 changed files with 4239740 additions and 1374287 deletions

View File

@ -4,9 +4,9 @@
*
* This file is part of Observium.
*
* @package observium
* @subpackage db
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
* @package observium
* @subpackage db
* @copyright (C) Adam Armstrong
*
*/
@ -31,7 +31,7 @@ It's a bit simplistic, but gives you the really useful bits in non-class form.
*/
function dbClientInfo()
{
return mysql_get_client_info();
return mysql_get_client_info();
}
/**
@ -41,7 +41,7 @@ function dbClientInfo()
*/
function dbHostInfo()
{
return mysql_get_host_info();
return mysql_get_host_info();
}
/**
@ -57,106 +57,84 @@ function dbHostInfo()
*/
function dbOpen($host = 'localhost', $user, $password, $database, $charset = 'utf8')
{
// Check host params
$host_array = explode(':', $host);
if (count($host_array) > 1)
{
if ($host_array[0] === 'p')
{
// This is for compatability with new style host option (from mysqli extension)
// p:example.com
// p:::1
array_shift($host_array);
$host = implode(':', $host_array);
$GLOBALS['config']['db_persistent'] = TRUE;
// Check host params
$host_array = explode(':', $host);
if (count($host_array) > 1) {
if ($host_array[0] === 'p') {
// This is for compatability with new style host option (from mysqli extension)
// p:example.com
// p:::1
array_shift($host_array);
$host = implode(':', $host_array);
$GLOBALS['config']['db_persistent'] = TRUE;
} elseif (count($host_array) === 2) {
// IPv6 host not possible here
$host = $host_array[0];
if (is_numeric($host_array[1])) {
// example.com:3306
$port = $host_array[1];
} else {
// example.com:/tmp/mysql.sock
$socket = $host_array[1];
}
}
}
else if (count($host_array) === 2)
{
// IPv6 host not possible here
$host = $host_array[0];
if (is_numeric($host_array[1]))
{
// example.com:3306
$port = $host_array[1];
} else {
// example.com:/tmp/mysql.sock
$socket = $host_array[1];
}
switch ($host) {
case 'localhost':
case '127.0.0.1':
case '::1':
case '':
// For localhost socket prefer
if (strlen($GLOBALS['config']['db_socket'])) {
$host .= ':' . $GLOBALS['config']['db_socket'];
} elseif (strlen($socket)) {
$host .= ':' . $socket;
} elseif (is_numeric($GLOBALS['config']['db_port'])) {
$host .= ':' . $GLOBALS['config']['db_port'];
} elseif (is_numeric($port)) {
$host .= ':' . $port;
}
break;
default:
// All other host uses only port
if (is_numeric($GLOBALS['config']['db_port'])) {
$host .= ':' . $GLOBALS['config']['db_port'];
} elseif (is_numeric($port)) {
$host .= ':' . $port;
}
}
}
switch ($host)
{
case 'localhost':
case '127.0.0.1':
case '::1':
case '':
// For localhost socket prefer
if (strlen($GLOBALS['config']['db_socket']))
{
$host .= ':' . $GLOBALS['config']['db_socket'];
}
else if (strlen($socket))
{
$host .= ':' . $socket;
}
else if (is_numeric($GLOBALS['config']['db_port']))
{
$host .= ':' . $GLOBALS['config']['db_port'];
}
else if (is_numeric($port))
{
$host .= ':' . $port;
}
break;
default:
// All other host uses only port
if (is_numeric($GLOBALS['config']['db_port']))
{
$host .= ':' . $GLOBALS['config']['db_port'];
}
else if (is_numeric($port))
{
$host .= ':' . $port;
}
}
$client_flags = 0;
// Optionally compress connection
if ($GLOBALS['config']['db_compress'] && defined('MYSQL_CLIENT_COMPRESS')) {
$client_flags |= MYSQL_CLIENT_COMPRESS;
}
$client_flags = 0;
// Optionally compress connection
if ($GLOBALS['config']['db_compress'] && defined('MYSQL_CLIENT_COMPRESS'))
{
$client_flags |= MYSQL_CLIENT_COMPRESS;
}
if ($GLOBALS['config']['db_persistent'] && ini_get('mysql.allow_persistent'))
{
// Open a persistent connection
$connection = mysql_pconnect($host, $user, $password, $client_flags);
} else {
// force opening a new link because we might be selecting a different database
$connection = mysql_connect($host, $user, $password, TRUE, $client_flags);
}
if ($connection)
{
if (mysql_select_db($database, $connection))
{
// Connected to DB
if ($charset)
{
mysql_set_charset($charset, $connection);
}
if ($GLOBALS['config']['db_persistent'] && ini_get('mysql.allow_persistent')) {
// Open a persistent connection
$connection = mysql_pconnect($host, $user, $password, $client_flags);
} else {
// DB not exist, reset $connection
if (OBS_DEBUG)
{
echo('MySQL connection error ' . mysql_errno() . ': ' . mysql_error($connection) . PHP_EOL);
}
return NULL;
// force opening a new link because we might be selecting a different database
$connection = mysql_connect($host, $user, $password, TRUE, $client_flags);
}
}
return $connection;
if ($connection) {
if (mysql_select_db($database, $connection)) {
// Connected to DB
if ($charset) {
mysql_set_charset($charset, $connection);
}
} else {
// DB not exist, reset $connection
if (OBS_DEBUG) {
echo('MySQL connection error ' . mysql_errno() . ': ' . mysql_error($connection) . PHP_EOL);
}
return NULL;
}
}
return $connection;
}
/**
@ -168,12 +146,11 @@ function dbOpen($host = 'localhost', $user, $password, $database, $charset = 'ut
*/
function dbClose($connection = NULL)
{
if (is_resource($connection))
{
return mysql_close($connection);
} else {
return mysql_close();
}
if (is_resource($connection)) {
return mysql_close($connection);
} else {
return mysql_close();
}
}
/**
@ -185,12 +162,11 @@ function dbClose($connection = NULL)
*/
function dbError($connection = NULL)
{
if (is_resource($connection))
{
return mysql_error($connection);
} else {
return mysql_error();
}
if (is_resource($connection)) {
return mysql_error($connection);
} else {
return mysql_error();
}
}
/**
@ -202,42 +178,38 @@ function dbError($connection = NULL)
*/
function dbErrorNo($connection = NULL)
{
if (is_resource($connection))
{
return mysql_errno($connection);
} else {
return mysql_errno();
}
if (is_resource($connection)) {
return mysql_errno($connection);
} else {
return mysql_errno();
}
}
function dbPing($connection = NULL)
{
if (is_resource($connection))
{
return mysql_ping($connection);
} else {
return mysql_ping();
}
if (is_resource($connection)) {
return mysql_ping($connection);
} else {
return mysql_ping();
}
}
function dbAffectedRows($connection = NULL)
{
if (is_resource($connection))
{
return mysql_affected_rows($connection);
} else {
return mysql_affected_rows();
}
if (is_resource($connection)) {
return mysql_affected_rows($connection);
} else {
return mysql_affected_rows();
}
}
function dbCallQuery($fullSql, $connection = NULL)
{
if (is_resource($connection))
{
return mysql_query($fullSql, $connection);
} else {
return mysql_query($fullSql);
}
if (is_resource($connection)) {
return mysql_query($fullSql, $connection);
} else {
return mysql_query($fullSql);
}
}
/**
@ -249,12 +221,11 @@ function dbCallQuery($fullSql, $connection = NULL)
*/
function dbEscape($string, $connection = NULL)
{
if (is_resource($connection))
{
return mysql_real_escape_string($string, $connection);
} else {
return mysql_real_escape_string($string);
}
if (is_resource($connection)) {
return mysql_real_escape_string($string, $connection);
} else {
return mysql_real_escape_string($string);
}
}
/**
@ -266,103 +237,98 @@ function dbEscape($string, $connection = NULL)
*/
function dbLastID($connection = NULL)
{
if (is_resource($connection))
{
return mysql_insert_id($connection);
} else {
return mysql_insert_id();
}
if (is_resource($connection)) {
return mysql_insert_id($connection);
} else {
return mysql_insert_id();
}
}
/*
* Fetches all of the rows (associatively) from the last performed query.
* Most other retrieval functions build off this
* */
function dbFetchRows($sql, $parameters = array(), $print_query = FALSE)
function dbFetchRows($sql, $parameters = [], $print_query = FALSE)
{
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
$rows = array();
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
$rows[] = $row;
$rows = [];
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
mysql_free_result($result);
$time_end = utime();
$GLOBALS['db_stats']['fetchrows_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchrows']++;
}
mysql_free_result($result);
$time_end = utime();
$GLOBALS['db_stats']['fetchrows_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchrows']++;
}
// no records, thus return empty array
// which should evaluate to false, and will prevent foreach notices/warnings
return $rows;
// no records, thus return empty array
// which should evaluate to false, and will prevent foreach notices/warnings
return $rows;
}
/*
* Like fetch(), accepts any number of arguments
* The first argument is an sprintf-ready query stringTypes
* */
function dbFetchRow($sql = null, $parameters = array(), $print_query = FALSE)
function dbFetchRow($sql = NULL, $parameters = [], $print_query = FALSE)
{
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
if ($result)
{
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
$time_end = utime();
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
if ($result) {
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
$time_end = utime();
$GLOBALS['db_stats']['fetchrow_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchrow']++;
$GLOBALS['db_stats']['fetchrow_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchrow']++;
return $row;
} else {
return null;
}
return $row;
} else {
return NULL;
}
//$time_start = utime();
//$time_start = utime();
}
/*
* Fetches the first call from the first row returned by the query
* */
function dbFetchCell($sql, $parameters = array(), $print_query = FALSE)
function dbFetchCell($sql, $parameters = [], $print_query = FALSE)
{
$time_start = utime();
//$row = dbFetchRow($sql, $parameters);
$result = dbQuery($sql, $parameters, $print_query);
if ($result)
{
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
$time_end = utime();
$time_start = utime();
//$row = dbFetchRow($sql, $parameters);
$result = dbQuery($sql, $parameters, $print_query);
if ($result) {
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
$time_end = utime();
$GLOBALS['db_stats']['fetchcell_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchcell']++;
$GLOBALS['db_stats']['fetchcell_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchcell']++;
return array_shift($row); // shift first field off first row
}
return array_shift($row); // shift first field off first row
}
return null;
return NULL;
}
function dbBeginTransaction()
{
mysql_query('begin');
mysql_query('begin');
}
function dbCommitTransaction()
{
mysql_query('commit');
mysql_query('commit');
}
function dbRollbackTransaction()
{
mysql_query('rollback');
mysql_query('rollback');
}
/*

View File

@ -6,7 +6,7 @@
*
* @package observium
* @subpackage db
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2021 Observium Limited
* @copyright (C) Adam Armstrong
*
*/
@ -18,10 +18,10 @@ define('OBS_DB_MYSQLND', function_exists('mysqli_get_client_stats'));
/**
* Get MySQL client info
*
* @return string $info
* @return string
*/
function dbClientInfo() {
return mysqli_get_client_info();
return mysqli_get_client_info();
}
/**
@ -29,8 +29,16 @@ function dbClientInfo() {
*
* @return string $info
*/
function dbHostInfo() {
return isset($GLOBALS[OBS_DB_LINK]) ? mysqli_get_host_info($GLOBALS[OBS_DB_LINK]) : '';
function dbHostInfo($connection = NULL) {
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to function mysqli_get_host_info() without link identifier.");
}
return '';
}
return mysqli_get_host_info($connection);
}
/**
@ -45,134 +53,108 @@ function dbHostInfo() {
* @return object $connection
*/
function dbOpen($host, $user, $password, $database, $charset = 'utf8') {
// Check host params
$host_array = explode(':', $host);
if (count($host_array) > 1) {
if ($host_array[0] === 'p') {
// p:example.com
// p:::1
array_shift($host_array);
$host = implode(':', $host_array);
$GLOBALS['config']['db_persistent'] = TRUE;
} elseif (count($host_array) === 2) {
// This is for compatibility with old style host option (from mysql extension)
// IPv6 host not possible here
$host = $host_array[0];
if (is_numeric($host_array[1])) {
// example.com:3306
$port = $host_array[1];
} else {
// example.com:/tmp/mysql.sock
$socket = $host_array[1];
}
}
}
// Server port
if (isset($GLOBALS['config']['db_port']) && is_numeric($GLOBALS['config']['db_port'])) {
$port = $GLOBALS['config']['db_port'];
} elseif (!isset($port)) {
$port = ini_get("mysqli.default_port");
}
// Server port
$port = $GLOBALS['config']['db_port'] ?? ini_get("mysqli.default_port");
// Server socket
if (isset($GLOBALS['config']['db_socket']) && !safe_empty($GLOBALS['config']['db_socket'])) {
$socket = $GLOBALS['config']['db_socket'];
} elseif (!isset($socket)) {
$socket = ini_get("mysqli.default_socket");
}
// Server socket
$socket = $GLOBALS['config']['db_socket'] ?? ini_get("mysqli.default_socket");
// Prepending host by p: for open a persistent connection.
if (isset($GLOBALS['config']['db_persistent']) && $GLOBALS['config']['db_persistent'] &&
ini_get('mysqli.allow_persistent')) {
$host = 'p:' . $host;
}
// Init new connection
$time_start = utime();
$GLOBALS['db_stats']['connect'] = 1;
$connection = mysqli_init();
if ($connection === (object)$connection) {
$client_flags = 0;
// Optionally compress connection
if ($GLOBALS['config']['db_compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
$client_flags |= MYSQLI_CLIENT_COMPRESS;
// Prepending host by p: for open a persistent connection.
if (isset($GLOBALS['config']['db_persistent']) && $GLOBALS['config']['db_persistent'] &&
ini_get('mysqli.allow_persistent')) {
$host = 'p:' . $host;
}
// Optionally enable SSL
if ($GLOBALS['config']['db_ssl']) {
$client_flags |= MYSQLI_CLIENT_SSL;
// Init new connection
$time_start = utime();
$GLOBALS['db_stats']['connect'] = 1;
$connection = mysqli_init();
if ($connection === (object)$connection) {
$client_flags = 0;
// Optionally compress connection
if ($GLOBALS['config']['db_compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
$client_flags |= MYSQLI_CLIENT_COMPRESS;
}
if (!empty($GLOBALS['config']['db_ssl_key']) || !empty($GLOBALS['config']['db_ssl_cert']) ||
!empty($GLOBALS['config']['db_ssl_ca']) || !empty($GLOBALS['config']['db_ssl_ca_path']) ||
!empty($GLOBALS['config']['db_ssl_ciphers'])) {
$db_ssl_key = $GLOBALS['config']['db_ssl_key'] ? $GLOBALS['config']['db_ssl_key'] : '';
$db_ssl_cert = $GLOBALS['config']['db_ssl_cert'] ? $GLOBALS['config']['db_ssl_cert'] : '';
$db_ssl_ca = $GLOBALS['config']['db_ssl_ca'] ? $GLOBALS['config']['db_ssl_ca'] : '';
$db_ssl_ca_path = $GLOBALS['config']['db_ssl_ca_path'] ? $GLOBALS['config']['db_ssl_ca_path'] : '';
$db_ssl_ciphers = $GLOBALS['config']['db_ssl_ciphers'] ? $GLOBALS['config']['db_ssl_ciphers'] : '';
mysqli_ssl_set($connection, $db_ssl_key, $db_ssl_cert, $db_ssl_ca, $db_ssl_ca_path, $db_ssl_ciphers);
}
// Optionally enable SSL
if ($GLOBALS['config']['db_ssl']) {
$client_flags |= MYSQLI_CLIENT_SSL;
// Disables SSL certificate validation on mysqlnd for MySQL 5.6 or later
// https://bugs.php.net/bug.php?id=68344
if (!$GLOBALS['config']['db_ssl_verify']) {
$client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
if (!empty($GLOBALS['config']['db_ssl_key']) || !empty($GLOBALS['config']['db_ssl_cert']) ||
!empty($GLOBALS['config']['db_ssl_ca']) || !empty($GLOBALS['config']['db_ssl_ca_path']) ||
!empty($GLOBALS['config']['db_ssl_ciphers'])) {
$db_ssl_key = $GLOBALS['config']['db_ssl_key'] ?: '';
$db_ssl_cert = $GLOBALS['config']['db_ssl_cert'] ?: '';
$db_ssl_ca = $GLOBALS['config']['db_ssl_ca'] ?: '';
$db_ssl_ca_path = $GLOBALS['config']['db_ssl_ca_path'] ?: '';
$db_ssl_ciphers = $GLOBALS['config']['db_ssl_ciphers'] ?: '';
mysqli_ssl_set($connection, $db_ssl_key, $db_ssl_cert, $db_ssl_ca, $db_ssl_ca_path, $db_ssl_ciphers);
}
mysqli_options($connection, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, $GLOBALS['config']['db_ssl_verify']);
}
// Disables SSL certificate validation on mysqlnd for MySQL 5.6 or later
// https://bugs.php.net/bug.php?id=68344
if (!$GLOBALS['config']['db_ssl_verify']) {
$client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
mysqli_options($connection, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, $GLOBALS['config']['db_ssl_verify']);
}
}
// Connection timeout
//$timeout = (isset($GLOBALS['config']['db_timeout']) && $GLOBALS['config']['db_timeout'] >= 1) ? (int) $GLOBALS['config']['db_timeout'] : 30;
//mysqli_options($connection, MYSQLI_OPT_CONNECT_TIMEOUT, $timeout);
// Convert integer and float columns back to PHP numbers. Boolean returns as int. Only valid for mysqlnd.
/*
if (defined('OBS_DB_MYSQLND') && OBS_DB_MYSQLND)
{
mysqli_options($connection, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, TRUE);
}
*/
// Report if no index or bad index was used in a query
//mysqli_report(MYSQLI_REPORT_INDEX);
// Set default ignore errors on php8.1
// https://php.watch/versions/8.1/mysqli-error-mode
mysqli_report(MYSQLI_REPORT_OFF);
if (!mysqli_real_connect($connection, $host, $user, $password, $database, (int)$port, $socket, $client_flags)) {
$error_num = @mysqli_connect_errno();
if (defined('OBS_DEBUG') && OBS_DEBUG) {
echo('MySQLi connection error ' . $error_num . ': ' . @mysqli_connect_error() . PHP_EOL);
}
if ($error_num == 2006 && PHP_VERSION_ID >= 70400 && PHP_VERSION_ID < 70402) {
print_error("PHP version less than 7.4.2 have multiple issues with MySQL 8.0, please update your PHP to latest.");
}
$GLOBALS['db_stats']['connect_sec'] = elapsed_time($time_start);
return NULL;
}
$GLOBALS['db_stats']['connect_sec'] = elapsed_time($time_start);
//print_vars($GLOBALS['db_stats']['connect_sec']);
if ($charset) {
mysqli_set_charset($connection, $charset);
}
}
// Connection timeout
//$timeout = (isset($GLOBALS['config']['db_timeout']) && $GLOBALS['config']['db_timeout'] >= 1) ? (int) $GLOBALS['config']['db_timeout'] : 30;
//mysqli_options($connection, MYSQLI_OPT_CONNECT_TIMEOUT, $timeout);
return $connection;
}
// Convert integer and float columns back to PHP numbers. Boolean returns as int. Only valid for mysqlnd.
/*
if (defined('OBS_DB_MYSQLND') && OBS_DB_MYSQLND)
{
mysqli_options($connection, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, TRUE);
function dbConnectionValid(&$connection) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {
return TRUE;
}
*/
// Report if no index or bad index was used in a query
//mysqli_report(MYSQLI_REPORT_INDEX);
// Set default ignore errors on php8.1
// https://php.watch/versions/8.1/mysqli-error-mode
mysqli_report(MYSQLI_REPORT_OFF);
if (!mysqli_real_connect($connection, $host, $user, $password, $database, (int)$port, $socket, $client_flags)) {
$error_num = @mysqli_connect_errno();
if (defined('OBS_DEBUG') && OBS_DEBUG) {
echo('MySQLi connection error ' . $error_num . ': ' . @mysqli_connect_error() . PHP_EOL);
}
if ($error_num == 2006 && PHP_VERSION_ID >= 70400 && PHP_VERSION_ID < 70402) {
print_error("PHP version less than 7.4.2 have multiple issues with MySQL 8.0, please update your PHP to latest.");
}
$time_end = utime();
$GLOBALS['db_stats']['connect_sec'] = $time_end - $time_start;
return NULL;
if (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
return TRUE;
}
$time_end = utime();
$GLOBALS['db_stats']['connect_sec'] = $time_end - $time_start;
//print_vars($GLOBALS['db_stats']['connect_sec']);
if ($charset) {
mysqli_set_charset($connection, $charset);
/*
if (version_compare(PHP_VERSION, '5.3.6', '<') && $charset == 'utf8')
{
// Seem as problem to set default charset on php < 5.3.6
mysqli_query($connection, "SET NAMES 'utf8' COLLATE 'utf8_general_ci';");
}
*/
}
}
return $connection;
return FALSE;
}
/**
@ -183,16 +165,15 @@ function dbOpen($host, $user, $password, $database, $charset = 'utf8') {
* @return bool Returns TRUE on success or FALSE on failure.
*/
function dbClose($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to function mysqli_close() without link identifier."); }
return;
}
return mysqli_close($connection);
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to function mysqli_close() without link identifier.");
}
return FALSE;
}
return mysqli_close($connection);
}
/**
@ -203,16 +184,15 @@ function dbClose($connection = NULL) {
* @return string $return
*/
function dbError($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
//if (!OBS_DB_SKIP) { print_error("Call to function mysqli_error() without link identifier."); }
return mysqli_connect_error();
}
return mysqli_error($connection);
if (!dbConnectionValid($connection)) {
// if (!OBS_DB_SKIP) {
// print_error("Call to function mysqli_error() without link identifier.");
// }
return mysqli_connect_error();
}
return mysqli_error($connection);
}
/**
@ -223,103 +203,115 @@ function dbError($connection = NULL) {
* @return int $return
*/
function dbErrorNo($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
//if (!OBS_DB_SKIP) { print_error("Call to function mysqli_errno() without link identifier."); }
return mysqli_connect_errno();
}
return mysqli_errno($connection);
if (!dbConnectionValid($connection)) {
// if (!OBS_DB_SKIP) {
// print_error("Call to function mysqli_errno() without link identifier.");
// }
return mysqli_connect_errno();
}
return mysqli_errno($connection);
}
/**
* Returns array with a list of warnings.
*
* @param mysqli $connection Link to resource with mysql connection, default last used connection
*
* @return array|false
*/
function dbWarnings($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
return;
}
$warning = [];
if (mysqli_warning_count($connection)) {
$e = mysqli_get_warnings($connection);
do {
//echo "Warning: $e->errno: $e->message\n";
$warning[] = "$e->errno: $e->message";
} while ($e->next());
}
return $warning;
if (!dbConnectionValid($connection)) {
// if (!OBS_DB_SKIP) {
// print_error("Call to function mysqli_get_warnings() without link identifier.");
// }
return FALSE;
}
$warning = [];
if (mysqli_warning_count($connection)) {
$e = mysqli_get_warnings($connection);
do {
//echo "Warning: $e->errno: $e->message\n";
$warning[] = "$e->errno: $e->message";
} while ($e -> next());
}
return $warning;
}
function dbPing($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
}
return mysqli_ping($connection);
if (!dbConnectionValid($connection)) {
// if (!OBS_DB_SKIP) {
// print_error("Call to function mysqli_ping() without link identifier.");
// }
return FALSE;
}
return mysqli_ping($connection);
}
function dbAffectedRows($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to function mysqli_affected_rows() without link identifier."); }
return;
}
return mysqli_affected_rows($connection);
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to function mysqli_affected_rows() without link identifier.");
}
return FALSE;
}
return mysqli_affected_rows($connection);
}
function dbCallQuery($fullSql, $connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to function mysqli_query() without link identifier."); }
return;
}
return mysqli_query($connection, $fullSql);
//return mysqli_query($connection, $fullSql, MYSQLI_USE_RESULT); // Unbuffered results, for speedup!
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to function mysqli_query() without link identifier.");
}
return FALSE;
}
return mysqli_query($connection, $fullSql);
//return mysqli_query($connection, $fullSql, MYSQLI_USE_RESULT); // Unbuffered results, for speedup!
}
/**
* Returns escaped string
*
* @param string $string Input string for escape in mysql query
* @param string $string Input string for escape in mysql query
* @param mysqli $connection Link to resource with mysql connection, default last used connection
*
* @return string
*/
function dbEscape($string, $connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to function mysqli_real_escape_string() without link identifier."); }
return;
}
$return = mysqli_real_escape_string($connection, $string);
if (!isset($return[0]) && isset($string[0])) {
// If character set empty, use escape alternative
// FIXME. I really not know why, but in unittests $connection object is lost!
print_debug("Mysql connection lost, in dbEscape() used escape alternative!");
$search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
$replace = array("\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z");
$return = str_replace($search, $replace, $string);
}
return $return;
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to function mysqli_real_escape_string() without link identifier.");
}
//return FALSE;
// FIXME. I really not know why, but in unittests $connection object is lost!
//print_debug("Mysql connection lost, in dbEscape() used escape alternative!");
$search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];
$replace = ["\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z"];
return str_replace($search, $replace, $string);
}
$return = mysqli_real_escape_string($connection, $string);
if (!isset($return[0]) && isset($string[0])) {
// If character set empty, use escape alternative
// FIXME. I really not know why, but in unittests $connection object is lost!
print_debug("Mysql connection lost, in dbEscape() used escape alternative!");
$search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];
$replace = ["\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z"];
$return = str_replace($search, $replace, $string);
}
return $return;
}
/**
@ -330,140 +322,196 @@ function dbEscape($string, $connection = NULL) {
* @return string
*/
function dbLastID($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to function mysqli_insert_id() without link identifier."); }
return;
}
if ($id = mysqli_insert_id($connection)) {
//print_debug("DEBUG ID by function");
//var_dump($id);
return $id;
} else {
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to function mysqli_insert_id() without link identifier.");
}
return FALSE;
}
if ($id = mysqli_insert_id($connection)) {
//print_debug("DEBUG ID by function");
//var_dump($id);
return $id;
}
// mysqli_insert_id() not return last id, after non empty warnings
//print_debug("DEBUG ID by query");
//var_dump($id);
return dbFetchCell('SELECT last_insert_id();');
}
//return mysqli_insert_id($connection);
//return mysqli_insert_id($connection);
}
/*
* Fetches all of the rows (associatively) from the last performed query.
/**
* Fetches all the rows (associatively) from the last performed query.
* Most other retrieval functions build off this
* */
*
* @param string $sql
* @param array $parameters
* @param bool $print_query
*
* @return array
*/
function dbFetchRows($sql, $parameters = [], $print_query = FALSE) {
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
$rows = [];
if ($result instanceof mysqli_result) {
if (OBS_DB_MYSQLND) {
// MySQL Native Driver
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
} else {
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$rows = [];
if ($result instanceof mysqli_result) {
if (OBS_DB_MYSQLND) {
// MySQL Native Driver
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
} else {
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
}
mysqli_free_result($result);
$parent = @debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT|DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function'];
//var_dump($parent);
if ($parent !== 'dbFetchColumn') {
$GLOBALS['db_stats']['fetchrows_sec'] += elapsed_time($time_start);
$GLOBALS['db_stats']['fetchrows']++;
}
}
mysqli_free_result($result);
$time_end = utime();
$GLOBALS['db_stats']['fetchrows_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchrows']++;
}
// no records, thus return empty array
// which should evaluate to false, and will prevent foreach notices/warnings
return $rows;
}
// no records, thus return empty array
// which should evaluate to false, and will prevent foreach notices/warnings
return $rows;
/**
* Process all the rows (associatively) from the last performed query.
* Passed anonymous function to each row.
* It Can be used to skip get a full array of a query and make specific operations.
* NOTE. Do not use it for collect arrays, only for process data
*
* @param callable $func
* @param string $sql
* @param array $parameters
* @param bool $print_query
*
* @return array
*/
function dbFetchFunc($func, $sql, $parameters = [], $print_query = FALSE) {
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
$rows = [];
if ($result instanceof mysqli_result) {
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
if ($return = $func($row, $i++)) {
//$rows += $func($row, $i++); // not correct recursive addition
$rows = array_replace_recursive($rows, (array)$return);
}
if ($return === FALSE) {
// Break query walk on FALSE return
print_debug("DEBUG: dbFetchFunc() stopped while loop. Query:\n".$sql);
break;
}
}
mysqli_free_result($result);
}
$parent = @debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT|DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function'];
//var_dump($parent);
if ($parent === 'dbFetchColumn') {
$GLOBALS['db_stats']['fetchcol_sec'] += elapsed_time($time_start);
$GLOBALS['db_stats']['fetchcol']++;
} else {
$GLOBALS['db_stats']['fetchfunc_sec'] += elapsed_time($time_start);
$GLOBALS['db_stats']['fetchfunc']++;
}
// no records, thus return empty array
// which should evaluate to false, and will prevent foreach notices/warnings
return $rows;
}
/*
* Like fetch(), accepts any number of arguments
* The first argument is an sprintf-ready query stringTypes
* */
function dbFetchRow($sql = NULL, $parameters = array(), $print_query = FALSE) {
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
if ($result) {
$row = mysqli_fetch_assoc($result);
mysqli_free_result($result);
$time_end = utime();
function dbFetchRow($sql = NULL, $parameters = [], $print_query = FALSE) {
$time_start = utime();
$result = dbQuery($sql, $parameters, $print_query);
if ($result) {
$row = mysqli_fetch_assoc($result);
mysqli_free_result($result);
$GLOBALS['db_stats']['fetchrow_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchrow']++;
$GLOBALS['db_stats']['fetchrow_sec'] += elapsed_time($time_start);
$GLOBALS['db_stats']['fetchrow']++;
return $row;
}
return [];
return $row;
}
return [];
}
/*
* Fetches the first call from the first row returned by the query
* */
function dbFetchCell($sql, $parameters = array(), $print_query = FALSE) {
$time_start = utime();
//$row = dbFetchRow($sql, $parameters);
$result = dbQuery($sql, $parameters, $print_query);
if ($result) {
$row = mysqli_fetch_assoc($result);
mysqli_free_result($result);
$time_end = utime();
function dbFetchCell($sql, $parameters = [], $print_query = FALSE) {
$GLOBALS['db_stats']['fetchcell_sec'] += $time_end - $time_start;
$GLOBALS['db_stats']['fetchcell']++;
$time_start = utime();
//$row = dbFetchRow($sql, $parameters);
$result = dbQuery($sql, $parameters, $print_query);
if ($result) {
$row = mysqli_fetch_assoc($result);
mysqli_free_result($result);
if (is_array($row)) {
return array_shift($row); // shift first field off first row
$GLOBALS['db_stats']['fetchcell_sec'] += elapsed_time($time_start);
$GLOBALS['db_stats']['fetchcell']++;
if (is_array($row)) {
return array_shift($row); // shift first field off first row
}
}
}
return NULL; // or ''?
return NULL; // or ''?
}
function dbBeginTransaction($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to begin db transaction without link identifier."); }
return;
}
mysqli_autocommit($connection, FALSE); // Set autocommit to off
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to begin db transaction without link identifier.");
}
return FALSE;
}
mysqli_autocommit($connection, FALSE); // Set autocommit to off
}
function dbCommitTransaction($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to commmit db transaction without link identifier."); }
return;
}
mysqli_commit($connection);
mysqli_autocommit($connection, TRUE); // Restore autocommit to on
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to commit db transaction without link identifier.");
}
return FALSE;
}
mysqli_commit($connection);
mysqli_autocommit($connection, TRUE); // Restore autocommit to on
}
function dbRollbackTransaction($connection = NULL) {
// Observium uses $observium_link global variable name for db link
if ($connection === (object)$connection) {}
elseif (isset($GLOBALS[OBS_DB_LINK]) && $GLOBALS[OBS_DB_LINK] === (object)$GLOBALS[OBS_DB_LINK]) {
$connection = $GLOBALS[OBS_DB_LINK];
} else {
if (!OBS_DB_SKIP) { print_error("Call to rollback db transaction without link identifier."); }
return;
}
mysqli_rollback($connection);
mysqli_autocommit($connection, TRUE); // Restore autocommit to on
if (!dbConnectionValid($connection)) {
if (!OBS_DB_SKIP) {
print_error("Call to rollback db transaction without link identifier.");
}
return FALSE;
}
mysqli_rollback($connection);
mysqli_autocommit($connection, TRUE); // Restore autocommit to on
}
// EOF