From ed1d212ae2daea5e4bd043417610177093e99f19 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 16 Jan 2016 03:03:51 -0500
Subject: [PATCH] Improved SVG cleanup code

---
 program/lib/Roundcube/rcube_cache.php |  104 +++++++++++++++++++++++----------------------------
 1 files changed, 47 insertions(+), 57 deletions(-)

diff --git a/program/lib/Roundcube/rcube_cache.php b/program/lib/Roundcube/rcube_cache.php
index 649dd93..fb1d5e1 100644
--- a/program/lib/Roundcube/rcube_cache.php
+++ b/program/lib/Roundcube/rcube_cache.php
@@ -1,6 +1,6 @@
 <?php
 
-/*
+/**
  +-----------------------------------------------------------------------+
  | This file is part of the Roundcube Webmail client                     |
  | Copyright (C) 2011, The Roundcube Dev Team                            |
@@ -17,7 +17,6 @@
  | Author: Aleksander Machniak <alec@alec.pl>                            |
  +-----------------------------------------------------------------------+
 */
-
 
 /**
  * Interface class for accessing Roundcube cache
@@ -92,7 +91,6 @@
         $this->prefix    = $prefix;
     }
 
-
     /**
      * Returns cached value.
      *
@@ -109,7 +107,6 @@
         return $this->cache[$key];
     }
 
-
     /**
      * Sets (add/update) value in cache.
      *
@@ -121,7 +118,6 @@
         $this->cache[$key]         = $data;
         $this->cache_changes[$key] = true;
     }
-
 
     /**
      * Returns cached value without storing it in internal memory.
@@ -139,7 +135,6 @@
         return $this->read_record($key, true);
     }
 
-
     /**
      * Sets (add/update) value in cache and immediately saves
      * it in the backend, no internal memory will be used.
@@ -153,7 +148,6 @@
     {
         return $this->write_record($key, $this->serialize($data));
     }
-
 
     /**
      * Clears the cache.
@@ -191,7 +185,6 @@
         $this->remove_record($key, $prefix_mode);
     }
 
-
     /**
      * Remove cache records older than ttl
      */
@@ -208,7 +201,6 @@
         }
     }
 
-
     /**
      * Remove expired records of all caches
      */
@@ -219,7 +211,6 @@
 
         $db->query("DELETE FROM " . $db->table_name('cache', true) . " WHERE `expires` < " . $db->now());
     }
-
 
     /**
      * Writes the cache back to the DB.
@@ -242,8 +233,10 @@
         if ($this->index_changed) {
             $this->write_index();
         }
-    }
 
+        // reset internal cache index, thanks to this we can force index reload
+        $this->index = null;
+    }
 
     /**
      * Reads cache entry.
@@ -330,7 +323,6 @@
         return $this->cache[$key];
     }
 
-
     /**
      * Writes single cache record into DB.
      *
@@ -355,8 +347,16 @@
             $result = $this->add_record($this->ckey($key), $data);
 
             // make sure index will be updated
-            if ($result && !array_key_exists($key, $this->cache_sums)) {
-                $this->cache_sums[$key] = true;
+            if ($result) {
+                if (!array_key_exists($key, $this->cache_sums)) {
+                    $this->cache_sums[$key] = true;
+                }
+
+                $this->load_index();
+
+                if (!$this->index_changed && !in_array($key, $this->index)) {
+                    $this->index_changed = true;
+                }
             }
 
             return $result;
@@ -400,7 +400,6 @@
         return $this->db->affected_rows($result);
     }
 
-
     /**
      * Deletes the cache record(s).
      *
@@ -420,22 +419,29 @@
             // Remove all keys
             if ($key === null) {
                 foreach ($this->index as $key) {
-                    $this->delete_record($key, false);
+                    $this->delete_record($this->ckey($key));
                 }
+
                 $this->index = array();
             }
             // Remove keys by name prefix
             else if ($prefix_mode) {
-                foreach ($this->index as $k) {
+                foreach ($this->index as $idx => $k) {
                     if (strpos($k, $key) === 0) {
-                        $this->delete_record($k);
+                        $this->delete_record($this->ckey($k));
+                        unset($this->index[$idx]);
                     }
                 }
             }
             // Remove one key by name
             else {
-                $this->delete_record($key);
+                $this->delete_record($this->ckey($key));
+                if (($idx = array_search($key, $this->index)) !== false) {
+                    unset($this->index[$idx]);
+                }
             }
+
+            $this->index_changed = true;
 
             return;
         }
@@ -457,7 +463,6 @@
             "DELETE FROM {$this->table} WHERE `user_id` = ?" . $where,
             $this->userid);
     }
-
 
     /**
      * Adds entry into memcache/apc DB.
@@ -488,44 +493,32 @@
             $this->debug('set', $key, $data, $result);
         }
 
-        if ($result) {
-            $this->index_changed = true;
+        return $result;
+    }
+
+    /**
+     * Deletes entry from memcache/apc DB.
+     *
+     * @param string $key Cache key name
+     *
+     * @param boolean True on success, False on failure
+     */
+    private function delete_record($key)
+    {
+        if ($this->type == 'memcache') {
+            // #1488592: use 2nd argument
+            $result = $this->db->delete($key, 0);
+        }
+        else {
+            $result = apc_delete($key);
+        }
+
+        if ($this->debug) {
+            $this->debug('delete', $key, null, $result);
         }
 
         return $result;
     }
-
-
-    /**
-     * Deletes entry from memcache/apc DB.
-     */
-    private function delete_record($key, $index=true)
-    {
-        $ckey = $this->ckey($key);
-
-        if ($this->type == 'memcache') {
-            // #1488592: use 2nd argument
-            $result = $this->db->delete($ckey, 0);
-        }
-        else {
-            $result = apc_delete($ckey);
-        }
-
-        if ($this->debug) {
-            $this->debug('delete', $ckey, null, $result);
-        }
-
-        if ($result) {
-            $this->index_changed = true;
-        }
-
-        if ($index) {
-            if (($idx = array_search($key, $this->index)) !== false) {
-                unset($this->index[$idx]);
-            }
-        }
-    }
-
 
     /**
      * Writes the index entry into memcache/apc DB.
@@ -556,7 +549,6 @@
         $this->add_record($this->ikey(), $data);
     }
 
-
     /**
      * Gets the index entry from memcache/apc DB.
      */
@@ -586,7 +578,6 @@
         $this->index = $data ? unserialize($data) : array();
     }
 
-
     /**
      * Creates per-user cache key name (for memcache and apc)
      *
@@ -598,7 +589,6 @@
     {
         return sprintf('%d:%s:%s', $this->userid, $this->prefix, $key);
     }
-
 
     /**
      * Creates per-user index cache key name (for memcache and apc)

--
Gitblit v1.9.1