-
Notifications
You must be signed in to change notification settings - Fork 1
/
mc-api.php
101 lines (100 loc) · 2.81 KB
/
mc-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Developed by Luuuuuis on 09.05.20, 20:35.
* Last modified 09.05.20, 18:39.
* Copyright (c) 2020.
*/
/**
* Get UUID from username
*
* @param string $username
* @return string|bool UUID (without dashes) on success, false on failure
*/
function username_to_uuid($username) {
$profile = username_to_profile($username);
if (is_array($profile) and isset($profile['id'])) {
return $profile['id'];
}
return false;
}
/**
* Get Profile (Username and UUID) from username
*
* @uses http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time
*
* @param string $username
* @return array|bool Array with id and name, false on failure
*/
function username_to_profile($username) {
if (is_valid_username($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 username from UUID
*
* @uses http://wiki.vg/Mojang_API#UUID_-.3E_Name_history
*
* @param string $uuid
* @return string|bool Username on success, false on failure
*/
function uuid_to_username($uuid) {
$uuid = minify_uuid($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 string is a valid Minecraft username
*
* @param string $string to check
* @return bool Whether username is valid or not
*/
function is_valid_username($string) {
return is_string($string) and strlen($string) >= 2 and strlen($string) <= 16 and ctype_alnum(str_replace('_', '', $string));
}
/**
* Remove dashes from UUID
*
* @param string $uuid
* @return string|bool UUID without dashes (32 chars), false on failure
*/
function minify_uuid($uuid) {
if (is_string($uuid)) {
$minified = str_replace('-', '', $uuid);
if (strlen($minified) === 32) {
return $minified;
}
}
return false;
}
/**
* Add dashes to an UUID
*
* @param string $uuid
* @return string|bool UUID with dashes (36 chars), false on failure
*/
function format_uuid($uuid) {
$uuid = minify_uuid($uuid);
if (is_string($uuid)) {
return substr($uuid, 0, 8) . '-' . substr($uuid, 8, 4) . '-' . substr($uuid, 12, 4) . '-' . substr($uuid, 16, 4) . '-' . substr($uuid, 20, 12);
}
return false;
}