Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2012-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * An object allowing management of mailbox state within a
  16   * Horde_Imap_Client_Base object.
  17   *
  18   * NOTE: This class is NOT intended to be accessed outside of a Base object.
  19   * There is NO guarantees that the API of this class will not change across
  20   * versions.
  21   *
  22   * @author    Michael Slusarz <slusarz@horde.org>
  23   * @category  Horde
  24   * @copyright 2012-2017 Horde LLC
  25   * @internal
  26   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  27   * @package   Imap_Client
  28   */
  29  class Horde_Imap_Client_Base_Mailbox
  30  {
  31      /**
  32       * Mapping object.
  33       *
  34       * @var Horde_Imap_Client_Ids_Map
  35       */
  36      public $map;
  37  
  38      /**
  39       * Is mailbox opened?
  40       *
  41       * @var boolean
  42       */
  43      public $open;
  44  
  45      /**
  46       * Is mailbox sync'd with remote server (via CONDSTORE/QRESYNC)?
  47       *
  48       * @var boolean
  49       */
  50      public $sync;
  51  
  52      /**
  53       * Status information.
  54       *
  55       * @var array
  56       */
  57      protected $_status = array();
  58  
  59      /**
  60       * Constructor.
  61       */
  62      public function __construct()
  63      {
  64          $this->reset();
  65      }
  66  
  67      /**
  68       * Get status information for the mailbox.
  69       *
  70       * @param integer $entry  STATUS_* constant.
  71       *
  72       * @return mixed  Status information.
  73       */
  74      public function getStatus($entry)
  75      {
  76          if (isset($this->_status[$entry])) {
  77              return $this->_status[$entry];
  78          }
  79  
  80          switch ($entry) {
  81          case Horde_Imap_Client::STATUS_FLAGS:
  82          case Horde_Imap_Client::STATUS_SYNCFLAGUIDS:
  83          case Horde_Imap_Client::STATUS_SYNCVANISHED:
  84              return array();
  85  
  86          case Horde_Imap_Client::STATUS_FIRSTUNSEEN:
  87              /* If we know there are no messages in the current mailbox, we
  88               * know there are no unseen messages. */
  89              return empty($this->_status[Horde_Imap_Client::STATUS_MESSAGES])
  90                  ? false
  91                  : null;
  92  
  93          case Horde_Imap_Client::STATUS_RECENT_TOTAL:
  94          case Horde_Imap_Client::STATUS_SYNCMODSEQ:
  95              return 0;
  96  
  97          case Horde_Imap_Client::STATUS_PERMFLAGS:
  98              /* If PERMFLAGS is not returned by server, must assume that all
  99               * flags can be changed permanently (RFC 3501 [6.3.1]). */
 100              $flags = isset($this->_status[Horde_Imap_Client::STATUS_FLAGS])
 101                  ? $this->_status[Horde_Imap_Client::STATUS_FLAGS]
 102                  : array();
 103              $flags[] = "\\*";
 104              return $flags;
 105  
 106          case Horde_Imap_Client::STATUS_UIDNOTSTICKY:
 107              /* In the absence of explicit uidnotsticky identification, assume
 108               * that UIDs are sticky. */
 109              return false;
 110  
 111          case Horde_Imap_Client::STATUS_UNSEEN:
 112              /* If we know there are no messages in the current mailbox, we
 113               * know there are no unseen messages . */
 114              return empty($this->_status[Horde_Imap_Client::STATUS_MESSAGES])
 115                  ? 0
 116                  : null;
 117  
 118          default:
 119              return null;
 120          }
 121      }
 122  
 123      /**
 124       * Set status information for the mailbox.
 125       *
 126       * @param integer $entry  STATUS_* constant.
 127       * @param mixed $value    Status information.
 128       */
 129      public function setStatus($entry, $value)
 130      {
 131          switch ($entry) {
 132          case Horde_Imap_Client::STATUS_FIRSTUNSEEN:
 133          case Horde_Imap_Client::STATUS_HIGHESTMODSEQ:
 134          case Horde_Imap_Client::STATUS_MESSAGES:
 135          case Horde_Imap_Client::STATUS_UNSEEN:
 136          case Horde_Imap_Client::STATUS_UIDNEXT:
 137          case Horde_Imap_Client::STATUS_UIDVALIDITY:
 138              $value = intval($value);
 139              break;
 140  
 141          case Horde_Imap_Client::STATUS_RECENT:
 142              /* Keep track of RECENT_TOTAL information. */
 143              $this->_status[Horde_Imap_Client::STATUS_RECENT_TOTAL] = isset($this->_status[Horde_Imap_Client::STATUS_RECENT_TOTAL])
 144                  ? ($this->_status[Horde_Imap_Client::STATUS_RECENT_TOTAL] + $value)
 145                  : intval($value);
 146              break;
 147  
 148          case Horde_Imap_Client::STATUS_SYNCMODSEQ:
 149              /* This is only set once per access. */
 150              if (isset($this->_status[$entry])) {
 151                  return;
 152              }
 153              $value = intval($value);
 154              break;
 155  
 156          case Horde_Imap_Client::STATUS_SYNCFLAGUIDS:
 157          case Horde_Imap_Client::STATUS_SYNCVANISHED:
 158              if (!isset($this->_status[$entry])) {
 159                  $this->_status[$entry] = array();
 160              }
 161              $this->_status[$entry] = array_merge($this->_status[$entry], $value);
 162              return;
 163          }
 164  
 165          $this->_status[$entry] = $value;
 166      }
 167  
 168      /**
 169       * Reset the mailbox information.
 170       */
 171      public function reset()
 172      {
 173          $keep = array(
 174              Horde_Imap_Client::STATUS_SYNCFLAGUIDS,
 175              Horde_Imap_Client::STATUS_SYNCMODSEQ,
 176              Horde_Imap_Client::STATUS_SYNCVANISHED
 177          );
 178  
 179          foreach (array_diff(array_keys($this->_status), $keep) as $val) {
 180              unset($this->_status[$val]);
 181          }
 182  
 183          $this->map = new Horde_Imap_Client_Ids_Map();
 184          $this->open = $this->sync = false;
 185      }
 186  
 187  }