SirSpamModz

php auth module

Jul 31st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.71 KB | None | 0 0
  1. <?php
  2.  
  3. class credentials
  4. {
  5.     /**
  6.     * PerformLogin
  7.     *
  8.     * Performs the default login proceedure.
  9.     *
  10.     * @param string $username The username of the person logging in.
  11.     * @param string $password The password of the person logging in.
  12.     */
  13.     public static function PerformLogin($username, $password)
  14.     {
  15.         if(!credentials::UsernameExists($username)) {
  16.             return array(
  17.                 'success' => false,
  18.                 'response' => "Username doesn't exist"
  19.             );
  20.         }
  21.  
  22.         if(sql::Instance() === false) {
  23.             return array(
  24.                 'success' => false,
  25.                 'response' => 'SQL connect failure'
  26.             );
  27.         }
  28.  
  29.         $is_ok = true;
  30.  
  31.         if($stmt = sql::Prepare("SELECT `id`, `allowed_access`, `password`, `email` FROM `credentials` WHERE `username` = ? LIMIT 1")) {
  32.             $stmt->bind_param('s', $username);
  33.             $is_ok = $stmt->execute();
  34.             $stmt->bind_result($fetch_user_id, $fetch_allowed_access, $fetch_password, $fetch_email);
  35.             $is_ok = $stmt->fetch();
  36.             $stmt->close();
  37.         }
  38.         else {
  39.             $is_ok = false;
  40.         }
  41.  
  42.         if(!$is_ok) {
  43.             return array(
  44.                 'success' => false,
  45.                 'response' => 'SQL query failed'
  46.             );
  47.         }
  48.  
  49.         if(!cryptography::ComparePasswords($password, $fetch_password)) {
  50.             return array(
  51.                 'success' => false,
  52.                 'response' => 'Invalid password'
  53.             );
  54.         }
  55.  
  56.         if($fetch_allowed_access === 'false') {
  57.             return array(
  58.                 'success' => false,
  59.                 'response' => 'No account permissions'
  60.             );
  61.         }
  62.  
  63.         $session_expiry = strtotime('1month');
  64.         $session_token = cryptography::RandomString(100) . hash('md5', $username . (string)$session_expiry);
  65.  
  66.  
  67.         if($stmt = sql::Prepare("INSERT INTO `sessions` (`user_id`, `token`, `expiry`) VALUES (?, ?, ?)")) {
  68.             $stmt->bind_param('isi', $fetch_user_id, $session_token, $session_expiry);
  69.             $is_ok = $stmt->execute();
  70.             $stmt->close();
  71.         }
  72.         else {
  73.             $is_ok = false;
  74.         }
  75.  
  76.         if($is_ok) {
  77.             return array(
  78.                 'success' => true,
  79.                 'response' => 'Login successful',
  80.                 'session_token' => $session_token,
  81.             );
  82.         }
  83.         else {
  84.             return array(
  85.                 'success' => false,
  86.                 'response' => 'SQL query failed',
  87.             );
  88.         }
  89.     }
  90.  
  91.     /**
  92.     * PerformRegistration
  93.     *
  94.     * Performs the default registration proceedure.
  95.     *
  96.     * @param string $username The username of the person registering.
  97.     * @param string $password The password of the person registering.
  98.     * @param string $email The password of the person registering.
  99.     */
  100.     public static function PerformRegistration($username, $password, $email)
  101.     {
  102.         if(strlen($username) < 6) {
  103.             return array(
  104.                 'success' => false,
  105.                 'response' => 'Username must be atleast 6 characters'
  106.             );
  107.         }
  108.  
  109.         if(strlen($username) < 6) {
  110.             return array(
  111.                 'success' => false,
  112.                 'response' => 'Password must be atleast 6 characters'
  113.             );
  114.         }
  115.  
  116.         if(credentials::UsernameExists($username)) {
  117.             return array(
  118.                 'success' => false,
  119.                 'response' => 'Username is taken'
  120.             );
  121.         }
  122.  
  123.         if(credentials::EmailExists($email)) {
  124.             return array(
  125.                 'success' => false,
  126.                 'response' => 'Email already in use'
  127.             );
  128.         }
  129.  
  130.         $password = cryptography::HashPassword($password);
  131.  
  132.         if(sql::Instance() === false) {
  133.             return array(
  134.                 'success' => false,
  135.                 'response' => 'SQL connect failure'
  136.             );
  137.         }
  138.  
  139.         if($stmt = sql::Prepare("INSERT INTO `credentials` (`id`, `allowed_access`, `username`, `password`, `email`) VALUES (NULL, 'false', ?, ?, ?)")) {
  140.             $stmt->bind_param('sss', $username, $password, $email);
  141.             $stmt->execute();
  142.             $stmt->close();
  143.         }
  144.         else {
  145.             return array(
  146.                 'success' => false,
  147.                 'response' => 'SQL prepare failure'
  148.             );
  149.         }
  150.  
  151.  
  152.         return array(
  153.             'success' => false,
  154.             'response' => 'Registration successful'
  155.         );
  156.     }
  157.  
  158.     /**
  159.     * Checks if a username is on the table
  160.     *
  161.     * @param string $username The username you're looking up.
  162.     */
  163.     public static function UsernameExists($username)
  164.     {
  165.         if(sql::Instance() === false) {
  166.             return false;
  167.         }
  168.  
  169.         if($stmt = sql::Prepare('SELECT COUNT(`id`) FROM `credentials` WHERE `username` = ?')) {
  170.             $stmt->bind_param('s', $username);
  171.             $stmt->execute();
  172.             $stmt->bind_result($fetch_count);
  173.             $stmt->fetch();
  174.             $stmt->close();
  175.         }
  176.         else {
  177.             return false;
  178.         }
  179.  
  180.         return (isset($fetch_count) ? ($fetch_count > 0) : false);
  181.     }
  182.  
  183.     /**
  184.     * Checks if a email is on the table
  185.     *
  186.     * @param string $email The email you're looking up.
  187.     */
  188.     public static function EmailExists($email)
  189.     {
  190.         if(sql::Instance() === false) {
  191.             return false;
  192.         }
  193.  
  194.         if($stmt = sql::Prepare('SELECT COUNT(`id`) FROM `credentials` WHERE `email` = ?')) {
  195.             $stmt->bind_param('s', $email);
  196.             $stmt->execute();
  197.             $stmt->bind_result($fetch_count);
  198.             $stmt->fetch();
  199.             $stmt->close();
  200.         }
  201.         else {
  202.             return false;
  203.         }
  204.  
  205.         return (isset($fetch_count) ? ($fetch_count > 0) : false);
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment