Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
config.php
1<?php
2namespace Bitrix\Pull;
3
8
9
10class Config
11{
12 public const ONE_YEAR = 31536000; //60 * 60 * 24 * 365;
13
14 public static function get(array $params = [])
15 {
16 if (!\CPullOptions::GetQueueServerStatus())
17 return false;
18
19 $userId = (int)($params['USER_ID'] ?? 0);
20 if (isset($params['CHANNEL']) && !($params['CHANNEL'] instanceof \Bitrix\Pull\Model\Channel))
21 {
22 throw new ArgumentException('$params["CHANNEL"] should be instance of \Bitrix\Pull\Model\Channel');
23 }
24
25 if ($userId === 0 && !isset($params['CHANNEL']))
26 {
27 global $USER;
28 $userId = (int)$USER->GetID();
29 if ($userId === 0)
30 {
31 return false;
32 }
33 }
34
35 $params['CACHE'] = (bool)($params['CACHE'] ?? true);
36 $params['REOPEN'] = (bool)($params['REOPEN'] ?? false);
37
38 $cache = $params['CACHE'] !== false;
39 $reopen = $params['REOPEN'] !== false;
40
41 if ($userId !== 0)
42 {
43 $privateChannelType = $params['CUSTOM_TYPE'] ?? \CPullChannel::TYPE_PRIVATE;
44 $privateChannel = \CPullChannel::Get($userId, $cache, $reopen, $privateChannelType);
45 $sharedChannelType = $params['CUSTOM_TYPE'] ?? \CPullChannel::TYPE_SHARED;
46 $sharedChannel = \CPullChannel::GetShared($cache, $reopen, $sharedChannelType);
47 }
48 else // isset($params['CHANNEL'])
49 {
50 $privateChannel = [
51 'CHANNEL_ID' => $params['CHANNEL']->getPrivateId(),
52 'CHANNEL_PUBLIC_ID' => $params['CHANNEL']->getPublicId(),
53 'CHANNEL_DT' => $params['CHANNEL']->getDateCreate()->getTimestamp(),
54 'CHANNEL_DT_END' => $params['CHANNEL']->getDateCreate()->getTimestamp() + static::ONE_YEAR,
55 ];
56 $sharedChannel = null;
57 }
58
59 $domain = defined('BX24_HOST_NAME')? BX24_HOST_NAME: $_SERVER['SERVER_NAME'];
60
61 $isSharedMode = \CPullOptions::IsServerShared();
62 $serverConfig = [
63 'VERSION' => $isSharedMode ? \Bitrix\Pull\SharedServer\Config::getServerVersion(): \CPullOptions::GetQueueServerVersion(),
64 'SERVER_ENABLED' => \CPullOptions::GetQueueServerStatus(),
65 'MODE' => \CPullOptions::GetQueueServerMode(),
66 'LONG_POLLING' => $isSharedMode ? \Bitrix\Pull\SharedServer\Config::getLongPollingUrl(): \CPullOptions::GetListenUrl(),
67 'LONG_POOLING_SECURE' => $isSharedMode ? \Bitrix\Pull\SharedServer\Config::getLongPollingUrl() : \CPullOptions::GetListenSecureUrl(),
68 'WEBSOCKET_ENABLED' => $isSharedMode ? true : \CPullOptions::GetWebSocket(),
69 'WEBSOCKET' => $isSharedMode ? \Bitrix\Pull\SharedServer\Config::getWebSocketUrl() : \CPullOptions::GetWebSocketUrl(),
70 'WEBSOCKET_SECURE' => $isSharedMode ? \Bitrix\Pull\SharedServer\Config::getWebSocketUrl() : \CPullOptions::GetWebSocketSecureUrl(),
71 'PUBLISH_ENABLED' => $isSharedMode ? true : \CPullOptions::GetPublishWebEnabled(),
72 'PUBLISH' => $isSharedMode ? \Bitrix\Pull\SharedServer\Config::getWebPublishUrl() : \CPullOptions::GetPublishWebUrl(),
73 'PUBLISH_SECURE' => $isSharedMode ? \Bitrix\Pull\SharedServer\Config::getWebPublishUrl() : \CPullOptions::GetPublishWebSecureUrl(),
74 'CONFIG_TIMESTAMP' => \CPullOptions::GetConfigTimestamp(),
75 ];
76 foreach ($serverConfig as $key => $value)
77 {
78 if(is_string($value) && mb_strpos($value, '#DOMAIN#') !== false)
79 {
80 $serverConfig[$key] = str_replace('#DOMAIN#', $domain, $value);
81 }
82 }
83 $config['SERVER'] = $serverConfig;
84 if($isSharedMode)
85 {
86 $config['CLIENT_ID'] = Client::getPublicLicenseCode();
87 }
88
89 $config['API'] = Array(
90 'REVISION_WEB' => PULL_REVISION_WEB,
91 'REVISION_MOBILE' => PULL_REVISION_MOBILE,
92 );
93
94 $config['CHANNELS'] = [];
95 if ($sharedChannel)
96 {
97 $config['CHANNELS']['SHARED'] = [
98 'ID' => \CPullChannel::SignChannel($sharedChannel["CHANNEL_ID"]),
99 'START' => $sharedChannel['CHANNEL_DT'],
100 'END' => $sharedChannel['CHANNEL_DT']+\CPullChannel::CHANNEL_TTL,
101 ];
102 }
103 if ($privateChannel)
104 {
105 if (\CPullOptions::GetQueueServerVersion() > 3)
106 {
107 $privateId = $privateChannel['CHANNEL_PUBLIC_ID']
108 ? "{$privateChannel['CHANNEL_ID']}:{$privateChannel['CHANNEL_PUBLIC_ID']}"
109 : $privateChannel['CHANNEL_ID']
110 ;
111 $privateId = \CPullChannel::SignChannel($privateId);
112
113 $publicId = \CPullChannel::SignPublicChannel($privateChannel['CHANNEL_PUBLIC_ID']);
114 }
115 else
116 {
117 $privateId = \CPullChannel::SignChannel($privateChannel['CHANNEL_ID']);
118 $publicId = '';
119 }
120
121 $config['CHANNELS']['PRIVATE'] = [
122 'ID' => $privateId,
123 'PUBLIC_ID' => $publicId,
124 'START' => $privateChannel['CHANNEL_DT'],
125 'END' => $privateChannel['CHANNEL_DT_END'] ?? $privateChannel['CHANNEL_DT']+\CPullChannel::CHANNEL_TTL,
126 ];
127 }
128
129 $params['JSON'] = (bool)($params['JSON'] ?? false);
130
131 $config['PUBLIC_CHANNELS'] = \Bitrix\Pull\Channel::getPublicIds(['JSON' => $params['JSON']]);
132 if (\CPullOptions::GetQueueServerVersion() >= 5)
133 {
134 $channelsForToken = [];
135 if ($sharedChannel)
136 {
137 $channelsForToken[] = $sharedChannel['CHANNEL_ID'];
138 }
139 if ($privateChannel && $userId == 0)
140 {
141 $channelsForToken[] = $privateChannel['CHANNEL_ID'];
142 if ($privateChannel['CHANNEL_PUBLIC_ID'] != '')
143 {
144 $channelsForToken[] = $privateChannel['CHANNEL_PUBLIC_ID'];
145 }
146 }
147 [$config['JWT'], $config['EXP']] = \Bitrix\Pull\Auth\Jwt::create($channelsForToken, $userId, [
148 'ttl' => \CPullOptions::GetConfigTtl()
149 ]);
150 }
151 if (\CPullOptions::GetConfigTtl() > 0 && !isset($config['EXP']))
152 {
153 $config['EXP'] = time() + \CPullOptions::GetConfigTtl();
154 }
155
156 if ($params['JSON'])
157 {
158 $result['server'] = array_change_key_case($config['SERVER'], CASE_LOWER);
159 $result['api'] = array_change_key_case($config['API'], CASE_LOWER);
160
161 foreach ($config['CHANNELS'] as $type => $channel)
162 {
163 $type = mb_strtolower($type);
164 $result['channels'][$type] = array_change_key_case($channel, CASE_LOWER);
165 $result['channels'][$type]['type'] = $type;
166 $result['channels'][$type]['start'] = date('c', $channel['START']);
167 $result['channels'][$type]['end'] = date('c', $channel['END']);
168 }
169
170 if($isSharedMode)
171 {
172 $result['clientId'] = $config['CLIENT_ID'];
173 }
174 if (isset($config['JWT']))
175 {
176 $result['jwt'] = $config['JWT'];
177 }
178 if (isset($config['EXP']))
179 {
180 $result['exp'] = $config['EXP'];
181 }
182
183 $result['publicChannels'] = $config['PUBLIC_CHANNELS'];
184
185 $config = $result;
186 }
187
188 return $config;
189
190 }
191
196 public static function getPublishUrl($channelId = "")
197 {
198 $params = [];
199 if(\CPullOptions::IsServerShared())
200 {
201 $result = \Bitrix\Pull\SharedServer\Config::getPublishUrl();
202 $params["clientId"] = \Bitrix\Pull\SharedServer\Client::getPublicLicenseCode();
203 }
204 else
205 {
206 $result = \CPullOptions::GetPublishUrl();
207 }
208
209 if($channelId != "")
210 {
211 $params["CHANNEL_ID"] = $channelId;
212 }
213
214 return \CHTTP::urlAddParams($result, $params);
215 }
216
217 public static function getJsonRpcUrl()
218 {
219 $params = [];
220 if(\CPullOptions::IsServerShared())
221 {
222 $result = \Bitrix\Pull\SharedServer\Config::getJsonRpcUrl();
223 $params["clientId"] = \Bitrix\Pull\SharedServer\Client::getPublicLicenseCode();
224 }
225 else
226 {
227 $result = \CPullOptions::GetJsonRpcUrl();
228 }
229
230 return \CHTTP::urlAddParams($result, $params);
231 }
232
233 public static function getHostId()
234 {
235 static $hostId = null;
236
237 if ($hostId === null)
238 {
239 $hostId = Option::get("pull", "host_id", "");
240 }
241 if ($hostId == '')
242 {
243 $hostId = Random::getString(32);
244 Option::set("pull", "host_id", $hostId);
245 }
246
247 return $hostId;
248 }
249
250 public static function getSignatureKey()
251 {
252 if(\CPullOptions::IsServerShared())
253 {
254 return \Bitrix\Pull\SharedServer\Config::getSignatureKey();
255 }
256 else
257 {
258 return \CPullOptions::GetSignatureKey();
259 }
260 }
261
262 public static function isProtobufUsed()
263 {
264 $result =
265 \CPullOptions::IsServerShared() ||
266 (
267 \CPullOptions::GetQueueServerVersion() == 4 &&
268 \CPullOptions::IsProtobufSupported() &&
269 \CPullOptions::IsProtobufEnabled()
270 );
271
272 return $result;
273 }
274
275 public static function isJsonRpcUsed(): bool
276 {
277 return \CPullOptions::GetQueueServerVersion() >= 5;
278 }
279}
static get($moduleId, $name, $default="", $siteId=false)
Definition option.php:30
static set($moduleId, $name, $value="", $siteId="")
Definition option.php:253
static getPublishUrl($channelId="")
Definition config.php:196
static isProtobufUsed()
Definition config.php:262
static getHostId()
Definition config.php:233
static getJsonRpcUrl()
Definition config.php:217
static getSignatureKey()
Definition config.php:250
static isJsonRpcUsed()
Definition config.php:275