Skip to content

Commit

Permalink
PHP8.3 fixes, installer fixes
Browse files Browse the repository at this point in the history
- Fixed bug in last commit that removed dbi_free_result
- Fixed PHP 8.3 warnings in Event.php class
- Installer fixes, particularly for new installs
  • Loading branch information
craigk5n committed Nov 28, 2023
1 parent 872b8f1 commit e683ee0
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 151 deletions.
21 changes: 14 additions & 7 deletions includes/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand All @@ -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.
Expand Down
160 changes: 87 additions & 73 deletions includes/dbi4php.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 6 additions & 1 deletion install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -381,7 +382,11 @@ function_exists('gd_info'),
<?php
// Get status for each step
$settingsStorage = getenv('WEBCALENDAR_USE_ENV') ? translate("Environment variables") : "includes/settings.php";
$dbConnectionStatus = $canConnectDb ? "Can connect" : ("Cannot connect" . (empty($connectError) ? "" : ": " . $connectError));
if (empty($_SESSION['db_type'])) {
$dbConnectionStatus = translate('Unknown');
} else {
$dbConnectionStatus = $canConnectDb ? "Can connect" : ("Cannot connect" . (empty($connectError) ? "" : ": " . $connectError));
}
$setupVersion = $PROGRAM_VERSION;
$phpVersionStatus = (version_compare(phpversion(), '8.0', '>=')) ? "Supported" : "Not supported";
?>
Expand Down
2 changes: 1 addition & 1 deletion install/install_appsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,6 @@ function handlePulldownUpdate() {
printNextPageButton($action);
}
} else {
printSubmitButton($action, $html, $buttonLabel);
printSubmitButton($action, $html ?? null, $buttonLabel ?? null);
}
?>
62 changes: 34 additions & 28 deletions install/install_appsettings_handler.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
<?php
$appSettingsCorrect = false;
$app_settings = ['readonly', 'single_user', 'use_http_auth', 'user_inc', 'mode'];
if ($usingEnv) {
// This shouldn't happen.
$error = translate('Unknown error');
} else {
// Should not be getting here...
if ($appSettingsCorrect) {
// Save settings to session
$_SESSION['use_http_auth'] == 'N'; // default
$_SESSION['user_inc'] = $_POST['user_inc'];
if ($_SESSION['user_inc'] == 'http') {
$_SESSION['user_inc'] == 'user.php';
$_SESSION['use_http_auth'] == 'Y';
} else if ($_SESSION['user_inc'] == 'none') {
$_SESSION['user_inc'] == 'user.php'; // single-user
}
$_SESSION['single_user'] = $_POST['user_inc'] == 'none' ? 'Y' : 'N';
// Save settings to session
$_SESSION['use_http_auth'] == 'N'; // default
$_SESSION['user_inc'] = $_POST['user_inc'];
if ($_SESSION['user_inc'] == 'http') {
$_SESSION['user_inc'] == 'user.php';
$_SESSION['use_http_auth'] == 'Y';
} else if ($_SESSION['user_inc'] == 'none') {
$_SESSION['user_inc'] == 'user.php'; // single-user
}
$_SESSION['single_user'] = $_POST['user_inc'] == 'none' ? 'Y' : 'N';
if (empty($_POST['readonly']))
$_SESSION['readonly'] = 'N';
else
$_SESSION['readonly'] = $_POST['readonly'] == '1' ? 'Y' : 'N';
$_SESSION['mode'] = $_POST['mode'];
$_SESSION['single_user_login'] = $_POST['single_user_login'];
// echo "<pre>"; print_r($_SESSION); echo "</pre>"; 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 "<pre>"; print_r($_SESSION); echo "</pre>"; 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');
}
Loading

0 comments on commit e683ee0

Please sign in to comment.