Skip to content

Instantly share code, notes, and snippets.

@AmauryCarrade
Created October 4, 2015 18:05
Show Gist options
  • Save AmauryCarrade/f6792e194dfc1c234b71 to your computer and use it in GitHub Desktop.
Save AmauryCarrade/f6792e194dfc1c234b71 to your computer and use it in GitHub Desktop.
MC History
<?php
use Symfony\Component\HttpFoundation\Response;
$app->get("/tools/minecraft/history", function() use($app)
{
$user = isset($_GET["user"]) ? trim($_GET["user"]) : "";
$valid = true;
$emptyHistory = false;
$data = array();
if(!empty($user))
{
$name = null;
$uuid = null;
// Names & UUID
if(strlen($user) <= 16)
{
$name = $user;
$c = curl_init('https://api.mojang.com/users/profiles/minecraft/'.$name);
curl_setopt($c, CURLOPT_POST, 0);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($c);
curl_close($c);
$raw_data = json_decode($result, true);
if(isset($raw_data['id']))
{
$uuid = $raw_data['id'];
$data['legacy'] = isset($raw_data['legacy']);
$data['input_is_uuid'] = false;
}
else
{
$c = curl_init('https://api.mojang.com/users/profiles/minecraft/'.$name.'?at=1422986400');
curl_setopt($c, CURLOPT_POST, 0);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($c);
curl_close($c);
$raw_data = json_decode($result, true);
if(isset($raw_data['id']))
{
$uuid = $raw_data['id'];
}
else
{
$valid = false;
}
$data['legacy'] = isset($raw_data['legacy']);
$data['input_is_uuid'] = false;
}
}
else
{
$uuid = str_replace("-", "", $user);
$data['input_is_uuid'] = true;
}
// UUID formatting
$data['uuid'] = $uuid;
if(!isset($_GET['raw_uuid']))
{
$data['uuid'] = preg_replace("#(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})#", "$1-$2-$3-$4-$5", $data['uuid']);
}
// Names history
$c = curl_init('https://api.mojang.com/user/profiles/' . $uuid . '/names');
curl_setopt($c, CURLOPT_POST, 0);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($c);
curl_close($c);
$raw_data = json_decode($result, true);
if(isset($raw_data[0]['name']))
{
if($name != null)
{
$data['name'] = $name;
}
$data['history'] = array();
$data['history'][0]['date'] = null;
$data['history'][0]['name'] = $raw_data[0]['name'];
$i = 0;
while(isset($raw_data[++$i]))
{
$data['history'][$i]['date'] = new \DateTime("@" . $raw_data[$i]['changedToAt'] / 1000); // timestamp
$data['history'][$i]['name'] = $raw_data[$i]['name'];
}
$data['current_name'] = $raw_data[$i - 1]['name'];
}
else
{
$emptyHistory = true;
}
}
return new Response($app['twig']->render('tools/minecraft/history.html.twig', array(
'section' => 'tools.minecraft.history',
'user' => $user,
'valid' => $valid,
'emptyHistory' => $emptyHistory,
'data' => $data
)));
})
->bind("tools.minecraft.history");
$app->get("/uuid", function() use($app)
{
return $app->redirect($app['url_generator']->generate('tools.minecraft.history'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment