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\Stream; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\MimePart; |
10
|
|
|
use ZBateson\MailMimeParser\Message; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Factory class for PartStream objects and registration class for Message |
14
|
|
|
* handles. |
15
|
|
|
* |
16
|
|
|
* PartStreamRegistry is used for \ZBateson\MailMimeParser\MessageParser to |
17
|
|
|
* register Message stream handles for opening with PartStreams, and to open |
18
|
|
|
* file handles for specific mime parts of a message. The PartStreamRegistry |
19
|
|
|
* maintains a list of opened resources, closing them either when unregistering |
20
|
|
|
* a Message or on destruction. |
21
|
|
|
* |
22
|
|
|
* @author Zaahid Bateson |
23
|
|
|
*/ |
24
|
|
|
class PartStreamRegistry |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array Array of handles, with message IDs as keys. |
28
|
|
|
*/ |
29
|
|
|
private $registeredHandles; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array Array of PartStream handles with message IDs as keys. |
33
|
|
|
*/ |
34
|
|
|
private $registeredPartStreamHandles; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Registers an ID for the passed resource handle. |
38
|
|
|
* |
39
|
|
|
* @param string $id |
40
|
|
|
* @param resource $handle |
41
|
|
|
*/ |
42
|
|
|
public function register($id, $handle) |
43
|
|
|
{ |
44
|
|
|
if (!isset($this->registeredHandles[$id])) { |
45
|
|
|
$this->registeredHandles[$id] = $handle; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Unregisters the given message ID. |
51
|
|
|
* |
52
|
|
|
* @param string $id |
53
|
|
|
*/ |
54
|
|
|
public function unregister($id) |
55
|
|
|
{ |
56
|
|
|
unset($this->registeredHandles[$id], $this->registeredPartStreamHandles); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the resource handle with the passed $id. |
61
|
|
|
* |
62
|
|
|
* @param string $id |
63
|
|
|
* @return resource |
64
|
|
|
*/ |
65
|
|
|
public function get($id) |
66
|
|
|
{ |
67
|
|
|
if (!isset($this->registeredHandles[$id])) { |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
return $this->registeredHandles[$id]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Attaches a stream filter on the passed resource $handle for the part's |
75
|
|
|
* encoding. |
76
|
|
|
* |
77
|
|
|
* @param \ZBateson\MailMimeParser\MimePart $part |
78
|
|
|
* @param resource $handle |
79
|
|
|
*/ |
80
|
|
|
private function attachEncodingFilterToStream(MimePart $part, $handle) |
81
|
|
|
{ |
82
|
|
|
$encoding = strtolower($part->getHeaderValue('Content-Transfer-Encoding')); |
83
|
|
|
switch ($encoding) { |
84
|
|
|
case 'quoted-printable': |
85
|
|
|
stream_filter_append($handle, 'convert.quoted-printable-decode', STREAM_FILTER_READ); |
86
|
|
|
break; |
87
|
|
|
case 'base64': |
88
|
|
|
stream_filter_append($handle, 'convert.base64-decode', STREAM_FILTER_READ); |
89
|
|
|
break; |
90
|
|
|
case 'x-uuencode': |
91
|
|
|
stream_filter_append($handle, 'mailmimeparser-uudecode', STREAM_FILTER_READ); |
92
|
|
|
break; |
93
|
|
|
default: |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Attaches a mailmimeparser-encode stream filter based on the part's |
100
|
|
|
* defined charset. |
101
|
|
|
* |
102
|
|
|
* @param \ZBateson\MailMimeParser\MimePart $part |
103
|
|
|
* @param resource $handle |
104
|
|
|
*/ |
105
|
|
|
private function attachCharsetFilterToStream(MimePart $part, $handle) |
106
|
|
|
{ |
107
|
|
|
$contentType = strtolower($part->getHeaderValue('Content-Type', 'text/plain')); |
108
|
|
|
if (strpos($contentType, 'text/') === 0) { |
109
|
|
|
stream_filter_append( |
110
|
|
|
$handle, |
111
|
|
|
'mailmimeparser-encode.' . $part->getHeaderParameter('Content-Type', 'charset') |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Creates a part stream handle for the start and end position of the |
118
|
|
|
* message stream, and attaches it to the passed MimePart. |
119
|
|
|
* |
120
|
|
|
* @param MimePart $part |
121
|
|
|
* @param Message $message |
122
|
|
|
* @param int $start |
123
|
|
|
* @param int $end |
124
|
|
|
*/ |
125
|
|
|
public function attachPartStreamHandle(MimePart $part, Message $message, $start, $end) |
126
|
|
|
{ |
127
|
|
|
$id = $message->getObjectId(); |
128
|
|
|
if (empty($this->registeredHandles[$id])) { |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
$handle = fopen('mmp-mime-message://' . $id . '?start=' . |
132
|
|
|
$start . '&end=' . $end, 'r'); |
133
|
|
|
|
134
|
|
|
$this->attachEncodingFilterToStream($part, $handle); |
135
|
|
|
$this->attachCharsetFilterToStream($part, $handle); |
136
|
|
|
$this->registeredPartStreamHandles[$id] = $handle; |
137
|
|
|
$part->attachContentResourceHandle($handle); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|