From 0618dbbab3190c086d549a0d15144ebe350a5b44 Mon Sep 17 00:00:00 2001 From: Arturo Gonzalez Date: Wed, 1 Jun 2016 14:43:31 +0200 Subject: [PATCH] Rolling release in progress... (Added minor releases) --- pandora_console/general/footer.php | 16 +- pandora_console/include/db/mysql.php | 235 +++++++++++++++++++++++ pandora_console/include/functions.php | 14 ++ pandora_console/include/functions_db.php | 11 +- pandora_console/index.php | 23 +++ 5 files changed, 291 insertions(+), 8 deletions(-) diff --git a/pandora_console/general/footer.php b/pandora_console/general/footer.php index 23022c3d75..d134e8c715 100644 --- a/pandora_console/general/footer.php +++ b/pandora_console/general/footer.php @@ -26,8 +26,22 @@ if (! file_exists ($config["homedir"] . $license_file)) { $license_file = 'general/license/pandora_info_en.html'; } +if (!$config["minor_release_open"]) { + $config["minor_release_open"] = 0; +} +if (enterprise_installed()) { + if (!$config["minor_release_enterprise"]) { + $config["minor_release_enterprise"] = 0; + } +} + echo ''; -echo sprintf(__('Pandora FMS %s - Build %s', $pandora_version, $build_version)); +if (enterprise_installed()) { + echo sprintf(__('Pandora FMS %s - Build %s - MR %s', $pandora_version, $build_version, $config["minor_release_enterprise"])); +} +else { + echo sprintf(__('Pandora FMS %s - Build %s - MR %s', $pandora_version, $build_version, $config["minor_release_open"])); +} echo '
'; echo ''. __('Page generated at') . ' '. ui_print_timestamp ($time, true, array ("prominent" => "timestamp")); //Always use timestamp here echo ''; diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index 3f4a924f67..2419c54bb0 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -1175,4 +1175,239 @@ function mysql_db_process_file ($path, $handle_error = true) { return false; } } + +// --------------------------------------------------------------- +// Initiates a transaction and run the queries of an sql file +// --------------------------------------------------------------- + +function db_run_sql_file ($location) { + global $config; + + // Load file + $commands = file_get_contents($location); + + // Delete comments + $lines = explode("\n", $commands); + $commands = ''; + foreach ($lines as $line) { + $line = trim($line); + if ($line && !preg_match('/^--/', $line) && !preg_match('/^\/\*/', $line)) { + $commands .= $line; + } + } + + // Convert to array + $commands = explode(";", $commands); + + // Run commands + mysql_db_process_sql_begin(); // Begin transaction + foreach ($commands as $command) { + if (trim($command)) { + $result = mysql_query($command); + + if (!$result) { + break; // Error + } + } + } + if ($result) { + mysql_db_process_sql_commit(); // Save results + return true; + } + else { + mysql_db_process_sql_rollback(); // Undo results + return false; + } +} + +/** + * Process the file or files in /extras/mr and apply minor releases. + * + * @return string Return error/success message. + */ +function db_update_schema () { + global $config; + + $dir = $config["homedir"]."/extras/mr"; + $message = ''; + + if (file_exists($dir) && is_dir($dir)) { + if (is_readable($dir)) { + if (enterprise_installed()) { + $files = scandir($dir); // Get all the files from the directory ordered by asc + + if ($files !== false) { + $pattern = "/^\d+\.open\.sql$/"; + $sqlfiles = preg_grep($pattern, $files); // Get the name of the correct files + $pattern = "/\.open\.sql$/"; + $replacement = ""; + $sqlfiles_num = preg_replace($pattern, $replacement, $sqlfiles); // Get the number of the file + $sqlfiles = null; + + if ($sqlfiles_num) { + foreach ($sqlfiles_num as $sqlfile_num) { + + $file = "$dir/$sqlfile_num.open.sql"; + + if ($config["minor_release_open"] >= $sqlfile_num) { + if (!file_exists($dir."/updated") || !is_dir($dir."/updated")) { + mkdir($dir."/updated"); + } + $file_dest = "$dir/updated/$sqlfile_num.open.sql"; + if (copy($file, $file_dest)) { + unlink($file); + } + } + else { + $result = db_run_sql_file($file); + + if ($result) { + $update_config = update_config_token("minor_release_open", $sqlfile_num); + if ($update_config) { + $config["minor_release_open"] = $sqlfile_num; + } + + if ($config["minor_release_open"] == $sqlfile_num) { + if (!file_exists($dir."/updated") || !is_dir($dir."/updated")) { + mkdir($dir."/updated"); + } + + $file_dest = "$dir/updated/$sqlfile_num.open.sql"; + + if (copy($file, $file_dest)) { + unlink($file); + } + } + + $message = "

".__('The database schema has been updated to the minor release')." $sqlfile_num

"; + } + else { + $message = "

".__('An error occurred while updating the database schema to the minor release')." $sqlfile_num

"; + break; + } + } + } + } + + $pattern2 = "/^\d+\.ent\.sql$/"; + $sqlfiles2 = preg_grep($pattern2, $files); // Get the name of the correct files + $files = null; + $pattern2 = "/\.ent\.sql$/"; + $replacement2 = ""; + $sqlfiles_num2 = preg_replace($pattern2, $replacement2, $sqlfiles2); // Get the number of the file + $sqlfiles2 = null; + + if ($sqlfiles_num2) { + foreach ($sqlfiles_num2 as $sqlfile_num2) { + + $file = "$dir/$sqlfile_num2.ent.sql"; + + if ($config["minor_release_enterprise"] >= $sqlfile_num2) { + if (!file_exists($dir."/updated") || !is_dir($dir."/updated")) { + mkdir($dir."/updated"); + } + $file_dest = "$dir/updated/$sqlfile_num2.ent.sql"; + if (copy($file, $file_dest)) { + unlink($file); + } + } + else { + $result = db_run_sql_file($file); + + if ($result) { + $update_config2 = update_config_token("minor_release_enterprise", $sqlfile_num2); + if ($update_config2) { + $config["minor_release_enterprise"] = $sqlfile_num2; + } + + if ($config["minor_release_enterprise"] == $sqlfile_num2) { + if (!file_exists($dir."/updated") || !is_dir($dir."/updated")) { + mkdir($dir."/updated"); + } + $file_dest = "$dir/updated/$sqlfile_num2.ent.sql"; + if (copy($file, $file_dest)) { + unlink($file); + } + } + + $message = "

".__('The database schema has been updated to the minor release')." $sqlfile_num2

"; + } + else { + $message = "

".__('An error occurred while updating the database schema to the minor release')." $sqlfile_num2

"; + break; + } + } + } + } + } + } + else { + $files = scandir($dir); // Get all the files from the directory ordered by asc + + if ($files !== false) { + $pattern = "/^\d+\.open\.sql$/"; + $sqlfiles = preg_grep($pattern, $files); // Get the name of the correct files + $files = null; + $pattern = "/\.open\.sql$/"; + $replacement = ""; + $sqlfiles_num = preg_replace($pattern, $replacement, $sqlfiles); // Get the number of the file + $sqlfiles = null; + + if ($sqlfiles_num) { + foreach ($sqlfiles_num as $sqlfile_num) { + + $file = "$dir/$sqlfile_num.open.sql"; + + if ($config["minor_release_open"] >= $sqlfile_num) { + if (!file_exists($dir."/updated") || !is_dir($dir."/updated")) { + mkdir($dir."/updated"); + } + $file_dest = "$dir/updated/$sqlfile_num.open.sql"; + if (copy($file, $file_dest)) { + unlink($file); + } + } + else { + $result = db_run_sql_file($file); + + if ($result) { + $update_config = update_config_token("minor_release_open", $sqlfile_num); + if ($update_config) { + $config["minor_release_open"] = $sqlfile_num; + } + + if ($config["minor_release_open"] == $sqlfile_num) { + if (!file_exists($dir."/updated") || !is_dir($dir."/updated")) { + mkdir($dir."/updated"); + } + + $file_dest = "$dir/updated/$sqlfile_num.open.sql"; + if (copy($file, $file_dest)) { + unlink($file); + } + } + + $message = "

".__('The database schema has been updated to the minor release')." $sqlfile_num

"; + } + else { + $message = "

".__('An error occurred while updating the database schema to the minor release ')." $sqlfile_num

"; + break; + } + } + } + } + } + } + } + else { + $message = "

".__('The directory '.$dir.' should have read permissions in order to update the database schema')."

"; + } + } + else { + $message = "

".__('The directory '.$dir.' does not exist')."

"; + } + + return $message; +} + ?> diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index b632f7004c..7ac39285e7 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -2619,4 +2619,18 @@ function pandora_setlocale() { setlocale(LC_ALL, str_replace(array_keys($replace_locale), $replace_locale, $user_language)); } + +function update_config_token ($cfgtoken, $cfgvalue) { + global $config; + + $delete = db_process_sql ("DELETE FROM tconfig WHERE token = '$cfgtoken'"); + $insert = db_process_sql ("INSERT INTO tconfig (token, value) VALUES ('$cfgtoken', '$cfgvalue')"); + + if ($delete && $insert) { + return true; + } + else { + return false; + } +} ?> diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 68ff6fa0e6..4a6c8ff684 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -1292,9 +1292,9 @@ function db_check_minor_relase_available () { if ($files !== false) { // Enterprise installed if (enterprise_installed()) { - $pattern = "/^\d+\.open.sql$/"; + $pattern = "/^\d+\.open\.sql$/"; $sqlfiles = preg_grep($pattern, $files); // Get the name of the correct files - $pattern = "/\.open.sql$/"; + $pattern = "/\.open\.sql$/"; $replacement = ""; $sqlfiles_num = preg_replace($pattern, $replacement, $sqlfiles); // Get the number of the file @@ -1302,17 +1302,16 @@ function db_check_minor_relase_available () { if ($sqlfiles_num) { foreach ($sqlfiles_num as $sqlfile_num) { - $file = "$dir/$sqlfile_num.sql"; if ($config["minor_release_open"] < $sqlfile_num) { $have_open_minor = true; } } } - $pattern2 = "/^\d+\.ent.sql$/"; + $pattern2 = "/^\d+\.ent\.sql$/"; $sqlfiles2 = preg_grep($pattern2, $files); // Get the name of the correct files $files = null; - $pattern2 = "/\.ent.sql$/"; + $pattern2 = "/\.ent\.sql$/"; $replacement2 = ""; $sqlfiles_num2 = preg_replace($pattern2, $replacement2, $sqlfiles2); // Get the number of the file @@ -1320,7 +1319,6 @@ function db_check_minor_relase_available () { if ($sqlfiles_num2) { foreach ($sqlfiles_num2 as $sqlfile_num2) { - $file = "$dir/$sqlfile_num2.sql"; if ($config["minor_release_enterprise"] < $sqlfile_num2) { $have_ent_minor = true; } @@ -1339,7 +1337,6 @@ function db_check_minor_relase_available () { if ($sqlfiles_num) { foreach ($sqlfiles_num as $sqlfile_num) { - $file = "$dir/$sqlfile_num.sql"; if ($config["minor_release"] < $sqlfile_num) { $have_open_minor = true; } diff --git a/pandora_console/index.php b/pandora_console/index.php index 7875442db3..0ab42a79b0 100755 --- a/pandora_console/index.php +++ b/pandora_console/index.php @@ -174,6 +174,7 @@ if ($change_pass == 1) { $changed_pass = login_update_password_check ($password_old, $password_new, $password_confirm, $id); } +$minor_release_message = false; $searchPage = false; $search = get_parameter_get("head_search_keywords"); if (strlen($search) > 0) { @@ -430,6 +431,8 @@ if (! isset ($config['id_user'])) { db_logon ($nick_in_db, $_SERVER['REMOTE_ADDR']); $_SESSION['id_usuario'] = $nick_in_db; $config['id_user'] = $nick_in_db; + + $minor_release_message = db_update_schema(); //========================================================== //-------- SET THE CUSTOM CONFIGS OF USER ------------------ @@ -596,6 +599,26 @@ if ($old_global_counter_chat != $now_global_counter_chat) { $_SESSION['new_chat'] = true; } +if ($minor_release_message) { + echo "
$minor_release_message
"; + echo ""; +} + if (get_parameter ('login', 0) !== 0) { // Display news dialog include_once("general/news_dialog.php");