1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
syncajax.php
См. документацию.
1<?php
2namespace Bitrix\Calendar\Controller;
3
4use Bitrix\Calendar\Access\ActionDictionary;
5use Bitrix\Calendar\Access\Model\TypeModel;
6use Bitrix\Calendar\Access\SyncAccessController;
7use Bitrix\Calendar\Access\TypeAccessController;
8use Bitrix\Calendar\Controller\Filter\RestrictExternalUser;
9use Bitrix\Calendar\Core\Role\Helper;
10use Bitrix\Calendar\Core\Role\User;
11use Bitrix\Calendar\Internals\Counter\CounterService;
12use Bitrix\Calendar\Internals\Counter\Event\EventDictionary;
13use Bitrix\Calendar\Internals\SectionTable;
14use Bitrix\Calendar\Sync\Google;
15use Bitrix\Calendar\Sync\ICloud;
16use Bitrix\Calendar\Util;
17use Bitrix\Main\Error;
18use Bitrix\Main\HttpApplication;
19use Bitrix\Main\HttpResponse;
20use Bitrix\Main\Loader;
21use Bitrix\Main\LoaderException;
22use Bitrix\Main\Localization\Loc;
23use Bitrix\Calendar\Sync;
24use Bitrix\Main\Engine\ActionFilter;
25use Bitrix\Calendar\Core\Oauth;
26
27Loc::loadMessages(__FILE__);
28
30{
31 public function configureActions()
32 {
33 return [
34 'handleMobileAuth' => [
35 '-prefilters' => [
36 ActionFilter\Authentication::class,
37 ActionFilter\Csrf::class,
38 ],
39 ],
40 'getOutlookLink' => [
41 '+prefilters' => [
43 ],
44 ],
45 ];
46 }
47
48 public function getSyncInfoAction()
49 {
50 $params = [];
51 $request = $this->getRequest();
52 $params['type'] = $request->getPost('type');
53 $params['userId'] = \CCalendar::getCurUserId();
54
55 return \CCalendarSync::GetSyncInfo($params);
56 }
57
58 public function removeConnectionAction($connectionId, $removeCalendars)
59 {
60 $userId = \CCalendar::GetUserId();
61
62 if (
64 $userId,
65 ActionDictionary::ACTION_SYNC_DELETE,
66 $connectionId,
67 )
68 )
69 {
70 return false;
71 }
72
73 \CCalendar::setOwnerId($userId);
74 \CCalendar::RemoveConnection(['id' => (int)$connectionId, 'del_calendars' => $removeCalendars === 'Y']);
75 CounterService::addEvent(EventDictionary::SYNC_CHANGED, ['user_ids' => [$userId]]);
76
77 return true;
78 }
79
83 public function addConnectionAction(): void
84 {
85 $request = $this->getRequest();
86 $params['user_id'] = \CCalendar::getCurUserId();
87 $params['user_name'] = $request['userName'];
88 $params['name'] = $request['name'];
89 $params['link'] = $request['server'];
90 $params['pass'] = $request['pass'];
91
92 foreach ($params as $parameter)
93 {
94 if ($parameter === '')
95 {
96 $this->addError(new Error(Loc::getMessage('EC_CALDAV_URL_ERROR'), 'incorrect_parameters'));
97 break;
98 }
99 }
100
101 if (Loader::IncludeModule('dav'))
102 {
103 $res = \CCalendar::AddConnection($params);
104
105 if ($res === true)
106 {
107 \CDavGroupdavClientCalendar::DataSync("user", $params['user_id']);
108 }
109 else
110 {
111 $this->addError(new Error($res, 'incorrect_parameters'));
112 }
113
114 CounterService::addEvent(EventDictionary::SYNC_CHANGED, ['user_ids' => [$params['user_id']]]);
115 }
116 }
117
128 {
129 $response = [
130 'status' => 'error',
131 'message' => 'Could not finish sync.',
132 ];
133
134 if (!\CCalendar::isGoogleApiEnabled())
135 {
136 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_GOOGLE_API_REQUIRED'), 'google_api_required'));
137
138 return $response;
139 }
140 if (!Loader::includeModule('dav'))
141 {
142 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED'), 'dav_required'));
143
144 return $response;
145 }
146
147 return (new Google\StartSynchronizationManager(\CCalendar::GetCurUserId()))->synchronize();
148 }
149
158 {
159 if (!Loader::includeModule('dav'))
160 {
161 return [
162 'status' => 'error',
163 'message' => 'Module dav is required',
164 ];
165 }
166 if (!Loader::includeModule('socialservices'))
167 {
168 return [
169 'status' => 'error',
170 'message' => 'Module socialservices is required',
171 ];
172 }
173
174 $owner = Helper::getRole(\CCalendar::GetUserId(), User::TYPE);
175
176 return (new Sync\Office365\StartSyncController($owner))->synchronize();
177 }
178
189 public function createIcloudConnectionAction(?string $appleId, ?string $appPassword)
190 {
191 $appleId = trim($appleId);
192 $appPassword = trim($appPassword);
193
194 if (!Loader::includeModule('dav'))
195 {
196 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED')));
197
198 return [
199 'status' => 'error',
200 'message' => Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED'),
201 ];
202 }
203 $typeModel = TypeModel::createFromXmlId(User::TYPE);
204 $accessController = new TypeAccessController(\CCalendar::GetUserId());
205 if (!$accessController->check(ActionDictionary::ACTION_TYPE_EDIT, $typeModel, []))
206 {
207 $this->addError(new Error('Access Denied'));
208
209 return [
210 'status' => 'error',
211 'message' => 'Access Denied',
212 ];
213 }
214 if (!preg_match("/[a-z]{4}-[a-z]{4}-[a-z]{4}-[a-z]{4}/", $appPassword))
215 {
216 $this->addError(new Error('Incorrect app password'));
217
218 return [
219 'status' => 'incorrect_app_pass',
220 'message' => 'Incorrect app password'
221 ];
222 }
223
224 $connectionId = (new Icloud\VendorSyncManager())->initConnection($appleId, $appPassword);
225 if (!$connectionId)
226 {
227 $this->addError(new Error(Loc::getMessage('EC_SYNCALAX_ICLOUD_WRONG_AUTH')));
228
229 return [
230 'status' => 'error',
231 'message' => Loc::getMessage('EC_SYNCALAX_ICLOUD_WRONG_AUTH'),
232 ];
233 }
234
235 return [
236 'status' => 'success',
237 'connectionId' => $connectionId
238 ];
239 }
240
241 public function syncIcloudConnectionAction($connectionId)
242 {
243 if (!Loader::includeModule('dav'))
244 {
245 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED')));
246
247 return [
248 'status' => 'error',
249 'message' => Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED'),
250 ];
251 }
252
253 $result = (new Icloud\VendorSyncManager())->syncIcloudConnection($connectionId);
254
255 if ($result['status'] === 'error' && $result['message'])
256 {
257 $this->addError(new Error($result['message']));
258 }
259
260 return $result;
261 }
262
263 public function updateConnectionAction()
264 {
265 $params = [];
266 $request = $this->getRequest();
267 $params['type'] = $request->getPost('type');
268 $params['userId'] = \CCalendar::getCurUserId();
269 $requestUid = $request->getPost('requestUid');
270 if (!empty($requestUid))
271 {
272 Util::setRequestUid($requestUid);
273 }
274
276
278
279 return \CCalendarSync::GetSyncInfo($params);
280 }
281
288 public function deactivateConnectionAction(int $connectionId, $removeCalendars = 'N'): bool
289 {
290 try
291 {
292 if (!Loader::includeModule('dav'))
293 {
294 return false;
295 }
296
297 return \CCalendarSync::deactivateConnection($connectionId);
298 }
299 catch (\Exception $e)
300 {
301 return false;
302 }
303 }
304
305 public function getAllSectionsForIcloudAction(int $connectionId)
306 {
307 return \CCalendarSect::getAllSectionsForVendor($connectionId, [Sync\Icloud\Helper::ACCOUNT_TYPE]);
308 }
309
310 public function getAllSectionsForOffice365Action(int $connectionId)
311 {
312 return \CCalendarSect::getAllSectionsForVendor($connectionId, [Sync\Office365\Helper::ACCOUNT_TYPE]);
313 }
314
315 public function getAllSectionsForGoogleAction(int $connectionId)
316 {
317 return \CCalendarSect::getAllSectionsForVendor($connectionId, Sync\Google\Dictionary::ACCESS_ROLE_TO_EXTERNAL_TYPE);
318 }
319
320 public function clearSuccessfulConnectionNotifierAction(string $accountType)
321 {
323 (int)\CCalendar::GetUserId(),
324 $accountType
325 );
326 }
327
329 {
330 $userId = \CCalendar::getCurUserId();
331 \CUserOptions::DeleteOption('calendar', 'last_sync_iphone', false, $userId);
332 \CUserOptions::DeleteOption('calendar', 'last_sync_mac', false, $userId);
333 }
334
336 {
337 CUserOptions::SetOption('calendar', 'showGoogleApplicationRefused', 'N');
338 }
339
340 public function getOutlookLinkAction(int $id)
341 {
342 $result = '';
343 $section = SectionTable::query()
344 ->setSelect(['XML_ID', 'CAL_TYPE', 'NAME', 'OWNER_ID'])
345 ->where('ID', $id)
346 ->exec()->fetchObject()
347 ;
348
349 if ($section)
350 {
351 $result = \CCalendarSect::GetOutlookLink([
352 'ID' => $section->getId(),
353 'XML_ID' => $section->getXmlId(),
354 'TYPE' => $section->getCalType(),
355 'NAME' => $section->getName(),
356 'PREFIX' => \CCalendar::GetOwnerName($section->getCalType(), $section->getOwnerId()),
357 'LINK_URL' => \CCalendar::GetOuterUrl()
358 ]);
359 }
360
361 return ['result' => $result];
362 }
363
364 public function getOauthConnectionLinkAction(string $serviceName): array
365 {
366 $result = [];
367
368 if (Loader::includeModule('intranet') && !\Bitrix\Intranet\Util::isIntranetUser())
369 {
370 $this->addError(new Error('Access denied', 403));
371
372 return $result;
373 }
374
375 $oauthEntity = Oauth\Factory::getInstance()->getByName($serviceName);
376 if ($oauthEntity && $url = $oauthEntity->getUrl())
377 {
378 $result['connectionLink'] = $url;
379 }
380 else
381 {
382 $this->addError(new Error('Link not found', 404));
383 }
384
385 return $result;
386 }
387
388 public function handleMobileAuthAction(string $serviceName, string $hitHash): HttpResponse
389 {
390 $httpResponse = new HttpResponse();
391 $httpResponse->addHeader('Location', 'bitrix24://');
392
393 if (empty($serviceName) || empty($hitHash))
394 {
395 return $httpResponse;
396 }
397
398 if (!$GLOBALS['USER']->LoginHitByHash($hitHash, false, true))
399 {
400 return $httpResponse;
401 }
402
403 HttpApplication::getInstance()->getSession()->set('MOBILE_OAUTH', true);
404
405 $oauthEntity = Oauth\Factory::getInstance()->getByName($serviceName);
406 if ($oauthEntity && $url = $oauthEntity->getUrl())
407 {
408 return $this->redirectTo($url)->setSkipSecurity(true);
409 }
410
411 return $httpResponse;
412 }
413}
$accessController
Определения options.php:23
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
clearSuccessfulConnectionNotifierAction(string $accountType)
Определения syncajax.php:320
removeConnectionAction($connectionId, $removeCalendars)
Определения syncajax.php:58
createIcloudConnectionAction(?string $appleId, ?string $appPassword)
Определения syncajax.php:189
disableShowGoogleApplicationRefusedAction()
Определения syncajax.php:335
handleMobileAuthAction(string $serviceName, string $hitHash)
Определения syncajax.php:388
getAllSectionsForOffice365Action(int $connectionId)
Определения syncajax.php:310
getAllSectionsForGoogleAction(int $connectionId)
Определения syncajax.php:315
getAllSectionsForIcloudAction(int $connectionId)
Определения syncajax.php:305
getOauthConnectionLinkAction(string $serviceName)
Определения syncajax.php:364
createOffice365ConnectionAction()
Определения syncajax.php:157
getOutlookLinkAction(int $id)
Определения syncajax.php:340
disableIphoneOrMacConnectionAction()
Определения syncajax.php:328
deactivateConnectionAction(int $connectionId, $removeCalendars='N')
Определения syncajax.php:288
syncIcloudConnectionAction($connectionId)
Определения syncajax.php:241
static clearFinishedSyncNotificationAgent(int $userId, string $vendorName)
Определения notificationmanager.php:92
static setRequestUid(string $requestUid='')
Определения util.php:529
addError(Error $error)
Определения controller.php:1070
Определения error.php:15
static UpdateUserConnections()
Определения calendar_sync.php:1214
</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
static can($userId, string $action, $itemId=null, $params=null)
$GLOBALS['____1690880296']
Определения license.php:1
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$response
Определения result.php:21
$url
Определения iframe.php:7