-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUUIDUtils.php
137 lines (111 loc) · 2.82 KB
/
UUIDUtils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
function addDashes($uuid)
{
return substr($uuid, 0, 8) . '-' . substr($uuid, 8, 4) . '-' . substr($uuid, 12, 4) . '-' . substr($uuid, 16, 4) . '-' . substr($uuid, 20);
}
function isUUID($str)
{
$allowed = '0123456789abcdef';
$chars = str_split($str);
if(strlen($str) == 32)
{
// UUID without dashes
foreach($chars as $char)
{
if (strpos($allowed, $char) !== FALSE)
{
// Ok
}
else
{
return false;
}
}
return true;
}
elseif(strlen($str) == 32 + 4)
{
// UUID with dashes
$i = 0;
foreach($chars as $char)
{
if($i == 8 || $i == 13 || $i == 18 || $i == 23) // 8 13 18 23
{
if(!($char === '-'))
return false;
}
else
{
if (strpos($allowed, $char) !== FALSE)
{
}
else
{
return false;
}
}
$i++;
}
return true;
}
else
return false;
}
function getNameOrUUID($str)
{
$is_success = true;
$fail_message = '';
if(isUUID($str))
{
$player_uuid = str_replace('-', '', $str);
$result = file_get_contents('https://api.mojang.com/user/profiles/' . $player_uuid . '/names');
$player_uuid = addDashes($player_uuid);
$result_json = json_decode($result, true);
if(count($result_json) >= 1)
{
foreach($result_json as $change)
{
if($change == end($result_json))
$player_name = $change['name'];
}
}
else
{
$is_success = false;
$fail_message = 'uuid_not_found';
}
}
else
{
if(strlen($str) >= 3 && strlen($str) <= 16)
{
$player_name = $str;
$result = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . $player_name);
$result_json = json_decode($result);
if(isset($result_json->id))
{
$player_uuid = addDashes($result_json->id);
$player_name = $result_json->name;
}
else
{
$is_success = false;
$fail_message = 'name_not_found';
}
}
else
{
$is_success = false;
$fail_message = 'not_recognized';
}
}
if($is_success)
{
$obj = array('success' => true, 'name' => $player_name, 'uuid' => $player_uuid);
}
else
{
$obj = array('success' => false, 'message' => $fail_message);
}
return json_encode($obj);
}