1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
6
|
|
|
*/ |
7
|
|
|
namespace ZBateson\MailMimeParser\Parser; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Message\PartHeaderContainer; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Reads headers from the input stream if {@see PartBuilder::canHaveHeaders} |
13
|
|
|
* returns true. isSupported returns true regardless (which is why it's |
14
|
|
|
* "optional"), which ensures sub-parsers are called. |
15
|
|
|
* |
16
|
|
|
* @author Zaahid Bateson |
17
|
|
|
*/ |
18
|
|
|
class HeaderParser |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Ensures the header isn't empty and contains a colon separator character, |
22
|
|
|
* then splits it and calls $partBuilder->addHeader. |
23
|
|
|
* |
24
|
|
|
* @param string $header |
25
|
|
|
* @param PartBuilder $partBuilder |
26
|
|
|
*/ |
27
|
|
|
private function addRawHeaderToPart($header, PartHeaderContainer $headerContainer) |
28
|
|
|
{ |
29
|
|
|
if ($header !== '' && strpos($header, ':') !== false) { |
30
|
|
|
$a = explode(':', $header, 2); |
31
|
|
|
$headerContainer->add($a[0], trim($a[1])); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Reads header lines up to an empty line, adding them to the passed |
37
|
|
|
* $headerContainer. |
38
|
|
|
* |
39
|
|
|
* @param PartBuilder $partBuilder the current part to add headers to |
40
|
|
|
*/ |
41
|
|
|
public function parse(PartBuilder $partBuilder, PartHeaderContainer $container) |
42
|
|
|
{ |
43
|
|
|
$handle = $partBuilder->getMessageResourceHandle(); |
44
|
|
|
$header = ''; |
45
|
|
|
do { |
46
|
|
|
$line = MessageParser::readLine($handle); |
47
|
|
|
if ($line === false || $line === '' || $line[0] !== "\t" && $line[0] !== ' ') { |
48
|
|
|
$this->addRawHeaderToPart($header, $container); |
49
|
|
|
$header = ''; |
50
|
|
|
} else { |
51
|
|
|
$line = "\r\n" . $line; |
52
|
|
|
} |
53
|
|
|
$header .= rtrim($line, "\r\n"); |
54
|
|
|
} while ($header !== ''); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|