Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
client.php
1<?php
10namespace Bitrix\Rest\OAuth;
11
18
19if(!defined("BITRIX_OAUTH_URL"))
20{
21 $defaultValue = \Bitrix\Main\Config\Option::get('rest', 'oauth_server', 'https://oauth.bitrix.info');
22 define("BITRIX_OAUTH_URL", $defaultValue);
23}
24
25if(!defined('BITRIXREST_URL'))
26{
27 define('BITRIXREST_URL', BITRIX_OAUTH_URL);
28}
29
30class Client
31{
32 const SERVICE_URL = BITRIXREST_URL;
33 const SERVICE_PATH = "/rest/";
34
35 const METHOD_METHODS = 'methods';
36 const METHOD_BATCH = 'batch';
37
38 const METHOD_APPLICATION_ADD = 'application.add';
39 const METHOD_APPLICATION_UPDATE = 'application.update';
40 const METHOD_APPLICATION_DELETE = 'application.delete';
41 const METHOD_APPLICATION_INSTALL = 'application.install';
42 const METHOD_APPLICATION_INSTALL_SUBSCRIPTION = 'application.install.subscription';
43 const METHOD_APPLICATION_UNINSTALL = 'application.uninstall';
44 const METHOD_APPLICATION_STAT = 'application.stat';
45 const METHOD_APPLICATION_LIST = 'application.list';
46 const METHOD_APPLICATION_USAGE = 'application.usage.add';
47
48 const METHOD_APPLICATION_VERSION_UPDATE = 'application.version.update';
49 const METHOD_APPLICATION_VERSION_DELETE = 'application.version.delete';
50
51 const METHOD_REST_AUTHORIZE = 'rest.authorize';
52 const METHOD_REST_CHECK = 'rest.check';
53 const METHOD_REST_CODE = 'rest.code';
54 const METHOD_REST_EVENT_CALL = 'rest.event.call';
55
58
59 private const NAME_IDENTIFIER_REQUEST = 'bx24_request_id';
60
61 protected $clientId;
62 protected $clientSecret;
63 protected $licenseKey;
64
66 {
67 $this->clientId = $clientId;
68 $this->clientSecret = $clientSecret;
69 $this->licenseKey = $licenseKey;
70 }
71
72 protected function prepareRequestData($additionalParams)
73 {
74 if(!is_array($additionalParams))
75 {
76 $additionalParams = array();
77 }
78 else
79 {
80 $additionalParams = Encoding::convertEncoding($additionalParams, LANG_CHARSET, "utf-8");
81 }
82
83 return $additionalParams;
84 }
85
86 protected function prepareRequest($additionalParams, $licenseCheck = false)
87 {
88 $additionalParams = $this->prepareRequestData($additionalParams);
89
90 $additionalParams['client_id'] = $this->clientId;
91 $additionalParams['client_secret'] = $this->clientSecret;
92 $additionalParams['client_redirect_uri'] = OAuthService::getRedirectUri();
93 $additionalParams['member_id'] = \CRestUtil::getMemberId();
94
95 if($licenseCheck)
96 {
97 $additionalParams = \CRestUtil::signLicenseRequest($additionalParams, $this->licenseKey);
98 }
99
100 return $additionalParams;
101 }
102
103 protected function prepareResponse($result)
104 {
105 try
106 {
107 return Json::decode($result);
108 }
109 catch(ArgumentException $e)
110 {
111 return false;
112 }
113 }
114
115 protected function getHttpClient()
116 {
117 return new HttpClient(array(
118 'socketTimeout' => static::HTTP_SOCKET_TIMEOUT,
119 'streamTimeout' => static::HTTP_STREAM_TIMEOUT,
120 ));
121 }
122
123 protected function getRequestId(string $methodName): string
124 {
125 $requestId = '';
126 if (isset($_SERVER['BX24_REQUEST_ID']))
127 {
128 if (str_contains($methodName, '?'))
129 {
130 $separator = '&';
131 }
132 else
133 {
134 $separator = '/?';
135 }
136
137 $requestId = $separator . http_build_query([
138 static::NAME_IDENTIFIER_REQUEST => urlencode($_SERVER['BX24_REQUEST_ID'])
139 ]);
140 }
141
142 return $requestId;
143 }
144
145 protected function getRequestUrl($methodName): string
146 {
147 return static::SERVICE_URL . static::SERVICE_PATH . $methodName . $this->getRequestId($methodName);
148 }
149
161 public function call($methodName, $additionalParams = null, $licenseCheck = false)
162 {
163 if ($this->clientId && $this->clientSecret)
164 {
165 $additionalParams = $this->prepareRequest($additionalParams, $licenseCheck);
166
167 $httpClient = $this->getHttpClient();
168 $httpResult = $httpClient->post(
169 $this->getRequestUrl($methodName),
170 $additionalParams
171 );
172
173 $response = $this->prepareResponse($httpResult);
174
175 if ($response)
176 {
177 if (!$licenseCheck && is_array($response) && isset($response['error']) && $response['error'] === 'verification_needed')
178 {
179 return $this->call($methodName, $additionalParams, true);
180 }
181 }
182 else
183 {
184 addMessage2Log(
185 'Strange answer from Bitrix Service! '
186 . static::SERVICE_URL
187 . static::SERVICE_PATH
188 . $methodName . ": "
189 . $httpClient->getStatus() . ' '
190 . implode(" ", $httpClient->getError())
191 . $httpResult
192 );
193 }
194
195 return $response;
196 }
197 else
198 {
199 throw new SystemException("No client credentials");
200 }
201 }
202
203 public function batch($actions)
204 {
205 $batch = array();
206
207 if(is_array($actions))
208 {
209 foreach($actions as $queryKey => $cmdData)
210 {
211 list($cmd, $cmdParams) = array_values($cmdData);
212 $batch['cmd'][$queryKey] = $cmd.(is_array($cmdParams) ? '?'.http_build_query($this->prepareRequestData($cmdParams)) : '');
213 }
214 }
215
216 return $this->call(static::METHOD_BATCH, $batch);
217 }
218
219 public function addApplication(array $applicationSettings)
220 {
221 return $this->call(static::METHOD_APPLICATION_ADD, array(
222 "TITLE" => $applicationSettings["TITLE"],
223 "REDIRECT_URI" => $applicationSettings["REDIRECT_URI"],
224 "SCOPE" => $applicationSettings["SCOPE"],
225 ));
226 }
227
228 public function updateApplication(array $applicationSettings)
229 {
230 return $this->call(static::METHOD_APPLICATION_UPDATE, array(
231 "CLIENT_ID" => $applicationSettings["CLIENT_ID"],
232 "TITLE" => $applicationSettings["TITLE"],
233 "REDIRECT_URI" => $applicationSettings["REDIRECT_URI"],
234 "SCOPE" => $applicationSettings["SCOPE"],
235 ));
236 }
237
238 public function deleteApplication(array $applicationSettings)
239 {
240 return $this->call(static::METHOD_APPLICATION_DELETE, array(
241 "CLIENT_ID" => $applicationSettings["CLIENT_ID"],
242 ));
243 }
244
245 public function installApplication(array $applicationSettings)
246 {
247 $queryFields = array(
248 "CLIENT_ID" => $applicationSettings["CLIENT_ID"],
249 "VERSION" => $applicationSettings["VERSION"],
250 );
251
252 if(isset($applicationSettings["CHECK_HASH"]) && isset($applicationSettings["INSTALL_HASH"]))
253 {
254 $queryFields['CHECK_HASH'] = $applicationSettings["CHECK_HASH"];
255 $queryFields['INSTALL_HASH'] = $applicationSettings["INSTALL_HASH"];
256 }
257
258 if ($applicationSettings['BY_SUBSCRIPTION'] === 'Y')
259 {
260 $method = static::METHOD_APPLICATION_INSTALL_SUBSCRIPTION;
261 }
262 else
263 {
264 $method = static::METHOD_APPLICATION_INSTALL;
265 }
266
267 return $this->call($method, $queryFields);
268 }
269
270 public function unInstallApplication(array $applicationSettings)
271 {
272 return $this->call(static::METHOD_APPLICATION_UNINSTALL, array(
273 "CLIENT_ID" => $applicationSettings["CLIENT_ID"],
274 ));
275 }
276
277 public function getAuth($clientId, $scope, array $additionalParams = array())
278 {
279 return $this->call(static::METHOD_REST_AUTHORIZE, array(
280 "CLIENT_ID" => $clientId,
281 "SCOPE" => $scope,
282 "PARAMS" => $additionalParams,
283 ));
284 }
285
286 public function checkAuth($accessToken)
287 {
288 return $this->call(static::METHOD_REST_CHECK, array(
289 "TOKEN" => $accessToken,
290 ));
291 }
292
293 public function getCode($clientId, $state, $additionalParams)
294 {
295 return $this->call(static::METHOD_REST_CODE, array(
296 "CLIENT_ID" => $clientId,
297 "STATE" => $state,
298 "PARAMS" => $additionalParams,
299 ));
300 }
301
302 public function getApplicationList()
303 {
304 return $this->call(static::METHOD_APPLICATION_LIST);
305 }
306
307 public function sendApplicationUsage(array $usage)
308 {
309 return $this->call(static::METHOD_APPLICATION_USAGE, array(
310 "USAGE" => $usage,
311 ));
312 }
313
314 public function sendEvent(array $eventItems)
315 {
316 return $this->call(static::METHOD_REST_EVENT_CALL, array(
317 "QUERY" => $eventItems,
318 ));
319 }
320}
updateApplication(array $applicationSettings)
Definition client.php:228
sendApplicationUsage(array $usage)
Definition client.php:307
getRequestId(string $methodName)
Definition client.php:123
installApplication(array $applicationSettings)
Definition client.php:245
checkAuth($accessToken)
Definition client.php:286
__construct($clientId, $clientSecret, $licenseKey)
Definition client.php:65
getAuth($clientId, $scope, array $additionalParams=array())
Definition client.php:277
prepareRequest($additionalParams, $licenseCheck=false)
Definition client.php:86
const METHOD_APPLICATION_UNINSTALL
Definition client.php:43
const METHOD_APPLICATION_INSTALL
Definition client.php:41
prepareRequestData($additionalParams)
Definition client.php:72
call($methodName, $additionalParams=null, $licenseCheck=false)
Definition client.php:161
const METHOD_APPLICATION_INSTALL_SUBSCRIPTION
Definition client.php:42
const METHOD_APPLICATION_VERSION_UPDATE
Definition client.php:48
addApplication(array $applicationSettings)
Definition client.php:219
getRequestUrl($methodName)
Definition client.php:145
const METHOD_APPLICATION_VERSION_DELETE
Definition client.php:49
getCode($clientId, $state, $additionalParams)
Definition client.php:293
unInstallApplication(array $applicationSettings)
Definition client.php:270
const METHOD_APPLICATION_DELETE
Definition client.php:40
sendEvent(array $eventItems)
Definition client.php:314
deleteApplication(array $applicationSettings)
Definition client.php:238
const METHOD_APPLICATION_UPDATE
Definition client.php:39