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

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

minor misc bug fixes

File size: 6.3 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="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        $this->secret_key = App::getParam('signing_key');
121        $this->random_number = $this->_getRandomNumber();
122    }
123
124    /**
125     * Print ASCII number.
126     *
127     * @access  public
128     * @param   mixed   $num    Number to convert.
129     * @return  string  ASCII-ized number
130     * @author  Quinn Comendant <quinn@strangecode.com>
131     * @since   07 Dec 2005 21:59:25
132     */
133    function getAsciiNumber($num)
134    {
135        if (preg_match('/[^\d]/', $num)) {
136            App::logMsg(sprintf('Bad number: %s', $num), LOG_ERR, __FILE__, __LINE__);
137            return false;
138        }
139
140        // Number must be an array of strings.
141        $num = preg_split('// ', strval($num), -1, PREG_SPLIT_NO_EMPTY);
142
143        // All chars must be same height.
144        $output = '';
145        $char_height = sizeof($this->ascii_numbers[0]);
146        for ($i=0; $i<$char_height; $i++) {
147            foreach ($num as $n) {
148                $output .= $this->ascii_numbers[$n][$i];
149            }
150            $output .= "\n";
151        }
152
153        return $output;
154    }
155
156    /**
157     * Prints the captcha ASCII numbers.
158     *
159     * @access  public
160     * @author  Quinn Comendant <quinn@strangecode.com>
161     * @since   07 Dec 2005 22:09:04
162     */
163    function printAsciiNumber()
164    {
165        $ascii = $this->getAsciiNumber($this->random_number);
166        ?><pre name="sc-captcha" id="sc-captcha"><?php echo $ascii ?></pre><?php
167    }
168
169    /**
170     * Prints a form to enter captcha, including the required hidden hash form.
171     *
172     * @access  public
173     * @author  Quinn Comendant <quinn@strangecode.com>
174     * @since   07 Dec 2005 22:09:04
175     */
176    function printForm()
177    {
178        $hash = $this->_getMD5key($this->random_number);
179        ?>
180        <input name="sc-captcha-input" id="sc-captcha-input" type="text" />
181        <input name="sc-captcha-hash" id="sc-captcha-hash" type="hidden" value="<?php echo $hash ?>" />
182        <?php
183    }
184
185    /**
186     * Validate submitted number against ascii captcha.
187     * Regenerate md5 hash from submitted captcha number and compare with posted hash.
188     *
189     * @access  public
190     * @return  bool    True if number matches hash or false if EVIL ROBOT.
191     * @author  Quinn Comendant <quinn@strangecode.com>
192     * @since   07 Dec 2005 22:19:33
193     */
194    function valid()
195    {
196        $number = getFormData('sc-captcha-input');
197        $hash = getFormData('sc-captcha-hash');
198
199        if ('' == $number . $hash) {
200            return false;
201        }
202
203        return $this->_getMD5key($number) == $hash;
204    }
205
206    /**
207     * Generate random 4-digit number.
208     *
209     * @access  private
210     * @return  int         Number with 4 digits
211     * @author  Quinn Comendant <quinn@strangecode.com>
212     * @since   07 Dec 2005 21:40:25
213     */
214    function _getRandomNumber()
215    {
216        return rand(1000, 9999);
217    }
218
219    /**
220     * Generate md5 hash of number using secret key.
221     *
222     * @access  private
223     * @param   string  $input  Input text to whack.
224     * @return  string  md5 sum of input + seed
225     * @author  Quinn Comendant <quinn@strangecode.com>
226     * @since   07 Dec 2005 21:53:35
227     */
228    function _getMD5key($input)
229    {
230        return md5($this->secret_key . $input);
231    }
232
233}
234
235?>
Note: See TracBrowser for help on using the repository browser.