fixed errors php7

This commit is contained in:
daniel 2018-10-04 17:17:39 +02:00
parent e2f7157545
commit 0ce7d3a2ed
2 changed files with 288 additions and 254 deletions

View File

@ -14,52 +14,35 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
/*
function extension_db_status_extension_tables() {
return array(
'tbackup',
'tfiles_repo',
'tfiles_repo_group',
'tipam_ip',
'tipam_network',
'tuser_task',
'tuser_task_scheduled',
);
}
*/
function extension_db_status() {
global $config;
$db_user = get_parameter('db_user', '');
$db_password = get_parameter('db_password', '');
$db_host = get_parameter('db_host', '');
$db_name = get_parameter('db_name', '');
$db_status_execute = (bool)get_parameter('db_status_execute', false);
ui_print_page_header (__("DB Schema check"),
"images/extensions.png", false, "", true, "");
if (!is_user_admin($config['id_user'])) {
db_pandora_audit("ACL Violation",
"Trying to access db status");
require ("general/noaccess.php");
return;
}
ui_print_info_message(
__('This extension checks the DB is correct. Because sometimes the old DB from a migration has not some fields in the tables or the data is changed.'));
ui_print_info_message(
__('At the moment the checks is for MySQL/MariaDB.'));
echo "<form method='post'>";
echo "<fieldset>";
echo "<legend>" . __('DB settings') . "</legend>";
$table = null;
$table = new stdClass();
$table->data = array();
$row = array();
$row[] = __("DB User with privileges");
@ -75,175 +58,263 @@ function extension_db_status() {
$table->data[] = $row;
html_print_table($table);
echo "</fieldset>";
echo "<div style='text-align: right;'>";
html_print_input_hidden('db_status_execute', 1);
html_print_submit_button(__('Execute Test'), 'submit', false, 'class="sub"');
echo "</div>";
echo "</form>";
if ($db_status_execute) {
extension_db_status_execute_checks($db_user, $db_password,
$db_host, $db_name);
}
}
function extension_db_status_execute_checks($db_user, $db_password, $db_host, $db_name) {
global $config;
$connection_system = $config['dbconnection'];
// Avoid SQL injection
$db_name = io_safe_output($db_name);
$db_name = str_replace(';', ' ', $db_name);
$db_name = explode(" ", $db_name);
$db_name = $db_name[0];
$connection_test = mysql_connect ($db_host, $db_user, $db_password);
if ($config["mysqli"] === true) {
$connection_test = mysqli_connect($db_host, $db_user, $db_password);
}
else{
$connection_test = mysql_connect($db_host, $db_user, $db_password);
}
if (!$connection_test) {
ui_print_error_message(
__('Unsuccessful connected to the DB'));
}
else {
$create_db = mysql_query ("CREATE DATABASE `$db_name`");
if($config["mysqli"] === true){
$create_db = mysqli_query($connection_test, "CREATE DATABASE `$db_name`");
}else{
$create_db = mysql_query("CREATE DATABASE `$db_name`");
}
if (!$create_db) {
ui_print_error_message(
__('Unsuccessful created the testing DB'));
}
else {
mysql_select_db($db_name, $connection_test);
if ($config["mysqli"] === true) {
mysqli_select_db($connection_test, $db_name);
}
else{
mysql_select_db($db_name, $connection_test);
}
$install_tables = extension_db_status_execute_sql_file(
$config['homedir'] . "/pandoradb.sql",
$connection_test);
if (!$install_tables) {
ui_print_error_message(
__('Unsuccessful installed tables into the testing DB'));
}
else {/*
if (enterprise_installed()) {
$install_tables_enterprise =
extension_db_status_execute_sql_file(
$config['homedir'] . "/enterprise/pandoradb.sql",
$connection_test);
if (!$install_tables_enterprise) {
ui_print_error_message(
__('Unsuccessful installed enterprise tables into the testing DB'));
}
}
*/
else {
extension_db_check_tables_differences(
$connection_test,
$connection_system,
$db_name,
$config['dbname']);
//extension_db_check_data_differences();
}
mysql_select_db($db_name, $connection_test);
mysql_query ("DROP DATABASE IF EXISTS `$db_name`");
if ($config["mysqli"] === true) {
mysqli_select_db($connection_test, $db_name);
mysqli_query($connection_test, "DROP DATABASE IF EXISTS `$db_name`");
}
else{
mysql_select_db($db_name, $connection_test);
mysql_query("DROP DATABASE IF EXISTS `$db_name`", $connection_test);
}
}
}
}
function extension_db_check_tables_differences($connection_test,
$connection_system, $db_name_test, $db_name_system) {
global $config;
// --------- Check the tables --------------------------------------
mysql_select_db($db_name_test, $connection_test);
$result = mysql_query("SHOW TABLES", $connection_test);
if ($config["mysqli"] === true) {
mysqli_select_db($connection_test, $db_name_test);
$result = mysqli_query($connection_test, "SHOW TABLES");
}else{
mysql_select_db($db_name_test, $connection_test);
$result = mysql_query("SHOW TABLES", $connection_test);
}
$tables_test = array();
while ($row = mysql_fetch_array ($result)) {
$tables_test[] = $row[0];
if ($config["mysqli"] === true) {
while ($row = mysqli_fetch_array($result)) {
$tables_test[] = $row[0];
}
mysqli_free_result($result);
mysqli_select_db($connection_test, $db_name_system);
$result = mysqli_query( $connection_test, "SHOW TABLES");
}
mysql_free_result ($result);
//~ $tables_test = array_merge($tables_test,
//~ extension_db_status_extension_tables());
mysql_select_db($db_name_system, $connection_test);
$result = mysql_query("SHOW TABLES", $connection_test);
else{
while ($row = mysql_fetch_array($result)) {
$tables_test[] = $row[0];
}
mysql_free_result($result);
mysql_select_db($db_name_system, $connection_test);
$result = mysql_query("SHOW TABLES", $connection_test);
}
$tables_system = array();
while ($row = mysql_fetch_array ($result)) {
$tables_system[] = $row[0];
if ($config["mysqli"] === true) {
while ($row = mysqli_fetch_array ($result)) {
$tables_system[] = $row[0];
}
mysqli_free_result($result);
}
mysql_free_result ($result);
else{
while ($row = mysql_fetch_array ($result)) {
$tables_system[] = $row[0];
}
mysql_free_result($result);
}
$diff_tables = array_diff($tables_test, $tables_system);
ui_print_result_message(
!empty($diff_tables),
__('Success! %s DB contains all tables', get_product_name()),
__('%s DB could not retrieve all tables. The missing tables are (%s)',
get_product_name(), implode(", ", $diff_tables)));
if (!empty($diff_tables)) {
foreach ($diff_tables as $table) {
mysql_select_db($db_name_test, $connection_test);
$result = mysql_query("SHOW CREATE TABLE " . $table, $connection_test);
$tables_test = array();
while ($row = mysql_fetch_array ($result)) {
ui_print_info_message(
__('You can execute this SQL query for to fix.') . "<br />" .
'<pre>' .
$row[1] .
'</pre>'
);
if ($config["mysqli"] === true) {
mysqli_select_db($connection_test, $db_name_test);
$result = mysqli_query($connection_test, "SHOW CREATE TABLE " . $table);
$tables_test = array();
while ($row = mysql_fetch_array($result)) {
ui_print_info_message(
__('You can execute this SQL query for to fix.') . "<br />" .
'<pre>' .
$row[1] .
'</pre>'
);
}
mysqli_free_result($result);
}
else{
mysql_select_db($db_name_test, $connection_test);
$result = mysql_query("SHOW CREATE TABLE " . $table, $connection_test);
$tables_test = array();
while ($row = mysql_fetch_array($result)) {
ui_print_info_message(
__('You can execute this SQL query for to fix.') . "<br />" .
'<pre>' .
$row[1] .
'</pre>'
);
}
mysql_free_result($result);
}
mysql_free_result ($result);
}
}
// --------------- Check the fields -------------------------------
$correct_fields = true;
foreach ($tables_system as $table) {
mysql_select_db($db_name_test, $connection_test);
$result = mysql_query("EXPLAIN " . $table, $connection_test);
if ($config["mysqli"] === true) {
mysqli_select_db($connection_test, $db_name_test);
$result = mysqli_query($connection_test, "EXPLAIN " . $table);
}
else{
mysql_select_db($db_name_test, $connection_test);
$result = mysql_query("EXPLAIN " . $table, $connection_test);
}
$fields_test = array();
if (!empty($result)) {
while ($row = mysql_fetch_array ($result)) {
$fields_test[$row[0]] = array(
'field ' => $row[0],
'type' => $row[1],
'null' => $row[2],
'key' => $row[3],
'default' => $row[4],
'extra' => $row[5]);
if ($config["mysqli"] === true) {
while ($row = mysqli_fetch_array ($result)) {
$fields_test[$row[0]] = array(
'field ' => $row[0],
'type' => $row[1],
'null' => $row[2],
'key' => $row[3],
'default' => $row[4],
'extra' => $row[5]);
}
mysqli_free_result ($result);
mysqli_select_db($connection_test, $db_name_system);
}
else{
while ($row = mysql_fetch_array ($result)) {
$fields_test[$row[0]] = array(
'field ' => $row[0],
'type' => $row[1],
'null' => $row[2],
'key' => $row[3],
'default' => $row[4],
'extra' => $row[5]);
}
mysql_free_result ($result);
mysql_select_db($db_name_system, $connection_test);
}
mysql_free_result ($result);
}
mysql_select_db($db_name_system, $connection_test);
$result = mysql_query("EXPLAIN " . $table, $connection_test);
if($config["mysqli"] === true){
$result = mysqli_query($connection_test, "EXPLAIN " . $table);
}
else{
$result = mysql_query("EXPLAIN " . $table, $connection_test);
}
$fields_system = array();
if (!empty($result)) {
while ($row = mysql_fetch_array ($result)) {
$fields_system[$row[0]] = array(
'field ' => $row[0],
'type' => $row[1],
'null' => $row[2],
'key' => $row[3],
'default' => $row[4],
'extra' => $row[5]);
if ($config["mysqli"] === true) {
while ($row = mysqli_fetch_array ($result)) {
$fields_system[$row[0]] = array(
'field ' => $row[0],
'type' => $row[1],
'null' => $row[2],
'key' => $row[3],
'default' => $row[4],
'extra' => $row[5]);
}
mysqli_free_result($result);
}
else{
while ($row = mysql_fetch_array($result)) {
$fields_system[$row[0]] = array(
'field ' => $row[0],
'type' => $row[1],
'null' => $row[2],
'key' => $row[3],
'default' => $row[4],
'extra' => $row[5]);
}
mysql_free_result($result);
}
mysql_free_result ($result);
}
foreach ($fields_test as $name_field => $field_test) {
if (!isset($fields_system[$name_field])) {
$correct_fields = false;
ui_print_error_message(
__('Unsuccessful the table %s has not the field %s',
$table, $name_field));
@ -257,63 +328,59 @@ function extension_db_check_tables_differences($connection_test,
else {
$correct_fields = false;
$field_system = $fields_system[$name_field];
$diff = array_diff($field_test, $field_system);
if (!empty($diff)) {
$info_message = "";
$error_message = "";
if($diff['type']){
$error_message .= "Unsuccessful the field ".$name_field." in the table ".$table." must be set the type with ".$diff['type']."<br>";
}
if($diff['null']){
$error_message .= "Unsuccessful the field $name_field in the table $table must be null: (".$diff['null'].").<br>";
}
if($diff['default']){
$error_message .= "Unsuccessful the field $name_field in the table $table must be set ".$diff['default']." as default value.<br>";
$error_message .= "Unsuccessful the field $name_field in the table $table must be set ".$diff['default']." as default value.<br>";
}
if($field_test['null'] == "YES" || !isset($field_test['null']) || $field_test['null'] == ""){
$null_defect = " NULL";
}
else{
$null_defect = " NOT NULL";
}
if(!isset($field_test['default']) || $field_test['default'] == ""){
$default_value = "";
}
else{
$default_value = " DEFAULT ".$field_test['default'];
}
if($diff['type'] || $diff['null'] || $diff['default']){
$info_message .= "ALTER TABLE " . $table . " MODIFY COLUMN " . $name_field . " " . $field_test['type'] . $null_defect . $default_value.";";
}
if($diff['key']){
$error_message .= "Unsuccessful the field $name_field in the table $table must be set the key as defined in the SQL file.<br>";
$info_message .= "<br><br>Please check the SQL file for to know the kind of key needed.";
}
if($diff['extra']){
if($diff['extra']){
$error_message .= "Unsuccessful the field $name_field in the table $table must be set as defined in the SQL file.<br>";
$info_message .= "<br><br>Please check the SQL file for to know the kind of extra config needed.";
}
ui_print_error_message(
__($error_message));
ui_print_info_message(
__($info_message));
ui_print_error_message(__($error_message));
ui_print_info_message(__($info_message));
}
}
}
}
if ($correct_fields) {
ui_print_success_message(
__('Successful all the tables have the correct fields')
@ -322,6 +389,7 @@ function extension_db_check_tables_differences($connection_test,
}
function extension_db_status_execute_sql_file($url, $connection) {
global $config;
if (file_exists($url)) {
$file_content = file($url);
$query = "";
@ -329,10 +397,19 @@ function extension_db_status_execute_sql_file($url, $connection) {
if (trim($sql_line) != "" && strpos($sql_line, "--") === false) {
$query .= $sql_line;
if (preg_match("/;[\040]*\$/", $sql_line)) {
if (!$result = mysql_query($query, $connection)) {
echo mysql_error(); //Uncomment for debug
echo "<i><br>$query<br></i>";
return 0;
if ($config["mysqli"] === true) {
if (!$result = mysqli_query($connection, $query)) {
echo mysqli_error(); //Uncomment for debug
echo "<i><br>$query<br></i>";
return 0;
}
}
else{
if (!$result = mysql_query($query, $connection)) {
echo mysql_error(); //Uncomment for debug
echo "<i><br>$query<br></i>";
return 0;
}
}
$query = "";
}

View File

@ -16,123 +16,80 @@
function dbmanager_query ($sql, &$error, $dbconnection) {
global $config;
switch ($config["dbtype"]) {
case "mysql":
$retval = array();
if ($sql == '')
return false;
$sql = html_entity_decode($sql, ENT_QUOTES);
if ($config["mysqli"]) {
$result = mysqli_query ($dbconnection, $sql);
if ($result === false) {
$backtrace = debug_backtrace ();
$error = mysqli_error ($dbconnection);
return false;
}
}
else{
$result = mysql_query ($sql, $dbconnection);
if ($result === false) {
$backtrace = debug_backtrace ();
$error = mysql_error ();
return false;
}
}
if ($result === true) {
if($config["mysqli"]){
return mysqli_affected_rows ($dbconnection);
}
else{
return mysql_affected_rows ();
}
}
if($config["mysqli"]){
while ($row = mysqli_fetch_array ($result, MYSQL_ASSOC)) {
array_push ($retval, $row);
}
}
else{
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
array_push ($retval, $row);
}
}
if($config["mysqli"]){
mysqli_free_result ($result);
}
else{
mysql_free_result ($result);
}
if (! empty ($retval))
return $retval;
//Return false, check with === or !==
return "Empty";
break;
case "postgresql":
case "oracle":
$retval = array();
if ($sql == '')
return false;
$sql = html_entity_decode($sql, ENT_QUOTES);
$result = db_process_sql($sql, "affected_rows", '', false, $status);
//$result = mysql_query ($sql);
if ($result === false) {
$backtrace = debug_backtrace();
$error = db_get_last_error();
if (empty($error)) {
return "Empty";
}
return false;
}
if ($status == 2) {
return $result;
}
else {
return $result;
}
break;
$retval = array();
if ($sql == '')
return false;
$sql = html_entity_decode($sql, ENT_QUOTES);
if ($config["mysqli"]) {
$result = mysqli_query ($dbconnection, $sql);
if ($result === false) {
$backtrace = debug_backtrace ();
$error = mysqli_error ($dbconnection);
return false;
}
}
else{
$result = mysql_query ($sql, $dbconnection);
if ($result === false) {
$backtrace = debug_backtrace ();
$error = mysql_error ();
return false;
}
}
if ($result === true) {
if($config["mysqli"]){
return mysqli_affected_rows ($dbconnection);
}
else{
return mysql_affected_rows ();
}
}
if($config["mysqli"]){
while ($row = mysqli_fetch_array ($result)) {
array_push ($retval, $row);
}
}
else{
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
array_push ($retval, $row);
}
}
if($config["mysqli"]){
mysqli_free_result ($result);
}
else{
mysql_free_result ($result);
}
if (! empty ($retval))
return $retval;
//Return false, check with === or !==
return "Empty";
}
function dbmgr_extension_main () {
ui_require_css_file ('dbmanager', 'extensions/dbmanager/');
global $config;
if (!is_user_admin($config['id_user'])) {
db_pandora_audit("ACL Violation", "Trying to access Setup Management");
require ("general/noaccess.php");
return;
}
/*
* Disabled at the moment.
if (!check_referer()) {
require ("general/noaccess.php");
return;
}
*/
$sql = (string) get_parameter ('sql');
ui_print_page_header (__('Database interface'), "images/gm_db.png", false, false, true);
echo '<div class="notify">';
echo __("This is an advanced extension to interface with %s database directly from WEB console
using native SQL sentences. Please note that <b>you can damage</b> your %s installation
@ -142,10 +99,10 @@ function dbmgr_extension_main () {
with a depth knowledge of %s internals.",
get_product_name(), get_product_name(), get_product_name());
echo '</div>';
echo "<br />";
echo "Some samples of usage: <blockquote><em>SHOW STATUS;<br />DESCRIBE tagente<br />SELECT * FROM tserver<br />UPDATE tagente SET id_grupo = 15 WHERE nombre LIKE '%194.179%'</em></blockquote>";
echo "<br /><br />";
echo "<form method='post' action=''>";
html_print_textarea ('sql', 5, 50, html_entity_decode($sql, ENT_QUOTES));
@ -155,45 +112,45 @@ function dbmgr_extension_main () {
html_print_submit_button (__('Execute SQL'), '', false, 'class="sub next"');
echo '</div>';
echo "</form>";
// Processing SQL Code
if ($sql == '')
return;
echo "<br />";
echo "<hr />";
echo "<br />";
$dbconnection = $config['dbconnection'];
$error = '';
$result = dbmanager_query ($sql, $error, $dbconnection);
if ($result === false) {
echo '<strong>An error has occured when querying the database.</strong><br />';
echo $error;
db_pandora_audit("DB Interface Extension", "Error in SQL", false, false, $sql);
return;
}
if (! is_array ($result)) {
echo "<strong>Output: <strong>".$result;
db_pandora_audit("DB Interface Extension", "SQL", false, false, $sql);
return;
}
echo "<div style='overflow: auto;'>";
$table = new stdClass();
$table->width = '100%';
$table->class = 'databox data';
$table->head = array_keys ($result[0]);
$table->data = $result;
html_print_table ($table);
echo "</div>";
}