Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <?php
  2. $mainSiteURL = "http://bonpharma.orionit.tech";
  3.  
  4. const DB_SERVER = "localhost";
  5. const DB_USER = "PUMovMk1Kjb69i7";
  6. const DB_PASSWORD = "SkD8tCxJPpagiQv";
  7. const DB = "bonpharma";
  8.  
  9. /*
  10. * Database connection
  11. */
  12. function connectDb()
  13. {
  14. $db = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB);
  15.  
  16. if ($db->connect_errno)
  17. echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
  18. else
  19. $db->set_charset('utf8');
  20.  
  21. return $db;
  22. }
  23.  
  24. function performLogin($db, $user, $hash)
  25. {
  26. if(!($saltQuery = $db->prepare("SELECT Salt FROM Users WHERE Username = ? LIMIT 1;")))
  27. $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
  28.  
  29. if (!$saltQuery->bind_param("s", $user))
  30. $this->internal_error_with_msg("Binding parameters failed: (" . $saltQuery->errno . ") " . $saltQuery->error);
  31.  
  32. $saltQuery->execute();
  33.  
  34. $saltQuery->bind_result($salt);
  35. $saltQuery->store_result();
  36.  
  37. if($saltQuery->num_rows() > 0)
  38. {
  39. $saltQuery->fetch();
  40. $saltQuery->close();
  41.  
  42. $hash = strtolower($hash);
  43. $userHash = hash('sha512', $salt . $hash);
  44.  
  45. if(!($userCheckQuery = $db->prepare("SELECT ID FROM Users WHERE Username = ? AND Hash = ? LIMIT 1;")))
  46. $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
  47.  
  48. if (!$userCheckQuery->bind_param("ss", $user, $userHash))
  49. $this->internal_error_with_msg("Binding parameters failed: (" . $userCheckQuery->errno . ") " . $userCheckQuery->error);
  50.  
  51. $userCheckQuery->execute();
  52.  
  53. $userCheckQuery->bind_result($UID);
  54. $userCheckQuery->store_result();
  55.  
  56. if($userCheckQuery->num_rows() > 0)
  57. {
  58. $userCheckQuery->fetch();
  59. $userCheckQuery->close();
  60.  
  61. if(!($tokenQuery = $db->prepare("SELECT Token FROM UserTokens WHERE UserID = ? LIMIT 1;")))
  62. $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
  63.  
  64. if (!$tokenQuery->bind_param("i", $UID))
  65. $this->internal_error_with_msg("Binding parameters failed: (" . $tokenQuery->errno . ") " . $tokenQuery->error);
  66.  
  67. $tokenQuery->execute();
  68.  
  69. $tokenQuery->bind_result($Token);
  70. $tokenQuery->store_result();
  71.  
  72. if ($tokenQuery->num_rows > 0)
  73. {
  74. $tokenQuery->fetch();
  75. $tokenQuery->close();
  76. return $Token;
  77. }
  78. else
  79. {
  80. $tokenQuery->close();
  81.  
  82. // Generate token for UID
  83. $Token = bin2hex(random_bytes(50));
  84.  
  85. // Insert token
  86. if(!($insertTokenQuery = $db->prepare("INSERT INTO UserTokens (UserID, Token) VALUES (?, ?);")))
  87. $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
  88.  
  89. if (!$insertTokenQuery->bind_param("is", $UID, $Token))
  90. $this->internal_error_with_msg("Binding parameters failed: (" . $insertTokenQuery->errno . ") " . $insertTokenQuery->error);
  91.  
  92. $insertTokenQuery->execute();
  93. $insertTokenQuery->close();
  94. return $Token;
  95. }
  96. }
  97. else
  98. $userCheckQuery->close();
  99. }
  100. else
  101. $saltQuery->close();
  102.  
  103. return "";
  104. }
  105.  
  106. $db = connectDb();
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement