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
2021-06-27 21:01:43 +02:00

170 lines
5.1 KiB
PHP

<?php
namespace App\Cache;
class Cache {
/**
* Get Username from API or cache
*
* @param $uuid
* @return bool|mixed The username if success or the UUID if failed
*/
public static function getUsername($uuid) {
if (file_exists(storage_path('app/uuid/'.$uuid.'.json'))) {
$json = file_get_contents(storage_path('app/uuid/'.$uuid.'.json'));
$json = json_decode($json, true);
if ((time() - strtotime($json['time'])) > 3600) {
$username = self::getUsernameUncached($uuid);
if ($username == false) {
return $json['name'];
} else {
$json = [];
$json['id'] = $uuid;
$json['name'] = $username;
self::saveJson($json);
return $json['name'];
}
} else {
return $json['name'];
}
} else {
$username = self::getUsernameUncached($uuid);
if ($username === $uuid) {
return $uuid;
} else {
$json = [];
$json['id'] = $uuid;
$json['name'] = $username;
self::saveJson($json);
return $json['name'];
}
}
}
/**
* Get UUID from API or cache
*
* @param $username
* @return bool|mixed The UUID if success or the username if failed
*/
public static function getUUID($username) {
foreach(glob(storage_path('app/uuid/*')) as $file) {
$json = file_get_contents($file);
$json = json_decode($json, true);
if ($json['name'] !== $username) continue;
if ((time() - strtotime($json['time'])) > 3600) {
$uuid = self::getUUIDUncached($username);
if ($uuid == false) {
unlink(storage_path('app/uuid/'.$file));
return $json['name'];
} else {
$json = [];
$json['id'] = $uuid;
$json['name'] = $username;
self::saveJson($json);
return $json['id'];
}
} else {
return $json['id'];
}
}
$uuid = self::getUUIDUncached($username);
if ($uuid == false) {
return $username;
} else {
$json = [];
$json['id'] = $uuid;
$json['name'] = $username;
self::saveJson($json);
return $json['id'];
}
}
/**
* Get the UUID by the username
*
* @param $username
* @return bool|mixed The UUID without dashes, or false if failed
*/
private static function getUUIDUncached($username) {
$profile = self::getProfile($username);
if (is_array($profile) and isset($profile['id']))
return $profile['id'];
return false;
}
/**
* Get the profile (username & UUID) from the username
*
* @uses http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time
*
* @param $username
* @return bool|mixed Array with ID and name, or false if failed
*/
private static function getProfile($username) {
if (self::isValidUsername($username)) {
$json = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . $username);
if (!empty($json)) {
$data = json_decode($json, true);
if (is_array($data) and !empty($data)) {
return $data;
}
}
}
return false;
}
/**
* Get the username from the UUID
*
* @uses http://wiki.vg/Mojang_API#UUID_-.3E_Name_history
*
* @param $uuid
* @return bool|mixed Username, or false if failed
*/
private static function getUsernameUncached($uuid) {
if (is_string($uuid)) {
$json = file_get_contents('https://api.mojang.com/user/profiles/' . $uuid . '/names');
if (!empty($json)) {
$data = json_decode($json, true);
if (!empty($data) and is_array($data)) {
$last = array_pop($data);
if (is_array($last) and isset($last['name'])) {
return $last['name'];
}
}
}
}
return false;
}
/**
* Check if the username is correct.
*
* @param $username
* @return bool Valid or not
*/
private static function isValidUsername($username) {
return is_string($username) and strlen($username) >= 2 and strlen($username) <= 16 and ctype_alnum(str_replace('_', '', $username));
}
/**
* Save the JSON to a file
*
* @param $json
*/
private static function saveJson($json) {
$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));
}
}