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