/ Published in: PHP
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.
Expand |
Embed | Plain Text
<?php /* Be sure to set the public and private keys */ class recaptcha { private $serverURLS = NULL; private $ReCaptchaResponse = NULL; private $keys = NULL; function __construct() { $this->serverURLS = new stdClass(); $this->serverURLS->RECAPTCHA_API_SERVER = "http://api.recaptcha.net"; $this->serverURLS->RECAPTCHA_API_SECURE_SERVER = "https://api-secure.recaptcha.net"; $this->serverURLS->RECAPTCHA_VERIFY_SERVER = "api-verify.recaptcha.net"; $this->ReCaptchaResponse = new stdClass(); $this->ReCaptchaResponse->is_valid = NULL; $this->ReCaptchaResponse->error = NULL; $this->keys = new stdClass(); $this->keys->recaptcha->publick = "6LcJOQkAAAAAABi3XIlSazOJTxbUNiVfQ0dkXAOT "; $this->keys->recaptcha->privatek = "INSERT YOUR PRIVATE KEY HERE"; $this->keys->mailhide->publick = "INSERT YOUR PUBLIC KEY HERE"; } private function _recaptcha_qsencode ($data) { $req = ""; foreach ($data as $key => $value) { $req .= $key . '=' . urlencode( stripslashes($value) ) . '&'; } // Cut the last '&' $req = substr($req, 0, strlen($req) - 1); return $req; } private function _recaptcha_http_post($host, $path, $data, $port = 80) { $req = $this->_recaptcha_qsencode ($data); $http_request = "POST " . $path . " HTTP/1.0 "; $http_request .= "Host: " . $host . " "; $http_request .= "Content-Type: application/x-www-form-urlencoded; "; $http_request .= "Content-Length: " . strlen($req) . " "; $http_request .= "User-Agent: reCAPTCHA/PHP "; $http_request .= " "; $http_request .= $req; $response = ''; if(false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) { die ('Could not open socket'); } fwrite($fs, $http_request); while (!feof($fs)) { $response .= fgets($fs, 1160); // One TCP-IP packet } fclose($fs); $response = explode(" ", $response, 2); return $response; } public function recaptcha_get_html ($error = null, $use_ssl = false) { if ($this->keys->recaptcha->publick == null || $this->keys->recaptcha->publick == "") { die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>"); } else {} if ($use_ssl) { $server = $this->serverURLS->RECAPTCHA_API_SECURE_SERVER; } else { $server = $this->serverURLS->RECAPTCHA_API_SERVER; } $errorpart = ""; if ($error) { $errorpart = "&error=" . $error; } else {} return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $this->keys->recaptcha->publick . $errorpart . '"></script> <noscript> <iframe src="'. $server . '/noscript?k=' . $this->keys->recaptcha->publick . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/> <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/> </noscript>'; } public function recaptcha_check_answer ($remoteip, $challenge, $response, $extra_params = array()) { if ($this->keys->recaptcha->privatek == null || $this->keys->recaptcha->privatek == "") { die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>"); } else {} if ($remoteip == null || $remoteip == "") { } else {} //discard spam submissions $this->ReCaptchaResponse->is_valid = false; $this->ReCaptchaResponse->error = "incorrect_captcha_sol"; return $this->ReCaptchaResponse; } $response = $this->_recaptcha_http_post ($this->serverURLS->RECAPTCHA_VERIFY_SERVER, "/verify", array ( "privatekey" => $this->keys->recaptcha->privatek, "remoteip" => $remoteip, "challenge" => $challenge, "response" => $response ) + $extra_params ); $this->ReCaptchaResponse->is_valid = true; } else { $this->ReCaptchaResponse->is_valid = false; } return $this->ReCaptchaResponse; } private function recaptcha_get_signup_url ($domain = null, $appname = null) { return "http://recaptcha.net/api/getkey?" . $this->_recaptcha_qsencode (array ("domain" => $domain, "app" => $appname)); } private function _recaptcha_aes_pad($val) { $block_size = 16; } /* Mailhide related code */ private function _recaptcha_aes_encrypt($val,$ky) { } else {} $mode = MCRYPT_MODE_CBC; $enc = MCRYPT_RIJNDAEL_128; $val = $this->_recaptcha_aes_pad($val); return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); } private function _recaptcha_mailhide_urlbase64 ($x) { } /* gets the reCAPTCHA Mailhide url for a given email, public key and private key */ public function recaptcha_mailhide_url($email) { if ($this->keys->mailhide->publick == "" || $this->keys->mailhide->publick == null || $this->keys->mailhide->privatek == "" || $this->keys->mailhide->privatek == null) { "you can do so at <a href='http://mailhide.recaptcha.net/apikey'>http://mailhide.recaptcha.net/apikey</a>"); } else {} $cryptmail = $this->_recaptcha_aes_encrypt ($email, $ky); return "http://mailhide.recaptcha.net/d?k=" . $this->keys->mailhide->publick . "&c=" . $this->_recaptcha_mailhide_urlbase64 ($cryptmail); } private function _recaptcha_mailhide_email_parts ($email) { } } else { } return $arr; } public function recaptcha_mailhide_html($email) { $emailparts = $this->_recaptcha_mailhide_email_parts ($email); $url = $this->recaptcha_mailhide_url ($email); "\" 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]); } } /* $recaptcha = new recaptcha(); $error = null; if ($_POST["recaptcha_response_field"]) { $resp = $recaptcha->recaptcha_check_answer ( $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"] ); if ($resp->is_valid) { echo "You got it!"; } else { # set the error code so that we can display it $error = $resp->error; } } else {} echo $recaptcha->recaptcha_get_html($error); The Mailhide version of example@example.com is echo $recaptcha->recaptcha_mailhide_html ("example@example.com"); The url for the email is: echo $recaptcha->recaptcha_mailhide_url ("example@example.com"); */ ?>
You need to login to post a comment.
