forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructure.php
151 lines (132 loc) · 3.6 KB
/
Structure.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
* File: Structure.php
* Category: -
* Author: M.Goldenbaum
* Created: 17.09.20 20:38
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP;
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException;
/**
* Class Structure
*
* @package Webklex\PHPIMAP
*/
class Structure {
/**
* Raw structure
*
* @var string $raw
*/
public $raw = "";
/**
* @var Header $header
*/
private $header = null;
/**
* Message type (if multipart or not)
*
* @var int $type
*/
public $type = IMAP::MESSAGE_TYPE_TEXT;
/**
* All available parts
*
* @var Part[] $parts
*/
public $parts = [];
/**
* Config holder
*
* @var array $config
*/
protected $config = [];
/**
* Structure constructor.
* @param $raw_structure
* @param Header $header
*
* @throws MessageContentFetchingException
* @throws InvalidMessageDateException
*/
public function __construct($raw_structure, Header $header) {
$this->raw = $raw_structure;
$this->header = $header;
$this->config = ClientManager::get('options');
$this->parse();
}
/**
* Parse the given raw structure
*
* @throws MessageContentFetchingException
* @throws InvalidMessageDateException
*/
protected function parse(){
$this->findContentType();
$this->parts = $this->find_parts();
}
/**
* Determine the message content type
*/
public function findContentType(){
$content_type = $this->header->get("content-type");
$content_type = (is_array($content_type)) ? implode(' ', $content_type) : $content_type;
if(stripos($content_type, 'multipart') === 0) {
$this->type = IMAP::MESSAGE_TYPE_MULTIPART;
}else{
$this->type = IMAP::MESSAGE_TYPE_TEXT;
}
}
/**
* Determine the message content type
*/
public function getBoundary(){
$boundary = $this->header->find("/boundary\=\"?(.*)\"?/");
return str_replace('"', '', $boundary);
}
/**
* Find all available parts
*
* @return array
* @throws MessageContentFetchingException
* @throws InvalidMessageDateException
*/
public function find_parts(){
if($this->type === IMAP::MESSAGE_TYPE_MULTIPART) {
if (($boundary = $this->getBoundary()) === null) {
throw new MessageContentFetchingException("no content found", 0);
}
$boundaries = [
$boundary
];
if (preg_match("/boundary\=\"?(.*)\"?/", $this->raw, $match) == 1) {
if(is_array($match[1])){
foreach($match[1] as $matched){
$boundaries[] = str_replace('"', '', $matched);
}
}else{
if(!empty($match[1])) {
$boundaries[] = str_replace('"', '', $match[1]);
}
}
}
$raw_parts = explode( $boundaries[0], str_replace($boundaries, $boundaries[0], $this->raw) );
$parts = [];
$part_number = 0;
foreach($raw_parts as $part) {
$part = trim(rtrim($part));
if ($part !== "--") {
$parts[] = new Part($part, null, $part_number);
$part_number++;
}
}
return $parts;
}
return [new Part($this->raw, $this->header)];
}
}