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