1: <?php
2: /**
3: * Object representation of a RFC 822 e-mail address.
4: *
5: * @since 1.1.0
6: *
7: * Copyright 2012 Horde LLC (http://www.horde.org/)
8: *
9: * See the enclosed file COPYING for license information (BSD). If you
10: * did not receive this file, see http://www.horde.org/licenses/bsd.
11: *
12: * @author Michael Slusarz <slusarz@horde.org>
13: * @category Horde
14: * @license http://www.horde.org/licenses/bsd New BSD License
15: * @package Mail
16: */
17:
18: /**
19: * Object representation of a RFC 822 e-mail address.
20: *
21: * @author Michael Slusarz <slusarz@horde.org>
22: * @category Horde
23: * @license http://www.horde.org/licenses/bsd New BSD License
24: * @package Mail
25: *
26: * @property string $adl ADL data (DEPRECATED).
27: * @property string $encoded The full MIME/IDN encoded address. (Since 1.2.0)
28: * @property string $full_address The full mailbox@host address.
29: * @property string $personal_decoded The MIME decoded personal part.
30: * (DEPRECATED)
31: * @property string $personal_encoded The MIME encoded personal part.
32: */
33: class Horde_Mail_Rfc822_Address extends Horde_Mail_Rfc822_Object implements ArrayAccess
34: {
35: /**
36: * Comments associated with the personal phrase.
37: *
38: * @var array
39: */
40: public $comment = array();
41:
42: /**
43: * Hostname of the address.
44: *
45: * @var string
46: */
47: public $host = null;
48:
49: /**
50: * Local-part of the address.
51: *
52: * @var string
53: */
54: public $mailbox = null;
55:
56: /**
57: * Personal part of the address.
58: *
59: * @var string
60: */
61: public $personal = null;
62:
63: /**
64: * Routing information (obsoleted by RFC 2822 [4.4]).
65: *
66: * @deprecated
67: *
68: * @var array
69: */
70: public $route = array();
71:
72: /**
73: * Constructor.
74: *
75: * @param string $addresses If set, address is parsed and used as the
76: * object address (since 1.2.0). Address is not
77: * validated; first e-mail address parsed is
78: * used.
79: */
80: public function __construct($address = null)
81: {
82: if (!is_null($address)) {
83: $rfc822 = new Horde_Mail_Rfc822();
84: $addr = $rfc822->parseAddressList($address, array(
85: 'nest_groups' => false,
86: 'validate' => false
87: ));
88: if (count($addr)) {
89: foreach ($addr[0] as $key => $val) {
90: $this->$key = $val;
91: }
92: }
93: }
94: }
95:
96: /**
97: */
98: public function __get($name)
99: {
100: switch ($name) {
101: case 'adl':
102: // DEPRECATED
103: return empty($route)
104: ? ''
105: : implode(',', $route);
106:
107: case 'encoded':
108: // Returns the full MIME/IDN encoded address.
109: return $this->writeAddress(array(
110: 'encode' => true,
111: 'idn' => false
112: ));
113:
114: case 'full_address':
115: // Return the full mailbox@host address.
116: return is_null($this->host)
117: ? $this->mailbox
118: : $this->mailbox . '@' . $this->host;
119:
120: case 'personal_decoded':
121: // DEPRECATED
122: return Horde_Mime::decode($this->personal, 'UTF-8');
123:
124: case 'personal_encoded':
125: return Horde_Mime::encode($this->personal, 'UTF-8');
126:
127: default:
128: return null;
129: }
130: }
131:
132: /**
133: * Write an address given information in this part.
134: *
135: * @param array $opts Optional arguments:
136: * - encode: (boolean) MIME encode the personal part?
137: * - idn: (boolean) If true, decode IDN domain names (Punycode/RFC 3490).
138: * If false, convert domain names into IDN if necessary (@since
139: * 1.5.0).
140: * If null, does no conversion.
141: * Requires the idn or intl PHP module.
142: * DEFAULT: true
143: *
144: * @return string The correctly escaped/quoted address.
145: */
146: public function writeAddress(array $opts = array())
147: {
148: $host = ltrim($this->host, '@');
149:
150: if (!array_key_exists('idn', $opts)) {
151: $opts['idn'] = true;
152: }
153:
154: switch ($opts['idn']) {
155: case true:
156: if (function_exists('idn_to_utf8')) {
157: $host = idn_to_utf8($host);
158: }
159: break;
160:
161: case false:
162: if (function_exists('idn_to_ascii')) {
163: $host = idn_to_ascii($host);
164: }
165: break;
166: }
167:
168: $rfc822 = new Horde_Mail_Rfc822();
169: $address = $rfc822->encode($this->mailbox, 'address') . '@' . $host;
170: $personal = empty($opts['encode'])
171: ? $this->personal
172: : $this->personal_encoded;
173:
174: return (strlen($personal) && ($personal != $address))
175: ? $rfc822->encode($personal, 'personal') . ' <' . $address . '>'
176: : $address;
177: }
178:
179: /* ArrayAccess methods. TODO: Here for BC purposes. Remove for 2.0. */
180:
181: /**
182: */
183: public function offsetExists($offset)
184: {
185: return (bool)$this->$offset;
186: }
187:
188: /**
189: */
190: public function offsetGet($offset)
191: {
192: return $this->$offset;
193: }
194:
195: /**
196: */
197: public function offsetSet($offset, $value)
198: {
199: if (property_exists($this, $offset)) {
200: $this->$offset = $value;
201: }
202: }
203:
204: /**
205: */
206: public function offsetUnset($offset)
207: {
208: if (property_exists($this, $offset)) {
209: switch ($offset) {
210: case 'comment':
211: $this->comment = array();
212: break;
213:
214: default:
215: $this->$offset = null;
216: break;
217: }
218: }
219: }
220:
221: }
222: