unactivated users CLI cleanup; user management bugs fixed; working at CAPTCHA
[living-lab-site.git] / application / libraries / Captcha.php
1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
2
3 class Captcha {
4         
5         private $ci = NULL;
6         private $db;
7         private $params = NULL;
8         
9         public function __construct()
10         {
11                 $this->ci =& get_instance();
12                 $this->ci->config->load('captcha');
13                 $this->ci->load->library('Singleton_db');
14                 $this->db = $this->ci->singleton_db->connect();
15                 
16                 $this->params = $this->ci->config->item('captcha_params');
17                 
18                 if (!$this->params)
19                         die('Cannot load CAPTCHA config file.');
20         }
21         
22         /**
23          * Generates a CAPTCHA image and returns an HTML image tag for it.
24          * 
25          * @param string $word
26          * @return string
27          */
28         public function get_captcha_tag($word = NULL)
29         {
30                 $this->load->helper('captcha');
31                 
32                 if ($word)
33                         $this->params['word'] = $word;
34
35                 $cap = create_captcha($this->params);
36
37                 $data = array(
38                         'captcha_time' => $cap['time'],
39                         'ip_address' => $this->input->ip_address(),
40                         'word' => $cap['word']
41                         );
42
43                 $str_query = $this->db->insert_string('captcha', $data);
44                 $this->db->query($str_query);
45
46                 return $cap['image'];
47         }
48         
49         /**
50          * Check againt the DB if the word(s) entered by the user ($word) matches
51          * the CAPTCHA and if the CAPTCHA did not expired.
52          */
53         public function check_captcha($word)
54         {
55                 // First, delete old captchas
56                 $expiration_limit = (!$this->params['expiration']
57                                 ? 7200 : $this->params['expiration']);
58                 $expiration = time() - $expiration_limit; // Two hour limit
59                 $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
60                 // TODO also delete the CAPTCHA image file
61
62                 // Then see if a captcha exists:
63                 $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
64                 $binds = array($word, $this->input->ip_address(), $expiration);
65                 $query = $this->db->query($sql, $binds);
66                 $row = $query->row();
67
68                 if ($row->count == 0)
69                 {
70                         return FALSE;
71                 }
72                 
73                 return TRUE;
74         }
75 }
76
77 /* End of file Captcha.php */
78 /* Location: ./application/libraries/Captcha.php */