From 449ec1c298e28df4e2de30feaa4fbe1ae53be360 Mon Sep 17 00:00:00 2001 From: Olav Morken <olav.morken@uninett.no> Date: Tue, 18 Aug 2009 07:12:19 +0000 Subject: [PATCH] Add timezone-option. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1687 44740490-163a-0410-bde0-09ae8108e29a --- config-templates/config.php | 11 ++++++++- docs/simplesamlphp-install.txt | 19 ++++++--------- lib/SimpleSAML/Utilities.php | 44 ++++++++++++++++++++++++++++++++++ www/_include.php | 3 +++ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/config-templates/config.php b/config-templates/config.php index 5232851b4..89605903b 100644 --- a/config-templates/config.php +++ b/config-templates/config.php @@ -71,7 +71,16 @@ $config = array ( */ 'technicalcontact_name' => 'Administrator', 'technicalcontact_email' => 'na@example.org', - + + /* + * The timezone of the server. This option should be set to the timezone you want + * simpleSAMLphp to report the time in. The default is to guess the timezone based + * on your system timezone. + * + * See this page for a list of valid timezones: http://php.net/manual/en/timezones.php + */ + 'timezone' => NULL, + /* * Logging. * diff --git a/docs/simplesamlphp-install.txt b/docs/simplesamlphp-install.txt index b9e5f8777..00815151c 100644 --- a/docs/simplesamlphp-install.txt +++ b/docs/simplesamlphp-install.txt @@ -166,6 +166,13 @@ file, `config.php`, right away: 'language.default' => 'no', +- + Set the timezone which you use: + + 'timezone' => 'Europe/Oslo', + + * [List of Supported Timezones at php.net](http://php.net/manual/en/timezones.php) + Configuring PHP --------------- @@ -175,18 +182,6 @@ Configuring PHP Some parts of simpleSAMLphp will allow you to send e-mails. In example sending error reports to technical admin, as well as sending in metadata to the federation administrators. If you want to make use of this functionality, you should make sure your PHP installation is configured to be able to send e-mails. It's a common problem that PHP is not configured to send e-mails properly. The configuration differs from system to system. On UNIX, PHP is using sendmail, on Windows SMTP. -### Configuring time zone in PHP - -Some default installations of PHP does not include a timezone setting. The result is that a lot of warnings is thrown in the log files, which may be annoying. Therefore we reccomend to always set a timezone in the `php.ini` configuration file. In example like this: - - [Date] - ; Defines the default timezone used by the date functions - date.timezone = Europe/Oslo - - - * [List of Supported Timezones at php.net](http://php.net/manual/en/timezones.php) - - Enable modules -------------- diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 781e26e2b..c4d9ae043 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -1920,6 +1920,50 @@ class SimpleSAML_Utilities { SimpleSAML_Logger::debug('Successfully validated certificate.'); } + + /** + * Initialize the timezone. + * + * This function should be called before any calls to date(). + */ + public static function initTimezone() { + static $initialized = FALSE; + + if ($initialized) { + return; + } + + $initialized = TRUE; + + $globalConfig = SimpleSAML_Configuration::getInstance(); + + $timezone = $globalConfig->getString('timezone', NULL); + if ($timezone !== NULL) { + if (!date_default_timezone_set($timezone)) { + throw new SimpleSAML_Error_Exception('Invalid timezone set in the \'timezone\'-option in config.php.'); + } + return; + } + + /* We don't have a timezone configured. */ + + /* + * The date_default_timezone_get()-function is likely to cause a warning. + * Since we have a custom error handler which logs the errors with a backtrace, + * this error will be logged even if we prefix the function call with '@'. + * Instead we temporarily replace the error handler. + */ + function ignoreError() { + /* Don't do anything with this error. */ + return TRUE; + } + set_error_handler('ignoreError'); + $serverTimezone = date_default_timezone_get(); + restore_error_handler(); + + /* Set the timezone to the default. */ + date_default_timezone_set($serverTimezone); + } } ?> \ No newline at end of file diff --git a/www/_include.php b/www/_include.php index 0533175d4..f7446668e 100644 --- a/www/_include.php +++ b/www/_include.php @@ -94,4 +94,7 @@ SimpleSAML_Configuration::setConfigDir($configdir); /* Make sure that the session is initialized before any output. */ SimpleSAML_Session::getInstance(); +/* Set the timezone. */ +SimpleSAML_Utilities::initTimezone(); + ?> \ No newline at end of file -- GitLab