From d9d4b9bdab8ac4b6e5c01926da9d5a084707be94 Mon Sep 17 00:00:00 2001 From: Steven Wittens Date: Wed, 27 Jul 2005 01:58:43 +0000 Subject: - #27231: Friendly DB error screens. --- includes/bootstrap.inc | 18 +++++++++++++++++- includes/common.inc | 4 ++-- includes/database.inc | 6 +++++- includes/database.mysql.inc | 31 +++++++++++++++++++++++++++++-- includes/database.pgsql.inc | 27 ++++++++++++++++++++++++++- includes/theme.inc | 27 ++++++++++++++++++++++++++- includes/unicode.inc | 11 +++++++++-- 7 files changed, 114 insertions(+), 10 deletions(-) (limited to 'includes') diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index fe85eb447..8354520e6 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -864,4 +864,20 @@ function _drupal_bootstrap($phase) { } } -?> +/** + * Enables use of the theme system without requiring database access. Since + * there is not database access no theme will be enabled and the default + * themable fuctions will be called. Some themable functions can not be used + * without the full Drupal API loaded. For example, theme_page() is + * unavailable and theme_maintenance_page() must be used in its place. + */ +function drupal_maintenance_theme() { + global $theme; + require_once './includes/theme.inc'; + require_once './includes/common.inc'; + require_once './includes/unicode.inc'; + unicode_check(); + $theme = ''; +} + +?> \ No newline at end of file diff --git a/includes/common.inc b/includes/common.inc index 93724afd6..2672bf895 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1831,7 +1831,7 @@ function drupal_implode_autocomplete($array) { function _drupal_bootstrap_full() { static $called; - global $locale, $multibyte; + global $locale; if ($called) { return; @@ -1850,7 +1850,7 @@ function _drupal_bootstrap_full() { // Emit the correct charset HTTP header. drupal_set_header('Content-Type: text/html; charset=utf-8'); // Detect string handling method - $multibyte = unicode_check(); + unicode_check(); // Initialize $_GET['q'] prior to loading modules and invoking hook_init(). if (!empty($_GET['q'])) { $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/')); diff --git a/includes/database.inc b/includes/database.inc index c76b0083d..fa111b460 100644 --- a/includes/database.inc +++ b/includes/database.inc @@ -116,7 +116,11 @@ function db_set_active($name = 'default') { include_once($handler); } else { - die('Unsupported database type'); + drupal_maintenance_theme(); + drupal_set_title('Unsupported database type'); + print theme('maintenance_page', '

The database type '. theme('placeholder', $db_type) .' is unsupported. Please use either mysql for MySQL databases or pgsql for PostgreSQL databases. The database information is in your settings.php file.

+

For more help, see the Installation and upgrading handbook. If you are unsure what these terms mean you should probably contact your hosting provider.

'); + exit; } $db_conns[$name] = db_connect($connect_url); diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index 78bd24656..22012d432 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -34,9 +34,36 @@ function db_connect($url) { // server. // - 2 means CLIENT_FOUND_ROWS: return the number of found // (matched) rows, not the number of affected rows. - $connection = mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2) or die(mysql_error()); + $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2); + if (!$connection) { + drupal_maintenance_theme(); + drupal_set_title('Unable to connect to database server'); + print theme('maintenance_page', '

This either means that the username and password information in your settings.php file is incorrect or we can\'t contact the MySQL database server. This could mean your hosting provider\'s database server is down.

+

The MySQL error was: '. theme('placeholder', mysql_error()) .'.

+

Currently, the username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.

+ +

For more help, see the Installation and upgrading handbook. If you are unsure what these terms mean you should probably contact your hosting provider.

'); + exit; + } - mysql_select_db(substr($url['path'], 1)) or die('unable to select database'); + if (!mysql_select_db(substr($url['path'], 1))) { + drupal_maintenance_theme(); + drupal_set_title('Unable to select database'); + print theme('maintenance_page', '

We were able to connect to the MySQL database server (which means your username and password is okay) but not able to select the database.

+

The MySQL error was: '. theme('placeholder', mysql_error()) .'.

+

Currently, the database is '. theme('placeholder', substr($url['path'], 1)) .'. The username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.

+ +

For more help, see the Installation and upgrading handbook. If you are unsure what these terms mean you should probably contact your hosting provider.

'); + exit; + } return $connection; } diff --git a/includes/database.pgsql.inc b/includes/database.pgsql.inc index 9e4ecf21b..46acbe14a 100644 --- a/includes/database.pgsql.inc +++ b/includes/database.pgsql.inc @@ -25,7 +25,32 @@ function db_connect($url) { $conn_string = ' user='. $url['user'] .' dbname='. substr($url['path'], 1) .' password='. $url['pass'] . ' host=' . $url['host']; $conn_string .= isset($url['port']) ? ' port=' . $url['port'] : ''; - $connection = pg_connect($conn_string) or die(pg_last_error()); + + // pg_last_error() does not return a useful error message for database + // connection errors. We must turn on error tracking to get at a good error + // message, which will be stored in $php_errormsg. + $track_errors_previous = ini_get('track_errors'); + ini_set('track_errors', 1); + + $connection = @pg_connect($conn_string); + if (!$connection) { + drupal_maintenance_theme(); + drupal_set_title('Unable to connect to database'); + print theme('maintenance_page', '

This either means that the database information in your settings.php file is incorrect or we can\'t contact the PostgreSQL database server. This could mean your hosting provider\'s database server is down.

+

The PostgreSQL error was: '. theme('placeholder', decode_entities($php_errormsg)) .'

+

Currently, the database is '. theme('placeholder', substr($url['path'], 1)) .', the username is '. theme('placeholder', $url['user']) .', and the database server is '. theme('placeholder', $url['host']) .'.

+ +

For more help, see the Installation and upgrading handbook. If you are unsure what these terms mean you should probably contact your hosting provider.

'); + exit; + } + + // Restore error tracking setting + ini_set('track_errors', $track_errors_previous); return $connection; } diff --git a/includes/theme.inc b/includes/theme.inc index 02f74fd36..04100a8e9 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -160,7 +160,7 @@ function list_theme_engines($refresh = FALSE) { function theme() { global $theme, $theme_engine; - if (!$theme) { + if ($theme === NULL) { // Initialize the enabled theme. $theme = init_theme(); } @@ -440,6 +440,31 @@ function theme_page($content) { return $output; } +function theme_maintenance_page($content) { + drupal_set_header('Content-Type: text/html; charset=utf-8'); + theme('add_style', 'misc/drupal.css'); + theme('add_style', 'misc/maintenance.css'); + $output = "\n"; + $output .= ''; + $output .= ''; + $output .= ''; + $output .= ' '. drupal_get_title() .''; + $output .= theme_get_styles(); + $output .= ''; + $output .= ''; + $output .= '

' . drupal_get_title() . '

'; + + $output .= theme('status_messages'); + + $output .= "\n\n"; + $output .= $content; + $output .= "\n\n"; + + $output .= ''; + + return $output; +} + /** * Returns themed set of status and/or error messages. The messages are grouped * by type. diff --git a/includes/unicode.inc b/includes/unicode.inc index 368bc8596..6acc41411 100644 --- a/includes/unicode.inc +++ b/includes/unicode.inc @@ -5,6 +5,13 @@ define('UNICODE_ERROR', -1); define('UNICODE_SINGLEBYTE', 0); define('UNICODE_MULTIBYTE', 1); +/** + * Wrapper around _unicode_check(). + */ +function unicode_check() { + $GLOBALS['multibyte'] = _unicode_check(); +} + /** * Perform checks about Unicode support in PHP, and set the right settings if * needed. @@ -16,7 +23,7 @@ define('UNICODE_MULTIBYTE', 1); * @param $errors * Whether to report any fatal errors with form_set_error(). */ -function unicode_check($errors = false) { +function _unicode_check($errors = false) { // Set the standard C locale to ensure consistent, ASCII-only string handling. setlocale(LC_CTYPE, 'C'); @@ -70,7 +77,7 @@ function unicode_check($errors = false) { * Return the required Unicode status and errors for admin/settings. */ function unicode_settings() { - $status = unicode_check(true); + $status = _unicode_check(true); $options = array(UNICODE_SINGLEBYTE => t('Standard PHP: operations on Unicode strings are emulated on a best-effort basis. Install the PHP mbstring extension for improved Unicode support.', array('%url' => 'http://www.php.net/manual/nl/ref.mbstring.php')), UNICODE_MULTIBYTE => t('Multi-byte: operations on Unicode strings are supported through the PHP mbstring extension.', array('%url' => 'http://www.php.net/manual/nl/ref.mbstring.php')), UNICODE_ERROR => t('Invalid: the current configuration is incompatible with Drupal.')); -- cgit v1.2.3