Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
bodystructure.php
1<?php
2
4
61{
62 protected $number;
63 protected $data = array();
64 protected $isMultipart = false, $partsCount = 0;
65 protected const TYPE_INDEX = 0;
66 protected const SUBTYPE_INDEX = 1;
67 protected const ENCODING_INDEX = 5;
68
69 protected const DEFAULT_PROPERTIES = [
70 'text',
71 'html',
72 null,
73 null,
74 null,
75 '8bit',
76 ];
77
78 protected function formatProperty($property, $propertyIndex)
79 {
80 if(is_string($property) && !empty($property))
81 {
82 $property = mb_strtolower($property);
83 }
84 else
85 {
86 $property = self::DEFAULT_PROPERTIES[$propertyIndex];
87 }
88
89 return $property;
90 }
91
92 public function __construct(array $bodystructure, $number = null)
93 {
94 $this->number = $number;
95 $this->data = &$bodystructure;
96
97 if (is_array($bodystructure[0]))
98 {
99 $this->isMultipart = true;
100
101 $this->partsCount = count($bodystructure[0]);
102 for ($i = 0; $i < $this->partsCount; $i++)
103 {
104 $bodystructure[0][$i] = new static(
105 $bodystructure[0][$i],
106 (string) (!is_null($number) ? sprintf('%s.%u', $number, $i + 1) : $i + 1)
107 );
108 }
109 }
110 else
111 {
112 if (is_null($number))
113 {
114 $this->number = 1;
115 }
116
117 $bodystructure[self::TYPE_INDEX] = $this->formatProperty($bodystructure[self::TYPE_INDEX], self::TYPE_INDEX);
118 $bodystructure[self::ENCODING_INDEX] = $this->formatProperty($bodystructure[self::ENCODING_INDEX], self::ENCODING_INDEX);
119 }
120
121 $bodystructure[self::SUBTYPE_INDEX] = $this->formatProperty($bodystructure[self::SUBTYPE_INDEX], self::SUBTYPE_INDEX);
122
123 if (!empty($bodystructure[2]) && is_array($bodystructure[2]))
124 {
125 $params = array();
126
127 $count = count($bodystructure[2]);
128 for ($i = 0; $i < $count; $i += 2)
129 {
130 $params[mb_strtolower($bodystructure[2][$i])] = $bodystructure[2][$i + 1];
131 }
132
133 $bodystructure[2] = $params;
134 }
135
136 $disposition = &$bodystructure[$this->getDispositionIndex()];
137
138 $disposition[0] = mb_strtolower($disposition[0]);
139 if (!empty($disposition[1]) && is_array($disposition[1]))
140 {
141 $params = array();
142
143 $count = count($disposition[1]);
144 for ($i = 0; $i < $count; $i += 2)
145 {
146 $params[mb_strtolower($disposition[1][$i])] = $disposition[1][$i + 1];
147 }
148
149 $disposition[1] = $params;
150 }
151 }
152
153 public function getNumber()
154 {
155 return $this->number;
156 }
157
158 public function getType()
159 {
160 return ($this->isMultipart ? 'multipart' : $this->data[0]);
161 }
162
163 public function getSubtype()
164 {
165 return $this->data[1];
166 }
167
168 public function getParams()
169 {
170 if(is_array($this->data[2]))
171 {
172 return $this->data[2];
173 }
174 else if(is_string($this->data[2]))
175 {
176 return ['name' => $this->data[2]];
177 }
178 else
179 {
180 return ['name' => 'file'];
181 }
182 }
183
184 public function getId()
185 {
186 return $this->isMultipart ? false : $this->data[3];
187 }
188
189 public function getEncoding()
190 {
191 return ($this->isMultipart ? false : $this->data[5]);
192 }
193
194 protected function getDispositionIndex()
195 {
196 switch ($this->getType())
197 {
198 case 'multipart':
199 return 3;
200 case 'message':
201 return 'rfc822' === $this->getSubtype() ? 11 : 8;
202 case 'text':
203 return 9;
204 default:
205 return 8;
206 }
207 }
208
209 public function getDisposition()
210 {
211 return $this->data[$this->getDispositionIndex()];
212 }
213
214 public function isMultipart()
215 {
216 return $this->isMultipart;
217 }
218
219 public function isText()
220 {
221 return $this->getType() === 'text';
222 }
223
224 public function isAttachment()
225 {
226 return $this->getDisposition()[0] === 'attachment';
227 }
228
229 public function isBodyText()
230 {
231 return $this->isText() && !$this->isAttachment() && $this->getSubtype() != 'calendar';
232 }
233
234 public function traverse(callable $callback, $flat = false)
235 {
236 $items = array();
237
238 if ($this->isMultipart)
239 {
240 for ($i = 0; $i < $this->partsCount; $i++)
241 {
242 $items[] = $this->data[0][$i]->traverse($callback, $flat);
243 }
244 }
245
246 $result = array($callback($this, $items));
247
248 if ($flat)
249 {
250 $result = array_merge($result, ...$items);
251 }
252 else
253 {
254 $result[] = $items;
255 }
256
257 return $result;
258 }
259
260}
__construct(array $bodystructure, $number=null)
formatProperty($property, $propertyIndex)
traverse(callable $callback, $flat=false)