Removed oracle and postgre support on installation. Ticket #457

This commit is contained in:
Arturo Gonzalez 2017-03-06 12:44:07 +01:00
parent 7f0359283d
commit a46d4502e6
1 changed files with 1 additions and 451 deletions

View File

@ -71,7 +71,7 @@
<div style='height: 10px'>
<?php
$version = 'NG_BETA';
$build = '170306';
$build = '170303';
$banner = "v$version Build $build";
error_reporting(0);
@ -258,160 +258,6 @@ function parse_mysqli_dump($connection, $url) {
return 0;
}
function parse_postgresql_dump($connection, $url, $debug = false) {
if (file_exists($url)) {
$file_content = file($url);
$query = "";
foreach ($file_content as $sql_line) {
$clean_line = trim($sql_line);
$comment = preg_match("/^(\s|\t)*--.*$/", $clean_line);
if ($comment) {
continue;
}
if (empty($clean_line)) {
continue;
}
$query .= $clean_line;
//Check if the end of query with the the semicolon and any returns in the end of line
if(preg_match("/;[\040]*\$/", $clean_line)) {
//And execute and clean buffer
pg_send_query($connection, $query);
$result = pg_get_result($connection);
if ($debug) {
var_dump($query);
var_dump(pg_result_error($result));
}
if (pg_result_status($result) == PGSQL_FATAL_ERROR) {
echo pg_result_error($result);
echo "<i><br>$query<br></i>";
return 0;
}
$query = "";
}
}
return 1;
}
else {
return 0;
}
}
function parse_oracle_dump($connection, $url, $debug = false) {
if (file_exists($url)) {
$file_content = file($url);
$query = "";
$plsql_block = false;
$datetime_tz_format = oci_parse($connection, 'alter session set NLS_TIMESTAMP_TZ_FORMAT =\'YYYY-MM-DD HH24:MI:SS\'');
$datetime_format = oci_parse($connection, 'alter session set NLS_TIMESTAMP_FORMAT =\'YYYY-MM-DD HH24:MI:SS\'');
$date_format = oci_parse($connection, 'alter session set NLS_DATE_FORMAT =\'YYYY-MM-DD HH24:MI:SS\'');
$decimal_separator = oci_parse($connection, 'alter session set NLS_NUMERIC_CHARACTERS =\',.\'');
oci_execute($datetime_tz_format);
oci_execute($datetime_format);
oci_execute($date_format);
oci_execute($decimal_separator);
oci_free_statement($datetime_tz_format);
oci_free_statement($datetime_format);
oci_free_statement($date_format);
oci_free_statement($decimal_separator);
foreach ($file_content as $sql_line) {
$clean_line = trim($sql_line);
$comment = preg_match("/^(\s|\t)*--.*$/", $clean_line);
if ($comment) {
continue;
}
if (empty($clean_line)) {
continue;
}
//Support for PL/SQL blocks
if (preg_match("/^BEGIN$/", $clean_line)) {
$query .= $clean_line . ' ';
$plsql_block = true;
}
else {
$query .= $clean_line;
}
//Check query's end with a back slash and any returns in the end of line or if it's a PL/SQL block 'END;;' string
if ((preg_match("/;[\040]*\$/", $clean_line) && !$plsql_block) ||
(preg_match("/^END;;[\040]*\$/", $clean_line) && $plsql_block)) {
$plsql_block = false;
//Execute and clean buffer
//Delete the last semicolon from current query
$query = substr($query, 0, strlen($query) - 1);
$sql = oci_parse($connection, $query);
$result = oci_execute($sql);
if ($debug) {
var_dump($query);
}
if (!$result) {
$e = oci_error($sql);
echo "<tr><td><div class='warn'>Errors creating schema:</div><div style=\"overflow:auto; height:50px;\" >";
echo htmlentities($e['message'], ENT_QUOTES);
echo "<i><br>$query<br></i>";
echo "</div></td></tr>";
return 0;
}
$query = "";
oci_free_statement($sql);
}
}
return 1;
}
else {
return 0;
}
}
function oracle_drop_all_objects ($connection) {
//Drop all objects of the current installation
$stmt = oci_parse($connection,
"BEGIN " .
"FOR cur_rec IN (SELECT object_name, object_type " .
"FROM user_objects " .
"WHERE object_type IN ('TABLE', 'VIEW', 'PACKAGE', 'PROCEDURE', 'FUNCTION', 'SEQUENCE', 'SNAPSHOT', 'MATERIALIZED VIEW')) LOOP " .
"BEGIN " .
"IF cur_rec.object_type = 'TABLE' THEN " .
"EXECUTE IMMEDIATE 'DROP ' || cur_rec.object_type || ' \"' || cur_rec.object_name || '\" CASCADE CONSTRAINTS'; " .
"ELSE " .
"EXECUTE IMMEDIATE 'DROP ' || cur_rec.object_type || ' \"' || cur_rec.object_name || '\"'; " .
"END IF; " .
"EXCEPTION " .
"WHEN OTHERS THEN " .
"DBMS_OUTPUT.put_line('FAILED: DROP ' || cur_rec.object_type || ' \"' || cur_rec.object_name || '\"'); " .
"END; " .
"END LOOP; " .
"END; ");
$result = oci_execute($stmt);
oci_free_statement($stmt);
return 0;
}
function random_name ($size) {
$temp = "";
for ($a=0;$a< $size;$a++)
@ -612,8 +458,6 @@ function install_step2() {
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>";
if ($res > 0) {
@ -659,12 +503,6 @@ function install_step3() {
if (extension_loaded("mysqli")) {
$options .= "<option value='mysqli'>MySQL(mysqli)</option>";
}
if (extension_loaded("pgsql")) {
$options .= "<option value='pgsql'>PostgreSQL</option>";
}
if (extension_loaded("oci8")) {
$options .= "<option value='oracle'>Oracle</option>";
}
$error = false;
if (empty($options)) {
@ -1034,289 +872,6 @@ function install_step4() {
$everything_ok = 1;
}
break;
case 'oracle':
$connection = oci_connect($dbuser, $dbpassword, '//' . $dbhost . '/' . $dbname);
if (!$connection) {
check_generic(0, "Connection with Database");
}
else {
check_generic(1, "Connection with Database");
// Drop all objects if needed
if ($dbdrop == 1) {
oracle_drop_all_objects($connection);
}
$step1 = parse_oracle_dump($connection, "pandoradb.oracle.sql");
check_generic($step1, "Creating schema");
if ($step1) {
$step2 = parse_oracle_dump($connection, "pandoradb.data.oracle.sql");
}
check_generic ($step2, "Populating database");
if (PHP_OS == "FreeBSD")
{
$step_freebsd = adjust_paths_for_freebsd ($engine, $connection);
check_generic ($step_freebsd, "Adjusting paths in database for FreeBSD");
}
echo "<tr><td><div class='warn'>Please, you will need to setup your Pandora FMS server, editing the </i>/etc/pandora/pandora_server.conf</i> file and set database password.</div></tr></td>";
if ($step2) {
$step3 = is_writable("include");
}
check_generic ($step3, "Write permissions to save config file in './include'");
if ($step3) {
$cfgin = fopen ("include/config.inc.php","r");
$cfgout = fopen ($pandora_config,"w");
$config_contents = fread ($cfgin, filesize("include/config.inc.php"));
$dbtype = 'oracle';
$config_new = '<?php
// Begin of automatic config file
$config["dbtype"] = "' . $dbtype . '"; //DB type (mysql, postgresql, oracle)
$config["dbname"]="' . $dbname . '"; // Oracle DataBase name
$config["dbuser"]="' . $dbuser . '"; // DB User
$config["dbpass"]="' . $dbpassword . '"; // 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 cases.
----------Attention--------------------
*/
$config["homeurl"]="' . $url . '"; // Base URL
$config["homeurl_static"]="'.$url.'"; // Don\'t delete
// End of automatic config file
?>';
$step4 = fputs ($cfgout, $config_new);
$step4 = $step4 + fputs ($cfgout, $config_contents);
if ($step4 > 0)
$step4 = 1;
fclose ($cfgin);
fclose ($cfgout);
chmod ($pandora_config, 0600);
}
check_generic ($step4, "Created new config file at '" . $pandora_config . "'");
if (($step4 + $step3 + $step2 + $step1) == 4) {
$everything_ok = 1;
}
}
break;
case 'pgsql':
$step1 = $step2 = $step3 = $step4 = $step5 = $step6 = $step7 = 0;
$connection = pg_connect("host='" . $dbhost . "' dbname='postgres' 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 && $dbaction == 'db_exist') {
$result = pg_query($connection, "DROP DATABASE \"" . $dbname . "\";");
}
if ($dbaction != 'db_exist' || $dbdrop == 1) {
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'");
}
else {
$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($connection, "pandoradb.postgreSQL.sql");
}
check_generic($step3, "Creating schema");
if ($step3) {
$step4 = parse_postgresql_dump($connection, "pandoradb.data.postgreSQL.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");
}
if ($step4) {
$random_password = random_name (8);
pg_query($connection, "DROP USER pandora");
pg_send_query($connection, "CREATE USER pandora WITH PASSWORD '" . $random_password . "'");
$result = pg_get_result($connection);
if (pg_result_status($result) != PGSQL_FATAL_ERROR) {
//Set the privileges for DB
pg_send_query($connection, "GRANT ALL PRIVILEGES ON DATABASE pandora TO pandora;");
$result = pg_get_result($connection);
$setDBPrivileges = 0;
if (pg_result_status($result) != PGSQL_FATAL_ERROR) {
$setDBPrivileges = 1;
}
if ($setDBPrivileges) {
//Set the privileges for each tables.
pg_send_query($connection, "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';");
$result = pg_get_result($connection);
$tables = array();
while ($row = pg_fetch_assoc($result)) {
$tables[] = $row['table_name'];
}
$correct = 1;
foreach ($tables as $table) {
pg_send_query($connection, "GRANT ALL PRIVILEGES ON TABLE " . $table . " TO pandora;");
$result = pg_get_result($connection);
if (pg_result_status($result) == PGSQL_FATAL_ERROR) {
$correct = 0;
break;
}
//For each table make owner pandora
pg_send_query($connection, "ALTER TABLE " . $table . " OWNER TO pandora;");
$result = pg_get_result($connection);
if (pg_result_status($result) == PGSQL_FATAL_ERROR) {
$correct = 0;
break;
}
//INI ----- Grant for secuences
pg_send_query($connection, "SELECT column_name FROM information_schema.columns WHERE table_name = '" . $table . "';");
$result2 = pg_get_result($connection);
$columns = array();
while ($row = pg_fetch_assoc($result2)) {
$columns[] = $row['column_name'];
}
//Check for each column if it have a sequence to grant
foreach ($columns as $column) {
pg_send_query($connection, "SELECT pg_get_serial_sequence('" . $table . "', '" . $column . "');");
$result3 = pg_get_result($connection);
$sequence = pg_fetch_assoc($result3);
if (!empty($sequence['pg_get_serial_sequence'])) {
pg_send_query($connection, "GRANT ALL PRIVILEGES ON SEQUENCE " . $sequence['pg_get_serial_sequence'] . " to pandora;");
$result4 = pg_get_result($connection);
if (pg_result_status($result4) == PGSQL_FATAL_ERROR) {
$correct = 0;
break;
}
}
}
//END ----- Grant for secuences
}
if ($correct) {
$step5 = 1;
}
}
}
}
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>");
if ($step5) {
$step6 = is_writable("include");
}
check_generic ($step6, "Write permissions to save config file in './include'");
if ($step6) {
$cfgin = fopen ("include/config.inc.php","r");
$cfgout = fopen ($pandora_config,"w");
$config_contents = fread ($cfgin, filesize("include/config.inc.php"));
$dbtype = 'postgresql';
$config_new = '<?php
// Begin of automatic config file
$config["dbtype"] = "' . $dbtype . '"; //DB type (mysql, postgresql...in future others)
$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 cases.
----------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;
}
}
break;
}
echo "</table>";
@ -1358,11 +913,6 @@ function install_step4() {
mysqli_query ($connection, "DROP DATABASE $dbname");
}
break;
case 'pgsql':
break;
case 'oracle':
oracle_drop_all_objects($connection);
break;
}
echo "</div>";
}