Lot's of bug fixes
This commit is contained in:
parent
ad320963fc
commit
7718ed6c32
196 changed files with 51167 additions and 3010 deletions
|
@ -3,92 +3,161 @@ 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'))) {
|
||||
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) {
|
||||
$json = file_get_contents('https://api.mojang.com/users/profiles/'.$uuid.'./names');
|
||||
if(empty($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'];
|
||||
}
|
||||
|
||||
$json = json_decode($json, true);
|
||||
if(isset($json['error'])) {
|
||||
$json = file_get_contents(storage_path('app/uuid/'.$uuid.'.json'));
|
||||
$json = json_decode($json, true);
|
||||
return $json['name'];
|
||||
}
|
||||
|
||||
$name = $json[count($json) -1]['name'];
|
||||
$json = [];
|
||||
$json['id'] = $uuid;
|
||||
$json['name'] = $name;
|
||||
self::saveJson($json);
|
||||
return $json['name'];
|
||||
} else {
|
||||
return $json['name'];
|
||||
}
|
||||
} else {
|
||||
$json = file_get_contents('https://api.mojang.com/user/profiles/'.$uuid.'/names');
|
||||
if(empty($json))
|
||||
$username = self::getUsernameUncached($uuid);
|
||||
if ($username === $uuid) {
|
||||
return $uuid;
|
||||
|
||||
$json = json_decode($json, true);
|
||||
if(isset($json['error']))
|
||||
return $uuid;
|
||||
|
||||
$name = $json[count($json) -1]['name'];
|
||||
$json = [];
|
||||
$json['id'] = $uuid;
|
||||
$json['name'] = $name;
|
||||
self::saveJson($json);
|
||||
return $json['name'];
|
||||
} 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) {
|
||||
$json = file_get_contents('https://api.mojang.com/users/profiles/minecraft/'.$username);
|
||||
if(empty($json)) {
|
||||
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 $username;
|
||||
return $json['name'];
|
||||
} else {
|
||||
$json = [];
|
||||
$json['id'] = $uuid;
|
||||
$json['name'] = $username;
|
||||
self::saveJson($json);
|
||||
return $json['id'];
|
||||
}
|
||||
|
||||
$json = json_decode($json, true);
|
||||
if(isset($json['error'])) {
|
||||
unlink(storage_path('app/uuid/'.$file));
|
||||
return $username;
|
||||
}
|
||||
|
||||
self::saveJson($json);
|
||||
return $json['id'];
|
||||
} else {
|
||||
return $json['id'];
|
||||
}
|
||||
}
|
||||
|
||||
$json = file_get_contents('https://api.mojang.com/users/profiles/minecraft/'.$username);
|
||||
if(empty($json))
|
||||
$uuid = self::getUUIDUncached($username);
|
||||
if ($uuid == false) {
|
||||
return $username;
|
||||
|
||||
$json = json_decode($json, true);
|
||||
if(isset($json['error']))
|
||||
return $username;
|
||||
|
||||
self::saveJson($json);
|
||||
return $json['id'];
|
||||
} else {
|
||||
$json = [];
|
||||
$json['id'] = $uuid;
|
||||
$json['name'] = $username;
|
||||
self::saveJson($json);
|
||||
return $json['id'];
|
||||
}
|
||||
}
|
||||
|
||||
public static function saveJson($json) {
|
||||
/**
|
||||
* 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'],
|
||||
|
|
Reference in a new issue