Added support for mysqli with php7.0.

This commit is contained in:
Junichi Satoh 2016-11-24 09:43:28 +09:00
parent fcc2a245bf
commit e8a466aaf7
2 changed files with 318 additions and 50 deletions

View File

@ -28,8 +28,21 @@ function mysql_connect_db($host = null, $db = null, $user = null, $pass = null,
if ($port === null)
$port = $config["dbport"];
if ($config["mysqli"] === null && extension_loaded(mysqli))
$config["mysqli"] = 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().
if ($config["mysqli"] === true) {
$connect_id = mysqli_connect($host, $user, $pass, $db, $port);
if (mysqli_connect_error() > 0) {
return false;
}
db_change_cache_id ($db, $host);
mysqli_select_db($connect_id, $db);
}
else {
$connect_id = @mysql_connect($host . ":" . $port, $user, $pass, true);
if (! $connect_id) {
return false;
@ -38,6 +51,8 @@ function mysql_connect_db($host = null, $db = null, $user = null, $pass = null,
db_change_cache_id ($db, $host);
mysql_select_db($db, $connect_id);
}
return $connect_id;
}
@ -301,20 +316,47 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection =
$dbconnection = $config['dbconnection'];
}
if ($config["mysqli"] === true) {
$result = mysqli_query ($dbconnection, $sql);
}
else {
$result = mysql_query ($sql, $dbconnection);
}
$time = microtime (true) - $start;
if ($result === false) {
$backtrace = debug_backtrace ();
if ($config["mysqli"] === true) {
$error = sprintf ('%s (\'%s\') in <strong>%s</strong> on line %d',
mysqli_error ($dbconnection), $sql, $backtrace[0]['file'], $backtrace[0]['line']);
db_add_database_debug_trace ($sql, mysqli_error ($dbconnection));
}
else {
$error = sprintf ('%s (\'%s\') in <strong>%s</strong> on line %d',
mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']);
db_add_database_debug_trace ($sql, mysql_error ($dbconnection));
}
set_error_handler ('db_sql_error_handler');
trigger_error ($error);
restore_error_handler ();
return false;
}
elseif ($result === true) {
if ($config["mysqli"] === true) {
if ($rettype == "insert_id") {
$result = mysqli_insert_id ($dbconnection);
}
elseif ($rettype == "info") {
$result = mysqli_info ($dbconnection);
}
else {
$result = mysqli_affected_rows ($dbconnection);
}
db_add_database_debug_trace ($sql, $result, mysqli_affected_rows ($dbconnection),
array ('time' => $time));
}
else {
if ($rettype == "insert_id") {
$result = mysql_insert_id ($dbconnection);
}
@ -327,8 +369,22 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection =
db_add_database_debug_trace ($sql, $result, mysql_affected_rows ($dbconnection),
array ('time' => $time));
}
return $result;
}
else {
if ($config["mysqli"] === true) {
db_add_database_debug_trace ($sql, 0, mysqli_affected_rows ($dbconnection),
array ('time' => $time));
while ($row = mysqli_fetch_assoc ($result)) {
array_push ($retval, $row);
}
if ($cache === true)
$sql_cache[$sql_cache ['id']][$sql] = $retval;
mysqli_free_result ($result);
}
else {
db_add_database_debug_trace ($sql, 0, mysql_affected_rows ($dbconnection),
array ('time' => $time));
@ -341,6 +397,7 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection =
mysql_free_result ($result);
}
}
}
if (! empty ($retval))
return $retval;
@ -357,7 +414,18 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection =
* @return string String cleaned.
*/
function mysql_escape_string_sql($string) {
global $config;
$dbconnection = $config['dbconnection'];
if ($dbconnection == null) {
$dbconnection = mysql_connect_db();
}
if ($config["mysqli"] === true) {
$str = mysqli_real_escape_string($dbconnection, $string);
}
else {
$str = mysql_real_escape_string($string);
}
return $str;
}
@ -756,14 +824,21 @@ function mysql_db_get_all_rows_filter ($table, $filter = array(), $fields = fals
function mysql_db_get_num_rows ($sql) {
global $config;
if ($config["mysqli"] === true) {
$result = mysqli_query($config['dbconnection'], $sql);
if ($result) {
return mysqli_num_rows($result);
}
}
else {
$result = mysql_query($sql, $config['dbconnection']);
if ($result) {
return mysql_num_rows($result);
}
else {
return 0;
}
return 0;
}
/**
@ -974,39 +1049,73 @@ function mysql_db_process_sql_delete($table, $where, $where_join = 'AND') {
* @return mixed The row or false in error.
*/
function mysql_db_get_all_row_by_steps_sql($new = true, &$result, $sql = null) {
global $config;
if ($config["mysqli"] === true) {
if ($new == true)
$result = mysqli_query($config['dbconnection'], $sql);
if ($result) {
return mysqli_fetch_assoc($result);
}
}
else {
if ($new == true)
$result = mysql_query($sql);
if ($result) {
return mysql_fetch_assoc($result);
}
else {
return array();
}
return array();
}
/**
* Starts a database transaction.
*/
function mysql_db_process_sql_begin() {
global $config;
if ($config["mysqli"] === true) {
mysqli_query ($config['dbconnection'], 'SET AUTOCOMMIT = 0');
mysqli_query ($config['dbconnection'], 'START TRANSACTION');
}
else {
mysql_query ('SET AUTOCOMMIT = 0');
mysql_query ('START TRANSACTION');
}
}
/**
* Commits a database transaction.
*/
function mysql_db_process_sql_commit() {
global $config;
if ($config["mysqli"] === true) {
mysqli_query ($config['dbconnection'], 'COMMIT');
mysqli_query ($config['dbconnection'], 'SET AUTOCOMMIT = 1');
}
else {
mysql_query ('COMMIT');
mysql_query ('SET AUTOCOMMIT = 1');
}
}
/**
* Rollbacks a database transaction.
*/
function mysql_db_process_sql_rollback() {
global $config;
if ($config["mysqli"] === true) {
mysqli_query ($config['dbconnection'], 'ROLLBACK ');
mysqli_query ($config['dbconnection'], 'SET AUTOCOMMIT = 1');
}
else {
mysql_query ('ROLLBACK ');
mysql_query ('SET AUTOCOMMIT = 1');
}
}
/**
@ -1020,7 +1129,7 @@ function mysql_safe_sql_string($string) {
global $config;
return mysql_real_escape_string($string, $config['dbconnection']);
return mysql_real_escape_string($config['dbconnection'], $string);
}
/**
@ -1029,7 +1138,14 @@ function mysql_safe_sql_string($string) {
* @return string Return the string error.
*/
function mysql_db_get_last_error() {
global $config;
if ($config["mysqli"] === true) {
return mysqli_error();
}
else {
return mysql_error();
}
}
/**
@ -1066,9 +1182,18 @@ function mysql_get_system_time() {
* @return mixed Return the type name or False in error case.
*/
function mysql_db_get_type_field_table($table, $field) {
global $config;
if ($config["mysqli"] === true) {
$result = mysqli_query($config['dbconnection'], 'SELECT parameters FROM ' . $table);
return mysqli_field_type($result, $field);
}
else {
$result = mysql_query('SELECT parameters FROM ' . $table);
return mysql_field_type($result, $field);
}
}
/**
@ -1092,8 +1217,13 @@ function mysql_db_get_table_count($sql, $search_history_db = false) {
// Connect to the history DB
if (! isset ($config['history_db_connection']) || $config['history_db_connection'] === false) {
if ($config["mysqli"] === true) {
$config['history_db_connection'] = mysqli_connect_db ($config['history_db_host'], $config['history_db_user'], io_output_password($config['history_db_pass']), $config['history_db_name'], $config['history_db_port'], false);
}
else {
$config['history_db_connection'] = mysql_connect_db ($config['history_db_host'], $config['history_db_name'], $config['history_db_user'], io_output_password($config['history_db_pass']), $config['history_db_port'], false);
}
}
if ($config['history_db_connection'] !== false) {
$history_count = mysql_db_get_value_sql ($sql, $config['history_db_connection']);
if ($history_count === false) {
@ -1118,7 +1248,7 @@ function mysql_get_fields($table) {
* Based on the function which installs the pandoradb.sql schema.
*
* @param string $path File path.
* @param bool $handle_error Whether to handle the mysql_query errors or throw an exception.
* @param bool $handle_error Whether to handle the mysqli_query/mysql_query errors or throw an exception.
*
* @return bool Return the final status of the operation.
*/
@ -1138,7 +1268,13 @@ function mysql_db_process_file ($path, $handle_error = true) {
$query .= $sql_line;
if (preg_match("/;[\040]*\$/", $sql_line)) {
if (!$result = mysql_query($query)) {
if ($config["mysqli"] === true) {
$query_result = mysqli_query($config['dbconnection'], $query);
}
else {
$query_result = mysql_query($query);
}
if (!$result = $query_result) {
// Error. Rollback the transaction
mysql_db_process_sql_rollback();

View File

@ -235,6 +235,29 @@ function parse_mysql_dump($url) {
return 0;
}
function parse_mysqli_dump($connection, $url) {
if (file_exists($url)) {
$file_content = file($url);
$query = "";
foreach($file_content as $sql_line) {
if (trim($sql_line) != "" && strpos($sql_line, "--") === false) {
$query .= $sql_line;
if(preg_match("/;[\040]*\$/", $sql_line)) {
if (!$result = mysqli_query($connection, $query)) {
echo mysqli_error(); //Uncomment for debug
echo "<i><br>$query<br></i>";
return 0;
}
$query = "";
}
}
}
return 1;
}
else
return 0;
}
function parse_postgresql_dump($connection, $url, $debug = false) {
if (file_exists($url)) {
$file_content = file($url);
@ -436,6 +459,9 @@ function adjust_paths_for_freebsd($engine, $connection = false) {
case 'mysql':
$result = mysql_query($adjust_sql[$i]);
break;
case 'mysqli':
$result = mysqli_query($connection, $adjust_sql[$i]);
break;
case 'oracle':
//Delete the last semicolon from current query
$query = substr($adjust_sql[$i], 0, strlen($adjust_sql[$i]) - 1);
@ -584,6 +610,7 @@ function install_step2() {
echo "</td><td>";
echo "</td></tr>";
check_extension("mysql", "PHP MySQL extension");
check_extension("mysqli", "PHP MySQL(mysqli) extension");
check_extension("pgsql", "PHP PostgreSQL extension");
check_extension("oci8", "PHP Oracle extension");
echo "</table>";
@ -628,6 +655,9 @@ function install_step3() {
if (extension_loaded("mysql")) {
$options .= "<option value='mysql'>MySQL</option>";
}
if (extension_loaded("mysqli")) {
$options .= "<option value='mysqli'>MySQL(mysqli)</option>";
}
if (extension_loaded("pgsql")) {
$options .= "<option value='pgsql'>PostgreSQL</option>";
}
@ -906,6 +936,99 @@ function install_step4() {
}
}
if (($step7 + $step6 + $step5 + $step4 + $step3 + $step2 + $step1) == 7) {
$everything_ok = 1;
}
break;
case 'mysqli':
$connection = mysqli_connect ($dbhost, $dbuser, $dbpassword);
if (mysqli_connect_error() > 0) {
check_generic ( 0, "Connection with Database");
}
else {
check_generic ( 1, "Connection with Database");
// Drop database if needed and don't want to install over an existing DB
if ($dbdrop == 1) {
mysqli_query ($connection, "DROP DATABASE IF EXISTS `$dbname`");
}
// Create schema
if ($dbaction == 'db_new' || $dbdrop == 1) {
$step1 = mysqli_query ($connection, "CREATE DATABASE `$dbname`");
check_generic ($step1, "Creating database '$dbname'");
}
else {
$step1 = 1;
}
if ($step1 == 1) {
$step2 = mysqli_select_db($connection, $dbname);
check_generic ($step2, "Opening database '$dbname'");
$step3 = parse_mysqli_dump($connection, "pandoradb.sql");
check_generic ($step3, "Creating schema");
$step4 = parse_mysqli_dump($connection, "pandoradb_data.sql");
check_generic ($step4, "Populating database");
if (PHP_OS == "FreeBSD") {
$step_freebsd = adjust_paths_for_freebsd ($engine, $connection);
check_generic ($step_freebsd, "Adjusting paths in database for FreeBSD");
}
$random_password = random_name (8);
$host = $dbhost; // set default granted origin to the origin of the queries
if (($dbhost != 'localhost') && ($dbhost != '127.0.0.1'))
$host = $dbgrant; // if the granted origin is different from local machine, set the valid origin
$step5 = mysqli_query ($connection, "GRANT ALL PRIVILEGES ON `$dbname`.* to pandora@$host
IDENTIFIED BY '".$random_password."'");
mysqli_query ($connection, "FLUSH PRIVILEGES");
check_generic ($step5, "Established privileges for user pandora. A new random password has been generated: <b>$random_password</b><div class='warn'>Please write it down, you will need to setup your Pandora FMS server, editing the </i>/etc/pandora/pandora_server.conf</i> file</div>");
$step6 = is_writable("include");
check_generic ($step6, "Write permissions to save config file in './include'");
$cfgin = fopen ("include/config.inc.php","r");
$cfgout = fopen ($pandora_config,"w");
$config_contents = fread ($cfgin, filesize("include/config.inc.php"));
$dbtype = 'mysql';
$config_new = '<?php
// Begin of automatic config file
$config["dbtype"] = "' . $dbtype . '"; //DB type (mysql, postgresql...in future others)
$config["mysqli"] = true;
$config["dbname"]="'.$dbname.'"; // MySQL DataBase name
$config["dbuser"]="pandora"; // DB User
$config["dbpass"]="'.$random_password.'"; // DB Password
$config["dbhost"]="'.$dbhost.'"; // DB Host
$config["homedir"]="'.$path.'"; // Config homedir
/*
----------Attention--------------------
Please note that in certain installations:
- reverse proxy.
- web server in other ports.
- https
This variable might be dynamically altered.
But it is save as backup in the
$config["homeurl_static"]
for expecial needs.
----------Attention--------------------
*/
$config["homeurl"]="'.$url.'"; // Base URL
$config["homeurl_static"]="'.$url.'"; // Don\'t delete
// End of automatic config file
?>';
$step7 = fputs ($cfgout, $config_new);
$step7 = $step7 + fputs ($cfgout, $config_contents);
if ($step7 > 0)
$step7 = 1;
fclose ($cfgin);
fclose ($cfgout);
chmod ($pandora_config, 0600);
check_generic ($step7, "Created new config file at '".$pandora_config."'");
}
}
if (($step7 + $step6 + $step5 + $step4 + $step3 + $step2 + $step1) == 7) {
$everything_ok = 1;
}
@ -1225,6 +1348,15 @@ function install_step4() {
mysql_query ("DROP DATABASE $dbname");
}
break;
case 'mysqli':
if (mysqli_error($connection) != "") {
echo "<div class='err'> <b>ERROR:</b> ". mysqli_error($connection).".</div>";
}
if ($step1 == 1) {
mysqli_query ($connection, "DROP DATABASE $dbname");
}
break;
case 'pgsql':
break;
case 'oracle':