Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $mainSiteURL = "http://bonpharma.orionit.tech";
- const DB_SERVER = "localhost";
- const DB_USER = "PUMovMk1Kjb69i7";
- const DB_PASSWORD = "SkD8tCxJPpagiQv";
- const DB = "bonpharma";
- /*
- * Database connection
- */
- function connectDb()
- {
- $db = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB);
- if ($db->connect_errno)
- echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
- else
- $db->set_charset('utf8');
- return $db;
- }
- function performLogin($db, $user, $hash)
- {
- if(!($saltQuery = $db->prepare("SELECT Salt FROM Users WHERE Username = ? LIMIT 1;")))
- $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
- if (!$saltQuery->bind_param("s", $user))
- $this->internal_error_with_msg("Binding parameters failed: (" . $saltQuery->errno . ") " . $saltQuery->error);
- $saltQuery->execute();
- $saltQuery->bind_result($salt);
- $saltQuery->store_result();
- if($saltQuery->num_rows() > 0)
- {
- $saltQuery->fetch();
- $saltQuery->close();
- $hash = strtolower($hash);
- $userHash = hash('sha512', $salt . $hash);
- if(!($userCheckQuery = $db->prepare("SELECT ID FROM Users WHERE Username = ? AND Hash = ? LIMIT 1;")))
- $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
- if (!$userCheckQuery->bind_param("ss", $user, $userHash))
- $this->internal_error_with_msg("Binding parameters failed: (" . $userCheckQuery->errno . ") " . $userCheckQuery->error);
- $userCheckQuery->execute();
- $userCheckQuery->bind_result($UID);
- $userCheckQuery->store_result();
- if($userCheckQuery->num_rows() > 0)
- {
- $userCheckQuery->fetch();
- $userCheckQuery->close();
- if(!($tokenQuery = $db->prepare("SELECT Token FROM UserTokens WHERE UserID = ? LIMIT 1;")))
- $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
- if (!$tokenQuery->bind_param("i", $UID))
- $this->internal_error_with_msg("Binding parameters failed: (" . $tokenQuery->errno . ") " . $tokenQuery->error);
- $tokenQuery->execute();
- $tokenQuery->bind_result($Token);
- $tokenQuery->store_result();
- if ($tokenQuery->num_rows > 0)
- {
- $tokenQuery->fetch();
- $tokenQuery->close();
- return $Token;
- }
- else
- {
- $tokenQuery->close();
- // Generate token for UID
- $Token = bin2hex(random_bytes(50));
- // Insert token
- if(!($insertTokenQuery = $db->prepare("INSERT INTO UserTokens (UserID, Token) VALUES (?, ?);")))
- $this->internal_error_with_msg("Prepare failed: (" . $db->errno . ") " . $db->error);
- if (!$insertTokenQuery->bind_param("is", $UID, $Token))
- $this->internal_error_with_msg("Binding parameters failed: (" . $insertTokenQuery->errno . ") " . $insertTokenQuery->error);
- $insertTokenQuery->execute();
- $insertTokenQuery->close();
- return $Token;
- }
- }
- else
- $userCheckQuery->close();
- }
- else
- $saltQuery->close();
- return "";
- }
- $db = connectDb();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement