1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
mailbox.php
См. документацию.
1<?php
2
3namespace Bitrix\Mail;
4
5use Bitrix\Mail\Internals\MailboxAccessTable;
6use Bitrix\Main\Data\Cache;
7use Bitrix\Main\DB\ArrayResult;
8use Bitrix\Main\Entity;
9use Bitrix\Main\Localization;
10
12
29class MailboxTable extends Entity\DataManager
30{
31
32 private const CACHE_TTL = 86400;
33 public const SHARED_MAILBOX_KEY = 'mailbox_shared_mailboxes';
34 public const OWNER_MAILBOX_KEY = 'mailbox_owners_mailboxes';
35 public const SHARED_CACHE_DIR = '/mail/shared/';
36 public const OWNER_CACHE_DIR = '/mail/owner/';
37 private static array $ownerCache = [];
38 private static array $onlyIdOwnerCache = [];
39 private static array $sharedCache = [];
40 private static array $onlyIdSharedCache = [];
41
42 public static function getFilePath()
43 {
44 return __FILE__;
45 }
46
47 public static function getTableName()
48 {
49 return 'b_mail_mailbox';
50 }
51
58 public static function getUserMailboxWithEmail($email): mixed
59 {
60 foreach (static::getUserMailboxes() as $mailbox)
61 {
62 if ($mailbox['EMAIL'] == $email)
63 {
64 return $mailbox;
65 }
66 }
67
68 return null;
69 }
70
78 public static function getMailboxesWithEmail($email)
79 {
80 $result = [];
81 $list = self::getList(([
82 'select' => [
83 'ID',
84 'USER_ID',
85 ],
86 'filter' => [
87 '=EMAIL' => $email,
88 ],
89 ]));
90
91 while ($item = $list->fetch())
92 {
93 $result[] = $item;
94 }
95
97 $dbResult->setCount($list->getSelectedRowsCount());
98
99 return $dbResult;
100 }
101
102 public static function getOwnerId($mailboxId): int
103 {
104 $mailbox = self::getList([
105 'select' => [
106 'USER_ID',
107 ],
108 'filter' => [
109 '=ID' => $mailboxId,
110 ],
111 'limit' => 1,
112 ])->fetch();
113
114 if (isset($mailbox['USER_ID']))
115 {
116 return (int) $mailbox['USER_ID'];
117 }
118
119 return 0;
120 }
121
122 public static function getUserMailbox($mailboxId, $userId = null)
123 {
124 $mailboxes = static::getUserMailboxes($userId);
125
126 return array_key_exists($mailboxId, $mailboxes) ? $mailboxes[$mailboxId] : false;
127 }
128
129 public static function getTheOwnersMailboxes($userId = null, bool $onlyIds = false): array
130 {
131 global $USER;
132
133 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
134 {
135 return [];
136 }
137
138 if (!($userId > 0))
139 {
140 $userId = $USER->getId();
141 }
142
143 if ($onlyIds && isset(self::$onlyIdOwnerCache[$userId]))
144 {
145 return self::$onlyIdOwnerCache[$userId];
146 }
147
148 if (!$onlyIds && isset(self::$ownerCache[$userId]))
149 {
150 return self::$ownerCache[$userId];
151 }
152
153 $cacheManager = Cache::createInstance();
154 $cacheKey = self::getOwnerMailboxCacheKey($userId);
155 if ($cacheManager->initCache(self::CACHE_TTL, $cacheKey,self::OWNER_CACHE_DIR))
156 {
157 $result = $cacheManager->getVars();
158 //cache stores only id values, but empty value also works for full request
159 if ($onlyIds || $result === [])
160 {
161 return $result;
162 }
163 }
164
165 self::$onlyIdOwnerCache[$userId] = [];
166 if (!$onlyIds)
167 {
168 self::$ownerCache[$userId] = [];
169 }
170
172 'filter' => [
173 [
174 '=USER_ID' => $userId,
175 ],
176 '=ACTIVE' => 'Y',
177 '=SERVER_TYPE' => 'imap',
178 ],
179 'order' => [
180 'ID' => 'DESC',
181 ],
182 ];
183
184 if ($onlyIds)
185 {
186 $getListParams['select'] = ['ID'];
187 }
188
189 $res = static::getList($getListParams);
190 while ($mailbox = $res->fetch())
191 {
192 static::normalizeEmail($mailbox);
193 $mailboxId = $mailbox['ID'] ?? null;
194 self::$onlyIdOwnerCache[$userId][$mailboxId] = [
195 'ID' => $mailboxId,
196 ];
197
198 if (!$onlyIds)
199 {
200 self::$ownerCache[$userId][$mailboxId] = $mailbox;
201 }
202 }
203
204 if (empty(self::$onlyIdOwnerCache[$userId]))
205 {
206 self::$ownerCache[$userId] = [];
207 }
208
209 if ($cacheManager->startDataCache(self::CACHE_TTL, $cacheKey,self::OWNER_CACHE_DIR))
210 {
211 $cacheManager->endDataCache(self::$onlyIdOwnerCache[$userId]);
212 }
213
214 return $onlyIds ? self::$onlyIdOwnerCache[$userId] : self::$ownerCache[$userId];
215 }
216
217 public static function getTheSharedMailboxes($userId = null, bool $onlyIds = false): array
218 {
219 global $USER;
220
221 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
222 {
223 return [];
224 }
225
226 if (!($userId > 0))
227 {
228 $userId = $USER->getId();
229 }
230
231 if ($onlyIds && isset(self::$onlyIdSharedCache[$userId]))
232 {
233 return self::$onlyIdSharedCache[$userId];
234 }
235
236 if (!$onlyIds && isset(self::$sharedCache[$userId]))
237 {
238 return self::$sharedCache[$userId];
239 }
240
241 $cacheManager = Cache::createInstance();
242 $cacheKey = self::getSharedMailboxCacheKey($userId);
243 if ($cacheManager->initCache(self::CACHE_TTL, $cacheKey, self::SHARED_CACHE_DIR))
244 {
245 $result = $cacheManager->getVars();
246 //cache stores only id values, but empty value also works for full request
247 if ($onlyIds || $result === [])
248 {
249 return $result;
250 }
251 }
252
253 self::$onlyIdSharedCache[$userId] = [];
254 if (!$onlyIds)
255 {
256 self::$sharedCache[$userId] = [];
257 }
258
259 (new \CAccess)->updateCodes(['USER_ID' => $userId]);
260
262 'runtime' => [
263 new Entity\ReferenceField(
264 'ACCESS',
265 'Bitrix\Mail\Internals\MailboxAccessTable',
266 [
267 '=this.ID' => 'ref.MAILBOX_ID',
268 ],
269 [
270 'join_type' => 'LEFT',
271 ],
272 ),
273 new Entity\ReferenceField(
274 'USER_ACCESS',
275 'Bitrix\Main\UserAccess',
276 [
277 'this.ACCESS.ACCESS_CODE' => 'ref.ACCESS_CODE',
278 ],
279 [
280 'join_type' => 'LEFT',
281 ],
282 ),
283 ],
284 'filter' => [
285 [
286 'LOGIC' => 'AND',
287 '!=USER_ID' => $userId,
288 '=USER_ACCESS.USER_ID' => $userId,
289 ],
290 '=ACTIVE' => 'Y',
291 '=SERVER_TYPE' => 'imap',
292 ],
293 'order' => [
294 'ID' => 'DESC',
295 ],
296 ];
297
298 if ($onlyIds)
299 {
300 $getListParams['select'] = ['ID'];
301 }
302
303 $res = static::getList($getListParams);
304
305 while ($mailbox = $res->fetch())
306 {
307 static::normalizeEmail($mailbox);
308
309 $mailboxId = $mailbox['ID'] ?? null;
310 self::$onlyIdSharedCache[$userId][$mailboxId] = [
311 'ID' => $mailboxId,
312 ];
313
314 if (!$onlyIds)
315 {
316 self::$sharedCache[$userId][$mailboxId] = $mailbox;
317 }
318 }
319
320 if (empty(self::$onlyIdSharedCache[$userId]))
321 {
322 self::$sharedCache[$userId] = [];
323 }
324
325 if ($cacheManager->startDataCache(self::CACHE_TTL, $cacheKey,self::SHARED_CACHE_DIR))
326 {
327 $cacheManager->endDataCache(self::$onlyIdSharedCache[$userId]);
328 }
329
330 return $onlyIds ? self::$onlyIdSharedCache[$userId] : self::$sharedCache[$userId];
331 }
332
339 public static function getUserMailboxes($userId = null, bool $onlyIds = false): array
340 {
341 global $USER;
342
343 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
344 {
345 return [];
346 }
347
348 if (!($userId > 0))
349 {
350 $userId = $USER->getId();
351 }
352
353 $sharedMailboxes = static::getTheSharedMailboxes($userId, $onlyIds);
354 $ownersMailboxes = static::getTheOwnersMailboxes($userId, $onlyIds);
355
356 return $ownersMailboxes + $sharedMailboxes;
357 }
358
359 public static function onAfterAdd(Entity\Event $event): void
360 {
361 $mailbox = $event->getParameter('fields');
362 if (isset($mailbox['USER_ID']))
363 {
364 self::cleanOwnerCacheByUserId($mailbox['USER_ID']);
365 }
366 }
367
368 public static function onAfterUpdate(Entity\Event $event): void
369 {
370 $mailbox = $event->getParameter('fields');
371 if (isset($mailbox['USER_ID']))
372 {
373 unset(self::$ownerCache[$mailbox['USER_ID']]);
374 }
375 }
376
377 public static function onAfterDelete(Entity\Event $event): void
378 {
379 self::cleanAllCache();
380 }
381
382 public static function normalizeEmail(&$mailbox)
383 {
384 foreach (array($mailbox['EMAIL'], $mailbox['NAME'], $mailbox['LOGIN']) as $item)
385 {
386 $address = new \Bitrix\Main\Mail\Address($item);
387 if ($address->validate())
388 {
389 $mailbox['EMAIL'] = $address->getEmail();
390 break;
391 }
392 }
393
394 return $mailbox;
395 }
396
397 public static function getMap()
398 {
399 return array(
400 'ID' => array(
401 'data_type' => 'integer',
402 'primary' => true,
403 'autocomplete' => true,
404 ),
405 'TIMESTAMP_X' => array(
406 'data_type' => 'datetime',
407 ),
408 'LID' => array(
409 'data_type' => 'string',
410 'required' => true,
411 ),
412 'ACTIVE' => array(
413 'data_type' => 'boolean',
414 'values' => array('N', 'Y'),
415 ),
416 'SERVICE_ID' => array(
417 'data_type' => 'integer',
418 ),
419 'EMAIL' => array(
420 'data_type' => 'string',
421 ),
422 'USERNAME' => array(
423 'data_type' => 'string',
424 ),
425 'NAME' => array(
426 'data_type' => 'string',
427 ),
428 'SERVER' => array(
429 'data_type' => 'string',
430 ),
431 'PORT' => array(
432 'data_type' => 'integer',
433 ),
434 'LINK' => array(
435 'data_type' => 'string',
436 ),
437 'LOGIN' => array(
438 'data_type' => 'string',
439 ),
440 'CHARSET' => array(
441 'data_type' => 'string',
442 ),
443 'PASSWORD' => array(
444 'data_type' => (static::cryptoEnabled('PASSWORD') ? 'crypto' : 'string'),
445 'save_data_modification' => function()
446 {
447 return array(
448 function ($value)
449 {
450 return static::cryptoEnabled('PASSWORD') ? $value : \CMailUtil::crypt($value);
451 },
452 );
453 },
454 'fetch_data_modification' => function()
455 {
456 return array(
457 function ($value)
458 {
459 return static::cryptoEnabled('PASSWORD') ? $value : \CMailUtil::decrypt($value);
460 },
461 );
462 },
463 ),
464 'DESCRIPTION' => array(
465 'data_type' => 'text',
466 ),
467 'USE_MD5' => array(
468 'data_type' => 'boolean',
469 'values' => array('N', 'Y'),
470 ),
471 'DELETE_MESSAGES' => array(
472 'data_type' => 'boolean',
473 'values' => array('N', 'Y'),
474 ),
475 'PERIOD_CHECK' => array(
476 'data_type' => 'integer',
477 ),
478 'MAX_MSG_COUNT' => array(
479 'data_type' => 'integer',
480 ),
481 'MAX_MSG_SIZE' => array(
482 'data_type' => 'integer',
483 ),
484 'MAX_KEEP_DAYS' => array(
485 'data_type' => 'integer',
486 ),
487 'USE_TLS' => array(
488 'data_type' => 'enum',
489 'values' => array('N', 'Y', 'S'),
490 ),
491 'SERVER_TYPE' => array(
492 'data_type' => 'enum',
493 'values' => array('smtp', 'pop3', 'imap', 'controller', 'domain', 'crdomain'),
494 ),
495 'DOMAINS' => array(
496 'data_type' => 'string',
497 ),
498 'RELAY' => array(
499 'data_type' => 'boolean',
500 'values' => array('N', 'Y'),
501 ),
502 'AUTH_RELAY' => array(
503 'data_type' => 'boolean',
504 'values' => array('N', 'Y'),
505 ),
506 'USER_ID' => array(
507 'data_type' => 'integer',
508 ),
509 'SYNC_LOCK' => array(
510 'data_type' => 'integer',
511 ),
512 'OPTIONS' => array(
513 'data_type' => 'text',
514 'save_data_modification' => function()
515 {
516 return array(
517 function ($options)
518 {
519 return serialize($options);
520 },
521 );
522 },
523 'fetch_data_modification' => function()
524 {
525 return array(
526 function ($values)
527 {
528 return unserialize($values, ['allowed_classes' => false]);
529 },
530 );
531 },
532 ),
533 'SITE' => array(
534 'data_type' => 'Bitrix\Main\Site',
535 'reference' => array('=this.LID' => 'ref.LID'),
536 ),
537 );
538 }
539
540 private static function cleanOwnerCacheByUserId(int $userId): void
541 {
542 unset(self::$ownerCache[$userId]);
543 unset(self::$onlyIdOwnerCache[$userId]);
544 Cache::createInstance()
545 ->clean(self::getOwnerMailboxCacheKey($userId),MailboxTable::OWNER_CACHE_DIR)
546 ;
547 }
548
549 private static function getOwnerMailboxCacheKey(int $userId): string
550 {
552 }
553
554 private static function cleanAllCache(): void
555 {
556 self::$onlyIdOwnerCache = [];
557 self::$ownerCache = [];
558 self::$onlyIdSharedCache = [];
559 self::$sharedCache = [];
560
561 $cacheManager = Cache::createInstance();
562 $cacheManager->cleanDir(self::SHARED_CACHE_DIR);
563 $cacheManager->cleanDir(self::OWNER_CACHE_DIR);
564 }
565
566 public static function cleanUserSharedCache(int $userId): void
567 {
568 unset(self::$sharedCache[$userId]);
569 unset(self::$onlyIdSharedCache[$userId]);
570
571 Cache::createInstance()
572 ->clean(self::getSharedMailboxCacheKey($userId),MailboxTable::SHARED_CACHE_DIR)
573 ;
574 }
575
576 private static function getSharedMailboxCacheKey(int $userId): string
577 {
578 return self::SHARED_MAILBOX_KEY . '_' . $userId;
579 }
580
581 public static function cleanAllSharedCache(): void
582 {
583 self::$onlyIdSharedCache = [];
584 self::$sharedCache = [];
585
586 $cacheManager = Cache::createInstance();
587 $cacheManager->cleanDir(self::SHARED_CACHE_DIR);
588 }
589}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getMap()
Определения mailbox.php:397
static getTheOwnersMailboxes($userId=null, bool $onlyIds=false)
Определения mailbox.php:129
static getTheSharedMailboxes($userId=null, bool $onlyIds=false)
Определения mailbox.php:217
static onAfterAdd(Entity\Event $event)
Определения mailbox.php:359
static getUserMailboxWithEmail($email)
Определения mailbox.php:58
static getFilePath()
Определения mailbox.php:42
const SHARED_CACHE_DIR
Определения mailbox.php:35
static cleanUserSharedCache(int $userId)
Определения mailbox.php:566
static getUserMailbox($mailboxId, $userId=null)
Определения mailbox.php:122
static getMailboxesWithEmail($email)
Определения mailbox.php:78
static normalizeEmail(&$mailbox)
Определения mailbox.php:382
static cleanAllSharedCache()
Определения mailbox.php:581
const SHARED_MAILBOX_KEY
Определения mailbox.php:33
static onAfterUpdate(Entity\Event $event)
Определения mailbox.php:368
static getUserMailboxes($userId=null, bool $onlyIds=false)
Определения mailbox.php:339
static onAfterDelete(Entity\Event $event)
Определения mailbox.php:377
const OWNER_CACHE_DIR
Определения mailbox.php:36
const OWNER_MAILBOX_KEY
Определения mailbox.php:34
static getOwnerId($mailboxId)
Определения mailbox.php:102
static getTableName()
Определения mailbox.php:47
Определения event.php:5
static loadMessages($file)
Определения loc.php:65
$options
Определения commerceml2.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$getListParams
Определения iblock_catalog_list.php:210
global $USER
Определения csv_new_run.php:40
Определения ufield.php:9
$email
Определения payment.php:49
$event
Определения prolog_after.php:141
$dbResult
Определения updtr957.php:3