[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/IMAP/ -> Cache.php (source)

   1  <?php
   2  /**
   3   * The IMAP_Cache:: class facilitates in caching output from the PHP imap
   4   * extension in the current session.
   5   *
   6   * $Horde: framework/IMAP/IMAP/Cache.php,v 1.4.12.11 2006/04/28 15:43:40 slusarz Exp $
   7   *
   8   * Copyright 2003-2006 Michael Slusarz <slusarz@bigworm.colorado.edu>
   9   *
  10   * See the enclosed file COPYING for license information (GPL). If you
  11   * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  12   *
  13   * @author  Michael Slusarz <slusarz@bigworm.colorado.edu>
  14   * @since   Horde 3.0
  15   * @package Horde_IMAP
  16   */
  17  class IMAP_Cache {
  18  
  19      /**
  20       * Pointer to the session cache.
  21       *
  22       * @var array
  23       */
  24      var $_cache;
  25  
  26      /**
  27       * The cached results of imap_status() calls.
  28       *
  29       * @var array
  30       */
  31      var $_statuscache = array();
  32  
  33      /**
  34       * Returns a reference to the global IMAP_Cache object, only creating
  35       * it if it doesn't already exist.
  36       *
  37       * This method must be invoked as:
  38       *   $imap_cache = &IMAP_Cache::singleton();
  39       *
  40       * @return IMAP_Cache  The IMAP_Cache instance.
  41       */
  42      function &singleton()
  43      {
  44          static $object;
  45  
  46          if (!isset($object)) {
  47              $object = new IMAP_Cache();
  48          }
  49  
  50          return $object;
  51      }
  52  
  53      /**
  54       * Constructor
  55       */
  56      function IMAP_Cache()
  57      {
  58          if (!isset($_SESSION['imap_cache'])) {
  59              $_SESSION['imap_cache'] = array();
  60          }
  61          $this->_cache = &$_SESSION['imap_cache'];
  62      }
  63  
  64      /**
  65       * Get data from the cache.
  66       *
  67       * @param resource $imap   The IMAP resource stream.
  68       * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
  69       * @param string $key      The name of a specific entry to return.
  70       * @param boolean $check   Check for updated mailbox?
  71       *
  72       * @return mixed  The data requested, or false if not available.
  73       */
  74      function getCache($imap, $mailbox, $key = null, $check = true)
  75      {
  76          if (isset($this->_cache[$mailbox])) {
  77              if (!$check || $this->checkCache($imap, $mailbox)) {
  78                  $ptr = &$this->_cache[$mailbox];
  79                  if (!is_null($key)) {
  80                      if (isset($ptr['d'][$key])) {
  81                          return $ptr['d'][$key];
  82                      }
  83                  } else {
  84                      return $ptr['d'];
  85                  }
  86              }
  87          }
  88          return false;
  89      }
  90  
  91      /**
  92       * Is the cache information up-to-date?
  93       *
  94       * @param resource $imap   The IMAP resource stream.
  95       * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
  96       * @param boolean $update  Should the cache ID string be updated?
  97       *
  98       * @return boolean  True if cache information up-to-date, false if not.
  99       */
 100      function checkCache($imap, $mailbox, $update = false)
 101      {
 102          if (isset($this->_cache[$mailbox])) {
 103              $id = $this->_getCacheID($imap, $mailbox);
 104              if ($this->_cache[$mailbox]['k'] == $id) {
 105                  return true;
 106              } elseif ($update) {
 107                  $this->storeCache($imap, $mailbox);
 108              }
 109          } elseif ($update) {
 110              $this->storeCache($imap, $mailbox);
 111          }
 112          return false;
 113      }
 114  
 115      /**
 116       * Store data in the cache.
 117       *
 118       * @param resource $imap   The IMAP resource stream.
 119       * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
 120       * @param array $values    The data to add to the cache.
 121       */
 122      function storeCache($imap, $mailbox, $values = array())
 123      {
 124          if (!isset($this->_cache[$mailbox])) {
 125              $this->_cache[$mailbox] = array('d' => array());
 126          }
 127          $ptr = &$this->_cache[$mailbox];
 128          $ptr = array(
 129              'k' => $this->_getCacheID($imap, $mailbox),
 130              'd' => array_merge($ptr['d'], $values)
 131          );
 132      }
 133  
 134      /**
 135       * Returns and caches the results of an imap_status() call.
 136       *
 137       * @since Horde 3.1.2
 138       *
 139       * @param resource $imap   The IMAP resource string.
 140       * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
 141       *
 142       * @return stdClass  The imap_status() object or the empty string.
 143       */
 144      function getStatus($imap, $mailbox)
 145      {
 146          $this->_getCacheID($imap, $mailbox);
 147          return $this->_statuscache[$mailbox];
 148      }
 149  
 150      /**
 151       * Generate the unique ID string for the mailbox.
 152       *
 153       * @access private
 154       *
 155       * @param resource $imap   The IMAP resource stream.
 156       * @param string $mailbox  The full ({hostname}mailbox) mailbox name.
 157       *
 158       * @return string  A unique string for the current state of the mailbox.
 159       */
 160      function _getCacheID($imap, $mailbox)
 161      {
 162          if (!isset($this->_statuscache[$mailbox])) {
 163              $this->_statuscache[$mailbox] = @imap_status($imap, $mailbox, SA_ALL);
 164              if (!$this->_statuscache[$mailbox]) {
 165                  Horde::logMessage(imap_last_error(), __FILE__, __LINE__, PEAR_LOG_NOTICE);
 166              }
 167          }
 168          if ($this->_statuscache[$mailbox]) {
 169              $ob = $this->_statuscache[$mailbox];
 170              return implode('|', array($ob->messages, $ob->recent, $ob->unseen, $ob->uidnext, $ob->uidvalidity));
 171          } else {
 172              return $mailbox;
 173          }
 174      }
 175  
 176  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7