From c8ae2497b7e4c7393210ed186acd672a0040389f Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 03 Sep 2008 12:03:19 -0400
Subject: [PATCH] Auto-detect client language and timezone if desired by config/prefs

---
 CHANGELOG                             |    5 +
 program/include/rcube_shared.inc      |   56 ------------------
 installer/config.php                  |    2 
 program/include/main.inc              |   10 ++-
 program/include/rcmail.php            |   19 ++++-
 program/steps/settings/func.inc       |    7 +-
 program/localization/en_US/labels.inc |    1 
 config/main.inc.php.dist              |    7 +-
 program/include/rcube_template.php    |    2 
 program/include/rcube_config.php      |    4 
 program/js/app.js                     |    8 ++
 program/steps/settings/save_prefs.inc |    2 
 12 files changed, 48 insertions(+), 75 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 0e6d676..02289eb 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,11 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2008/09/03 (thomasb)
+----------
+- Allow to auto-detect client language if none set (#1484434)
+- Auto-detect the client timezone (user configurable)
+
 2008/09/03 (alec)
 ----------
 - Add RFC2231 header value continuations support for attachment
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 09fad19..fcf7e8f 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -129,8 +129,9 @@
 // please provide a string of exactly 24 chars.
 $rcmail_config['des_key'] = 'rcmail-!24ByteDESkey*Str';
 
-// the default locale setting
-$rcmail_config['language'] = 'en_US';
+// the default locale setting (leave empty for auto-detection)
+// RFC1766 formatted language name like en_US, de_DE, de_CH, fr_FR, pt_BR
+$rcmail_config['language'] = null;
 
 // use this format for short date display
 $rcmail_config['date_short'] = 'D H:i';
@@ -338,7 +339,7 @@
 $rcmail_config['pagesize'] = 40;
 
 // use this timezone to display date/time
-$rcmail_config['timezone'] = intval(date('O'))/100 - date('I');
+$rcmail_config['timezone'] = 'auto';
 
 // is daylight saving On?
 $rcmail_config['dst_active'] = (bool)date('I');
diff --git a/installer/config.php b/installer/config.php
index fb5d932..12af1c6 100644
--- a/installer/config.php
+++ b/installer/config.php
@@ -453,7 +453,7 @@
 echo $input_locale->show($RCI->getprop('language'));
 
 ?>
-<div>The default locale setting. This also defines the language of the login screen.</div>
+<div>The default locale setting. This also defines the language of the login screen.<br/>Leave it empty to auto-detect the user agent language.</div>
 <p class="hint">Enter a <a href="http://www.faqs.org/rfcs/rfc1766">RFC1766</a> formatted language name. Examples: en_US, de_DE, de_CH, fr_FR, pt_BR</p>
 </dd>
 
diff --git a/program/include/main.inc b/program/include/main.inc
index 83ed8c0..469d844 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -706,9 +706,13 @@
     return '';
    
   // get user's timezone
-  $tz = $CONFIG['timezone'];
-  if ($CONFIG['dst_active'])
-    $tz++;
+  if ($CONFIG['timezone'] == 'auto')
+    $tz = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : intval(date('O'))/100;
+  else {
+    $tz = $CONFIG['timezone'];
+    if ($CONFIG['dst_active'])
+      $tz++;
+  }
 
   // convert time to user's timezone
   $timestamp = $ts - date('Z', $ts) + ($tz * 3600);
diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 348a916..243b6f8 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -15,7 +15,7 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  +-----------------------------------------------------------------------+
 
- $Id: rcube_browser.php 328 2006-08-30 17:41:21Z thomasb $
+ $Id: rcmail.php 328 2006-08-30 17:41:21Z thomasb $
 
 */
 
@@ -167,7 +167,7 @@
       $this->config->merge((array)$this->user->get_prefs());
     }
     
-    $_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language'));
+    $_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language', $_SESSION['language']));
 
     // set localization
     setlocale(LC_ALL, $_SESSION['language'] . '.utf8');
@@ -183,7 +183,13 @@
   private function language_prop($lang)
   {
     static $rcube_languages, $rcube_language_aliases;
-
+    
+    // user HTTP_ACCEPT_LANGUAGE if no language is specified
+    if (empty($lang) || $lang == 'auto') {
+       $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
+       $lang = str_replace('-', '_', $accept_langs[0]);
+     }
+     
     if (empty($rcube_languages)) {
       @include(INSTALL_PATH . 'program/localization/index.inc');
     }
@@ -471,6 +477,9 @@
       $_SESSION['imap_ssl']  = $imap_ssl;
       $_SESSION['password']  = $this->encrypt_passwd($pass);
       $_SESSION['login_time'] = mktime();
+      
+      if ($_REQUEST['_timezone'] != '_default_')
+        $_SESSION['timezone'] = floatval($_REQUEST['_timezone']);
 
       // force reloading complete list of subscribed mailboxes
       $this->set_imap_prop();
@@ -641,7 +650,7 @@
    */
   public function load_language($lang = null)
   {
-    $lang = $lang ? $this->language_prop($lang) : $_SESSION['language'];
+    $lang = $this->language_prop(($lang ? $lang : $_SESSION['language']));
     
     // load localized texts
     if (empty($this->texts) || $lang != $_SESSION['language']) {
@@ -748,7 +757,7 @@
       $this->user->save_prefs(array('message_sort_col' => $_SESSION['sort_col'], 'message_sort_order' => $_SESSION['sort_order']));
     }
 
-    $_SESSION = array('language' => $USER->language, 'auth_time' => time(), 'temp' => true);
+    $_SESSION = array('language' => $this->user->language, 'auth_time' => time(), 'temp' => true);
     setcookie('sessauth', '-del-', time() - 60);
     $this->user->reset();
   }
diff --git a/program/include/rcube_config.php b/program/include/rcube_config.php
index 43f735b..98e688d 100644
--- a/program/include/rcube_config.php
+++ b/program/include/rcube_config.php
@@ -84,9 +84,9 @@
       ini_set('log_errors', 1);
 
       if ($this->prop['log_driver'] == 'syslog') {
-	ini_set('error_log', 'syslog');
+        ini_set('error_log', 'syslog');
       } else {
-	ini_set('error_log', $this->prop['log_dir'].'/errors');
+        ini_set('error_log', $this->prop['log_dir'].'/errors');
       }
     }
     if ($this->prop['debug_level'] & 4) {
diff --git a/program/include/rcube_shared.inc b/program/include/rcube_shared.inc
index d82b1fe..0dd661e 100644
--- a/program/include/rcube_shared.inc
+++ b/program/include/rcube_shared.inc
@@ -28,62 +28,6 @@
 
 
 /**
- * Provide details about the client's browser
- *
- * @return array Key-value pairs of browser properties
- */
-function rcube_browser()
-{
-  $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
-
-  $bw['ver'] = 0;
-  $bw['win'] = stristr($HTTP_USER_AGENT, 'win');
-  $bw['mac'] = stristr($HTTP_USER_AGENT, 'mac');
-  $bw['linux'] = stristr($HTTP_USER_AGENT, 'linux');
-  $bw['unix']  = stristr($HTTP_USER_AGENT, 'unix');
-
-  $bw['ns4'] = stristr($HTTP_USER_AGENT, 'mozilla/4') && !stristr($HTTP_USER_AGENT, 'msie');
-  $bw['ns']  = ($bw['ns4'] || stristr($HTTP_USER_AGENT, 'netscape'));
-  $bw['ie']  = stristr($HTTP_USER_AGENT, 'msie');
-  $bw['mz']  = stristr($HTTP_USER_AGENT, 'mozilla/5');
-  $bw['opera'] = stristr($HTTP_USER_AGENT, 'opera');
-  $bw['safari'] = stristr($HTTP_USER_AGENT, 'safari');
-
-  if($bw['ns'])
-  {
-    $test = eregi("mozilla\/([0-9\.]+)", $HTTP_USER_AGENT, $regs);
-    $bw['ver'] = $test ? (float)$regs[1] : 0;
-  }
-  if($bw['mz'])
-  {
-    $test = ereg("rv:([0-9\.]+)", $HTTP_USER_AGENT, $regs);
-    $bw['ver'] = $test ? (float)$regs[1] : 0;
-  }
-  if($bw['ie'])
-  {
-    $test = eregi("msie ([0-9\.]+)", $HTTP_USER_AGENT, $regs);
-    $bw['ver'] = $test ? (float)$regs[1] : 0;
-  }
-  if($bw['opera'])
-  {
-    $test = eregi("opera ([0-9\.]+)", $HTTP_USER_AGENT, $regs);
-    $bw['ver'] = $test ? (float)$regs[1] : 0;
-  }
-
-  if(eregi(" ([a-z]{2})-([a-z]{2})", $HTTP_USER_AGENT, $regs))
-    $bw['lang'] =  $regs[1];
-  else
-    $bw['lang'] =  'en';
-
-  $bw['dom'] = ($bw['mz'] || $bw['safari'] || ($bw['ie'] && $bw['ver']>=5) || ($bw['opera'] && $bw['ver']>=7));
-  $bw['pngalpha'] = $bw['mz'] || $bw['safari'] || ($bw['ie'] && $bw['ver']>=5.5) ||
-                    ($bw['ie'] && $bw['ver']>=5 && $bw['mac']) || ($bw['opera'] && $bw['ver']>=7) ? TRUE : FALSE;
-
-  return $bw;
-}
-
-
-/**
  * Send HTTP headers to prevent caching this page
  */
 function send_nocacheing_headers()
diff --git a/program/include/rcube_template.php b/program/include/rcube_template.php
index db2275e..847ea0d 100755
--- a/program/include/rcube_template.php
+++ b/program/include/rcube_template.php
@@ -922,6 +922,7 @@
         $input_user   = new html_inputfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30) + $attrib);
         $input_pass   = new html_passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30) + $attrib);
         $input_action = new html_hiddenfield(array('name' => '_action', 'value' => 'login'));
+        $input_tzone  = new html_hiddenfield(array('name' => '_timezone', 'id' => 'rcmlogintz', 'value' => '_default_'));
         $input_host   = null;
 
         if (is_array($default_host)) {
@@ -960,6 +961,7 @@
         }
 
         $out = $input_action->show();
+        $out .= $input_tzone->show();
         $out .= $table->show();
 
         // surround html output with a form tag
diff --git a/program/js/app.js b/program/js/app.js
index ff6ff8c..7ba8572 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -318,13 +318,19 @@
       case 'login':
         var input_user = rcube_find_object('rcmloginuser');
         var input_pass = rcube_find_object('rcmloginpwd');
+        var input_tz = rcube_find_object('rcmlogintz');
+
         if (input_user)
           input_user.onkeyup = function(e){ return rcmail.login_user_keyup(e); };
         if (input_user && input_user.value=='')
           input_user.focus();
         else if (input_pass)
           input_pass.focus();
-          
+
+        // detect client timezone
+        if (input_tz)
+          input_tz.value = new Date().getTimezoneOffset() / -60;
+
         this.enable_command('login', true);
         break;
       
diff --git a/program/localization/en_US/labels.inc b/program/localization/en_US/labels.inc
index b628799..ac43c85 100644
--- a/program/localization/en_US/labels.inc
+++ b/program/localization/en_US/labels.inc
@@ -254,6 +254,7 @@
 $labels['edititem']  = 'Edit item';
 
 $labels['setdefault']  = 'Set default';
+$labels['autodetect']  = 'Auto';
 $labels['language']  = 'Language';
 $labels['timezone']  = 'Time zone';
 $labels['pagesize']  = 'Rows per page';
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index 5b6e113..ef3c24d 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -49,14 +49,15 @@
     $select_lang->add(array_values($a_lang), array_keys($a_lang));
   
     $table->add('title', html::label($field_id, Q(rcube_label('language'))));
-    $table->add(null, $select_lang->show($_SESSION['language']));
+    $table->add(null, $select_lang->show($config['language']));
   }
 
 
   // show page size selection
   if (!isset($no_override['timezone'])) {
     $field_id = 'rcmfd_timezone';
-    $select_timezone = new html_select(array('name' => '_timezone', 'id' => $field_id));
+    $select_timezone = new html_select(array('name' => '_timezone', 'id' => $field_id, 'onchange' => "document.getElementById('rcmfd_dst').disabled=this.selectedIndex==0"));
+    $select_timezone->add(rcube_label('autodetect'), 'auto');
     $select_timezone->add('(GMT -11:00) Midway Island, Samoa', '-11');
     $select_timezone->add('(GMT -10:00) Hawaii', '-10');
     $select_timezone->add('(GMT -9:30) Marquesas Islands', '-9.5');
@@ -104,7 +105,7 @@
   // daylight savings
   if (!isset($no_override['dst_active'])) {
     $field_id = 'rcmfd_dst';
-    $input_dst = new html_checkbox(array('name' => '_dst_active', 'id' => $field_id, 'value' => 1));
+    $input_dst = new html_checkbox(array('name' => '_dst_active', 'id' => $field_id, 'value' => 1, 'disabled' => ($config['timezone'] == 'auto')));
     
     $table->add('title', html::label($field_id, Q(rcube_label('dstactive'))));
     $table->add(null, $input_dst->show($config['dst_active']));
diff --git a/program/steps/settings/save_prefs.inc b/program/steps/settings/save_prefs.inc
index 3bccffa..edd184a 100644
--- a/program/steps/settings/save_prefs.inc
+++ b/program/steps/settings/save_prefs.inc
@@ -20,7 +20,7 @@
 */
 
 $a_user_prefs = array(
-  'timezone'     => isset($_POST['_timezone']) ? floatval($_POST['_timezone']) : $CONFIG['timezone'],
+  'timezone'     => isset($_POST['_timezone']) ? (is_numeric($_POST['_timezone']) ? floatval($_POST['_timezone']) : get_input_value('_timezone', RCUBE_INPUT_POST)) : $CONFIG['timezone'],
   'dst_active'   => isset($_POST['_dst_active']) ? TRUE : FALSE,
   'pagesize'     => is_numeric($_POST['_pagesize']) ? max(2, intval($_POST['_pagesize'])) : $CONFIG['pagesize'],
   'prettydate'   => isset($_POST['_pretty_date']) ? TRUE : FALSE,

--
Gitblit v1.9.1