1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
4 * A library which simplifies the insertion and verification of CATCHAs.
6 * @author Călin-Andrei Burloiu
13 private $params = NULL;
15 public function __construct($params = NULL)
17 $this->ci =& get_instance();
18 $this->ci->config->load('captcha');
19 $this->ci->load->library('Singleton_db');
20 $this->db = $this->ci->singleton_db->connect();
22 // Configuration parameters.
25 $this->params = $this->ci->config->item('captcha_params');
28 $this->params = $params;
31 die('Cannot load CAPTCHA config file.');
34 public function get_params()
39 public function set_params($params)
41 $this->params = $params;
45 * Generates a CAPTCHA image and returns an array of associative data
51 public function get_captcha($word = NULL)
53 $this->ci->load->helper('captcha');
56 $this->params['captcha_params']['word'] = $word;
58 // Creating the CAPTCHA.
59 $cap = create_captcha($this->params['captcha_params']);
62 'captcha_time' => $cap['time'],
63 'ip_address' => $this->ci->input->ip_address(),
64 'word' => $cap['word']
67 // Remember in DB the CAPTCHA - user mapping.
68 $str_query = $this->db->insert_string('captcha', $data);
69 $this->db->query($str_query);
75 * Check againt the DB if the word(s) entered by the user ($word) matches
76 * the CAPTCHA and if the CAPTCHA did not expired.
81 public function check_captcha($word)
83 // First, delete old captchas
84 $expiration_limit = (!$this->params['captcha_params']['expiration']
85 ? 7200 : $this->params['captcha_params']['expiration']);
86 $expiration = time() - $expiration_limit; // Two hour limit
87 $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
88 // TODO also delete the CAPTCHA image file
90 // Then see if a captcha exists:
91 $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
92 $binds = array($word, $this->ci->input->ip_address(), $expiration);
93 $query = $this->db->query($sql, $binds);
105 /* End of file Captcha.php */
106 /* Location: ./application/libraries/Captcha.php */