Skip to content

Instantly share code, notes, and snippets.

@mrdarrengriffin
Created January 9, 2018 18:00
Show Gist options
  • Save mrdarrengriffin/dd195e6d6017a8d3d9a7fb76db553b01 to your computer and use it in GitHub Desktop.
Save mrdarrengriffin/dd195e6d6017a8d3d9a7fb76db553b01 to your computer and use it in GitHub Desktop.
<?php
Class MojangAPI{
private $db;
private $playersTable = "minecraft_player";
private $fetchInterval = 60;
public $using;
public $username;
public $uuid;
private $userData;
public function __construct(){
$this->db = Db::getInstance();
}
public function useUsername($username){
$this->using = "username";
$this->username = $username;
}
public function useUuid($uuid){
$this->using = "uuid";
$this->uuid = $uuid;
}
public function getUserData(){
$fetch = false;
if($dbUser = $this->getDbUser()){
$this->using = "uuid";
$this->uuid = $dbUser['uuid'];
if($this->shouldFetch($dbUser['last_fetched'])){
$fetch = true;
}else{
$userData = $dbUser;
}
}else{
$fetch = true;
}
if($fetch){
$userData = $this->fetchUserFromAPI();
if(!$userData){
return false;
}
$userData['last_fetched'] = time();
$this->updateOrCreateUser($userData,(bool)$dbUser);
}
unset($userData['id']);
$this->userData = $userData;
}
public function updateOrCreateUser($userData,$create = false){
if($create){
$this->db->where('uuid',$userData['uuid']);
$this->db->update($this->playersTable,$userData);
}else{
$this->db->insert($this->playersTable,$userData);
}
}
public function fetchUuidFromUsername($username){
$rawData = file_get_contents("https://api.mojang.com/users/profiles/minecraft/".$username);
if(!(bool)$rawData){return false;}
$jsonData = json_decode($rawData,true);
return $jsonData['id'];
}
public function fetchUserFromAPI(){
if($this->using == "username"){
$this->uuid = $this->fetchUuidFromUsername($this->username);
}
@$rawData = file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/".$this->uuid);
if((bool)$rawData == false){return false;}
$jsonData = json_decode($rawData,true);
$data = json_decode(base64_decode($jsonData['properties'][0]['value']),true);
$userData['uuid'] = $data['profileId'];
$userData['username'] = $data['profileName'];
$userData['skin_base64'] = base64_encode(@file_get_contents($data['textures']['SKIN']['url'])) ?: null;
$userData['cape_base64'] = base64_encode(@file_get_contents($data['textures']['CAPE']['url'])) ?: null;
$userData['name_history'] = @file_get_contents("https://api.mojang.com/user/profiles/".$this->uuid."/names");
return $userData;
}
public function getDbUser(){
$result = $this->db->where($this->using,$this->{$this->using})->getOne($this->playersTable);
if($this->db->count == 0){return false;}
return $result;
}
public function shouldFetch($timestamp){
if(in_array($timestamp,array("",NULL,0,-1,false))){return true;}
if((time() - $timestamp) > $this->fetchInterval){return true;}
return false;
}
public function getSkinSrc(){
return 'data:image/png;base64, '.$this->userData['skin_base64'];
}
public function getUsername(){
return $this->userData['username'];
}
public function getUuid(){
return $this->userData['uuid'];
}
public function getSecondsSinceLastFetch(){
if(time() - $this->userData['last_fetched'] == 0){return "just now";}
return time() - $this->userData['last_fetched'];
}
public function getSkinBase64(){
return $this->userData['skin_base64'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment