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