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