Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //TODO Document this.
- class Login{
- /**
- * Active record object containing data from `user`, `staff` and `staff_perms` or
- * null if the person is a guest.
- */
- public $record = null;
- //TODO Document this.
- private $loginError=true;
- /**
- * Session handler
- */
- public function __construct(){
- $CI =& get_instance();
- $cUser = $CI->input->cookie('username');
- $pUser = $CI->input->post('username');
- $username = $cUser or $pUser;
- if($username){
- $this->benchmark->mark('Login_GetUserData_start');
- $CI->db->from("user");
- $CI->db->join('staff','staff.user_name = user.name');
- $CI->db->join('rank','staff.rank_id = rank.id');
- $CI->db->where(array('user.name'=>$username));
- $CI->db->limit(1);
- $this->record=$CI->db->get();
- $this->benchmark->mark('Login_GetUserData_end');
- if($cUser){
- if(!$this->record->session_hash==$CI->input->cookie('session')){
- $this->loginError=true;
- $this->record=null;
- }else{
- $this->loginError=false;
- }
- }else{
- if(!$this->record->password==sha1($CI->input->post('password'))){
- $this->loginError=true;
- }else{
- //TODO Generate hash, update database, update cookie
- }
- }
- }else{
- $record = null;
- }//End if
- //TODO Raise hell on bad login.
- }//End function
- /**
- * Returns true is the person is not logged in. (Hence, a guest)
- * @return bool
- */
- public function isGuest(){
- if($this->record==null)
- return true;
- else return false;
- }
- /**
- * For users that are permanantly banned or timed out it returns true.
- * Everyone else, including guests is returns false
- * @return bool
- */
- public function isBanned(){
- if(($this->record->timeout==-1) || ($this->record->timeout > time()))
- return true;
- else return false;
- }
- /**
- * Returns true for active admins. For inactive or former admins it returns false.
- * @return bool
- */
- public function isAdmin(){
- if($this->record->status=="normal")
- return true;
- else return false;
- }
- /**
- * Converts time from GMT to that user's local time. For guests this
- * function just returns the time unmodified.
- * @param int $time Unix timestamp
- * @return int Unix timestamp
- */
- public function timeToLocal($time){
- if(!$this->isGuest()){
- $tz=$this->record->timezone_offset;
- $dst=(bool) $this->record->timezone_dst;
- $time=gmt_to_local($time,$tz,$dst);
- }
- return $time;
- }
- /**
- * Convert a unix timestamp into a string. Defaults to RFC822 format
- * when the user has not supplied a custom format. By default this
- * method will convert the time given into the user's timezone before
- * generating the string.
- * @param int $time Unix timestamp
- * @param bool $convertToLocal Convert the timestamp into user's timezone?
- * @return string|false On errors returns false
- */
- public function timeToString($time,$convertToLocal=true){
- $format=DATE_RFC822;
- if($convertToLocal) $time=$this->timeToLocal($time);
- if(!$this->isGuest()){
- $format= $this->record->time_format or $format;
- }
- return @date($format,$time);
- }
- //TODO Add accessor functions for database fields.
- }
- ?>
Add Comment
Please, Sign In to add comment