paper: Mention grant agreement number in Acknowledgement
[living-lab-site.git] / application / libraries / Captcha.php
1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
2
3 /**
4  * A library which simplifies the insertion and verification of CATCHAs.
5  * 
6  * @author Călin-Andrei Burloiu
7  * @category Library
8  */
9 class Captcha {
10         
11         private $ci = NULL;
12         private $db;
13         private $params = NULL;
14         
15         public function __construct($params = NULL)
16         {
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();
21                 
22                 // Configuration parameters.
23                 if (!$params)
24                 {
25                         $this->params = $this->ci->config->item('captcha_params');
26                 }
27                 else
28                         $this->params = $params;
29                 
30                 if (!$this->params)
31                         die('Cannot load CAPTCHA config file.');
32         }
33         
34         public function get_params()
35         {
36                 return $this->params;
37         }
38
39         public function set_params($params)
40         {
41                 $this->params = $params;
42         }
43
44         /**
45          * Generates a CAPTCHA image and returns an array of associative data
46          * about the image.
47          * 
48          * @param string $word
49          * @return array
50          */
51         public function get_captcha($word = NULL)
52         {
53                 $this->ci->load->helper('captcha');
54                 
55                 if ($word)
56                         $this->params['captcha_params']['word'] = $word;
57
58                 // Creating the CAPTCHA.
59                 $cap = create_captcha($this->params['captcha_params']);
60
61                 $data = array(
62                         'captcha_time' => $cap['time'],
63                         'ip_address' => $this->ci->input->ip_address(),
64                         'word' => $cap['word']
65                         );
66
67                 // Remember in DB the CAPTCHA - user mapping.
68                 $str_query = $this->db->insert_string('captcha', $data);
69                 $this->db->query($str_query);
70
71                 return $cap;
72         }
73         
74         /**
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.
77          * 
78          * @param string $word
79          * @return boolean
80          */
81         public function check_captcha($word)
82         {
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
89
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);
94                 $row = $query->row();
95
96                 if ($row->count == 0)
97                 {
98                         return FALSE;
99                 }
100                 
101                 return TRUE;
102         }
103 }
104
105 /* End of file Captcha.php */
106 /* Location: ./application/libraries/Captcha.php */