From edb52522ec6caccd4e0e8729abebda30a460ff0f Mon Sep 17 00:00:00 2001
From: theduke <chris@theduke.at>
Date: Thu, 18 Apr 2013 00:17:30 +0200
Subject: [PATCH] Implemented cookie inclusions.

---
 boost.admin.htaccess.inc |   22 ++++++++++++++++++----
 boost.admin.inc          |   12 ++++++++++++
 boost.module             |   24 +++++++++++++++++++++---
 3 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/boost.admin.htaccess.inc b/boost.admin.htaccess.inc
index 3bebe36..fb6c308 100644
--- a/boost.admin.htaccess.inc
+++ b/boost.admin.htaccess.inc
@@ -204,12 +204,12 @@ function boost_admin_htaccess_generate_htaccess() {
   foreach ($enabled_file_extensions as $extension => $values) {
     $type = $values['content_type'];
     if ($values['gzip']) {
-      $output['gzip'] .= "  RewriteCond $document_root$base_path$cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension\.gz -s\n";
-      $output['gzip'] .= "  RewriteRule .* $cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension\.gz [L,T=$type,E=no-gzip:1]\n";
+      $output['gzip'] .= "  RewriteCond $document_root$base_path$cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}%{ENV:boost_cookie_inclusions}\.$extension\.gz -s\n";
+      $output['gzip'] .= "  RewriteRule .* $cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}%{ENV:boost_cookie_inclusions}\.$extension\.gz [L,T=$type,E=no-gzip:1]\n";
       $gzip_count++;
     }
-    $output['normal'] .= "  RewriteCond $document_root$base_path$cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension -s\n";
-    $output['normal'] .= "  RewriteRule .* $cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension [L,T=$type]\n";
+    $output['normal'] .= "  RewriteCond $document_root$base_path$cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}%{ENV:boost_cookie_inclusions}\.$extension -s\n";
+    $output['normal'] .= "  RewriteRule .* $cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}%{ENV:boost_cookie_inclusions}\.$extension [L,T=$type]\n";
     $normal_count++;
   }
   $skip = !empty($gzip_count) ? $normal_count + $gzip_count + 1 : $normal_count;
@@ -222,6 +222,20 @@ function boost_admin_htaccess_generate_htaccess() {
     $string .= "  # Allow for alt paths to be set via htaccess rules; allows for cached variants (future mobile support)\n";
     $string .= "  RewriteRule .* - [E=boostpath:$normal_dir]\n";
     $string .= "\n";
+
+    // Handle cookie inclusions.
+    $string .= "  # Handle cookie inclusions\n\n";
+
+    $string .= "  RewriteRule .* - [E=cookie_inclusions:]\n\n";
+
+    $cookie_inclusions = variable_get('boost_cookie_inclusions', BOOST_COOKIE_INCLUSIONS);
+    $cookie_inclusions = explode('\n', $cookie_inclusions);
+
+    foreach ($cookie_inclusions as $cookie) {
+      $string .= "  RewriteCond %{HTTP_COOKIE} $cookie=([^;]+) [NC]\n";
+      $string .= "  RewriteRule .* - [E=boost_cookie_inclusions:%{ENV:boost_cookie_inclusions}_$cookie=%1]\n\n";
+    }
+
     $string .= "  # Caching for anonymous users\n";
     $string .= "  # Skip boost IF not get request OR uri has wrong dir OR cookie is set OR request came from this server" . (variable_get('boost_ssl_bypass', BOOST_SSL_BYPASS) ? " OR https request" : "") . "\n";
     $string .= "  RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [OR]\n";
diff --git a/boost.admin.inc b/boost.admin.inc
index 9001208..de31de6 100644
--- a/boost.admin.inc
+++ b/boost.admin.inc
@@ -46,6 +46,18 @@ function boost_admin_settings() {
     '#description' => $description,
   );
 
+  $form['cookies'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Cookie inclusions'),
+  );
+
+  $form['cookies']['boost_cookie_inclusions'] = array(
+    '#type' => 'textarea',
+    '#title' => '<span class="element-invisible">' . t('Cookies to respect in cache generation') . '</span>',
+    '#description' => t('If your page can look or behave differently when certain cookies are set, boost needs to know about them.') . '<br />' . t('Enter the name of all cookies that should be respected, one cookie per line.') . '<br /><br />' . t('Note that <b>you need to re-generate your htaccess file after changing this setting</b>.'),
+    '#default_value' => variable_get('boost_cookie_inclusions', BOOST_COOKIE_INCLUSIONS),
+  );
+
 
   $types = boost_get_storage_types();
   $period = drupal_map_assoc(array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 64800, 86400, 2*86400, 3*86400, 4*86400, 5*86400, 6*86400, 604800, 2*604800, 3*604800, 4*604800, 8*604800, 16*604800, 52*604800), 'format_interval');
diff --git a/boost.module b/boost.module
index d4825ad..48c4b7e 100644
--- a/boost.module
+++ b/boost.module
@@ -12,6 +12,11 @@
 define('BOOST_CACHEABILITY_PAGES', '');
 
 /**
+ * Default cacheablily setting.
+ */
+define('BOOST_COOKIE_INCLUSIONS', '');
+
+/**
  * Whether the host supports gzip
  */
 define('BOOST_GZIP', function_exists('gzencode'));
@@ -188,7 +193,7 @@ function boost_menu() {
 function boost_permission() {
   return array(
     'boost flush pages' => array(
-      'title' => t('Flush pages from Boost cache'), 
+      'title' => t('Flush pages from Boost cache'),
       'description' => t('Allow users to flush individual pages from the Boost cache using the Boost status block.'),
     ),
   );
@@ -199,7 +204,7 @@ function boost_permission() {
  */
 function boost_block_info() {
   $blocks['status'] = array(
-    'info' => t('Boost: Pages cache status'), 
+    'info' => t('Boost: Pages cache status'),
     'cache' => DRUPAL_NO_CACHE,
   );
 
@@ -439,8 +444,21 @@ function boost_transform_url($url = NULL, $b_path = NULL) {
       $items[$hash] = array('cache_this' => FALSE);
       return $items[$hash];
     }
+    $boost_char = variable_get('boost_char', BOOST_CHAR);
     $parts['base_dir'] = boost_get_normal_cache_dir() . '/' . $parts['host'] . $b_path;
-    $parts['filename'] = $parts['base_dir'] . $parts['full_path'] . variable_get('boost_char', BOOST_CHAR) . $parts['query'];
+    $parts['filename'] = $parts['base_dir'] . $parts['full_path'] . $boost_char . $parts['query'];
+
+    // Handle cookie inclusions.
+
+    $cookie_inclusions = variable_get('boost_cookie_inclusions', BOOST_COOKIE_INCLUSIONS);
+    $cookie_inclusions = explode('\n', $cookie_inclusions);
+
+    foreach ($cookie_inclusions as $cookie) {
+      if (isset($_COOKIE[$cookie])) {
+        $parts['filename'] .= $boost_char . $cookie . '=' . $_COOKIE[$cookie];
+      }
+    }
+
     $parts['directory'] = dirname($parts['filename']);
 
     // Get the internal path (node/8).
-- 
1.7.2.5

