Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
syncajax.php
1<?php
3
11use Bitrix\Calendar\Sync\ICloud;
22
23Loc::loadMessages(__FILE__);
24
26{
27 public function configureActions()
28 {
29 return [
30 'handleMobileAuth' => [
31 '-prefilters' => [
32 ActionFilter\Authentication::class,
33 ActionFilter\Csrf::class,
34 ],
35 ],
36 ];
37 }
38
39 public function getSyncInfoAction()
40 {
41 $params = [];
42 $request = $this->getRequest();
43 $params['type'] = $request->getPost('type');
44 $params['userId'] = \CCalendar::getCurUserId();
45
46 return \CCalendarSync::GetSyncInfo($params);
47 }
48
49 public function removeConnectionAction($connectionId, $removeCalendars)
50 {
51 \CCalendar::setOwnerId(\CCalendar::getCurUserId());
52 \CCalendar::RemoveConnection(['id' => (int)$connectionId, 'del_calendars' => $removeCalendars === 'Y']);
53
54 return true;
55 }
56
60 public function addConnectionAction(): void
61 {
62 $request = $this->getRequest();
63 $params['user_id'] = \CCalendar::getCurUserId();
64 $params['user_name'] = $request['userName'];
65 $params['name'] = $request['name'];
66 $params['link'] = $request['server'];
67 $params['pass'] = $request['pass'];
68
69 foreach ($params as $parameter)
70 {
71 if ($parameter === '')
72 {
73 $this->addError(new Error(Loc::getMessage('EC_CALDAV_URL_ERROR'), 'incorrect_parameters'));
74 break;
75 }
76 }
77
78 if (Loader::IncludeModule('dav'))
79 {
80 $res = \CCalendar::AddConnection($params);
81
82 if ($res === true)
83 {
84 \CDavGroupdavClientCalendar::DataSync("user", $params['user_id']);
85 }
86 else
87 {
88 $this->addError(new Error($res, 'incorrect_parameters'));
89 }
90 }
91 }
92
102 public function createGoogleConnectionAction(): array
103 {
104 $response = [
105 'status' => 'error',
106 'message' => 'Could not finish sync.',
107 ];
108
109 if (!\CCalendar::isGoogleApiEnabled())
110 {
111 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_GOOGLE_API_REQUIRED'), 'google_api_required'));
112
113 return $response;
114 }
115 if (!Loader::includeModule('dav'))
116 {
117 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED'), 'dav_required'));
118
119 return $response;
120 }
121
122 return (new Google\StartSynchronizationManager(\CCalendar::GetCurUserId()))->synchronize();
123 }
124
132 public function createOffice365ConnectionAction(): array
133 {
134 if (!Loader::includeModule('dav'))
135 {
136 return [
137 'status' => 'error',
138 'message' => 'Module dav is required',
139 ];
140 }
141 if (!Loader::includeModule('socialservices'))
142 {
143 return [
144 'status' => 'error',
145 'message' => 'Module socialservices is required',
146 ];
147 }
148
149 $owner = Helper::getRole(\CCalendar::GetUserId(), User::TYPE);
150
151 return (new Sync\Office365\StartSyncController($owner))->synchronize();
152 }
153
164 public function createIcloudConnectionAction(?string $appleId, ?string $appPassword)
165 {
166 $appleId = trim($appleId);
167 $appPassword = trim($appPassword);
168
169 if (!Loader::includeModule('dav'))
170 {
171 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED')));
172
173 return [
174 'status' => 'error',
175 'message' => Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED'),
176 ];
177 }
178 $typeModel = TypeModel::createFromXmlId(User::TYPE);
179 $accessController = new TypeAccessController(\CCalendar::GetUserId());
180 if (!$accessController->check(ActionDictionary::ACTION_TYPE_EDIT, $typeModel, []))
181 {
182 $this->addError(new Error('Access Denied'));
183
184 return [
185 'status' => 'error',
186 'message' => 'Access Denied',
187 ];
188 }
189 if (!preg_match("/[a-z]{4}-[a-z]{4}-[a-z]{4}-[a-z]{4}/", $appPassword))
190 {
191 $this->addError(new Error('Incorrect app password'));
192
193 return [
194 'status' => 'incorrect_app_pass',
195 'message' => 'Incorrect app password'
196 ];
197 }
198
199 $connectionId = (new Icloud\VendorSyncManager())->initConnection($appleId, $appPassword);
200 if (!$connectionId)
201 {
202 $this->addError(new Error(Loc::getMessage('EC_SYNCALAX_ICLOUD_WRONG_AUTH')));
203
204 return [
205 'status' => 'error',
206 'message' => Loc::getMessage('EC_SYNCALAX_ICLOUD_WRONG_AUTH'),
207 ];
208 }
209
210 return [
211 'status' => 'success',
212 'connectionId' => $connectionId
213 ];
214 }
215
216 public function syncIcloudConnectionAction($connectionId)
217 {
218 if (!Loader::includeModule('dav'))
219 {
220 $this->addError(new Error(Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED')));
221
222 return [
223 'status' => 'error',
224 'message' => Loc::getMessage('EC_SYNCAJAX_DAV_REQUIRED'),
225 ];
226 }
227
228 $result = (new Icloud\VendorSyncManager())->syncIcloudConnection($connectionId);
229
230 if ($result['status'] === 'error' && $result['message'])
231 {
232 $this->addError(new Error($result['message']));
233 }
234
235 return $result;
236 }
237
238 public function updateConnectionAction()
239 {
240 $params = [];
241 $request = $this->getRequest();
242 $params['type'] = $request->getPost('type');
243 $params['userId'] = \CCalendar::getCurUserId();
244 $requestUid = $request->getPost('requestUid');
245 if (!empty($requestUid))
246 {
247 Util::setRequestUid($requestUid);
248 }
249
250 \CCalendarSync::UpdateUserConnections();
251
253
254 return \CCalendarSync::GetSyncInfo($params);
255 }
256
257 public function getAuthLinkAction()
258 {
259 $type = $this->getRequest()->getPost('type');
260 $type = in_array($type, ['slider', 'banner'], true)
261 ? $type
262 : 'banner'
263 ;
264 if (\Bitrix\Main\Loader::includeModule("mobile"))
265 {
266 return ['link' => \Bitrix\Mobile\Deeplink::getAuthLink("calendar_sync_".$type)];
267 }
268 return null;
269 }
270
277 public function deactivateConnectionAction(int $connectionId, $removeCalendars = 'N'): bool
278 {
279 try
280 {
281 if (!Loader::includeModule('dav'))
282 {
283 return false;
284 }
285
286 return \CCalendarSync::deactivateConnection($connectionId);
287 }
288 catch (\Exception $e)
289 {
290 return false;
291 }
292 }
293
294 public function getAllSectionsForIcloudAction(int $connectionId)
295 {
296 return \CCalendarSect::getAllSectionsForVendor($connectionId, [Sync\Icloud\Helper::ACCOUNT_TYPE]);
297 }
298
299 public function getAllSectionsForOffice365Action(int $connectionId)
300 {
301 return \CCalendarSect::getAllSectionsForVendor($connectionId, [Sync\Office365\Helper::ACCOUNT_TYPE]);
302 }
303
304 public function getAllSectionsForGoogleAction(int $connectionId)
305 {
306 return \CCalendarSect::getAllSectionsForVendor($connectionId, Sync\Google\Dictionary::ACCESS_ROLE_TO_EXTERNAL_TYPE);
307 }
308
309 public function clearSuccessfulConnectionNotifierAction(string $accountType)
310 {
311 \Bitrix\Calendar\Sync\Managers\NotificationManager::clearFinishedSyncNotificationAgent(
312 (int)\CCalendar::GetUserId(),
313 $accountType
314 );
315 }
316
318 {
319 $userId = \CCalendar::getCurUserId();
320 \CUserOptions::DeleteOption('calendar', 'last_sync_iphone', false, $userId);
321 \CUserOptions::DeleteOption('calendar', 'last_sync_mac', false, $userId);
322 }
323
325 {
326 CUserOptions::SetOption('calendar', 'showGoogleApplicationRefused', 'N');
327 }
328
329 public function getOutlookLinkAction(int $id)
330 {
331 $result = '';
332 $section = SectionTable::query()
333 ->setSelect(['XML_ID', 'CAL_TYPE', 'NAME', 'OWNER_ID'])
334 ->where('ID', $id)
335 ->exec()->fetchObject()
336 ;
337
338 if ($section)
339 {
340 $result = \CCalendarSect::GetOutlookLink([
341 'ID' => $section->getId(),
342 'XML_ID' => $section->getXmlId(),
343 'TYPE' => $section->getCalType(),
344 'NAME' => $section->getName(),
345 'PREFIX' => \CCalendar::GetOwnerName($section->getCalType(), $section->getOwnerId()),
346 'LINK_URL' => \CCalendar::GetOuterUrl()
347 ]);
348 }
349
350 return ['result' => $result];
351 }
352
353 public function getOauthConnectionLinkAction(string $serviceName): array
354 {
355 $result = [];
356
357 if (Loader::includeModule('intranet') && !\Bitrix\Intranet\Util::isIntranetUser())
358 {
359 $this->addError(new Error('Access denied', 403));
360
361 return $result;
362 }
363
364 $oauthEntity = Oauth\Factory::getInstance()->getByName($serviceName);
365 if ($oauthEntity && $url = $oauthEntity->getUrl())
366 {
367 $result['connectionLink'] = $url;
368 }
369 else
370 {
371 $this->addError(new Error('Link not found', 404));
372 }
373
374 return $result;
375 }
376
377 public function handleMobileAuthAction(string $serviceName, string $hitHash): HttpResponse
378 {
379 $httpResponse = new HttpResponse();
380 $httpResponse->addHeader('Location', 'bitrix24://');
381
382 if (empty($serviceName) || empty($hitHash))
383 {
384 return $httpResponse;
385 }
386
387 if (!$GLOBALS['USER']->LoginHitByHash($hitHash, false, true))
388 {
389 return $httpResponse;
390 }
391
392 HttpApplication::getInstance()->getSession()->set('MOBILE_OAUTH', true);
393
394 $oauthEntity = Oauth\Factory::getInstance()->getByName($serviceName);
395 if ($oauthEntity && $url = $oauthEntity->getUrl())
396 {
397 return $this->redirectTo($url)->setSkipSecurity(true);
398 }
399
400 return $httpResponse;
401 }
402}
clearSuccessfulConnectionNotifierAction(string $accountType)
Definition syncajax.php:309
removeConnectionAction($connectionId, $removeCalendars)
Definition syncajax.php:49
createIcloudConnectionAction(?string $appleId, ?string $appPassword)
Definition syncajax.php:164
handleMobileAuthAction(string $serviceName, string $hitHash)
Definition syncajax.php:377
getAllSectionsForOffice365Action(int $connectionId)
Definition syncajax.php:299
getAllSectionsForGoogleAction(int $connectionId)
Definition syncajax.php:304
getAllSectionsForIcloudAction(int $connectionId)
Definition syncajax.php:294
getOauthConnectionLinkAction(string $serviceName)
Definition syncajax.php:353
deactivateConnectionAction(int $connectionId, $removeCalendars='N')
Definition syncajax.php:277
syncIcloudConnectionAction($connectionId)
Definition syncajax.php:216
static setRequestUid(string $requestUid='')
Definition util.php:512
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1444769544']
Definition license.php:1