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 2012-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (BSD). If you
   6   * did not receive this file, see http://www.horde.org/licenses/bsd.
   7   *
   8   * @category  Horde
   9   * @copyright 2012-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/bsd New BSD License
  11   * @package   Mail
  12   */
  13  
  14  /**
  15   * Object representation of a RFC 822 group e-mail address.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2012-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/bsd New BSD License
  21   * @package   Mail
  22   *
  23   * @property string $groupname  Groupname (UTF-8).
  24   * @property-read string $groupname_encoded  MIME encoded groupname (UTF-8).
  25   * @property-read string $label  The shorthand label for this group.
  26   * @property-read boolean $valid  Returns true if there is enough information
  27   *                                in object to create a valid address.
  28   */
  29  class Horde_Mail_Rfc822_Group
  30      extends Horde_Mail_Rfc822_Object
  31      implements Countable
  32  {
  33      /**
  34       * List of group e-mail address objects.
  35       *
  36       * @var Horde_Mail_Rfc822_GroupList
  37       */
  38      public $addresses;
  39  
  40      /**
  41       * Group name (MIME decoded).
  42       *
  43       * @var string
  44       */
  45      protected $_groupname = 'Group';
  46  
  47      /**
  48       * Constructor.
  49       *
  50       * @param string $groupname  If set, used as the group name.
  51       * @param mixed $addresses   If a GroupList object, used as the address
  52       *                           list. Any other non-null value is parsed and
  53       *                           used as the address list (addresses not
  54       *                           verified; sub-groups are ignored).
  55       */
  56      public function __construct($groupname = null, $addresses = null)
  57      {
  58          if (!is_null($groupname)) {
  59              $this->groupname = $groupname;
  60          }
  61  
  62          if (is_null($addresses)) {
  63              $this->addresses = new Horde_Mail_Rfc822_GroupList();
  64          } elseif ($addresses instanceof Horde_Mail_Rfc822_GroupList) {
  65              $this->addresses = clone $addresses;
  66          } else {
  67              $rfc822 = new Horde_Mail_Rfc822();
  68              $this->addresses = $rfc822->parseAddressList($addresses, array(
  69                  'group' => true
  70              ));
  71          }
  72      }
  73  
  74      /**
  75       */
  76      public function __set($name, $value)
  77      {
  78          switch ($name) {
  79          case 'groupname':
  80              $this->_groupname = Horde_Mime::decode($value);
  81              break;
  82          }
  83      }
  84  
  85      /**
  86       */
  87      public function __get($name)
  88      {
  89          switch ($name) {
  90          case 'groupname':
  91          case 'label':
  92              return $this->_groupname;
  93  
  94          case 'groupname_encoded':
  95              return Horde_Mime::encode($this->_groupname);
  96  
  97          case 'valid':
  98              return (bool)strlen($this->_groupname);
  99          }
 100      }
 101  
 102      /**
 103       */
 104      protected function _writeAddress($opts)
 105      {
 106          $addr = $this->addresses->writeAddress($opts);
 107          $groupname = $this->groupname;
 108          if (!empty($opts['encode'])) {
 109              $groupname = Horde_Mime::encode($groupname, $opts['encode']);
 110          }
 111          if (empty($opts['noquote'])) {
 112              $rfc822 = new Horde_Mail_Rfc822();
 113              $groupname = $rfc822->encode($groupname, 'personal');
 114          }
 115          if (!empty($opts['comment']) && !empty($this->comment)) {
 116              $rfc822 = new Horde_Mail_Rfc822();
 117              foreach ($this->comment as $val) {
 118                  $personal .= ' (' . $rfc822->encode($val, 'comment') . ')';
 119              }
 120          }
 121  
 122          return ltrim($groupname) . ':' .
 123              (strlen($addr) ? (' ' . $addr) : '') . ';';
 124      }
 125  
 126      /**
 127       */
 128      public function match($ob)
 129      {
 130          return $this->addresses->match($ob);
 131      }
 132  
 133      /* Countable methods. */
 134  
 135      /**
 136       * Address count.
 137       *
 138       * @return integer  The number of addresses.
 139       */
 140      #[\ReturnTypeWillChange]
 141      public function count()
 142      {
 143          return count($this->addresses);
 144      }
 145  
 146  }