2020-02-26 13:59:58 +00:00
|
|
|
<?php
|
2022-11-28 20:07:07 +00:00
|
|
|
|
2020-02-26 13:59:58 +00:00
|
|
|
namespace App\Cache;
|
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
class Cache
|
|
|
|
{
|
2020-02-26 13:59:58 +00:00
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
public static function getUsername($uuid)
|
|
|
|
{
|
|
|
|
if (file_exists(storage_path('app/uuid/' . $uuid . '.json'))) {
|
|
|
|
$json = file_get_contents(storage_path('app/uuid/' . $uuid . '.json'));
|
2020-02-26 13:59:58 +00:00
|
|
|
$json = json_decode($json, true);
|
2022-11-28 20:07:07 +00:00
|
|
|
if ((time() - strtotime($json['time'])) > 3600) {
|
|
|
|
$json = file_get_contents('https://api.mojang.com/user/profile/' . $uuid);
|
|
|
|
if (empty($json)) {
|
|
|
|
$json = file_get_contents(storage_path('app/uuid/' . $uuid . '.json'));
|
2021-06-29 20:05:00 +00:00
|
|
|
$json = json_decode($json, true);
|
2020-02-26 13:59:58 +00:00
|
|
|
return $json['name'];
|
2021-06-29 20:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_decode($json, true);
|
2022-11-28 20:07:07 +00:00
|
|
|
if (isset($json['error'])) {
|
|
|
|
$json = file_get_contents(storage_path('app/uuid/' . $uuid . '.json'));
|
2021-06-29 20:05:00 +00:00
|
|
|
$json = json_decode($json, true);
|
2020-02-26 13:59:58 +00:00
|
|
|
return $json['name'];
|
|
|
|
}
|
2021-06-29 20:05:00 +00:00
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
$name = $json['name'];
|
2021-06-27 19:01:43 +00:00
|
|
|
$json = [];
|
|
|
|
$json['id'] = $uuid;
|
2021-06-29 20:05:00 +00:00
|
|
|
$json['name'] = $name;
|
2021-06-27 19:01:43 +00:00
|
|
|
self::saveJson($json);
|
|
|
|
}
|
2021-06-29 20:05:00 +00:00
|
|
|
} else {
|
2022-11-28 20:07:07 +00:00
|
|
|
$json = file_get_contents('https://api.mojang.com/user/profile/' . $uuid);
|
|
|
|
if (empty($json))
|
2021-06-29 20:05:00 +00:00
|
|
|
return $uuid;
|
|
|
|
|
|
|
|
$json = json_decode($json, true);
|
2022-11-28 20:07:07 +00:00
|
|
|
if (isset($json['error']))
|
2021-06-29 20:05:00 +00:00
|
|
|
return $uuid;
|
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
$name = $json['name'];
|
2021-06-29 20:05:00 +00:00
|
|
|
$json = [];
|
|
|
|
$json['id'] = $uuid;
|
|
|
|
$json['name'] = $name;
|
|
|
|
self::saveJson($json);
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
2022-11-28 20:07:07 +00:00
|
|
|
return $json['name'];
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
public static function getUUID($username)
|
|
|
|
{
|
|
|
|
foreach (glob(storage_path('app/uuid/*')) as $file) {
|
2020-02-26 13:59:58 +00:00
|
|
|
$json = file_get_contents($file);
|
|
|
|
$json = json_decode($json, true);
|
2022-11-28 20:07:07 +00:00
|
|
|
if ($json['name'] !== $username)
|
2021-06-29 20:05:00 +00:00
|
|
|
continue;
|
2020-02-26 13:59:58 +00:00
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
if ((time() - strtotime($json['time'])) > 3600) {
|
|
|
|
$json = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . $username);
|
|
|
|
if (empty($json)) {
|
|
|
|
unlink(storage_path('app/uuid/' . $file));
|
2021-06-29 20:05:00 +00:00
|
|
|
return $username;
|
|
|
|
}
|
2020-02-26 13:59:58 +00:00
|
|
|
|
2021-06-29 20:05:00 +00:00
|
|
|
$json = json_decode($json, true);
|
2022-11-28 20:07:07 +00:00
|
|
|
if (isset($json['error'])) {
|
|
|
|
unlink(storage_path('app/uuid/' . $file));
|
2021-06-29 20:05:00 +00:00
|
|
|
return $username;
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
2021-06-29 20:05:00 +00:00
|
|
|
|
|
|
|
self::saveJson($json);
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
2022-11-28 20:07:07 +00:00
|
|
|
return $json['id'];
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
$json = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . $username);
|
|
|
|
if (empty($json))
|
2020-02-26 13:59:58 +00:00
|
|
|
return $username;
|
2021-06-27 19:01:43 +00:00
|
|
|
|
2021-06-29 20:05:00 +00:00
|
|
|
$json = json_decode($json, true);
|
2022-11-28 20:07:07 +00:00
|
|
|
if (isset($json['error']))
|
2021-06-29 20:05:00 +00:00
|
|
|
return $username;
|
2020-02-26 13:59:58 +00:00
|
|
|
|
2021-06-29 20:05:00 +00:00
|
|
|
self::saveJson($json);
|
|
|
|
return $json['id'];
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
public static function saveJson($json)
|
|
|
|
{
|
2020-02-26 13:59:58 +00:00
|
|
|
$array = [
|
|
|
|
'id' => $json['id'],
|
|
|
|
'name' => $json['name'],
|
|
|
|
'time' => date('d-m-Y H:m:s')
|
|
|
|
];
|
|
|
|
|
2022-11-28 20:07:07 +00:00
|
|
|
file_put_contents(storage_path('app/uuid/' . $json['id'] . '.json'), json_encode($array));
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|