Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   Mime
  12   */
  13  
  14  /**
  15   * This class represents address fields (RFC 5322).
  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  class Horde_Mime_Headers_Addresses
  25  extends Horde_Mime_Headers_Element_Single
  26  implements Horde_Mime_Headers_Element_Address
  27  {
  28      /**
  29       * By default, if more than 1 address header is found, the addresses are
  30       * appended together into a single field.  Set this value to false to
  31       * ignore all but the *last* header.
  32       *
  33       * @var boolean
  34       */
  35      public $append_addr = true;
  36  
  37      /**
  38       */
  39      public function __clone()
  40      {
  41          $this->_values = clone $this->_values;
  42      }
  43  
  44      /**
  45       */
  46      public function __get($name)
  47      {
  48          switch ($name) {
  49          case 'full_value':
  50          case 'value':
  51          case 'value_single':
  52              return strval($this->_values);
  53          }
  54  
  55          return parent::__get($name);
  56      }
  57  
  58      /**
  59       */
  60      public function getAddressList($first = false)
  61      {
  62          return $first
  63              ? $this->_values
  64              : array($this->_values);
  65      }
  66  
  67      /**
  68       *
  69       * @throws Horde_Mime_Exception
  70       */
  71      protected function _setValue($value)
  72      {
  73          /* @todo Implement with traits */
  74          $rfc822 = new Horde_Mail_Rfc822();
  75  
  76          try {
  77              $addr_list = $rfc822->parseAddressList($value);
  78          } catch (Horde_Mail_Exception $e) {
  79              throw new Horde_Mime_Exception($e);
  80          }
  81  
  82          foreach ($addr_list as $ob) {
  83              if ($ob instanceof Horde_Mail_Rfc822_Group) {
  84                  $ob->groupname = $this->_sanityCheck($ob->groupname);
  85              } else {
  86                  $ob->personal = $this->_sanityCheck($ob->personal);
  87              }
  88          }
  89  
  90          switch (Horde_String::lower($this->name)) {
  91          case 'bcc':
  92          case 'cc':
  93          case 'from':
  94          case 'to':
  95              /* Catch malformed undisclosed-recipients entries. */
  96              if ((count($addr_list) == 1) &&
  97                  preg_match("/^\s*undisclosed-recipients:?\s*$/i", $addr_list[0]->bare_address)) {
  98                  $addr_list = new Horde_Mail_Rfc822_List(
  99                      'undisclosed-recipients:;'
 100                  );
 101              }
 102              break;
 103          }
 104  
 105          if ($this->append_addr && $this->_values) {
 106              $this->_values->add($addr_list);
 107          } else {
 108              $this->_values = $addr_list;
 109          }
 110      }
 111  
 112      /**
 113       */
 114      public static function getHandles()
 115      {
 116          return array(
 117              // Mail: RFC 3798
 118              'disposition-notification-to',
 119              // Mail: RFC 5322 (Address)
 120              'from',
 121              'to',
 122              'cc',
 123              'bcc',
 124              'reply-to',
 125              'sender'
 126          );
 127      }
 128  
 129      /**
 130       * @param array $opts  See doSendEncode().
 131       */
 132      protected function _sendEncode($opts)
 133      {
 134          return self::doSendEncode($this->getAddressList(), $opts);
 135      }
 136  
 137      /**
 138       * Do send encoding for addresses.
 139       *
 140       * Needed as a static function because it is used by both single and
 141       * multiple address headers.
 142       *
 143       * @todo  Implement with traits.
 144       *
 145       * @param array $alist  An array of Horde_Mail_Rfc822_List objects.
 146       * @param array $opts   Additional options:
 147       *   - charset: (string) Encodes the headers using this charset.
 148       *              DEFAULT: UTF-8
 149       *   - defserver: (string) The default domain to append to mailboxes.
 150       *                DEFAULT: No default name.
 151       *   - idn: (boolean)  Encode IDN domain names (RFC 3490) if true.
 152       *           DEFAULT: true
 153       */
 154      public static function doSendEncode($alist, array $opts = array())
 155      {
 156          $out = array();
 157          $opts = array_merge(array('idn' => true), $opts);
 158          foreach ($alist as $ob) {
 159              if (!empty($opts['defserver'])) {
 160                  foreach ($ob->raw_addresses as $ob2) {
 161                      if (is_null($ob2->host)) {
 162                          $ob2->host = $opts['defserver'];
 163                      }
 164                  }
 165              }
 166  
 167              $out[] = $ob->writeAddress(array(
 168                  'encode' => empty($opts['charset']) ? null : $opts['charset'],
 169                  'idn' => $opts['idn']
 170              ));
 171          }
 172  
 173          return $out;
 174      }
 175  
 176  }