source: trunk/lib/Captcha.inc.php @ 136

Last change on this file since 136 was 136, checked in by scdev, 18 years ago

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File size: 6.5 KB
Line 
1<?php
2/**
3 * Captcha.inc.php
4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
5 *
6 * ASCII captcha system.
7 *
8 * @author  Quinn Comendant <quinn@strangecode.com>
9 * @version 1.0
10-------------------------------------------------------------------------------------
11// Example.
12
13// Instantiate new Captcha. This automatically generates a new 4-digit captcha number.
14$captcha = new Captcha();
15
16<!-- HTML form for captcha -->
17<label for="sc-captcha"><?php echo _("Reverse Turing Test") ?></label>
18<p class="sc-help"><?php echo _("Please type the following number to prove you are a human. This is a measure to prevent spam robots from submitting this form.") ?></p>
19<pre style="font-size: 0.5em;"><?php $captcha->printAsciiNumber() ?></pre>
20<?php $captcha->printForm() ?>
21-------------------------------------------------------------------------------------
22 */
23class Captcha {
24
25    var $secret_key = 'some random seed text for the md5';
26    var $random_number;
27    var $ascii_numbers = array(
28        array(
29            '  #####   ',
30            ' ##   ##  ',
31            '##     ## ',
32            '##     ## ',
33            '##     ## ',
34            ' ##   ##  ',
35            '  #####   ',
36        ), array(
37            '   ##   ',
38            ' ####   ',
39            '   ##   ',
40            '   ##   ',
41            '   ##   ',
42            '   ##   ',
43            ' ###### ',
44        ), array(
45            ' #######  ',
46            '##     ## ',
47            '       ## ',
48            ' #######  ',
49            '##        ',
50            '##        ',
51            '######### ',
52        ), array(
53            ' #######  ',
54            '##     ## ',
55            '       ## ',
56            ' #######  ',
57            '       ## ',
58            '##     ## ',
59            ' #######  ',
60        ), array(
61            '##        ',
62            '##    ##  ',
63            '##    ##  ',
64            '##    ##  ',
65            '######### ',
66            '      ##  ',
67            '      ##  ',
68        ), array(
69            '######## ',
70            '##       ',
71            '##       ',
72            '#######  ',
73            '      ## ',
74            '##    ## ',
75            ' ######  ',
76        ), array(
77            ' #######  ',
78            '##     ## ',
79            '##        ',
80            '########  ',
81            '##     ## ',
82            '##     ## ',
83            ' #######  ',
84        ), array(
85            '######## ',
86            '##    ## ',
87            '    ##   ',
88            '   ##    ',
89            '  ##     ',
90            '  ##     ',
91            '  ##     ',
92        ), array(
93            ' #######  ',
94            '##     ## ',
95            '##     ## ',
96            ' #######  ',
97            '##     ## ',
98            '##     ## ',
99            ' #######  ',
100        ), array(
101            ' #######  ',
102            '##     ## ',
103            '##     ## ',
104            ' ######## ',
105            '       ## ',
106            '##     ## ',
107            ' #######  ',
108        )
109    );
110   
111    /**
112     * Constructor. Initialized new random number.
113     *
114     * @access  public
115     * @author  Quinn Comendant <quinn@strangecode.com>
116     * @since   20 Jan 2006 13:08:22
117     */
118    function Captcha()
119    {
120        $app =& App::getInstance();
121   
122        $this->secret_key = $app->getParam('signing_key');
123        $this->random_number = $this->_getRandomNumber();
124    }
125
126    /**
127     * Print ASCII number.
128     *
129     * @access  public
130     * @param   mixed   $num    Number to convert.
131     * @return  string  ASCII-ized number
132     * @author  Quinn Comendant <quinn@strangecode.com>
133     * @since   07 Dec 2005 21:59:25
134     */
135    function getAsciiNumber($num=null)
136    {
137        $app =& App::getInstance();
138   
139        if (!isset($num)) {
140            $num = $this->random_number;
141        }
142
143        if (preg_match('/[^\d]/', $num)) {
144            $app->logMsg(sprintf('Bad number: %s', $num), LOG_ERR, __FILE__, __LINE__);
145            return false;
146        }
147
148        // Number must be an array of strings.
149        $num = preg_split('// ', strval($num), -1, PREG_SPLIT_NO_EMPTY);
150
151        // All chars must be same height.
152        $output = '';
153        $char_height = sizeof($this->ascii_numbers[0]);
154        for ($i=0; $i<$char_height; $i++) {
155            foreach ($num as $n) {
156                $output .= $this->ascii_numbers[$n][$i];
157            }
158            $output .= "\n";
159        }
160
161        return $output;
162    }
163
164    /**
165     * Prints the captcha ASCII numbers.
166     *
167     * @access  public
168     * @author  Quinn Comendant <quinn@strangecode.com>
169     * @since   07 Dec 2005 22:09:04
170     */
171    function printAsciiNumber()
172    {
173        $ascii = $this->getAsciiNumber($this->random_number);
174        ?><pre name="sc-captcha" id="sc-captcha"><?php echo $ascii ?></pre><?php
175    }
176
177    /**
178     * Prints a form to enter captcha, including the required hidden hash form.
179     *
180     * @access  public
181     * @author  Quinn Comendant <quinn@strangecode.com>
182     * @since   07 Dec 2005 22:09:04
183     */
184    function printForm()
185    {
186        $hash = $this->_getMD5key($this->random_number);
187        ?>
188        <input name="sc-captcha-input" id="sc-captcha-input" type="text" />
189        <input name="sc-captcha-hash" id="sc-captcha-hash" type="hidden" value="<?php echo $hash ?>" />
190        <?php
191    }
192
193    /**
194     * Validate submitted number against ascii captcha.
195     * Regenerate md5 hash from submitted captcha number and compare with posted hash.
196     *
197     * @access  public
198     * @return  bool    True if number matches hash or false if EVIL ROBOT.
199     * @author  Quinn Comendant <quinn@strangecode.com>
200     * @since   07 Dec 2005 22:19:33
201     */
202    function valid()
203    {
204        $number = getFormData('sc-captcha-input');
205        $hash = getFormData('sc-captcha-hash');
206
207        if ('' == $number . $hash) {
208            return false;
209        }
210
211        return $this->_getMD5key($number) == $hash;
212    }
213
214    /**
215     * Generate random number 3-to-5 digits long.
216     *
217     * @access  private
218     * @return  int         Generated number.
219     * @author  Quinn Comendant <quinn@strangecode.com>
220     * @since   07 Dec 2005 21:40:25
221     */
222    function _getRandomNumber()
223    {
224        return substr(strval(rand(10000, 99999)), 0, rand(3, 5));
225    }
226
227    /**
228     * Generate md5 hash of number using secret key.
229     *
230     * @access  private
231     * @param   string  $input  Input text to whack.
232     * @return  string  md5 sum of input + seed
233     * @author  Quinn Comendant <quinn@strangecode.com>
234     * @since   07 Dec 2005 21:53:35
235     */
236    function _getMD5key($input)
237    {
238        return md5($this->secret_key . $input);
239    }
240
241}
242
243?>
Note: See TracBrowser for help on using the repository browser.