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;
|
|
|
|
|
|
|
|
use common\components\SkinSystem\Api as SkinSystemApi;
|
2016-11-01 22:06:39 +05:30
|
|
|
use DateInterval;
|
|
|
|
use DateTime;
|
2019-05-13 22:09:11 +05:30
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2016-12-23 04:20:34 +05:30
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Yii;
|
2016-09-06 15:26:39 +05:30
|
|
|
|
|
|
|
class Textures {
|
|
|
|
|
|
|
|
public $displayElyMark = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Account
|
|
|
|
*/
|
|
|
|
protected $account;
|
|
|
|
|
|
|
|
public function __construct(Account $account) {
|
|
|
|
$this->account = $account;
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:09:11 +05:30
|
|
|
public function getMinecraftResponse(): array {
|
2018-04-18 02:17:25 +05:30
|
|
|
$response = [
|
2016-09-06 15:26:39 +05:30
|
|
|
'name' => $this->account->username,
|
|
|
|
'id' => str_replace('-', '', $this->account->uuid),
|
|
|
|
'properties' => [
|
|
|
|
[
|
|
|
|
'name' => 'textures',
|
|
|
|
'signature' => 'Cg==',
|
|
|
|
'value' => $this->getTexturesValue(),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->displayElyMark) {
|
|
|
|
$response['ely'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTexturesValue($encrypted = true) {
|
|
|
|
$array = [
|
2016-11-01 22:06:39 +05:30
|
|
|
'timestamp' => (new DateTime())->add(new DateInterval('P2D'))->getTimestamp(),
|
2016-09-06 15:26:39 +05:30
|
|
|
'profileId' => str_replace('-', '', $this->account->uuid),
|
|
|
|
'profileName' => $this->account->username,
|
|
|
|
'textures' => $this->getTextures(),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->displayElyMark) {
|
|
|
|
$array['ely'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$encrypted) {
|
|
|
|
return $array;
|
|
|
|
}
|
2018-04-18 02:17:25 +05:30
|
|
|
|
|
|
|
return static::encrypt($array);
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 04:20:34 +05:30
|
|
|
public function getTextures(): array {
|
2019-05-13 22:09:11 +05:30
|
|
|
/** @var SkinSystemApi $api */
|
|
|
|
$api = Yii::$container->get(SkinSystemApi::class);
|
2019-11-09 20:16:22 +05:30
|
|
|
if (YII_ENV_PROD) {
|
|
|
|
$api->setClient(new \GuzzleHttp\Client([
|
|
|
|
'connect_timeout' => 2,
|
|
|
|
'decode_content' => false,
|
|
|
|
'read_timeout' => 5,
|
|
|
|
'stream' => true,
|
|
|
|
'timeout' => 5,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2016-12-23 04:20:34 +05:30
|
|
|
try {
|
2019-05-13 22:09:11 +05:30
|
|
|
$textures = $api->textures($this->account->username);
|
2016-12-23 04:20:34 +05:30
|
|
|
} catch (RequestException $e) {
|
|
|
|
Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage());
|
2019-05-13 22:09:11 +05:30
|
|
|
} catch (GuzzleException $e) {
|
2019-11-09 20:16:22 +05:30
|
|
|
Yii::warning($e);
|
2016-12-23 04:20:34 +05:30
|
|
|
}
|
|
|
|
|
2019-05-13 22:09:11 +05:30
|
|
|
return $textures ?? [];
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2019-05-13 22:09:11 +05:30
|
|
|
public static function encrypt(array $data): string {
|
2016-09-06 15:26:39 +05:30
|
|
|
return base64_encode(stripcslashes(json_encode($data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function decrypt($string, $assoc = true) {
|
|
|
|
return json_decode(base64_decode($string), $assoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|