Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailbox.php
1<?php
2
3namespace Bitrix\Mail;
4
8
9Localization\Loc::loadMessages(__FILE__);
10
27class MailboxTable extends Entity\DataManager
28{
29
30 public static function getFilePath()
31 {
32 return __FILE__;
33 }
34
35 public static function getTableName()
36 {
37 return 'b_mail_mailbox';
38 }
39
46 public static function getUserMailboxWithEmail($email): mixed
47 {
48 foreach (static::getUserMailboxes() as $mailbox)
49 {
50 if ($mailbox['EMAIL'] == $email)
51 {
52 return $mailbox;
53 }
54 }
55
56 return null;
57 }
58
66 public static function getMailboxesWithEmail($email)
67 {
68 $result = [];
69 $list = self::getList(([
70 'select' => [
71 'ID',
72 'USER_ID',
73 ],
74 'filter' => [
75 '=EMAIL' => $email,
76 ],
77 ]));
78
79 while ($item = $list->fetch())
80 {
81 $result[] = $item;
82 }
83
84 $dbResult = new ArrayResult($result);
85 $dbResult->setCount($list->getSelectedRowsCount());
86
87 return $dbResult;
88 }
89
90 public static function getOwnerId($mailboxId): int
91 {
92 $mailbox = self::getList([
93 'select' => [
94 'USER_ID',
95 ],
96 'filter' => [
97 '=ID' => $mailboxId,
98 ],
99 'limit' => 1,
100 ])->fetch();
101
102 if (isset($mailbox['USER_ID']))
103 {
104 return (int) $mailbox['USER_ID'];
105 }
106
107 return 0;
108 }
109
110 public static function getUserMailbox($mailboxId, $userId = null)
111 {
112 $mailboxes = static::getUserMailboxes($userId);
113
114 return array_key_exists($mailboxId, $mailboxes) ? $mailboxes[$mailboxId] : false;
115 }
116
117 public static function getTheOwnersMailboxes($userId = null): array
118 {
119 global $USER;
120
121 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
122 {
123 return [];
124 }
125
126 if (!($userId > 0))
127 {
128 $userId = $USER->getId();
129 }
130
131 static $mailboxes = [];
132 static $userMailboxes = [];
133
134 if (!array_key_exists($userId, $userMailboxes))
135 {
136 $userMailboxes[$userId] = [];
137
138 (new \CAccess)->updateCodes(['USER_ID' => $userId]);
139
140 $res = static::getList([
141 'filter' => [
142 [
143 '=USER_ID' => $userId,
144 ],
145 '=ACTIVE' => 'Y',
146 '=SERVER_TYPE' => 'imap',
147 ],
148 'order' => [
149 'ID' => 'DESC',
150 ],
151 ]);
152
153 while ($mailbox = $res->fetch())
154 {
155 static::normalizeEmail($mailbox);
156
157 $mailboxes[$mailbox['ID']] = $mailbox;
158 $userMailboxes[$userId][] = $mailbox['ID'];
159 }
160 }
161
162 $result = [];
163
164 foreach ($userMailboxes[$userId] as $mailboxId)
165 {
166 $result[$mailboxId] = $mailboxes[$mailboxId];
167 }
168
169 return $result;
170 }
171
172 public static function getTheSharedMailboxes($userId = null): array
173 {
174 global $USER;
175
176 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
177 {
178 return [];
179 }
180
181 if (!($userId > 0))
182 {
183 $userId = $USER->getId();
184 }
185
186 static $mailboxes = [];
187 static $userMailboxes = [];
188
189 if (!array_key_exists($userId, $userMailboxes))
190 {
191 $userMailboxes[$userId] = [];
192
193 (new \CAccess)->updateCodes(['USER_ID' => $userId]);
194
195 $res = static::getList([
196 'runtime' => [
197 new Entity\ReferenceField(
198 'ACCESS',
199 'Bitrix\Mail\Internals\MailboxAccessTable',
200 [
201 '=this.ID' => 'ref.MAILBOX_ID',
202 ],
203 [
204 'join_type' => 'LEFT',
205 ]
206 ),
207 new Entity\ReferenceField(
208 'USER_ACCESS',
209 'Bitrix\Main\UserAccess',
210 [
211 'this.ACCESS.ACCESS_CODE' => 'ref.ACCESS_CODE',
212 ],
213 [
214 'join_type' => 'LEFT',
215 ]
216 ),
217 ],
218 'filter' => [
219 [
220 'LOGIC' => 'AND',
221 '!=USER_ID' => $userId,
222 '=USER_ACCESS.USER_ID' => $userId,
223 ],
224 '=ACTIVE' => 'Y',
225 '=SERVER_TYPE' => 'imap',
226 ],
227 'order' => [
228 'ID' => 'DESC',
229 ],
230 ]);
231
232 while ($mailbox = $res->fetch())
233 {
234 static::normalizeEmail($mailbox);
235
236 $mailboxes[$mailbox['ID']] = $mailbox;
237 $userMailboxes[$userId][] = $mailbox['ID'];
238 }
239 }
240
241 $result = [];
242
243 foreach ($userMailboxes[$userId] as $mailboxId)
244 {
245 $result[$mailboxId] = $mailboxes[$mailboxId];
246 }
247
248 return $result;
249 }
250
257 public static function getUserMailboxes($userId = null): array
258 {
259 global $USER;
260
261 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
262 {
263 return [];
264 }
265
266 if (!($userId > 0))
267 {
268 $userId = $USER->getId();
269 }
270
271 $sharedMailboxes = static::getTheSharedMailboxes($userId);
272 $ownersMailboxes = static::getTheOwnersMailboxes($userId);
273
274 return $ownersMailboxes + $sharedMailboxes;
275 }
276
277 public static function normalizeEmail(&$mailbox)
278 {
279 foreach (array($mailbox['EMAIL'], $mailbox['NAME'], $mailbox['LOGIN']) as $item)
280 {
281 $address = new \Bitrix\Main\Mail\Address($item);
282 if ($address->validate())
283 {
284 $mailbox['EMAIL'] = $address->getEmail();
285 break;
286 }
287 }
288
289 return $mailbox;
290 }
291
292 public static function getMap()
293 {
294 return array(
295 'ID' => array(
296 'data_type' => 'integer',
297 'primary' => true,
298 'autocomplete' => true,
299 ),
300 'TIMESTAMP_X' => array(
301 'data_type' => 'datetime',
302 ),
303 'LID' => array(
304 'data_type' => 'string',
305 'required' => true
306 ),
307 'ACTIVE' => array(
308 'data_type' => 'boolean',
309 'values' => array('N', 'Y'),
310 ),
311 'SERVICE_ID' => array(
312 'data_type' => 'integer',
313 ),
314 'EMAIL' => array(
315 'data_type' => 'string',
316 ),
317 'USERNAME' => array(
318 'data_type' => 'string',
319 ),
320 'NAME' => array(
321 'data_type' => 'string',
322 ),
323 'SERVER' => array(
324 'data_type' => 'string',
325 ),
326 'PORT' => array(
327 'data_type' => 'integer',
328 ),
329 'LINK' => array(
330 'data_type' => 'string',
331 ),
332 'LOGIN' => array(
333 'data_type' => 'string',
334 ),
335 'CHARSET' => array(
336 'data_type' => 'string',
337 ),
338 'PASSWORD' => array(
339 'data_type' => (static::cryptoEnabled('PASSWORD') ? 'crypto' : 'string'),
340 'save_data_modification' => function()
341 {
342 return array(
343 function ($value)
344 {
345 return static::cryptoEnabled('PASSWORD') ? $value : \CMailUtil::crypt($value);
346 }
347 );
348 },
349 'fetch_data_modification' => function()
350 {
351 return array(
352 function ($value)
353 {
354 return static::cryptoEnabled('PASSWORD') ? $value : \CMailUtil::decrypt($value);
355 }
356 );
357 }
358 ),
359 'DESCRIPTION' => array(
360 'data_type' => 'text',
361 ),
362 'USE_MD5' => array(
363 'data_type' => 'boolean',
364 'values' => array('N', 'Y'),
365 ),
366 'DELETE_MESSAGES' => array(
367 'data_type' => 'boolean',
368 'values' => array('N', 'Y'),
369 ),
370 'PERIOD_CHECK' => array(
371 'data_type' => 'integer',
372 ),
373 'MAX_MSG_COUNT' => array(
374 'data_type' => 'integer',
375 ),
376 'MAX_MSG_SIZE' => array(
377 'data_type' => 'integer',
378 ),
379 'MAX_KEEP_DAYS' => array(
380 'data_type' => 'integer',
381 ),
382 'USE_TLS' => array(
383 'data_type' => 'enum',
384 'values' => array('N', 'Y', 'S'),
385 ),
386 'SERVER_TYPE' => array(
387 'data_type' => 'enum',
388 'values' => array('smtp', 'pop3', 'imap', 'controller', 'domain', 'crdomain')
389 ),
390 'DOMAINS' => array(
391 'data_type' => 'string',
392 ),
393 'RELAY' => array(
394 'data_type' => 'boolean',
395 'values' => array('N', 'Y'),
396 ),
397 'AUTH_RELAY' => array(
398 'data_type' => 'boolean',
399 'values' => array('N', 'Y'),
400 ),
401 'USER_ID' => array(
402 'data_type' => 'integer',
403 ),
404 'SYNC_LOCK' => array(
405 'data_type' => 'integer',
406 ),
407 'OPTIONS' => array(
408 'data_type' => 'text',
409 'save_data_modification' => function()
410 {
411 return array(
412 function ($options)
413 {
414 return serialize($options);
415 }
416 );
417 },
418 'fetch_data_modification' => function()
419 {
420 return array(
421 function ($values)
422 {
423 return unserialize($values);
424 }
425 );
426 }
427 ),
428 'SITE' => array(
429 'data_type' => 'Bitrix\Main\Site',
430 'reference' => array('=this.LID' => 'ref.LID'),
431 ),
432 );
433 }
434
435}
static getTheSharedMailboxes($userId=null)
Definition mailbox.php:172
static getUserMailboxWithEmail($email)
Definition mailbox.php:46
static getUserMailbox($mailboxId, $userId=null)
Definition mailbox.php:110
static getMailboxesWithEmail($email)
Definition mailbox.php:66
static normalizeEmail(&$mailbox)
Definition mailbox.php:277
static getUserMailboxes($userId=null)
Definition mailbox.php:257
static getTheOwnersMailboxes($userId=null)
Definition mailbox.php:117
static getOwnerId($mailboxId)
Definition mailbox.php:90