Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ?php
- require_once "vendor/autoload.php";
- ini_set('display_startup_errors', 1);
- ini_set('display_errors', 1);
- error_reporting(-1);
- final class Database extends PDO {
- private $config = [
- "Host" => "127.0.0.1",
- "User" => "root",
- "Pass" => "",
- "Name" => "kitsune"
- ];
- private $connection = null;
- public function __construct() {
- $connectionString = sprintf("mysql:dbname=%s;host=%s", $this->config["Name"], $this->config["Host"]);
- parent::__construct($connectionString, $this->config["User"], $this->config["Pass"],
- array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
- }
- public function addUser($username, $password, $email) {
- $hashedPassword = strtoupper(md5($password));
- $staticKey = 'e4a2dbcca10a7246817a83cd';
- $fancyPassword = $this->getLoginHash($hashedPassword, $staticKey, $username);
- $insertPenguin = "INSERT INTO `penguins` (`ID`, `Username`, `Nickname`, `Password`, `Email`, `RegistrationDate`, `Igloos`) VALUES ";
- $insertPenguin .= "(NULL, :Username, :Username, :Password, :Email, :Date, :Igloos);";
- $insertStatement = $this->prepare($insertPenguin);
- $insertStatement->bindValue(":Username", $username);
- $insertStatement->bindValue(":Password", $fancyPassword);
- $insertStatement->bindValue(":Email", $email);
- $insertStatement->bindValue(":Date", time());
- $insertStatement->bindValue(":Igloos", "1|0");
- $insertStatement->execute();
- $insertStatement->closeCursor();
- $penguinId = $this->lastInsertId();
- $this->addActiveIgloo($penguinId);
- return $penguinId;
- }
- private function addActiveIgloo($penguinId) {
- $insertStatement = $this->prepare("INSERT INTO `igloos` (`ID`, `Owner`) VALUES (NULL, :Owner);");
- $insertStatement->bindValue(":Owner", $penguinId);
- $insertStatement->execute();
- $insertStatement->closeCursor();
- $iglooId = $this->lastInsertId();
- $setActiveIgloo = $this->prepare("UPDATE `penguins` SET `Igloo` = :Igloo WHERE ID = :Penguin;");
- $setActiveIgloo->bindValue(":Igloo", $iglooId);
- $setActiveIgloo->bindValue(":Penguin", $penguinId);
- $setActiveIgloo->execute();
- $setActiveIgloo->closeCursor();
- }
- public function usernameTaken($username) {
- $usernameTaken = "SELECT Username FROM `penguins` WHERE Username = :Username";
- $takenQuery = $this->prepare($usernameTaken);
- $takenQuery->bindValue(":Username", $username);
- $takenQuery->execute();
- $rowCount = $takenQuery->rowCount();
- $takenQuery->closeCursor();
- return $rowCount > 0;
- }
- public function emailTaken($email) {
- $emailTaken = "SELECT Email FROM `penguins` WHERE Email = :Email";
- $takenQuery = $this->prepare($emailTaken);
- $takenQuery->bindValue(":Username", $email);
- $takenQuery->execute();
- $rowCount = $takenQuery->rowCount();
- $takenQuery->closeCursor();
- return $rowCount > 0;
- }
- private function encryptPassword($password, $md5 = true) {
- if($md5 !== false) {
- $password = md5($password);
- }
- $hash = substr($password, 16, 16) . substr($password, 0, 16);
- return $hash;
- }
- private function getLoginHash($password, $staticKey, $username) {
- $hash = $this->encryptPassword($password, false);
- $hash .= $staticKey;
- $hash .= 'Y(02.>\'H}t":E1';
- $hash = $this->encryptPassword($hash);
- $hash = password_hash($hash, PASSWORD_DEFAULT, [ 'cost' => 12 ]);
- return $hash;
- }
- }
- function response($data) {
- die(json_encode($data));
- }
- function attemptDataRetrieval($key) {
- if(array_key_exists($key, $_POST)) {
- return $_POST[$key];
- }
- response([
- "success" => false,
- "message" => "<strong>Uh oh!</strong> Please fill out the form completely."
- ]);
- }
- $recaptcha = new \ReCaptcha\ReCaptcha("6LdJdjIUAAAAAMUx5-cQgL2O2R8DroI79yp5jk-1");
- $resp = $recaptcha->verify(attemptDataRetrieval("captcha"), $_SERVER["REMOTE_ADDR"]);
- if(!$resp->isSuccess()) response(["success" => false, "message" => "<strong>Uh oh!</strong> Invalid captcha."]);
- $username = attemptDataRetrieval("username");
- $password = attemptDataRetrieval("password");
- $email = attemptDataRetrieval("email");
- if(strlen($username) < 4 || strlen($username) > 12) {
- $lengthWord = strlen($username) < 3 ? "short" : "long";
- response([
- "success" => false,
- "message" => "<strong>Uh oh!</strong> Username is too $lengthWord."
- ]);
- } elseif(strlen($password) < 4) {
- response([
- "success" => false,
- "message" => "<strong>Uh oh!</strong> Password is too short."
- ]);
- } elseif(strlen($email) < 4) {
- response([
- "success" => false,
- "message" => "<strong>Uh oh!</strong> Email is too short."
- ]);
- }
- $db = new Database();
- if($db->usernameTaken($username)) {
- response([
- "success" => false,
- "message" => "<strong>Uh oh!</strong> The username you've specified is already in use."
- ]);
- }
- if($db->emailTaken($email)) {
- response([
- "success" => false,
- "message" => "<strong>Uh oh!</strong> The email you've specified is already in use."
- ]);
- }
- $playerId = $db->addUser($username, $password, $email);
- response([
- "success" => true,
- "message" => "<strong>Hooray!</strong> You have successfully registered your account. Your player id is <strong>$playerId</strong>."
- ]);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement