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

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

Q - added some sc-small classes to form inputs.

File size: 6.6 KB
RevLine 
[42]1<?php
[29]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
[53]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>
[121]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>
[53]19<pre style="font-size: 0.5em;"><?php $captcha->printAsciiNumber() ?></pre>
20<?php $captcha->printForm() ?>
21-------------------------------------------------------------------------------------
[29]22 */
23class Captcha {
[42]24
[29]25    var $secret_key = 'some random seed text for the md5';
[53]26    var $random_number;
[29]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    );
[53]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    {
[136]120        $app =& App::getInstance();
121   
122        $this->secret_key = $app->getParam('signing_key');
[53]123        $this->random_number = $this->_getRandomNumber();
124    }
[42]125
[29]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     */
[108]135    function getAsciiNumber($num=null)
[29]136    {
[136]137        $app =& App::getInstance();
138   
[108]139        if (!isset($num)) {
140            $num = $this->random_number;
141        }
142
[29]143        if (preg_match('/[^\d]/', $num)) {
[136]144            $app->logMsg(sprintf('Bad number: %s', $num), LOG_ERR, __FILE__, __LINE__);
[42]145            return false;
[29]146        }
[42]147
[29]148        // Number must be an array of strings.
[176]149        $num = preg_split('//', strval($num), -1, PREG_SPLIT_NO_EMPTY);
[29]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        }
[42]160
[29]161        return $output;
162    }
[42]163
[29]164    /**
[53]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    /**
[29]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    {
[53]186        $hash = $this->_getMD5key($this->random_number);
[29]187        ?>
[176]188        <input name="sc-captcha-input" id="sc-captcha-input" class="sc-small" type="text" />
[53]189        <input name="sc-captcha-hash" id="sc-captcha-hash" type="hidden" value="<?php echo $hash ?>" />
[29]190        <?php
191    }
[42]192
[29]193    /**
[42]194     * Validate submitted number against ascii captcha.
[29]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    {
[53]204        $number = getFormData('sc-captcha-input');
[29]205        $hash = getFormData('sc-captcha-hash');
206
207        if ('' == $number . $hash) {
[42]208            return false;
[29]209        }
210
211        return $this->_getMD5key($number) == $hash;
212    }
213
214    /**
[108]215     * Generate random number 3-to-5 digits long.
[29]216     *
217     * @access  private
[108]218     * @return  int         Generated number.
[29]219     * @author  Quinn Comendant <quinn@strangecode.com>
220     * @since   07 Dec 2005 21:40:25
221     */
222    function _getRandomNumber()
223    {
[108]224        return substr(strval(rand(10000, 99999)), 0, rand(3, 5));
[29]225    }
[42]226
[29]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    }
[42]240
[29]241}
242
243?>
Note: See TracBrowser for help on using the repository browser.