diff --git a/README.md b/README.md index 735c662..8dcc97c 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,11 @@ Safe & feature-rich auth plugin. # Changelog +###v1.0.7 +* Preparation for MySQL support!MySQL support! +* Fixed messages +* Xbox bypass (not finished) + ###v1.0.6 * You can now use & for color codes diff --git a/plugin.yml b/plugin.yml index 53c6e46..156a4d7 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: PiggyAuth main: PiggyAuth\Main -version: 1.0.6 +version: 1.0.7 api: [2.0.0] load: POSTWORLD author: MCPEPIG diff --git a/resources/config.yml b/resources/config.yml index f835c38..f1b286d 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -41,10 +41,12 @@ blindness: true see-messages: false #If enabled, players joining when another player has their username is kicked single-session: true -#If enabled, only players with xbox can login (ClearSky only!) -xbox-only: false +#If enabled, players with logged in with xbox are automatically registered/logged in. (ClearSky only!) +xbox-bypass: false +#Database (sqlite3) +database: sqlite3 #Version -version: v1.0.5 +version: v1.0.7 #Messages @@ -65,6 +67,7 @@ register: "&6Please register by typing in /register " register-popup: "&6Please register." register-tip: "&6Please register." register-success: "&aYou have been registered. Your pin is {pin}." +auto-register: "&aYou were automatically registered. Your password is {password}. Change it with /changepassword." already-registered: "&cYou are already registered." password-too-short: "&cPassword is too short." password-not-match: "&cYour password is not the same." diff --git a/src/PiggyAuth/Commands/PinCommand.php b/src/PiggyAuth/Commands/PinCommand.php index 454fee1..9053819 100644 --- a/src/PiggyAuth/Commands/PinCommand.php +++ b/src/PiggyAuth/Commands/PinCommand.php @@ -20,7 +20,7 @@ public function execute(CommandSender $sender, $currentAlias, array $args) { $sender->sendMessage("§cYou must use the command in-game."); return false; } - $sender->sendMessage(str_replace("{pin}", $this->plugin->getPin($sender), $this->plugin->getConfig()->get("pin"))); + $sender->sendMessage(str_replace("{pin}", $this->plugin->database->getPin($sender->getName()), $this->plugin->getMessage("pin"))); return true; } diff --git a/src/PiggyAuth/Databases/Database.php b/src/PiggyAuth/Databases/Database.php new file mode 100644 index 0000000..a3d1e3b --- /dev/null +++ b/src/PiggyAuth/Databases/Database.php @@ -0,0 +1,16 @@ +getDataFolder() . "players.db")) { + $this->db = new \SQLite3($plugin->getDataFolder() . "players.db", SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE); + $this->db->exec("CREATE TABLE players (name TEXT PRIMARY KEY, password TEXT, pin INT, uuid INT, attempts INT);"); + } else { + $this->db = new \SQLite3($plugin->getDataFolder() . "players.db", SQLITE3_OPEN_READWRITE); + //Updater + } + if($outdated) { + $this->db->exec("ALTER TABLE players ADD COLUMN pins INT"); //Just in case :P + $this->db->exec("ALTER TABLE players ADD COLUMN attempts INT"); + } + } + + public function getPlayer($player) { + $player = strtolower($player); + $statement = $this->db->prepare("SELECT * FROM players WHERE name = :name"); + $statement->bindValue(":name", $player, SQLITE3_TEXT); + $result = $statement->execute(); + if($result instanceof \SQLite3Result) { + $data = $result->fetchArray(SQLITE3_ASSOC); + $result->finalize(); + if(isset($data["name"])) { + unset($data["name"]); + $statement->close(); + return $data; + } + } + $statement->close(); + return null; + } + + public function updatePlayer($player, $password, $pin, $uuid, $attempts) { + $statement = $this->db->prepare("UPDATE players SET pin = :pin, password = :password, uuid = :uuid, attempts = :attempts WHERE name = :name"); + $statement->bindValue(":name", strtolower($player), SQLITE3_TEXT); + $statement->bindValue(":password", $password, SQLITE3_TEXT); + $statement->bindValue(":pin", $pin, SQLITE3_INTEGER); + $statement->bindValue(":uuid", $uuid, SQLITE3_INTEGER); + $statement->bindValue(":attempts", $attempts, SQLITE3_INTEGER); + $statement->execute(); + } + + public function getPin($player) { + $data = $this->getPlayer($player); + if(!is_null($data)) { + if(!isset($data["pin"])) { + $pin = mt_rand(1000, 9999); //If you use $this->generatePin(), there will be issues! + $this->updatePlayer($player, $pin, $this->getPassword($player), $this->getUUID($player), $this->getAttempts($player)); + return $pin; + } + return $data["pin"]; + } + return null; + } + + public function getPassword($player) { //ENCRYPTED! + $data = $this->getPlayer($player); + if(!is_null($data)) { + return $data["password"]; + } + return null; + } + + public function getUUID($player) { + $data = $this->getPlayer($player); + if(!is_null($data)) { + return $data["uuid"]; + } + return null; + } + + public function getAttempts($player) { + $data = $this->getPlayer($player); + if(!is_null($data)) { + if(!isset($data["attempts"])) { + $this->updatePlayer($player, $this->getPin($player), $this->getPassword($player), $this->getUUID($player), 0); + return 0; + } + return $data["attempts"]; + } + return null; + } +} diff --git a/src/PiggyAuth/EventListener.php b/src/PiggyAuth/EventListener.php index f602d5f..02e1d83 100644 --- a/src/PiggyAuth/EventListener.php +++ b/src/PiggyAuth/EventListener.php @@ -185,14 +185,23 @@ public function onJoin(PlayerJoinEvent $event) { $effect->setVisible(false); $player->addEffect($effect); } - if($this->plugin->getConfig()->get("auto-authentication")) { - $data = $this->plugin->getPlayer($player->getName()); - if(!is_null($data)) { - if($player->getUniqueId()->toString() == $data["uuid"]) { - $this->plugin->force($player); - return true; - } + $data = $this->plugin->database->getPlayer($player->getName()); + if($this->plugin->getConfig()->get("auto-authentication") && !is_null($data) && $player->getUniqueId()->toString() == $data["uuid"]) { + $this->plugin->force($player); + return true; + } + if($this->plugin->getConfig()->get("xbox-bypass") && $this->plugin->getServer()->getName() == "ClearSky" && $player->isAuthenticated()) { + $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; + $randompassword = []; + $characteramount = strlen($alphabet) - 1; + for($i = 0; $i < $this->getConfig()->get("minimum-password-length") - 1; $i++) { + $character = mt_rand(0, $alphaLength); + array_push($randompassword, $characters[$character]); } + $randompassword = implode($randompassword); + $this->plugin->register($player, $randompassword, $randompassword); + $player->sendMessage(str_replace("{password}", $randompassword, $this->plugin->getMessage("auto-registered"))); + return true; } $this->plugin->getServer()->getScheduler()->scheduleDelayedTask(new TimeoutTask($this->plugin, $player), $this->plugin->getConfig()->get("timeout") * 20); } @@ -214,14 +223,6 @@ public function onPrelogin(PlayerPreLoginEvent $event) { $event->setCancelled(); } } - if($this->plugin->getConfig()->get("xbox-only")) { - if($this->plugin->getServer()->getName() == "ClearSky") { - if(!$player->isXboxAuthenticated()) { - $player->close("", "You must use Xbox to login."); - $event->setCancelled(); - } - } - } } public function onQuit(PlayerQuitEvent $event) { diff --git a/src/PiggyAuth/Main.php b/src/PiggyAuth/Main.php index 82b560c..e816959 100644 --- a/src/PiggyAuth/Main.php +++ b/src/PiggyAuth/Main.php @@ -8,6 +8,7 @@ use PiggyAuth\Commands\LogoutCommand; use PiggyAuth\Commands\RegisterCommand; use PiggyAuth\Commands\ResetPasswordCommand; +use PiggyAuth\Databases\SQLite3; use PiggyAuth\Tasks\MessageTick; use PiggyAuth\Tasks\PopupTipTick; use PiggyAuth\Tasks\TimeoutTask; @@ -21,29 +22,10 @@ class Main extends PluginBase { public $confirmPassword; public $messagetick; public $tries; + public $database; public function onEnable() { $this->saveDefaultConfig(); - if(!file_exists($this->getDataFolder() . "players.db")) { - $this->db = new \SQLite3($this->getDataFolder() . "players.db", SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE); - $this->db->exec("CREATE TABLE players (name TEXT PRIMARY KEY, password TEXT, pin INT, uuid INT, attempts INT);"); - } else { - $this->db = new \SQLite3($this->getDataFolder() . "players.db", SQLITE3_OPEN_READWRITE); - //Updater - if(!$this->getConfig()->exists("version")) { - $this->getConfig()->set("version", $this->getDescription()->getVersion()); - $this->getConfig()->save(); - $this->db->exec("ALTER TABLE players ADD COLUMN pins INT"); //Just in case :P - $this->db->exec("ALTER TABLE players ADD COLUMN attempts INT"); - } elseif($this->getConfig()->get("version") < $this->getDescription()->getVersion()) { - switch($this->getConfig()->get("version")) { - default: - $this->getConfig()->set("version", $this->getDescription()->getVersion()); - $this->getConfig()->save(); - break; - } - } - } $this->getServer()->getCommandMap()->register('changepassword', new ChangePasswordCommand('changepassword', $this)); $this->getServer()->getCommandMap()->register('forgotpassword', new ForgotPasswordCommand('forgotpassword', $this)); $this->getServer()->getCommandMap()->register('login', new LoginCommand('login', $this)); @@ -55,77 +37,34 @@ public function onEnable() { if($this->getConfig()->get("popup") || $this->getConfig()->get("tip")) { $this->getServer()->getScheduler()->scheduleRepeatingTask(new PopupTipTick($this), 20); } - $this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this); - $this->getLogger()->info("§aEnabled."); - } - - public function getPlayer($player) { - $player = strtolower($player); - $statement = $this->db->prepare("SELECT * FROM players WHERE name = :name"); - $statement->bindValue(":name", $player, SQLITE3_TEXT); - $result = $statement->execute(); - if($result instanceof \SQLite3Result) { - $data = $result->fetchArray(SQLITE3_ASSOC); - $result->finalize(); - if(isset($data["name"])) { - unset($data["name"]); - $statement->close(); - return $data; + $outdated = false; + if(!$this->getConfig()->exists("version")) { + $this->getConfig()->set("version", $this->getDescription()->getVersion()); + $this->getConfig()->save(); + $outdated = true; + } elseif($this->getConfig()->get("version") < $this->getDescription()->getVersion()) { + switch($this->getConfig()->get("version")) { + default: + $this->getConfig()->set("version", $this->getDescription()->getVersion()); + $this->getConfig()->save(); + break; } } - $statement->close(); - return null; - } - - public function updatePlayer(Player $player, $password, $pin, $uuid, $attempts) { - $statement = $this->db->prepare("UPDATE players SET pin = :pin, password = :password, uuid = :uuid, attempts = :attempts WHERE name = :name"); - $statement->bindValue(":name", strtolower($player->getName()), SQLITE3_TEXT); - $statement->bindValue(":password", $password, SQLITE3_TEXT); - $statement->bindValue(":pin", $pin, SQLITE3_INTEGER); - $statement->bindValue(":uuid", $uuid, SQLITE3_INTEGER); - $statement->bindValue(":attempts", $attempts, SQLITE3_INTEGER); - $statement->execute(); - } - - public function getPin(Player $player) { - $data = $this->getPlayer($player->getName()); - if(!is_null($data)) { - if(!isset($data["pin"])) { - $pin = mt_rand(1000, 9999); //If you use $this->generatePin(), there will be issues! - $this->updatePlayer($player, $pin, $this->getPassword($player), $this->getUUID($player), $this->getAttempts($player)); - return $pin; - } - return $data["pin"]; + switch($this->getConfig()->get("database")) { + case "sqlite3": + $this->database = new SQLite3($this, $outdated); + break; + default: + $this->database = new SQLite3($this, $outdated); + $this->getLogger()->error("§cDatabase not found, using default."); + break; } - return null; - } - - public function getPassword(Player $player) { //ENCRYPTED! - $data = $this->getPlayer($player->getName()); - if(!is_null($data)) { - return $data["password"]; - } - return null; - } - - public function getUUID(Player $player) { - $data = $this->getPlayer($player->getName()); - if(!is_null($data)) { - return $data["uuid"]; - } - return null; + $this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this); + $this->getLogger()->info("§aEnabled."); } - public function getAttempts(Player $player) { - $data = $this->getPlayer($player->getName()); - if(!is_null($data)) { - if(!isset($data["attempts"])) { - $this->updatePlayer($player, $this->getPin($player), $this->getPassword($player), $this->getUUID($player), 0); - return 0; - } - return $data["attempts"]; - } - return null; + public function getDatabase() { + return $this->database; } public function generatePin(Player $player) { @@ -137,14 +76,14 @@ public function generatePin(Player $player) { } public function isCorrectPassword(Player $player, $password) { - if(password_verify($password, $this->getPassword($player))) { + if(password_verify($password, $this->database->getPassword($player->getName()))) { return true; } return false; } public function isCorrectPin(Player $player, $pin) { - if($pin == $this->getPin($player)) { + if($pin == $this->database->getPin($player->getName())) { return true; } return false; @@ -156,7 +95,7 @@ public function isAuthenticated(Player $player) { } public function isRegistered($player) { - return $this->getPlayer(strtolower($player)) !== null; + return $this->database->getPlayer($player) !== null; } public function login(Player $player, $password) { @@ -172,7 +111,7 @@ public function login(Player $player, $password) { if(isset($this->tries[strtolower($player->getName())])) { $this->tries[strtolower($player->getName())]++; if($this->tries[strtolower($player->getName())] >= $this->getConfig()->get("tries")) { - $this->updatePlayer($player, $this->getPassword($player), $this->getPin($player), $this->getUUID($player), $this->getAttempts($player) + 1); + $this->database->updatePlayer($player->getName(), $this->database->getPassword($player->getName()), $this->database->getPin($player->getName()), $this->database->getUUID($player->getName()), $this->database->getAttempts($player->getName()) + 1); $player->kick($this->getMessage("too-many-tries")); return false; } @@ -204,11 +143,11 @@ public function force(Player $player, $login = true) { $player->removeEffect(16); } if($login) { - $player->sendMessage(str_replace("{attempts}", $this->getAttempts($player), $this->getMessage("authentication-success"))); + $player->sendMessage(str_replace("{attempts}", $this->database->getAttempts($player->getName()), $this->getMessage("authentication-success"))); } else { - $player->sendMessage(str_replace("{pin}", $this->getPin($player), $this->getMessage("register-success"))); + $player->sendMessage(str_replace("{pin}", $this->database->getPin($player->getName()), $this->getMessage("register-success"))); } - $this->updatePlayer($player, $this->getPassword($player), $this->getPin($player), $player->getUniqueId()->toString(), 0); + $this->database->updatePlayer($player->getName(), $this->database->getPassword($player->getName()), $this->database->getPin($player->getName()), $player->getUniqueId()->toString(), 0); return true; } @@ -225,7 +164,7 @@ public function register(Player $player, $password, $confirmpassword) { $player->sendMessage($this->getMessage("password-not-match")); return false; } - $statement = $this->db->prepare("INSERT INTO players (name, password, pin, uuid, attempts) VALUES (:name, :password, :pin, :uuid, :attempts)"); + $statement = $this->database->db->prepare("INSERT INTO players (name, password, pin, uuid, attempts) VALUES (:name, :password, :pin, :uuid, :attempts)"); $statement->bindValue(":name", strtolower($player->getName()), SQLITE3_TEXT); $statement->bindValue(":password", password_hash($password, PASSWORD_BCRYPT), SQLITE3_TEXT); $statement->bindValue(":pin", $this->generatePin($player), SQLITE3_INTEGER); @@ -246,8 +185,8 @@ public function changepassword(Player $player, $oldpassword, $newpassword) { return false; } $pin = $this->generatePin($player); - $this->updatePlayer($player, password_hash($newpassword, PASSWORD_BCRYPT), $newpin, $player->getUniqueId()->toString(), 0); - $player->sendMessage($this->getMessage("password-change-success")); + $this->database->updatePlayer($player->getName(), password_hash($newpassword, PASSWORD_BCRYPT), $pin, $player->getUniqueId()->toString(), 0); + $player->sendMessage($this->getMessage("change-password-success")); return true; } @@ -265,14 +204,14 @@ public function forgotpassword(Player $player, $pin, $newpassword) { return false; } $newpin = $this->generatePin($player); - $this->updatePlayer($player, password_hash($newpassword, PASSWORD_BCRYPT), $newpin, $this->getUUID($player), $this->getPlayer($player)["attempts"]); + $this->database->updatePlayer($player->getName(), password_hash($newpassword, PASSWORD_BCRYPT), $newpin, $this->database->getUUID($player->getName()), $this->database->getAttempts($player->getName())); $player->sendMessage(str_replace("{pin}", $newpin, $this->getMessage("forgot-password-success"))); } public function resetpassword($player, $sender) { $player = strtolower($player); if($this->isRegistered($player)) { - $statement = $this->db->prepare("DELETE FROM players WHERE name = :name"); + $statement = $this->database->db->prepare("DELETE FROM players WHERE name = :name"); $statement->bindValue(":name", $player, SQLITE3_TEXT); $statement->execute(); if(isset($this->authenticated[$player])) { @@ -304,8 +243,8 @@ public function logout(Player $player, $quit = true) { } } } - - public function getMessage($message){ + + public function getMessage($message) { return str_replace("&", "§", $this->getConfig()->get($message)); } diff --git a/src/PiggyAuth/Tasks/TimeoutTask.php b/src/PiggyAuth/Tasks/TimeoutTask.php index 7f3e90e..5064e16 100644 --- a/src/PiggyAuth/Tasks/TimeoutTask.php +++ b/src/PiggyAuth/Tasks/TimeoutTask.php @@ -12,7 +12,7 @@ public function __construct($plugin, $player) { public function onRun($currentTick) { if(!$this->plugin->isAuthenticated($this->player)) { - $this->player->kick($this->plugin->getConfig()->get("timeout-message")); + $this->player->kick($this->plugin->getMessage("timeout-message")); } }