Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User {
- ##############################################
- /* Program Cache */
- private $error; // stores the program error, this is the next to top level error. We can grab this through get_error();
- ##############################################
- /**
- * is_logged_in function.
- * Determines if a user is logged in or not.
- * @access public
- * @return void
- */
- public function is_logged_in() {
- if(isset($_SESSION['user_id'])) {
- $user_id = $_SESSION['user_id'];
- $check = $this->database->num_rows_fq("SELECT * FROM `users` WHERE `id`='$user_id' AND `rank` <> 'banned'");
- if($check == 0) {
- return false;
- } else {
- return true;
- }
- } else {
- return false;
- }
- }
- public function not_logged_in($page) {
- $_SESSION['return_page'] = $page;
- $this->core->redirect('login.php');
- }
- ######################
- # Register Functions #
- ######################
- public function register($username, $password, $confirm_password, $email, $rank = 'user', $activated = '0') {
- if(empty($username) || empty($password) || empty($confirm_password) || empty($email)) {
- $this->error = "All fields are required.";
- return false;
- }
- if($password != $confirm_password) {
- $this->error = "Your passwords do not match up.";
- return false;
- }
- if(!$this->valid_email($email)) {
- $this->error = "That is not a correct e-mail address.";
- return false;
- }
- if(!$this->check_username($username)) {
- $this->error = "That username is already in use.";
- return false;
- }
- if(!$this->check_email($email)) {
- $this->error = "That e-mail address is already in use.";
- return false;
- }
- $time = time();
- $ip = $_SERVER["REMOTE_ADDR"];
- $password_enc = md5($password);
- $this->database->query("INSERT INTO `users` (username, password, email, registered, ip, activated, unencrypted, rank) VALUES('$username', '$password_enc', '$email', '$time', '$ip', '$activated', '$password', '$rank')");
- return true;
- }
- /**
- * valid_email function.
- * Checks to see if an e-mail is the right format.
- * @access public
- * @param mixed $email
- * @return void
- */
- public function valid_email($email) {
- if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * check_username function.
- * Checks to see if a username is in use.
- * @access public
- * @param mixed $username
- * @return void
- */
- public function check_username($username) {
- $check = $this->database->num_rows_fq("SELECT * FROM `users` WHERE `username`='$username'");
- if($check == 0) {
- return true; // not in use
- } else {
- return false; // in use
- }
- }
- /**
- * check_email function.
- * Checks to see if an email is in use.
- * @access public
- * @param mixed $email
- * @return void
- */
- public function check_email($email) {
- $check = $this->database->num_rows_fq("SELECT * FROM `users` WHERE `email`='$email'");
- if($check == 0) {
- return true; // not in use
- } else {
- return false; // in use
- }
- }
- ###################
- # Login Functions #
- ###################
- /**
- * login function.
- * Performs a login on a user.
- * @access public
- * @param mixed $username
- * @param mixed $password
- * @return void
- */
- public function login($username, $password) {
- if(empty($username) || empty($password)) {
- $this->error = "You have left a field blank.";
- return false;
- }
- if(!$this->check_username_login($username)) {
- $this->error = "You have entered an invalid username.";
- return false;
- }
- if(!$this->check_password_login($username, $password)) {
- $this->error = "You have entered an invalid password.";
- return false;
- }
- if($this->core->settings->get_setting('admin_only_login') == 1) {
- $ranks = $this->core->settings->get_setting('login_groups');
- $ranks = explode(',', $ranks);
- $accepted = false;
- foreach($ranks as $rank) {
- if($this->is_rank($rank, $this->username_to_id($username))) {
- $accepted = true;
- break;
- }
- }
- if($accepted == false) {
- $this->error = "Only certain users can login at this current time.";
- return false;
- }
- }
- $this->set_sessions($username);
- /* Do event. */
- $time = time();
- $message = "{$username} just logged in!";
- $this->database->query("INSERT INTO `events` (type, time, message) VALUES('login', '$time', '$message')");
- $return_page = $_SESSION['return_page'];
- if($return_page != "") {
- $_SESSION['return_page'] = "";
- $this->core->redirect($return_page);
- }
- return true;
- }
- /**
- * set_sessions function.
- * Sets the sessions for a user.
- * @access private
- * @param mixed $username
- * @return void
- */
- private function set_sessions($username) {
- $_SESSION['user_id'] = $this->username_to_id($username);
- }
- /**
- * check_username_login function.
- * Check whether or not a username exists.
- * @access public
- * @param mixed $username
- * @return void
- */
- public function check_username_login($username) {
- $check = $this->database->num_rows_fq("SELECT * FROM `users` WHERE `username`='$username'");
- if($check != 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * check_password_login function.
- * Checks whether a password is right for a username.
- * @access public
- * @param mixed $username
- * @param mixed $password
- * @return void
- */
- public function check_password_login($username, $password) {
- $password = md5($password);
- $check = $this->database->num_rows_fq("SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password' AND `rank` <> 'banned'");
- if($check != 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * logout function.
- * Perform a logout & event on a user.
- * @access public
- * @return void
- */
- public function logout() {
- $time = time();
- $username = $this->get_detail("username");
- $message = "{$username} just logged out!";
- $this->database->query("INSERT INTO `events` (type, time, message) VALUES('logout', '$time', '$message')");
- unset($_SESSION['user_id']);
- $this->core->redirect('index.php');
- }
- ##################
- # User Functions #
- ##################
- /**
- * username_to_id function.
- * Converts a username to a database user ID.
- * @access public
- * @param mixed $username
- * @return void
- */
- public function username_to_id($username) {
- $this->database->query("SELECT `id` FROM `users` WHERE `username`='$username'");
- $detail = $this->database->fetch_object();
- return $detail->id;
- }
- /**
- * get_detail function.
- * Gets a specific detail about a user.
- * @access public
- * @param mixed $field
- * @param string $user_id. (default: '')
- * @return void
- */
- public function get_detail($field, $user_id = '') {
- $user_id = ($user_id == '') ? $_SESSION['user_id'] : $user_id;
- $this->database->query("SELECT `$field` FROM `users` WHERE `id`='$user_id'");
- $detail = $this->database->fetch_object();
- return $detail->$field;
- }
- /**
- * is_rank function.
- * Determines if a user is a specific rank or not.
- * @access public
- * @param mixed $rank
- * @param string $user_id. (default: '')
- * @return void
- */
- public function is_rank($rank, $user_id = '') {
- $user_id = ($user_id == '') ? $_SESSION['user_id'] : $user_id;
- $user_rank = $this->get_detail('rank', $user_id);
- if($user_rank == $rank) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * check_id function.
- * Checks a specific user ID in the database, existance.
- * @access public
- * @param mixed $user_id
- * @return void
- */
- public function check_id($user_id) {
- $check = $this->database->num_rows_fq("SELECT * FROM `users` WHERE `id`='$user_id'");
- if($check == 0) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * delete_user function.
- * Deletes a user.
- * @access public
- * @param mixed $user_id
- * @return void
- */
- public function delete_user($user_id) {
- $this->database->query("DELETE FROM `users` WHERE `id`='$user_id'");
- }
- /**
- * update_field function.
- * Updates a field for a user.
- * @access public
- * @param mixed $field
- * @param mixed $value
- * @param string $user_id. (default: '')
- * @return void
- */
- public function update_field($field, $value, $user_id = '') {
- $user_id = ($user_id == '') ? $_SESSION['user_id'] : $user_id;
- $this->database->query("UPDATE `users` SET `$field`='$value' WHERE `id`='$user_id'");
- }
- #####################
- # Program Functions #
- #####################
- /**
- * get_error function.
- * Gets the program error for this class and returns it.
- * @access public
- * @return void
- */
- public function get_error() {
- return $this->error;
- }
- }
Add Comment
Please, Sign In to add comment