/app/controllers/components/recaptcha.php

https://github.com/jeevangnanam/Hotel-Management-System · PHP · 140 lines · 90 code · 23 blank · 27 comment · 26 complexity · 743d434e8bfcab6bc22d9fee1240ddf6 MD5 · raw file

  1. <?php
  2. /**
  3. * @link http://bakery.cakephp.org/articles/view/recaptcha-component-helper-for-cakephp
  4. */
  5. class RecaptchaComponent extends Object {
  6. public $publickey = '';
  7. public $privatekey= '';
  8. public $is_valid = false;
  9. public $error = "";
  10. public function startup(&$controller){
  11. $this->publickey = Configure::read('Service.recaptcha_public_key');
  12. $this->privatekey = Configure::read('Service.recaptcha_private_key');
  13. Configure::write("Recaptcha.apiServer","http://api.recaptcha.net");
  14. Configure::write("Recaptcha.apiSecureServer","https://api-secure.recaptcha.net");
  15. Configure::write("Recaptcha.verifyServer","api-verify.recaptcha.net");
  16. Configure::write("Recaptcha.pubKey", $this->publickey);
  17. Configure::write("Recaptcha.privateKey", $this->privatekey);
  18. $this->controller =& $controller;
  19. $this->controller->helpers[] = "Recaptcha";
  20. }
  21. public function valid($form){
  22. if (isset($form['recaptcha_challenge_field']) && isset($form['recaptcha_response_field'])){
  23. if($this->recaptcha_check_answer(
  24. $this->privatekey,
  25. $_SERVER["REMOTE_ADDR"],
  26. $form['recaptcha_challenge_field'],
  27. $form['recaptcha_response_field']
  28. ) == 0)
  29. return false;
  30. if ($this->is_valid)
  31. return true;
  32. }
  33. return false;
  34. }
  35. /**
  36. * Calls an HTTP POST function to verify if the user's guess was correct
  37. * @param string $privkey
  38. * @param string $remoteip
  39. * @param string $challenge
  40. * @param string $response
  41. * @param array $extra_params an array of extra variables to post to the server
  42. * @return ReCaptchaResponse
  43. */
  44. public function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array()){
  45. if ($privkey == null || $privkey == ''){
  46. die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
  47. }
  48. if ($remoteip == null || $remoteip == ''){
  49. die ("For security reasons, you must pass the remote ip to reCAPTCHA");
  50. }
  51. //discard spam submissions
  52. if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
  53. $this->is_valid = false;
  54. $this->error = 'incorrect-captcha-sol';
  55. return 0;
  56. }
  57. $response = $this->_recaptcha_http_post(Configure::read('Recaptcha.verifyServer'), "/verify",
  58. array (
  59. 'privatekey' => $privkey,
  60. 'remoteip' => $remoteip,
  61. 'challenge' => $challenge,
  62. 'response' => $response
  63. ) + $extra_params
  64. );
  65. $answers = explode ("\n", $response [1]);
  66. if (trim ($answers [0]) == 'true') {
  67. $this->is_valid = true;
  68. return 1;
  69. }else{
  70. $this->is_valid = false;
  71. $this->error = $answers [1];
  72. return 0;
  73. }
  74. }
  75. /**
  76. * Submits an HTTP POST to a reCAPTCHA server
  77. * @param string $host
  78. * @param string $path
  79. * @param array $data
  80. * @param int port
  81. * @return array response
  82. */
  83. protected function _recaptcha_http_post($host, $path, $data, $port = 80) {
  84. $req = $this->_recaptcha_qsencode ($data);
  85. $http_request = "POST $path HTTP/1.0\r\n";
  86. $http_request .= "Host: $host\r\n";
  87. $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
  88. $http_request .= "Content-Length: " . strlen($req) . "\r\n";
  89. $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
  90. $http_request .= "\r\n";
  91. $http_request .= $req;
  92. $response = '';
  93. if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
  94. die ('Could not open socket');
  95. }
  96. fwrite($fs, $http_request);
  97. while ( !feof($fs) )
  98. $response .= fgets($fs, 1160); // One TCP-IP packet
  99. fclose($fs);
  100. $response = explode("\r\n\r\n", $response, 2);
  101. return $response;
  102. }
  103. /**
  104. * Encodes the given data into a query string format
  105. * @param $data - array of string elements to be encoded
  106. * @return string - encoded request
  107. */
  108. protected function _recaptcha_qsencode ($data) {
  109. $req = "";
  110. foreach ( $data as $key => $value )
  111. $req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
  112. // Cut the last '&'
  113. $req=substr($req,0,strlen($req)-1);
  114. return $req;
  115. }
  116. }
  117. ?>