X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=application%2Flibraries%2FCaptcha.php;fp=application%2Flibraries%2FCaptcha.php;h=6950a9c4ffb03ead3d6be05e4ef06342f0bf7562;hb=a34b84478af8de577afef1b762fc6861b2fd74e7;hp=fc4147194d864e388b8f98cbf00e26b9e20a58bd;hpb=8889adf32898adeff7a85cc040f5f409d3bce36c;p=living-lab-site.git diff --git a/application/libraries/Captcha.php b/application/libraries/Captcha.php index fc41471..6950a9c 100644 --- a/application/libraries/Captcha.php +++ b/application/libraries/Captcha.php @@ -1,67 +1,95 @@ ci =& get_instance(); $this->ci->config->load('captcha'); $this->ci->load->library('Singleton_db'); $this->db = $this->ci->singleton_db->connect(); - $this->params = $this->ci->config->item('captcha_params'); + // Configuration parameters. + if (!$params) + { + $this->params = $this->ci->config->item('captcha_params'); + } + else + $this->params = $params; if (!$this->params) die('Cannot load CAPTCHA config file.'); } + public function get_params() + { + return $this->params; + } + + public function set_params($params) + { + $this->params = $params; + } + /** - * Generates a CAPTCHA image and returns an HTML image tag for it. + * Generates a CAPTCHA image and returns an array of associative data + * about the image. * * @param string $word - * @return string + * @return array */ - public function get_captcha_tag($word = NULL) + public function get_captcha($word = NULL) { - $this->load->helper('captcha'); + $this->ci->load->helper('captcha'); if ($word) - $this->params['word'] = $word; + $this->params['captcha_params']['word'] = $word; - $cap = create_captcha($this->params); + // Creating the CAPTCHA. + $cap = create_captcha($this->params['captcha_params']); $data = array( 'captcha_time' => $cap['time'], - 'ip_address' => $this->input->ip_address(), + 'ip_address' => $this->ci->input->ip_address(), 'word' => $cap['word'] ); + // Remember in DB the CAPTCHA - user mapping. $str_query = $this->db->insert_string('captcha', $data); $this->db->query($str_query); - return $cap['image']; + return $cap; } /** * Check againt the DB if the word(s) entered by the user ($word) matches * the CAPTCHA and if the CAPTCHA did not expired. + * + * @param string $word + * @return boolean */ public function check_captcha($word) { // First, delete old captchas - $expiration_limit = (!$this->params['expiration'] - ? 7200 : $this->params['expiration']); + $expiration_limit = (!$this->params['captcha_params']['expiration'] + ? 7200 : $this->params['captcha_params']['expiration']); $expiration = time() - $expiration_limit; // Two hour limit $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration); // TODO also delete the CAPTCHA image file // Then see if a captcha exists: $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?"; - $binds = array($word, $this->input->ip_address(), $expiration); + $binds = array($word, $this->ci->input->ip_address(), $expiration); $query = $this->db->query($sql, $binds); $row = $query->row();