| [ Index ] |
PHP Cross Reference of MyBB 1.6.7 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * MyBB 1.6 4 * Copyright 2010 MyBB Group, All Rights Reserved 5 * 6 * Website: http://mybb.com 7 * License: http://mybb.com/about/license 8 * 9 * This class is based from reCAPTCHA's PHP library, adapted for use in MyBB. 10 * 11 * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net 12 * AUTHORS: 13 * Mike Crawford 14 * Ben Maurer 15 * 16 * Permission is hereby granted, free of charge, to any person obtaining a copy 17 * of this software and associated documentation files (the "Software"), to deal 18 * in the Software without restriction, including without limitation the rights 19 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 * copies of the Software, and to permit persons to whom the Software is 21 * furnished to do so, subject to the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be included in 24 * all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 32 * THE SOFTWARE. 33 * 34 * $Id: class_captcha.php 5605 2011-09-19 11:17:26Z Tomm $ 35 */ 36 37 class captcha 38 { 39 /** 40 * Type of CAPTCHA. 41 * 42 * 1 = Default CAPTCHA 43 * 2 = reCAPTCHA 44 * 45 * @var int 46 */ 47 public $type = 0; 48 49 /** 50 * The template to display the CAPTCHA in 51 * 52 * @var string 53 */ 54 public $captch_template = ''; 55 56 /** 57 * CAPTCHA Server URL 58 * 59 * @var string 60 */ 61 public $server = ''; 62 63 /** 64 * CAPTCHA Secure Server URL 65 * 66 * @var string 67 */ 68 public $secure_server = ''; 69 70 /** 71 * CAPTCHA Verify Server 72 * 73 * @var string 74 */ 75 public $verify_server = ''; 76 77 /** 78 * HTML of the built CAPTCHA 79 * 80 * @var string 81 */ 82 public $html = ''; 83 84 /** 85 * The errors that occurred when handling data. 86 * 87 * @var array 88 */ 89 public $errors = array(); 90 91 function __construct($build = false, $template = "") 92 { 93 global $mybb; 94 95 $this->type = $mybb->settings['captchaimage']; 96 97 // Prepare the build template 98 if($template) 99 { 100 $this->captcha_template = $template; 101 102 if($this->type == 2) 103 { 104 $this->captcha_template .= "_recaptcha"; 105 } 106 } 107 108 // Work on which CAPTCHA we've got installed 109 if($this->type == 2 && $mybb->settings['captchapublickey'] && $mybb->settings['captchaprivatekey']) 110 { 111 // We want to use reCAPTCHA, set the server options 112 $this->server = "http://www.google.com/recaptcha/api"; 113 $this->secure_server = "https://www.google.com/recaptcha/api"; 114 $this->verify_server = "www.google.com"; 115 116 if($build == true) 117 { 118 $this->build_recaptcha(); 119 } 120 } 121 else if($this->type == 1) 122 { 123 if(!function_exists("imagecreatefrompng")) 124 { 125 // We want to use the default CAPTCHA, but it's not installed 126 return false; 127 } 128 else if($build == true) 129 { 130 $this->build_captcha(); 131 } 132 } 133 134 // Plugin hook 135 } 136 137 function build_captcha($return = false) 138 { 139 global $db, $lang, $templates; 140 141 // This will build a MyBB CAPTCHA 142 $randomstr = random_str(5); 143 $imagehash = md5(random_str(12)); 144 145 $insert_array = array( 146 "imagehash" => $imagehash, 147 "imagestring" => $randomstr, 148 "dateline" => TIME_NOW 149 ); 150 151 $db->insert_query("captcha", $insert_array); 152 eval("\$this->html = \"".$templates->get($this->captcha_template)."\";"); 153 //eval("\$this->html = \"".$templates->get("member_register_regimage")."\";"); 154 } 155 156 function build_recaptcha() 157 { 158 global $lang, $mybb, $templates; 159 160 // This will build a reCAPTCHA 161 $server = $this->server; 162 $public_key = $mybb->settings['captchapublickey']; 163 164 eval("\$this->html = \"".$templates->get($this->captcha_template, 1, 0)."\";"); 165 //eval("\$this->html = \"".$templates->get("member_register_regimage_recaptcha")."\";"); 166 } 167 168 function build_hidden_captcha() 169 { 170 global $mybb, $templates; 171 172 $field = array(); 173 174 if($this->type == 1) 175 { 176 // Names 177 $hash = "imagehash"; 178 $string = "imagestring"; 179 180 // Values 181 $field['hash'] = $db->escape_string($mybb->input['imagehash']); 182 $field['string'] = $db->escape_string($mybb->input['imagestring']); 183 } 184 else if($this->type == 2) 185 { 186 // Names 187 $hash = "recaptcha_challenge_field"; 188 $string = "recaptcha_response_field"; 189 190 // Values 191 $field['hash'] = $mybb->input['recaptcha_challenge_field']; 192 $field['string'] = $mybb->input['recaptcha_response_field']; 193 } 194 195 eval("\$this->html = \"".$templates->get("post_captcha_hidden")."\";"); 196 return $this->html; 197 } 198 199 function validate_captcha() 200 { 201 global $db, $lang, $mybb; 202 203 // Plugin hook 204 205 if($this->type == 1) 206 { 207 // We have a normal CAPTCHA to handle 208 $imagehash = $db->escape_string($mybb->input['imagehash']); 209 $imagestring = $db->escape_string(my_strtolower($mybb->input['imagestring'])); 210 211 $query = $db->simple_select("captcha", "*", "imagehash = '{$imagehash}' AND LOWER(imagestring) = '{$imagestring}'"); 212 $imgcheck = $db->fetch_array($query); 213 214 if(!$imgcheck['dateline']) 215 { 216 $this->set_error($lang->invalid_captcha_verify); 217 } 218 219 $db->delete_query("captcha", "imagehash = '{$imagehash}'"); 220 } 221 elseif($this->type == 2) 222 { 223 $challenge = $mybb->input['recaptcha_challenge_field']; 224 $response = $mybb->input['recaptcha_response_field']; 225 226 if(!$challenge || strlen($challenge) == 0 || !$response || strlen($response) == 0) 227 { 228 $this->set_error($lang->invalid_captcha); 229 } 230 else 231 { 232 // We have a reCAPTCHA to handle 233 $data = $this->_qsencode(array( 234 'privatekey' => $mybb->settings['captchaprivatekey'], 235 'remoteip' => $mybb->session->ipaddress, 236 'challenge' => $challenge, 237 'response' => $response 238 )); 239 240 // Contact Google and see if our reCAPTCHA was successful 241 $http_request = "POST /recaptcha/api/verify HTTP/1.0\r\n"; 242 $http_request .= "Host: $this->verify_server\r\n"; 243 $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; 244 $http_request .= "Content-Length: ".strlen($data)."\r\n"; 245 $http_request .= "User-Agent: reCAPTCHA/PHP\r\n"; 246 $http_request .= "\r\n"; 247 $http_request .= $data; 248 249 $fs = @fsockopen($this->verify_server, 80, $errno, $errstr, 10); 250 251 if($fs == false) 252 { 253 $this->set_error($lang->invalid_captcha_transmit); 254 } 255 else 256 { 257 // We connected, but is it correct? 258 fwrite($fs, $http_request); 259 260 while(!feof($fs)) 261 { 262 $response .= fgets($fs, 1160); 263 } 264 265 fclose($fs); 266 267 $response = explode("\r\n\r\n", $response, 2); 268 $answer = explode("\n", $response[1]); 269 270 if(trim($answer[0]) != 'true') 271 { 272 // We got it wrong! Oh no... 273 $this->set_error($lang->invalid_captcha_verify); 274 } 275 } 276 } 277 } 278 279 // Plugin hook 280 281 if(count($this->errors) > 0) 282 { 283 return false; 284 } 285 else 286 { 287 return true; 288 } 289 } 290 291 /** 292 * Add an error to the error array. 293 */ 294 function set_error($error, $data='') 295 { 296 $this->errors[$error] = array( 297 "error_code" => $error, 298 "data" => $data 299 ); 300 } 301 302 /** 303 * Returns the error(s) that occurred when handling data 304 * in a format that MyBB can handle. 305 * 306 * @return An array of errors in a MyBB format. 307 */ 308 function get_errors() 309 { 310 global $lang; 311 312 foreach($this->errors as $error) 313 { 314 $lang_string = $error['error_code']; 315 316 if(!$lang->$lang_string) 317 { 318 $errors[] = $error['error_code']; 319 continue; 320 } 321 322 if(!empty($error['data']) && !is_array($error['data'])) 323 { 324 $error['data'] = array($error['data']); 325 } 326 327 if(is_array($error['data'])) 328 { 329 array_unshift($error['data'], $lang->$lang_string); 330 $errors[] = call_user_func_array(array($lang, "sprintf"), $error['data']); 331 } 332 else 333 { 334 $errors[] = $lang->$lang_string; 335 } 336 } 337 338 return $errors; 339 } 340 341 private function _qsencode($data) 342 { 343 $req = ''; 344 foreach($data as $key => $value) 345 { 346 $req .= $key.'='.urlencode(stripslashes($value)).'&'; 347 } 348 349 $req = substr($req, 0, (strlen($req) - 1)); 350 351 return $req; 352 } 353 } 354 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Mar 31 17:55:03 2012 | Cross-referenced by PHPXref 0.7.1 |