[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/Mail/Rfc822/ -> Address.php (source)

   1  <?php
   2  /**
   3   * Copyright 2012-2014 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file COPYING 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-2014 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 e-mail address.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2012-2014 Horde LLC
  20   * @license   http://www.horde.org/licenses/bsd New BSD License
  21   * @package   Mail
  22   *
  23   * @property-read string $bare_address  The bare mailbox@host address.
  24   * @property-read string $bare_address_idn  The bare mailbox@host address (IDN
  25   *                                          encoded). (@since 2.1.0)
  26   * @property-read string $encoded  The full MIME/IDN encoded address (UTF-8).
  27   * @property string $host  Returns the host part (UTF-8).
  28   * @property-read string $host_idn  Returns the IDN encoded host part.
  29   * @property-read string $label  The shorthand label for this address.
  30   * @property string $personal  The personal part (UTF-8).
  31   * @property-read string $personal_encoded  The MIME encoded personal part
  32   *                                          (UTF-8).
  33   * @property-read boolean $valid  Returns true if there is enough information
  34   *                                in object to create a valid address.
  35   */
  36  class Horde_Mail_Rfc822_Address extends Horde_Mail_Rfc822_Object
  37  {
  38      /**
  39       * Comments associated with the personal phrase.
  40       *
  41       * @var array
  42       */
  43      public $comment = array();
  44  
  45      /**
  46       * Local-part of the address.
  47       *
  48       * @var string
  49       */
  50      public $mailbox = null;
  51  
  52      /**
  53       * Hostname of the address.
  54       *
  55       * @var string
  56       */
  57      protected $_host = null;
  58  
  59      /**
  60       * Personal part of the address.
  61       *
  62       * @var string
  63       */
  64      protected $_personal = null;
  65  
  66      /**
  67       * Constructor.
  68       *
  69       * @param string $address  If set, address is parsed and used as the
  70       *                         object address. Address is not validated;
  71       *                         first e-mail address parsed is used.
  72       */
  73      public function __construct($address = null)
  74      {
  75          if (!is_null($address)) {
  76              $rfc822 = new Horde_Mail_Rfc822();
  77              $addr = $rfc822->parseAddressList($address);
  78              if (count($addr)) {
  79                  foreach ($addr[0] as $key => $val) {
  80                      $this->$key = $val;
  81                  }
  82              }
  83          }
  84      }
  85  
  86      /**
  87       */
  88      public function __set($name, $value)
  89      {
  90          switch ($name) {
  91          case 'host':
  92              $value = ltrim($value, '@');
  93              $this->_host = function_exists('idn_to_utf8')
  94                  ? strtolower(idn_to_utf8($value))
  95                  : strtolower($value);
  96              break;
  97  
  98          case 'personal':
  99              $this->_personal = strlen($value)
 100                  ? Horde_Mime::decode($value)
 101                  : null;
 102              break;
 103          }
 104      }
 105  
 106      /**
 107       */
 108      public function __get($name)
 109      {
 110          switch ($name) {
 111          case 'bare_address':
 112              return is_null($this->host)
 113                  ? $this->mailbox
 114                  : $this->mailbox . '@' . $this->host;
 115  
 116          case 'bare_address_idn':
 117              $personal = $this->_personal;
 118              $this->_personal = null;
 119              $res = $this->encoded;
 120              $this->_personal = $personal;
 121              return $res;
 122  
 123          case 'encoded':
 124              return $this->writeAddress(true);
 125  
 126          case 'host':
 127              return $this->_host;
 128  
 129          case 'host_idn':
 130              return function_exists('idn_to_ascii')
 131                  ? idn_to_ascii($this->_host)
 132                  : $this->host;
 133  
 134          case 'label':
 135              return is_null($this->personal)
 136                  ? $this->bare_address
 137                  : $this->_personal;
 138  
 139          case 'personal':
 140              return (strcasecmp($this->_personal, $this->bare_address) === 0)
 141                  ? null
 142                  : $this->_personal;
 143  
 144          case 'personal_encoded':
 145              return Horde_Mime::encode($this->personal);
 146  
 147          case 'valid':
 148              return (bool)strlen($this->mailbox);
 149  
 150          default:
 151              return null;
 152          }
 153      }
 154  
 155      /**
 156       */
 157      protected function _writeAddress($opts)
 158      {
 159          $rfc822 = new Horde_Mail_Rfc822();
 160  
 161          $address = $rfc822->encode($this->mailbox, 'address');
 162          $host = empty($opts['idn']) ? $this->host : $this->host_idn;
 163          if (strlen($host)) {
 164              $address .= '@' . $host;
 165          }
 166          $personal = $this->personal;
 167          if (strlen($personal)) {
 168              if (!empty($opts['encode'])) {
 169                  $personal = Horde_Mime::encode($this->personal, $opts['encode']);
 170              }
 171              $personal = $rfc822->encode($personal, 'personal');
 172          }
 173  
 174          return (strlen($personal) && ($personal != $address))
 175              ? $personal . ' <' . $address . '>'
 176              : $address;
 177      }
 178  
 179      /**
 180       */
 181      public function match($ob)
 182      {
 183          if (!($ob instanceof Horde_Mail_Rfc822_Address)) {
 184              $ob = new Horde_Mail_Rfc822_Address($ob);
 185          }
 186  
 187          return ($this->bare_address == $ob->bare_address);
 188      }
 189  
 190      /**
 191       * Do a case-insensitive match on the address. Per RFC 822/2822/5322,
 192       * although the host portion of an address is case-insensitive, the
 193       * mailbox portion is platform dependent.
 194       *
 195       * @param mixed $ob  Address data.
 196       *
 197       * @return boolean  True if the data reflects the same case-insensitive
 198       *                  address.
 199       */
 200      public function matchInsensitive($ob)
 201      {
 202          if (!($ob instanceof Horde_Mail_Rfc822_Address)) {
 203              $ob = new Horde_Mail_Rfc822_Address($ob);
 204          }
 205  
 206          return (Horde_String::lower($this->bare_address) == Horde_String::lower($ob->bare_address));
 207      }
 208  
 209      /**
 210       * Do a case-insensitive match on the address for a given domain.
 211       * Matches as many parts of the subdomain in the address as is given in
 212       * the input.
 213       *
 214       * @param string $domain  Domain to match.
 215       *
 216       * @return boolean  True if the address matches the given domain.
 217       */
 218      public function matchDomain($domain)
 219      {
 220          $host = $this->host;
 221          if (is_null($host)) {
 222              return false;
 223          }
 224  
 225          $match_domain = explode('.', $domain);
 226          $match_host = array_slice(explode('.', $host), count($match_domain) * -1);
 227  
 228          return (strcasecmp($domain, implode('.', $match_host)) === 0);
 229      }
 230  
 231  }


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