|
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 php_user_filter; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Stream filter converts binary streams to uuencoded text. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Zaahid Bateson |
|
15
|
|
|
*/ |
|
16
|
|
|
class UUEncodeStreamFilter extends php_user_filter |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Name used when registering with stream_filter_register. |
|
20
|
|
|
*/ |
|
21
|
|
|
const STREAM_FILTER_NAME = 'mailmimeparser-uuencode'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var StreamLeftover |
|
25
|
|
|
*/ |
|
26
|
|
|
private $leftovers; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
private $headerWritten = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* UUEncodes the passed $data string and appends it to $out. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $data data to convert |
|
37
|
|
|
* @param resource $out output bucket stream |
|
38
|
|
|
*/ |
|
39
|
7 |
|
private function convertAndAppend($data, $out) |
|
40
|
|
|
{ |
|
41
|
7 |
|
$converted = convert_uuencode($data); |
|
42
|
7 |
|
$cleaned = rtrim(substr(rtrim($converted), 0, -1)); // remove end line ` character |
|
43
|
7 |
|
if (empty($cleaned)) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
7 |
|
$cleaned = "\r\n" . $cleaned; |
|
47
|
7 |
|
stream_bucket_append($out, stream_bucket_new($this->stream, $cleaned)); |
|
|
|
|
|
|
48
|
7 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Writes out the header for a uuencoded part to the passed stream resource |
|
52
|
|
|
* handle. |
|
53
|
|
|
* |
|
54
|
|
|
* @param resource $out |
|
55
|
|
|
*/ |
|
56
|
7 |
|
private function writeUUEncodingHeader($out) |
|
57
|
|
|
{ |
|
58
|
7 |
|
$data = 'begin 666 '; |
|
59
|
7 |
|
if (isset($this->params['filename'])) { |
|
60
|
7 |
|
$data .= $this->params['filename']; |
|
61
|
7 |
|
} else { |
|
62
|
|
|
$data .= 'null'; |
|
63
|
|
|
} |
|
64
|
7 |
|
stream_bucket_append($out, stream_bucket_new($this->stream, $data)); |
|
65
|
7 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the footer for a uuencoded part. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
7 |
|
private function getUUEncodingFooter() |
|
73
|
|
|
{ |
|
74
|
7 |
|
return "\r\n`\r\nend\r\n\r\n"; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Reads from the input bucket stream, converts, and writes the uuencoded |
|
79
|
|
|
* stream to $out. |
|
80
|
|
|
* |
|
81
|
|
|
* @param resource $in input bucket stream |
|
82
|
|
|
* @param resource $out output bucket stream |
|
83
|
|
|
* @param int $consumed incremented by number of bytes read from $in |
|
84
|
|
|
*/ |
|
85
|
7 |
|
private function readAndConvert($in, $out, &$consumed) |
|
86
|
|
|
{ |
|
87
|
7 |
|
while ($bucket = stream_bucket_make_writeable($in)) { |
|
88
|
7 |
|
$data = $this->leftovers->value . $bucket->data; |
|
89
|
7 |
|
if (!$this->headerWritten) { |
|
90
|
7 |
|
$this->writeUUEncodingHeader($out); |
|
91
|
7 |
|
$this->headerWritten = true; |
|
92
|
7 |
|
} |
|
93
|
7 |
|
$consumed += $bucket->datalen; |
|
94
|
7 |
|
$nRemain = strlen($data) % 45; |
|
95
|
7 |
|
$toConvert = $data; |
|
96
|
7 |
|
if ($nRemain === 0) { |
|
97
|
1 |
|
$this->leftovers->value = ''; |
|
98
|
1 |
|
$this->leftovers->encodedValue = $this->getUUEncodingFooter(); |
|
99
|
1 |
|
} else { |
|
100
|
7 |
|
$this->leftovers->value = substr($data, -$nRemain); |
|
101
|
7 |
|
$this->leftovers->encodedValue = "\r\n" . |
|
102
|
7 |
|
rtrim(substr(rtrim(convert_uuencode($this->leftovers->value)), 0, -1)) |
|
103
|
7 |
|
. $this->getUUEncodingFooter(); |
|
104
|
7 |
|
$toConvert = substr($data, 0, -$nRemain); |
|
105
|
|
|
} |
|
106
|
7 |
|
$this->convertAndAppend($toConvert, $out); |
|
107
|
7 |
|
} |
|
108
|
7 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Filter implementation converts encoding before returning PSFS_PASS_ON. |
|
112
|
|
|
* |
|
113
|
|
|
* @param resource $in |
|
114
|
|
|
* @param resource $out |
|
115
|
|
|
* @param int $consumed |
|
116
|
|
|
* @param bool $closing |
|
117
|
|
|
* @return int |
|
118
|
|
|
*/ |
|
119
|
7 |
|
public function filter($in, $out, &$consumed, $closing) |
|
120
|
|
|
{ |
|
121
|
7 |
|
$this->readAndConvert($in, $out, $consumed); |
|
122
|
7 |
|
return PSFS_PASS_ON; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets up the leftovers object |
|
127
|
|
|
*/ |
|
128
|
7 |
|
public function onCreate() |
|
129
|
|
|
{ |
|
130
|
7 |
|
if (isset($this->params['leftovers'])) { |
|
131
|
7 |
|
$this->leftovers = $this->params['leftovers']; |
|
132
|
7 |
|
} |
|
133
|
7 |
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: