From e170b4b7f85767703293116c95d9e02020b1c99a Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Thu, 07 Sep 2006 14:54:37 -0400
Subject: [PATCH] Some bugfixes and session expiration stuff

---
 CHANGELOG                                 |    9 +++
 index.php                                 |    6 +-
 program/include/session.inc               |    2 
 program/steps/error.inc                   |    4 
 program/include/main.inc                  |   96 ++++++++++++++++++++-----------
 program/steps/mail/func.inc               |    6 +-
 program/steps/settings/manage_folders.inc |    4 +
 program/js/app.js                         |   41 +++++++++----
 8 files changed, 109 insertions(+), 59 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index c3d6e27..2a5c907 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,15 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2006/09/07 (thomasb)
+----------
+- Made automatic draft saving configurable
+- Fixed JS bug when renaming folders (Bug #1483989)
+- Don't wait for complete page load when calling JavaScript init()
+- Some improvements to prevent session expiration
+- Prevent from double submit of spell check requests
+
+
 2006/09/01 (thomasb)
 ----------
 - Imporoved message parsing and HTML validation
diff --git a/index.php b/index.php
index 68c0f17..85a9eef 100644
--- a/index.php
+++ b/index.php
@@ -2,7 +2,7 @@
 /*
  +-----------------------------------------------------------------------+
  | RoundCube Webmail IMAP Client                                         |
- | Version 0.1-20060904                                                  |
+ | Version 0.1-20060907                                                  |
  |                                                                       |
  | Copyright (C) 2005-2006, RoundCube Dev. - Switzerland                 |
  | Licensed under the GNU GPL                                            |
@@ -40,7 +40,7 @@
 
 */
 
-define('RCMAIL_VERSION', '0.1-20060904');
+define('RCMAIL_VERSION', '0.1-20060907');
 
 // define global vars
 $CHARSET = 'UTF-8';
@@ -181,7 +181,7 @@
 else if ($_action!='login' && $_SESSION['user_id'])
   {
   if (!rcmail_authenticate_session() ||
-      ($CONFIG['session_lifetime'] && isset($SESS_CHANGED) && $SESS_CHANGED + $CONFIG['session_lifetime']*60 < mktime()))
+      (!empty($CONFIG['session_lifetime']) && isset($SESS_CHANGED) && $SESS_CHANGED + $CONFIG['session_lifetime']*60 < mktime()))
     {
     $message = show_message('sessionerror', 'error');
     rcmail_kill_session();
diff --git a/program/include/main.inc b/program/include/main.inc
index ae7d50b..0d3cf82 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -39,40 +39,12 @@
   // check client
   $BROWSER = rcube_browser();
 
-  // load config file
-  include_once('config/main.inc.php');
-  $CONFIG = is_array($rcmail_config) ? $rcmail_config : array();
-  
-  // load host-specific configuration
-  rcmail_load_host_config($CONFIG);
-  
-  $CONFIG['skin_path'] = $CONFIG['skin_path'] ? unslashify($CONFIG['skin_path']) : 'skins/default';
-
-  // load db conf
-  include_once('config/db.inc.php');
-  $CONFIG = array_merge($CONFIG, $rcmail_config);
-
-  if (empty($CONFIG['log_dir']))
-    $CONFIG['log_dir'] = $INSTALL_PATH.'logs';
-  else
-    $CONFIG['log_dir'] = unslashify($CONFIG['log_dir']);
-
-  // set PHP error logging according to config
-  if ($CONFIG['debug_level'] & 1)
-    {
-    ini_set('log_errors', 1);
-    ini_set('error_log', $CONFIG['log_dir'].'/errors');
-    }
-  if ($CONFIG['debug_level'] & 4)
-    ini_set('display_errors', 1);
-  else
-    ini_set('display_errors', 0);
-
+  // load configuration
+  $CONFIG = rcmail_load_config();
 
   // set session garbage collecting time according to session_lifetime
   if (!empty($CONFIG['session_lifetime']))
-    ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']+2)*60);
-
+    ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']) * 120);
 
   // prepare DB connection
   require_once('include/rcube_'.(empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']).'.inc');
@@ -80,7 +52,7 @@
   $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr'], $CONFIG['db_persistent']);
   $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
   $DB->db_connect('w');
-    
+
   // we can use the database for storing session data
   if (!$DB->is_error())
     include_once('include/session.inc');
@@ -129,6 +101,44 @@
   }
 
 
+// load roundcube configuration into global var
+function rcmail_load_config()
+  {
+	global $INSTALL_PATH;
+
+  // load config file
+	include_once('config/main.inc.php');
+	$conf = is_array($rcmail_config) ? $rcmail_config : array();
+
+  // load host-specific configuration
+  rcmail_load_host_config($conf);
+
+  $conf['skin_path'] = $conf['skin_path'] ? unslashify($conf['skin_path']) : 'skins/default';
+
+  // load db conf
+  include_once('config/db.inc.php');
+  $conf = array_merge($conf, $rcmail_config);
+
+  if (empty($conf['log_dir']))
+    $conf['log_dir'] = $INSTALL_PATH.'logs';
+  else
+    $conf['log_dir'] = unslashify($conf['log_dir']);
+
+  // set PHP error logging according to config
+  if ($conf['debug_level'] & 1)
+    {
+    ini_set('log_errors', 1);
+    ini_set('error_log', $conf['log_dir'].'/errors');
+    }
+  if ($conf['debug_level'] & 4)
+    ini_set('display_errors', 1);
+  else
+    ini_set('display_errors', 0);
+
+  return $conf;
+  }
+
+
 // load a host-specific config file if configured
 function rcmail_load_host_config(&$config)
   {
@@ -169,15 +179,24 @@
 function rcmail_authenticate_session()
   {
   $now = mktime();
-  $valid = ($_COOKIE['sessauth'] == rcmail_auth_hash(session_id(), $_SESSION['auth_time']));
+  $valid = ($_COOKIE['sessauth'] == rcmail_auth_hash(session_id(), $_SESSION['auth_time']) ||
+						$_COOKIE['sessauth'] == rcmail_auth_hash(session_id(), $_SESSION['last_auth']));
 
   // renew auth cookie every 5 minutes (only for GET requests)
   if (!$valid || ($_SERVER['REQUEST_METHOD']!='POST' && $now-$_SESSION['auth_time'] > 300))
     {
+	  $_SESSION['last_auth'] = $_SESSION['auth_time'];
     $_SESSION['auth_time'] = $now;
     setcookie('sessauth', rcmail_auth_hash(session_id(), $now));
     }
-    
+
+  if (!$valid)
+    write_log('timeouts',
+      "REQUEST: " . var_export($_REQUEST, true) .
+      "\nEXPECTED: " . rcmail_auth_hash(session_id(), $_SESSION['auth_time']) .
+      "\nOR LAST: " . rcmail_auth_hash(session_id(), $_SESSION['last_auth']) .
+      "\nSESSION: " . var_export($_SESSION, true));
+
   return $valid;
   }
 
@@ -353,11 +372,15 @@
       $javascript .= "$JS_OBJECT_NAME.set_env('$js_config_var', '" . $CONFIG[$js_config_var] . "');\n";
     }
   }
+
+  // don't wait for page onload. Call init at the bottom of the page (delayed)
+  $javascript_foot = "if (window.call_init)\n call_init('$JS_OBJECT_NAME');";
   
   if (!empty($GLOBALS['_framed']))
     $javascript .= "$JS_OBJECT_NAME.set_env('framed', true);\n";
     
-  $OUTPUT->add_script($javascript);
+  $OUTPUT->add_script($javascript, 'head');
+  $OUTPUT->add_script($javascript_foot, 'foot');
   $OUTPUT->include_script('common.js');
   $OUTPUT->include_script('app.js');
   $OUTPUT->scripts_path = 'program/js/';
@@ -1802,6 +1825,9 @@
 function write_log($name, $line)
   {
   global $CONFIG;
+
+  if (!is_string($line))
+    $line = var_export($line, true);
   
   $log_entry = sprintf("[%s]: %s\n",
                  date("d-M-Y H:i:s O", mktime()),
diff --git a/program/include/session.inc b/program/include/session.inc
index 00ca299..dc77147 100644
--- a/program/include/session.inc
+++ b/program/include/session.inc
@@ -45,7 +45,7 @@
 
   if ($sql_arr = $DB->fetch_assoc($sql_result))
     {
-    $SESS_CHANGED = mktime(); //$sql_arr['changed'];
+    $SESS_CHANGED = $sql_arr['changed'];
 
     if (strlen($sql_arr['vars']))
       return $sql_arr['vars'];
diff --git a/program/js/app.js b/program/js/app.js
index 1b7312f..8d2f1ed 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -49,9 +49,9 @@
                              'application/x-shockwave-flash');
 
   // default environment vars
-  this.env.keep_alive = 60;        // seconds
+  this.env.keep_alive = 50;        // seconds
   this.env.request_timeout = 180;  // seconds
-  this.env.draft_autosave = 300;   // seconds
+  this.env.draft_autosave = 0;     // seconds
 
 
   // set environment variable
@@ -148,7 +148,10 @@
           {
           this.enable_command('add-attachment', 'send-attachment', 'remove-attachment', 'send', true);
           if (this.env.spellcheck)
-            this.enable_command('spellcheck', true);
+            {
+            this.env.spellcheck.spelling_state_observer = function(s){ rcube_webmail_client.set_spellcheck_state(s); };
+            this.set_spellcheck_state('ready');
+            }
           if (this.env.drafts_mailbox)
             this.enable_command('savedraft', true);
           }
@@ -930,8 +933,11 @@
         break;
         
       case 'spellcheck':
-        if (this.env.spellcheck && this.env.spellcheck.spellCheck)
+        if (this.env.spellcheck && this.env.spellcheck.spellCheck && this.spellcheck_ready)
+          {
           this.env.spellcheck.spellCheck(this.env.spellcheck.check_link);
+          this.set_spellcheck_state('checking');
+          }
         break;
 
       case 'savedraft':
@@ -942,7 +948,8 @@
           break;
 
         // if saving Drafts is disabled in main.inc.php
-        if (!this.env.drafts_mailbox)
+        // or if compose form did not change
+        if (!this.env.drafts_mailbox || this.cmp_hash == this.compose_field_hash())
           break;
 
         this.set_busy(true, 'savingmessage');
@@ -2025,6 +2032,13 @@
     };
 
 
+  this.set_spellcheck_state = function(s)
+    {
+	this.spellcheck_ready = (s=='check_spelling' || s=='ready');
+    this.enable_command('spellcheck', this.spellcheck_ready);
+	};
+
+
   this.auto_save_start = function()
     {
     if (this.env.draft_autosave)
@@ -2783,7 +2797,7 @@
   this.reset_folder_rename = function()
     {
     var cell = this.name_input ? this.name_input.parentNode : null;
-    if (cell && this.edit_folder)
+    if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
       cell.innerHTML = this.env.subscriptionrows[this.edit_folder][1];
       
     this.edit_folder = null;
@@ -3568,7 +3582,7 @@
 
       case 'expunge':
         this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
-        break;      
+        break;
       }
 
     request_obj.reset();
@@ -3886,6 +3900,12 @@
   }  // end class rcube_http_request
 
 
+// helper function to call the init method with a delay
+function call_init(o)
+  {
+  if (window[o] && window[o].init)
+    setTimeout(o+'.init()', 200);
+  }
 
 function console(str)
   {
@@ -3893,10 +3913,3 @@
     document.debugform.console.value += str+'\n--------------------------------------\n';
   }
 
-
-// set onload handler
-window.onload = function(e)
-  {
-  if (window.rcube_webmail_client)
-    rcube_webmail_client.init();
-  };
diff --git a/program/steps/error.inc b/program/steps/error.inc
index 2d87a9d..f616ba1 100644
--- a/program/steps/error.inc
+++ b/program/steps/error.inc
@@ -69,11 +69,11 @@
   {
   $__error_title = "SERVICE CURRENTLY NOT AVAILABLE!";
   $__error_text  = "Please contact your server-administrator.";
-  
+
   if (($CONFIG['debug_level'] & 4) && $ERROR_MESSAGE)
     $__error_text = $ERROR_MESSAGE;
   else
-    $__error_text = 'Error No. '.dechex($ERROR_CODE).')';
+    $__error_text = 'Error No. ['.dechex($ERROR_CODE).']';
   }
 
 
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 0cd6652..827f131 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -1245,9 +1245,9 @@
   {
   global $IMAP;
 
-  if (!is_array($message_struct['parts']))
-    return FALSE;
-    
+  if (empty($message_struct['parts']))
+    return $message_struct['UID'] ? $IMAP->get_body($message_struct['UID']) : false;
+
   // check all message parts
   foreach ($message_struct['parts'] as $pid => $part)
     {
diff --git a/program/steps/settings/manage_folders.inc b/program/steps/settings/manage_folders.inc
index af0c829..e938908 100644
--- a/program/steps/settings/manage_folders.inc
+++ b/program/steps/settings/manage_folders.inc
@@ -73,10 +73,12 @@
     
   if ($rename && $REMOTE_REQUEST)
     {
-    $commands = sprintf("this.replace_folder_row('%s','%s','%s');",
+    $commands = sprintf("this.replace_folder_row('%s','%s','%s');\n",
                         rep_specialchars_output(get_input_value('_folder_oldname', RCUBE_INPUT_GET), 'js'),
                         rep_specialchars_output($rename, 'js'),
                         rep_specialchars_output(rcube_charset_convert($rename, 'UTF-7'), 'js'));
+
+    $commands .= "this.reset_folder_rename();\n";
                         
     rcube_remote_response($commands);
     }

--
Gitblit v1.9.1