Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class credentials
- {
- /**
- * PerformLogin
- *
- * Performs the default login proceedure.
- *
- * @param string $username The username of the person logging in.
- * @param string $password The password of the person logging in.
- */
- public static function PerformLogin($username, $password)
- {
- if(!credentials::UsernameExists($username)) {
- return array(
- 'success' => false,
- 'response' => "Username doesn't exist"
- );
- }
- if(sql::Instance() === false) {
- return array(
- 'success' => false,
- 'response' => 'SQL connect failure'
- );
- }
- $is_ok = true;
- if($stmt = sql::Prepare("SELECT `id`, `allowed_access`, `password`, `email` FROM `credentials` WHERE `username` = ? LIMIT 1")) {
- $stmt->bind_param('s', $username);
- $is_ok = $stmt->execute();
- $stmt->bind_result($fetch_user_id, $fetch_allowed_access, $fetch_password, $fetch_email);
- $is_ok = $stmt->fetch();
- $stmt->close();
- }
- else {
- $is_ok = false;
- }
- if(!$is_ok) {
- return array(
- 'success' => false,
- 'response' => 'SQL query failed'
- );
- }
- if(!cryptography::ComparePasswords($password, $fetch_password)) {
- return array(
- 'success' => false,
- 'response' => 'Invalid password'
- );
- }
- if($fetch_allowed_access === 'false') {
- return array(
- 'success' => false,
- 'response' => 'No account permissions'
- );
- }
- $session_expiry = strtotime('1month');
- $session_token = cryptography::RandomString(100) . hash('md5', $username . (string)$session_expiry);
- if($stmt = sql::Prepare("INSERT INTO `sessions` (`user_id`, `token`, `expiry`) VALUES (?, ?, ?)")) {
- $stmt->bind_param('isi', $fetch_user_id, $session_token, $session_expiry);
- $is_ok = $stmt->execute();
- $stmt->close();
- }
- else {
- $is_ok = false;
- }
- if($is_ok) {
- return array(
- 'success' => true,
- 'response' => 'Login successful',
- 'session_token' => $session_token,
- );
- }
- else {
- return array(
- 'success' => false,
- 'response' => 'SQL query failed',
- );
- }
- }
- /**
- * PerformRegistration
- *
- * Performs the default registration proceedure.
- *
- * @param string $username The username of the person registering.
- * @param string $password The password of the person registering.
- * @param string $email The password of the person registering.
- */
- public static function PerformRegistration($username, $password, $email)
- {
- if(strlen($username) < 6) {
- return array(
- 'success' => false,
- 'response' => 'Username must be atleast 6 characters'
- );
- }
- if(strlen($username) < 6) {
- return array(
- 'success' => false,
- 'response' => 'Password must be atleast 6 characters'
- );
- }
- if(credentials::UsernameExists($username)) {
- return array(
- 'success' => false,
- 'response' => 'Username is taken'
- );
- }
- if(credentials::EmailExists($email)) {
- return array(
- 'success' => false,
- 'response' => 'Email already in use'
- );
- }
- $password = cryptography::HashPassword($password);
- if(sql::Instance() === false) {
- return array(
- 'success' => false,
- 'response' => 'SQL connect failure'
- );
- }
- if($stmt = sql::Prepare("INSERT INTO `credentials` (`id`, `allowed_access`, `username`, `password`, `email`) VALUES (NULL, 'false', ?, ?, ?)")) {
- $stmt->bind_param('sss', $username, $password, $email);
- $stmt->execute();
- $stmt->close();
- }
- else {
- return array(
- 'success' => false,
- 'response' => 'SQL prepare failure'
- );
- }
- return array(
- 'success' => false,
- 'response' => 'Registration successful'
- );
- }
- /**
- * Checks if a username is on the table
- *
- * @param string $username The username you're looking up.
- */
- public static function UsernameExists($username)
- {
- if(sql::Instance() === false) {
- return false;
- }
- if($stmt = sql::Prepare('SELECT COUNT(`id`) FROM `credentials` WHERE `username` = ?')) {
- $stmt->bind_param('s', $username);
- $stmt->execute();
- $stmt->bind_result($fetch_count);
- $stmt->fetch();
- $stmt->close();
- }
- else {
- return false;
- }
- return (isset($fetch_count) ? ($fetch_count > 0) : false);
- }
- /**
- * Checks if a email is on the table
- *
- * @param string $email The email you're looking up.
- */
- public static function EmailExists($email)
- {
- if(sql::Instance() === false) {
- return false;
- }
- if($stmt = sql::Prepare('SELECT COUNT(`id`) FROM `credentials` WHERE `email` = ?')) {
- $stmt->bind_param('s', $email);
- $stmt->execute();
- $stmt->bind_result($fetch_count);
- $stmt->fetch();
- $stmt->close();
- }
- else {
- return false;
- }
- return (isset($fetch_count) ? ($fetch_count > 0) : false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment