2020-02-26 13:59:58 +00:00
|
|
|
<?php
|
|
|
|
namespace App\Cache;
|
|
|
|
|
|
|
|
class Cache {
|
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
/**
|
|
|
|
* Get Username from API or cache
|
|
|
|
*
|
|
|
|
* @param $uuid
|
|
|
|
* @return bool|mixed The username if success or the UUID if failed
|
|
|
|
*/
|
2020-02-26 13:59:58 +00:00
|
|
|
public static function getUsername($uuid) {
|
2021-06-27 19:01:43 +00:00
|
|
|
if (file_exists(storage_path('app/uuid/'.$uuid.'.json'))) {
|
2020-02-26 13:59:58 +00:00
|
|
|
$json = file_get_contents(storage_path('app/uuid/'.$uuid.'.json'));
|
|
|
|
$json = json_decode($json, true);
|
2021-06-27 19:01:43 +00:00
|
|
|
if ((time() - strtotime($json['time'])) > 3600) {
|
|
|
|
$username = self::getUsernameUncached($uuid);
|
|
|
|
if ($username == false) {
|
2020-02-26 13:59:58 +00:00
|
|
|
return $json['name'];
|
2021-06-27 19:01:43 +00:00
|
|
|
} else {
|
|
|
|
$json = [];
|
|
|
|
$json['id'] = $uuid;
|
|
|
|
$json['name'] = $username;
|
|
|
|
self::saveJson($json);
|
2020-02-26 13:59:58 +00:00
|
|
|
return $json['name'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $json['name'];
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-27 19:01:43 +00:00
|
|
|
$username = self::getUsernameUncached($uuid);
|
|
|
|
if ($username === $uuid) {
|
2020-02-26 13:59:58 +00:00
|
|
|
return $uuid;
|
2021-06-27 19:01:43 +00:00
|
|
|
} else {
|
|
|
|
$json = [];
|
|
|
|
$json['id'] = $uuid;
|
|
|
|
$json['name'] = $username;
|
|
|
|
self::saveJson($json);
|
|
|
|
return $json['name'];
|
|
|
|
}
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
/**
|
|
|
|
* Get UUID from API or cache
|
|
|
|
*
|
|
|
|
* @param $username
|
|
|
|
* @return bool|mixed The UUID if success or the username if failed
|
|
|
|
*/
|
2020-02-26 13:59:58 +00:00
|
|
|
public static function getUUID($username) {
|
|
|
|
foreach(glob(storage_path('app/uuid/*')) as $file) {
|
|
|
|
$json = file_get_contents($file);
|
|
|
|
$json = json_decode($json, true);
|
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
if ($json['name'] !== $username) continue;
|
2020-02-26 13:59:58 +00:00
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
if ((time() - strtotime($json['time'])) > 3600) {
|
|
|
|
$uuid = self::getUUIDUncached($username);
|
|
|
|
if ($uuid == false) {
|
2020-02-26 13:59:58 +00:00
|
|
|
unlink(storage_path('app/uuid/'.$file));
|
2021-06-27 19:01:43 +00:00
|
|
|
return $json['name'];
|
|
|
|
} else {
|
|
|
|
$json = [];
|
|
|
|
$json['id'] = $uuid;
|
|
|
|
$json['name'] = $username;
|
|
|
|
self::saveJson($json);
|
|
|
|
return $json['id'];
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $json['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
$uuid = self::getUUIDUncached($username);
|
|
|
|
if ($uuid == false) {
|
2020-02-26 13:59:58 +00:00
|
|
|
return $username;
|
2021-06-27 19:01:43 +00:00
|
|
|
} else {
|
|
|
|
$json = [];
|
|
|
|
$json['id'] = $uuid;
|
|
|
|
$json['name'] = $username;
|
|
|
|
self::saveJson($json);
|
|
|
|
return $json['id'];
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 13:59:58 +00:00
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2020-02-26 13:59:58 +00:00
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
/**
|
|
|
|
* 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));
|
2020-02-26 13:59:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 19:01:43 +00:00
|
|
|
/**
|
|
|
|
* Save the JSON to a file
|
|
|
|
*
|
|
|
|
* @param $json
|
|
|
|
*/
|
|
|
|
private 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|