Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Structure.php

raw symbols SCIP
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 string $raw = "";
32
33 /**
34 * @var Header $header
35 */
36 private Header $header;
37
38 /**
39 * Message type (if multipart or not)
40 *
41 * @var int $type
42 */
43 public int $type = IMAP::MESSAGE_TYPE_TEXT;
44
45 /**
46 * All available parts
47 *
48 * @var Part[] $parts
49 */
50 public array $parts = [];
51
52 /**
53 * Options holder
54 *
55 * @var array $options
56 */
57 protected array $options = [];
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->options = $header->getConfig()->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(): void {
81 $this->findContentType();
82 $this->parts = $this->find_parts();
83 }
84
85 /**
86 * Determine the message content type
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 * Find all available headers and return the leftover body segment
99 * @var string $context
100 * @var integer $part_number
101 *
102 * @return Part[]
103 * @throws InvalidMessageDateException
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 * @param string $boundary
128 * @param string $context
129 * @param int $part_number
130 *
131 * @return array
132 * @throws InvalidMessageDateException
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 * Find all available parts
156 *
157 * @return array
158 * @throws MessageContentFetchingException
159 * @throws InvalidMessageDateException
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 }