File: Cache.php

package info (click to toggle)
imp4 4.2-4lenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,252 kB
  • ctags: 5,316
  • sloc: php: 21,340; xml: 19,302; makefile: 68; sql: 14
file content (343 lines) | stat: -rw-r--r-- 10,898 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php

require_once 'Horde/IMAP/Cache.php';

/**
 * The IMP_IMAP_Cache:: class extends Horde's IMAP_Cache:: class to add extra
 * IMP-specific functionality.
 *
 * $Horde: imp/lib/IMAP/Cache.php,v 1.36.2.2 2008/01/02 11:31:29 jan Exp $
 *
 * Copyright 2006-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @author  Michael Slusarz <slusarz@horde.org>
 * @since   IMP 4.2
 * @package Horde_IMAP
 */
class IMP_IMAP_Cache extends IMAP_Cache {

    /**
     * The cached results of imap_status() calls.
     *
     * @var array
     */
    var $_statuscache = array();

    /**
     * Cached results for the imap_search() call to determine arrival time.
     *
     * @var $_arrival
     */
    var $_arrival = array();

    /**
     * Has the shutdown function been registered?
     *
     * @var mixed
     */
    var $_tosave = false;

    /**
     * Use Horde_Cache?
     *
     * @var boolean
     */
    var $_usecache = false;

    /**
     * Returns a reference to the global IMP_IMAP_Cache object, only creating
     * it if it doesn't already exist.
     *
     * This method must be invoked as:
     *   $imap_cache = &IMP_IMAP_Cache::singleton();
     *
     * @return IMP_IMAP_Cache  The global IMP_IMAP_Cache instance.
     */
    function &singleton()
    {
        static $object;

        if (!isset($object)) {
            $object = new IMP_IMAP_Cache();
        }

        return $object;
    }

    /**
     * Constructor
     */
    function IMP_IMAP_Cache()
    {
        $this->_usecache = !empty($GLOBALS['conf']['mlistcache']['use_mlistcache']) &&
                           ($GLOBALS['conf']['cache']['driver'] != 'none');

        parent::IMAP_Cache();
    }

    /**
     * Get data from the cache.
     *
     * @param resource $imap   The IMAP resource stream (not needed).
     * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
     * @param string $key      The name of a specific entry to return.
     * @param boolean $check   Check for updated mailbox?
     *
     * @return mixed  The data requested, or false if not available.
     */
    function getCache($imap, $mailbox, $key = null, $check = true)
    {
        $imp_imap = &IMP_IMAP::singleton();
        $res = parent::getCache($imp_imap->stream(), $mailbox, $key, $check);
        if ($this->_usecache &&
            ($res === false) &&
            !isset($this->_cache[$mailbox])) {
            require_once 'Horde/Cache.php';
            $cache = &Horde_Cache::singleton($GLOBALS['conf']['cache']['driver'], Horde::getDriverConfig('cache', $GLOBALS['conf']['cache']['driver']));
            $res = $cache->get($this->_getHordeCacheID($mailbox), $GLOBALS['conf']['mlistcache']['lifetime']);
            if ($res === false) {
                $this->_cache[$mailbox] = array('d' => array(), 'k' => null);
            } else {
                require_once 'Horde/Serialize.php';
                $this->_cache[$mailbox] = Horde_Serialize::unserialize($res, SERIALIZE_BASIC);
                $res = parent::getCache($imp_imap->stream(), $mailbox, $key, $check);
                Horde::logMessage('Retrieved ' . $mailbox . ' from cache.', __FILE__, __LINE__, PEAR_LOG_DEBUG);
            }
        }
        return $res;
    }

    /**
     * Store data in the cache.
     *
     * @param resource $imap   The IMAP resource stream (not needed).
     * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
     * @param array $values    The data to add to the cache.
     */
    function storeCache($imap, $mailbox, $values = array())
    {
        $this->_cache[$mailbox] = array(
            'k' => $this->_getCacheID($imap, $mailbox),
            'd' => $values
        );
        $this->_saveCache($mailbox);
    }

    /**
     * Store data in the cache, preserving any data already in the cache
     * entry and not altering the current cache key.
     *
     * @param resource $imap   The IMAP resource stream (not needed).
     * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
     * @param array $values    The data to add to the cache.
     */
    function updateCache($imap, $mailbox, $values = array())
    {
        if (isset($this->_cache[$mailbox])) {
            $ptr = &$this->_cache[$mailbox];
            $ptr['d'] = array_merge($ptr['d'], $values);
            $this->_saveCache($mailbox);
        } else {
            $this->storeCache($imap, $mailbox, $values);
        }
    }

    /**
     * Flag cached entries as expired.
     *
     * @param string $mailbox  A mailbox name.
     * @param integer $mask    A bitmask for the following updates:
     * <pre>
     * 1 = Expire cache entries
     * 2 = Expire imap_status() entries
     * 4 = Expire getMailboxArrival() entries
     * </pre>
     */
    function expireCache($mailbox, $mask = 0)
    {
        $full_mailbox = IMP::serverString($mailbox);

        if ($mask & 1) {
            unset($this->_cache[$full_mailbox]);
            $this->_saveCache($full_mailbox);
        }

        if ($mask & 2) {
            unset($this->_statuscache[$full_mailbox]);
        }

        if ($mask & 4) {
            unset($this->_arrival[$mailbox]);
        }
    }

    /**
     * Returns and caches the results of an imap_status() call.
     *
     * @param resource $imap   The IMAP resource stream (not needed).
     * @param string $mailbox  A mailbox name.
     *
     * @return stdClass  The imap_status() object or the empty string.
     */
    function getStatus($imap, $mailbox)
    {
        $mailbox = IMP::serverString($mailbox);
        if (!isset($this->_statuscache[$mailbox]) &&
            !$GLOBALS['imp_search']->isSearchMbox(substr($mailbox, strpos($mailbox, '}') + 1))) {
            $imp_imap = &IMP_IMAP::singleton();
            $this->_statuscache[$mailbox] = @imap_status($imp_imap->stream(), $mailbox, SA_ALL);
            if (!$this->_statuscache[$mailbox]) {
                unset($this->_statuscache[$mailbox]);
                if ($err = imap_last_error()) {
                    Horde::logMessage($err, __FILE__, __LINE__, PEAR_LOG_NOTICE);
                }
            }
        }

        return empty($this->_statuscache[$mailbox]) ? '' : $this->_statuscache[$mailbox];
    }

    /**
     * Generate the unique ID string for the mailbox.
     *
     * @access private
     *
     * @param resource $imap   The IMAP resource stream (not needed).
     * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
     *
     * @return string  A unique string for the current state of the mailbox.
     */
    function _getCacheID($imap, $mailbox)
    {
        $this->getStatus($imap, $mailbox);
        if (!empty($this->_statuscache[$mailbox])) {
            $ob = $this->_statuscache[$mailbox];
            $sortpref = IMP::getSort(substr($mailbox, strpos($mailbox, '}') + 1));
            return implode('|', array($ob->messages, $ob->uidnext, $ob->uidvalidity, $sortpref['by'], $sortpref['dir']));
        } else {
            return $mailbox;
        }
    }

    /**
     * Returns the list of message UIDs in arrival order.
     *
     * @param string $mailbox   The mailbox to query.
     * @param boolean $delhide  Use this value instead of the value from
     *                          IMP::hideDeleteMsgs().
     * @param string $base      The base IMAP search string - defaults to all.
     *
     * @return array  See imap_search().
     */
    function getMailboxArrival($mailbox, $delhide = null, $base = 'ALL')
    {
        $search = array(strtoupper(trim($base)));

        if ($delhide === null) {
            $delhide = IMP::hideDeletedMsgs();
        }

        if ($delhide) {
            $search[] = 'UNDELETED';
        }

        $cache_id = serialize($search);

        if (!isset($this->_arrival[$mailbox][$cache_id])) {
            $imp_imap = &IMP_IMAP::singleton();
            if ($imp_imap->changeMbox($mailbox, IMP_IMAP_AUTO)) {
                $res = @imap_search($imp_imap->stream(), implode(' ', $search), SE_UID);
                if (!isset($this->_arrival[$mailbox])) {
                    $this->_arrival[$mailbox] = array();
                }
            } else {
                $res = array();
            }
            $this->_arrival[$mailbox][$cache_id] = (empty($res)) ? array() : $res;
        }

        return $this->_arrival[$mailbox][$cache_id];
    }

    /**
     * Get the unique mailbox ID for the current mailbox status. Needed
     * because some applications (such as DIMP) may be keeping more than 1
     * copy of IMAP data (i.e. on the browser, on the server).
     *
     * @since IMP 4.2
     *
     * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
     *
     * @return string  A unique string for the current state of the mailbox.
     */
    function getCacheID($mailbox)
    {
        return $this->_getCacheID(null, $mailbox);
    }

    /**
     * Register the shutdown save function.
     *
     * @access private
     *
     * @var string $mailbox  The mailbox item to save.
     */
    function _saveCache($mailbox)
    {
        if ($this->_tosave === false) {
            register_shutdown_function(array(&$this, '_shutdown'));
            $this->_tosave = array();
        }
        $this->_tosave[$mailbox] = true;
    }

    /**
     * Shutdown function
     *
     * @access private
     */
    function _shutdown()
    {
        if (!$this->_usecache) {
            return;
        }

        $in_session = array();

        require_once 'Horde/Cache.php';
        require_once 'Horde/Serialize.php';
        $cache = &Horde_Cache::singleton($GLOBALS['conf']['cache']['driver'], Horde::getDriverConfig('cache', $GLOBALS['conf']['cache']['driver']));
        foreach (array_keys($this->_tosave) as $val) {
            $cacheid = $this->_getHordeCacheID($val);
            if (isset($this->_cache[$val])) {
                if ($cache->set($cacheid, Horde_Serialize::serialize($this->_cache[$val], SERIALIZE_BASIC), $GLOBALS['conf']['mlistcache']['lifetime'])) {
                    Horde::logMessage('Stored ' . $val . ' in cache.', __FILE__, __LINE__, PEAR_LOG_DEBUG);
                }
                $in_session[] = $val;
            } else {
                $cache->expire($cacheid);
            }
        }

        /* Unset all other mailboxes in the current session. */
        foreach (array_diff(array_keys($this->_cache), $in_session) as $k) {
            unset($this->_cache[$k]);
        }
    }

    /**
     * Get the cache ID to use for Horde_Cache.
     *
     * @access private
     *
     * @param string $mailbox  The mailbox to store.
     */
    function _getHordeCacheID($mailbox)
    {
        return 'imp_imapcache_ ' . $_SESSION['imp']['user'] . '|' . $mailbox;
    }

}