2016-09-06 15:26:39 +05:30
|
|
|
<?php
|
2019-05-13 22:09:11 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-06 15:26:39 +05:30
|
|
|
namespace common\models;
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
use Carbon\Carbon;
|
|
|
|
use common\components\SkinsSystemApi;
|
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient;
|
2019-05-13 22:09:11 +05:30
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2016-12-23 04:20:34 +05:30
|
|
|
use Yii;
|
2016-09-06 15:26:39 +05:30
|
|
|
|
|
|
|
class Textures {
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
protected Account $account;
|
2016-09-06 15:26:39 +05:30
|
|
|
|
|
|
|
public function __construct(Account $account) {
|
|
|
|
$this->account = $account;
|
|
|
|
}
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
public function getMinecraftResponse(bool $signed = false): array {
|
|
|
|
$uuid = str_replace('-', '', $this->account->uuid);
|
|
|
|
$profile = $this->getProfile($signed);
|
|
|
|
if ($profile === null) {
|
2021-03-04 15:48:04 +05:30
|
|
|
// This case shouldn't happen at all, but synchronization isn't perfect and sometimes
|
|
|
|
// information might be now updated. Provide fallback solution
|
2021-03-03 19:34:42 +05:30
|
|
|
$profile = [
|
|
|
|
'name' => $this->account->username,
|
|
|
|
'id' => $uuid,
|
|
|
|
'properties' => [
|
|
|
|
[
|
|
|
|
'name' => 'textures',
|
|
|
|
'value' => base64_encode(json_encode([
|
|
|
|
'timestamp' => Carbon::now()->getPreciseTimestamp(3),
|
|
|
|
'profileId' => $uuid,
|
|
|
|
'profileName' => $this->account->username,
|
|
|
|
'textures' => [],
|
|
|
|
])),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'ely',
|
|
|
|
'value' => 'but why are you asking?',
|
|
|
|
],
|
2016-09-06 15:26:39 +05:30
|
|
|
],
|
2021-03-03 19:34:42 +05:30
|
|
|
];
|
|
|
|
|
|
|
|
if ($signed) {
|
|
|
|
// I don't remember why this value has been used, but it was, so keep the same behavior until
|
|
|
|
// figure out why it was made in this way
|
|
|
|
$profile['properties'][0]['signature'] = 'Cg==';
|
|
|
|
}
|
|
|
|
} elseif ($profile['id'] !== $uuid) {
|
|
|
|
// Also a case that shouldn't happen, but is technically possible
|
|
|
|
$profile['id'] = $uuid;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2021-08-16 18:46:46 +05:30
|
|
|
if ($signed) {
|
|
|
|
// This is a completely impossible case. But the most impossible things happen most of the time.
|
|
|
|
// We have received complaints that sometimes an empty value comes in the signature field.
|
|
|
|
// This code is an attempt at an investigation. If no such cases are reported for the foreseeable future,
|
|
|
|
// then this code can be removed
|
|
|
|
foreach ($profile['properties'] as &$property) {
|
|
|
|
if ($property['name'] === 'textures') {
|
|
|
|
if (!isset($property['signature'])) {
|
|
|
|
Yii::warning('Signature was required, but field was not returned from the skinsystem\'s server');
|
|
|
|
$property['signature'] = 'Cg==';
|
|
|
|
} elseif (empty($property['signature'])) {
|
|
|
|
Yii::warning('Signature was required, but contains an empty value from skinsystem\'s server');
|
|
|
|
$property['signature'] = 'Cg==';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
return $profile;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
private function getProfile(bool $signed): ?array {
|
|
|
|
/** @var SkinsSystemApi $api */
|
|
|
|
$api = Yii::$container->get(SkinsSystemApi::class);
|
2019-11-09 20:16:22 +05:30
|
|
|
if (YII_ENV_PROD) {
|
2021-03-03 19:34:42 +05:30
|
|
|
$api->setClient(new GuzzleHttpClient([
|
2019-11-09 20:16:22 +05:30
|
|
|
'connect_timeout' => 2,
|
2021-03-03 19:34:42 +05:30
|
|
|
'read_timeout' => 7,
|
2019-11-09 20:16:22 +05:30
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2016-12-23 04:20:34 +05:30
|
|
|
try {
|
2021-03-03 19:34:42 +05:30
|
|
|
return $api->profile($this->account->username, $signed);
|
2019-05-13 22:09:11 +05:30
|
|
|
} catch (GuzzleException $e) {
|
2021-03-03 19:34:42 +05:30
|
|
|
Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage());
|
2016-12-23 04:20:34 +05:30
|
|
|
}
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
return null;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|