| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP; |
| 14 |
|
| 15 |
|
| 16 |
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; |
| 17 |
use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class Structure { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public string $raw = ""; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private Header $header; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public int $type = IMAP::MESSAGE_TYPE_TEXT; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public array $parts = []; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
protected array $options = []; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __construct($raw_structure, Header $header) { |
| 68 |
$this->raw = $raw_structure; |
| 69 |
$this->header = $header; |
| 70 |
$this->options = $header->getConfig()->get('options'); |
| 71 |
$this->parse(); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
protected function parse(): void { |
| 81 |
$this->findContentType(); |
| 82 |
$this->parts = $this->find_parts(); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function findContentType(): void { |
| 89 |
$content_type = $this->header->get("content_type")->first(); |
| 90 |
if($content_type && stripos($content_type, 'multipart') === 0) { |
| 91 |
$this->type = IMAP::MESSAGE_TYPE_MULTIPART; |
| 92 |
}else{ |
| 93 |
$this->type = IMAP::MESSAGE_TYPE_TEXT; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private function parsePart(string $context, int $part_number = 0): array { |
| 106 |
$body = $context; |
| 107 |
while (($pos = strpos($body, "\r\n")) > 0) { |
| 108 |
$body = substr($body, $pos + 2); |
| 109 |
} |
| 110 |
$headers = substr($context, 0, strlen($body) * -1); |
| 111 |
$body = substr($body, 0, -2); |
| 112 |
|
| 113 |
$config = $this->header->getConfig(); |
| 114 |
$headers = new Header($headers, $config); |
| 115 |
if (($boundary = $headers->getBoundary()) !== null) { |
| 116 |
$parts = $this->detectParts($boundary, $body, $part_number); |
| 117 |
|
| 118 |
if(count($parts) > 1) { |
| 119 |
return $parts; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return [new Part($body, $this->header->getConfig(), $headers, $part_number)]; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private function detectParts(string $boundary, string $context, int $part_number = 0): array { |
| 135 |
$base_parts = explode( "--".$boundary, $context); |
| 136 |
if(count($base_parts) == 0) { |
| 137 |
$base_parts = explode($boundary, $context); |
| 138 |
} |
| 139 |
$final_parts = []; |
| 140 |
foreach($base_parts as $ctx) { |
| 141 |
$ctx = substr($ctx, 2); |
| 142 |
if ($ctx !== "--" && $ctx != "" && $ctx != "\r\n") { |
| 143 |
$parts = $this->parsePart($ctx, $part_number); |
| 144 |
foreach ($parts as $part) { |
| 145 |
$final_parts[] = $part; |
| 146 |
$part_number = $part->part_number; |
| 147 |
} |
| 148 |
$part_number++; |
| 149 |
} |
| 150 |
} |
| 151 |
return $final_parts; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function find_parts(): array { |
| 162 |
if($this->type === IMAP::MESSAGE_TYPE_MULTIPART) { |
| 163 |
if (($boundary = $this->header->getBoundary()) === null) { |
| 164 |
throw new MessageContentFetchingException("no content found", 0); |
| 165 |
} |
| 166 |
|
| 167 |
return $this->detectParts($boundary, $this->raw); |
| 168 |
} |
| 169 |
|
| 170 |
return [new Part($this->raw, $this->header->getConfig(), $this->header)]; |
| 171 |
} |
| 172 |
} |