pmpro_ReCaptcha

The Paid Memberships Pro pmpro ReCaptcha class.

Defined (1)

The class is defined in the following location(s).

/includes/lib/recaptchalib.php  
  1. class pmpro_ReCaptcha 
  2. private static $_signupUrl = "https://www.google.com/recaptcha/admin"; 
  3. private static $_siteVerifyUrl = 
  4. "https://www.google.com/recaptcha/api/siteverify?"; 
  5. private $_secret; 
  6. private static $_version = "php_1.0"; 
  7.  
  8. /** 
  9. * Constructor. 
  10. * @param string $secret shared secret between site and ReCAPTCHA server. 
  11. */ 
  12. function __construct($secret) 
  13. if ($secret == null || $secret == "") { 
  14. die("To use reCAPTCHA you must get an API key from <a href='" 
  15. . self::$_signupUrl . "'>" . self::$_signupUrl . "</a>"); 
  16. $this->_secret=$secret; 
  17.  
  18. /** 
  19. * Encodes the given data into a query string format. 
  20. * @param array $data array of string elements to be encoded. 
  21. * @return string - encoded request. 
  22. */ 
  23. private function _encodeQS($data) 
  24. $req = ""; 
  25. foreach ($data as $key => $value) { 
  26. $req .= $key . '=' . urlencode(stripslashes($value)) . '&'; 
  27.  
  28. // Cut the last '&' 
  29. $req=substr($req, 0, strlen($req)-1); 
  30. return $req; 
  31.  
  32. /** 
  33. * Submits an HTTP GET to a reCAPTCHA server. 
  34. * @param string $path url path to recaptcha server. 
  35. * @param array $data array of parameters to be sent. 
  36. * @return array response 
  37. */ 
  38. private function _submitHTTPGet($path, $data) 
  39. $req = $this->_encodeQS($data); 
  40. $response = file_get_contents($path . $req); 
  41. return $response; 
  42.  
  43. /** 
  44. * Calls the reCAPTCHA siteverify API to verify whether the user passes 
  45. * CAPTCHA test. 
  46. * @param string $remoteIp IP address of end user. 
  47. * @param string $response response string from recaptcha verification. 
  48. * @return ReCaptchaResponse 
  49. */ 
  50. public function verifyResponse($remoteIp, $response) 
  51. // Discard empty solution submissions 
  52. if ($response == null || strlen($response) == 0) { 
  53. $recaptchaResponse = new pmpro_ReCaptchaResponse(); 
  54. $recaptchaResponse->success = false; 
  55. $recaptchaResponse->errorCodes = 'missing-input'; 
  56. return $recaptchaResponse; 
  57.  
  58. $getResponse = $this->_submitHttpGet( 
  59. self::$_siteVerifyUrl,  
  60. array ( 
  61. 'secret' => $this->_secret,  
  62. 'remoteip' => $remoteIp,  
  63. 'v' => self::$_version,  
  64. 'response' => $response 
  65. ); 
  66. $answers = json_decode($getResponse, true); 
  67. $recaptchaResponse = new pmpro_ReCaptchaResponse(); 
  68.  
  69. if (trim($answers['success']) == true) { 
  70. $recaptchaResponse->success = true; 
  71. } else { 
  72. $recaptchaResponse->success = false; 
  73. if(!empty($answers['errorCodes'])) 
  74. $recaptchaResponse->errorCodes = $answers['errorCodes']; 
  75. else 
  76. $recaptchaResponse->errorCodes = 'Unknown error.'; 
  77.  
  78. return $recaptchaResponse;