Your Ad Here

Posted By

MMDeveloper on 12/29/09


Tagged

recaptcha


Versions (?)

Recaptcha OOP Class


 / Published in: PHP
 

URL: http://skyward.nefec.org

for those who use the Recaptcha system, here's a class you can use. All of the code is directly from the Recaptcha website, just cleaned up into a stand-alone class.

  1. <?php
  2.  
  3. /*
  4. Be sure to set the public and private keys
  5. */
  6.  
  7.  
  8. class recaptcha {
  9. private $serverURLS = NULL;
  10. private $ReCaptchaResponse = NULL;
  11. private $keys = NULL;
  12.  
  13.  
  14. function __construct() {
  15. $this->serverURLS = new stdClass();
  16. $this->serverURLS->RECAPTCHA_API_SERVER = "http://api.recaptcha.net";
  17. $this->serverURLS->RECAPTCHA_API_SECURE_SERVER = "https://api-secure.recaptcha.net";
  18. $this->serverURLS->RECAPTCHA_VERIFY_SERVER = "api-verify.recaptcha.net";
  19.  
  20. $this->ReCaptchaResponse = new stdClass();
  21. $this->ReCaptchaResponse->is_valid = NULL;
  22. $this->ReCaptchaResponse->error = NULL;
  23.  
  24. $this->keys = new stdClass();
  25.  
  26.  
  27. $this->keys->recaptcha->publick = "6LcJOQkAAAAAABi3XIlSazOJTxbUNiVfQ0dkXAOT ";
  28. $this->keys->recaptcha->privatek = "INSERT YOUR PRIVATE KEY HERE";
  29.  
  30. $this->keys->mailhide->publick = "INSERT YOUR PUBLIC KEY HERE";
  31. $this->keys->mailhide->privatek =INSERT YOUR OTHER PRIVATE KEY HERE";
  32.  
  33.  
  34. }
  35.  
  36. private function _recaptcha_qsencode ($data) {
  37. $req = "";
  38. foreach ($data as $key => $value) {
  39. $req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
  40. }
  41.  
  42. // Cut the last '&'
  43. $req = substr($req, 0, strlen($req) - 1);
  44. return $req;
  45. }
  46.  
  47. private function _recaptcha_http_post($host, $path, $data, $port = 80) {
  48.  
  49. $req = $this->_recaptcha_qsencode ($data);
  50.  
  51. $http_request = "POST " . $path . " HTTP/1.0
  52. ";
  53. $http_request .= "Host: " . $host . "
  54. ";
  55. $http_request .= "Content-Type: application/x-www-form-urlencoded;
  56. ";
  57. $http_request .= "Content-Length: " . strlen($req) . "
  58. ";
  59. $http_request .= "User-Agent: reCAPTCHA/PHP
  60. ";
  61. $http_request .= "
  62. ";
  63. $http_request .= $req;
  64.  
  65. $response = '';
  66. if(false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) {
  67. die ('Could not open socket');
  68. }
  69.  
  70. fwrite($fs, $http_request);
  71.  
  72. while (!feof($fs)) {
  73. $response .= fgets($fs, 1160); // One TCP-IP packet
  74. }
  75. fclose($fs);
  76. $response = explode("
  77.  
  78. ", $response, 2);
  79.  
  80. return $response;
  81. }
  82.  
  83. public function recaptcha_get_html ($error = null, $use_ssl = false) {
  84. if ($this->keys->recaptcha->publick == null || $this->keys->recaptcha->publick == "") {
  85. die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
  86. } else {}
  87.  
  88. if ($use_ssl) {
  89. $server = $this->serverURLS->RECAPTCHA_API_SECURE_SERVER;
  90. }
  91. else {
  92. $server = $this->serverURLS->RECAPTCHA_API_SERVER;
  93. }
  94.  
  95. $errorpart = "";
  96. if ($error) {
  97. $errorpart = "&amp;error=" . $error;
  98. } else {}
  99. return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $this->keys->recaptcha->publick . $errorpart . '"></script>
  100.  
  101. <noscript>
  102. <iframe src="'. $server . '/noscript?k=' . $this->keys->recaptcha->publick . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
  103. <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  104. <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
  105. </noscript>';
  106. }
  107.  
  108. public function recaptcha_check_answer ($remoteip, $challenge, $response, $extra_params = array()) {
  109. if ($this->keys->recaptcha->privatek == null || $this->keys->recaptcha->privatek == "") {
  110. die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
  111. } else {}
  112.  
  113. if ($remoteip == null || $remoteip == "") {
  114. die ("For security reasons, you must pass the remote ip to reCAPTCHA");
  115. } else {}
  116.  
  117.  
  118.  
  119. //discard spam submissions
  120. if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
  121. $this->ReCaptchaResponse->is_valid = false;
  122. $this->ReCaptchaResponse->error = "incorrect_captcha_sol";
  123. return $this->ReCaptchaResponse;
  124. }
  125.  
  126. $response = $this->_recaptcha_http_post ($this->serverURLS->RECAPTCHA_VERIFY_SERVER, "/verify",
  127. "privatekey" => $this->keys->recaptcha->privatek,
  128. "remoteip" => $remoteip,
  129. "challenge" => $challenge,
  130. "response" => $response
  131. ) + $extra_params
  132. );
  133.  
  134. $answers = explode ("\n", $response[1]);
  135.  
  136. if (trim ($answers [0]) == "true") {
  137. $this->ReCaptchaResponse->is_valid = true;
  138. }
  139. else {
  140. $this->ReCaptchaResponse->is_valid = false;
  141. $this->ReCaptchaResponse->error = str_replace("-", "_", $answers[1]);
  142. }
  143. return $this->ReCaptchaResponse;
  144.  
  145. }
  146.  
  147. private function recaptcha_get_signup_url ($domain = null, $appname = null) {
  148. return "http://recaptcha.net/api/getkey?" . $this->_recaptcha_qsencode (array ("domain" => $domain, "app" => $appname));
  149. }
  150.  
  151. private function _recaptcha_aes_pad($val) {
  152. $block_size = 16;
  153. $numpad = $block_size - (strlen ($val) % $block_size);
  154. return str_pad($val, strlen ($val) + $numpad, chr($numpad));
  155. }
  156.  
  157. /* Mailhide related code */
  158.  
  159. private function _recaptcha_aes_encrypt($val,$ky) {
  160. if (!function_exists ("mcrypt_encrypt")) {
  161. die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.");
  162. } else {}
  163. $mode = MCRYPT_MODE_CBC;
  164. $enc = MCRYPT_RIJNDAEL_128;
  165. $val = $this->_recaptcha_aes_pad($val);
  166. return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  167. }
  168.  
  169.  
  170. private function _recaptcha_mailhide_urlbase64 ($x) {
  171. return strtr(base64_encode ($x), "+/", "-_");
  172. }
  173.  
  174. /* gets the reCAPTCHA Mailhide url for a given email, public key and private key */
  175. public function recaptcha_mailhide_url($email) {
  176. if ($this->keys->mailhide->publick == "" || $this->keys->mailhide->publick == null || $this->keys->mailhide->privatek == "" || $this->keys->mailhide->privatek == null) {
  177. die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " .
  178. "you can do so at <a href='http://mailhide.recaptcha.net/apikey'>http://mailhide.recaptcha.net/apikey</a>");
  179. } else {}
  180.  
  181. $ky = pack('H*', $this->keys->mailhide->privatek);
  182. $cryptmail = $this->_recaptcha_aes_encrypt ($email, $ky);
  183.  
  184. return "http://mailhide.recaptcha.net/d?k=" . $this->keys->mailhide->publick . "&c=" . $this->_recaptcha_mailhide_urlbase64 ($cryptmail);
  185. }
  186.  
  187. private function _recaptcha_mailhide_email_parts ($email) {
  188. $arr = preg_split("/@/", $email );
  189.  
  190. if (strlen ($arr[0]) <= 4) {
  191. $arr[0] = substr ($arr[0], 0, 1);
  192. }
  193. else if (strlen ($arr[0]) <= 6) {
  194. $arr[0] = substr ($arr[0], 0, 3);
  195. }
  196. else {
  197. $arr[0] = substr ($arr[0], 0, 4);
  198. }
  199. return $arr;
  200. }
  201.  
  202. public function recaptcha_mailhide_html($email) {
  203. $emailparts = $this->_recaptcha_mailhide_email_parts ($email);
  204. $url = $this->recaptcha_mailhide_url ($email);
  205.  
  206. return htmlentities($emailparts[0]) . "<a href=\"" . htmlentities ($url) .
  207. "\" onclick=\"window.open('" . htmlentities ($url) . "', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;\" title=\"Reveal this e-mail address\">...</a>@" . htmlentities ($emailparts [1]);
  208.  
  209. }
  210. }
  211.  
  212.  
  213.  
  214. /*
  215. $recaptcha = new recaptcha();
  216. $error = null;
  217.  
  218. if ($_POST["recaptcha_response_field"]) {
  219. $resp = $recaptcha->recaptcha_check_answer (
  220. $_SERVER["REMOTE_ADDR"],
  221. $_POST["recaptcha_challenge_field"],
  222. $_POST["recaptcha_response_field"]
  223. );
  224.  
  225. if ($resp->is_valid) {
  226. echo "You got it!";
  227. } else {
  228. # set the error code so that we can display it
  229. $error = $resp->error;
  230. }
  231. } else {}
  232.  
  233. echo $recaptcha->recaptcha_get_html($error);
  234.  
  235.  
  236.  
  237.  
  238.  
  239. The Mailhide version of example@example.com is
  240.  echo $recaptcha->recaptcha_mailhide_html ("example@example.com");
  241.  
  242. The url for the email is:
  243.  echo $recaptcha->recaptcha_mailhide_url ("example@example.com"); */
  244. ?>

Report this snippet  

You need to login to post a comment.