diff --git a/config-templates/config.php b/config-templates/config.php index 5232851b4d27ec2962d6406ba9806c04743195e6..89605903b49bd2535c2fb6be172a31abe191d58c 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 b9e5f8777538c76937d536d5e24f859be62bf5a9..00815151ca21f5df0b262fc32305ed8c822c11a7 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 781e26e2bd7df79fc5312a26e52770fab5d0313e..c4d9ae043be5a2c6d0c2a72f40b98340c42d4a84 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 0533175d4ed46be125affe0f987282c75cc5a26e..f7446668e3df998573f500495148003d4022826a 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