2016-07-19 12:39:03 +02:00
< ? php
// Pandora FMS - http://pandorafms.com
// ==================================================
// Copyright (c) 2005-2012 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 General Public License
// as published by the Free Software Foundation for 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.
// Only accesible by ajax
if ( is_ajax ()) {
global $config ;
check_login ();
2017-02-14 09:36:11 +01:00
$updare_rr = get_parameter ( 'updare_rr' , 0 );
2017-03-21 10:39:28 +01:00
$remove_rr = get_parameter ( 'remove_rr' , 0 );
2017-03-21 14:58:42 +01:00
$remove_rr_extras = get_parameter ( 'remove_rr_extras' , 0 );
2016-07-19 12:39:03 +02:00
2017-02-14 09:36:11 +01:00
if ( $updare_rr ) {
2016-07-19 12:39:03 +02:00
$number = get_parameter ( 'number' );
2017-03-16 16:00:57 +01:00
$package = get_parameter ( 'package' );
2017-03-16 18:23:07 +01:00
$ent = get_parameter ( 'ent' );
2017-03-16 19:14:19 +01:00
$offline = get_parameter ( 'offline' );
2017-03-16 18:23:07 +01:00
if ( ! $ent ) {
2017-03-24 11:22:30 +01:00
$dir = $config [ 'attachment_store' ] . " /downloads/pandora_console/extras/mr " ;
2017-03-16 18:23:07 +01:00
}
else {
2017-03-16 19:14:19 +01:00
if ( $offline ) {
2017-03-17 09:02:40 +01:00
$dir = $package . " /extras/mr " ;
2017-03-16 19:14:19 +01:00
}
else {
$dir = sys_get_temp_dir () . " /pandora_oum/ " . $package . " /extras/mr " ;
}
2017-03-16 18:23:07 +01:00
}
2017-03-16 14:31:33 +01:00
$file = " $dir / $number .sql " ;
2016-07-19 13:48:56 +02:00
$dangerous_query = false ;
$mr_file = fopen ( $file , " r " );
while ( ! feof ( $mr_file )) {
$line = fgets ( $mr_file );
if (( preg_match ( " /^drop/ " , $line )) ||
2016-07-19 15:39:31 +02:00
( preg_match ( " /^truncate table/ " , $line ))) {
2016-07-19 13:48:56 +02:00
$dangerous_query = true ;
}
}
if ( $dangerous_query ) {
$error_file = fopen ( $config [ " homedir " ] . " /extras/mr/error.txt " , " w " );
2017-03-14 10:36:10 +01:00
$message = " <div> " ;
$message .= " <div style='width:25%; float:left'><img style='padding-left:20px; padding-top:20px;' src='images/icono_error_mr.png'></div> " ;
$message .= " <div style='width:75%; float:left;'><h3><strong style='font-family:Verdana; font-size:13pt;'>ERROR</strong></h3> " ;
2017-03-15 10:24:29 +01:00
$message .= " <p style='font-family:Verdana; font-size:12pt;'> " . __ ( 'The sql file contains a dangerous query' ) . " </p></div> " ;
2017-03-14 10:36:10 +01:00
$message .= " </div> " ;
2016-07-19 13:48:56 +02:00
fwrite ( $error_file , $message );
fclose ( $error_file );
}
else {
if ( file_exists ( $dir ) && is_dir ( $dir )) {
if ( is_readable ( $dir )) {
2017-03-15 09:36:31 +01:00
if (( $number > $config [ 'MR' ] + 1 ) || ( $number == $config [ 'MR' ])) {
2017-03-14 15:20:12 +01:00
$message = " bad_mr_filename " ;
}
2017-03-15 09:36:31 +01:00
else if ( $config [ " MR " ] > $number ) {
2017-03-21 12:57:02 +01:00
if ( ! file_exists ( $config [ " homedir " ] . " /extras/mr/updated " ) || ! is_dir ( $config [ " homedir " ] . " /extras/mr/updated " )) {
mkdir ( $config [ " homedir " ] . " /extras/mr/updated " );
2016-07-19 13:48:56 +02:00
}
2017-03-16 14:56:10 +01:00
$file_dest = $config [ " homedir " ] . " /extras/mr/updated/ $number .sql " ;
2017-03-21 12:57:02 +01:00
copy ( $file , $file_dest );
2017-03-16 16:28:27 +01:00
$message = " bad_mr_filename " ;
2016-07-19 13:48:56 +02:00
}
else {
$result = db_run_sql_file ( $file );
2017-03-21 15:13:04 +01:00
2016-07-19 13:48:56 +02:00
if ( $result ) {
2017-02-14 09:36:11 +01:00
$update_config = update_config_token ( " MR " , $number );
2016-07-19 13:48:56 +02:00
if ( $update_config ) {
2017-02-14 09:36:11 +01:00
$config [ " MR " ] = $number ;
2016-07-19 12:39:03 +02:00
}
2017-02-14 09:36:11 +01:00
if ( $config [ " MR " ] == $number ) {
2017-03-21 12:57:02 +01:00
if ( ! file_exists ( $config [ " homedir " ] . " /extras/mr/updated " ) || ! is_dir ( $config [ " homedir " ] . " /extras/mr/updated " )) {
mkdir ( $config [ " homedir " ] . " /extras/mr/updated " );
2016-07-19 13:48:56 +02:00
}
2017-03-16 14:56:10 +01:00
$file_dest = $config [ " homedir " ] . " /extras/mr/updated/ $number .sql " ;
2017-03-16 16:28:27 +01:00
2017-03-21 12:57:02 +01:00
copy ( $file , $file_dest );
2016-07-19 12:39:03 +02:00
}
}
2016-07-19 13:48:56 +02:00
else {
$error_file = fopen ( $config [ " homedir " ] . " /extras/mr/error.txt " , " w " );
2017-03-14 10:36:10 +01:00
$message = " <div> " ;
$message .= " <div style='width:25%; float:left'><img style='padding-left:20px; padding-top:20px;' src='images/icono_error_mr.png'></div> " ;
$message .= " <div style='width:75%; float:left;'><h3><strong style='font-family:Verdana; font-size:13pt;'>ERROR</strong></h3> " ;
2017-03-15 10:24:29 +01:00
$message .= " <p style='font-family:Verdana; font-size:12pt;'> " . __ ( 'An error occurred while updating the database schema to the minor release ' ) . $number . " </p></div> " ;
2017-03-14 10:36:10 +01:00
$message .= " </div> " ;
2016-07-19 13:48:56 +02:00
fwrite ( $error_file , $message );
fclose ( $error_file );
}
2016-07-19 12:39:03 +02:00
}
}
2016-07-19 13:48:56 +02:00
else {
$error_file = fopen ( $config [ " homedir " ] . " /extras/mr/error.txt " , " w " );
2017-03-14 10:36:10 +01:00
$message = " <div> " ;
$message .= " <div style='width:25%; float:left'><img style='padding-left:20px; padding-top:20px;' src='images/icono_error_mr.png'></div> " ;
$message .= " <div style='width:75%; float:left;'><h3><strong style='font-family:Verdana; font-size:13pt;'>ERROR</strong></h3> " ;
2017-03-15 10:24:29 +01:00
$message .= " <p style='font-family:Verdana; font-size:12pt;'> " . __ ( 'The directory ' ) . $dir . __ ( ' should have read permissions in order to update the database schema' ) . " </p></div> " ;
2017-03-14 10:36:10 +01:00
$message .= " </div> " ;
2016-07-19 13:48:56 +02:00
fwrite ( $error_file , $message );
fclose ( $error_file );
}
2016-07-19 12:39:03 +02:00
}
else {
$error_file = fopen ( $config [ " homedir " ] . " /extras/mr/error.txt " , " w " );
2017-03-14 10:36:10 +01:00
$message = " <div> " ;
$message .= " <div style='width:25%; float:left'><img style='padding-left:20px; padding-top:20px;' src='images/icono_error_mr.png'></div> " ;
$message .= " <div style='width:75%; float:left;'><h3><strong style='font-family:Verdana; font-size:13pt;'>ERROR</strong></h3> " ;
2017-03-15 10:24:29 +01:00
$message .= " <p style='font-family:Verdana; font-size:12pt;'> " . __ ( 'The directory ' ) . $dir . __ ( ' does not exist' ) . " </p></div> " ;
2017-03-14 10:36:10 +01:00
$message .= " </div> " ;
2016-07-19 12:39:03 +02:00
fwrite ( $error_file , $message );
fclose ( $error_file );
}
}
echo $message ;
return ;
}
2017-03-21 10:39:28 +01:00
if ( $remove_rr ) {
2017-04-06 17:38:05 +02:00
$numbers = get_parameter ( 'number' , 0 );
foreach ( $numbers as $number ) {
for ( $i = 1 ; $i <= $number ; $i ++ ) {
$file = $config [ " homedir " ] . " /extras/mr/ $i .sql " ;
if ( file_exists ( $file )) {
unlink ( $file );
}
}
}
return ;
}
2017-03-21 14:48:12 +01:00
if ( $remove_rr_extras ) {
$dir = $config [ " homedir " ] . " /extras/mr/ " ;
if ( file_exists ( $dir ) && is_dir ( $dir )) {
if ( is_readable ( $dir )) {
$files = scandir ( $dir ); // Get all the files from the directory ordered by asc
if ( $files !== false ) {
$pattern = " /^ \ d+ \ .sql $ / " ;
$sqlfiles = preg_grep ( $pattern , $files ); // Get the name of the correct files
$files = null ;
$pattern = " / \ .sql $ / " ;
$replacement = " " ;
$sqlfiles_num = preg_replace ( $pattern , $replacement , $sqlfiles ); // Get the number of the file
foreach ( $sqlfiles_num as $num ) {
$file = $dir . " $num .sql " ;
if ( file_exists ( $file )) {
unlink ( $file );
}
}
}
}
}
2017-03-21 15:13:04 +01:00
return ;
2017-03-21 14:48:12 +01:00
}
2016-07-19 12:39:03 +02:00
}
?>