Passed
Push — master ( 916c78...351345 )
by Malte
01:55
created

Part::parseDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/*
3
* File: Part.php
4
* Category: -
5
* Author: M.Goldenbaum
6
* Created: 17.09.20 20:38
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\PHPIMAP;
14
15
16
use Carbon\Carbon;
17
use Illuminate\Support\Str;
18
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
19
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
20
21
/**
22
 * Class Part
23
 *
24
 * @package Webklex\PHPIMAP
25
 */
26
class Part {
27
28
    /**
29
     * @var string $raw
30
     */
31
    public $raw = "";
32
33
    /**
34
     * @var int $type
35
     */
36
    public $type = IMAP::MESSAGE_TYPE_TEXT;
37
38
    /**
39
     * @var string $content
40
     */
41
    public $content = "";
42
43
    /**
44
     * @var string $subtype
45
     */
46
    public $subtype = null;
47
48
    /**
49
     * @var string $charset
50
     */
51
    public $charset = "utf-8";
52
53
    /**
54
     * @var int $encoding
55
     */
56
    public $encoding = IMAP::MESSAGE_ENC_OTHER;
57
58
    /**
59
     * @var boolean $ifdisposition
60
     */
61
    public $ifdisposition = false;
62
63
    /**
64
     * @var string $disposition
65
     */
66
    public $disposition = null;
67
68
    /**
69
     * @var boolean $ifdescription
70
     */
71
    public $ifdescription = false;
72
73
    /**
74
     * @var string $description
75
     */
76
    public $description = null;
77
78
    /**
79
     * @var string $filename
80
     */
81
    public $filename = null;
82
83
    /**
84
     * @var string $name
85
     */
86
    public $name = null;
87
88
    /**
89
     * @var string $id
90
     */
91
    public $id = null;
92
93
    /**
94
     * @var integer $bytes
95
     */
96
    public $bytes = null;
97
98
    /**
99
     * @var Header $header
100
     */
101
    private $header = null;
102
103
    /**
104
     * Part constructor.
105
     * @param $raw_part
106
     * @param Header $header
107
     *
108
     * @throws InvalidMessageDateException
109
     */
110
    public function __construct($raw_part, $header = null) {
111
        $this->raw = $raw_part;
112
        $this->header = $header;
113
        $this->parse();
114
    }
115
116
    /**
117
     * Parse the raw parts
118
     *
119
     * @throws InvalidMessageDateException
120
     */
121
    protected function parse(){
122
        if ($this->header === null) {
123
            $body = $this->findHeaders();
124
        }else{
125
            $body = $this->raw;
126
        }
127
128
        $this->parseSubtype();
129
        $this->parseDisposition();
130
        $this->parseDescription();
131
        $this->parseEncoding();
132
133
        $this->charset = $this->header->get("charset");
134
        $this->name = $this->header->get("name");
135
        $this->filename = $this->header->get("filename");
136
        $this->id = $this->header->get("id");
137
138
        $this->content = trim(rtrim($body));
139
        $this->bytes = strlen($this->content);
140
    }
141
142
    /**
143
     * Find all available headers and return the left over body segment
144
     *
145
     * @return string
146
     * @throws InvalidMessageDateException
147
     */
148
    private function findHeaders(){
149
        $body = $this->raw;
150
        while (($pos = strpos($body, "\r\n")) > 0) {
151
            $body = substr($body, $pos + 2);
152
        }
153
        $headers = substr($this->raw, 0, strlen($body) * -1);
154
        $body = substr($body, 0, -2);
155
156
        $this->header = new Header($headers);
157
158
        return (string) $body;
159
    }
160
161
    private function parseSubtype(){
162
        $content_type = $this->header->get("content-type");
163
        if (($pos = strpos($content_type, "/")) !== false){
164
            $this->subtype = substr($content_type, $pos + 1);
165
        }
166
    }
167
168
    private function parseDisposition(){
169
        $content_disposition = $this->header->get("content-disposition");
170
        if($content_disposition !== null) {
171
            $this->ifdisposition = true;
172
            $this->disposition = $content_disposition;
173
        }
174
    }
175
176
    private function parseDescription(){
177
        $content_description = $this->header->get("content-description");
178
        if($content_description !== null) {
179
            $this->ifdescription = true;
180
            $this->description = $content_description;
181
        }
182
    }
183
184
    private function parseEncoding(){
185
        $encoding = $this->header->get("content-transfer-encoding");
186
        if($encoding !== null) {
187
            switch (strtolower($encoding)) {
188
                case "quoted-printable":
189
                    $this->encoding = IMAP::MESSAGE_ENC_QUOTED_PRINTABLE;
190
                    break;
191
                case "base64":
192
                    $this->encoding = IMAP::MESSAGE_ENC_BASE64;
193
                    break;
194
                case "7bit":
195
                    $this->encoding = IMAP::MESSAGE_ENC_7BIT;
196
                    break;
197
                case "8bit":
198
                    $this->encoding = IMAP::MESSAGE_ENC_8BIT;
199
                    break;
200
                case "binary":
201
                    $this->encoding = IMAP::MESSAGE_ENC_BINARY;
202
                    break;
203
                default:
204
                    $this->encoding = IMAP::MESSAGE_ENC_OTHER;
205
                    break;
206
207
            }
208
        }
209
    }
210
211
}