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

Last change on this file since 32 was 32, checked in by scdev, 18 years ago
File size: 5.4 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 */
11class Captcha {
12   
13    var $secret_key = 'some random seed text for the md5';
14    var $ascii_numbers = array(
15        array(
16            '  #####   ',
17            ' ##   ##  ',
18            '##     ## ',
19            '##     ## ',
20            '##     ## ',
21            ' ##   ##  ',
22            '  #####   ',
23        ), array(
24            '   ##   ',
25            ' ####   ',
26            '   ##   ',
27            '   ##   ',
28            '   ##   ',
29            '   ##   ',
30            ' ###### ',
31        ), array(
32            ' #######  ',
33            '##     ## ',
34            '       ## ',
35            ' #######  ',
36            '##        ',
37            '##        ',
38            '######### ',
39        ), array(
40            ' #######  ',
41            '##     ## ',
42            '       ## ',
43            ' #######  ',
44            '       ## ',
45            '##     ## ',
46            ' #######  ',
47        ), array(
48            '##        ',
49            '##    ##  ',
50            '##    ##  ',
51            '##    ##  ',
52            '######### ',
53            '      ##  ',
54            '      ##  ',
55        ), array(
56            '######## ',
57            '##       ',
58            '##       ',
59            '#######  ',
60            '      ## ',
61            '##    ## ',
62            ' ######  ',
63        ), array(
64            ' #######  ',
65            '##     ## ',
66            '##        ',
67            '########  ',
68            '##     ## ',
69            '##     ## ',
70            ' #######  ',
71        ), array(
72            '######## ',
73            '##    ## ',
74            '    ##   ',
75            '   ##    ',
76            '  ##     ',
77            '  ##     ',
78            '  ##     ',
79        ), array(
80            ' #######  ',
81            '##     ## ',
82            '##     ## ',
83            ' #######  ',
84            '##     ## ',
85            '##     ## ',
86            ' #######  ',
87        ), array(
88            ' #######  ',
89            '##     ## ',
90            '##     ## ',
91            ' ######## ',
92            '       ## ',
93            '##     ## ',
94            ' #######  ',
95        )
96    );
97   
98    /**
99     * Print ASCII number.
100     *
101     * @access  public
102     * @param   mixed   $num    Number to convert.
103     * @return  string  ASCII-ized number
104     * @author  Quinn Comendant <quinn@strangecode.com>
105     * @since   07 Dec 2005 21:59:25
106     */
107    function getAsciiNumber($num)
108    {
109        if (preg_match('/[^\d]/', $num)) {
110            App::logMsg(sprintf('Bad number: %s', $num), LOG_ERR, __FILE__, __LINE__);
111            return false;
112        }
113       
114        // Number must be an array of strings.
115        $num = preg_split('// ', strval($num), -1, PREG_SPLIT_NO_EMPTY);
116
117        // All chars must be same height.
118        $output = '';
119        $char_height = sizeof($this->ascii_numbers[0]);
120        for ($i=0; $i<$char_height; $i++) {
121            foreach ($num as $n) {
122                $output .= $this->ascii_numbers[$n][$i];
123            }
124            $output .= "\n";
125        }
126       
127        return $output;
128    }
129   
130    /**
131     * Prints a form to enter captcha, including the required hidden hash form.
132     *
133     * @access  public
134     * @author  Quinn Comendant <quinn@strangecode.com>
135     * @since   07 Dec 2005 22:09:04
136     */
137    function printForm()
138    {
139        $number = $this->_getRandomNumber();
140        $hash = $this->_getMD5key($number);
141        $ascii = $this->getAsciiNumber($number);
142        ?>
143        <label for="sc-captcha"><?php echo _("Reverse Turing Test") ?></label>
144        <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>
145        <pre style="font-size: 0.5em;"><?php echo $ascii ?></pre>
146        <input name="sc-captcha" id="sc-captcha" type="text" />
147        <input name="sc-captcha-hash" type="hidden" value="<?php echo $hash ?>" />
148        <?php
149    }
150   
151    /**
152     * Validate submitted number against ascii captcha.
153     * Regenerate md5 hash from submitted captcha number and compare with posted hash.
154     *
155     * @access  public
156     * @return  bool    True if number matches hash or false if EVIL ROBOT.
157     * @author  Quinn Comendant <quinn@strangecode.com>
158     * @since   07 Dec 2005 22:19:33
159     */
160    function valid()
161    {
162        $number = getFormData('sc-captcha');
163        $hash = getFormData('sc-captcha-hash');
164
165        if ('' == $number . $hash) {
166            return false;
167        }
168
169        return $this->_getMD5key($number) == $hash;
170    }
171
172    /**
173     * Generate random 4-digit number.
174     *
175     * @access  private
176     * @return  int         Number with 4 digits
177     * @author  Quinn Comendant <quinn@strangecode.com>
178     * @since   07 Dec 2005 21:40:25
179     */
180    function _getRandomNumber()
181    {
182        return rand(1000, 9999);
183    }
184       
185    /**
186     * Generate md5 hash of number using secret key.
187     *
188     * @access  private
189     * @param   string  $input  Input text to whack.
190     * @return  string  md5 sum of input + seed
191     * @author  Quinn Comendant <quinn@strangecode.com>
192     * @since   07 Dec 2005 21:53:35
193     */
194    function _getMD5key($input)
195    {
196        return md5($this->secret_key . $input);
197    }
198   
199}
200
201?>
Note: See TracBrowser for help on using the repository browser.