|
| 1 | +<?php |
| 2 | +class MinecraftProfile { |
| 3 | + private $username; |
| 4 | + private $uuid; |
| 5 | + private $properties; |
| 6 | + |
| 7 | + /** |
| 8 | + * @param string $username The player's username. |
| 9 | + * @param string $uuid The player's UUID. |
| 10 | + * @param array $properties The player's properties specified on their Mojang profile. |
| 11 | + */ |
| 12 | + function __CONSTRUCT($username, $uuid, $properties = array()) { |
| 13 | + $this->username = $username; |
| 14 | + $this->uuid = $uuid; |
| 15 | + $this->properties = $properties; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @return string The player's username. |
| 20 | + */ |
| 21 | + public function getUsername() { |
| 22 | + return $this->username; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @return string The player's UUID. |
| 27 | + */ |
| 28 | + public function getUUID() { |
| 29 | + return $this->uuid; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return array The player's properties listed on their mojang profile. |
| 34 | + */ |
| 35 | + public function getProperties() { |
| 36 | + return $this->properties; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return array Returns an array with keys of 'properties, usernname and uuid'. |
| 41 | + */ |
| 42 | + public function getProfileAsArray() { |
| 43 | + return array("username" => $this->username, "uuid" => $this->uuid, "properties" => $this->properties); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class ProfileUtils { |
| 48 | + /** |
| 49 | + * @param string $identifier Either the player's Username or UUID. |
| 50 | + * @param int $timeout The length in seconds of the http request timeout. |
| 51 | + * @return MinecraftProfile|null Returns null if fetching of profile failed. Else returns completed user profile. |
| 52 | + */ |
| 53 | + public static function getProfile($identifier, $timeout = 5) { |
| 54 | + if(strlen($identifier) <= 16){ |
| 55 | + $identifier = ProfileUtils::getUUIDFromUsername($identifier, $timeout); |
| 56 | + $url = "https://sessionserver.mojang.com/session/minecraft/profile/".$identifier['uuid']; |
| 57 | + } else { |
| 58 | + $url = "https://sessionserver.mojang.com/session/minecraft/profile/".$identifier; |
| 59 | + } |
| 60 | + $ctx = stream_context_create( |
| 61 | + array( |
| 62 | + 'http' => array( |
| 63 | + 'timeout' => $timeout |
| 64 | + ) |
| 65 | + ) |
| 66 | + ); |
| 67 | + $ret = file_get_contents($url, 0, $ctx); |
| 68 | + if(isset($ret) && $ret != null && $ret != false) { |
| 69 | + $data = json_decode($ret, true); |
| 70 | + return new MinecraftProfile($data['name'], $data['id'], $data['properties']); |
| 71 | + }else { |
| 72 | + return null; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param int $timeout http timeout in seconds |
| 78 | + * @param $username string Minecraft username. |
| 79 | + * @return array (Key => Value) "username" => Minecraft username (properly capitalized) "uuid" => Minecraft UUID |
| 80 | + */ |
| 81 | + public static function getUUIDFromUsername($username, $timeout = 5) { |
| 82 | + if(strlen($username) > 16) |
| 83 | + return array("username" => "", "uuid" => ""); |
| 84 | + $url = 'https://api.mojang.com/profiles/minecraft'; |
| 85 | + $options = array( |
| 86 | + 'http' => array( |
| 87 | + 'header' => "Content-type: application/json\r\n", |
| 88 | + 'method' => 'POST', |
| 89 | + 'content' => '["'.$username.'"]', |
| 90 | + 'timeout' => $timeout |
| 91 | + ), |
| 92 | + ); |
| 93 | + $context = stream_context_create($options); |
| 94 | + $result = file_get_contents($url, false, $context); |
| 95 | + |
| 96 | + // Verification |
| 97 | + if(isset($result) && $result != null && $result != false) |
| 98 | + { |
| 99 | + $ress = json_decode($result, true); |
| 100 | + $ress = $ress[0]; |
| 101 | + $res = Array("username" => $ress['name'], "uuid" => $ress['id']); |
| 102 | + return $res; |
| 103 | + } |
| 104 | + else |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param $usernames array of usernames to be collected. Maximum of 100 usernames. |
| 110 | + * @param int $timeout http timeout in seconds |
| 111 | + * @return array of array (Key => Value) "username" => Minecraft username (properly capitalized) "uuid" => Minecraft UUID |
| 112 | + */ |
| 113 | + public static function getUUIDsFromUsernames($usernames, $timeout = 5){ |
| 114 | + $usernames = array_splice($usernames, 0, 100); |
| 115 | + foreach($usernames as $user) |
| 116 | + if(strlen($user) > 16) |
| 117 | + return array("username" => "", "uuid" => ""); |
| 118 | + |
| 119 | + $url = 'https://api.mojang.com/profiles/minecraft'; |
| 120 | + $first = true; |
| 121 | + $contents = "["; |
| 122 | + foreach ($usernames as $user) { |
| 123 | + if(!$first) { |
| 124 | + $contents .= ", "; |
| 125 | + } |
| 126 | + $contents .= '"' . $user . '"'; |
| 127 | + $first = false; |
| 128 | + } |
| 129 | + $contents = "]"; |
| 130 | + |
| 131 | + $options = array( |
| 132 | + 'http' => array( |
| 133 | + 'header' => "Content-type: application/json\r\n", |
| 134 | + 'method' => 'POST', |
| 135 | + 'content' => $contents, |
| 136 | + 'timeout' => $timeout |
| 137 | + ) |
| 138 | + ); |
| 139 | + |
| 140 | + $context = stream_context_create($options); |
| 141 | + $result = file_get_contents($url, false, $context); |
| 142 | + |
| 143 | + // Verification |
| 144 | + if(isset($result) && $result != null && $result != false) |
| 145 | + { |
| 146 | + $output = array(); |
| 147 | + $ress = json_decode($result, true); |
| 148 | + for($i = 0; $i < count($ress); $i++){ |
| 149 | + $ress = $ress[$i]; |
| 150 | + $res = Array("username" => $ress['name'], "uuid" => $ress['id']); |
| 151 | + array_push($output, $res); |
| 152 | + } |
| 153 | + |
| 154 | + return $output; |
| 155 | + } |
| 156 | + else |
| 157 | + return null; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @param $uuid string UUID to format |
| 162 | + * @return string Properly formatted UUID (According to UUID v4 Standards xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx WHERE y = 8,9,A,or B and x = random digits.) |
| 163 | + */ |
| 164 | + public static function formatUUID($uuid) { |
| 165 | + $uid = ""; |
| 166 | + $uid .= substr($uuid, 0, 8)."-"; |
| 167 | + $uid .= substr($uuid, 8, 4)."-"; |
| 168 | + $uid .= substr($uuid, 12, 4)."-"; |
| 169 | + $uid .= substr($uuid, 16, 4)."-"; |
| 170 | + $uid .= substr($uuid, 20); |
| 171 | + return $uid; |
| 172 | + } |
| 173 | +} |
0 commit comments