IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » hongster's snipts » kohana The latest kohana snipts from hongster.

showing 1-1 of 1 snipts for kohana
  • User authentication class.
    <?php defined('SYSPATH') or die('No direct script access.');
    
    class Auth_Core {
    	const REMEMBER_PERIOD = 1209600; // 2 weeks
    	
    	/*
    		If session is set, check last_login. If last_login is > 5 min ago,
    		update the database+cookie.
    		If no session, and has remember_code(from cookie), verify
    		remember_code. If remember_code is valid, login the user. If 
    		remember_code is invalid, preform logout.
    	*/
    	public static function auto_login() {
    		$session = Session::instance();
    	
    		$id = $session->get('id');
    		$remember_code = cookie::get('remember_code', FALSE);
    		if ($id) { // Has logged in
    			// Update DB every 5 or more minutes
    			if ((time() - $session->get('last_login', 0)) > 300) {
    				$user = ORM::factory('user', $id);
    				if ($user->loaded) {
    					self::force_login($user, $remember_code != FALSE);
    				}
    			}
    			
    			return; // Impt
    		}
    		
    		// Session expired, but cookie remind us to continue login status
    		if ($remember_code) {
    			$user = ORM::factory('user')
    				->where('remember_code', $remember_code)
    				->find();
    			if ($user->loaded) { // Valid code
    				// Is remember period expired?
    				if ((time() - strtotime($user->last_login)) 
    						< self::REMEMBER_PERIOD) {
    					self::force_login($user, TRUE);
    					return; // Impt
    				}
    			}
    			
    			// Invalid code or REMEMBER_PERIOD has passed
    			self::logout();
    		}
    	}
    	
    	/*
    		Determine if user is logged in.
    		@return boolean
    	*/
    	public static function is_login() {
    		$is_login = Session::instance()->get('id', FALSE);
    		return ($is_login != FALSE);
    	}
    	
    	/*
    		Force login without authentication.
    		@param User_Model $user
    		@param boolean $remember Keep login status for 2 week. Default FALSE.
    	*/
    	private static function force_login($user, $remember=FALSE) {
    		$last_login = time();
    		
    		$session = Session::instance();
    		$session->set('id', $user->id);
    		$session->set('last_login',time());
    		
    		$user->last_login = date('Y-m-d H:i:s', $last_login);
    		
    		if ($remember) {
    			// Gen remember_code
    			$user->remember_code = self::_rand_code();
    			// Set cookie
    			cookie::set(
    				'remember_code', 
    				$user->remember_code, 
    				self::REMEMBER_PERIOD);
    		}
    		else {
    			$user->remember_code = '';
    			cookie::delete('remember_code');
    		}
    		
    		$user->save();
    	}
    	
    	/*
    		Login and create session.
    		@param string $username
    		@param string $password
    		@param boolean $remember Keep login status for 2 week. Default FALSE.
    		@return boolean True, if login successful. False, otherwise.
    	*/
    	public static function login($username, $password, $remember=FALSE) {
    		$user = ORM::factory('user')
    			->where(array('username'=>$username, 'password'=>md5($password)))
    			->find();
    			
    		if ($user->loaded) {
    			self::force_login($user, $remember);
    			return TRUE;
    		}
    		
    		return FALSE;
    	}
    	
    	/*
    		Logout and destroy session.
    	*/
    	public static function logout() {
    		$session = Session::instance();
    						
    		$id = $session->get('id');
    		if ($id) {
    			$user = ORM::factory('user', $id);
    			var_dump($user);
    			if ($user->loaded) {
    				$user->last_login = date('Y-m-d H:i:s', time());
    				$user->remember_code = '';
    				$user->save();
    			}
    		}
    		
    		$session->destroy();
    		cookie::delete('remember_code');
    	}
    	
    	/*
    		Generate random alphanumeric code. [a-zA-Z0-9]
    		@param $length Length of code. Default 16.
    		@return string Random alphanumeric code.
    	*/
    	private static function _rand_code($length=16) {
    
    		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    
    		$code = '';
    
    		for (;$length>0;$length--) {
    
    			$code .= $chars[rand(0, 62)];
    
    		}
    
    		
    
    		return $code;
    
    	}
    }
    
    ?>
    

    copy | embed

    0 comments - tagged in  posted by hongster on Aug 02, 2009 at 2:39 a.m. EDT
Sign up to create your own snipts, or login.