Skip to content

Commit

Permalink
Improve Timezone initialization, default to UTC
Browse files Browse the repository at this point in the history
Fixes #382
  • Loading branch information
dregad committed Dec 14, 2014
2 parents 257c1fe + 5080d38 commit 27a65d5
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 64 deletions.
14 changes: 6 additions & 8 deletions account_prefs_update.php
Expand Up @@ -112,14 +112,12 @@
$t_prefs->refresh_delay = config_get( 'min_refresh_delay' );
}

if( function_exists( 'timezone_identifiers_list' ) ) {
$t_timezone = gpc_get_string( 'timezone' );
if( in_array( $t_timezone, timezone_identifiers_list() ) ) {
if( $t_timezone == config_get_global( 'default_timezone' ) ) {
$t_prefs->timezone = '';
} else {
$t_prefs->timezone = $t_timezone;
}
$t_timezone = gpc_get_string( 'timezone' );
if( in_array( $t_timezone, timezone_identifiers_list() ) ) {
if( $t_timezone == config_get_global( 'default_timezone' ) ) {
$t_prefs->timezone = '';
} else {
$t_prefs->timezone = $t_timezone;
}
}

Expand Down
35 changes: 25 additions & 10 deletions admin/check/check_i18n_inc.php
Expand Up @@ -42,18 +42,33 @@
'Default timezone has been specified in config_inc.php (default_timezone option)',
in_array( $t_config_default_timezone, timezone_identifiers_list() ),
array(
true => 'Default timezone is: ' . htmlentities( $t_config_default_timezone ),
false => 'Invalid timezone \'' . htmlentities( $t_config_default_timezone ) . '\' specified for the default_timezone configuration option.'
true => "Default timezone is '" . htmlentities( $t_config_default_timezone ) . "'",
false => "Invalid timezone '" . htmlentities( $t_config_default_timezone ) . "' specified. "
. 'Refer to the <a href="http://php.net/timezones">List of Supported Timezones</a>.'
)
);
} else {
$t_php_default_timezone = ini_get( 'date.timezone' );
check_print_test_row(
'Default timezone has been specified in config_inc.php (default_timezone option) or php.ini (date.timezone directive)',
in_array( $t_php_default_timezone, timezone_identifiers_list() ),
array(
true => 'Default timezone (specified by the date.timezone directive in php.ini) is: ' . htmlentities( $t_php_default_timezone ),
false => 'Invalid timezone \'' . htmlentities( $t_php_default_timezone ) . '\' specified for the date.timezone php.ini directive.'
)
);
$t_msg = 'No timezone has been specified in config_inc.php (default_timezone option)';
$t_tz_link = '<a href="http://ch1.php.net/datetime.configuration#ini.date.timezone">date.timezone</a>';

if( $t_php_default_timezone ) {
check_print_test_row(
$t_msg,
in_array( $t_php_default_timezone, timezone_identifiers_list() ),
array(
true => "Default timezone (specified by the $t_tz_link directive in php.ini) "
. "is '" . htmlentities( $t_php_default_timezone ) . "'",
false => "Invalid timezone '" . htmlentities( $t_config_default_timezone ) . "' specified. "
)
);
} else {
check_print_test_warn_row(
$t_msg,
!empty( $t_php_default_timezone ),
array(
false => "Timezone has been defaulted to 'UTC'."
)
);
}
}
8 changes: 7 additions & 1 deletion admin/install.php
Expand Up @@ -630,6 +630,12 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
echo '<input id="' . $t_key . '" name="' . $t_key . '" type="textbox" value="' . $f_db_table_prefix . '">';
echo "\n\t</td>\n</tr>\n\n";
}

# Default timezone, get PHP setting if not defined in Mantis
$t_tz = config_get_global( 'default_timezone' );
if( is_blank( $t_tz ) ) {
$t_tz = @date_default_timezone_get();
}
?>
<!-- Timezone -->
<tr>
Expand All @@ -638,7 +644,7 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
</td>
<td>
<select id="timezone" name="timezone">
<?php print_timezone_option_list( config_get_global( 'default_timezone' ) ) ?>
<?php print_timezone_option_list( $t_tz ) ?>
</select>
</td>
</tr>
Expand Down
6 changes: 5 additions & 1 deletion admin/schema.php
Expand Up @@ -50,7 +50,11 @@ function db_null_date() {
function installer_db_now() {
global $g_db;

return $g_db->BindTimeStamp( time() );
$t_timezone = @date_default_timezone_get();
date_default_timezone_set( 'UTC' );
$t_time = $g_db->BindTimeStamp( time() );
@date_default_timezone_set( $t_timezone );
return $t_time;
}

# Special handling for Oracle (oci8):
Expand Down
13 changes: 9 additions & 4 deletions config_defaults_inc.php
Expand Up @@ -1134,12 +1134,17 @@
/**
* Default timezone to use in MantisBT
*
* If this config is left blank, it will be initialized by calling function
* This configuration is normally initialized when installing Mantis.
* It should be set to one of the values specified in the
* {@link http://php.net/timezones List of Supported Timezones}.
* If this config is left blank, the timezone will be initialized by calling
* {@link http://php.net/date-default-timezone-get date_default_timezone_get()}
* to determine the default timezone.
* Note that this function's behavior was modified in PHP 5.4.0.
* (note that this function's behavior was modified in PHP 5.4.0), which will
* fall back to 'UTC' if unable to determine the timezone.
* Correct configuration of this variable can be confirmed by running the
* administration checks.
* Users can override the default timezone under their preferences.
*
* @link http://php.net/timezones List of Supported Timezones
* @global string $g_default_timezone
*/
$g_default_timezone = '';
Expand Down
36 changes: 15 additions & 21 deletions core.php
Expand Up @@ -236,33 +236,27 @@ function __autoload( $p_class ) {
$g_login_anonymous = true;
}

# Attempt to set the current timezone to the user's desired value
# Note that PHP 5.1 on RHEL/CentOS doesn't support the timezone functions
# used here so we just skip this action on RHEL/CentOS platforms.
if( function_exists( 'timezone_identifiers_list' ) ) {
if( in_array( config_get_global( 'default_timezone' ), timezone_identifiers_list() ) ) {
# if a default timezone is set in config, set it here, else we use php.ini's value
# having a timezone set avoids a php warning
date_default_timezone_set( config_get_global( 'default_timezone' ) );
} else {
# To ensure proper detection of timezone settings issues, we must not
# initialize the default timezone when executing admin checks
if( basename( $g_short_path ) != 'check' ) {
config_set_global( 'default_timezone', date_default_timezone_get(), true );
}
}

# Set the current timezone
if( !defined( 'MANTIS_MAINTENANCE_MODE' ) ) {
require_api( 'authentication_api.php' );

# To reduce overhead, we assume that the timezone configuration is valid,
# i.e. it exists in timezone_identifiers_list(). If not, a PHP NOTICE will
# be raised. Use admin checks to validate configuration.
@date_default_timezone_set( config_get_global( 'default_timezone' ) );
$t_tz = @date_default_timezone_get();
config_set_global( 'default_timezone', $t_tz, true );

if( auth_is_user_authenticated() ) {
# Determine the current timezone according to user's preferences
require_api( 'user_pref_api.php' );

$t_user_timezone = user_pref_get_pref( auth_get_current_user_id(), 'timezone' );
if( !is_blank( $t_user_timezone ) ) {
date_default_timezone_set( $t_user_timezone );
}
$t_tz = user_pref_get_pref( auth_get_current_user_id(), 'timezone' );
@date_default_timezone_set( $t_tz );
}
unset( $t_tz );
}

# Cache current user's collapse API data
if( !defined( 'MANTIS_MAINTENANCE_MODE' ) ) {
require_api( 'collapse_api.php' );
collapse_cache_token();
Expand Down
4 changes: 4 additions & 0 deletions core/date_api.php
Expand Up @@ -23,18 +23,22 @@
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses authentication_api.php
* @uses config_api.php
* @uses constant_inc.php
* @uses helper_api.php
* @uses html_api.php
* @uses lang_api.php
* @uses user_pref_api.php
*/

require_api( 'authentication_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'helper_api.php' );
require_api( 'html_api.php' );
require_api( 'lang_api.php' );
require_api( 'user_pref_api.php' );

# Keeps track of whether the external files required for jscalendar to work
# have already been included in the output sent to the client. jscalendar
Expand Down
19 changes: 10 additions & 9 deletions core/print_api.php
Expand Up @@ -1911,18 +1911,19 @@ function print_bug_attachment_preview_image( array $p_attachment ) {
* @return void
*/
function print_timezone_option_list( $p_timezone ) {
if( !function_exists( 'timezone_identifiers_list' ) ) {
echo "\t" . '<option value="' . $p_timezone . '" selected="selected">' . $p_timezone . '</option>' . "\n";
return;
}

$t_identifiers = timezone_identifiers_list( DateTimeZone::ALL );

foreach ( $t_identifiers as $t_identifier ) {
$t_zone = explode( '/', $t_identifier );
if( isset( $t_zone[1] ) != '' ) {
$t_locations[$t_zone[0]][$t_zone[0] . '/' . $t_zone[1]] = array( str_replace( '_', ' ', $t_zone[1] ), $t_identifier );
foreach( $t_identifiers as $t_identifier ) {
$t_zone = explode( '/', $t_identifier, 2 );
if( isset( $t_zone[1] ) ) {
$t_id = $t_zone[1];
} else {
$t_id = $t_identifier;
}
$t_locations[$t_zone[0]][$t_identifier] = array(
str_replace( '_', ' ', $t_id ),
$t_identifier
);
}

foreach( $t_locations as $t_continent => $t_locations ) {
Expand Down
27 changes: 17 additions & 10 deletions docbook/Admin_Guide/en-US/config/timezone.xml
Expand Up @@ -10,20 +10,27 @@
<term>$g_default_timezone</term>
<listitem>
<para>Default timezone to use in MantisBT.
This must be set to one of the values specified in the
<ulink url="http://php.net/timezones ">
This configuration is normally initialized when
installing Mantis.
It should be set to one of the values specified in the
<ulink url="http://php.net/timezones">
List of Supported Timezones</ulink>.
</para>
<para>If this config is left blank (default), it will be
initialized by calling function
<para>If this config is left blank, the timezone
will be initialized by calling function
<ulink url="http://php.net/date-default-timezone-get">
date_default_timezone_get()</ulink>
to determine the default timezone.
With PHP &gt;= 5.3, this will result in a system warning if
the timezone has not been defined in
<ulink url="http://php.net/date.timezone">php.ini</ulink>.
Note that this function's behavior was modified in PHP 5.4.0.
date_default_timezone_get()</ulink>,
which will fall back to <emphasis>UTC</emphasis>
if unable to determine the timezone.
</para>
<para>Correct configuration of this variable can be confirmed
by running the administration checks.
Users can override the default timezone under user
their preferences.
</para>
<note>Note that this function's behavior was modified in
PHP 5.4.0.
</note>
</listitem>
</varlistentry>
</variablelist>
Expand Down
1 change: 1 addition & 0 deletions scripts/travis_before_script.sh
Expand Up @@ -118,6 +118,7 @@ declare -A query=(
[db_password]=$DB_PASSWORD
[admin_username]=$DB_USER
[admin_password]=$DB_PASSWORD
[timezone]=UTC
)

# Build http query string
Expand Down

0 comments on commit 27a65d5

Please sign in to comment.