Advertisement
Guest User

test

a guest
Jul 2nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "vendor/autoload.php";
  4.  
  5. final class Database extends PDO {
  6.  
  7. private $config = [
  8. "Host" => "127.0.0.1",
  9. "User" => "root",
  10. "Pass" => "",
  11. "Name" => "kitsune"
  12. ];
  13.  
  14. private $connection = null;
  15.  
  16. public function __construct() {
  17. $connectionString = sprintf("mysql:dbname=%s;host=%s", $this->config["Name"], $this->config["Host"]);
  18.  
  19. parent::__construct($connectionString, $this->config["User"], $this->config["Pass"]);
  20. }
  21.  
  22. public function addUser($username, $password, $color, $email = "none@kodomo.love") {
  23. $swid = $this->generateUniqueId();
  24.  
  25. $hashedPassword = strtoupper(md5($password));
  26.  
  27. $insertPenguin = 'INSERT INTO `penguins` (`ID`, `Username`, `Nickname`, `Password`, `SWID`, `Email`,
  28.  
  29. `RegistrationDate`, ';
  30. $insertPenguin .= '`Inventory`, `Color`, `Igloos`, `Floors`, `Locations`, `LoginKey`, `ConfirmationHash`,
  31.  
  32. `CareInventory`, ';
  33. $insertPenguin .= '`Igloo`, `Furniture`, `Stamps`) VALUES ';
  34. $insertPenguin .= '(NULL, :Username, :Username, :Password, :Swid, :Email, :Date, :Color, :Color, :Igloos,
  35.  
  36. :Floors, :Locations, ';
  37. $insertPenguin .= '"", "", "", 1, "", "");';
  38.  
  39. $insertStatement = $this->prepare($insertPenguin);
  40. $insertStatement->bindValue(":Username", $username);
  41. $insertStatement->bindValue(":Password", $hashedPassword);
  42. $insertStatement->bindValue(":Swid", $swid);
  43. $insertStatement->bindValue(":Email", $email);
  44. $insertStatement->bindValue(":Date", time());
  45. $insertStatement->bindValue(":Color", $color);
  46. $insertStatement->bindValue(":Igloos", "1|0");
  47. $insertStatement->bindValue(":Floors", "0|0");
  48. $insertStatement->bindValue(":Locations", "1|0");
  49.  
  50. $insertStatement->execute();
  51. $insertStatement->closeCursor();
  52.  
  53. $insertStatement = $this->prepare("INSERT INTO `igloos` (`ID`, `Owner`, `Furniture`, `Likes`) VALUES (NULL,
  54.  
  55. :Owner, '', '[]');");
  56.  
  57. $penguinId = $this->lastInsertId();
  58.  
  59. $this->addActiveIgloo($penguinId);
  60.  
  61. return $penguinId;
  62. }
  63.  
  64. private function addActiveIgloo($penguinId) {
  65. $insertStatement = $this->prepare("INSERT INTO `igloos` (`ID`, `Owner`, `Likes`) VALUES (NULL, :Owner,
  66.  
  67. '[]');");
  68. $insertStatement->bindValue(":Owner", $penguinId);
  69. $insertStatement->execute();
  70. $insertStatement->closeCursor();
  71.  
  72. $iglooId = $this->lastInsertId();
  73.  
  74. $setActiveIgloo = $this->prepare("UPDATE `penguins` SET `Igloo` = :Igloo WHERE ID = :Penguin;");
  75. $setActiveIgloo->bindValue(":Igloo", $iglooId);
  76. $setActiveIgloo->bindValue(":Penguin", $penguinId);
  77. $setActiveIgloo->execute();
  78. $setActiveIgloo->closeCursor();
  79. }
  80.  
  81. public function usernameTaken($username) {
  82. $usernameTaken = "SELECT Username FROM `penguins` WHERE Username = :Username";
  83.  
  84. $takenQuery = $this->prepare($usernameTaken);
  85. $takenQuery->bindValue(":Username", $username);
  86. $takenQuery->execute();
  87.  
  88. $rowCount = $takenQuery->rowCount();
  89. $takenQuery->closeCursor();
  90.  
  91. return $rowCount > 0;
  92. }
  93.  
  94. private function generateUniqueId() {
  95. mt_srand((double)microtime() * 10000);
  96.  
  97. $charid = md5(uniqid(rand(), true));
  98. $hyphen = chr(45);
  99. $uuid = chr(123)
  100. . substr($charid, 0, 8) . $hyphen
  101. . substr($charid, 8, 4) . $hyphen
  102. . substr($charid, 12, 4) . $hyphen
  103. . substr($charid, 16, 4) . $hyphen
  104. . substr($charid, 20, 12)
  105. . chr(125);
  106.  
  107. return $uuid;
  108. }
  109.  
  110. }
  111.  
  112. function response($data) {
  113. die(json_encode($data));
  114. }
  115.  
  116. function attemptDataRetrieval($key) {
  117. if(array_key_exists($key, $_POST)) {
  118. return $_POST[$key];
  119. }
  120.  
  121. response([
  122. "success" => false,
  123. "message" => "<strong>Uh oh!</strong> Please fill out the form completely."
  124. ]);
  125. }
  126.  
  127. $recaptcha = new \ReCaptcha\ReCaptcha("6LeaqycUAAAAAD0a0NSBLsDs9o2pTsSwcLYpFGRn");
  128. $resp = $recaptcha->verify(attemptDataRetrieval("captcha"), $_SERVER["REMOTE_ADDR"]);
  129. if(!$resp->isSuccess()) response(["success" => false, "message" => "<strong>Uh oh!</strong> Invalid captcha."]);
  130.  
  131. $username = attemptDataRetrieval("username");
  132. $password = attemptDataRetrieval("password");
  133. $email = attemptDataRetrieval("email");
  134. $colors = range(1, 17);
  135.  
  136. if(strlen($username) < 4 || strlen($username) > 12) {
  137. $lengthWord = strlen($username) < 3 ? "short" : "long";
  138. response([
  139. "success" => false,
  140. "message" => "<strong>Uh oh!</strong> Username is too $lengthWord."
  141. ]);
  142. } elseif(strlen($password) < 4) {
  143. response([
  144. "success" => false,
  145. "message" => "<strong>Uh oh!</strong> Password is too short."
  146. ]);
  147. }
  148.  
  149. $db = new Database();
  150.  
  151. if($db->usernameTaken($username)) {
  152. response([
  153. "success" => false,
  154. "message" => "<strong>Uh oh!</strong> The username you've specified is already in use."
  155. ]);
  156. }
  157.  
  158. $playerId = $db->addUser($username, $password, 1, $email);
  159.  
  160. response([
  161. "success" => true,
  162. "message" => "<strong>Hooray!</strong> You have successfully registered your account on Arctic CP. Your player id is <strong>
  163.  
  164. $playerId</strong>."
  165. ]);
  166.  
  167. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement