Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
client.php
1<?php
2
4
10
11class Client
12{
13 const TYPE_CP = "CP";
14
15 public static function register($preferredServer = "", array $options = []): Result
16 {
17 $result = new Result();
18 if($preferredServer == "")
19 {
20 $serverAddressResult = static::selectServer();
21 if(!$serverAddressResult->isSuccess())
22 {
23 Config::setRegistered(false);
24 return $result->addErrors($serverAddressResult->getErrors());
25 }
26 $preferredServer = $serverAddressResult->getData()['hostname'];
27 }
28 Config::setServerAddress($preferredServer);
29 $endpoint = $options['endpoint'] ?? "";
30 $registerResult = static::performRegister($endpoint);
31 if(!$registerResult->isSuccess())
32 {
33 Config::setRegistered(false);
34 return $result->addErrors($registerResult->getErrors());
35 }
36
37 $registrationData = $registerResult->getData();
38
39 Config::setSignatureKey($registrationData["securityKey"]);
40 Config::setRegistered(true);
41
42 return $result;
43 }
44
45 public static function selectServer(): Result
46 {
47 $result = new Result();
48 $httpClient = new HttpClient();
49 $response = $httpClient->get(Config::getDefaultCloudServer() . Config::HOSTNAME_URL);
50 if(!$response)
51 {
52 $errors = $httpClient->getError();
53 foreach ($errors as $code => $message)
54 {
55 $result->addError(new Error($message, $code));
56 }
57 return $result;
58 }
59 if ($httpClient->getStatus() != 200)
60 {
61 return $result->addError(new Error("Unexpected server response code " . $httpClient->getStatus(), "WRONG_RESPONSE_CODE"));
62 }
63 $result->setData([
64 'hostname' => $response
65 ]);
66 return $result;
67 }
68
69 public static function getServerList(): Result
70 {
71 $result = new Result();
72 $httpClient = new HttpClient([
73 "socketTimeout" => 5,
74 "streamTimeout" => 5
75 ]);
76 $response = $httpClient->get(Config::getDefaultCloudServer() . Config::SERVER_LIST_URL);
77 if(!$response)
78 {
79 $errors = $httpClient->getError();
80 foreach ($errors as $code => $message)
81 {
82 $result->addError(new Error($message, $code));
83 }
84 return $result;
85 }
86 if ($httpClient->getStatus() != 200)
87 {
88 return $result->addError(new Error("Unexpected server response code " . $httpClient->getStatus(), "WRONG_RESPONSE_CODE"));
89 }
90 $list = explode("\n", $response);
91 $list = array_filter($list, function($a){return $a != "";});
92 $list = array_map(
93 function($a){
94 list($url, $region) = explode(";", $a);
95 return [
96 'url' => $url,
97 'region' => $region
98 ];
99 },
100 $list
101 );
102
103 $result->setData([
104 'serverList' => $list
105 ]);
106 return $result;
107 }
108
109 protected static function performRegister(string $licenseServerEndpoint = ""): Result
110 {
111 $result = new Result();
112 $params = [
113 "BX_LICENCE" => static::getPublicLicenseCode(),
114 "BX_TYPE" => static::TYPE_CP,
115 ];
116 $params["BX_HASH"] = static::signRequest($params);
117 $params["BX_ALL"] = "y";
118
119 $request = [
120 "verificationQuery" => http_build_query($params)
121 ];
122
123 $httpClient = new HttpClient([
124 "disableSslVerification" => true
125 ]);
126
127 $licenseServerEndpoint = $licenseServerEndpoint ?: Config::getRegisterUrl();
128 $queryResult = $httpClient->query(HttpClient::HTTP_POST, $licenseServerEndpoint, $request);
129
130 if(!$queryResult)
131 {
132 $errors = $httpClient->getError();
133 foreach ($errors as $code => $message)
134 {
135 $result->addError(new Error($message, $code));
136 }
137 return $result;
138 }
139 $returnCode = $httpClient->getStatus();
140 $response = $httpClient->getResult();
141 if ($returnCode != 200)
142 {
143 try
144 {
145 $parsedResponse = Json::decode($response);
146 $result->addError(new Error($parsedResponse["error"]));
147 }
148 catch (\Exception $e)
149 {
150 $result->addError(new Error("Server returned " . $returnCode . " code", "WRONG_SERVER_RESPONSE"));
151 }
152
153 return $result;
154 }
155
156 if($response == "")
157 {
158 $result->addError(new Error("Empty server response", "EMPTY_SERVER_RESPONSE"));
159 return $result;
160 }
161
162 try
163 {
164 $parsedResponse = Json::decode($response);
165 }
166 catch (\Exception $e)
167 {
168 $result->addError(new Error("Could not parse server response. Raw response: " . $response));
169 return $result;
170 }
171
172 if($parsedResponse["status"] === "error")
173 {
174 $result->addError(new Error($parsedResponse["error"]));
175 }
176 else
177 {
178 $result->setData($parsedResponse);
179 }
180
181 return $result;
182 }
183
184 public static function getPublicLicenseCode(): string
185 {
186 return Application::getInstance()->getLicense()->getPublicHashKey();
187 }
188
189 protected static function signRequest(array $request): string
190 {
191 $paramStr = md5(implode("|", $request));
192 return md5($paramStr . Application::getInstance()->getLicense()->getHashLicenseKey());
193 }
194}
static signRequest(array $request)
Definition client.php:189
static performRegister(string $licenseServerEndpoint="")
Definition client.php:109