|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* Idea taken from HHVM's ConvertFilter and coded to avoid requiring a |
|
6
|
|
|
* different license and because I don't want to deal with legalities. |
|
7
|
|
|
* |
|
8
|
|
|
* Unfortunately the HHVM version seems to fail for larger base64-encoded files |
|
9
|
|
|
* (both encoding and decoding). Once the stream needs to be buffered, if it |
|
10
|
|
|
* isn't buffered on a 3-byte chunk for encoding, or 4 byte-chunk for decoding, |
|
11
|
|
|
* it won't make sense. Currently it is only used for quoted-printable encoding |
|
12
|
|
|
* and decoding. |
|
13
|
|
|
* |
|
14
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
15
|
|
|
*/ |
|
16
|
|
|
namespace ZBateson\MailMimeParser\Stream; |
|
17
|
|
|
|
|
18
|
|
|
use php_user_filter; |
|
19
|
|
|
|
|
20
|
|
|
class ConvertStreamFilter extends php_user_filter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Name used when registering with stream_filter_register. |
|
24
|
|
|
*/ |
|
25
|
|
|
const STREAM_FILTER_NAME = 'convert.*'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string function to call for encoding/decoding |
|
29
|
|
|
*/ |
|
30
|
|
|
private $fnFilterName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Sets up which function should be called. |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function onCreate() |
|
38
|
|
|
{ |
|
39
|
|
|
// strip off 'convert.' |
|
40
|
|
|
$name = substr($this->filtername, 8); |
|
41
|
|
|
$this->isEncode = false; |
|
|
|
|
|
|
42
|
|
|
$aFilters = [ |
|
43
|
|
|
'quoted-printable-encode' => true, |
|
44
|
|
|
'quoted-printable-decode' => true, |
|
45
|
|
|
]; |
|
46
|
|
|
if (!isset($aFilters[$name])) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
$this->fnFilterName = str_replace('-', '_', $name); |
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Filter implementation converts calls the relevant encode/decode filter |
|
55
|
|
|
* and chunk_split if needed, before returning PSFS_PASS_ON. |
|
56
|
|
|
* |
|
57
|
|
|
* @param resource $in |
|
58
|
|
|
* @param resource $out |
|
59
|
|
|
* @param int $consumed |
|
60
|
|
|
* @param bool $closing |
|
61
|
|
|
* @return int |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public function filter($in, $out, &$consumed, $closing) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
while ($bucket = stream_bucket_make_writeable($in)) { |
|
66
|
|
|
$data = call_user_func($this->fnFilterName, $bucket->data); |
|
67
|
|
|
stream_bucket_append($out, stream_bucket_new($this->stream, $data)); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
return PSFS_PASS_ON; |
|
70
|
|
|
} |
|
71
|
|
|
} |
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: