Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  /**
   3   * Copyright 2011-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 2011-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * An object that provides a way to switch between UTF7-IMAP and
  16   * human-readable representations of a mailbox name.
  17   *
  18   * @author    Michael Slusarz <slusarz@horde.org>
  19   * @category  Horde
  20   * @copyright 2011-2017 Horde LLC
  21   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  22   * @package   Imap_Client
  23   *
  24   * @property-read string $list_escape  Escapes mailbox for use in LIST
  25   *                                     command (UTF-8).
  26   * @property-read string $utf7imap  Mailbox in UTF7-IMAP.
  27   * @property-read string $utf8  Mailbox in UTF-8.
  28   */
  29  class Horde_Imap_Client_Mailbox implements Serializable
  30  {
  31      /**
  32       * UTF7-IMAP representation of mailbox.
  33       * If boolean true, it is identical to UTF-8 representation.
  34       *
  35       * @var mixed
  36       */
  37      protected $_utf7imap;
  38  
  39      /**
  40       * UTF8 representation of mailbox.
  41       *
  42       * @var string
  43       */
  44      protected $_utf8;
  45  
  46      /**
  47       * Shortcut to obtaining mailbox object.
  48       *
  49       * @param string $mbox       The mailbox name.
  50       * @param boolean $utf7imap  Is mailbox UTF7-IMAP encoded? Otherwise,
  51       *                           mailbox is assumed to be UTF-8.
  52       *
  53       * @return Horde_Imap_Client_Mailbox  A mailbox object.
  54       */
  55      public static function get($mbox, $utf7imap = false)
  56      {
  57          return ($mbox instanceof Horde_Imap_Client_Mailbox)
  58              ? $mbox
  59              : new Horde_Imap_Client_Mailbox($mbox, $utf7imap);
  60      }
  61  
  62      /**
  63       * Constructor.
  64       *
  65       * @param string $mbox       The mailbox name.
  66       * @param boolean $utf7imap  Is mailbox UTF7-IMAP encoded (true).
  67       *                           Otherwise, mailbox is assumed to be UTF-8
  68       *                           encoded.
  69       */
  70      public function __construct($mbox, $utf7imap = false)
  71      {
  72          if (strcasecmp($mbox, 'INBOX') === 0) {
  73              $mbox = 'INBOX';
  74          }
  75  
  76          if ($utf7imap) {
  77              $this->_utf7imap = $mbox;
  78          } else {
  79              $this->_utf8 = $mbox;
  80          }
  81      }
  82  
  83      /**
  84       */
  85      public function __get($name)
  86      {
  87          switch ($name) {
  88          case 'list_escape':
  89              return preg_replace("/\*+/", '%', $this->utf8);
  90  
  91          case 'utf7imap':
  92              if (!isset($this->_utf7imap)) {
  93                  $n = Horde_Imap_Client_Utf7imap::Utf8ToUtf7Imap($this->_utf8);
  94                  $this->_utf7imap = ($n == $this->_utf8)
  95                      ? true
  96                      : $n;
  97              }
  98  
  99              return ($this->_utf7imap === true)
 100                  ? $this->_utf8
 101                  : $this->_utf7imap;
 102  
 103          case 'utf8':
 104              if (!isset($this->_utf8)) {
 105                  $this->_utf8 = Horde_Imap_Client_Utf7imap::Utf7ImapToUtf8($this->_utf7imap);
 106                  if ($this->_utf8 == $this->_utf7imap) {
 107                      $this->_utf7imap = true;
 108                  }
 109              }
 110              return (string)$this->_utf8;
 111          }
 112      }
 113  
 114      /**
 115       */
 116      public function __toString()
 117      {
 118          return $this->utf8;
 119      }
 120  
 121      /**
 122       * Compares this mailbox to another mailbox string.
 123       *
 124       * @return boolean  True if the items are equal.
 125       */
 126      public function equals($mbox)
 127      {
 128          return ($this->utf8 == $mbox);
 129      }
 130  
 131      /* Serializable methods. */
 132  
 133      /**
 134       */
 135      public function serialize()
 136      {
 137          return json_encode(array($this->_utf7imap, $this->_utf8));
 138      }
 139  
 140      /**
 141       */
 142      public function unserialize($data)
 143      {
 144          list($this->_utf7imap, $this->_utf8) = json_decode($data, true);
 145      }
 146  
 147  }