Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // namespace libs\classes;
- class User
- {
- /**
- * @var int User's ID.
- */
- public $id;
- /**
- * @var string Username.
- */
- public $username;
- /**
- * @var string Password.
- */
- public $password;
- /**
- * @var string Email.
- */
- public $email;
- /**
- * @var string User's Gender.
- */
- public $gender;
- /**
- * @var string The User's Role.
- */
- public $role;
- /**
- * @var string The User's Profile pic.
- */
- public $profile_pic;
- /**
- * @var int The User's Account Status.
- */
- public $acc_status;
- /**
- * @var string User reset password security question.
- */
- public $reset_password_q;
- /**
- * @var string User reset password security answer.
- */
- public $reset_password_a;
- /**
- * @var string User reset password token.
- */
- public $reset_token;
- /**
- * @var string User reregistration date.
- */
- public $reg_on;
- 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)
- {
- if(!empty($id)){
- $this->id = $id;
- }
- if(!empty($username)){
- $this->username = $username;
- }
- if(!empty($password)){
- $this->password = $password;
- }
- if(!empty($email)){
- $this->email = $email;
- }
- if(!empty($gender)){
- $this->gender = $gender;
- }
- if(!empty($role)){
- echo $this->role = $role;
- }
- if(!empty($profile_pic)){
- $this->profile_pic = $profile_pic;
- }
- if(!empty($acc_status)){
- $this->acc_status = $acc_status;
- }
- if(!empty($reset_password_q)){
- $this->reset_password_q = $reset_password_q;
- }
- if(!empty($reset_password_a)){
- $this->reset_password_a = $reset_password_a;
- }
- if(!empty($reset_token)){
- $this->reset_token = $reset_token;
- }
- if(!empty($reg_on)){
- $this->reg_on = $reg_on;
- }
- }
- /**
- * Get all users.
- *
- * @return
- */
- public function get_users(): array
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Initialize an empty array.
- $data = [];
- // Prepare an sql statement.
- $stmt = $conn->prepare('SELECT * FROM dgn_users ORDER BY id DESC');
- // Execute query.
- if ($stmt->execute()) {
- // Store result.
- $stmt->store_result();
- $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);
- // Loop through user data.
- while ($stmt->fetch()) {
- // Instantiate a User Object.
- $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);
- // Create an array of object.
- array_push($data, $user);
- }
- }
- // Close connection and statement.
- $stmt->close();
- $conn->close();
- // Return user data.
- return $data;
- }
- public static function get_user(int $id)
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Prepare an SQL statement.
- $stmt = $conn->prepare('SELECT id, username, email, gender, user_role, profile_pic, acc_status, reg_on FROM dgn_users WHERE id = ? LIMIT 1');
- // Bind parameter.
- $stmt->bind_param('i', $id);
- // Execute the query.
- $stmt->execute();
- // Store result.
- $stmt->bind_result($id, $username, $email, $gender, $role, $profile_pic, $acc_status, $reg_on);
- // Fetch user data.
- if ($stmt->fetch()) {
- // Instantiate a User Object.
- $user = new User($id, $username, $password = null, $email, $gender, $role, $profile_pic, $acc_status, null, null, null, $reg_on);
- }
- // Close connection and statement.
- $stmt->close();
- $conn->close();
- return $user = (!empty($user)) ? $user : '';
- }
- /**
- * Check if a user already exist.
- *
- * @param string The username.
- * @return bool false or true if the user exsit.
- */
- public static function user_exist(string $username): bool
- {
- if (empty($username)) {
- // Throw an error message.
- throw new Exception('Error trying to verify a User Object that doesn\'t has it\'s username set', 1);
- } else {
- // Clean user data.
- $username = (string) clean_data($username);
- }
- // Include our connection file.
- require 'libs/conn.php';
- // Prepare an SQL statement.
- $stmt = $conn->prepare('SELECT id, username, email FROM dgn_users WHERE username = ? LIMIT 1');
- // Bind parameter.
- $stmt->bind_param('s', $username);
- // Execute the query.
- $stmt->execute();
- // Store return values.
- $stmt->store_result();
- // Store the number of rows.
- $num_rows = $stmt->num_rows();
- // Close connection and statement.
- $stmt->close();
- $conn->close();
- // Return the number of rows.
- return (bool) $num_rows;
- }
- public static function get_username(int $id): string
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Prepare an SQL statement.
- $stmt = $conn->prepare('SELECT username FROM dgn_users WHERE id = ?');
- // Bind parameter.
- $stmt->bind_param('i', $id);
- // Execute the query.
- $stmt->execute();
- // Store return values.
- $stmt->bind_result($username);
- // Fetch data.
- if ($stmt->fetch()) {
- return $username;
- }
- return '';
- }
- /**
- * Check if a user already exist.
- *
- * @param string The username.
- * @return bool false or true if the user exsit.
- */
- public function verify_user()
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Prepare an SQL statement.
- $stmt = $conn->prepare('SELECT id, username, password, email, acc_status FROM dgn_users WHERE username = ?');
- // Bind parameter.
- $stmt->bind_param('s', $this->username);
- // Execute the query.
- $stmt->execute();
- // Store return values.
- $stmt->bind_result($id, $username, $password, $email, $acc_status);
- // Check if the user exist.
- $stmt->fetch();
- // Verify password and account status.
- if (password_verify($this->password, $password) && !empty($acc_status)) {
- // Store session.
- $_SESSION['is_loggedin'] = true;
- $_SESSION['id'] = $id;
- $_SESSION['username'] = $username;
- $_SESSION['email'] = $email;
- return true;
- }
- // Close connection and statement.
- $stmt->close();
- $conn->close();
- return false;
- }
- /**
- * Check if a user account is active.
- *
- * @param string The username.
- * @return bool false or true if the user account is active.
- */
- public static function is_active($username): bool
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Prepare an SQL statement.
- $stmt = $conn->prepare('SELECT acc_status FROM dgn_users WHERE username = ?');
- // Bind parameter.
- $stmt->bind_param('s', clean_data($username));
- // Execute the query.
- $stmt->execute();
- // Store return values.
- $stmt->bind_result($acc_status);
- // Fetch user data.
- if ($stmt->fetch()) {
- // Check if the user's account is active.
- if (!empty($acc_status)) {
- return true;
- }
- }
- // Close connection and statement.
- $stmt->close();
- $conn->close();
- return false;
- }
- /**
- * Check if a user already exist.
- *
- * @param string The username.
- * @return bool false or true if the user exsit.
- */
- public function create_account(): bool
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Generate a user token if not set.
- if (empty($this->token)) {
- echo $this->token = (string) rand(100000, 999999);
- }
- // Prepare an SQL statement.
- $stmt = $conn->prepare('INSERT INTO dgn_users(username, password, email, gender, user_role, token) VALUES(?, ?, ?, ?, ?, ?)');
- // Bind parameter.
- $stmt->bind_param('sssssi', $this->username, $this->password, $this->email, $this->gender, $this->role, $this->token);
- // Execute the query.
- if ($stmt->execute()) {
- session_start();
- // Store session.
- $_SESSION['is_loggedin'] = true;
- $_SESSION['username'] = $this->username;
- $_SESSION['email'] = $this->email;
- return true;
- }
- // Close connection and statement.
- $stmt->close();
- $conn->close();
- // Return the number of rows.
- return false;
- }
- /**
- * Gets the number of Admins an Moderators.
- *
- * @return int The number of Admins an Moderators.
- */
- public static function super_users_count(): int
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Prepare an SQL statement.
- $stmt = $conn->prepare('SELECT * FROM dgn_users WHERE user_role = "administrator" or user_role = "moderator" ');
- // Execute query.
- if ($stmt->execute()) {
- // Store result.
- $stmt->store_result();
- $num_rows = $stmt->num_rows;
- }
- // Close connection.
- $conn->close();
- // Close statement.
- $stmt->close();
- return $num_rows;
- }
- /**
- * Gets the number of Users.
- *
- * @return int The number of Users.
- */
- public static function users_count(): int
- {
- // Include our connection file.
- require 'libs/conn.php';
- // Prepare an SQL statement.
- $stmt = $conn->prepare('SELECT * FROM dgn_users');
- // Execute query.
- if ($stmt->execute()) {
- // Store result.
- $stmt->store_result();
- $num_rows = $stmt->num_rows;
- }
- // Close connection.
- $conn->close();
- // Close statement.
- $stmt->close();
- return $num_rows;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement