/
Headers.php
535 lines (475 loc) · 14.6 KB
/
Headers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?php
/**
* Copyright 2002-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2002-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
*/
/**
* This class represents the collection of header values for a single mail
* message part.
*
* It supports the base e-mail spec (RFC 5322) and the MIME extensions to that
* spec (RFC 2045).
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2002-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
*/
class Horde_Mime_Headers
implements ArrayAccess, IteratorAggregate, Serializable
{
/* Serialized version. */
const VERSION = 3;
/**
* The default charset to use when parsing text parts with no charset
* information.
*
* @todo Make this a non-static property or pass as parameter to static
* methods in Horde 6.
* @var string
*/
public static $defaultCharset = 'us-ascii';
/**
* Cached handler information for Header Element objects.
*
* @var array
*/
protected static $_handlers = array();
/**
* The internal headers array.
*
* @var Horde_Support_CaseInsensitiveArray
*/
protected $_headers;
/**
* Constructor.
*/
public function __construct()
{
$this->_headers = new Horde_Support_CaseInsensitiveArray();
}
/**
*/
public function __clone()
{
$copy = new Horde_Support_CaseInsensitiveArray();
foreach ($this->_headers as $key => $val) {
$copy[$key] = clone $val;
}
$this->_headers = $copy;
}
/**
* Returns the headers in array format.
*
* @param array $opts Optional parameters:
* <pre>
* - broken_rfc2231: (boolean) Attempt to work around non-RFC
* 2231-compliant MUAs by generating both a RFC
* 2047-like parameter name and also the correct RFC
* 2231 parameter
* DEFAULT: false
* - canonical: (boolean) Use canonical (RFC 822/2045) CRLF EOLs?
* DEFAULT: Uses "\n"
* - charset: (string) Encodes the headers using this charset. If empty,
* encodes using UTF-8.
* DEFAULT: No encoding.
* - defserver: (string) The default domain to append to mailboxes.
* DEFAULT: No default name.
* - lang: (string) The language to use when encoding.
* DEFAULT: None specified
* - nowrap: (integer) Don't wrap the headers.
* DEFAULT: Headers are wrapped.
* </pre>
*
* @return array The headers in array format. Keys are header names, but
* case sensitivity cannot be guaranteed. Values are
* header values.
*/
public function toArray(array $opts = array())
{
$charset = array_key_exists('charset', $opts)
? (empty($opts['charset']) ? 'UTF-8' : $opts['charset'])
: null;
$eol = empty($opts['canonical'])
? $this->_eol
: "\r\n";
$ret = array();
foreach ($this->_headers as $ob) {
$sopts = array(
'charset' => $charset
);
if (($ob instanceof Horde_Mime_Headers_Addresses) ||
($ob instanceof Horde_Mime_Headers_AddressesMulti)) {
if (!empty($opts['defserver'])) {
$sopts['defserver'] = $opts['defserver'];
}
} elseif ($ob instanceof Horde_Mime_Headers_ContentParam) {
$sopts['broken_rfc2231'] = !empty($opts['broken_rfc2231']);
if (!empty($opts['lang'])) {
$sopts['lang'] = $opts['lang'];
}
}
$tmp = array();
foreach ($ob->sendEncode(array_filter($sopts)) as $val) {
if (empty($opts['nowrap'])) {
/* Remove any existing linebreaks and wrap the line. */
$htext = $ob->name . ': ';
$val = ltrim(
substr(
wordwrap(
$htext . strtr(trim($val), array("\r" => '', "\n" => '')),
76,
$eol . ' '
),
strlen($htext)
)
);
}
$tmp[] = $val;
}
$ret[$ob->name] = (count($tmp) == 1)
? reset($tmp)
: $tmp;
}
return $ret;
}
/**
* Returns all headers concatenated into a single string.
*
* @param array $opts See toArray().
*
* @return string The headers in string format.
*/
public function toString(array $opts = array())
{
$eol = empty($opts['canonical'])
? $this->_eol
: "\r\n";
$text = '';
foreach ($this->toArray($opts) as $key => $val) {
foreach ((is_array($val) ? $val : array($val)) as $entry) {
$text .= $key . ': ' . $entry . $eol;
}
}
return $text . $eol;
}
/**
* Add/append/replace a header.
*
* @param string $header The header name.
* @param string $value The header value (UTF-8).
* @param array $opts DEPRECATED
*/
public function addHeader($header, $value, array $opts = array())
{
/* Existing header? Add to that object. */
$header = trim($header);
if ($hdr = $this[$header]) {
$hdr->setValue($value);
return;
}
$classname = $this->_getHeaderClassName($header);
try {
$ob = new $classname($header, $value);
} catch (InvalidArgumentException $e) {
/* Ignore an invalid header. */
return;
} catch (Horde_Mime_Exception $e) {
return;
}
switch ($classname) {
case 'Horde_Mime_Headers_ContentParam_ContentDisposition':
case 'Horde_Mime_Headers_ContentParam_ContentType':
/* BC */
if (!empty($opts['params'])) {
foreach ($opts['params'] as $key => $val) {
$ob[$key] = $val;
}
}
break;
}
$this->_headers[$ob->name] = $ob;
}
/**
* Add a Horde_Mime_Headers_Element object to the current header list.
*
* @since 2.5.0
*
* @param Horde_Mime_Headers_Element $ob Header object to add.
* @param boolean $check Check that the header and object
* type match?
*
* @throws InvalidArgumentException
*/
public function addHeaderOb(Horde_Mime_Headers_Element $ob, $check = false)
{
if ($check) {
$cname = $this->_getHeaderClassName($ob->name);
if (!($ob instanceof $cname)) {
throw new InvalidArgumentException(sprintf(
'Object is not correct class: %s',
$cname
));
}
}
/* Existing header? Add to that object. */
if ($hdr = $this[$ob->name]) {
$hdr->setValue($ob);
} else {
$this->_headers[$ob->name] = $ob;
}
}
/**
* Return the header class to use for a header name.
*
* @param string $header The header name.
*
* @return string The Horde_Mime_Headers_* class to use.
*/
protected function _getHeaderClassName($header)
{
if (empty(self::$_handlers)) {
$search = array(
'Horde_Mime_Headers_Element_Single',
'Horde_Mime_Headers_AddressesMulti',
'Horde_Mime_Headers_Addresses',
'Horde_Mime_Headers_ContentDescription',
'Horde_Mime_Headers_ContentId',
'Horde_Mime_Headers_ContentLanguage',
'Horde_Mime_Headers_ContentParam_ContentDisposition',
'Horde_Mime_Headers_ContentParam_ContentType',
'Horde_Mime_Headers_ContentTransferEncoding',
'Horde_Mime_Headers_Date',
'Horde_Mime_Headers_Identification',
'Horde_Mime_Headers_MessageId',
'Horde_Mime_Headers_Mime',
'Horde_Mime_Headers_MimeVersion',
'Horde_Mime_Headers_Received',
'Horde_Mime_Headers_Subject',
'Horde_Mime_Headers_UserAgent'
);
foreach ($search as $val) {
foreach ($val::getHandles() as $hdr) {
self::$_handlers[$hdr] = $val;
}
}
}
$header = Horde_String::lower($header);
return isset(self::$_handlers[$header])
? self::$_handlers[$header]
: 'Horde_Mime_Headers_Element_Multiple';
}
/**
* Get a header from the header array.
*
* @param string $header The header name.
*
* @return Horde_Mime_Headers_Element Element object, or null if not
* found.
*/
public function getHeader($header)
{
return $this[$header];
}
/**
* Remove a header from the header array.
*
* @param string $header The header name.
*/
public function removeHeader($header)
{
unset($this[$header]);
}
/* Static methods. */
/**
* Builds a Horde_Mime_Headers object from header text.
*
* @param mixed $text A text string (or, as of 2.3.0, a Horde_Stream
* object or stream resource) containing the headers.
*
* @return Horde_Mime_Headers A new Horde_Mime_Headers object.
*/
public static function parseHeaders($text)
{
$curr = null;
$headers = new Horde_Mime_Headers();
$hdr_list = array();
if ($text instanceof Horde_Stream) {
$stream = $text;
$stream->rewind();
} else {
$stream = new Horde_Stream_Temp();
$stream->add($text, true);
}
while (!$stream->eof()) {
if (!($val = rtrim($stream->getToChar("\n", false), "\r"))) {
break;
}
if ($curr && (($val[0] == ' ') || ($val[0] == "\t"))) {
$curr->text .= ' ' . ltrim($val);
} else {
$pos = strpos($val, ':');
$curr = new stdClass;
$curr->header = substr($val, 0, $pos);
$curr->text = ltrim(substr($val, $pos + 1));
$hdr_list[] = $curr;
}
}
foreach ($hdr_list as $val) {
/* When parsing, only keep the FIRST header seen for single value
* text-only headers, since newer headers generally are appended
* to the top of the message. */
if (!($ob = $headers[$val->header]) ||
!($ob instanceof Horde_Mime_Headers_Element_Single) ||
($ob instanceof Horde_Mime_Headers_Addresses)) {
$headers->addHeader($val->header, rtrim($val->text));
}
}
if (!($text instanceof Horde_Stream)) {
$stream->close();
}
return $headers;
}
/* Serializable methods. */
/**
* Serialization.
*
* @return string Serialized data.
*/
public function serialize()
{
$data = array(
// Serialized data ID.
self::VERSION,
$this->_headers->getArrayCopy(),
// TODO: BC
$this->_eol
);
return serialize($data);
}
/**
* Unserialization.
*
* @param string $data Serialized data.
*
* @throws Exception
*/
public function unserialize($data)
{
$data = @unserialize($data);
if (!is_array($data) ||
!isset($data[0]) ||
($data[0] != self::VERSION)) {
throw new Horde_Mime_Exception('Cache version change');
}
$this->_headers = new Horde_Support_CaseInsensitiveArray($data[1]);
// TODO: BC
$this->_eol = $data[2];
}
/* ArrayAccess methods. */
/**
* Does header exist?
*
* @since 2.5.0
*
* @param string $header Header name.
*
* @return boolean True if header exists.
*/
public function offsetExists($offset)
{
return isset($this->_headers[trim($offset)]);
}
/**
* Return header element object.
*
* @since 2.5.0
*
* @param string $header Header name.
*
* @return Horde_Mime_Headers_Element Element object, or null if not
* found.
*/
public function offsetGet($offset)
{
return $this->_headers[trim($offset)];
}
/**
* Store a header element object.
*
* @since 2.5.0
*
* @param string $offset Not used.
* @param Horde_Mime_Headers_Element $elt Header element.
*/
public function offsetSet($offset, $value)
{
$this->addHeaderOb($value);
}
/**
* Remove a header element object.
*
* @since 2.5.0
*
* @param string $offset Header name.
*/
public function offsetUnset($offset)
{
unset($this->_headers[trim($offset)]);
}
/* IteratorAggregate function */
/**
* @since 2.5.0
*/
public function getIterator()
{
return new ArrayIterator($this->_headers);
}
/* Deprecated functions */
/**
* Handle deprecated methods.
*/
public function __call($name, $arguments)
{
$d = new Horde_Mime_Headers_Deprecated($this);
return call_user_func_array(array($d, $name), $arguments);
}
/**
* Handle deprecated static methods.
*/
public static function __callStatic($name, $arguments)
{
$d = new Horde_Mime_Headers_Deprecated();
return call_user_func_array(array($d, $name), $arguments);
}
/**
* @deprecated
*/
protected $_eol = "\n";
/**
* @deprecated
*/
public function setEOL($eol)
{
$this->_eol = $eol;
}
/**
* @deprecated
*/
public function getEOL()
{
return $this->_eol;
}
/* Constants for getValue(). @deprecated */
const VALUE_STRING = 1;
const VALUE_BASE = 2;
const VALUE_PARAMS = 3;
}