Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
licensemanager.php
1<?php
2
3namespace Bitrix\Mail\Helper;
4
7use Bitrix\Bitrix24;
11
16{
17 private const MAILBOX_IS_LOCKED_PROPERTY = 1;
18 private const MAILBOX_IS_AVAILABLE_PROPERTY = 0;
19
20 public static function checkTheMailboxForSyncAvailability(int $checkedMailboxId): bool
21 {
22 $maxCountAvailableMailboxes = self::getUserMailboxesLimit();
23
24 if ($maxCountAvailableMailboxes < 0)
25 {
26 return true;
27 }
28
29 static $checkedMailboxes = [];
30
31 if (!array_key_exists($checkedMailboxId, $checkedMailboxes))
32 {
33 $checkedMailboxes[$checkedMailboxId] = MailboxTable::getById($checkedMailboxId)->fetch();
34 }
35
36 $mailboxAvailabilitySyncStatus = false;
37
38 if ($checkedMailboxes[$checkedMailboxId] && isset($checkedMailboxes[$checkedMailboxId]['USER_ID']))
39 {
40 $userId = (int) $checkedMailboxes[$checkedMailboxId]['USER_ID'];
41
42 static $userMailboxes = [];
43
44 if (!array_key_exists($userId, $userMailboxes))
45 {
46 $userMailboxes[$userId] = MailboxTable::getList([
47 'select' => [
48 'ID',
49 ],
50 'filter' => [
51 '=USER_ID' => $userId,
52 '=ACTIVE' => 'Y',
53 ],
54 'order' => [
55 'ID' => 'ASC',
56 ],
57 ])->fetchAll();
58 }
59
60 $mailboxNumber = 1;
61
62 foreach ($userMailboxes[$userId] as $mailbox)
63 {
64 if (isset($mailbox['ID']))
65 {
66 $mailboxId = (int) $mailbox['ID'];
67 if ($mailboxNumber <= $maxCountAvailableMailboxes)
68 {
69 if ($mailboxId === $checkedMailboxId)
70 {
71 $mailboxAvailabilitySyncStatus = true;
72 }
73
74 static::removeTariffRestrictionsOnTheMailbox($mailboxId);
75 }
76 else
77 {
78 static::imposeTariffRestrictionsOnTheMailbox($mailboxId);
79 }
80 }
81
82 $mailboxNumber ++;
83 }
84 }
85
86 return $mailboxAvailabilitySyncStatus;
87 }
88
89 private static function getMailboxTariffRestrictions($mailboxId, $overwriteCache = false): int
90 {
91 static $mailboxesRestriction = [];
92
93 if ($overwriteCache || !array_key_exists($mailboxId, $mailboxesRestriction))
94 {
95 $filter = [
96 '=MAILBOX_ID' => $mailboxId,
97 '=ENTITY_TYPE' => 'MAILBOX',
98 '=ENTITY_ID' => $mailboxId,
99 '=PROPERTY_NAME' => 'TARIFF_RESTRICTIONS',
100 ];
101
102 $restriction = MailEntityOptionsTable::getList([
103 'select' => [
104 'VALUE',
105 ],
106 'filter' => $filter,
107 'limit' => 1,
108 ])->fetch();
109
110 if (isset($restriction['VALUE']))
111 {
112 $mailboxesRestriction[$mailboxId] = (int) $restriction['VALUE'];
113 }
114 else
115 {
116 $mailboxesRestriction[$mailboxId] = 0;
117 }
118 }
119
120 return $mailboxesRestriction[$mailboxId];
121 }
122
123 private static function removeTariffRestrictionsOnTheMailbox($mailboxId): void
124 {
125 if (static::getMailboxTariffRestrictions($mailboxId) !== static::MAILBOX_IS_AVAILABLE_PROPERTY)
126 {
127 static::setTheOptionOfTariffRestrictions($mailboxId, static::MAILBOX_IS_AVAILABLE_PROPERTY);
128 static::getMailboxTariffRestrictions($mailboxId, true);
129 }
130 }
131
132 private static function imposeTariffRestrictionsOnTheMailbox($mailboxId): void
133 {
134 if (static::getMailboxTariffRestrictions($mailboxId) !== static::MAILBOX_IS_LOCKED_PROPERTY)
135 {
137 null,
138 'imposed_tariff_restrictions_on_the_mailbox',
139 null,
140 $mailboxId
141 );
142
143 static::setTheOptionOfTariffRestrictions($mailboxId, static::MAILBOX_IS_LOCKED_PROPERTY);
144 static::getMailboxTariffRestrictions($mailboxId, true);
145 }
146 }
147
148 private static function setTheOptionOfTariffRestrictions($mailboxId, $optionValue): void
149 {
150 $filter = [
151 '=MAILBOX_ID' => $mailboxId,
152 '=ENTITY_TYPE' => 'MAILBOX',
153 '=ENTITY_ID' => $mailboxId,
154 '=PROPERTY_NAME' => 'TARIFF_RESTRICTIONS',
155 ];
156
157 $keyRow = [
158 'MAILBOX_ID' => $mailboxId,
159 'ENTITY_TYPE' => 'MAILBOX',
160 'ENTITY_ID' => $mailboxId,
161 'PROPERTY_NAME' => 'TARIFF_RESTRICTIONS',
162 ];
163
164 $fields = $keyRow;
165
166 $fields['DATE_INSERT'] = new Main\Type\DateTime();
167 $fields['VALUE'] = $optionValue;
168
169 if (MailEntityOptionsTable::getCount($filter))
170 {
171 MailEntityOptionsTable::update(
172 $keyRow,
173 [
174 'VALUE' => $optionValue,
175 'DATE_INSERT' => new Main\Type\DateTime(),
176 ],
177 );
178 }
179 else
180 {
181 MailEntityOptionsTable::add(
182 $fields
183 );
184 }
185 }
186
187 public static function isEnabledNotificationOfMailMessageInCrm($userId): bool
188 {
189 if (Main\Loader::includeModule('crm') && Main\Loader::includeModule('im'))
190 {
191 foreach (
192 [
193 \CIMSettings::CLIENT_SITE,
194 \CIMSettings::CLIENT_MAIL,
195 \CIMSettings::CLIENT_PUSH,
196 ] as $clientId
197 )
198 {
199 if (
200 \CIMSettings::GetNotifyAccess(
201 $userId,
202 'crm',
203 \CCrmNotifierSchemeType::IncomingEmailName,
204 $clientId
205 )
206 )
207 {
208 return true;
209 }
210 }
211 }
212
213 return false;
214 }
215
216 private static function getCountSenders(int $userId = 0)
217 {
218 global $USER;
219
220 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
221 {
222 return false;
223 }
224
225 if (!($userId > 0))
226 {
227 $userId = $USER->getId();
228 }
229
230
231 return SenderTable::getCount([
232 'IS_CONFIRMED' => true,
233 [
234 'LOGIC' => 'OR',
235 '=USER_ID' => $userId,
236 'IS_PUBLIC' => true,
237 ],
238 ]);
239 }
240
241 public static function isMailClientReadyToUse($userId = null): bool
242 {
243 global $USER;
244
245 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
246 {
247 return false;
248 }
249
250 if (!($userId > 0))
251 {
252 $userId = $USER->getId();
253 }
254
255 if (
256 self::isSyncAvailable()
257 && (self::getCountSenders($userId) || count(MailboxTable::getUserMailboxes($userId)) > 0 )
258 && self::checkUserHasNotExceededTheConnectedMailboxesLimit($userId)
259 )
260 {
261 return true;
262 }
263
264 return false;
265 }
266
267 public static function checkUserHasNotExceededTheConnectedMailboxesLimit($userId = null): bool
268 {
269 global $USER;
270
271 if (!($userId > 0 || (is_object($USER) && $USER->isAuthorized())))
272 {
273 return false;
274 }
275
276 if (!($userId > 0))
277 {
278 $userId = $USER->getId();
279 }
280
281 $userMailboxesLimit = LicenseManager::getUserMailboxesLimit();
282
283 if (
284 $userMailboxesLimit >= 0
285 && count(MailboxTable::getTheOwnersMailboxes($userId)) > $userMailboxesLimit
286 )
287 {
288 return false;
289 }
290
291 return true;
292 }
293
299 public static function isSyncAvailable()
300 {
301 if (!Main\Loader::includeModule('bitrix24'))
302 {
303 return true;
304 }
305
306 return (bool) Bitrix24\Feature::isFeatureEnabled('mail_mailbox_sync');
307 }
308
309 public static function getSharedMailboxesLimit()
310 {
311 if (!Main\Loader::includeModule('bitrix24'))
312 {
313 return -1;
314 }
315
316 return (int) Bitrix24\Feature::getVariable('mail_shared_mailboxes_limit');
317 }
318
324 public static function getUserMailboxesLimit()
325 {
326 if (!Main\Loader::includeModule('bitrix24'))
327 {
328 return -1;
329 }
330
331 if (!static::isSyncAvailable())
332 {
333 return 0;
334 }
335
336 return (int) Bitrix24\Feature::getVariable('mail_user_mailboxes_limit');
337 }
338
339 public static function getEmailsLimitToSendMessage(): int
340 {
341 if (Main\Loader::includeModule('bitrix24') && (!static::isSyncAvailable() || \CBitrix24::IsDemoLicense()))
342 {
343 return 1;
344 }
345
346 return -1;
347 }
348
354 public static function getSyncOldLimit()
355 {
356 if (!Main\Loader::includeModule('bitrix24'))
357 {
358 return (int) Main\Config\Option::get('mail', 'sync_old_limit2', -1);
359 }
360
361 return (int) Bitrix24\Feature::getVariable('mail_sync_old_limit');
362 }
363
369 public static function isCleanupOldEnabled()
370 {
371 return static::getSyncOldLimit() > 0;
372 }
373
374}
static isEnabledNotificationOfMailMessageInCrm($userId)
static checkTheMailboxForSyncAvailability(int $checkedMailboxId)
static checkUserHasNotExceededTheConnectedMailboxesLimit($userId=null)
static isMailClientReadyToUse($userId=null)
static add($userId, $type, $fields, $mailboxId=null)
static getUserMailboxes($userId=null)
Definition mailbox.php:257
static getTheOwnersMailboxes($userId=null)
Definition mailbox.php:117