Skip to content

Instantly share code, notes, and snippets.

@LukeHandle
Last active August 29, 2015 14:16
Show Gist options
  • Save LukeHandle/fb2d6ea21e709df7b2eb to your computer and use it in GitHub Desktop.
Save LukeHandle/fb2d6ea21e709df7b2eb to your computer and use it in GitHub Desktop.
Simple PHP script to add UUID to users in an XenForo database who have "NULL" UUIDs (for use with the Minecraft name changer plugin)
<?php
/*
* LukeHandle
* Generic disclaimer about no responsibility for this script
*
* Updated 9th March 2015
*/
$httpRequests = 0;
$records = 0;
$mysqli = new mysqli("MYSQL_ADDRESS", "USERNAME", "PASSWORD", "TABLE");
function getUUIDFromUsername($username, $timeout = 5) {
global $httpRequests;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://api.mojang.com/users/profiles/minecraft/" . $username . "?at=0",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => $timeout
));
$json_response = curl_exec($ch);
$httpRequests++;
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
curl_close($ch);
if($status === 200) {
$json_response = json_decode($json_response, 1);
$uuid = $json_response['id'];
return $uuid;
} else {
error_log(__FILE__.' '.__FUNCTION__.' '.$username.' '.$status.' '.$time.'s String: '.str_replace(array("\r", "\n"), "", $json_response));
return false;
}
}
$result = $mysqli->query("SELECT user_id, username FROM xf_user WHERE uuid IS NULL");
$rows = $result->fetch_all();
foreach($rows as $row){
$records++;
$uuid = getUUIDFromUsername($row[1]);
if ($uuid !== false) {
echo "XF ID: $row[0], Username: $row[1], UUID: $uuid\n";
if ($mysqli->query("UPDATE xf_user SET uuid = \"$uuid\" WHERE user_id = $row[0]") === TRUE) {
echo "XF ID: $row[0], updated successfully\n\n";
} else {
echo "XF ID: $row[0], Error updating record: " . $mysqli->error . "\n\n";
}
} else {
echo "XF ID: $row[0], Username: $row[1], UUID: NOT AVAILABLE\n\n";
}
}
echo "HTTP Requests: " . $httpRequests . "\n";
echo "Records Processed: " . $records . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment