3
0
Fork 0
This repository has been archived on 2024-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
ThemeParkPlus-Panel/app/Cache/Cache.php

102 lines
3.2 KiB
PHP
Raw Normal View History

2020-02-26 13:59:58 +00:00
<?php
2020-02-26 13:59:58 +00:00
namespace App\Cache;
class Cache
{
2020-02-26 13:59:58 +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);
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);
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
$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 {
$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);
if (isset($json['error']))
2021-06-29 20:05:00 +00:00
return $uuid;
$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
}
return $json['name'];
2020-02-26 13:59:58 +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);
if ($json['name'] !== $username)
2021-06-29 20:05:00 +00:00
continue;
2020-02-26 13:59:58 +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);
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
}
return $json['id'];
2020-02-26 13:59:58 +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);
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
}
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')
];
file_put_contents(storage_path('app/uuid/' . $json['id'] . '.json'), json_encode($array));
2020-02-26 13:59:58 +00:00
}
}