15 public static function register($preferredServer =
"", array $options = []):
Result
18 if($preferredServer ==
"")
20 $serverAddressResult = static::selectServer();
21 if(!$serverAddressResult->isSuccess())
23 Config::setRegistered(
false);
24 return $result->addErrors($serverAddressResult->getErrors());
26 $preferredServer = $serverAddressResult->getData()[
'hostname'];
28 Config::setServerAddress($preferredServer);
29 $endpoint = $options[
'endpoint'] ??
"";
30 $registerResult = static::performRegister($endpoint);
31 if(!$registerResult->isSuccess())
33 Config::setRegistered(
false);
34 return $result->addErrors($registerResult->getErrors());
37 $registrationData = $registerResult->getData();
39 Config::setSignatureKey($registrationData[
"securityKey"]);
40 Config::setRegistered(
true);
49 $response = $httpClient->get(Config::getDefaultCloudServer() . Config::HOSTNAME_URL);
52 $errors = $httpClient->getError();
53 foreach ($errors as $code => $message)
55 $result->addError(
new Error($message, $code));
59 if ($httpClient->getStatus() != 200)
61 return $result->addError(
new Error(
"Unexpected server response code " . $httpClient->getStatus(),
"WRONG_RESPONSE_CODE"));
64 'hostname' => $response
76 $response = $httpClient->get(Config::getDefaultCloudServer() . Config::SERVER_LIST_URL);
79 $errors = $httpClient->getError();
80 foreach ($errors as $code => $message)
82 $result->addError(
new Error($message, $code));
86 if ($httpClient->getStatus() != 200)
88 return $result->addError(
new Error(
"Unexpected server response code " . $httpClient->getStatus(),
"WRONG_RESPONSE_CODE"));
90 $list = explode(
"\n", $response);
91 $list = array_filter($list,
function($a){
return $a !=
"";});
94 list($url, $region) = explode(
";", $a);
104 'serverList' => $list
113 "BX_LICENCE" => static::getPublicLicenseCode(),
114 "BX_TYPE" => static::TYPE_CP,
116 $params[
"BX_HASH"] = static::signRequest($params);
117 $params[
"BX_ALL"] =
"y";
120 "verificationQuery" => http_build_query($params)
124 "disableSslVerification" =>
true
127 $licenseServerEndpoint = $licenseServerEndpoint ?: Config::getRegisterUrl();
128 $queryResult = $httpClient->query(HttpClient::HTTP_POST, $licenseServerEndpoint, $request);
132 $errors = $httpClient->getError();
133 foreach ($errors as $code => $message)
135 $result->addError(
new Error($message, $code));
139 $returnCode = $httpClient->getStatus();
140 $response = $httpClient->getResult();
141 if ($returnCode != 200)
145 $parsedResponse = Json::decode($response);
146 $result->addError(
new Error($parsedResponse[
"error"]));
148 catch (\Exception $e)
150 $result->addError(
new Error(
"Server returned " . $returnCode .
" code",
"WRONG_SERVER_RESPONSE"));
158 $result->addError(
new Error(
"Empty server response",
"EMPTY_SERVER_RESPONSE"));
164 $parsedResponse = Json::decode($response);
166 catch (\Exception $e)
168 $result->addError(
new Error(
"Could not parse server response. Raw response: " . $response));
172 if($parsedResponse[
"status"] ===
"error")
174 $result->addError(
new Error($parsedResponse[
"error"]));
178 $result->setData($parsedResponse);