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

/**
 * Users
 *
 * This model represents user authentication data. It operates the following tables:
 * - user account data,
 * - user profiles
 *
 * @package	Tank_auth
 * @author	Ilya Konyukhov (http://konyukhov.com/soft/)
 */
class Users_model extends CI_Model
{
	private $table_name			= 'sys_users';			// user accounts
	private $profile_table_name	= 'sys_user_profiles';	// user profiles
    private $tb_session	 	    = 'ci_sessions';

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

		$ci =& get_instance();
		$this->table_name			= $ci->config->item('db_table_prefix', 'tank_auth').$this->table_name;
		$this->profile_table_name	= $ci->config->item('db_table_prefix', 'tank_auth').$this->profile_table_name;
	}

	/**
	 * Get user record by Id
	 *
	 * @param	int
	 * @param	bool
	 * @return	object
	 */
	function get_user_by_id($user_id, $activated)
	{
		$this->db->where('id', $user_id);
		$this->db->where('activated', $activated ? 1 : 0);

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
		return NULL;
	}

	/**
	 * Get user record by login (username or email)
	 *
	 * @param	string
	 * @return	object
	 */
	function get_user_by_login($login)
	{
		$this->db->where('LOWER(username)=', strtolower($login));
		$this->db->or_where('LOWER(email)=', strtolower($login));

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
		return NULL;
	}
	
	function search_login_in_ci_session($login)
	{
	    $this->db->select('timestamp,sys_users.id as user_id');
	    $this->db->from('ci_sessions');
	    $this->db->join('sys_users', 'ci_sessions.user_id=sys_users.id');    
	    $this->db->where('sys_users.username', $login);
	    $query = $this->db->get();	    
	    if ($query->num_rows() > 0 )
	    {
	        $expire = time() - $this->config->item('sess_expiration');
	        if((int)$query->row()->timestamp < $expire)
	        {
	            return !$this->delete_user_by_id($query->row()->user_id);
	        }
	        else
	        {
	            return TRUE;
	        }
	    } 
		return FALSE;
	}
	
	/*function validate_timestamp($id_session)
	{
	    $tb_session = $this->config->item('sess_save_path');
	    $expire = time() - $this->config->item('sess_expiration');
        $this->db->where("timestamp < {$expire}");
		$this->db->where('id', $id_session);
		$query = $this->db->get($tb_session);	
		if($query->num_rows() > 0)
		{
		    $this->delete_user_by_login
		}
		
		return !$this->db->delete($this->config->item('sess_save_path'));
	}*/
	
	function set_user_id_session($session_id,$user_id)
	{	    
	    $this->db->set('user_id', $user_id);
	    $this->db->where('id',$session_id);
	    $this->db->update('ci_sessions');
	    //return $this->db->affected_rows();
	}

	/**
	 * Get user record by username
	 *
	 * @param	string
	 * @return	object
	 */
	function get_user_by_username($username)
	{
		$this->db->where('LOWER(username)=', strtolower($username));

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
		return NULL;
	}

	/**
	 * Get user record by email
	 *
	 * @param	string
	 * @return	object
	 */
	function get_user_by_email($email)
	{
		$this->db->where('LOWER(email)=', strtolower($email));

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
		return NULL;
	}

	/**
	 * Check if username available for registering
	 *
	 * @param	string
	 * @return	bool
	 */
	function is_username_available($username)
	{
		$this->db->select('1', FALSE);
		$this->db->where('LOWER(username)=', strtolower($username));

		$query = $this->db->get($this->table_name);
		return $query->num_rows() == 0;
	}

	/**
	 * Check if email available for registering
	 *
	 * @param	string
	 * @return	bool
	 */
	function is_email_available($email)
	{
		$this->db->select('1', FALSE);
		$this->db->where('LOWER(email)=', strtolower($email));
		$this->db->or_where('LOWER(new_email)=', strtolower($email));

		$query = $this->db->get($this->table_name);
		return $query->num_rows() == 0;
	}

	/**
	 * Create new user record
	 *
	 * @param	array
	 * @param	bool
	 * @return	array
	 */
	function create_user($data, $activated = TRUE)
	{
		$data['created'] = date('Y-m-d H:i:s');
		$data['activated'] = $activated ? 1 : 0;

		if ($this->db->insert($this->table_name, $data)) {
			$user_id = $this->db->insert_id();
			if ($activated)	$this->create_profile($user_id);
			return array('user_id' => $user_id);
		}
		return NULL;
	}

	/**
	 * Activate user if activation key is valid.
	 * Can be called for not activated sys_users only.
	 *
	 * @param	int
	 * @param	string
	 * @param	bool
	 * @return	bool
	 */
	function activate_user($user_id, $activation_key, $activate_by_email)
	{
		$this->db->select('1', FALSE);
		$this->db->where('id', $user_id);
		if ($activate_by_email) {
			$this->db->where('new_email_key', $activation_key);
		} else {
			$this->db->where('new_password_key', $activation_key);
		}
		$this->db->where('activated', 0);
		$query = $this->db->get($this->table_name);

		if ($query->num_rows() == 1) {

			$this->db->set('activated', 1);
			$this->db->set('new_email_key', NULL);
			$this->db->where('id', $user_id);
			$this->db->update($this->table_name);

			$this->create_profile($user_id);
			return TRUE;
		}
		return FALSE;
	}

	/**
	 * Purge table of non-activated users
	 *
	 * @param	int
	 * @return	void
	 */
	function purge_na($expire_period = 172800)
	{
		$this->db->where('activated', 0);
		$this->db->where('UNIX_TIMESTAMP(created) <', time() - $expire_period);
		$this->db->delete($this->table_name);
	}

	/**
	 * Delete user record
	 *
	 * @param	int
	 * @return	bool
	 */
	function delete_user($user_id)
	{
		$this->db->where('id', $user_id);
		$this->db->delete($this->table_name);
		if ($this->db->affected_rows() > 0) {
			$this->delete_profile($user_id);
			return TRUE;
		}
		return FALSE;
	}
	
	/**
	 * Delete user session record
	 *
	 * @param	string
	 * @return	bool
	 */
    function delete_user_by_login($login)
	{
	    $this->db->select('id');
	    $this->db->where('username',$login);
	    $query = $this->db->get('sys_users');
	    if($query->num_rows() == 1) $id = $query->row()->id;
	    else return false;
	    
	    $this->delete_user_by_id($id);    
	}
	
	function delete_user_by_id($user_id)
	{
	    $this->db->where('user_id', $user_id);
	    $deleted = $this->db->delete($this->tb_session);
	    if($deleted) return true;
	    else return false;
	}	

	/**
	 * Set new password key for user.
	 * This key can be used for authentication when resetting user's password.
	 *
	 * @param	int
	 * @param	string
	 * @return	bool
	 */
	function set_password_key($user_id, $new_pass_key)
	{
		$this->db->set('new_password_key', $new_pass_key);
		$this->db->set('new_password_requested', date('Y-m-d H:i:s'));
		$this->db->where('id', $user_id);

		$this->db->update($this->table_name);
		return $this->db->affected_rows() > 0;
	}

	/**
	 * Check if given password key is valid and user is authenticated.
	 *
	 * @param	int
	 * @param	string
	 * @param	int
	 * @return	void
	 */
	function can_reset_password($user_id, $new_pass_key, $expire_period = 900)
	{
		$this->db->select('1', FALSE);
		$this->db->where('id', $user_id);
		$this->db->where('new_password_key', $new_pass_key);
		$this->db->where('UNIX_TIMESTAMP(new_password_requested) >', time() - $expire_period);

		$query = $this->db->get($this->table_name);
		return $query->num_rows() == 1;
	}

	/**
	 * Change user password if password key is valid and user is authenticated.
	 *
	 * @param	int
	 * @param	string
	 * @param	string
	 * @param	int
	 * @return	bool
	 */
	function reset_password($user_id, $new_pass, $new_pass_key, $expire_period = 900)
	{
		$this->db->set('password', $new_pass);
		$this->db->set('new_password_key', NULL);
		$this->db->set('new_password_requested', NULL);
		$this->db->where('id', $user_id);
		$this->db->where('new_password_key', $new_pass_key);
		$this->db->where('UNIX_TIMESTAMP(new_password_requested) >=', time() - $expire_period);

		$this->db->update($this->table_name);
		return $this->db->affected_rows() > 0;
	}

	/**
	 * Change user password
	 *
	 * @param	int
	 * @param	string
	 * @return	bool
	 */
	function change_password($user_id, $new_pass)
	{
		$this->db->set('password', $new_pass);
		$this->db->where('id', $user_id);

		$this->db->update($this->table_name);
		return $this->db->affected_rows() > 0;
	}

	/**
	 * Set new email for user (may be activated or not).
	 * The new email cannot be used for login or notification before it is activated.
	 *
	 * @param	int
	 * @param	string
	 * @param	string
	 * @param	bool
	 * @return	bool
	 */
	function set_new_email($user_id, $new_email, $new_email_key, $activated)
	{
		$this->db->set($activated ? 'new_email' : 'email', $new_email);
		$this->db->set('new_email_key', $new_email_key);
		$this->db->where('id', $user_id);
		$this->db->where('activated', $activated ? 1 : 0);

		$this->db->update($this->table_name);
		return $this->db->affected_rows() > 0;
	}

	/**
	 * Activate new email (replace old email with new one) if activation key is valid.
	 *
	 * @param	int
	 * @param	string
	 * @return	bool
	 */
	function activate_new_email($user_id, $new_email_key)
	{
		$this->db->set('email', 'new_email', FALSE);
		$this->db->set('new_email', NULL);
		$this->db->set('new_email_key', NULL);
		$this->db->where('id', $user_id);
		$this->db->where('new_email_key', $new_email_key);

		$this->db->update($this->table_name);
		return $this->db->affected_rows() > 0;
	}

	/**
	 * Update user login info, such as IP-address or login time, and
	 * clear previously generated (but not activated) passwords.
	 *
	 * @param	int
	 * @param	bool
	 * @param	bool
	 * @return	void
	 */
	function update_login_info($user_id, $record_ip, $record_time, $acessos_sistema = null)
	{
		$this->db->set('new_password_key', NULL);
		$this->db->set('new_password_requested', NULL);

		if(isset($record_ip))		$this->db->set('last_ip', $this->input->ip_address());
		if(isset($record_time))	 	$this->db->set('last_login', date('Y-m-d H:i:s'));
		if(isset($acessos_sistema)) $this->db->set('acessos_sistema', $acessos_sistema+1);
		
		$this->db->where('id', $user_id);
		$this->db->update($this->table_name);
	}
	
	

	/**
	 * Insert user login , such as IP-address or login time, and
	 * clear previously generated (but not activated) passwords.
	 *
	 * @param	int
	 * @param	bool
	 * @param	bool
	 * @return	void
	 */
	function insert_new_login($login, $datahora, $ip, $perfil)
	{
		$this->login   	= $login;
        $this->datahora = $datahora;
        $this->ip    	= $ip;
        $this->perfil 	= $perfil;

        $this->db->insert('sys_access', $this);
	}
	
	/**
	 * Ban user
	 *
	 * @param	int
	 * @param	string
	 * @return	void
	 */
	function ban_user($user_id, $reason = NULL)
	{
		$this->db->where('id', $user_id);
		$this->db->update($this->table_name, array(
			'banned'		=> 1,
			'ban_reason'	=> $reason,
		));
	}

	/**
	 * Unban user
	 *
	 * @param	int
	 * @return	void
	 */
	function unban_user($user_id)
	{
		$this->db->where('id', $user_id);
		$this->db->update($this->table_name, array(
			'banned'		=> 0,
			'ban_reason'	=> NULL,
		));
	}

	/**
	 * Create an empty profile for a new user
	 *
	 * @param	int
	 * @return	bool
	 */
	private function create_profile($user_id)
	{
		$this->db->set('user_id', $user_id);
		return $this->db->insert($this->profile_table_name);
	}

	/**
	 * Delete user profile
	 *
	 * @param	int
	 * @return	void
	 */
	private function delete_profile($user_id)
	{
		$this->db->where('user_id', $user_id);
		$this->db->delete($this->profile_table_name);
	}
	
	
	/**
	 * Funções para redefinir senha
	 */
	 function get_usuario_by_email($email)
	{
	    $this->db->or_where('LOWER(email)=', strtolower($email));
	
	    $query = $this->db->get($this->table_name);
	    if ($query->num_rows() == 1) return $query->row();
	    return NULL;
	}
	 
	function update_new_password_key($email, $new_password_key)
	{
		$this->db->set('new_password_key', $new_password_key);
		$this->db->where('email', $email);
		$this->db->update($this->table_name);
	    return TRUE;
	}
	
	function get_user_by_key($key_concat)
	{
		//$this->db->select();
		$this->db->where('CONCAT(`id_cadastro`,`new_password_key`)', $key_concat);
		$query = $this->db->get($this->table_name);
		if ($query->num_rows() === 1)
		{
			return $query->row();
		}
		else
		{
			return NULL;
		}
	}
	
	function definir_nova_senha($password, $key)
	{
		$data = array(
        'password' => $password,
        'new_password_key' => NULL,
        );

        $this->db->where('CONCAT(`id_cadastro`,`new_password_key`)', $key);
        $this->db->update($this->table_name, $data);
        
        if($this->db->affected_rows() === 1){
            //echo $this->db->last_query();
            return TRUE;   
        } else {
            return FALSE;
        }
	}
	
}

/* End of file users.php */
/* Location: ./application/models/auth/users.php */