Passed
Pull Request — master (#19)
by
unknown
02:08
created

Structure::getBoundary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/*
3
* File: Structure.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 Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
17
use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException;
18
19
/**
20
 * Class Structure
21
 *
22
 * @package Webklex\PHPIMAP
23
 */
24
class Structure {
25
26
    /**
27
     * Raw structure
28
     *
29
     * @var string $raw
30
     */
31
    public $raw = "";
32
33
    /**
34
     * @var Header $header
35
     */
36
    private $header = null;
37
38
    /**
39
     * Message type (if multipart or not)
40
     *
41
     * @var int $type
42
     */
43
    public $type = IMAP::MESSAGE_TYPE_TEXT;
44
45
    /**
46
     * All available parts
47
     *
48
     * @var Part[] $parts
49
     */
50
    public $parts = [];
51
52
    /**
53
     * Config holder
54
     *
55
     * @var array $config
56
     */
57
    protected $config = [];
58
59
    /**
60
     * Structure constructor.
61
     * @param $raw_structure
62
     * @param Header $header
63
     *
64
     * @throws MessageContentFetchingException
65
     * @throws InvalidMessageDateException
66
     */
67
    public function __construct($raw_structure, Header $header) {
68
        $this->raw = $raw_structure;
69
        $this->header = $header;
70
        $this->config = ClientManager::get('options');
71
        $this->parse();
72
    }
73
74
    /**
75
     * Parse the given raw structure
76
     *
77
     * @throws MessageContentFetchingException
78
     * @throws InvalidMessageDateException
79
     */
80
    protected function parse(){
81
        $this->findContentType();
82
        $this->parts = $this->find_parts();
83
    }
84
85
    /**
86
     * Determine the message content type
87
     */
88
    public function findContentType(){
89
        if(stripos($this->header->get("content-type"), 'multipart') === 0) {
90
            $this->type = IMAP::MESSAGE_TYPE_MULTIPART;
91
        }else{
92
            $this->type = IMAP::MESSAGE_TYPE_TEXT;
93
        }
94
    }
95
96
    /**
97
     * Determine the message content type
98
     */
99
    public function getBoundary(){
100
        return $this->header->find("/boundary\=\"(.*)\"/");
101
    }
102
103
    /**
104
     * Find all available parts
105
     *
106
     * @return array
107
     * @throws MessageContentFetchingException
108
     * @throws InvalidMessageDateException
109
     */
110
    public function find_parts(){
111
        if($this->type === IMAP::MESSAGE_TYPE_MULTIPART) {
112
            if (($boundary = $this->getBoundary()) === null)  {
113
                throw new MessageContentFetchingException("no content found", 0);
114
            }
115
116
            $boundaries = [
117
                $boundary
118
            ];
119
120
            if (preg_match("/boundary\=\"(.*)\"/", $this->raw, $match) == 1) {
121
                if(is_array($match[1])){
122
                    foreach($match[1] as $matched){
123
                        $boundaries[] = $matched;
124
                    }
125
                }else{
126
                    if(!empty($match[1])) {
127
                        $boundaries[] = $match[1];
128
                    }
129
                }
130
            }
131
132
            $raw_parts = explode( $boundaries[0], str_replace($boundaries, $boundaries[0], $this->raw) );
133
            $parts = [];
134
            $part_number = 0;
135
            foreach($raw_parts as $part) {
136
                $part = trim(rtrim($part));
137
                if ($part !== "--") {
138
                    $parts[] = new Part($part, null, $part_number);
139
                    $part_number++;
140
                }
141
            }
142
            return $parts;
143
        }
144
145
        return [new Part($this->raw, $this->header)];
146
    }
147
}