diff --git a/includes/config.php b/includes/config.php index 90894028a..1d4d07c65 100644 --- a/includes/config.php +++ b/includes/config.php @@ -191,9 +191,17 @@ function do_config($callingFromInstall=false) } } } + } else if (!file_exists(__DIR__ . '/settings.php') && !$callingFromInstall) { + // Redirect to installer + if (file_exists(__DIR__ . '/../install/index.php')) { + header('Location: install/index.php'); + exit; + } else { + die_miserable_death(translate('Could not find settings.php file...')); + } } else { // Load from settings.php file - $settings_content = file_get_contents(__DIR__ . '/settings.php'); + $settings_content = @file_get_contents(__DIR__ . '/settings.php'); if (empty($settings_content)) { if ($callingFromInstall) { return; // not an error during install @@ -228,11 +236,10 @@ function do_config($callingFromInstall=false) } // Extract db settings into global vars. - $db_database = $settings['db_database']; - $db_host = $settings['db_host']; - $db_login = $settings['db_login']; - $db_password = (empty($settings['db_password']) - ? '' : $settings['db_password']); + $db_database = $settings['db_database'] ?? ''; + $db_host = $settings['db_host'] ?? ''; + $db_login = $settings['db_login'] ?? ''; + $db_password = $settings['db_password'] ?? ''; $db_persistent = (preg_match( '/(1|yes|true|on)/i', $settings['db_persistent'] @@ -241,7 +248,7 @@ function do_config($callingFromInstall=false) '/(1|yes|true|on)/i', $settings['db_debug'] ) ? true : false); - $db_type = $settings['db_type']; + $db_type = $settings['db_type'] ?? ''; // If no db settings, then user has likely started install but not yet // completed. So, send them back to the install script. diff --git a/includes/dbi4php.php b/includes/dbi4php.php index 7e4412115..354a83c93 100644 --- a/includes/dbi4php.php +++ b/includes/dbi4php.php @@ -571,89 +571,103 @@ function dbi_get_blob( $table, $column, $key ) { * * @return bool True on success */ -function dbi_free_result( $res ) { - if( $res === true ) // Not needed for UPDATE, DELETE, etc - return; +function dbi_free_result($res) +{ + if ($res === true) { // Not needed for UPDATE, DELETE, etc. + return true; + } - if( strcmp( $GLOBALS['db_type'], 'ibase' ) == 0 ) - return ibase_free_result( $res ); - elseif( strcmp( $GLOBALS['db_type'], 'ibm_db2' ) == 0 ) - return db2_free_result( $res ); - elseif( strcmp( $GLOBALS['db_type'], 'mysqli' ) == 0 ) - return mysqli_free_result( $res ); - elseif( strcmp( $GLOBALS['db_type'], 'odbc' ) == 0 ) - return odbc_free_result( $res ); - elseif( strcmp( $GLOBALS['db_type'], 'oracle' ) == 0 ) { - // Not supported. Ingore. - if( $GLOBALS['oracle_statement'] >= 0 ) { - OCIFreeStatement( $GLOBALS['oracle_statement'] ); - $GLOBALS['oracle_statement'] = -1; - } - } elseif( strcmp( $GLOBALS['db_type'], 'postgresql' ) == 0 ) - return pg_freeresult( $res ); - elseif( strcmp( $GLOBALS['db_type'], 'sqlite' ) == 0 ) { - // Not supported + $dbType = $GLOBALS['db_type'] ?? 'undefined'; + switch ($dbType) { + case 'ibase': + return ibase_free_result($res); + case 'ibm_db2': + return db2_free_result($res); + case 'mysqli': + return mysqli_free_result($res); + case 'odbc': + return odbc_free_result($res); + case 'oracle': + if ($GLOBALS['oracle_statement'] >= 0) { + OCIFreeStatement($GLOBALS['oracle_statement']); + $GLOBALS['oracle_statement'] = -1; + } + return true; // Assuming a successful operation as it's not directly supported. + case 'postgresql': + return pg_freeresult($res); + case 'sqlite': + // Not supported for SQLite, just return true. + return true; + case 'sqlite3': + // Not needed for SQLite3, just return true. + return true; + case 'undefined': + dbi_fatal_error('dbi_free_result(): ' . translate('db_type not defined.')); + break; + default: + dbi_fatal_error('dbi_free_result(): ' . translate('Unsupported db_type.') . ' (' . htmlentities($dbType) . ')'); + break; } - elseif( strcmp( $GLOBALS['db_type'], 'sqlite3' ) == 0 ) { - // Not needed - } else - dbi_fatal_error( 'dbi_free_result(): ' - . translate( 'db_type not defined.' ) ); } -/** - * Gets the latest database error message. - * - * @return string The text of the last database error. (The type of information - * varies depending on which type of database is being used.) - */ -function dbi_error() { - if( strcmp( $GLOBALS['db_type'], 'ibase' ) == 0 ) - $ret = ibase_errmsg(); - elseif( strcmp( $GLOBALS['db_type'], 'ibm_db2' ) == 0 ) { - $ret = db2_conn_errormsg(); - if( $ret == '' ) - $ret = db2_stmt_errormsg(); - } elseif (strcmp($GLOBALS['db_type'], 'mysqli') == 0) { - if (!empty($GLOBALS['db_connection_info']['last_error'])) { - $ret = $GLOBALS['db_connection_info']['last_error']; - } else { - $ret = $GLOBALS['db_connection']->error; - } - } elseif( strcmp( $GLOBALS['db_type'], 'odbc' ) == 0 ) - // No way to get error from ODBC API. - $ret = translate( 'Unknown ODBC error.' ); - elseif( strcmp( $GLOBALS['db_type'], 'oracle' ) == 0 ) { - $e = OCIError( $GLOBALS['oracle_connection'] - ? $GLOBALS['oracle_connection'] : '' ); - $ret = htmlentities( $e['message'] ); - } elseif( strcmp( $GLOBALS['db_type'], 'postgresql' ) == 0 ) - $ret = pg_errormessage( $GLOBALS['postgresql_connection'] ); - elseif( strcmp( $GLOBALS['db_type'], 'sqlite' ) == 0 ) { - if( empty( $GLOBALS['db_sqlite_error_str'] ) ) { - $ret = sqlite_last_error( $GLOBALS['sqlite_c'] ); - } else { - $ret = $GLOBALS['db_sqlite_error_str']; - $GLOBALS['db_sqlite_error_str'] = ''; - } - } elseif ( strcmp ( $GLOBALS['db_type'], 'sqlite3' ) == 0 ) { - try { - if ( empty($$GLOBALS['sqlite3_c']) || !empty($GLOBALS['db_sqlite_error_str'])) { - $ret = $GLOBALS['db_sqlite_error_str']; +function dbi_error() +{ + $dbType = $GLOBALS['db_type'] ?? 'undefined'; + + switch ($dbType) { + case 'ibase': + return ibase_errmsg(); + + case 'ibm_db2': + $ret = db2_conn_errormsg(); + return ($ret == '') ? db2_stmt_errormsg() : $ret; + + case 'mysqli': + if (!empty($GLOBALS['db_connection_info']['last_error'])) { + return $GLOBALS['db_connection_info']['last_error']; } else { - $ret = $GLOBALS['sqlite3_c']->lastErrorMsg (); + return $GLOBALS['db_connection']->error; } - } catch ( Exception $e) { - $GLOBALS['db_sqlite_error_str'] = $e->getMessage(); - $ret = $e->getMessage(); - } - } else - $ret = 'dbi_error(): ' . translate( 'db_type not defined.' ); - return ( strlen( $ret ) ? $ret : translate( 'Unknown error.' ) ); + case 'odbc': + return translate('Unknown ODBC error.'); + + case 'oracle': + $e = OCIError($GLOBALS['oracle_connection'] ? $GLOBALS['oracle_connection'] : ''); + return htmlentities($e['message']); + + case 'postgresql': + return pg_errormessage($GLOBALS['postgresql_connection']); + + case 'sqlite': + if (empty($GLOBALS['db_sqlite_error_str'])) { + return sqlite_last_error($GLOBALS['sqlite_c']); + } else { + return $GLOBALS['db_sqlite_error_str']; + } + + case 'sqlite3': + try { + if (empty($GLOBALS['sqlite3_c']) || !empty($GLOBALS['db_sqlite_error_str'])) { + return $GLOBALS['db_sqlite_error_str']; + } else { + return $GLOBALS['sqlite3_c']->lastErrorMsg(); + } + } catch (Exception $e) { + $GLOBALS['db_sqlite_error_str'] = $e->getMessage(); + return $e->getMessage(); + } + + case 'undefined': + return 'dbi_error(): ' . translate('db_type not defined.'); + + default: + return 'dbi_error(): ' . translate('Unsupported db_type.') . ' (' . htmlentities($dbType) . ')'; + } } + /** * Displays a fatal database error and aborts execution. * diff --git a/install/index.php b/install/index.php index b9edf2584..5d644dbb8 100644 --- a/install/index.php +++ b/install/index.php @@ -230,6 +230,7 @@ function_exists('gd_info'), $databaseExists = false; $databaseCurrent = false; $settingsSaved = true; // True if a valid settings.php found unless user changes settings +$detectedDbVersion = 'Unknown'; if ($canConnectDb) { $reportedDbVersion = getDbVersion(); $detectedDbVersion = getDatabaseVersionFromSchema(); @@ -381,7 +382,11 @@ function_exists('gd_info'), =')) ? "Supported" : "Not supported"; ?> diff --git a/install/install_appsettings.php b/install/install_appsettings.php index 780467787..1e40da88e 100644 --- a/install/install_appsettings.php +++ b/install/install_appsettings.php @@ -202,6 +202,6 @@ function handlePulldownUpdate() { printNextPageButton($action); } } else { - printSubmitButton($action, $html, $buttonLabel); + printSubmitButton($action, $html ?? null, $buttonLabel ?? null); } ?> \ No newline at end of file diff --git a/install/install_appsettings_handler.php b/install/install_appsettings_handler.php index fea01fc7c..e7200e3b5 100644 --- a/install/install_appsettings_handler.php +++ b/install/install_appsettings_handler.php @@ -1,38 +1,44 @@ "; print_r($_SESSION); echo ""; exit; - // Did the user change anything - $foundChange = false; - foreach ($app_settings as $setting) { - if ($_SESSION[$setting] != $settings[$setting] ){ - $foundChange = true; - } - } - if ($foundChange) { - // Require user to save and overwrite settings.php in a future step. - $_SESSION['appSettingsModified'] = 1; + $_SESSION['mode'] = $_POST['mode']; + $_SESSION['single_user_login'] = $_POST['single_user_login']; + // echo "
"; print_r($_SESSION); echo ""; exit; + // Did the user change anything + $foundChange = false; + foreach ($app_settings as $setting) { + if (empty($settings[$setting]) || $_SESSION[$setting] != $settings[$setting]) { + $foundChange = true; } - redirectToNextAction(); - } else { - $error = translate('Invalid Application Settings'); } + if ($foundChange) { + // Require user to save and overwrite settings.php in a future step. + $_SESSION['appSettingsModified'] = 1; + } + $appSettingsCorrect = isset($_SESSION['readonly']) && isset($_SESSION['user_inc']) && + isset($_SESSION['use_http_auth']) && isset($_SESSION['single_user']) + && isset($_SESSION['mode']); +} +if ($appSettingsCorrect) { + redirectToNextAction(); +} else { + $error = translate('Invalid Application Settings'); } diff --git a/install/install_auth_handler.php b/install/install_auth_handler.php index 642a09912..cadc6cd6c 100644 --- a/install/install_auth_handler.php +++ b/install/install_auth_handler.php @@ -1,35 +1,45 @@ format('D, d M Y H:i:s O'); - $content = preg_replace('/updated via install\/index.php on .*/', 'updated via install/index.php on ' . $formattedDate, $content); + // Update the date + $date = new DateTime(); + $formattedDate = $date->format('D, d M Y H:i:s O'); + $content = preg_replace('/updated via install\/index.php on .*/', 'updated via install/index.php on ' . $formattedDate, $content); - // Write the updated content back to the file - return file_put_contents($file, $content); + // Write the updated content back to the file + return file_put_contents($file, $content); +} + +function write_password_in_new_settings($file, $password, $hint) +{ + $date = new DateTime(); + $formattedDate = $date->format('D, d M Y H:i:s O'); + $content = "\n"; + return file_put_contents($file, $content); } // Handle form submission on Auth page (both setting and checking password) @@ -43,9 +53,14 @@ function update_password_in_settings($file, $password, $hint) { $error = translate('Your passwords must match.'); } $hint = $_POST['hint']; - $ret = update_password_in_settings(__DIR__ . '/../includes/settings.php', md5($password), $hint); - if (! $ret) { - $error = 'Error writing includes/settings.php file.'; + $settingsFile = __DIR__ . '/../includes/settings.php'; + if (file_exists($settingsFile) && strlen(file_get_contents($settingsFile) > 10)) { + $ret = update_password_in_settings($settingsFile, md5($password), $hint); + } else { + $ret = write_password_in_new_settings($settingsFile, md5($password), $hint); + } + if (!$ret) { + $error = 'Error writing ' . $settingsFile . ' file.'; } else { redirectToNextAction(); } @@ -62,4 +77,3 @@ function update_password_in_settings($file, $password, $hint) { $error = translate("Invalid passphrase."); } } -?> diff --git a/install/install_dbload_handler.php b/install/install_dbload_handler.php index 9adfe11eb..7c5aa3bb9 100644 --- a/install/install_dbload_handler.php +++ b/install/install_dbload_handler.php @@ -110,8 +110,6 @@ function createDB2Database($hostname, $login, $password, $databaseName): bool return true; } - - try { switch ($_SESSION['db_type']) { case 'mysqli': diff --git a/install/install_dbsettings.php b/install/install_dbsettings.php index e0ca704f5..5523d5c2e 100644 --- a/install/install_dbsettings.php +++ b/install/install_dbsettings.php @@ -43,7 +43,7 @@ function printDbSetting($name) list($checkType, $checkValue) = explode(':', $value); $isSupported = ($checkType === 'function') ? @function_exists($checkValue) : @class_exists($checkValue); ?> - diff --git a/install/install_dbsettings_handler.php b/install/install_dbsettings_handler.php index ecc6cb0be..bcf479b93 100644 --- a/install/install_dbsettings_handler.php +++ b/install/install_dbsettings_handler.php @@ -27,6 +27,7 @@ } if (!empty($value)) { $fileContent .= "$key: $value\n"; + $_SESSION[$key] = $value; } } diff --git a/install/install_dbtables.php b/install/install_dbtables.php index 421c91bd7..f1b1a6de1 100644 --- a/install/install_dbtables.php +++ b/install/install_dbtables.php @@ -5,6 +5,7 @@ //$detectedDbVersion = "v1.1.2"; //$databaseCurrent = false; $sql = ''; + $buttonLabel = translate('Upgrade Database'); if ($databaseCurrent) { $msg = translate("Your XXX database named 'YYY' is up to date. You may go on to the next step."); $msg = str_replace('XXX', $_SESSION['db_type'], $msg); @@ -17,6 +18,7 @@ $msg = str_replace('YYY', $_SESSION['db_database'], $msg); $sqlFile = getSqlFile($_SESSION['db_type'], false); $sql = extractSqlCommandsFromFile($sqlFile); + $buttonLabel = translate('Create Database'); } else { $msg = translate("Your XXX database named 'YYY' needs upgrading from version ZZZ."); $msg = str_replace('XXX', $_SESSION['db_type'], $msg); @@ -40,7 +42,7 @@ ?> diff --git a/install/install_dbtables_handler.php b/install/install_dbtables_handler.php index 03d5d767e..b98fed423 100644 --- a/install/install_dbtables_handler.php +++ b/install/install_dbtables_handler.php @@ -39,7 +39,7 @@ try { $success = true; if (empty($error)) { - if ($emptyDb) { + if ($emptyDatabase) { executeSqlFromFile($install_filename); } else { if (empty($detectedDbVersion) || $detectedDbVersion == 'Unknown') { diff --git a/install/install_functions.php b/install/install_functions.php index 27d96984d..81977d6bf 100644 --- a/install/install_functions.php +++ b/install/install_functions.php @@ -487,7 +487,7 @@ function getDatabaseVersionFromSchema($silent = true) global $database_upgrade_matrix, $PROGRAM_VERSION, $settings, $show_all_errors; $dbVersion = null; $success = true; - //$silent = false; + $silent = true; // Suppress errors based on $show_all_errors. if (!$show_all_errors) @@ -514,6 +514,9 @@ function getDatabaseVersionFromSchema($silent = true) // could have worked on. $res = false; $success = false; + if (!$silent) { + echo "Failed at: $sql