Passed
Push — 1.0.0 ( 5c7ec9...fcaf32 )
by Zaahid
03:19
created

MimeLiteralPart   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 176
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A decodeMime() 0 13 2
A decodeMatchedEntity() 0 13 2
A decodeSplitPart() 0 9 2
A ignoreSpacesBefore() 0 4 1
A ignoreSpacesAfter() 0 4 1
A addToLanguage() 0 7 1
A getLanguageArray() 0 4 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\Header\Part;
8
9
use ZBateson\MailMimeParser\Util\CharsetConverter;
10
11
/**
12
 * Represents a single mime header part token, with the possibility of it being
13
 * MIME-Encoded as per RFC-2047.
14
 * 
15
 * MimeLiteralPart automatically decodes the value if it's encoded.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
class MimeLiteralPart extends LiteralPart
20
{
21
    /**
22
     * @var string regex pattern matching a mime-encoded part
23
     */
24
    const MIME_PART_PATTERN = '=\?[A-Za-z\-_0-9\*]+\?[QBqb]\?[^\?]+\?=';
25
    
26
    /**
27
     * @var bool set to true to ignore spaces before this part
28
     */
29
    protected $canIgnoreSpacesBefore = false;
30
    
31
    /**
32
     * @var bool set to true to ignore spaces after this part
33
     */
34
    protected $canIgnoreSpacesAfter = false;
35
    
36
    /**
37
     * @var array maintains an array mapping rfc1766 language tags to parts of
38
     * text in the value.
39
     * 
40
     * Each array element is an array containing two elements, one with key
41
     * 'lang', and another with key 'value'.
42
     */
43
    protected $languages = [];
44
    
45
    /**
46
     * Decoding the passed token value if it's mime-encoded and assigns the
47
     * decoded value to a member variable. Sets canIgnoreSpacesBefore and
48
     * canIgnoreSpacesAfter.
49
     * 
50
     * @param CharsetConverter $charsetConverter
51
     * @param string $token
52
     */
53 10
    public function __construct(CharsetConverter $charsetConverter, $token)
54
    {
55 10
        parent::__construct($charsetConverter);
56 10
        $this->value = $this->decodeMime($token);
57
        // preg_match returns int
58 10
        $pattern = self::MIME_PART_PATTERN;
59 10
        $this->canIgnoreSpacesBefore = (bool) preg_match("/^\s*{$pattern}/", $token);
60 10
        $this->canIgnoreSpacesAfter = (bool) preg_match("/{$pattern}\s*\$/", $token);
61 10
    }
62
    
63
    /**
64
     * Finds and replaces mime parts with their values.
65
     * 
66
     * The method splits the token value into an array on mime-part-patterns,
67
     * either replacing a mime part with its value by calling iconv_mime_decode
68
     * or converts the encoding on the text part by calling convertEncoding.
69
     * 
70
     * @param string $value
71
     * @return string
72
     */
73 10
    protected function decodeMime($value)
74
    {
75 10
        $pattern = self::MIME_PART_PATTERN;
76
        // remove whitespace between two adjacent mime encoded parts
77 10
        $value = preg_replace("/($pattern)\\s+(?=$pattern)/", '$1', $value);
78
        // with PREG_SPLIT_DELIM_CAPTURE, matched and unmatched parts are returned
79 10
        $aMimeParts = preg_split("/($pattern)/", $value, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
80 10
        $ret = '';
81 10
        foreach ($aMimeParts as $entity) {
82 10
            $ret .= $this->decodeSplitPart($entity);
83 10
        }
84 10
        return $ret;
85
    }
86
    
87
    /**
88
     * Decodes a matched mime entity part into a string and returns it, after
89
     * adding the string into the languages array.
90
     * 
91
     * @param string[] $matches
92
     * @return string
93
     */
94 8
    private function decodeMatchedEntity($matches)
95
    {
96 8
        $body = $matches[4];
97 8
        if (strtoupper($matches[3]) === 'Q') {
98 8
            $body = quoted_printable_decode(str_replace('_', '=20', $body));
99 8
        } else {
100 1
            $body = base64_decode($body);
101
        }
102 8
        $language = $matches[2];
103 8
        $decoded = $this->convertEncoding($body, $matches[1], true);
104 8
        $this->addToLanguage($decoded, $language);
105 8
        return $decoded;
106
    }
107
    
108
    /**
109
     * Decodes a single mime-encoded entity.
110
     * 
111
     * Unfortunately, mb_decode_header fails for many charsets on PHP 5.4 and
112
     * PHP 5.5 (even if they're listed as supported).  iconv_mime_decode doesn't
113
     * support all charsets.
114
     * 
115
     * Parsing out the charset and body of the encoded entity seems to be the
116
     * way to go to support the most charsets.
117
     * 
118
     * @param string $entity
119
     * @return string
120
     */
121 10
    private function decodeSplitPart($entity)
122
    {
123 10
        if (preg_match("/^=\?([A-Za-z\-_0-9]+)\*?([A-Za-z\-_0-9]+)?\?([QBqb])\?([^\?]+)\?=$/", $entity, $matches)) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /^=\?([A-Za-z\-_0-9]+)\*...([QBqb])\?([^\?]+)\?=$/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
124 8
            return $this->decodeMatchedEntity($matches);
125
        }
126 7
        $decoded = $this->convertEncoding($entity);
127 7
        $this->addToLanguage($decoded);
128 7
        return $decoded;
129
    }
130
    
131
    /**
132
     * Returns true if spaces before this part should be ignored.
133
     * 
134
     * Overridden to return $this->canIgnoreSpacesBefore which is setup in the
135
     * constructor.
136
     * 
137
     * @return bool
138
     */
139 3
    public function ignoreSpacesBefore()
140
    {
141 3
        return $this->canIgnoreSpacesBefore;
142
    }
143
    
144
    /**
145
     * Returns true if spaces before this part should be ignored.
146
     * 
147
     * Overridden to return $this->canIgnoreSpacesAfter which is setup in the
148
     * constructor.
149
     * 
150
     * @return bool
151
     */
152 3
    public function ignoreSpacesAfter()
153
    {
154 3
        return $this->canIgnoreSpacesAfter;
155
    }
156
    
157
    /**
158
     * Adds the passed part into the languages array with the given language.
159
     * 
160
     * @param string $part
161
     * @param string|null $language
162
     */
163 10
    protected function addToLanguage($part, $language = null)
164
    {
165 10
        $this->languages[] = [
166 10
            'lang' => $language,
167
            'value' => $part
168 10
        ];
169 10
    }
170
    
171
    /**
172
     * Returns an array of parts mapped to languages in the header value, for
173
     * instance the string:
174
     * 
175
     * 'Hello and =?UTF-8*fr-be?Q?bonjour_?= =?UTF-8*it?Q?mi amici?=. Welcome!'
176
     * 
177
     * Would be mapped in the returned array as follows:
178
     * 
179
     * ```php
180
     * [
181
     *     0 => [ 'lang' => null, 'value' => 'Hello and ' ],
182
     *     1 => [ 'lang' => 'fr-be', 'value' => 'bonjour ' ],
183
     *     3 => [ 'lang' => 'it', 'value' => 'mi amici' ],
184
     *     4 => [ 'lang' => null, 'value' => ' Weolcome!' ]
185
     * ]
186
     * ```
187
     * 
188
     * @return string[][]
189
     */
190 3
    public function getLanguageArray()
191
    {
192 3
        return $this->languages;
193
    }
194
}
195