Completed
Push — master ( 6eca6d...a3ed97 )
by Zaahid
02:29
created

Base64DecodeStreamFilter::filterLine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
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
 * @author Zaahid Bateson
13
 */
14
class Base64DecodeStreamFilter extends php_user_filter
15
{
16
    /**
17
     * Name used when registering with stream_filter_register.
18
     */
19
    const STREAM_FILTER_NAME = 'convert.base64-decode';
20
    
21
    /**
22
     * @var string Leftovers from the last incomplete line that was parsed, to
23
     *      be prepended to the next line read.
24
     */
25
    private $leftover = '';
26
    
27
    /**
28
     * Returns an array of complete lines (including line endings) from the 
29
     * passed $bucket object.
30
     * 
31
     * If the last line on $bucket is incomplete, it's assigned to
32
     * $this->leftover and prepended to the first element of the first line in
33
     * the next call to getLines.
34
     * 
35
     * @param object $bucket
36
     * @return string[]
37
     */
38
    private function getRawBytes($bucket)
39
    {
40
        $raw = preg_replace('/\s+/', '', $bucket->data);
41
        if (!empty($this->leftover)) {
42
            $raw = $this->leftover . $raw;
43
            $this->leftover = '';
44
        }
45
        $nLeftover = strlen($raw) % 3;
46
        if ($nLeftover !== 0) {
47
            $this->leftover = substr($nLeftover, -$nLeftover);
48
        }
49
        return $raw;
50
    }
51
52
    /**
53
     * Filter implementation converts encoding before returning PSFS_PASS_ON.
54
     * 
55
     * @param resource $in
56
     * @param resource $out
57
     * @param int $consumed
58
     * @param bool $closing
59
     * @return int
60
     */
61
    public function filter($in, $out, &$consumed, $closing)
62
    {
63
        while ($bucket = stream_bucket_make_writeable($in)) {
64
            $bytes = $this->getRawBytes($bucket);
65
            $nConsumed = strlen($bucket->data);
66
            if ($this->leftover !== '') {
67
                $nConsumed -= $nConsumed - strlen(rtrim($bucket->data));
68
                $nConsumed -= strlen($this->leftover);
69
            }
70
            $consumed += $nConsumed;
71
            $converted = base64_decode($bytes);
72
            
73
            // $this->stream is undocumented.  It was found looking at HHVM's source code
74
            // for its convert.iconv.* implementation in ConvertIconFilter and explained
75
            // somewhat in this StackOverflow page: http://stackoverflow.com/a/31132646/335059
76
            // declaring a member variable called 'stream' breaks the PHP implementation (5.5.9
77
            // at least).
78
            stream_bucket_append($out, stream_bucket_new($this->stream, $converted));
1 ignored issue
show
Bug introduced by
The property stream does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
79
        }
80
        return PSFS_PASS_ON;
81
    }
82
}
83