Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- /**
- * Authentic (User Auth) library.
- *
- *
- * @package Authentic
- * @author Andrew Smith
- * @copyright (c) 2007 - 2008 Andrew Smith
- * @example $authentic = new Authentic();
- * @example $authentic->table = "tablename";
- */
- class Authentic_Core
- {
- public $table = 'users';
- public $user;
- public $data = array();
- public $match = array();
- public function __construct()
- {
- $this->db = new Database();
- $this->session = new Session();
- Log::add('debug', 'Authentic Library loaded');
- }
- /**
- * Try to Login
- *
- * @example $authentic->login(array('email'=>$email, 'sha1|password'=>$password))
- * @param array login fields
- * @return boolean
- */
- public function login($data = array())
- {
- $this->db->select('id');
- $data = $this->find_hash($data);
- $query = $this->db->getwhere($this->table, $data, 1, 0);
- if($query->count())
- {
- $row = $query->current();
- $this->session->set(array('user_id'=>$row->id));
- return TRUE;
- }
- return FALSE;
- }
- /**
- * Check if user is logged in
- *
- * @example $authentic->is_logged_in()
- * @return boolean
- * Added in thanks to GeoffOs on Kohanaphp forum
- */
- public function is_logged_in($role = '')
- {
- if($this->session->get('user_id'))
- {
- if(isset($this->user)) {
- if ($this->user->id == $this->session->get('user_id')) {
- if($role != NULL){
- return ($this->user->role == $role)?TRUE:FALSE;
- }
- return TRUE;
- }
- return FALSE;
- } else {
- $query = $this->db->getwhere($this->table, array('id'=>$this->session->get('user_id')));
- if($query->count())
- {
- if ($role != NULL)
- {
- // Check that the user has the given role
- $this->db->select('roles.name', 'users.id');
- $this->db->join('roles', $this->table.'.role_id = roles.id');
- $query = $this->db->getwhere($this->table, array($this->table.'.id'=>$this->session->get('user_id')), 1, 0);
- if ($query->count() == 1) {
- $row = $query->current();
- if($row->name == $role)
- {
- $this->user->id = $this->session->get('user_id');
- $this->user->role = $row->name;
- return TRUE;
- }
- }
- return FALSE;
- }
- $this->user->id = $this->session->get('user_id');
- return TRUE;
- }
- return FALSE;
- }
- }
- }
- /**
- * Registers user with info given
- *
- * @example $authentic->register(array('email'=>'i@me.com', 'username'=>'tester', 'sha1|password'=>'something', etc...))
- * @return boolean
- */
- public function register($data = array())
- {
- $data = $this->find_hash($data);
- if($this->db->insert($this->table, $data)){
- // if insert was successful
- return TRUE;
- }
- return FALSE;
- }
- /**
- * Update user info
- *
- * @example $authentic->update(array('id'=>3), array('email'=>'i@you.com', 'username'=>'test', 'sha1|password'=>'something', etc...));
- * @return boolean
- */
- public function update($match, $data)
- {
- $data = $this->find_hash($data);
- if($this->db->update($this->table, $data, $match)){
- // if update was successful
- return TRUE;
- };
- return FALSE;
- }
- /**
- * returns user id, also used to check if user exists
- *
- * @example $authentic->get_user('dipun')
- * @example $authentic->get_user('dipun','username')
- *
- * Can use to find user by any number of params
- * @example $authentic->get_user(array('dob'=>'19-06-87','UPPER(postcode)'=>'HA8 8TA','LOWER(securityQ)'=>'what is my favorite pet?','LOWER(securityA)'=>'the dog'))
- *
- * @return userID if user exists or false if not
- */
- public function get_user($username = null, $field = 'username'){
- if(is_array($username)){
- $query = $this->db->getwhere($this->table, $username);
- }else{
- $query = $this->db->getwhere($this->table, array($field=>$username));
- }
- if($query->count()){
- return $query->current();
- }
- return FALSE;
- }
- /**
- * Logs the user out
- *
- * @example $authentic->logout()
- */
- public function logout()
- {
- $this->session->set(array('user_id'=>FALSE));
- }
- /**
- * Password Hashing
- */
- private function hash($password, $hashtype)
- {
- $salt = md5($password);
- switch($hashtype){
- case "md5":
- $password = md5($salt.$password);
- break;
- case "sha512":
- $password = hash('sha512', $salt.$password);
- break;
- default:
- $password = sha1($salt.$password);
- }
- return $password;
- }
- /**
- * Finds the hashtype
- */
- private function find_hash($data = array())
- {
- // find in array the item to hash
- $findhash = preg_grep('#sha1\||md5\||sha512\|#', array_keys($data));
- // if no item in array to hash then return array
- if(count($findhash) == 0){ return $data; }
- // check for items in array to hash and loop through them
- for($i = 0; $i < count($findhash); $i++)
- {
- foreach($findhash as $split)
- {
- $hash_pass = explode('|', $split);
- if(array_key_exists($split, $data))
- {
- $pass = $data[$split];
- unset($data[$split]);
- $password = $this->hash($pass, $hash_pass[0]);
- $data[$hash_pass[1]] = $password;
- }
- }
- // returns hashed array
- return $data;
- }
- }
- } // End Authentic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement