Advertisement
binemmanuel

PHP User Class

Dec 3rd, 2019
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.29 KB | None | 0 0
  1. <?php
  2. // namespace libs\classes;
  3.  
  4. class User
  5. {
  6.     /**
  7.      * @var int User's ID.
  8.      */
  9.     public $id;
  10.  
  11.     /**
  12.      * @var string Username.
  13.      */
  14.     public $username;
  15.  
  16.     /**
  17.      * @var string Password.
  18.      */
  19.     public $password;
  20.  
  21.     /**
  22.      * @var string Email.
  23.      */
  24.     public $email;
  25.  
  26.     /**
  27.      * @var string User's Gender.
  28.      */
  29.     public $gender;
  30.  
  31.     /**
  32.      * @var string The User's Role.
  33.      */
  34.     public $role;
  35.  
  36.     /**
  37.      * @var string The User's Profile pic.
  38.      */
  39.     public $profile_pic;
  40.  
  41.     /**
  42.      * @var int The User's Account Status.
  43.      */
  44.     public $acc_status;
  45.  
  46.     /**
  47.      * @var string User reset password security question.
  48.      */
  49.     public $reset_password_q;
  50.  
  51.     /**
  52.      * @var string User reset password security answer.
  53.      */
  54.     public $reset_password_a;
  55.  
  56.     /**
  57.      * @var string User reset password token.
  58.      */
  59.     public $reset_token;
  60.  
  61.     /**
  62.      * @var string User reregistration date.
  63.      */
  64.     public $reg_on;
  65.    
  66.  
  67.     function __construct(int $id = null, string $username = null, string $password = null, string $email = null, string $gender = null, string $role = null, string $profile_pic = null, int $acc_status = null, string $reset_password_q = null, string $reset_password_a = null, string $reset_token = null, string $reg_on = null)
  68.     {
  69.         if(!empty($id)){
  70.             $this->id = $id;
  71.         }
  72.         if(!empty($username)){
  73.             $this->username = $username;
  74.         }
  75.         if(!empty($password)){
  76.             $this->password = $password;
  77.         }
  78.         if(!empty($email)){
  79.             $this->email = $email;
  80.         }
  81.         if(!empty($gender)){
  82.             $this->gender = $gender;
  83.         }
  84.         if(!empty($role)){
  85.             echo $this->role = $role;
  86.         }
  87.         if(!empty($profile_pic)){
  88.             $this->profile_pic = $profile_pic;
  89.         }
  90.         if(!empty($acc_status)){
  91.             $this->acc_status = $acc_status;
  92.         }
  93.         if(!empty($reset_password_q)){
  94.             $this->reset_password_q = $reset_password_q;
  95.         }
  96.         if(!empty($reset_password_a)){
  97.             $this->reset_password_a = $reset_password_a;
  98.         }
  99.         if(!empty($reset_token)){
  100.             $this->reset_token = $reset_token;
  101.         }
  102.         if(!empty($reg_on)){
  103.             $this->reg_on = $reg_on;
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Get all users.
  109.      *
  110.      * @return
  111.      */
  112.     public function get_users(): array
  113.     {
  114.         // Include our connection file.
  115.         require 'libs/conn.php';
  116.  
  117.         // Initialize an empty array.
  118.         $data = [];
  119.  
  120.         // Prepare an sql statement.
  121.         $stmt = $conn->prepare('SELECT * FROM dgn_users ORDER BY id DESC');
  122.  
  123.         // Execute query.
  124.         if ($stmt->execute()) {
  125.             // Store result.
  126.             $stmt->store_result();
  127.  
  128.             $stmt->bind_result($id, $username, $password, $email, $gender, $role, $profile_pic, $acc_status, $reset_password_q, $reset_password_a, $activation_key, $reset_token, $reg_on);
  129.  
  130.             // Loop through user data.
  131.             while ($stmt->fetch()) {
  132.                 // Instantiate a User Object.
  133.                 $user = new User($id, $username, $password, $email, $gender, $role, $profile_pic, $acc_status, $reset_password_q, $reset_password_a, $reset_token, $activation_key, $reg_on);
  134.  
  135.                 // Create an array of object.
  136.                 array_push($data, $user);
  137.             }
  138.            
  139.         }
  140.  
  141.         // Close connection and statement.
  142.         $stmt->close();
  143.         $conn->close();
  144.  
  145.         // Return user data.
  146.         return $data;
  147.     }
  148.  
  149.     public static function get_user(int $id)
  150.     {
  151.         // Include our connection file.
  152.         require 'libs/conn.php';
  153.  
  154.         // Prepare an SQL statement.
  155.         $stmt = $conn->prepare('SELECT id, username, email, gender, user_role, profile_pic, acc_status, reg_on FROM dgn_users WHERE id = ? LIMIT 1');
  156.  
  157.         // Bind parameter.
  158.         $stmt->bind_param('i', $id);
  159.  
  160.         // Execute the query.
  161.         $stmt->execute();
  162.  
  163.         // Store result.
  164.         $stmt->bind_result($id, $username, $email, $gender, $role, $profile_pic, $acc_status, $reg_on);
  165.  
  166.         // Fetch user data.
  167.         if ($stmt->fetch()) {
  168.             // Instantiate a User Object.
  169.                 $user = new User($id, $username, $password = null, $email, $gender, $role, $profile_pic, $acc_status, null, null, null, $reg_on);
  170.  
  171.         }
  172.  
  173.         // Close connection and statement.
  174.         $stmt->close();
  175.         $conn->close();
  176.  
  177.         return $user = (!empty($user)) ? $user : '';
  178.     }
  179.  
  180.     /**
  181.      * Check if a user already exist.
  182.      *
  183.      * @param string The username.
  184.      * @return bool false or true if the user exsit.
  185.      */
  186.     public static function user_exist(string $username): bool
  187.     {
  188.         if (empty($username)) {
  189.             // Throw an error message.
  190.             throw new Exception('Error trying to verify a User Object that doesn\'t has it\'s username set', 1);
  191.            
  192.         } else {
  193.             // Clean user data.
  194.             $username = (string) clean_data($username);
  195.         }
  196.  
  197.         // Include our connection file.
  198.         require 'libs/conn.php';
  199.  
  200.         // Prepare an SQL statement.
  201.         $stmt = $conn->prepare('SELECT id, username, email FROM dgn_users WHERE username = ? LIMIT 1');
  202.  
  203.         // Bind parameter.
  204.         $stmt->bind_param('s', $username);
  205.  
  206.         // Execute the query.
  207.         $stmt->execute();
  208.  
  209.         // Store return values.
  210.         $stmt->store_result();
  211.  
  212.         // Store the number of rows.
  213.         $num_rows = $stmt->num_rows();
  214.  
  215.         // Close connection and statement.
  216.         $stmt->close();
  217.         $conn->close();
  218.  
  219.         //  Return the number of rows.
  220.         return (bool) $num_rows;
  221.     }
  222.  
  223.     public static function get_username(int $id): string
  224.     {
  225.          // Include our connection file.
  226.         require 'libs/conn.php';
  227.  
  228.         // Prepare an SQL statement.
  229.         $stmt = $conn->prepare('SELECT username FROM dgn_users WHERE id = ?');
  230.  
  231.         // Bind parameter.
  232.         $stmt->bind_param('i', $id);
  233.  
  234.         // Execute the query.
  235.         $stmt->execute();
  236.  
  237.         // Store return values.
  238.         $stmt->bind_result($username);
  239.  
  240.         // Fetch data.
  241.         if ($stmt->fetch()) {
  242.             return $username;
  243.         }
  244.  
  245.         return '';
  246.     }
  247.  
  248.     /**
  249.      * Check if a user already exist.
  250.      *
  251.      * @param string The username.
  252.      * @return bool false or true if the user exsit.
  253.      */
  254.     public function verify_user()
  255.     {
  256.         // Include our connection file.
  257.         require 'libs/conn.php';
  258.  
  259.         // Prepare an SQL statement.
  260.         $stmt = $conn->prepare('SELECT id, username, password, email, acc_status FROM dgn_users WHERE username = ?');
  261.  
  262.         // Bind parameter.
  263.         $stmt->bind_param('s', $this->username);
  264.  
  265.         // Execute the query.
  266.         $stmt->execute();
  267.  
  268.         // Store return values.
  269.         $stmt->bind_result($id, $username, $password, $email, $acc_status);
  270.  
  271.         // Check if the user exist.
  272.         $stmt->fetch();
  273.  
  274.         // Verify password and account status.
  275.         if (password_verify($this->password, $password) && !empty($acc_status)) {
  276.             // Store session.
  277.             $_SESSION['is_loggedin'] = true;
  278.             $_SESSION['id'] = $id;
  279.             $_SESSION['username'] = $username;
  280.             $_SESSION['email'] = $email;
  281.  
  282.             return true;
  283.         }
  284.  
  285.         // Close connection and statement.
  286.         $stmt->close();
  287.         $conn->close();
  288.  
  289.         return false;
  290.     }
  291.  
  292.     /**
  293.      * Check if a user account is active.
  294.      *
  295.      * @param string The username.
  296.      * @return bool false or true if the user account is active.
  297.      */
  298.     public static function is_active($username): bool
  299.     {
  300.         // Include our connection file.
  301.         require 'libs/conn.php';
  302.  
  303.         // Prepare an SQL statement.
  304.         $stmt = $conn->prepare('SELECT acc_status FROM dgn_users WHERE username = ?');
  305.  
  306.         // Bind parameter.
  307.         $stmt->bind_param('s', clean_data($username));
  308.  
  309.         // Execute the query.
  310.         $stmt->execute();
  311.  
  312.         // Store return values.
  313.         $stmt->bind_result($acc_status);
  314.  
  315.         // Fetch user data.
  316.         if ($stmt->fetch()) {
  317.             // Check if the user's account is active.
  318.             if (!empty($acc_status)) {
  319.                 return true;
  320.             }
  321.         }
  322.  
  323.         // Close connection and statement.
  324.         $stmt->close();
  325.         $conn->close();
  326.  
  327.         return false;
  328.     }
  329.  
  330.     /**
  331.      * Check if a user already exist.
  332.      *
  333.      * @param string The username.
  334.      * @return bool false or true if the user exsit.
  335.      */
  336.     public function create_account(): bool
  337.     {
  338.         // Include our connection file.
  339.         require 'libs/conn.php';
  340.  
  341.         // Generate a user token if not set.
  342.         if (empty($this->token)) {
  343.             echo $this->token = (string) rand(100000, 999999);
  344.         }
  345.  
  346.         // Prepare an SQL statement.
  347.         $stmt = $conn->prepare('INSERT INTO dgn_users(username, password, email, gender, user_role, token) VALUES(?, ?, ?, ?, ?, ?)');
  348.  
  349.         // Bind parameter.
  350.         $stmt->bind_param('sssssi', $this->username, $this->password, $this->email, $this->gender, $this->role, $this->token);
  351.  
  352.         // Execute the query.
  353.         if ($stmt->execute()) {
  354.             session_start();
  355.  
  356.             // Store session.
  357.             $_SESSION['is_loggedin'] = true;
  358.             $_SESSION['username'] = $this->username;
  359.             $_SESSION['email'] = $this->email;
  360.  
  361.             return true;
  362.         }
  363.  
  364.         // Close connection and statement.
  365.         $stmt->close();
  366.         $conn->close();
  367.  
  368.         //  Return the number of rows.
  369.         return false;
  370.     }
  371.    
  372.     /**
  373.      * Gets the number of Admins an Moderators.
  374.      *
  375.      * @return int The number of Admins an Moderators.
  376.      */
  377.     public static function super_users_count(): int
  378.     {
  379.         // Include our connection file.
  380.         require 'libs/conn.php';
  381.  
  382.         // Prepare an SQL statement.
  383.         $stmt = $conn->prepare('SELECT * FROM dgn_users WHERE user_role = "administrator" or user_role = "moderator" ');
  384.  
  385.         // Execute query.
  386.         if ($stmt->execute()) {
  387.             // Store result.
  388.             $stmt->store_result();
  389.  
  390.             $num_rows = $stmt->num_rows;
  391.         }
  392.        
  393.         // Close connection.
  394.         $conn->close();
  395.        
  396.         // Close statement.
  397.         $stmt->close();
  398.  
  399.         return $num_rows;
  400.     }
  401.  
  402.     /**
  403.      * Gets the number of Users.
  404.      *
  405.      * @return int The number of Users.
  406.      */
  407.     public static function users_count(): int
  408.     {
  409.         // Include our connection file.
  410.         require 'libs/conn.php';
  411.  
  412.         // Prepare an SQL statement.
  413.         $stmt = $conn->prepare('SELECT * FROM dgn_users');
  414.  
  415.         // Execute query.
  416.         if ($stmt->execute()) {
  417.             // Store result.
  418.             $stmt->store_result();
  419.  
  420.             $num_rows = $stmt->num_rows;
  421.         }
  422.        
  423.         // Close connection.
  424.         $conn->close();
  425.        
  426.         // Close statement.
  427.         $stmt->close();
  428.  
  429.         return $num_rows;
  430.     }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement