|
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\Parser\Part\UUEncodedPartHeaderContainerFactory; |
|
10
|
|
|
use ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy; |
|
11
|
|
|
use ZBateson\MailMimeParser\Parser\Proxy\ParserNonMimeMessageProxy; |
|
12
|
|
|
use ZBateson\MailMimeParser\Parser\Proxy\ParserNonMimeMessageProxyFactory; |
|
13
|
|
|
use ZBateson\MailMimeParser\Parser\Proxy\ParserPartProxy; |
|
14
|
|
|
use ZBateson\MailMimeParser\Parser\Proxy\ParserUUEncodedPartProxy; |
|
15
|
|
|
use ZBateson\MailMimeParser\Parser\Proxy\ParserUUEncodedPartProxyFactory; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Parses content for non-mime messages and uu-encoded child parts. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Zaahid Bateson |
|
21
|
|
|
*/ |
|
22
|
|
|
class NonMimeParser extends AbstractParser |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var UUEncodedPartHeaderContainerFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $partHeaderContainerFactory; |
|
28
|
|
|
|
|
29
|
9 |
|
public function __construct( |
|
30
|
|
|
ParserNonMimeMessageProxyFactory $parserNonMimeMessageProxyFactory, |
|
31
|
|
|
ParserUUEncodedPartProxyFactory $parserUuEncodedPartProxyFactory, |
|
32
|
|
|
PartBuilderFactory $partBuilderFactory, |
|
33
|
|
|
UUEncodedPartHeaderContainerFactory $uuEncodedPartHeaderContainerFactory |
|
34
|
|
|
) { |
|
35
|
9 |
|
parent::__construct($parserNonMimeMessageProxyFactory, $parserUuEncodedPartProxyFactory, $partBuilderFactory); |
|
36
|
9 |
|
$this->partHeaderContainerFactory = $uuEncodedPartHeaderContainerFactory; |
|
37
|
9 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Always returns true, and should therefore be the last parser reached by |
|
41
|
|
|
* a ParserManager. |
|
42
|
|
|
* |
|
43
|
|
|
* @param PartBuilder $part |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function canParse(PartBuilder $part) |
|
47
|
|
|
{ |
|
48
|
6 |
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Creates a UUEncodedPartHeaderContainer attached to a PartBuilder, and |
|
53
|
|
|
* calls $this->parserManager->createParserProxyFor(). |
|
54
|
|
|
* |
|
55
|
|
|
* It also sets the PartBuilder's stream part start pos and content start |
|
56
|
|
|
* pos to that of $parent->getNextParStart() (since a 'begin' line is read |
|
57
|
|
|
* prior to another child being created, see parseNextPart()). |
|
58
|
|
|
* |
|
59
|
|
|
* @param ParserNonMimeMessageProxy $parent |
|
60
|
|
|
* @return ParserPartProxy |
|
61
|
|
|
*/ |
|
62
|
4 |
|
private function createPart(ParserNonMimeMessageProxy $parent) |
|
63
|
|
|
{ |
|
64
|
4 |
|
$hc = $this->partHeaderContainerFactory->newInstance($parent->getNextPartMode(), $parent->getNextPartFilename()); |
|
65
|
4 |
|
$pb = $this->partBuilderFactory->newChildPartBuilder($hc, $parent); |
|
66
|
4 |
|
$proxy = $this->parserManager->createParserProxyFor($pb); |
|
67
|
4 |
|
$pb->setStreamPartStartPos($parent->getNextPartStart()); |
|
68
|
4 |
|
$pb->setStreamContentStartPos($parent->getNextPartStart()); |
|
69
|
4 |
|
return $proxy; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Reads content from the passed ParserPartProxy's stream till a uu-encoded |
|
74
|
|
|
* 'begin' line is found, setting $proxy->setStreamPartContentAndEndPos() to |
|
75
|
|
|
* the last byte read before the begin line. |
|
76
|
|
|
* |
|
77
|
|
|
* @param ParserNonMimeMessageProxy|ParserUUEncodedPartProxy $proxy |
|
78
|
|
|
*/ |
|
79
|
7 |
|
private function parseNextPart(ParserPartProxy $proxy) |
|
80
|
|
|
{ |
|
81
|
7 |
|
$handle = $proxy->getMessageResourceHandle(); |
|
82
|
7 |
|
while (!feof($handle)) { |
|
83
|
7 |
|
$start = ftell($handle); |
|
84
|
7 |
|
$line = trim(MessageParser::readLine($handle)); |
|
85
|
7 |
|
if (preg_match('/^begin ([0-7]{3}) (.*)$/', $line, $matches)) { |
|
86
|
4 |
|
$proxy->setNextPartStart($start); |
|
|
|
|
|
|
87
|
4 |
|
$proxy->setNextPartMode($matches[1]); |
|
|
|
|
|
|
88
|
4 |
|
$proxy->setNextPartFilename($matches[2]); |
|
|
|
|
|
|
89
|
4 |
|
return; |
|
90
|
|
|
} |
|
91
|
7 |
|
$proxy->setStreamPartAndContentEndPos(ftell($handle)); |
|
92
|
|
|
} |
|
93
|
6 |
|
} |
|
94
|
|
|
|
|
95
|
8 |
|
public function parseContent(ParserPartProxy $proxy) |
|
96
|
|
|
{ |
|
97
|
8 |
|
$handle = $proxy->getMessageResourceHandle(); |
|
98
|
8 |
|
if ($proxy->getNextPartStart() !== null || feof($handle)) { |
|
|
|
|
|
|
99
|
2 |
|
return; |
|
100
|
|
|
} |
|
101
|
7 |
|
if ($proxy->getStreamContentStartPos() === null) { |
|
|
|
|
|
|
102
|
7 |
|
$proxy->setStreamContentStartPos(ftell($handle)); |
|
103
|
|
|
} |
|
104
|
7 |
|
$this->parseNextPart($proxy); |
|
105
|
7 |
|
} |
|
106
|
|
|
|
|
107
|
6 |
|
public function parseNextChild(ParserMimePartProxy $proxy) |
|
108
|
|
|
{ |
|
109
|
6 |
|
$handle = $proxy->getMessageResourceHandle(); |
|
110
|
6 |
|
if ($proxy->getNextPartStart() === null || feof($handle)) { |
|
|
|
|
|
|
111
|
5 |
|
return null; |
|
112
|
|
|
} |
|
113
|
4 |
|
$child = $this->createPart( |
|
114
|
4 |
|
$proxy |
|
115
|
|
|
); |
|
116
|
4 |
|
$proxy->clearNextPart(); |
|
|
|
|
|
|
117
|
4 |
|
return $child; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|