5use Bitrix\Mail\Internals\MailboxAccessTable;
6use Bitrix\Main\Data\Cache;
7use Bitrix\Main\DB\ArrayResult;
9use Bitrix\Main\Localization;
32 private const CACHE_TTL = 86400;
37 private static array $ownerCache = [];
38 private static array $onlyIdOwnerCache = [];
39 private static array $sharedCache = [];
40 private static array $onlyIdSharedCache = [];
49 return 'b_mail_mailbox';
60 foreach (static::getUserMailboxes() as $mailbox)
62 if ($mailbox[
'EMAIL'] ==
$email)
81 $list = self::getList(([
91 while ($item = $list->fetch())
97 $dbResult->setCount($list->getSelectedRowsCount());
104 $mailbox = self::getList([
114 if (isset($mailbox[
'USER_ID']))
116 return (
int) $mailbox[
'USER_ID'];
124 $mailboxes = static::getUserMailboxes(
$userId);
126 return array_key_exists($mailboxId, $mailboxes) ? $mailboxes[$mailboxId] :
false;
143 if ($onlyIds && isset(self::$onlyIdOwnerCache[
$userId]))
145 return self::$onlyIdOwnerCache[
$userId];
148 if (!$onlyIds && isset(self::$ownerCache[
$userId]))
150 return self::$ownerCache[
$userId];
153 $cacheManager = Cache::createInstance();
154 $cacheKey = self::getOwnerMailboxCacheKey(
$userId);
155 if ($cacheManager->initCache(self::CACHE_TTL, $cacheKey,self::OWNER_CACHE_DIR))
157 $result = $cacheManager->getVars();
159 if ($onlyIds ||
$result === [])
165 self::$onlyIdOwnerCache[
$userId] = [];
168 self::$ownerCache[
$userId] = [];
177 '=SERVER_TYPE' =>
'imap',
190 while ($mailbox =
$res->fetch())
192 static::normalizeEmail($mailbox);
193 $mailboxId = $mailbox[
'ID'] ??
null;
194 self::$onlyIdOwnerCache[
$userId][$mailboxId] = [
200 self::$ownerCache[
$userId][$mailboxId] = $mailbox;
204 if (empty(self::$onlyIdOwnerCache[
$userId]))
206 self::$ownerCache[
$userId] = [];
209 if ($cacheManager->startDataCache(self::CACHE_TTL, $cacheKey,self::OWNER_CACHE_DIR))
211 $cacheManager->endDataCache(self::$onlyIdOwnerCache[
$userId]);
214 return $onlyIds ? self::$onlyIdOwnerCache[
$userId] : self::$ownerCache[
$userId];
231 if ($onlyIds && isset(self::$onlyIdSharedCache[
$userId]))
233 return self::$onlyIdSharedCache[
$userId];
236 if (!$onlyIds && isset(self::$sharedCache[
$userId]))
238 return self::$sharedCache[
$userId];
241 $cacheManager = Cache::createInstance();
242 $cacheKey = self::getSharedMailboxCacheKey(
$userId);
243 if ($cacheManager->initCache(self::CACHE_TTL, $cacheKey, self::SHARED_CACHE_DIR))
245 $result = $cacheManager->getVars();
247 if ($onlyIds ||
$result === [])
253 self::$onlyIdSharedCache[
$userId] = [];
256 self::$sharedCache[
$userId] = [];
259 (new \CAccess)->updateCodes([
'USER_ID' =>
$userId]);
263 new Entity\ReferenceField(
265 'Bitrix\Mail\Internals\MailboxAccessTable',
267 '=this.ID' =>
'ref.MAILBOX_ID',
270 'join_type' =>
'LEFT',
273 new Entity\ReferenceField(
275 'Bitrix\Main\UserAccess',
277 'this.ACCESS.ACCESS_CODE' =>
'ref.ACCESS_CODE',
280 'join_type' =>
'LEFT',
288 '=USER_ACCESS.USER_ID' =>
$userId,
291 '=SERVER_TYPE' =>
'imap',
305 while ($mailbox =
$res->fetch())
307 static::normalizeEmail($mailbox);
309 $mailboxId = $mailbox[
'ID'] ??
null;
310 self::$onlyIdSharedCache[
$userId][$mailboxId] = [
316 self::$sharedCache[
$userId][$mailboxId] = $mailbox;
320 if (empty(self::$onlyIdSharedCache[
$userId]))
322 self::$sharedCache[
$userId] = [];
325 if ($cacheManager->startDataCache(self::CACHE_TTL, $cacheKey,self::SHARED_CACHE_DIR))
327 $cacheManager->endDataCache(self::$onlyIdSharedCache[
$userId]);
330 return $onlyIds ? self::$onlyIdSharedCache[
$userId] : self::$sharedCache[
$userId];
353 $sharedMailboxes = static::getTheSharedMailboxes(
$userId, $onlyIds);
354 $ownersMailboxes = static::getTheOwnersMailboxes(
$userId, $onlyIds);
356 return $ownersMailboxes + $sharedMailboxes;
361 $mailbox =
$event->getParameter(
'fields');
362 if (isset($mailbox[
'USER_ID']))
364 self::cleanOwnerCacheByUserId($mailbox[
'USER_ID']);
370 $mailbox =
$event->getParameter(
'fields');
371 if (isset($mailbox[
'USER_ID']))
373 unset(self::$ownerCache[$mailbox[
'USER_ID']]);
379 self::cleanAllCache();
384 foreach (
array($mailbox[
'EMAIL'], $mailbox[
'NAME'], $mailbox[
'LOGIN']) as $item)
386 $address = new \Bitrix\Main\Mail\Address($item);
387 if ($address->validate())
389 $mailbox[
'EMAIL'] = $address->getEmail();
401 'data_type' =>
'integer',
403 'autocomplete' =>
true,
405 'TIMESTAMP_X' =>
array(
406 'data_type' =>
'datetime',
409 'data_type' =>
'string',
413 'data_type' =>
'boolean',
414 'values' =>
array(
'N',
'Y'),
416 'SERVICE_ID' =>
array(
417 'data_type' =>
'integer',
420 'data_type' =>
'string',
423 'data_type' =>
'string',
426 'data_type' =>
'string',
429 'data_type' =>
'string',
432 'data_type' =>
'integer',
435 'data_type' =>
'string',
438 'data_type' =>
'string',
441 'data_type' =>
'string',
444 'data_type' => (static::cryptoEnabled(
'PASSWORD') ?
'crypto' :
'string'),
445 'save_data_modification' =>
function()
450 return static::cryptoEnabled(
'PASSWORD') ? $value : \CMailUtil::crypt($value);
454 'fetch_data_modification' =>
function()
459 return static::cryptoEnabled(
'PASSWORD') ? $value : \CMailUtil::decrypt($value);
464 'DESCRIPTION' =>
array(
465 'data_type' =>
'text',
468 'data_type' =>
'boolean',
469 'values' =>
array(
'N',
'Y'),
471 'DELETE_MESSAGES' =>
array(
472 'data_type' =>
'boolean',
473 'values' =>
array(
'N',
'Y'),
475 'PERIOD_CHECK' =>
array(
476 'data_type' =>
'integer',
478 'MAX_MSG_COUNT' =>
array(
479 'data_type' =>
'integer',
481 'MAX_MSG_SIZE' =>
array(
482 'data_type' =>
'integer',
484 'MAX_KEEP_DAYS' =>
array(
485 'data_type' =>
'integer',
488 'data_type' =>
'enum',
489 'values' =>
array(
'N',
'Y',
'S'),
491 'SERVER_TYPE' =>
array(
492 'data_type' =>
'enum',
493 'values' =>
array(
'smtp',
'pop3',
'imap',
'controller',
'domain',
'crdomain'),
496 'data_type' =>
'string',
499 'data_type' =>
'boolean',
500 'values' =>
array(
'N',
'Y'),
502 'AUTH_RELAY' =>
array(
503 'data_type' =>
'boolean',
504 'values' =>
array(
'N',
'Y'),
507 'data_type' =>
'integer',
509 'SYNC_LOCK' =>
array(
510 'data_type' =>
'integer',
513 'data_type' =>
'text',
514 'save_data_modification' =>
function()
523 'fetch_data_modification' =>
function()
528 return unserialize($values, [
'allowed_classes' =>
false]);
534 'data_type' =>
'Bitrix\Main\Site',
535 'reference' =>
array(
'=this.LID' =>
'ref.LID'),
540 private static function cleanOwnerCacheByUserId(
int $userId): void
542 unset(self::$ownerCache[
$userId]);
543 unset(self::$onlyIdOwnerCache[
$userId]);
544 Cache::createInstance()
549 private static function getOwnerMailboxCacheKey(
int $userId): string
554 private static function cleanAllCache(): void
556 self::$onlyIdOwnerCache = [];
557 self::$ownerCache = [];
558 self::$onlyIdSharedCache = [];
559 self::$sharedCache = [];
561 $cacheManager = Cache::createInstance();
562 $cacheManager->cleanDir(self::SHARED_CACHE_DIR);
563 $cacheManager->cleanDir(self::OWNER_CACHE_DIR);
568 unset(self::$sharedCache[
$userId]);
569 unset(self::$onlyIdSharedCache[
$userId]);
571 Cache::createInstance()
576 private static function getSharedMailboxCacheKey(
int $userId): string
578 return self::SHARED_MAILBOX_KEY .
'_' .
$userId;
583 self::$onlyIdSharedCache = [];
584 self::$sharedCache = [];
586 $cacheManager = Cache::createInstance();
587 $cacheManager->cleanDir(self::SHARED_CACHE_DIR);
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static getTheOwnersMailboxes($userId=null, bool $onlyIds=false)
static getTheSharedMailboxes($userId=null, bool $onlyIds=false)
static onAfterAdd(Entity\Event $event)
static getUserMailboxWithEmail($email)
static cleanUserSharedCache(int $userId)
static getUserMailbox($mailboxId, $userId=null)
static getMailboxesWithEmail($email)
static normalizeEmail(&$mailbox)
static cleanAllSharedCache()
static onAfterUpdate(Entity\Event $event)
static getUserMailboxes($userId=null, bool $onlyIds=false)
static onAfterDelete(Entity\Event $event)
static getOwnerId($mailboxId)
static loadMessages($file)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)