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