From 086b153ae274e528e709d179795d0bafd5f382bd Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Wed, 31 Oct 2012 11:50:33 +0100 Subject: Improve client-side timezone detection using jsTimezoneDetect by Jon Nylander (#1488725); removed obsolete dstactive detection --- program/include/rcmail.php | 4 +--- program/include/rcube_config.php | 15 ++++++++++++++- program/include/rcube_output_html.php | 5 +++-- program/js/app.js | 14 ++++++++------ program/js/jstz.min.js | 11 +++++++++++ 5 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 program/js/jstz.min.js (limited to 'program') diff --git a/program/include/rcmail.php b/program/include/rcmail.php index c2f76b388..3728e5d19 100644 --- a/program/include/rcmail.php +++ b/program/include/rcmail.php @@ -542,9 +542,7 @@ class rcmail extends rcube $_SESSION['login_time'] = time(); if (isset($_REQUEST['_timezone']) && $_REQUEST['_timezone'] != '_default_') - $_SESSION['timezone'] = floatval($_REQUEST['_timezone']); - if (isset($_REQUEST['_dstactive']) && $_REQUEST['_dstactive'] != '_default_') - $_SESSION['dst_active'] = intval($_REQUEST['_dstactive']); + $_SESSION['timezone'] = rcube_utils::get_input_value('_timezone', rcube_utils::INPUT_GPC); // force reloading complete list of subscribed mailboxes $storage->clear_cache('mailboxes', true); diff --git a/program/include/rcube_config.php b/program/include/rcube_config.php index b9fd95578..1f165ba4a 100644 --- a/program/include/rcube_config.php +++ b/program/include/rcube_config.php @@ -412,7 +412,20 @@ class rcube_config */ private function client_timezone() { - return isset($_SESSION['timezone']) && ($ctz = timezone_name_from_abbr("", $_SESSION['timezone'] * 3600, 0)) ? $ctz : date_default_timezone_get(); + if (isset($_SESSION['timezone']) && is_numeric($_SESSION['timezone']) + && ($ctz = timezone_name_from_abbr("", $_SESSION['timezone'] * 3600, 0))) { + return $ctz; + } + else if (!empty($_SESSION['timezone'])) { + try { + $tz = timezone_open($_SESSION['timezone']); + return $tz->getName(); + } + catch (Exception $e) { /* gracefully ignore */ } + } + + // fallback to server's timezone + return date_default_timezone_get(); } } diff --git a/program/include/rcube_output_html.php b/program/include/rcube_output_html.php index d42171869..71aec7775 100644 --- a/program/include/rcube_output_html.php +++ b/program/include/rcube_output_html.php @@ -1493,7 +1493,6 @@ class rcube_output_html extends rcube_output $input_task = new html_hiddenfield(array('name' => '_task', 'value' => 'login')); $input_action = new html_hiddenfield(array('name' => '_action', 'value' => 'login')); $input_tzone = new html_hiddenfield(array('name' => '_timezone', 'id' => 'rcmlogintz', 'value' => '_default_')); - $input_dst = new html_hiddenfield(array('name' => '_dstactive', 'id' => 'rcmlogindst', 'value' => '_default_')); $input_url = new html_hiddenfield(array('name' => '_url', 'id' => 'rcmloginurl', 'value' => $url)); $input_user = new html_inputfield(array('name' => '_user', 'id' => 'rcmloginuser') + $attrib + $user_attrib); @@ -1545,7 +1544,6 @@ class rcube_output_html extends rcube_output $out = $input_task->show(); $out .= $input_action->show(); $out .= $input_tzone->show(); - $out .= $input_dst->show(); $out .= $input_url->show(); $out .= $table->show(); @@ -1558,6 +1556,9 @@ class rcube_output_html extends rcube_output $out = $this->form_tag(array('name' => $form_name, 'method' => 'post'), $out); } + // include script for timezone detection + $this->include_script('jstz.min.js'); + return $out; } diff --git a/program/js/app.js b/program/js/app.js index c151a1012..6741a480e 100644 --- a/program/js/app.js +++ b/program/js/app.js @@ -424,12 +424,14 @@ function rcube_webmail() $('#rcmloginpwd').focus(); // detect client timezone - var dt = new Date(), - tz = dt.getTimezoneOffset() / -60, - stdtz = dt.getStdTimezoneOffset() / -60; - - $('#rcmlogintz').val(stdtz); - $('#rcmlogindst').val(tz > stdtz ? 1 : 0); + if (window.jstz && !bw.ie6) { + var timezone = jstz.determine(); + if (timezone.name()) + $('#rcmlogintz').val(timezone.name()); + } + else { + $('#rcmlogintz').val(new Date().getStdTimezoneOffset() / -60); + } // display 'loading' message on form submit, lock submit button $('form').submit(function () { diff --git a/program/js/jstz.min.js b/program/js/jstz.min.js new file mode 100644 index 000000000..81c5057fd --- /dev/null +++ b/program/js/jstz.min.js @@ -0,0 +1,11 @@ +var jstz=function(){var b=function(a){a=-a.getTimezoneOffset();return null!==a?a:0},c=function(){return b(new Date(2010,0,1,0,0,0,0))},f=function(){return b(new Date(2010,5,1,0,0,0,0))},e=function(){var a=c(),d=f(),b=c()-f();return new jstz.TimeZone(jstz.olson.timezones[0>b?a+",1":0