<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(FCPATH . 'common/libraries/Ob_license.php');

class Auth extends Ob_license {

	function __construct()
	{
		parent::__construct();

		$this->CI =& get_instance();
	}
	
	public function encode($password)
	{
		  $this->CI->load->library('encryption');
          return $this->CI->encryption->sha1($password);
	}
	
	public function generate_auth_token($user_id, $email)
	{
		return $this->hash_ob_password(random_string('alnum', 5).$email.strtotime("now").$user_id);
	}
	
	public function get_date($date_format='')
	{
		$CI	=& get_instance();
		$CI->load->helper('date');
		
		$date_format= ($date_format)?$date_format:'Y-m-d H:i:s';
		return gmdate($date_format);
	}
	
	public static function get_random_password($length = 8)
	{
		$salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$base = strlen($salt);
		$makepass = '';
		
		$random = random_string('alnum', $length+1);
		$shift = ord($random[0]);
		for ($i = 1; $i <= $length; ++$i)
		{
			$makepass .= $salt[($shift + ord($random[$i])) % $base];
			$shift += ord($random[$i]);
		}

		return $makepass;
	}
	
	public function get_allowance()
	{
		$CI	=& get_instance();
		$cur_date = $this->get_date('Y-m-d');
		
		$row = $CI->db->select('allow_period_id')->from('allowance_period')->where(array('from_date <=' => $cur_date, 'to_date >=' => $cur_date))->get()->row();
		
		return $row?$row->allow_period_id:0;
	}
}
