From ff52bee1a2c1b3f6654c8bfe41d1a3d8630e2b55 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Thu, 01 Mar 2007 15:40:00 -0500
Subject: [PATCH] Solved wrong caching of message preview (#1484153, #1484236)

---
 CHANGELOG                        |    6 +++
 index.php                        |    8 ++--
 program/include/rcube_shared.inc |   37 +++++++++++++++++-
 program/include/main.inc         |    8 ---
 program/steps/mail/show.inc      |   21 ++++++----
 5 files changed, 57 insertions(+), 23 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index af980f4..da194bb 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,12 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2007/03/01 (thomasb)
+----------
+- Solved page caching of message preview (closes #1484153)
+- Only use gzip compression if configured (closes #1484236)
+
+
 2007/02/25 (estadtherr)
 ----------
 - Fixed priority selector issue (#1484150)
diff --git a/index.php b/index.php
index 52d6ddc..458a113 100644
--- a/index.php
+++ b/index.php
@@ -2,9 +2,9 @@
 /*
  +-----------------------------------------------------------------------+
  | RoundCube Webmail IMAP Client                                         |
- | Version 0.1-20061206                                                  |
+ | Version 0.1-20070301                                                  |
  |                                                                       |
- | Copyright (C) 2005-2006, RoundCube Dev. - Switzerland                 |
+ | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | Redistribution and use in source and binary forms, with or without    |
@@ -40,7 +40,7 @@
 
 */
 
-define('RCMAIL_VERSION', '0.1-20061206');
+define('RCMAIL_VERSION', '0.1-20070301');
 
 // define global vars
 $CHARSET = 'UTF-8';
@@ -106,7 +106,7 @@
 if ($_action != 'get' && $_action != 'viewsource')
   {
   // use gzip compression if supported
-  if (function_exists('ob_gzhandler') && !ini_get('zlib.output_compression'))
+  if (function_exists('ob_gzhandler') && ini_get('zlib.output_compression'))
     ob_start('ob_gzhandler');
   else
     ob_start();
diff --git a/program/include/main.inc b/program/include/main.inc
index 7e7f216..b19c7f4 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -190,13 +190,6 @@
     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;
   }
 
@@ -559,6 +552,7 @@
     $_SESSION['username']  = $user;
     $_SESSION['user_lang'] = $sess_user_lang;
     $_SESSION['password']  = encrypt_passwd($pass);
+    $_SESSION['login_time'] = mktime();
 
     // force reloading complete list of subscribed mailboxes
     rcmail_set_imap_prop();
diff --git a/program/include/rcube_shared.inc b/program/include/rcube_shared.inc
index ba63c82..558fbf7 100644
--- a/program/include/rcube_shared.inc
+++ b/program/include/rcube_shared.inc
@@ -1216,17 +1216,48 @@
 
 
 // send header with expire date 30 days in future
-function send_future_expire_header()
+function send_future_expire_header($offset=2600000)
   {
   if (headers_sent())
     return;
 
-  header("Expires: ".gmdate("D, d M Y H:i:s", mktime()+2600000)." GMT");
-  header("Cache-Control: ");
+  header("Expires: ".gmdate("D, d M Y H:i:s", mktime()+$offset)." GMT");
+  header("Cache-Control: max-age=$offset");
   header("Pragma: ");
   }
 
 
+// check request for If-Modified-Since and send an according response
+function send_modified_header($mdate, $etag=null)
+{
+  if (headers_sent())
+    return;
+    
+  $iscached = false;
+  if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mdate)
+    $iscached = true;
+  
+  $etag = $etag ? "\"$etag\"" : null;
+  if ($etag && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
+    $iscached = true;
+  
+  if ($iscached)
+    header("HTTP/1.x 304 Not Modified");
+  else
+    header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mdate)." GMT");
+  
+  header("Cache-Control: max-age=0");
+  header("Expires: ");
+  header("Pragma: ");
+  
+  if ($etag)
+    header("Etag: $etag");
+  
+  if ($iscached)
+    exit;
+}
+
+
 // function to convert an array to a javascript array
 function array2js($arr, $type='')
   {
diff --git a/program/steps/mail/show.inc b/program/steps/mail/show.inc
index 841a41b..19e6e56 100644
--- a/program/steps/mail/show.inc
+++ b/program/steps/mail/show.inc
@@ -23,21 +23,14 @@
 
 $PRINT_MODE = $_action=='print' ? TRUE : FALSE;
 
-// allow caching, unless remote images are present
-if ((bool)get_input_value('_safe', RCUBE_INPUT_GET))
-  send_nocacheing_headers();
-else
-  send_future_expire_header();
-
 // similar code as in program/steps/mail/get.inc
 if ($_GET['_uid'])
   {
   $MESSAGE = array('UID' => get_input_value('_uid', RCUBE_INPUT_GET));
   $MESSAGE['headers'] = $IMAP->get_headers($MESSAGE['UID']);
-  $MESSAGE['structure'] = $IMAP->get_structure($MESSAGE['UID']);
   
   // go back to list if message not found (wrong UID)
-  if (!$MESSAGE['headers'] || !$MESSAGE['structure'])
+  if (!$MESSAGE['headers'])
     {
     show_message('messageopenerror', 'error');
     if ($_action=='preview' && template_exists('messagepreview'))
@@ -49,9 +42,19 @@
       }
     }
 
+  // calculate Etag for this request
+  $etag = md5($MESSAGE['UID'].$IMAP->get_mailbox_name().session_id().($PRINT_MODE?1:0));
+
+  // allow caching, unless remote images are present
+  if ((bool)get_input_value('_safe', RCUBE_INPUT_GET))
+    send_nocacheing_headers();
+  else
+    send_modified_header($_SESSION['login_time'], $etag);
+
+
   $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
   
-  if ($MESSAGE['structure'])
+  if ($MESSAGE['structure'] = $IMAP->get_structure($MESSAGE['UID']))
     list($MESSAGE['parts'], $MESSAGE['attachments']) = rcmail_parse_message(
       $MESSAGE['structure'],
       array('safe' => (bool)$_GET['_safe'],

--
Gitblit v1.9.1