Add IMP::getCacheOb().
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 20 Nov 2008 22:17:35 +0000 (15:17 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 20 Nov 2008 22:17:35 +0000 (15:17 -0700)
imp/lib/Crypt/pgp.php
imp/lib/IMP.php
imp/lib/Template.php

index ae7ffeb..2ca2600 100644 (file)
@@ -162,24 +162,16 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp
      */
     public function getPublicKey($address, $fingerprint = null, $server = true)
     {
-        $result = false;
-
         /* If there is a cache driver configured, try to get the public key
          * from the cache. */
-        if (!empty($GLOBALS['conf']['cache']['driver'])) {
-            $cache = &Horde_Cache::singleton($GLOBALS['conf']['cache']['driver'], Horde::getDriverConfig('cache', $GLOBALS['conf']['cache']['driver']));
-            if (is_a($cache, 'PEAR_Error')) {
-                Horde::fatal($cache, __FILE__, __LINE__);
-            } else {
-                $result = $cache->get("PGPpublicKey_" . $address . $fingerprint, 3600);
+        if (($cache = &IMP::getCacheOb())) {
+            $result = $cache->get("PGPpublicKey_" . $address . $fingerprint, 3600);
+            if ($result) {
+                Horde::logMessage('PGPpublicKey: ' . serialize($result), __FILE__, __LINE__, PEAR_LOG_DEBUG);
+                return $result;
             }
         }
 
-        if ($result) {
-            Horde::logMessage('PGPpublicKey: ' . serialize($result), __FILE__, __LINE__, PEAR_LOG_DEBUG);
-            return $result;
-        }
-
         /* Try retrieving by e-mail only first. */
         $params = IMP_Compose::getAddressSearchParams();
         $result = $GLOBALS['registry']->call('contacts/getField', array($address, self::PUBKEY_FIELD, $params['sources'], false, true));
index 4a6a25e..b8bfc33 100644 (file)
@@ -1467,8 +1467,8 @@ class IMP
 
         if (empty($cache_type) ||
             $cache_type == 'none' ||
-            ($cache_type == 'horde_cache' &&
-             $conf['cache']['driver'] == 'none')) {
+            (($cache_type == 'horde_cache') &&
+             !($cache = &self::getCacheOb()))) {
             Horde::includeScriptFiles();
             return;
         }
@@ -1511,7 +1511,6 @@ class IMP
             break;
 
         case 'horde_cache':
-            $cache = &Horde_Cache::singleton($conf['cache']['driver'], Horde::getDriverConfig('cache', $conf['cache']['driver']));
             $exists = $cache->exists($sig, empty($conf['server']['cachejsparams']['lifetime']) ? 0 : $conf['server']['cachejsparams']['lifetime']);
             $js_url = IMP::getCacheURL('js', $sig);
             break;
@@ -1622,8 +1621,8 @@ class IMP
 
         if (empty($cache_type) ||
             $cache_type == 'none' ||
-            ($cache_type == 'horde_cache' &&
-             $conf['cache']['driver'] == 'none')) {
+            (($cache_type == 'horde_cache') &&
+             !($cache = &self::getCacheOb()))) {
             $css_out = array_merge($css, $css_out);
         } else {
             $mtime = array(0);
@@ -1645,7 +1644,6 @@ class IMP
                 break;
 
             case 'horde_cache':
-                $cache = &Horde_Cache::singleton($GLOBALS['conf']['cache']['driver'], Horde::getDriverConfig('cache', $GLOBALS['conf']['cache']['driver']));
                 $exists = $cache->exists($sig, empty($GLOBALS['conf']['server']['cachecssparams']['lifetime']) ? 0 : $GLOBALS['conf']['server']['cachecssparams']['lifetime']);
                 $css_url = IMP::getCacheURL('css', $sig);
                 break;
@@ -1818,4 +1816,26 @@ class IMP
 
         return $msglist;
     }
+
+    /**
+     * Returns a Horde_Cache object (if configured) and handles any errors
+     * associated with creating the object.
+     *
+     * @return Horde_Cache  A pointer to a Horde_Cache object.
+     */
+    static public function &getCacheOb()
+    {
+        global $conf;
+
+        if ($conf['cache']['driver'] == 'none') {
+            return false;
+        }
+
+        $cache = &Horde_Cache::singleton($conf['cache']['driver'], Horde::getDriverConfig('cache', $conf['cache']['driver']));
+        if (is_a($cache, 'PEAR_Error')) {
+            Horde::fatal($cache, __FILE__, __LINE__);
+        }
+
+        return $cache;
+    }
 }
index f47ce7d..b702212 100644 (file)
@@ -109,7 +109,8 @@ class IMP_Template {
             $this->_basepath = $basepath;
         }
 
-        $this->_cache = &Horde_Cache::singleton($GLOBALS['conf']['cache']['driver'], Horde::getDriverConfig('cache', $GLOBALS['conf']['cache']['driver']));
+
+        $this->_cache = &IMP::getCacheOb();
     }
 
     /**
@@ -211,7 +212,7 @@ class IMP_Template {
         /* First, check for a cached compiled version. */
         $cacheid = 'horde_template|' . filemtime($file) . '|' . $file . '|'
             . $this->getOption('gettext');
-        if (!$force && is_null($this->_template) && isset($this->_cache)) {
+        if (!$force && is_null($this->_template) && $this->_cache) {
             $this->_template = $this->_cache->get($cacheid, 0);
             if ($this->_template === false) {
                 $this->_template = null;