Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   Mime
  12   */
  13  
  14  /**
  15   * This class represents a single header element.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2014-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Mime
  22   * @since     2.5.0
  23   *
  24   * @property-read string $name  Header name.
  25   * @property-read string $value_single  The first header value.
  26   */
  27  abstract class Horde_Mime_Headers_Element
  28  implements IteratorAggregate
  29  {
  30      /**
  31       * Header name (UTF-8, although limited to US-ASCII subset by RFCs).
  32       *
  33       * @var string
  34       */
  35      protected $_name;
  36  
  37      /**
  38       * Header values.
  39       *
  40       * @var array
  41       */
  42      protected $_values = array();
  43  
  44      /**
  45       * Constructor.
  46       *
  47       * @param string $name  Header name.
  48       * @param mixed $value  Header value(s).
  49       */
  50      public function __construct($name, $value)
  51      {
  52          $this->_name = trim($name);
  53          if (strpos($this->_name, ' ') !== false) {
  54              throw new InvalidArgumentException('Invalid header name');
  55          }
  56          $this->setValue($value);
  57      }
  58  
  59      /**
  60       */
  61      public function __get($name)
  62      {
  63          switch ($name) {
  64          case 'name':
  65              return $this->_name;
  66  
  67          case 'value_single':
  68              return reset($this->_values);
  69          }
  70      }
  71  
  72      /**
  73       * Set the value of the header.
  74       *
  75       * @param mixed $value  Header value(s).
  76       */
  77      final public function setValue($value)
  78      {
  79          $this->_setValue($value);
  80      }
  81  
  82      /**
  83       * TODO
  84       */
  85      abstract protected function _setValue($value);
  86  
  87      /**
  88       * Returns the encoded string value(s) needed when sending the header text
  89       * to a RFC compliant mail submission server.
  90       *
  91       * @param array $opts  Additional options:
  92       *   - charset: (string) Charset to encode to.
  93       *              DEFAULT: UTF-8
  94       *
  95       * @return array  An array of string values.
  96       */
  97      final public function sendEncode(array $opts = array())
  98      {
  99          return $this->_sendEncode(array_merge(array(
 100              'charset' => 'UTF-8'
 101          ), $opts));
 102      }
 103  
 104      /**
 105       * TODO
 106       */
 107      protected function _sendEncode($opts)
 108      {
 109          return $this->_values;
 110      }
 111  
 112      /**
 113       * Perform sanity checking on a header value.
 114       *
 115       * @param string $data  The header data.
 116       *
 117       * @return string  The cleaned header data.
 118       */
 119      protected function _sanityCheck($data)
 120      {
 121          $charset_test = array(
 122              'windows-1252',
 123              Horde_Mime_Headers::$defaultCharset
 124          );
 125  
 126          if (!Horde_String::validUtf8($data)) {
 127              /* Appears to be a PHP error with the internal String structure
 128               * which prevents accurate manipulation of the string. Copying
 129               * the data to a new variable fixes things. */
 130              $data = substr($data, 0);
 131  
 132              /* Assumption: broken charset in headers is generally either
 133               * UTF-8 or ISO-8859-1/Windows-1252. Test these charsets
 134               * first before using default charset. This may be a
 135               * Western-centric approach, but it's better than nothing. */
 136              foreach ($charset_test as $charset) {
 137                  $tmp = Horde_String::convertCharset($data, $charset, 'UTF-8');
 138                  if (Horde_String::validUtf8($tmp)) {
 139                      return $tmp;
 140                  }
 141              }
 142          }
 143  
 144          /* Ensure no null characters exist in header data. */
 145  	 	 if ($data === null) {
 146  	 	 	 return '';
 147  	 	 }
 148          return str_replace("\0", '', $data);
 149      }
 150  
 151      /**
 152       * If true, indicates the contents of the header is the default value.
 153       *
 154       * @since 2.8.0
 155       *
 156       * @return boolean  True if this header is the default value.
 157       */
 158      public function isDefault()
 159      {
 160          return false;
 161      }
 162  
 163      /* Static methods */
 164  
 165      /**
 166       * Return list of explicit header names handled by this driver.
 167       *
 168       * @return array  Header list.
 169       */
 170      public static function getHandles()
 171      {
 172          return array();
 173      }
 174  
 175      /* IteratorAggregate method */
 176  
 177      /**
 178       */
 179  	 #[ReturnTypeWillChange]
 180      public function getIterator()
 181      {
 182          return new ArrayIterator($this->_values);
 183      }
 184  
 185  }