[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/Imap/Client/ -> Url.php (source)

   1  <?php
   2  /**
   3   * Copyright 2008-2014 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file COPYING 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 2008-2014 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Object representation of a a POP3 (RFC 2384) or IMAP (RFC 5092/5593) URL.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2008-2014 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Imap_Client
  22   *
  23   * @property-read boolean $relative  Is this a relative URL?
  24   */
  25  class Horde_Imap_Client_Url implements Serializable
  26  {
  27      /**
  28       * The authentication method to use.
  29       *
  30       * @var string
  31       */
  32      public $auth = null;
  33  
  34      /**
  35       * The remote server (not present for relative URLs).
  36       *
  37       * @var string
  38       */
  39      public $hostspec = null;
  40  
  41      /**
  42       * The IMAP mailbox
  43       *
  44       * @var string
  45       */
  46      public $mailbox = null;
  47  
  48      /**
  49       * A byte range for use with IMAP FETCH.
  50       *
  51       * @var string
  52       */
  53      public $partial = null;
  54  
  55      /**
  56       * The remote port (not present for relative URLs).
  57       *
  58       * @var integer
  59       */
  60      public $port = null;
  61  
  62      /**
  63       * The protocol type. Either 'imap' or 'pop' (not present for relative
  64       * URLs).
  65       *
  66       * @var string
  67       */
  68      public $protocol = null;
  69  
  70      /**
  71       * A search query to be run with IMAP SEARCH.
  72       *
  73       * @var string
  74       */
  75      public $search = null;
  76  
  77      /**
  78       * A MIME part ID.
  79       *
  80       * @var string
  81       */
  82      public $section = null;
  83  
  84      /**
  85       * The username to use on the remote server.
  86       *
  87       * @var string
  88       */
  89      public $username = null;
  90  
  91      /**
  92       * The IMAP UID.
  93       *
  94       * @var string
  95       */
  96      public $uid = null;
  97  
  98      /**
  99       * The IMAP UIDVALIDITY for the given mailbox.
 100       *
 101       * @var integer
 102       */
 103      public $uidvalidity = null;
 104  
 105      /**
 106       * URLAUTH info (not parsed).
 107       *
 108       * @var string
 109       */
 110      public $urlauth = null;
 111  
 112      /**
 113       * Constructor.
 114       *
 115       * Absolute IMAP URLs takes one of the following forms:
 116       *   - imap://<iserver>[/]
 117       *   - imap://<iserver>/<enc-mailbox>[<uidvalidity>][?<enc-search>]
 118       *   - imap://<iserver>/<enc-mailbox>[<uidvalidity>]<iuid>[<isection>][<ipartial>][<iurlauth>]
 119       *
 120       * POP URLs take one of the following forms:
 121       *   - pop://<user>;auth=<auth>@<host>:<port>
 122       *
 123       * @param string $url  A URL string.
 124       */
 125      public function __construct($url = null)
 126      {
 127          if (!is_null($url)) {
 128              $this->_parse($url);
 129          }
 130      }
 131  
 132      /**
 133       * Create a POP3 (RFC 2384) or IMAP (RFC 5092/5593) URL.
 134       *
 135       * @return string  A URL string.
 136       */
 137      public function __toString()
 138      {
 139          $url = '';
 140  
 141          if (!is_null($this->protocol)) {
 142              $url = $this->protocol . '://';
 143  
 144              if (!is_null($this->username)) {
 145                  $url .= $this->username;
 146              }
 147  
 148              if (!is_null($this->auth)) {
 149                  $url .= ';AUTH=' . $this->auth . '@';
 150              } elseif (!is_null($this->username)) {
 151                  $url .= '@';
 152              }
 153  
 154              $url .= $this->hostspec;
 155  
 156              if (!is_null($this->port) && ($this->port != 143)) {
 157                  $url .= ':' . $this->port;
 158              }
 159          }
 160  
 161          $url .= '/';
 162  
 163          if (is_null($this->protocol) || ($this->protocol == 'imap')) {
 164              $url .= urlencode($this->mailbox);
 165  
 166              if (!empty($this->uidvalidity)) {
 167                  $url .= ';UIDVALIDITY=' . $this->uidvalidity;
 168              }
 169  
 170              if (!is_null($this->search)) {
 171                  $url .= '?' . urlencode($this->search);
 172              } else {
 173                  if (!is_null($this->uid)) {
 174                      $url .= '/;UID=' . $this->uid;
 175                  }
 176  
 177                  if (!is_null($this->section)) {
 178                      $url .= '/;SECTION=' . $this->section;
 179                  }
 180  
 181                  if (!is_null($this->partial)) {
 182                      $url .= '/;PARTIAL=' . $this->partial;
 183                  }
 184  
 185                  if (!is_null($this->urlauth)) {
 186                      $url .= '/;URLAUTH=' . $this->urlauth;
 187                  }
 188              }
 189          }
 190  
 191          return $url;
 192      }
 193  
 194      /**
 195       */
 196      public function __get($name)
 197      {
 198          switch ($name) {
 199          case 'relative':
 200              return (is_null($this->hostspec) &&
 201                  is_null($this->port) &&
 202                  is_null($this->protocol));
 203          }
 204      }
 205  
 206      /**
 207       */
 208      protected function _parse($url)
 209      {
 210          $data = parse_url(trim($url));
 211  
 212          if (isset($data['scheme'])) {
 213              $protocol = strtolower($data['scheme']);
 214              if (!in_array($protocol, array('imap', 'pop'))) {
 215                  return;
 216              }
 217  
 218              if (isset($data['host'])) {
 219                  $this->hostspec = $data['host'];
 220              }
 221              $this->port = isset($data['port'])
 222                  ? $data['port']
 223                  : 143;
 224              $this->protocol = $protocol;
 225          }
 226  
 227          /* Check for username/auth information. */
 228          if (isset($data['user'])) {
 229              if (($pos = stripos($data['user'], ';AUTH=')) !== false) {
 230                  $auth = substr($data['user'], $pos + 6);
 231                  if ($auth !== '*') {
 232                      $this->auth = $auth;
 233                  }
 234                  $data['user'] = substr($data['user'], 0, $pos);
 235              }
 236  
 237              if (strlen($data['user'])) {
 238                  $this->username = $data['user'];
 239              }
 240          }
 241  
 242          /* IMAP-only information. */
 243          if (is_null($this->protocol) || ($this->protocol == 'imap')) {
 244              if (isset($data['path'])) {
 245                  $data['path'] = ltrim($data['path'], '/');
 246                  $parts = explode('/;', $data['path']);
 247  
 248                  $mbox = array_shift($parts);
 249                  if (($pos = stripos($mbox, ';UIDVALIDITY=')) !== false) {
 250                      $this->uidvalidity = intval(substr($mbox, $pos + 13));
 251                      $mbox = substr($mbox, 0, $pos);
 252                  }
 253                  $this->mailbox = urldecode($mbox);
 254  
 255              }
 256  
 257              if (count($parts)) {
 258                  foreach ($parts as $val) {
 259                      list($k, $v) = explode('=', $val);
 260                      $property = strtolower($k);
 261                      $this->$property = $v;
 262                  }
 263              } elseif (isset($data['query'])) {
 264                  $this->search = urldecode($data['query']);
 265              }
 266          }
 267      }
 268  
 269      /* Serializable methods. */
 270  
 271      /**
 272       */
 273      public function serialize()
 274      {
 275          return strval($this);
 276      }
 277  
 278      /**
 279       */
 280      public function unserialize($data)
 281      {
 282          $this->_parse($data);
 283      }
 284  
 285  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1