Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
ServerAuth convert + Bug fixes for SimpleAuth converter + Czech trans…
Browse files Browse the repository at this point in the history
…lations

@lolmanz0 @SleepSpace9 @miguel456 Need to translate
invalid-hash-algorithm.
  • Loading branch information
DaPigGuy committed Apr 13, 2017
1 parent 5dd4bb6 commit 915a42e
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 18 deletions.
1 change: 1 addition & 0 deletions resources/languages/lang_cze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ invalid-file: "&cFile does not exist" #Needs translation
invalid-directory: "&cDirectory does not exist" #Needs translation
invalid-database: "&cInvalid database.\nAvailable databases: {db}" #Needs translation
invalid-auth: "&cThe auth plugin does not exist or is not supported by the converter.\nSupported auth plugins: {auth}" #Needs translation
invalid-hash-algorithm: "&c{algorithm} is not a valid hashing algorithm.\nAvailable algorithms are: {algorithms}." #Needs translation

#General
dont-say-password: "&cNesděluj své heslo!"
Expand Down
1 change: 1 addition & 0 deletions resources/languages/lang_eng.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ invalid-file: "&cFile does not exist"
invalid-directory: "&cDirectory does not exist"
invalid-database: "&cInvalid database.\nAvailable databases: {db}"
invalid-auth: "&cThe auth plugin does not exist or is not supported by the converter.\nSupported auth plugins: {auth}"
invalid-hash-algorithm: "&c{algorithm} is not a valid hashing algorithm.\nAvailable algorithms are: {algorithms}."

#General
dont-say-password: "&cDo not say your password."
Expand Down
1 change: 1 addition & 0 deletions resources/languages/lang_ger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ invalid-file: "&cFile does not exist" #Needs translation
invalid-directory: "&cDirectory does not exist" #Needs translation
invalid-database: "&cInvalid database.\nAvailable databases: {db}" #Needs translation
invalid-auth: "&cThe auth plugin does not exist or is not supported by the converter.\nSupported auth plugins: {auth}" #Needs translation
invalid-hash-algorithm: "&c{algorithm} is not a valid hashing algorithm.\nAvailable algorithms are: {algorithms}." #Needs translation

#General
dont-say-password: "&cSchreib dein Kennwort nicht in den Chat!"
Expand Down
1 change: 1 addition & 0 deletions resources/languages/lang_por.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ invalid-file: "&cFicheiro inexistente!"
invalid-directory: "&cDiretório/pasta não existente!"
invalid-database: "&cBase de dados inválida.\nBases de dados suportadas: {db}"
invalid-auth: "&cO plugin de conversão não existe ou não é suportado pelo conversor.\nPlugins de autenticação suportados: {auth}"
invalid-hash-algorithm: "&c{algorithm} is not a valid hashing algorithm.\nAvailable algorithms are: {algorithms}." #Needs translation

#General
dont-say-password: "&cNunca digas a tua palavra-passe a ninguém!"
Expand Down
48 changes: 41 additions & 7 deletions src/PiggyAuth/Commands/ConvertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,66 @@ public function execute(CommandSender $sender, $currentAlias, array $args)
case "yml":
case "yaml":
if (!isset($args[2])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <directory>");
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <directory>");
return false;
}
$this->plugin->simpleauthconverter->convertFromYML($args[2]);
break;
case "sqlite3":
if (!isset($args[2])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <file>");
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <file> <table>");
return false;
}
$this->plugin->simpleauthconverter->convertFromSQLite3($args[2]);
if (!isset($args[3])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " " . $args[2] . " <table>");
return false;
}
$this->plugin->simpleauthconverter->convertFromSQLite3($args[2], $args[3]);
break;
case "mysql":
if (!isset($args[2]) || !isset($args[3]) || !isset($args[4]) || !isset($args[5]) || !isset($args[6])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <db> <host> <user> <password> <name> <port>");
if (!isset($args[2]) || !isset($args[3]) || !isset($args[4]) || !isset($args[5]) || !isset($args[6]) || !isset($args[7])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <db> <host> <user> <password> <name> <port> <table>");
return false;
}
$this->plugin->simpleauthconverter->convertFromMySQL($args[2], $args[3], $args[4], $args[5], $args[6]);
$this->plugin->simpleauthconverter->convertFromMySQL($args[2], $args[3], $args[4], $args[5], $args[6], $args[7]);
break;
default:
$sender->sendMessage(str_replace("{db}", "YML, SQLite3, MySQL", $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "invalid-database")));
break;
}
break;
case "serverauth":
if (!isset($args[1])) {
$sender->sendMessage("/convert " . $args[0] . " <db> <info>");
return false;
}
switch (strtolower($args[1])) {
case "yml":
case "yaml":
if (!isset($args[2])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <directory> <algorithm>");
return false;
}
if (!isset($args[3])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " " . $args[2] . "<algorithm>");
return false;
}
$this->plugin->serverauthconverter->convertFromYML($args[2], $args[3]);
break;
case "mysql":
if (!isset($args[2]) || !isset($args[3]) || !isset($args[4]) || !isset($args[5]) || !isset($args[6]) || !isset($args[7]) || !isset($args[8])) {
$sender->sendMessage("/convert " . $args[0] . " " . $args[1] . " <db> <host> <user> <password> <name> <port> <table> <algorithm>");
return false;
}
$this->plugin->serverauthconverter->convertFromMySQL($args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8]);
break;
default:
$sender->sendMessage(str_replace("{db}", "YML, MySQL", $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "invalid-database")));
break;
}
break;
default:
$sender->sendMessage(str_replace("{auth}", "SimpleAuth", $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "invalid-auth")));
$sender->sendMessage(str_replace("{auth}", "SimpleAuth, ServerAuth", $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "invalid-auth")));
break;
}
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/PiggyAuth/Converter/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
interface Converter
{

public function convertFromSQLite3($file);
public function convertFromSQLite3($file, $table, $otherinfo);

public function convertFromMySQL($host, $user, $password, $name, $port);
public function convertFromMySQL($host, $user, $password, $name, $port, $table, $otherinfo);

public function convertFromYML($directory);
public function convertFromYML($directory, $otherinfo);
}
81 changes: 81 additions & 0 deletions src/PiggyAuth/Converter/ServerAuthConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace PiggyAuth\Converter;

use pocketmine\utils\Config;

class ServerAuthConverter implements Converter
{

public function __construct($plugin)
{
$this->plugin = $plugin;
}

public function convertFromSQLite3($file, $algorithm, $table)
{
return false;
}

public function convertFromMySQL($host, $user, $password, $name, $port, $table, $algorithm)
{
$credentials = $this->plugin->getConfig()->get("mysql");
$database = null;
$algorithms = hash_algos();
if (in_array($algorithm, $algorithms)) {
if ($credentials["host"] == $host && $credentials["user"] == $user && $credentials["password"] == $password && $credentials["name"] == $name && $credentials["port"] == $port) {
$database = $this->plugin->database->db;
} else {
$database = new \mysqli($credentials["host"], $credentials["user"], $credentials["password"], $credentials["name"], $credentials["port"]);
}
$result = $database->query("SELECT * FROM " . $table);
if ($result instanceof \mysqli_result) {
while ($row = $result->fetch_assoc()) {
if ($this->plugin->database->getOfflinePlayer($row["user"])) {
$this->plugin->getLogger()->info(str_replace("{player}", $row["user"], $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "player-account-already-exists")));
continue;
}
$this->plugin->database->insertDataWithoutPlayerObject($row["user"], $row["password"], "none", mt_rand(1000, 9999), "ServerAuth_" . $algorithm);
}
$result->free();
return true;
}
} else {
$this->plugin->getLogger()->info(str_replace("{algorithms}", implode(", ", $algorithms), str_replace("{algorithm}", $algorithm, $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "invalid-hash-algorithm"))));
return false;
}
}

public function convertFromYML($directoryname, $algorithm)
{
$algorithms = hash_algos();
if (in_array($algorithm, $algorithms)) {
if (is_dir($this->plugin->getDataFolder() . "convert/" . $directoryname . "/")) {
$files = scandir($this->plugin->getDataFolder() . "convert/" . $directoryname . "/");
foreach ($files as $file) {
if ($file !== "." && $file !== "..") {
if (strpos($file, ".yml") !== false) {
$yml = new Config($this->plugin->getDataFolder() . "convert/" . $directoryname . "/" . $file);
$data = $yml->getAll();
$name = str_replace(".yml", "", $file);
if ($this->plugin->database->getOfflinePlayer($name)) {
$this->plugin->getLogger()->info(str_replace("{player}", $name, $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "player-account-already-exists")));
continue;
}
$this->plugin->database->insertDataWithoutPlayerObject($name, $data["password"], "none", mt_rand(1000, 9999), "ServerAuth_" . $algorithm);
} else {
$this->plugin->getLogger()->info(str_replace("{file}", $file, $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "file-not-yml")));
}
}
}
} else {
$this->plugin->getLogger()->info(str_replace("{file}", $directoryname, $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "invalid-directory")));
return false;
}
} else {
$this->plugin->getLogger()->info(str_replace("{algorithms}", implode(", ", $algorithms), str_replace("{algorithm}", $algorithm, $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "invalid-hash-algorithm"))));
return false;
}
return true;
}
}
12 changes: 6 additions & 6 deletions src/PiggyAuth/Converter/SimpleAuthConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function __construct($plugin)
$this->plugin = $plugin;
}

public function convertFromSQLite3($file)
public function convertFromSQLite3($file, $table, $otherinfo = null)
{
if (file_exists($this->plugin->getDataFolder() . "convert/" . $file)) {
if (strpos($file, ".db") !== false) {
$db = new \SQLite3($this->plugin->getDataFolder() . "convert/" . $file, SQLITE3_OPEN_READWRITE);
$result = $db->query("SELECT * from players");
$result = $db->query("SELECT * from " . $table);
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
if ($this->plugin->database->getOfflinePlayer($row["name"])) {
$this->plugin->getLogger()->info(str_replace("{player}", $row["name"], $this->plugin->languagemanager->getMessageFromLanguage($this->plugin->languagemanager->getDefaultLanguage(), "player-account-already-exists")));
Expand All @@ -36,16 +36,16 @@ public function convertFromSQLite3($file)
return true;
}

public function convertFromMySQL($host, $user, $password, $name, $port)
public function convertFromMySQL($host, $user, $password, $name, $port, $table, $otherinfo = null)
{
$credentials = $this->plugin->getConfig()->get("mysql");
$database = null;
if ($credentials["host"] == $host && $credentials["user"] == $user && $credentials["password"] == $password && $credentials["name"] == $name && $credentials["port"] == $port) {
$database = $this->plugin->database->db;
} else {
$database = new \mysqli("sql3.freemysqlhosting.net", "sql3160794", "EA37ny32Gg", "sql3160794", 3606);
$database = new \mysqli($credentials["host"], $credentials["user"], $credentials["password"], $credentials["name"], $credentials["port"]);
}
$result = $database->query("SELECT * FROM simpleauth_players");
$result = $database->query("SELECT * FROM " . $table);
if ($result instanceof \mysqli_result) {
while ($row = $result->fetch_assoc()) {
if ($this->plugin->database->getOfflinePlayer($row["name"])) {
Expand All @@ -59,7 +59,7 @@ public function convertFromMySQL($host, $user, $password, $name, $port)
}
}

public function convertFromYML($directoryname)
public function convertFromYML($directoryname, $otherinfo = null)
{
if (is_dir($this->plugin->getDataFolder() . "convert/" . $directoryname . "/")) {
$directories = scandir($this->plugin->getDataFolder() . "convert/" . $directoryname . "/");
Expand Down
2 changes: 1 addition & 1 deletion src/PiggyAuth/Databases/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(Main $plugin)
$this->plugin = $plugin;
$credentials = $this->plugin->getConfig()->get("mysql");
$this->db = new \mysqli($credentials["host"], $credentials["user"], $credentials["password"], $credentials["name"], $credentials["port"]);
$task = new MySQLTask($credentials, "CREATE TABLE IF NOT EXISTS players (name VARCHAR(100) PRIMARY KEY, password VARCHAR(200), email VARCHAR(100), pin INT, ip VARCHAR(32), uuid VARCHAR(100), attempts INT, xbox BIT(1), language VARCHAR(3), auth VARCHAR(10));");
$task = new MySQLTask($credentials, "CREATE TABLE IF NOT EXISTS players (name VARCHAR(100) PRIMARY KEY, password VARCHAR(200), email VARCHAR(100), pin INT, ip VARCHAR(32), uuid VARCHAR(100), attempts INT, xbox BIT(1), language VARCHAR(3), auth VARCHAR(100));");
$this->plugin->getServer()->getScheduler()->scheduleAsyncTask($task);
//Updater
$result = $this->db->query("SELECT * FROM players");
Expand Down
2 changes: 1 addition & 1 deletion src/PiggyAuth/Databases/SQLite3.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(Main $plugin)
$this->plugin = $plugin;
if (!file_exists($this->plugin->getDataFolder() . "players.db")) {
$this->db = new \SQLite3($this->plugin->getDataFolder() . "players.db", SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
$this->db->exec("CREATE TABLE players (name VARCHAR(100) PRIMARY KEY, password VARCHAR(200), email VARCHAR(100), pin INT, ip VARCHAR(32), uuid VARCHAR(100), attempts INT, xbox BIT(1), language VARCHAR(3), auth VARCHAR(10));");
$this->db->exec("CREATE TABLE players (name VARCHAR(100) PRIMARY KEY, password VARCHAR(200), email VARCHAR(100), pin INT, ip VARCHAR(32), uuid VARCHAR(100), attempts INT, xbox BIT(1), language VARCHAR(3), auth VARCHAR(100));");
} else {
$this->db = new \SQLite3($this->plugin->getDataFolder() . "players.db", SQLITE3_OPEN_READWRITE);
//Updater
Expand Down
13 changes: 13 additions & 0 deletions src/PiggyAuth/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PiggyAuth\Commands\SendPinCommand;
use PiggyAuth\Commands\SetLanguageCommand;
use PiggyAuth\Commands\UnregisterCommand;
use PiggyAuth\Converter\ServerAuthConverter;
use PiggyAuth\Converter\SimpleAuthConverter;
use PiggyAuth\Emails\EmailManager;
use PiggyAuth\Events\PlayerChangePasswordEvent;
Expand Down Expand Up @@ -103,6 +104,7 @@ class Main extends PluginBase
public $keytime = 299; //300 = Reset
public $languagemanager;
public $messagetick;
public $serverauthconverter;
public $sessionmanager;
public $simpleauthconverter;
public $timeouttick;
Expand Down Expand Up @@ -159,6 +161,7 @@ public function onEnable()
$this->languagemanager = new LanguageManager($this);
$this->emailmanager = new EmailManager($this, $this->getConfig()->getNested("emails.mailgun.domain"), $this->getConfig()->getNested("emails.mailgun.api"), $this->getConfig()->getNested("emails.mailgun.public-api"), $this->getConfig()->getNested("emails.mailgun.from"));
$this->simpleauthconverter = new SimpleAuthConverter($this);
$this->serverauthconverter = new ServerAuthConverter($this);
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
foreach ($this->getServer()->getOnlinePlayers() as $player) { //Reload, players still here but plugin restarts!
$this->startSession($player);
Expand Down Expand Up @@ -197,6 +200,16 @@ public function generatePin(Player $player)

public function isCorrectPassword(Player $player, $password)
{
if (strpos($this->sessionmanager->getSession($player)->getOriginAuth(), "ServerAuth") !== false) {
$auth = explode("_", $this->sessionmanager->getSession($player)->getOriginAuth());
if (isset($auth[0]) && isset($auth[1])) {
if (hash($auth[1], $password) == $this->sessionmanager->getSession($player)->getPassword()) {
$this->sessionmanager->getSession($player)->updatePlayer("auth", "PiggyAuth");
return true;
}
return false;
}
}
switch ($this->sessionmanager->getSession($player)->getOriginAuth()) {
case "SimpleAuth":
if (hash_equals($this->sessionmanager->getSession($player)->getPassword(), $this->hashSimpleAuth(strtolower($player->getName()), $password))) {
Expand Down

0 comments on commit 915a42e

Please sign in to comment.