Guest User

Untitled

a guest
Mar 10th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. <?php
  2. //TODO Document this.
  3. class Login{
  4. /**
  5. * Active record object containing data from `user`, `staff` and `staff_perms` or
  6. * null if the person is a guest.
  7. */
  8. public $record = null;
  9. //TODO Document this.
  10. private $loginError=true;
  11.  
  12. /**
  13. * Session handler
  14. */
  15. public function __construct(){
  16. $CI =& get_instance();
  17. $cUser = $CI->input->cookie('username');
  18. $pUser = $CI->input->post('username');
  19. $username = $cUser or $pUser;
  20.  
  21. if($username){
  22. $this->benchmark->mark('Login_GetUserData_start');
  23. $CI->db->from("user");
  24. $CI->db->join('staff','staff.user_name = user.name');
  25. $CI->db->join('rank','staff.rank_id = rank.id');
  26. $CI->db->where(array('user.name'=>$username));
  27. $CI->db->limit(1);
  28. $this->record=$CI->db->get();
  29. $this->benchmark->mark('Login_GetUserData_end');
  30.  
  31. if($cUser){
  32. if(!$this->record->session_hash==$CI->input->cookie('session')){
  33. $this->loginError=true;
  34. $this->record=null;
  35. }else{
  36. $this->loginError=false;
  37. }
  38. }else{
  39. if(!$this->record->password==sha1($CI->input->post('password'))){
  40. $this->loginError=true;
  41. }else{
  42. //TODO Generate hash, update database, update cookie
  43. }
  44. }
  45. }else{
  46. $record = null;
  47. }//End if
  48.  
  49. //TODO Raise hell on bad login.
  50. }//End function
  51.  
  52.  
  53. /**
  54. * Returns true is the person is not logged in. (Hence, a guest)
  55. * @return bool
  56. */
  57. public function isGuest(){
  58. if($this->record==null)
  59. return true;
  60. else return false;
  61. }
  62.  
  63. /**
  64. * For users that are permanantly banned or timed out it returns true.
  65. * Everyone else, including guests is returns false
  66. * @return bool
  67. */
  68. public function isBanned(){
  69. if(($this->record->timeout==-1) || ($this->record->timeout > time()))
  70. return true;
  71. else return false;
  72. }
  73.  
  74. /**
  75. * Returns true for active admins. For inactive or former admins it returns false.
  76. * @return bool
  77. */
  78. public function isAdmin(){
  79. if($this->record->status=="normal")
  80. return true;
  81. else return false;
  82. }
  83.  
  84. /**
  85. * Converts time from GMT to that user's local time. For guests this
  86. * function just returns the time unmodified.
  87. * @param int $time Unix timestamp
  88. * @return int Unix timestamp
  89. */
  90. public function timeToLocal($time){
  91. if(!$this->isGuest()){
  92. $tz=$this->record->timezone_offset;
  93. $dst=(bool) $this->record->timezone_dst;
  94. $time=gmt_to_local($time,$tz,$dst);
  95. }
  96. return $time;
  97. }
  98.  
  99. /**
  100. * Convert a unix timestamp into a string. Defaults to RFC822 format
  101. * when the user has not supplied a custom format. By default this
  102. * method will convert the time given into the user's timezone before
  103. * generating the string.
  104. * @param int $time Unix timestamp
  105. * @param bool $convertToLocal Convert the timestamp into user's timezone?
  106. * @return string|false On errors returns false
  107. */
  108. public function timeToString($time,$convertToLocal=true){
  109. $format=DATE_RFC822;
  110. if($convertToLocal) $time=$this->timeToLocal($time);
  111. if(!$this->isGuest()){
  112. $format= $this->record->time_format or $format;
  113. }
  114. return @date($format,$time);
  115. }
  116.  
  117. //TODO Add accessor functions for database fields.
  118. }
  119. ?>
Add Comment
Please, Sign In to add comment