Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. <?php
  2. header("Content-type:application/json;charset=utf-8");
  3. header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  4. header("Cache-Control: post-check=0, pre-check=0", false);
  5. header("Pragma: no-cache");
  6. $action = strtolower($_GET["action"]);
  7. if ($action === "register") {
  8. $username = strtolower($_GET["username"]);
  9. $password = $_GET["password"];
  10. if (empty($username)) {
  11. $error[] = "EMPTY_USERNAME";
  12. }
  13. if (empty($password)) {
  14. $error[] = "EMPTY_PASSWORD";
  15. }
  16. if (strlen($username) > 15) {
  17. $error[] = "USERNAME_TOOLONG";
  18. }
  19. if (!ctype_alnum($username)) {
  20. $error[] = "INVALID_USERNAME";
  21. }
  22. if (file_exists("accounts/$username.json")) {
  23. $error[] = "USERNAME_TAKEN";
  24. }
  25. if (isset($error)) {
  26. $array = array(
  27. "error" => $error
  28. );
  29. } else {
  30. $passwordHash = password_hash($password, PASSWORD_DEFAULT);
  31. $registerArray = array(
  32. "passwordHash" => $passwordHash
  33. );
  34. if (!file_exists("accounts")) {
  35. mkdir("accounts", 0777, true);
  36. }
  37. $memberFile = fopen("accounts/$username.json", "w");
  38. $data = json_encode($registerArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  39. fwrite($memberFile, $data);
  40. fclose($memberFile);
  41. $array = array(
  42. "error" => false
  43. );
  44. }
  45. } elseif ($action === "login") {
  46. $username = strtolower($_GET["username"]);
  47. $password = $_GET["password"];
  48. if (file_exists("accounts/$username.json")) {
  49. $memberFile = file_get_contents("accounts/$username.json");
  50. $json = json_decode($memberFile, TRUE);
  51. $passwordHash = $json["passwordHash"];
  52. }
  53. if (empty($username)) {
  54. $error[] = "EMPTY_USERNAME";
  55. }
  56. if (empty($password)) {
  57. $error[] = "EMPTY_PASSWORD";
  58. }
  59. if (!file_exists("accounts/$username.json")) {
  60. $error[] = "NONEXISTANT_USERNAME";
  61. }
  62. if (!password_verify($password, $passwordHash)) {
  63. $error[] = "INVALID_LOGIN";
  64. }
  65. if (isset($error)) {
  66. $array = array(
  67. "error" => $error
  68. );
  69. } else {
  70. $array = array(
  71. "error" => false
  72. );
  73. }
  74. } else {
  75. $array = array(
  76. "error" => "INVALID_ACTION"
  77. );
  78. }
  79. echo json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement