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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

   1  <?php
   2  /**
   3   * Copyright 2008-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 2008-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Base object representation of a mail server URL.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2008-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Imap_Client
  22   * @since     2.25.0
  23   *
  24   * @property string $auth  The authentication method.
  25   * @property string $host  The server.
  26   * @property integer $port  The port.
  27   * @property string $username  The username.
  28   */
  29  abstract class Horde_Imap_Client_Url_Base implements Serializable
  30  {
  31      /**
  32       * The authentication method to use.
  33       *
  34       * @var string
  35       */
  36      protected $_auth = null;
  37  
  38      /**
  39       * The server name.
  40       *
  41       * @var string
  42       */
  43      protected $_host = null;
  44  
  45      /**
  46       * The port.
  47       *
  48       * @var integer
  49       */
  50      protected $_port = null;
  51  
  52      /**
  53       * The username.
  54       *
  55       * @var string
  56       */
  57      protected $_username = null;
  58  
  59      /**
  60       * Constructor.
  61       *
  62       * @param string $url  A URL string.
  63       */
  64      public function __construct($url = null)
  65      {
  66          if (!is_null($url)) {
  67              $this->_parse($url);
  68          }
  69      }
  70  
  71      /**
  72       */
  73      public function __get($name)
  74      {
  75          switch ($name) {
  76          case 'auth':
  77          case 'host':
  78          case 'port':
  79          case 'username':
  80              return $this->{'_' . $name};
  81          }
  82      }
  83  
  84      /**
  85       */
  86      public function __set($name, $value)
  87      {
  88          switch ($name) {
  89          case 'auth':
  90          case 'host':
  91          case 'port':
  92          case 'username':
  93              $this->{'_' . $name} = $value;
  94              break;
  95          }
  96      }
  97  
  98      /**
  99       * Create a POP3 (RFC 2384) or IMAP (RFC 5092/5593) URL.
 100       *
 101       * @return string  A URL string.
 102       */
 103      public function __toString()
 104      {
 105          $url = '';
 106  
 107          if (!is_null($this->username)) {
 108              $url .= $this->username;
 109              if (!is_null($this->auth)) {
 110                  $url .= ';AUTH=' . $this->auth;
 111              }
 112              $url .= '@';
 113          }
 114  
 115          $url .= $this->host;
 116  
 117          return $url;
 118      }
 119  
 120      /**
 121       */
 122      protected function _parse($url)
 123      {
 124          $data = parse_url(trim($url));
 125  
 126          if (isset($data['scheme'])) {
 127              if (isset($data['host'])) {
 128                  $this->host = $data['host'];
 129              }
 130              if (isset($data['port'])) {
 131                  $this->port = $data['port'];
 132              }
 133          }
 134  
 135          /* Check for username/auth information. */
 136          if (isset($data['user'])) {
 137              if (($pos = stripos($data['user'], ';AUTH=')) !== false) {
 138                  $auth = substr($data['user'], $pos + 6);
 139                  if ($auth !== '*') {
 140                      $this->auth = $auth;
 141                  }
 142                  $data['user'] = substr($data['user'], 0, $pos);
 143              }
 144  
 145              if (strlen($data['user'])) {
 146                  $this->username = $data['user'];
 147              }
 148          }
 149  
 150          $this->_parseUrl($data);
 151      }
 152  
 153      /**
 154       */
 155      abstract protected function _parseUrl(array $data);
 156  
 157      /* Serializable methods. */
 158  
 159      /**
 160       */
 161      public function serialize()
 162      {
 163          return serialize($this->__serialize());
 164      }
 165  
 166      /**
 167       */
 168      public function unserialize($data)
 169      {
 170          $data = @unserialize($data);
 171          if (!is_array($data)) {
 172              throw new Exception('Cache version change.');
 173          }
 174          $this->__unserialize($data);
 175      }
 176  
 177      /**
 178       * @return array
 179       */
 180      public function __serialize()
 181      {
 182          return array((string)$this);
 183      }
 184  
 185      public function __unserialize(array $data)
 186      {
 187          $this->_parse($data[0]);
 188      }
 189  
 190  }