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