Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rest.php
1<?php
2namespace Bitrix\Pull;
3
6
7if(!\Bitrix\Main\Loader::includeModule('rest'))
8{
9 return;
10}
11
12
13class Rest extends \IRestService
14{
15 public static function onRestServiceBuildDescription()
16 {
17 return array(
18 'pull' => array(
19 'pull.application.config.get' => array('callback' => array(__CLASS__, 'applicationConfigGet')),
20 'pull.application.event.add' => array('callback' => array(__CLASS__, 'applicationEventAdd')),
21 'pull.application.push.add' => array('callback' => array(__CLASS__, 'applicationPushAdd')),
22 'pull.watch.extend' => array('callback' => array(__CLASS__, 'watchExtend'), 'options' => array()),
23 ),
24 'pull_channel' => array(
25 'pull.config.get' => array('callback' => array(__CLASS__, 'configGet'), 'options' => array()),
26 'pull.channel.public.get' => array('callback' => array(__CLASS__, 'channelPublicGet'), 'options' => array()),
27 'pull.channel.public.list' => array('callback' => array(__CLASS__, 'channelPublicList'), 'options' => array()),
28 ),
29 'mobile' => Array(
30 'mobile.counter.types.get' => array('callback' => array(__CLASS__, 'counterTypesGet'), 'options' => array()),
31 'mobile.counter.get' => array('callback' => array(__CLASS__, 'counterGet'), 'options' => array()),
32 'mobile.counter.config.get' => array('callback' => array(__CLASS__, 'counterConfigGet'), 'options' => array()),
33 'mobile.counter.config.set' => array('callback' => array(__CLASS__, 'counterConfigSet'), 'options' => array()),
34
35 'mobile.push.types.get' => array('callback' => array(__CLASS__, 'pushTypesGet'), 'options' => array()),
36 'mobile.push.config.get' => array('callback' => array(__CLASS__, 'pushConfigGet'), 'options' => array()),
37 'mobile.push.config.set' => array('callback' => array(__CLASS__, 'pushConfigSet'), 'options' => array()),
38 'mobile.push.status.get' => array('callback' => array(__CLASS__, 'pushStatusGet'), 'options' => array()),
39 'mobile.push.status.set' => array('callback' => array(__CLASS__, 'pushStatusSet'), 'options' => array()),
40 'mobile.push.smartfilter.status.get' => array('callback' => array(__CLASS__, 'pushSmartfilterStatusGet'), 'options' => array()),
41 'mobile.push.smartfilter.status.set' => array('callback' => array(__CLASS__, 'pushSmartfilterStatusSet'), 'options' => array()),
42 )
43 );
44 }
45
46 public static function channelPublicGet($params, $n, \CRestServer $server)
47 {
48 $params = array_change_key_case($params, CASE_UPPER);
49
50 $type = \CPullChannel::TYPE_PRIVATE;
51 if ($params['APPLICATION'] === 'Y')
52 {
53 $clientId = $server->getClientId();
54 if (!$clientId)
55 {
56 throw new \Bitrix\Rest\RestException("Get application public channel available only for application authorization.", "WRONG_AUTH_TYPE", \CRestServer::STATUS_WRONG_REQUEST);
57 }
58 $type = $clientId;
59 }
60
61 $userId = (int)$params['USER_ID'];
62
63 $configParams = Array();
64 $configParams['TYPE'] = $type;
65 $configParams['USER_ID'] = $userId;
66 $configParams['JSON'] = true;
67
68 $config = \Bitrix\Pull\Channel::getPublicId($configParams);
69 if (!$config)
70 {
71 throw new \Bitrix\Rest\RestException("Push & Pull server is not configured", "SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
72 }
73
74 return $config;
75 }
76
77 public static function channelPublicList($params, $n, \CRestServer $server)
78 {
79 $params = array_change_key_case($params, CASE_UPPER);
80
81 $type = \CPullChannel::TYPE_PRIVATE;
82 if ($params['APPLICATION'] === 'Y')
83 {
84 $clientId = $server->getClientId();
85 if (!$clientId)
86 {
87 throw new \Bitrix\Rest\RestException("Get application public channel available only for application authorization.", "WRONG_AUTH_TYPE", \CRestServer::STATUS_WRONG_REQUEST);
88 }
89 $type = $clientId;
90 }
91
92 $users = Array();
93 if (is_string($params['USERS']))
94 {
95 $params['USERS'] = \CUtil::JsObjectToPhp($params['USERS']);
96 }
97 if (is_array($params['USERS']))
98 {
99 foreach ($params['USERS'] as $userId)
100 {
101 $userId = (int)$userId;
102 if ($userId > 0)
103 {
104 $users[$userId] = $userId;
105 }
106 }
107 }
108
109 if (empty($users))
110 {
111 throw new \Bitrix\Rest\RestException("A wrong format for the USERS field is passed", "INVALID_FORMAT", \CRestServer::STATUS_WRONG_REQUEST);
112 }
113
114 $configParams = Array();
115 $configParams['TYPE'] = $type;
116 $configParams['USERS'] = $users;
117 $configParams['JSON'] = true;
118
119 $config = \Bitrix\Pull\Channel::getPublicIds($configParams);
120 if (!$config)
121 {
122 throw new \Bitrix\Rest\RestException("Push & Pull server is not configured", "SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
123 }
124
125 return $config;
126 }
127
128 public static function applicationConfigGet($params, $n, \CRestServer $server)
129 {
130 $params = array_change_key_case($params, CASE_UPPER);
131
132 $clientId = $server->getClientId();
133 if (!$clientId)
134 {
135 throw new \Bitrix\Rest\RestException("Get access to application config available only for application authorization.", "WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
136 }
137
138 $configParams = Array();
139 $configParams['CACHE'] = $params['CACHE'] !== 'N';
140 $configParams['REOPEN'] = $params['REOPEN'] !== 'N';
141 $configParams['CUSTOM_TYPE'] = $clientId;
142 $configParams['JSON'] = true;
143
144 $config = \Bitrix\Pull\Config::get($configParams);
145 if (!$config)
146 {
147 throw new \Bitrix\Rest\RestException("Push & Pull server is not configured", "SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
148 }
149
150 return $config;
151 }
152
153 public static function applicationEventAdd($params, $n, \CRestServer $server)
154 {
155 $params = array_change_key_case($params, CASE_UPPER);
156
157 $clientId = $server->getClientId();
158 if (!$clientId)
159 {
160 throw new \Bitrix\Rest\RestException("Get access to application config available only for application authorization.", "WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
161 }
162
163 global $USER;
164
165 if (self::isAdmin())
166 {
167 $users = Array();
168 if (is_string($params['USER_ID']))
169 {
170 $params['USER_ID'] = \CUtil::JsObjectToPhp($params['USER_ID']);
171 }
172
173 if (!isset($params['USER_ID']))
174 {
175 $users = [Event::SHARED_CHANNEL];
176 }
177 else if (is_array($params['USER_ID']))
178 {
179 foreach ($params['USER_ID'] as $userId)
180 {
181 $userId = intval($userId);
182 if ($userId > 0)
183 {
184 $users[$userId] = $userId;
185 }
186 }
187 $users = array_values($users);
188 }
189 else
190 {
191 $users = (int)$params['USER_ID'];
192 }
193 }
194 else
195 {
196 if (isset($params['USER_ID']))
197 {
198 if ($params['USER_ID'] === $USER->GetId())
199 {
200 $users = $USER->GetId();
201 }
202 else
203 {
204 throw new \Bitrix\Rest\RestException("Only admin can send notifications to other channels", "USER_ID_ACCESS_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
205 }
206 }
207 else
208 {
209 $users = [Event::SHARED_CHANNEL];
210 }
211 }
212
213 if (isset($params['MODULE_ID']))
214 {
215 $moduleId = $params['MODULE_ID'];
216 if (preg_match("/[^a-z0-9._]/", $moduleId))
217 {
218 throw new \Bitrix\Rest\RestException("Module ID format error", "MODULE_ID_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
219 }
220 }
221 else
222 {
223 $moduleId = 'application';
224 }
225
226 $command = $params['COMMAND'];
227 if (!preg_match("/^[\d\w:_|\.\-]+$/", $command))
228 {
229 throw new \Bitrix\Rest\RestException("Command format error", "COMMAND_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
230 }
231
232 if (isset($params['PARAMS']))
233 {
234 if (is_string($params['PARAMS']))
235 {
236 $params['PARAMS'] = \CUtil::JsObjectToPhp($params['PARAMS']);
237 }
238
239 $eventParams = $params['PARAMS'];
240 if (!is_array($eventParams))
241 {
242 throw new \Bitrix\Rest\RestException("Params format error", "PARAMS_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
243 }
244 }
245 else
246 {
247 $eventParams = [];
248 }
249
250 Event::add($users, array(
251 'module_id' => $moduleId,
252 'command' => $command,
253 'params' => $eventParams,
254 ), $clientId);
255
256 return true;
257 }
258
259 public static function applicationPushAdd($params, $n, \CRestServer $server)
260 {
261 $params = array_change_key_case($params, CASE_UPPER);
262
263 $clientId = $server->getClientId();
264 if (!$clientId)
265 {
266 throw new \Bitrix\Rest\RestException("Send push notifications available only for application authorization.", "WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
267 }
268
269 if (!self::isAdmin())
270 {
271 throw new \Bitrix\Rest\RestException("You do not have access to send push notifications", "ACCESS_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
272 }
273
274 $users = Array();
275 if (is_string($params['USER_ID']))
276 {
277 $params['USER_ID'] = \CUtil::JsObjectToPhp($params['USER_ID']);
278 }
279
280 if (is_array($params['USER_ID']))
281 {
282 foreach ($params['USER_ID'] as $userId)
283 {
284 $userId = intval($userId);
285 if ($userId > 0)
286 {
287 $users[$userId] = $userId;
288 }
289 }
290 $users = array_values($users);
291 }
292 else
293 {
294 $users = (int)$params['USER_ID'];
295 }
296
297 if (!$params['TEXT'] || $params['TEXT'] == '')
298 {
299 throw new \Bitrix\Rest\RestException("Text can't be empty", "TEXT_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
300 }
301
302 $result = \Bitrix\Rest\AppTable::getList(
303 array(
304 'filter' => array(
305 '=CLIENT_ID' => $clientId
306 ),
307 'select' => array(
308 'CODE',
309 'APP_NAME',
310 'APP_NAME_DEFAULT' => 'LANG_DEFAULT.MENU_NAME',
311 )
312 )
313 )->fetch();
314 if (empty($result['APP_NAME']))
315 {
316 throw new \Bitrix\Rest\RestException("For send push-notification application name can't be empty", "EMPTY_APP_NAME", \CRestServer::STATUS_WRONG_REQUEST);
317 }
318
319 $appName = $result['APP_NAME'];
320
321 $text = $params['TEXT'];
322
323 $avatar = '';
324 if ($params['AVATAR'])
325 {
326 $parsedUrl = new Main\Web\Uri($params['AVATAR']);
327 $params['AVATAR'] = $parsedUrl->getUri();
328 if ($params['AVATAR'])
329 {
330 $avatar = $params['AVATAR'];
331 }
332 }
333
334 Push::add($users,
335 [
336 'module_id' => 'im',
337 'push' =>
338 [
339 'message' => $text,
340 'advanced_params' =>
341 [
342 "group"=> $clientId,
343 "avatarUrl"=> $avatar,
344 "senderName" => $appName,
345 "senderMessage" => $text,
346 ],
347 ]
348 ]);
349
350 return true;
351 }
352
353 public static function configGet($params, $n, \CRestServer $server)
354 {
355 $params = array_change_key_case($params, CASE_UPPER);
356
357 if ($server->getAuthType() === \Bitrix\Rest\OAuth\Auth::AUTH_TYPE)
358 {
359 throw new \Bitrix\Rest\RestException("Method not available for OAuth authorization.", "WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
360 }
361
362 global $USER;
363 $guestMode = defined("PULL_USER_ID") && (int)PULL_USER_ID != 0;
364 if($server->getAuthType() === GuestAuth::AUTH_TYPE && $guestMode)
365 {
366 $userId = (int)PULL_USER_ID;
367 }
368 else if ($USER->IsAuthorized())
369 {
370 $userId = $USER->getId();
371 }
372 else
373 {
374 throw new \Bitrix\Rest\RestException("Method not available for guest session at the moment.", "AUTHORIZE_ERROR", \CRestServer::STATUS_FORBIDDEN);
375 }
376
377 $configParams = Array();
378 $configParams['USER_ID'] = $userId;
379 $configParams['CACHE'] = !isset($params['CACHE']) || $params['CACHE'] !== 'N';
380 $configParams['REOPEN'] = !isset($params['REOPEN']) || $params['REOPEN'] !== 'N';
381 $configParams['JSON'] = true;
382
383 $config = \Bitrix\Pull\Config::get($configParams);
384 if (!$config)
385 {
386 throw new \Bitrix\Rest\RestException("Push & Pull server is not configured", "SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
387 }
388
389 $config['serverTime'] = date('c', time());
390
391 return $config;
392 }
393
394 public static function watchExtend($params, $n, \CRestServer $server)
395 {
396 $params = array_change_key_case($params, CASE_UPPER);
397
398 if(is_string($params['TAGS']))
399 {
400 $params['TAGS'] = \CUtil::JsObjectToPhp($params['TAGS']);
401 }
402
403 global $USER;
404 $userId = $USER->GetID();
405
406 return \CPullWatch::Extend($userId, array_values($params['TAGS']));
407 }
408
409 public static function counterTypesGet($params, $n, \CRestServer $server)
410 {
411 $types = \Bitrix\Pull\MobileCounter::getTypes();
412
413 if (isset($params['USER_VALUES']) && $params['USER_VALUES'] === 'Y')
414 {
415 $config = \Bitrix\Pull\MobileCounter::getConfig();
416 foreach ($types as $type => $value)
417 {
418 $types[$type]['VALUE'] = $config[$type];
419 }
420 }
421
422 $result = Array();
423 foreach ($types as $type)
424 {
425 $result[] = array_change_key_case($type, CASE_LOWER);
426 }
427
428 return $result;
429 }
430
431 public static function counterGet($params, $n, \CRestServer $server)
432 {
433 return \Bitrix\Pull\MobileCounter::get();
434 }
435
436 public static function counterConfigGet($params, $n, \CRestServer $server)
437 {
438 $result = Array();
439 $config = \Bitrix\Pull\MobileCounter::getConfig();
440 foreach ($config as $type => $value)
441 {
442 $result[] = Array(
443 'type' => $type,
444 'value' => $value,
445 );
446 }
447
448 return $result;
449 }
450
451 public static function counterConfigSet($params, $n, \CRestServer $server)
452 {
453 $params = array_change_key_case($params, CASE_UPPER);
454
455 if(is_string($params['CONFIG']))
456 {
457 $params['CONFIG'] = \CUtil::JsObjectToPhp($params['CONFIG']);
458 }
459
460 if (!is_array($params['CONFIG']) || empty($params['CONFIG']))
461 {
462 throw new \Bitrix\Rest\RestException("New config is not specified", "CONFIG_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
463 }
464
465 \Bitrix\Pull\MobileCounter::setConfig($params['CONFIG']);
466
467 return true;
468 }
469
470
471 public static function pushTypesGet($params, $n, \CRestServer $server)
472 {
473 $params = array_change_key_case($params, CASE_UPPER);
474
475 $userConfig = Array();
476 $config = \Bitrix\Pull\Push::getTypes();
477
478 $withUserValues = false;
479 if (isset($params['USER_VALUES']) && $params['USER_VALUES'] === 'Y')
480 {
481 $withUserValues = true;
482 $userConfig = \Bitrix\Pull\Push::getConfig();
483 }
484
485 $result = Array();
486 foreach ($config as $moduleId => $module)
487 {
488
489 $types = Array();
490 foreach ($module['TYPES'] as $typeId => $typeConfig)
491 {
492 if ($withUserValues)
493 {
494 $typeConfig['VALUE'] = $userConfig[$moduleId][$typeId];
495 }
496 $types[] = array_change_key_case($typeConfig, CASE_LOWER);
497 }
498 $module['TYPES'] = $types;
499
500 $result[] = array_change_key_case($module, CASE_LOWER);
501 }
502
503 \Bitrix\Main\Type\Collection::sortByColumn($result, array('module_id' => array(SORT_STRING, SORT_ASC)));
504
505 return $result;
506 }
507
508 public static function pushConfigGet($params, $n, \CRestServer $server)
509 {
510 $result = array();
511 $config = \Bitrix\Pull\Push::getConfig();
512 if (!$config)
513 {
514 $config = Array();
515 }
516
517 foreach ($config as $moduleId => $module)
518 {
519 foreach ($module as $typeId => $typeValue)
520 {
521 $result[] = Array(
522 'module_id' => $moduleId,
523 'type' => $typeId,
524 'active' => $typeValue
525 );
526 }
527 }
528 return $result;
529 }
530
531 public static function pushConfigSet($params, $n, \CRestServer $server)
532 {
533 $params = array_change_key_case($params, CASE_UPPER);
534
535 if(is_string($params['CONFIG']))
536 {
537 $params['CONFIG'] = \CUtil::JsObjectToPhp($params['CONFIG']);
538 }
539
540 if (!is_array($params['CONFIG']) || empty($params['CONFIG']))
541 {
542 throw new \Bitrix\Rest\RestException("New config is not specified", "CONFIG_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
543 }
544
545 $newConfig = Array();
546 foreach ($params['CONFIG'] as $config)
547 {
548 if (
549 !isset($config['module_id']) || empty($config['module_id'])
550 || !isset($config['type']) || empty($config['type'])
551 || !isset($config['active'])
552 )
553 {
554 continue;
555 }
556
557 $newConfig[$config['module_id']][$config['type']] = (bool)$config['active'];
558 }
559
560 \Bitrix\Pull\Push::setConfig($newConfig);
561
562 return true;
563 }
564
565 public static function pushStatusGet($params, $n, \CRestServer $server)
566 {
567 return \Bitrix\Pull\Push::getStatus();
568 }
569
570 public static function pushStatusSet($params, $n, \CRestServer $server)
571 {
572 $params = array_change_key_case($params, CASE_UPPER);
573
574 $status = (bool)$params['ACTIVE'];
575 \Bitrix\Pull\Push::setStatus($status);
576
577 return true;
578 }
579
580 public static function pushSmartfilterStatusGet($params, $n, \CRestServer $server)
581 {
582 return \Bitrix\Pull\PushSmartfilter::getStatus();
583 }
584
585 public static function pushSmartfilterStatusSet($params, $n, \CRestServer $server)
586 {
587 $params = array_change_key_case($params, CASE_UPPER);
588
589 $status = (bool)$params['ACTIVE'];
590
591 \Bitrix\Pull\PushSmartfilter::setStatus($status);
592
593 return true;
594 }
595
596 public static function notImplemented($params, $n, \CRestServer $server)
597 {
598 throw new \Bitrix\Rest\RestException("Method isn't implemented yet", "NOT_IMPLEMENTED", \CRestServer::STATUS_NOT_FOUND);
599 }
600
601 private static function isAdmin()
602 {
603 global $USER;
604 return (
605 isset($USER)
606 && is_object($USER)
607 && (
608 $USER->isAdmin()
609 || Main\Loader::includeModule('bitrix24') && \CBitrix24::isPortalAdmin($USER->getID())
610 )
611 );
612 }
613}
static includeModule($moduleName)
Definition loader.php:69
static onRestServiceBuildDescription()
Definition rest.php:15
static counterConfigSet($params, $n, \CRestServer $server)
Definition rest.php:451
static pushConfigGet($params, $n, \CRestServer $server)
Definition rest.php:508
static pushConfigSet($params, $n, \CRestServer $server)
Definition rest.php:531
static channelPublicList($params, $n, \CRestServer $server)
Definition rest.php:77
static channelPublicGet($params, $n, \CRestServer $server)
Definition rest.php:46
static counterGet($params, $n, \CRestServer $server)
Definition rest.php:431
static applicationEventAdd($params, $n, \CRestServer $server)
Definition rest.php:153
static pushSmartfilterStatusGet($params, $n, \CRestServer $server)
Definition rest.php:580
static notImplemented($params, $n, \CRestServer $server)
Definition rest.php:596
static pushSmartfilterStatusSet($params, $n, \CRestServer $server)
Definition rest.php:585
static pushStatusGet($params, $n, \CRestServer $server)
Definition rest.php:565
static configGet($params, $n, \CRestServer $server)
Definition rest.php:353
static pushStatusSet($params, $n, \CRestServer $server)
Definition rest.php:570
static pushTypesGet($params, $n, \CRestServer $server)
Definition rest.php:471
static counterTypesGet($params, $n, \CRestServer $server)
Definition rest.php:409
static watchExtend($params, $n, \CRestServer $server)
Definition rest.php:394
static applicationPushAdd($params, $n, \CRestServer $server)
Definition rest.php:259
static counterConfigGet($params, $n, \CRestServer $server)
Definition rest.php:436
static applicationConfigGet($params, $n, \CRestServer $server)
Definition rest.php:128