Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailservices.php
1<?php
2
3namespace Bitrix\Mail;
4
8
9Localization\Loc::loadMessages(__FILE__);
10
27class MailServicesTable 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_mailservices';
38 }
39
40 public static function checkFields(Entity\Result $result, $primary, array $data)
41 {
42 parent::checkFields($result, $primary, $data);
43
44 if (isset($data['SITE_ID']))
45 {
46 $selectResult = \Bitrix\Main\SiteTable::getByPrimary($data['SITE_ID']);
47 if (!$selectResult->fetch())
48 {
49 $field = static::getEntity()->getField('SITE_ID');
50 $result->addError(new Entity\FieldError(
51 $field,
52 Localization\Loc::getMessage('MAIN_ENTITY_FIELD_INVALID', array('#FIELD_TITLE#' => $field->getTitle())),
53 Entity\FieldError::INVALID_VALUE
54 ));
55 }
56 }
57
58 if (!empty($data['ICON']))
59 {
60 if (!is_scalar($data['ICON']) || !preg_match('/[0-9]+/', $data['ICON']))
61 {
62 $field = static::getEntity()->getField('ICON');
63 $result->addError(new Entity\FieldError(
64 $field,
65 Localization\Loc::getMessage('MAIN_ENTITY_FIELD_INVALID', array('#FIELD_TITLE#' => $field->getTitle())),
66 Entity\FieldError::INVALID_VALUE
67 ));
68 }
69 }
70
71 return $result;
72 }
73
74 public static function add(array $data)
75 {
76 if (isset($data['ICON']) && is_array($data['ICON']))
77 {
78 $iconError = $data['ICON']['name'] ? \CFile::checkImageFile($data['ICON']) : null;
79 if (is_null($iconError))
80 {
81 $data['ICON']['MODULE_ID'] = 'mail';
82
83 \CFile::saveForDB($data, 'ICON', 'mail/mailservices/icon');
84 }
85 }
86
87 return parent::add($data);
88 }
89
90 public static function update($primary, array $data): Entity\UpdateResult
91 {
92 if (empty($data))
93 return new Entity\UpdateResult();
94
95 $serviceForUpdate = static::getByPrimary(
96 $primary,
97 array(
98 'select' => array(
99 'ID', 'SITE_ID', 'ACTIVE', 'SERVICE_TYPE',
100 ),
101 )
102 )->fetch();
103 if (!$serviceForUpdate)
104 {
105 $updateResult = new Entity\UpdateResult();
106 $updateResult->addError(new Entity\EntityError(Localization\Loc::getMessage('mail_mailservice_not_found')));
107
108 return $updateResult;
109 }
110
111 if (isset($data['ICON']) && is_array($data['ICON']))
112 {
113 $iconError = $data['ICON']['name'] ? \CFile::checkImageFile($data['ICON']) : null;
114 if (is_null($iconError))
115 {
116 $data['ICON']['MODULE_ID'] = 'mail';
117
118 \CFile::saveForDB($data, 'ICON', 'mail/mailservices/icon');
119 }
120 }
121
122 $updateResult = parent::update($primary, $data);
123
124 if ($updateResult->isSuccess())
125 {
126 $serviceId = is_array($primary) ? $primary['ID'] : $primary;
127
128 $isSiteChanged = isset($data['SITE_ID']) && $data['SITE_ID'] != $serviceForUpdate['SITE_ID'];
129 $isDeactivated = isset($data['ACTIVE']) && $data['ACTIVE'] == 'N' && $serviceForUpdate['ACTIVE'] == 'Y';
130 if (($isSiteChanged || $isDeactivated) && $serviceForUpdate['SERVICE_TYPE'] == 'imap')
131 {
132 $emptyService = static::getList(array(
133 'select' => array('ID'),
134 'filter' => array(
135 '=SITE_ID' => $serviceForUpdate['SITE_ID'],
136 'ACTIVE' => 'Y',
137 '=SERVICE_TYPE' => 'imap',
138 '=SERVER' => '',
139 '=PORT' => '',
140 '=ENCRYPTION' => '',
141 '=LINK' => '',
142 ),
143 'limit' => 1,
144 ))->fetch();
145 }
146
147 if ($isSiteChanged || $isDeactivated && $emptyService)
148 {
149 $mbData = $emptyService
150 ? array('SERVICE_ID' => $emptyService['ID'])
151 : array('ACTIVE' => 'N', 'SERVICE_ID' => 0);
152 }
153 else
154 {
155 $mbData = array();
156 foreach ($data as $key => $value)
157 {
158 if (empty($value))
159 continue;
160
161 switch ($key)
162 {
163 case 'ACTIVE':
164 case 'NAME':
165 case 'SERVER':
166 case 'PORT':
167 case 'LINK':
168 $mbData[$key] = $value;
169 break;
170 case 'ENCRYPTION':
171 $mbData['USE_TLS'] = $value;
172 break;
173 }
174 }
175 }
176
177 $selectResult = \CMailbox::getList(array(), array('SERVICE_ID' => $serviceId));
178 while ($mailbox = $selectResult->fetch())
179 \CMailbox::update($mailbox['ID'], $mbData);
180 }
181
182 return $updateResult;
183 }
184
185 public static function getIconSrc($serviceName, $iconId = null)
186 {
187 if ($iconId)
188 {
189 $icon = \CFile::getFileArray($iconId);
190
191 return $icon['SRC'];
192 }
193 else
194 {
195 $icons = array(
196 'bitrix24' => '/bitrix/images/mail/mailservice-icon/' . Localization\Loc::getMessage('mail_mailservice_bitrix24_icon'),
197 'gmail' => '/bitrix/images/mail/mailservice-icon/post-gmail-icon.svg',
198 'icloud' => '/bitrix/images/mail/mailservice-icon/post-icloud-icon.svg',
199 'outlook.com' => '/bitrix/images/mail/mailservice-icon/post-outlook-icon.svg',
200 'office365' => '/bitrix/images/mail/mailservice-icon/post-office360-icon.svg',
201 'yahoo' => '/bitrix/images/mail/mailservice-icon/post-yahoo-icon.svg',
202 'aol' => '/bitrix/images/mail/mailservice-icon/post-aol-icon.svg',
203 'yandex' => '/bitrix/images/mail/mailservice-icon/post-yandex-icon.svg',
204 'mail.ru' => '/bitrix/images/mail/mailservice-icon/post-mail-icon.svg',
205 'ukr.net' => '/bitrix/images/mail/mailservice-icon/post-ukrnet-icon.png',
206 'exchange' => '/bitrix/images/mail/mailservice-icon/post-imap-icon.svg',
207 'exchangeOnline' => '/bitrix/images/mail/mailservice-icon/post-exchange-icon.svg',
208 'other' => '/bitrix/images/mail/mailservice-icon/post-imap-icon.svg',
209 );
210
211 if ($icons[$serviceName])
212 return $icons[$serviceName];
213 }
214 }
215
216 public static function getOAuthHelper($data)
217 {
218 switch ($data['NAME'])
219 {
220 case 'gmail':
221 return Helper\OAuth\Google::getInstance();
222 case 'yandex':
223 return Helper\OAuth\Yandex::getInstance();
224 case 'mail.ru':
225 return Helper\OAuth\Mailru::getInstance();
226 case 'office365':
227 case 'outlook.com':
228 case 'exchangeOnline':
229 return Helper\OAuth\Office365::getInstance();
230 }
231 }
232
233 public static function delete($primary): Entity\DeleteResult
234 {
235 $serviceForDelete = static::getByPrimary($primary)->fetch();
236 if (!$serviceForDelete)
237 {
238 $deleteResult = new Entity\DeleteResult();
239 $deleteResult->addError(new Entity\EntityError(Localization\Loc::getMessage('mail_mailservice_not_found')));
240
241 return $deleteResult;
242 }
243
244 $deleteResult = parent::delete($primary);
245
246 if ($deleteResult->isSuccess())
247 {
248 $serviceId = is_array($primary) ? $primary['ID'] : $primary;
249
250 if (in_array($serviceForDelete['SERVICE_TYPE'], array('controller', 'domain', 'crdomain')))
251 {
252 $mbData = array('ACTIVE' => 'N', 'SERVICE_ID' => 0);
253 }
254 else
255 {
256 $emptyService = static::getList(array(
257 'filter' => array(
258 '=SITE_ID' => $serviceForDelete['SITE_ID'],
259 'ACTIVE' => 'Y',
260 '=SERVER' => '',
261 '=PORT' => '',
262 '=ENCRYPTION' => '',
263 '=LINK' => ''
264 ),
265 'limit' => 1
266 ))->fetch();
267
268 $mbData = $emptyService
269 ? array('SERVICE_ID' => $emptyService['ID'], 'NAME' => $emptyService['NAME'])
270 : array('ACTIVE' => 'N', 'SERVICE_ID' => 0);
271 }
272
273 $selectResult = \CMailbox::getList(array(), array('SERVICE_ID' => $serviceId));
274 while ($mailbox = $selectResult->fetch())
275 \CMailbox::update($mailbox['ID'], $mbData);
276 }
277
278 return $deleteResult;
279 }
280
281 public static function getMap()
282 {
283 return array(
284 'ID' => array(
285 'data_type' => 'integer',
286 'primary' => true,
287 'autocomplete' => true,
288 ),
289 'SITE_ID' => array(
290 'data_type' => 'string',
291 'title' => Localization\Loc::getMessage('mail_mailservice_entity_site_field'),
292 'required' => true
293 ),
294 'ACTIVE' => array(
295 'data_type' => 'boolean',
296 'title' => Localization\Loc::getMessage('mail_mailservice_entity_active_field'),
297 'values' => array('N', 'Y'),
298 'required' => true
299 ),
300 'SORT' => array(
301 'data_type' => 'integer',
302 'title' => Localization\Loc::getMessage('mail_mailservice_entity_sort_field')
303 ),
304 'SERVICE_TYPE' => array(
305 'data_type' => 'enum',
306 'title' => Localization\Loc::getMessage('mail_mailservice_entity_type_field'),
307 'values' => array('imap', 'controller', 'domain', 'crdomain'),
308 'required' => true
309 ),
310 'NAME' => array(
311 'data_type' => 'string',
312 'title' => Localization\Loc::getMessage('mail_mailservice_entity_name_field'),
313 'required' => true
314 ),
315 'SERVER' => array(
316 'data_type' => 'string',
317 'title' => Localization\Loc::getMessage('mail_mailservice_entity_server_field'),
318 'save_data_modification' => function()
319 {
320 return array(
321 function ($value)
322 {
323 return mb_strtolower($value);
324 }
325 );
326 },
327 'fetch_data_modification' => function()
328 {
329 return array(
330 function ($value)
331 {
332 return mb_strtolower($value);
333 }
334 );
335 },
336 ),
337 'PORT' => array(
338 'data_type' => 'integer',
339 'title' => Localization\Loc::getMessage('mail_mailservice_entity_port_field'),
340 ),
341 'ENCRYPTION' => array(
342 'data_type' => 'boolean',
343 'title' => Localization\Loc::getMessage('mail_mailservice_entity_encryption_field'),
344 'values' => array('N', 'Y'),
345 ),
346 'LINK' => array(
347 'data_type' => 'string',
348 'title' => Localization\Loc::getMessage('mail_mailservice_entity_link_field'),
349 ),
350 'ICON' => array(
351 'data_type' => 'integer',
352 'title' => Localization\Loc::getMessage('mail_mailservice_entity_icon_field'),
353 ),
354 'TOKEN' => array(
355 'data_type' => 'string',
356 'title' => Localization\Loc::getMessage('mail_mailservice_entity_token_field'),
357 ),
358 'FLAGS' => array(
359 'data_type' => 'integer',
360 'title' => Localization\Loc::getMessage('mail_mailservice_entity_flags_field'),
361 ),
362 'SITE' => array(
363 'data_type' => 'Bitrix\Main\Site',
364 'reference' => array('=this.SITE_ID' => 'ref.LID'),
365 ),
366 new Entity\StringField('SMTP_SERVER'),
367 new Entity\IntegerField('SMTP_PORT'),
368 new Entity\BooleanField('SMTP_LOGIN_AS_IMAP', [
369 'values' => array('N', 'Y'),
370 'default_value' => 'N',
371 ]),
372 new Entity\BooleanField('SMTP_PASSWORD_AS_IMAP', [
373 'values' => array('N', 'Y'),
374 'default_value' => 'N',
375 ]),
377 'SMTP_ENCRYPTION',
378 array(
379 'values' => array('N', 'Y'),
380 )
381 ),
383 'UPLOAD_OUTGOING',
384 array(
385 'values' => array('N', 'Y'),
386 )
387 ),
388 );
389 }
390
391}
static getIconSrc($serviceName, $iconId=null)
static checkFields(Entity\Result $result, $primary, array $data)
static update($primary, array $data)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29