Skip to content

Instantly share code, notes, and snippets.

@PEMapModder
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save PEMapModder/4665407af4a39e315ec0 to your computer and use it in GitHub Desktop.

Select an option

Save PEMapModder/4665407af4a39e315ec0 to your computer and use it in GitHub Desktop.
<?php
@define("STDIN", fopen("php://stdin", "rt"));
function query($query){
echo $query;
return trim(fgets($query));
}
function out($message){
echo date("[H:i:s] ") . $message . PHP_EOL;
}
function quit($message, $code = 2){
out("Fatal: " . $message);
exit($code);
}
function addPlayer($name, $data){
global $players;
$data["name"] = $name;
$players[$name] = $data;
}
$players = [];
$path = query("Location of SimpleAuth players folder (relative to " . getcwd() . " or absolute): ");
if(!is_dir($path)){
quit("$path is not a directory!");
}
if(trim(basename($path), "/\\") !== "players") {
quit("The SimpleAuth players folder is named 'players instead of '" . basename($path) . "'.");
}
chdir($path);
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(".")) as $file){
if(is_file($file) and substr($file, -4) !== ".yml"){
addPlayer(basename($file, ".yml"), yaml_parse(file_get_contents($file)));
}
}
out("Finished loading database. Exporting to MySQL.");
$db = new mysqli(query("MySQL host: "), query("MySQL username: "), query("MySQL password: "), query("MySQL database name: "));
out("Preparing database...");
$db->query("CREATE TABLE IF NOT EXISTS simpleauth_players(name VARCHAR(16) PRIMARY KEY,hash CHAR(128),registerdate INT,logindate INT,lastip VARCHAR(50));");
out("Preparing query...");
$query = "INSERT INTO simpleauth_players(name, registerdate, logindate, lastip, hash)VALUES";
$i = 0;
$query .= implode(",", array_map(function($datum){
global $i;
extract($datum);
echo "[" . (++$i) . "] $name\r";
$name = $db->escape_string($name);
$lastip = $db->escape_string($lastip);
$hash = $db->escape_string($hash);
return "('$name',$registerdate,$logindate,'$lastip','$hash')";
}, $players));
out("Sending a query of " . strlen($query) . " bytes to MySQL database...");
$r = $db->query($query);
$db->close();
quit("Completed query with result: " . var_export($r, true), 1);
@Matthww

Matthww commented Jun 24, 2015

Copy link
Copy Markdown

Thnx for making this

but i have error when i do php Port-SimpleAuth.php

i get this error:
PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in /home/matthew/Port-SimpleAuth.php on line 8

@Matthww

Matthww commented Jul 25, 2015

Copy link
Copy Markdown

Do you have time to fix this?

@Matthww

Matthww commented Aug 4, 2015

Copy link
Copy Markdown

matthew@MineCraft2Matthew:~/Pocketmine/plugins/SimpleAuth$ php Port-SimpleAuth.php
Location of SimpleAuth players folder (relative to /home/matthew/Pocketmine/plugins/SimpleAuth or absolute): PHP Warning: fgets() expects parameter 1 to be resource, string given in /home/matthew/Pocketmine/plugins/SimpleAuth/Port-SimpleAuth.php on line 5
[16:55:47] Fatal: is not a directory!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment