1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
licensemanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Mail\Helper;
4
5use Bitrix\Main\ORM\Query\Join;
6use Bitrix\Main\ORM\Fields\Relations\Reference;
7use Bitrix\Mail\Internals\MailEntityOptionsTable;
8use Bitrix\Main;
9use Bitrix\Bitrix24;
10use Bitrix\Mail\MailboxTable;
11use Bitrix\Main\Mail\Internal\SenderTable;
12use Bitrix\Main\Type\DateTime;
13use Bitrix\Mail\Integration\Im\Notification;
14
19{
20 private const MAILBOX_IS_LOCKED_PROPERTY = 1;
21 private const MAILBOX_IS_AVAILABLE_PROPERTY = 0;
22
23 private static function sendNotificationsAboutBlockedMailboxes($ids): void
24 {
25 foreach ($ids as $id)
26 {
28 null,
29 'imposed_tariff_restrictions_on_the_mailbox',
30 null,
31 $id,
32 );
33 }
34 }
35
36 private static function getTariffRestrictionsMailboxListsByDBData($mailboxes, $filterByStatus = null): array
37 {
38 $lists = [
39 'IDS_FOR_ADD' => [],
40 'IDS_FOR_UPDATE' => [],
41 'IDS_ALL' => [],
42 ];
43
44 foreach ($mailboxes as $mailbox)
45 {
46 $mailboxId = (int) $mailbox['ID'];
47
48 if (is_null($mailbox['TARIFF_RESTRICTIONS']))
49 {
50 $lists['IDS_FOR_ADD'][] = $mailboxId;
51 $lists['IDS_ALL'][] = $mailboxId;
52 }
53 else if(is_null($filterByStatus) || (int)$mailbox['TARIFF_RESTRICTIONS'] === $filterByStatus)
54 {
55 $lists['IDS_FOR_UPDATE'][] = $mailboxId;
56 $lists['IDS_ALL'][] = $mailboxId;
57 }
58 }
59
60 return $lists;
61 }
62
63 private static function checkIdInTariffRestrictionsMailboxLists($checkedMailboxId, $lists): bool
64 {
65 foreach ($lists as $list)
66 {
67 foreach ($list as $id)
68 {
69 if ($id === $checkedMailboxId)
70 {
71 return true;
72 }
73 }
74 }
75
76 return false;
77 }
78
79 private static function getAvailabilitySyncStatusByMailboxList($checkedMailboxId, $mailboxList): bool
80 {
81 foreach ($mailboxList as $mailbox)
82 {
83 if ($checkedMailboxId === (int) $mailbox['ID'])
84 {
85 return !($mailbox['TARIFF_RESTRICTIONS']);
86 }
87 }
88
89 return true;
90 }
91
92 public static function checkTheMailboxForSyncAvailability(int $checkedMailboxId, ?int $ownerMailboxUserId = null): bool
93 {
94 $maxCountAvailableMailboxes = self::getUserMailboxesLimit();
95 static $checkedMailboxes = [];
96
97 if (!array_key_exists($checkedMailboxId, $checkedMailboxes))
98 {
99 if (is_null($ownerMailboxUserId))
100 {
101 $checkedMailboxes[$checkedMailboxId] = MailboxTable::getList(([
102 'select' => [
103 'ID',
104 'USER_ID',
105 ],
106 'filter' => [
107 '=ID' => $checkedMailboxId,
108 ],
109 ]))->fetch();
110 }
111 else
112 {
113 $checkedMailboxes[$checkedMailboxId] = [
114 'ID' => $checkedMailboxId,
115 'USER_ID' => $ownerMailboxUserId,
116 ];
117 }
118 }
119
120 if ($checkedMailboxes[$checkedMailboxId] && isset($checkedMailboxes[$checkedMailboxId]['USER_ID']))
121 {
122 $userId = (int) $checkedMailboxes[$checkedMailboxId]['USER_ID'];
123
124 static $userMailboxes = [];
125
126 if (!array_key_exists($userId, $userMailboxes))
127 {
128 $userMailboxes = MailboxTable::query()->addSelect('ID')
129 ->setSelect([
130 'ID',
131 'TARIFF_RESTRICTIONS' => 'OPTIONS.VALUE',
132 ])
133 ->where('USER_ID', $userId)
134 ->where('ACTIVE', 'Y')
135 ->registerRuntimeField(
136 '',
137 new Reference(
138 'OPTIONS',
139 MailEntityOptionsTable::class,
140 Join::on('this.ID', 'ref.MAILBOX_ID')->where('ref.PROPERTY_NAME', 'TARIFF_RESTRICTIONS'),
141 ['join_type' => Join::TYPE_LEFT],
142 ),
143 )->fetchAll();
144 }
145
146 if ($maxCountAvailableMailboxes < 0)
147 {
148 $activateMailboxes = self::getTariffRestrictionsMailboxListsByDBData(array_slice($userMailboxes, 0, count($userMailboxes)), static::MAILBOX_IS_LOCKED_PROPERTY);
149 static::changeTariffRestrictionsOnTheMailboxes($activateMailboxes, static::MAILBOX_IS_AVAILABLE_PROPERTY);
150 $mailboxAvailabilitySyncStatus = true;
151 }
152 else
153 {
154 $activateMailboxes = self::getTariffRestrictionsMailboxListsByDBData(array_slice($userMailboxes, 0, $maxCountAvailableMailboxes), static::MAILBOX_IS_LOCKED_PROPERTY);
155 $blockMailboxes = self::getTariffRestrictionsMailboxListsByDBData(array_slice($userMailboxes, $maxCountAvailableMailboxes), static::MAILBOX_IS_AVAILABLE_PROPERTY);
156
157 if (self::checkIdInTariffRestrictionsMailboxLists($checkedMailboxId, $activateMailboxes))
158 {
159 $mailboxAvailabilitySyncStatus = true;
160 }
161 else if(self::checkIdInTariffRestrictionsMailboxLists($checkedMailboxId, $blockMailboxes))
162 {
163 $mailboxAvailabilitySyncStatus = false;
164 }
165 else
166 {
167 $mailboxAvailabilitySyncStatus = self::getAvailabilitySyncStatusByMailboxList($checkedMailboxId, $userMailboxes);
168 }
169
170 self::sendNotificationsAboutBlockedMailboxes($blockMailboxes['IDS_ALL']);
171 static::changeTariffRestrictionsOnTheMailboxes($activateMailboxes, static::MAILBOX_IS_AVAILABLE_PROPERTY);
172 static::changeTariffRestrictionsOnTheMailboxes($blockMailboxes, static::MAILBOX_IS_LOCKED_PROPERTY);
173 }
174
175 return $mailboxAvailabilitySyncStatus;
176 }
177
178 return false;
179 }
180
181 public static function changeTariffRestrictionsOnTheMailboxes($tariffRestrictionsMailboxLists, $tariffRestrictionsType): void
182 {
183 if (isset($tariffRestrictionsMailboxLists['IDS_FOR_ADD']) && count($tariffRestrictionsMailboxLists['IDS_FOR_ADD']))
184 {
185 $rowsForAdd = [];
186 foreach ($tariffRestrictionsMailboxLists['IDS_FOR_ADD'] as $id)
187 {
188 $rowsForAdd[] = [
189 'MAILBOX_ID' => $id,
190 'ENTITY_TYPE' => 'MAILBOX',
191 'ENTITY_ID' => $id,
192 'PROPERTY_NAME' => 'TARIFF_RESTRICTIONS',
193 'VALUE' => $tariffRestrictionsType,
194 'DATE_INSERT' => new DateTime(),
195 ];
196 }
197
198 foreach ($rowsForAdd as $row)
199 {
200 $filter = [
201 '=MAILBOX_ID' => $row['MAILBOX_ID'],
202 '=ENTITY_TYPE' => 'MAILBOX',
203 '=ENTITY_ID' => $row['ENTITY_ID'],
204 '=PROPERTY_NAME' => 'TARIFF_RESTRICTIONS',
205 ];
206
207 $keyRow = [
208 'MAILBOX_ID' => $row['MAILBOX_ID'],
209 'ENTITY_TYPE' => 'MAILBOX',
210 'ENTITY_ID' => $row['ENTITY_ID'],
211 'PROPERTY_NAME' => 'TARIFF_RESTRICTIONS',
212 ];
213
214 if (MailEntityOptionsTable::getCount($filter))
215 {
216 MailEntityOptionsTable::update(
217 $keyRow,
218 [
219 'VALUE' => $row['VALUE'],
220 'DATE_INSERT' => new DateTime(),
221 ],
222 );
223 }
224 else
225 {
226 MailEntityOptionsTable::add($row);
227 }
228 }
229 }
230
231 if (isset($tariffRestrictionsMailboxLists['IDS_FOR_UPDATE']) && count($tariffRestrictionsMailboxLists['IDS_FOR_UPDATE']))
232 {
233 $rowsForUpdate = [];
234 foreach ($tariffRestrictionsMailboxLists['IDS_FOR_UPDATE'] as $id)
235 {
236 $rowsForUpdate[] = [
237 'MAILBOX_ID' => $id,
238 'ENTITY_TYPE' => 'MAILBOX',
239 'ENTITY_ID' => $id,
240 'PROPERTY_NAME' => 'TARIFF_RESTRICTIONS',
241 ];
242 }
243
244 if (count($rowsForUpdate))
245 {
246 MailEntityOptionsTable::updateMulti($rowsForUpdate, [
247 'VALUE' => $tariffRestrictionsType,
248 'DATE_INSERT' => new DateTime(),
249 ]);
250 }
251 }
252 }
253
255 {
256 if (Main\Loader::includeModule('crm') && Main\Loader::includeModule('im'))
257 {
258 foreach (
259 [
263 ] as $clientId
264 )
265 {
266 if (
268 $userId,
269 'crm',
270 \CCrmNotifierSchemeType::IncomingEmailName,
272 )
273 )
274 {
275 return true;
276 }
277 }
278 }
279
280 return false;
281 }
282
283 private static function getCountSenders(int $userId = 0)
284 {
285 global $USER;
286
287 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
288 {
289 return false;
290 }
291
292 if (!($userId > 0))
293 {
294 $userId = $USER->getId();
295 }
296
297
298 return SenderTable::getCount([
299 'IS_CONFIRMED' => true,
300 [
301 'LOGIC' => 'OR',
302 '=USER_ID' => $userId,
303 'IS_PUBLIC' => true,
304 ],
305 ]);
306 }
307
308 public static function isMailClientReadyToUse($userId = null): bool
309 {
310 global $USER;
311
312 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
313 {
314 return false;
315 }
316
317 if (!($userId > 0))
318 {
319 $userId = $USER->getId();
320 }
321
322 if (
323 self::isSyncAvailable()
324 && (self::getCountSenders($userId) || count(MailboxTable::getUserMailboxes($userId)) > 0 )
325 && self::checkUserHasNotExceededTheConnectedMailboxesLimit($userId)
326 )
327 {
328 return true;
329 }
330
331 return false;
332 }
333
335 {
336 global $USER;
337
338 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
339 {
340 return false;
341 }
342
343 if (!($userId > 0))
344 {
345 $userId = $USER->getId();
346 }
347
348 $userMailboxesLimit = LicenseManager::getUserMailboxesLimit();
349
350 if (
351 $userMailboxesLimit >= 0
352 && count(MailboxTable::getTheOwnersMailboxes($userId)) > $userMailboxesLimit
353 )
354 {
355 return false;
356 }
357
358 return true;
359 }
360
366 public static function isSyncAvailable()
367 {
368 if (!Main\Loader::includeModule('bitrix24'))
369 {
370 return true;
371 }
372
373 return (bool) Bitrix24\Feature::isFeatureEnabled('mail_mailbox_sync');
374 }
375
376 public static function getSharedMailboxesLimit()
377 {
378 if (!Main\Loader::includeModule('bitrix24'))
379 {
380 return -1;
381 }
382
383 return (int) Bitrix24\Feature::getVariable('mail_shared_mailboxes_limit');
384 }
385
391 public static function getUserMailboxesLimit()
392 {
393 if (!Main\Loader::includeModule('bitrix24'))
394 {
395 return -1;
396 }
397
398 if (!static::isSyncAvailable())
399 {
400 return 0;
401 }
402
403 return (int) Bitrix24\Feature::getVariable('mail_user_mailboxes_limit');
404 }
405
406 public static function getEmailsLimitToSendMessage(): int
407 {
408 if (Main\Loader::includeModule('bitrix24') && (!static::isSyncAvailable() || \CBitrix24::IsDemoLicense()))
409 {
410 return 1;
411 }
412
413 return -1;
414 }
415
421 public static function getSyncOldLimit()
422 {
423 if (!Main\Loader::includeModule('bitrix24'))
424 {
425 return (int) Main\Config\Option::get('mail', 'sync_old_limit2', -1);
426 }
427
428 return (int) Bitrix24\Feature::getVariable('mail_sync_old_limit');
429 }
430
436 public static function isCleanupOldEnabled(): bool
437 {
438 if (Main\Application::getConnection()->getType() === 'pgsql')
439 {
440 return false; // not implemented yet
441 }
442
443 return static::getSyncOldLimit() > 0;
444 }
445
446}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getEmailsLimitToSendMessage()
Определения licensemanager.php:406
static isCleanupOldEnabled()
Определения licensemanager.php:436
static checkTheMailboxForSyncAvailability(int $checkedMailboxId, ?int $ownerMailboxUserId=null)
Определения licensemanager.php:92
static getUserMailboxesLimit()
Определения licensemanager.php:391
static changeTariffRestrictionsOnTheMailboxes($tariffRestrictionsMailboxLists, $tariffRestrictionsType)
Определения licensemanager.php:181
static isEnabledNotificationOfMailMessageInCrm($userId)
Определения licensemanager.php:254
static getSharedMailboxesLimit()
Определения licensemanager.php:376
static checkUserHasNotExceededTheConnectedMailboxesLimit($userId=null)
Определения licensemanager.php:334
static isMailClientReadyToUse($userId=null)
Определения licensemanager.php:308
static add($userId, $type, $fields, $mailboxId=null)
Определения notification.php:175
static getTheOwnersMailboxes($userId=null, bool $onlyIds=false)
Определения mailbox.php:129
static getUserMailboxes($userId=null, bool $onlyIds=false)
Определения mailbox.php:339
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
static GetNotifyAccess($userId, $moduleId, $eventId, $clientId)
Определения im_settings.php:255
const CLIENT_SITE
Определения im_settings.php:15
const CLIENT_PUSH
Определения im_settings.php:18
const CLIENT_MAIL
Определения im_settings.php:17
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$filter
Определения iblock_catalog_list.php:54
global $USER
Определения csv_new_run.php:40
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$clientId
Определения seo_client.php:18