Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2014-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 2014-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Object representation of an IMAP (RFC 5092/5593) URL.
  16   *
  17   * Absolute IMAP URLs takes one of the following forms:
  18   *   - imap://<iserver>[/]
  19   *   - imap://<iserver>/<enc-mailbox>[<uidvalidity>][?<enc-search>]
  20   *   - imap://<iserver>/<enc-mailbox>[<uidvalidity>]<iuid>[<isection>][<ipartial>][<iurlauth>]
  21   *
  22   * @author    Michael Slusarz <slusarz@horde.org>
  23   * @category  Horde
  24   * @copyright 2008-2017 Horde LLC
  25   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  26   * @package   Imap_Client
  27   * @since     2.25.0
  28   *
  29   * @property Horde_Imap_Client_Mailbox $mailbox  IMAP Mailbox.
  30   * @property string $partial  Byte range for use with IMAP FETCH command.
  31   * @property string $search  Search query to be run with IMAP SEARCH.
  32   * @property string $section  MIME part ID.
  33   * @property string $uid  IMAP UID.
  34   * @property string $uidvalidity  IMAP UIDVALIDITY for the mailbox.
  35   * @property string $urlauth  URLAUTH info.
  36   */
  37  class Horde_Imap_Client_Url_Imap extends Horde_Imap_Client_Url_Base
  38  {
  39      /**
  40       * IMAP mailbox.
  41       *
  42       * @var Horde_Imap_Client_Mailbox
  43       */
  44      protected $_mailbox;
  45  
  46      /**
  47       * Byte range for use with IMAP FETCH command.
  48       *
  49       * @var string
  50       */
  51      protected $_partial;
  52  
  53      /**
  54       * Search query to be run with IMAP SEARCH.
  55       *
  56       * @var string
  57       */
  58      protected $_search;
  59  
  60      /**
  61       * MIME part ID.
  62       *
  63       * @var string
  64       */
  65      protected $_section;
  66  
  67      /**
  68       * IMAP UID.
  69       *
  70       * @var string
  71       */
  72      protected $_uid;
  73  
  74      /**
  75       * IMAP UIDVALIDITY for the given mailbox.
  76       *
  77       * @var integer
  78       */
  79      protected $_uidvalidity;
  80  
  81      /**
  82       * URLAUTH info (not parsed).
  83       *
  84       * @var string
  85       */
  86      protected $_urlauth;
  87  
  88      /**
  89       */
  90      public function __get($name)
  91      {
  92          switch ($name) {
  93          case 'mailbox':
  94              return $this->_mailbox;
  95  
  96          case 'partial':
  97          case 'search':
  98          case 'section':
  99          case 'uid':
 100          case 'uidvalidity':
 101          case 'urlauth':
 102              return isset($this->{'_' . $name})
 103                  ? $this->{'_' . $name}
 104                  : null;
 105  
 106          case 'port':
 107              return parent::__get($name) ?: 143;
 108  
 109          default:
 110              return parent::__get($name);
 111          }
 112      }
 113  
 114      /**
 115       */
 116      public function __set($name, $value)
 117      {
 118          switch ($name) {
 119          case 'mailbox':
 120              $this->_mailbox = Horde_Imap_Client_Mailbox::get($value);
 121              break;
 122  
 123          case 'partial':
 124          case 'search':
 125          case 'section':
 126          case 'uid':
 127          case 'uidvalidity':
 128          case 'urlauth':
 129              $this->{'_' . $name} = $value;
 130              break;
 131  
 132          default:
 133              parent::__set($name, $value);
 134              break;
 135          }
 136      }
 137  
 138      /**
 139       * Create an IMAP URL (RFC 5092/5593).
 140       *
 141       * @return string  A URL string.
 142       */
 143      public function __toString()
 144      {
 145          $url = 'imap://' . parent::__toString();
 146  
 147          if (($port = $this->port) != 143) {
 148              $url .= ':' . $port;
 149          }
 150  
 151          return $url . '/' . $this->_toImapString();
 152      }
 153  
 154      /**
 155       */
 156      protected function _toImapString()
 157      {
 158          $url = '';
 159  
 160          if ($mbox = $this->mailbox) {
 161              $url .= rawurlencode($mbox->utf7imap);
 162          }
 163  
 164          if ($uidvalid = $this->uidvalidity) {
 165              $url .= ';UIDVALIDITY=' . $uidvalid;
 166          }
 167  
 168          if ($search = $this->search) {
 169              $url .= '?' . rawurlencode($search);
 170          } else {
 171              if ($uid = $this->uid) {
 172                  $url .= '/;UID=' . $uid;
 173              }
 174  
 175              if ($section = $this->section) {
 176                  $url .= '/;SECTION=' . $section;
 177              }
 178  
 179              if ($partial = $this->partial) {
 180                  $url .= '/;PARTIAL=' . $partial;
 181              }
 182  
 183              if ($urlauth = $this->urlauth) {
 184                  $url .= '/;URLAUTH=' . $urlauth;
 185              }
 186          }
 187  
 188          return $url;
 189      }
 190  
 191      /**
 192       */
 193      protected function _parseUrl(array $data)
 194      {
 195          if (isset($data['path']) &&
 196              strlen($path = ltrim($data['path'], '/'))) {
 197              $parts = explode('/;', $path);
 198  
 199              $mbox = array_shift($parts);
 200              if (($pos = stripos($mbox, ';UIDVALIDITY=')) !== false) {
 201                  $this->uidvalidity = intval(substr($mbox, $pos + 13));
 202                  $mbox = substr($mbox, 0, $pos);
 203              }
 204  
 205              if ($mbox[0] === ';') {
 206                  array_unshift($parts, substr($mbox, 1));
 207              } elseif (strlen($mbox)) {
 208                  $this->_mailbox = Horde_Imap_Client_Mailbox::get(
 209                      rawurldecode($mbox),
 210                      true
 211                  );
 212              }
 213  
 214              if (isset($data['query'])) {
 215                  $this->search = rawurldecode($data['query']);
 216                  $parts = array();
 217              }
 218          } else {
 219              $parts = array();
 220          }
 221  
 222         if (count($parts)) {
 223              foreach ($parts as $val) {
 224                  list($k, $v) = explode('=', $val);
 225                  $this->{Horde_String::lower($k)} = $v;
 226              }
 227          }
 228      }
 229  
 230  }