diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 75aff3827f..ce2039ba59 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,12 @@ +2011-02-24 Miguel de Dios + + * include/db/postgresql.php: in function "postgresql_connect_db" added + quotes for special names in user or password or host. Fixed in function + "postgresql_process_sql" the output errors. + + * install.php: begin to make a source code to install Pandora in DB schema + on PostgreSQL. + 2011-02-24 Sergio Martin * install.php: Remove the cheching dependences diff --git a/pandora_console/include/db/postgresql.php b/pandora_console/include/db/postgresql.php index 161d6c0bc4..edb4910db4 100644 --- a/pandora_console/include/db/postgresql.php +++ b/pandora_console/include/db/postgresql.php @@ -26,10 +26,10 @@ function postgresql_connect_db($host = null, $db = null, $user = null, $pass = n if ($pass === null) $pass = $config["dbpass"]; - $config['dbconnection'] = pg_connect("host=" . $host . - " dbname=" . $db . - " user=" . $user . - " password=" . $pass); + $config['dbconnection'] = pg_connect("host='" . $host . "'" . + " dbname='" . $db . "'" . + " user='" . $user . "'" . + " password='" . $pass . "'"); if (! $config['dbconnection']) { include ($config["homedir"]."/general/error_authconfig.php"); @@ -208,8 +208,8 @@ function postgresql_process_sql($sql, $rettype = "affected_rows", $dbconnection if ($result === false) { $backtrace = debug_backtrace (); $error = sprintf ('%s (\'%s\') in %s on line %d', - mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']); - add_database_debug_trace ($sql, mysql_error ()); + pg_result_error($result), $sql, $backtrace[0]['file'], $backtrace[0]['line']); + add_database_debug_trace ($sql, pg_result_error($result)); set_error_handler ('sql_error_handler'); trigger_error ($error); restore_error_handler (); diff --git a/pandora_console/install.php b/pandora_console/install.php index e3436692c3..9d03e4aca7 100644 --- a/pandora_console/install.php +++ b/pandora_console/install.php @@ -170,6 +170,29 @@ function parse_mysql_dump($url){ return 0; } +function parse_postgresql_dump($url) { + if (file_exists($url)) { + $file_content = file($url); + + $query = ""; + + foreach($file_content as $sql_line){ + $comment = preg_match("/^(\s|\t)*--.*$/", $line); + if ($comment) { + continue; + } + + $clean_line = $sql_line; + var_dump($clean_line); + } + + return 1; + } + else { + return 0; + } +} + function random_name ($size){ $temp = ""; for ($a=0;$a< $size;$a++) @@ -297,7 +320,6 @@ function install_step2() { echo ""; $res = 0; $res += check_variable(phpversion(),"5.2","PHP version >= 5.2",1); - $res += check_extension("mysql","PHP MySQL extension"); $res += check_extension("gd","PHP GD extension"); $res += check_extension("ldap","PHP LDAP extension"); $res += check_extension("snmp","PHP SNMP extension"); @@ -312,7 +334,14 @@ function install_step2() { else { $res += check_exists ("/usr/bin/twopi","Graphviz Binary"); } - + + echo ""; + check_extension("mysql", "PHP MySQL extension"); + check_extension("pgsql", "PHP PostgreSQL extension"); echo "
"; + echo "DB Engines"; + echo ""; + echo "
"; echo ""; print_logo_status (3,5); @@ -343,6 +372,19 @@ function install_step2() { function install_step3() { + $options = ''; + if (extension_loaded("mysql")) { + $options .= ""; + } + if (extension_loaded("pgsql")) { + $options .= ""; + } + + $error = false; + if (empty($options)) { + $error = true; + } + echo "

Pandora FMS console installation wizard. Step #4 of 5

@@ -365,15 +407,31 @@ function install_step3() { Pandora FMS configuration and Database. Before continue, please be sure that you have no valuable Pandora FMS data in your Database.

-
-
-
DB User with privileges on MySQL
+ "; + if (!$error) { + echo ""; + } + echo "
DB ENGINE
"; + + + if ($error) { + echo " +
+ Warning: You haven't a any DB engine with PHP. Please check the previous step to DB engine dependencies. +
"; + } + else { + echo ""; + } + echo "
DB User with privileges on DB
DB Password for this user
-
DB Hostname of MySQL
+
DB Hostname
DB Name (pandora by default)
@@ -395,16 +453,20 @@ function install_step3() { - -

+ "; + + if (!$error) { + echo "

- + "; + } + echo "
"; - print_logo_status (4,5); + print_logo_status (4,5); - echo " + echo "
Pandora FMS is an OpenSource Software project registered at SourceForge @@ -416,13 +478,15 @@ function install_step4() { $pandora_config = "include/config.php"; if ( (! isset($_POST["user"])) || (! isset($_POST["dbname"])) || (! isset($_POST["host"])) || - (! isset($_POST["pass"])) ) { + (! isset($_POST["pass"])) || (!isset($_POST['engine'])) ) { $dbpassword = ""; $dbuser = ""; $dbhost = ""; $dbname = ""; + $engine = ""; } else { + $engine = $_POST['engine']; $dbpassword = $_POST["pass"]; $dbuser = $_POST["user"]; $dbhost = $_POST["host"]; @@ -454,72 +518,120 @@ function install_step4() {

Creating database and default configuration file

"; - if (! mysql_connect ($dbhost,$dbuser,$dbpassword)) { - check_generic ( 0, "Connection with Database"); - } - else { - check_generic ( 1, "Connection with Database"); - - // Drop database if needed - if ($dbdrop == 1) - mysql_query ("DROP DATABASE IF EXISTS $dbname"); - - // Create schema - $step1 = mysql_query ("CREATE DATABASE $dbname"); - check_generic ($step1, "Creating database '$dbname'"); - if ($step1 == 1) { - $step2 = mysql_select_db($dbname); - check_generic ($step2, "Opening database '$dbname'"); - - $step3 = parse_mysql_dump("pandoradb.sql"); - check_generic ($step3, "Creating schema"); - - $step4 = parse_mysql_dump("pandoradb_data.sql"); - check_generic ($step4, "Populating database"); - - $random_password = random_name (8); - $host = 'localhost'; - if ($dbhost != 'localhost') - $host = $_SERVER['SERVER_ADDR']; - $step5 = mysql_query ("GRANT ALL PRIVILEGES ON $dbname.* to pandora@$host - IDENTIFIED BY '".$random_password."'"); - mysql_query ("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'"); + switch ($engine) { + case 'mysql': + if (! mysql_connect ($dbhost, $dbuser, $dbpassword)) { + check_generic ( 0, "Connection with Database"); + } + else { + check_generic ( 1, "Connection with Database"); - $cfgin = fopen ("include/config.inc.php","r"); - $cfgout = fopen ($pandora_config,"w"); - $config_contents = fread ($cfgin, filesize("include/config.inc.php")); - $dbtype = 'mysql'; //TODO set other types - $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; + // Drop database if needed + if ($dbdrop == 1) + mysql_query ("DROP DATABASE IF EXISTS $dbname"); + + // Create schema + $step1 = mysql_query ("CREATE DATABASE $dbname"); + check_generic ($step1, "Creating database '$dbname'"); + if ($step1 == 1) { + $step2 = mysql_select_db($dbname); + check_generic ($step2, "Opening database '$dbname'"); + + $step3 = parse_mysql_dump("pandoradb.sql"); + check_generic ($step3, "Creating schema"); + + $step4 = parse_mysql_dump("pandoradb_data.sql"); + check_generic ($step4, "Populating database"); + + $random_password = random_name (8); + $host = 'localhost'; + if ($dbhost != 'localhost') + $host = $_SERVER['SERVER_ADDR']; + $step5 = mysql_query ("GRANT ALL PRIVILEGES ON $dbname.* to pandora@$host + IDENTIFIED BY '".$random_password."'"); + mysql_query ("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'; //TODO set other types + $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; + } + break; + case 'pgsql': + $step1 = $step2 = $step3 = $step4 = $step5 = $step6 = $step7 = 0; + + $connection = pg_connect("host='" . $dbhost . "' user='" . $dbuser . "' password='" . $dbpassword . "'"); + if ($connection === false) { + check_generic(0, "Connection with Database"); + } + else { + check_generic(1, "Connection with Database"); + + // Drop database if needed + if ($dbdrop == 1) { + pg_query($connection, "DROP DATABASE \"" . $dbname . "\";"); + } + + pg_send_query($connection, "CREATE DATABASE \"" . $dbname . "\" WITH ENCODING 'utf8';"); + $result = pg_get_result($connection); + if (pg_result_status($result) != PGSQL_FATAL_ERROR) { + $step1 = 1; + } + + check_generic ($step1, "Creating database '$dbname'"); + + if ($step1 == 1) { + //Reopen DB because I don't know how to use DB in PostgreSQL + pg_close($connection); + + $connection = pg_connect("host='" . $dbhost . "' dbname='" . $dbname . + "' user='" . $dbuser . "' password='" . $dbpassword . "'"); + + if ($connection !== false) { + $step2 = 1; + } + } + + check_generic ($step2, "Opening database '$dbname'"); + + if ($step2) { + $step3 = parse_postgresql_dump("pandoradb.postgreSQL.sql"); + check_generic ($step3, "Creating schema"); + } + } + break; } echo "
"; - print_logo_status (4,5); + print_logo_status(4,5); echo "
"; if ($everything_ok == 1) { @@ -533,11 +645,19 @@ function install_step4() { All database schemes created in this step have been dropped.

"; - if (mysql_error() != "") - echo "
ERROR: ". mysql_error().".
"; - - if ($step1 == 1) - mysql_query ("DROP DATABASE $dbname"); + switch ($engine) { + case 'mysql': + if (mysql_error() != "") { + echo "
ERROR: ". mysql_error().".
"; + } + + if ($step1 == 1) { + mysql_query ("DROP DATABASE $dbname"); + } + break; + case 'pgsql': + break; + } } echo "