Completed
Push — master ( 147214...53e4d6 )
by Zaahid
09:05
created

UUEncodeStreamFilter::getLines()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
ccs 0
cts 16
cp 0
rs 9.4285
cc 3
eloc 13
nc 4
nop 1
crap 12
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));
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...
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