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