Advertisement
Guest User

Untitled

a guest
May 6th, 2017
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.21 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3.  * Authentic (User Auth) library.
  4.  *
  5.  *
  6.  * @package    Authentic
  7.  * @author     Andrew Smith
  8.  * @copyright  (c) 2007 - 2008 Andrew Smith
  9.  * @example $authentic = new Authentic();
  10.  * @example $authentic->table = "tablename";
  11.  */
  12. class Authentic_Core
  13. {
  14.     public $table = 'users';
  15.     public $user;
  16.     public $data = array();
  17.     public $match = array();
  18.    
  19.     public function __construct()
  20.     {
  21.         $this->db = new Database();
  22.         $this->session = new Session();
  23.        
  24.         Log::add('debug', 'Authentic Library loaded');
  25.     }
  26.    
  27.     /**
  28.      * Try to Login
  29.      *
  30.      * @example $authentic->login(array('email'=>$email, 'sha1|password'=>$password))
  31.      * @param array login fields
  32.      * @return  boolean
  33.      */
  34.     public function login($data = array())
  35.     {
  36.         $this->db->select('id');
  37.         $data = $this->find_hash($data);
  38.         $query = $this->db->getwhere($this->table, $data, 1, 0);
  39.         if($query->count())
  40.         {
  41.             $row = $query->current();
  42.             $this->session->set(array('user_id'=>$row->id));
  43.             return TRUE;
  44.         }
  45.         return FALSE;
  46.     }
  47.    
  48.     /**
  49.      * Check if user is logged in
  50.      *
  51.      * @example $authentic->is_logged_in()
  52.      * @return boolean
  53.      * Added in thanks to GeoffOs on Kohanaphp forum
  54.      */        
  55.     public function is_logged_in($role = '')
  56.     {
  57.         if($this->session->get('user_id'))
  58.         {
  59.             if(isset($this->user)) {
  60.                 if ($this->user->id == $this->session->get('user_id')) {
  61.                     if($role != NULL){
  62.                         return ($this->user->role == $role)?TRUE:FALSE;
  63.                     }
  64.                     return TRUE;
  65.                 }
  66.                 return FALSE;
  67.             } else {
  68.                 $query = $this->db->getwhere($this->table, array('id'=>$this->session->get('user_id')));
  69.                 if($query->count())
  70.                 {
  71.                     if ($role != NULL)
  72.                     {
  73.                         // Check that the user has the given role
  74.                         $this->db->select('roles.name', 'users.id');
  75.                         $this->db->join('roles', $this->table.'.role_id = roles.id');
  76.                         $query = $this->db->getwhere($this->table, array($this->table.'.id'=>$this->session->get('user_id')), 1, 0);
  77.                         if ($query->count() == 1) {
  78.                             $row = $query->current();
  79.                             if($row->name == $role)
  80.                             {
  81.                                 $this->user->id = $this->session->get('user_id');
  82.                                 $this->user->role = $row->name;
  83.                                 return TRUE;
  84.                             }
  85.                         }
  86.                         return FALSE;
  87.                     }
  88.                     $this->user->id = $this->session->get('user_id');
  89.                     return TRUE;
  90.                 }
  91.                 return FALSE;
  92.             }
  93.         }
  94.     }
  95.    
  96.     /**
  97.     * Registers user with info given
  98.     *
  99.     * @example $authentic->register(array('email'=>'i@me.com', 'username'=>'tester', 'sha1|password'=>'something', etc...))
  100.     * @return boolean
  101.     */
  102.     public function register($data = array())
  103.     {
  104.         $data = $this->find_hash($data);
  105.         if($this->db->insert($this->table, $data)){
  106.             // if insert was successful
  107.             return TRUE;
  108.         }
  109.         return FALSE;
  110.     }
  111.    
  112.     /**
  113.      * Update user info
  114.      *
  115.      * @example $authentic->update(array('id'=>3), array('email'=>'i@you.com', 'username'=>'test', 'sha1|password'=>'something', etc...));
  116.      * @return  boolean
  117.      */
  118.     public function update($match, $data)
  119.     {
  120.         $data = $this->find_hash($data);
  121.         if($this->db->update($this->table, $data, $match)){
  122.             // if update was successful
  123.             return TRUE;   
  124.         };
  125.         return FALSE;
  126.     }
  127.  
  128.     /**
  129.     * returns user id, also used to check if user exists
  130.     *
  131.     * @example $authentic->get_user('dipun')
  132.     * @example $authentic->get_user('dipun','username')
  133.     *
  134.     * Can use to find user by any number of params
  135.     * @example $authentic->get_user(array('dob'=>'19-06-87','UPPER(postcode)'=>'HA8 8TA','LOWER(securityQ)'=>'what is my favorite pet?','LOWER(securityA)'=>'the dog'))
  136.     *
  137.     * @return userID if user exists or false if not
  138.     */
  139.     public function get_user($username = null, $field = 'username'){
  140.         if(is_array($username)){
  141.             $query = $this->db->getwhere($this->table, $username);
  142.         }else{
  143.             $query = $this->db->getwhere($this->table, array($field=>$username));
  144.         }
  145.         if($query->count()){
  146.             return $query->current();
  147.         }
  148.         return FALSE;
  149.     }
  150.    
  151.     /**
  152.      * Logs the user out
  153.      *
  154.      * @example $authentic->logout()
  155.      */
  156.     public function logout()
  157.     {
  158.         $this->session->set(array('user_id'=>FALSE));
  159.     }
  160.    
  161.     /**
  162.      * Password Hashing
  163.      */
  164.     private function hash($password, $hashtype)
  165.     {
  166.         $salt = md5($password);
  167.         switch($hashtype){
  168.             case "md5":
  169.                 $password = md5($salt.$password);
  170.             break;
  171.             case "sha512":
  172.                 $password = hash('sha512', $salt.$password);
  173.             break;
  174.             default:
  175.                 $password = sha1($salt.$password);
  176.         }
  177.         return $password;
  178.     }
  179.    
  180.     /**
  181.      * Finds the hashtype
  182.      */
  183.     private function find_hash($data = array())
  184.     {
  185.         // find in array the item to hash
  186.         $findhash = preg_grep('#sha1\||md5\||sha512\|#', array_keys($data));
  187.        
  188.         // if no item in array to hash then return array
  189.         if(count($findhash) == 0){ return $data; }
  190.        
  191.         // check for items in array to hash and loop through them
  192.         for($i = 0; $i < count($findhash); $i++)
  193.         {
  194.             foreach($findhash as $split)
  195.             {
  196.                 $hash_pass = explode('|', $split);
  197.                 if(array_key_exists($split, $data))
  198.                 {
  199.                     $pass = $data[$split];
  200.                     unset($data[$split]);
  201.                     $password = $this->hash($pass, $hash_pass[0]);
  202.                     $data[$hash_pass[1]] = $password;
  203.                 }
  204.             }
  205.             // returns hashed array
  206.             return $data;
  207.         }
  208.     }
  209. } // End Authentic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement