Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.55 KB | None | 0 0
  1. <?php
  2.  
  3. // Admin Class
  4.  
  5. class Admin {
  6.    
  7.     private $id;
  8.     private $forename;
  9.     private $surname;
  10.     private $avatar;
  11.     private $email;
  12.     private $password;
  13.     private $password_salt;
  14.     private $session_id;
  15.     private $created;
  16.     private $modified;
  17.     private $last_login;
  18.     private $active;
  19.    
  20.     private $core;
  21.    
  22.     function __construct($core, $id = false)
  23.     {
  24.        
  25.     $this->core = $core;
  26.        
  27.         if( $id !== false)
  28.         {
  29.             $sql = $this->core->db->prepare (' SELECT * FROM tbl_admins WHERE user_id = :id LIMIT 1');
  30.            
  31.             if( $sql->execute(array(':id' => $id)) )
  32.             {
  33.                 if( $sql->rowCount() < 1 )
  34.                 {
  35.                     throw new Exception( 'Specified Admin ID does not exist.' );
  36.                 }
  37.                 else
  38.                 {
  39.                     $admin = $sql->fetchObject();
  40.                    
  41.                     $this->id            = $admin->admin_id;
  42.                     $this->forename      = $admin->admin_forname;
  43.                     $this->surname       = $admin->admin_surname;
  44.                     $this->avatar        = $admin->admin_avatar;
  45.                     $this->email         = $admin->admin_email;
  46.                     $this->password      = $admin->admin_password;
  47.                     $this->password_salt = $admin->admin_password_salt;
  48.                     $this->session_id    = $admin->admin_session_id;
  49.                     $this->created       = strtotime($admin->admin_created);
  50.                     $this->modified      = strtotime($admin->admin_modified);
  51.                     $this->last_login    = strtotime($admin->admin_last_login);
  52.                     $this->active        = $admin->admin_active;
  53.                  }
  54.             }
  55.             else
  56.             {
  57.                 throw new Exception( 'There was a problem running the SQL query.' );
  58.             }
  59.         }
  60.     }
  61.    
  62.     // Magic Getter
  63.     function get_id() { return $this->id; }
  64.    
  65.     // Magic Setter
  66.     function __set( $property, $value )
  67.     {
  68.         if (property_exists($this, $property) )
  69.         {
  70.             $this->$property = $value;
  71.             $this->updateProp ($this, $value);
  72.             return $this;
  73.            
  74.         }
  75.     }
  76.    
  77.     // Function to fetch the currently logged in admin user:
  78.     public function current()
  79.     {
  80.         // If the cookie is set and it's not empty, set the session_id() using it:
  81.         if( !empty($_COOKIE[$this->core->settings->cookie_login_name]) )
  82.         {
  83.             session_id( $_COOKIE[$this->core->settings->cookie_login_name] );
  84.         }
  85.        
  86.         // Now look them up in the login DB:
  87.         $sql = $this->core->db->prepare( 'SELECT admin_id AS id FROM tbl_admins WHERE session_id = :sessid LIMIT 1' );
  88.         $sql->execute(array(':sessid' => session_id()));
  89.        
  90.         // If the count is greater than one:
  91.         if( $sql->rowCount() > 0 )
  92.         {
  93.             $admin = $sql->fetchObject();
  94.            
  95.             // Now return a new user object:
  96.             return new self( $this->core, $admin->id );
  97.         }
  98.        
  99.         // No logged in user found, return false:
  100.         return false;
  101.     }
  102.    
  103.     // Function to log a admin user in:
  104.     public function login( $email, $password, $remember = false )
  105.     {
  106.         // First check if the admin exists:
  107.         $sql = $this->core->db->prepare( 'SELECT admin_id AS id, admin_password AS password, admin_password_salt AS password_salt FROM tbl_admins WHERE admin_email = :email LIMIT 1' );
  108.        
  109.         if( $sql->execute(array(':email' => $email)) )
  110.         {
  111.             if( $sql->rowCount() < 1 )
  112.             {
  113.                 throw new Exception( 'Specified email could not be found.' );
  114.             }
  115.             else
  116.             {
  117.                 $adminObj = $sql->fetchObject();
  118.                 $adminUser    = new self( $this->core, $adminObj->id );
  119.                
  120.                 // Check the password:
  121.                 if( !$adminUser->checkPassword($password) )
  122.                 {
  123.                     throw new Exception( 'The specified password is incorrect.' );
  124.                 }
  125.                
  126.                 // Their password is OK, so we need to log them in:
  127.                 else
  128.                 {
  129.                     $ins = $this->core->db->prepare( 'INSERT INTO tbl_admins_log (log_id, log_admin_id, log_date, log_description)
  130.                                                                            VALUES (:lid, :adminid, :logdate, :desc)' );
  131.                    
  132.                    
  133.                     if( $ins->execute(array(':adminid' => $adminUser->id, ':lid' => session_id(), ':logdate' => date('Y-m-d H:i:s'))) )
  134.                     {
  135.                         // Now we need to set the cookie (if they wish to be remembered):
  136.                         if( $remember )
  137.                         {
  138.                             setcookie( $this->core->settings->cookie_login_name, session_id(), (time() + 7776000), '/');
  139.                         }
  140.                        
  141.                         // Everything's okay, so we need to now return the logged in user:
  142.                         return $adminUser;
  143.                     }
  144.                     else
  145.                     {
  146.                         throw new Exception( 'There was a problem logging you in. Please try again.' );
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.         else
  152.         {
  153.             throw new Exception( 'Database error.' );
  154.         }
  155.        
  156.         return false;
  157.     }
  158.    
  159.     // Log the user out:
  160.     public function logout()
  161.     {
  162.         if( !$this->current() )
  163.         {
  164.             return false;
  165.         }
  166.        
  167.         // Now destroy their session, delete the record from the DB and so on:
  168.         setcookie( $this->core->settings->cookie_login_name, '', (time() - 30000) );
  169.        
  170.         // Delete anything from the DB:
  171.         $del = $this->core->db->prepare( 'DELETE FROM tbl_admins WHERE admin_id = :id AND admin_session_id = :sessid' );
  172.        
  173.         if( $del->execute(array(':id' => $this->id, ':sessid' => session_id())) )
  174.         {
  175.             return session_destroy();
  176.         }
  177.        
  178.         return false;
  179.     }
  180.    
  181.    
  182.     // Function to update a user's property value in the DB:
  183.     public function updateProp( $field, $value )
  184.     {
  185.         $sql = $this->core->db->prepare( 'UPDATE tbl_admins SET admin_'.$field.' = :val WHERE admin_id = :id LIMIT 1' );
  186.         return $sql->execute(array(':val' => $value, ':id' => $this->id));
  187.     }
  188.    
  189.     // Check a user's password:
  190.     public function checkPassword( $password )
  191.     {
  192.         return ( $this->password == $this->_encryptPassword($password) );
  193.     }
  194.    
  195.     // Function to encrypt a user's password with their salt:
  196.     private function _encryptPassword( $password, $salt = '' )
  197.     {
  198.         $theSalt = (empty($salt)) ? $this->password_salt : $salt;
  199.         return hash( 'sha256', $password.$theSalt );
  200.     }
  201.    
  202.     //public function current()
  203.     //{
  204.     //    
  205.     //    // if the cookie is set and it's not empty, set the session id() using it:
  206.     //    if( !empty($_COOKIE[$this->core->settings->cookie_login_name]) )
  207.     //    {
  208.     //        session_id( $_COOKIE[$this->core->settings->cookie_login_name] );
  209.     //    }
  210.     //    
  211.     //    // Now we look them up win the login DB:
  212.     //    $sql = $this->core->db->prepare( 'SELECT log_admin_id AS id FROM tbl_admins_log WHERE log_id ' );
  213.     //}
  214.    
  215. }   // End Class
  216. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement