diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index 3f4a924f67..b4eee468b9 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -27,17 +27,32 @@ function mysql_connect_db($host = null, $db = null, $user = null, $pass = null, $pass = $config["dbpass"]; 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(). - $connect_id = @mysql_connect($host . ":" . $port, $user, $pass, true); - if (! $connect_id) { - return false; + 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; + } + + db_change_cache_id ($db, $host); + + mysql_select_db($db, $connect_id); + } - db_change_cache_id ($db, $host); - - mysql_select_db($db, $connect_id); return $connect_id; } @@ -301,44 +316,86 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection = $dbconnection = $config['dbconnection']; } - $result = mysql_query ($sql, $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 (); - $error = sprintf ('%s (\'%s\') in %s on line %d', - mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']); - db_add_database_debug_trace ($sql, mysql_error ($dbconnection)); + if ($config["mysqli"] === true) { + $error = sprintf ('%s (\'%s\') in %s 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 %s 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 ($rettype == "insert_id") { - $result = mysql_insert_id ($dbconnection); - } - elseif ($rettype == "info") { - $result = mysql_info ($dbconnection); + 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 { - $result = mysql_affected_rows ($dbconnection); + if ($rettype == "insert_id") { + $result = mysql_insert_id ($dbconnection); + } + elseif ($rettype == "info") { + $result = mysql_info ($dbconnection); + } + else { + $result = mysql_affected_rows ($dbconnection); + } + + db_add_database_debug_trace ($sql, $result, mysql_affected_rows ($dbconnection), + array ('time' => $time)); } - db_add_database_debug_trace ($sql, $result, mysql_affected_rows ($dbconnection), - array ('time' => $time)); return $result; } else { - db_add_database_debug_trace ($sql, 0, mysql_affected_rows ($dbconnection), - array ('time' => $time)); - while ($row = mysql_fetch_assoc ($result)) { - array_push ($retval, $row); - } + 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; - mysql_free_result ($result); + 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)); + while ($row = mysql_fetch_assoc ($result)) { + array_push ($retval, $row); + } + + if ($cache === true) + $sql_cache[$sql_cache ['id']][$sql] = $retval; + mysql_free_result ($result); + } } } @@ -357,7 +414,18 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection = * @return string String cleaned. */ function mysql_escape_string_sql($string) { - $str = mysql_real_escape_string($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; - $result = mysql_query($sql, $config['dbconnection']); + if ($config["mysqli"] === true) { + $result = mysqli_query($config['dbconnection'], $sql); - if ($result) { - return mysql_num_rows($result); + if ($result) { + return mysqli_num_rows($result); + } } - else { - return 0; + else { + $result = mysql_query($sql, $config['dbconnection']); + + if ($result) { + return mysql_num_rows($result); + } } + 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) { - if ($new == true) - $result = mysql_query($sql); + global $config; + + if ($config["mysqli"] === true) { + if ($new == true) + $result = mysqli_query($config['dbconnection'], $sql); - if ($result) { - return mysql_fetch_assoc($result); + if ($result) { + return mysqli_fetch_assoc($result); + } } else { - return array(); + if ($new == true) + $result = mysql_query($sql); + + if ($result) { + return mysql_fetch_assoc($result); + } } + return array(); } /** * Starts a database transaction. */ function mysql_db_process_sql_begin() { - mysql_query ('SET AUTOCOMMIT = 0'); - mysql_query ('START TRANSACTION'); + 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() { - mysql_query ('COMMIT'); - mysql_query ('SET AUTOCOMMIT = 1'); + 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() { - mysql_query ('ROLLBACK '); - mysql_query ('SET AUTOCOMMIT = 1'); + 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() { - return mysql_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) { - $result = mysql_query('SELECT parameters FROM ' . $table); + global $config; + + if ($config["mysqli"] === true) { + $result = mysqli_query($config['dbconnection'], 'SELECT parameters FROM ' . $table); - return mysql_field_type($result, $field); + return mysqli_field_type($result, $field); + } + else { + $result = mysql_query('SELECT parameters FROM ' . $table); + + return mysql_field_type($result, $field); + } } /** @@ -1092,7 +1217,12 @@ 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) { - $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["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']); @@ -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(); diff --git a/pandora_console/install.php b/pandora_console/install.php index 347d78c48d..93042a8e79 100644 --- a/pandora_console/install.php +++ b/pandora_console/install.php @@ -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 "
$query
"; + 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 ""; echo ""; 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 ""; @@ -628,6 +655,9 @@ function install_step3() { if (extension_loaded("mysql")) { $options .= ""; } + if (extension_loaded("mysqli")) { + $options .= ""; + } if (extension_loaded("pgsql")) { $options .= ""; } @@ -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: $random_password
Please write it down, you will need to setup your Pandora FMS server, editing the /etc/pandora/pandora_server.conf file
"); + + $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 = ''; + $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 "
ERROR: ". mysqli_error($connection).".
"; + } + + if ($step1 == 1) { + mysqli_query ($connection, "DROP DATABASE $dbname"); + } + break; case 'pgsql': break; case 'oracle':