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

/**
 * Login_attempts
 *
 * This model serves to watch on all attempts to login on the site
 * (to protect the site from brute-force attack to user database)
 *
 */
class Login_attempts extends CI_Model
{
	private $table_name = 'sec_login_attempts';

	function __construct()
	{
		parent::__construct();
		$ci =& get_instance();
	}

	/**
	 * Get number of attempts to login occured from given IP-address or login
	 *
	 * @param	string
	 * @param	string
	 * @return	int
	 */
	function get_attempts_num($ip_address, $login)
	{
		$this->db->select('1', FALSE);
		$this->db->where('ip_address', $ip_address);
		if (strlen($login) > 0) $this->db->or_where('login', $login);

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


	/**
	 * Get list of IP or Login with Max Attempts achived.
	 *
	 * @param	string
	 * @param	string
	 * @return	int
	 */
	function get_attempt_list($ip_address='')
	{
		if($ip_address=='') {
			// Grouped List
			$this->db->select('ip_address, count(*) as quantity');
			$this->db->group_by('ip_address');       
			$query = $this->db->get($this->table_name);
		} else {
			// Details List
			$this->db->where('ip_address',$ip_address);       
			$query = $this->db->get($this->table_name);
		}
		return $query->result_array();
	}



	/**
	 * Increase number of attempts for given IP-address and login
	 *
	 * @param	string
	 * @param	string
	 * @return	void
	 */
	function increase_attempt($ip_address, $login)
	{
		$this->db->insert($this->table_name, array('ip_address' => $ip_address, 'login' => $login));
	}

	/**
	 * Clear all attempt records for given IP-address and login.
	 * Also purge obsolete login attempts (to keep DB clear).
	 *
	 * @param	string
	 * @param	string
	 * @param	int
	 * @return	void
	 */
	function clear_attempts($ip_address, $login, $expire_period = 86400)
	{
		$this->db->where(array('ip_address' => $ip_address, 'login' => $login));

		// Purge obsolete login attempts
		$this->db->or_where('UNIX_TIMESTAMP(time) <', time() - $expire_period);

		$this->db->delete($this->table_name);
	}
}

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