getPath( 'class', 'com_chronocontact' ) );
// the class name must be the same as the file name without the .php at the end
class cf_recaptcha
{
//the next four fields must be defined for every plugin
var $result_TITLE = "ReCaptcha verification";
var $result_TOOLTIP = "use the popular reCaptcha image verification in your form as simple as adding a {ReCaptcha} text in your form and enabling and configuring the plugin correctly, more details inside!";
var $plugin_name = "cf_recaptcha"; // must be the same as the class name
var $event = "ONLOADONSUBMIT"; // must be defined and in Uppercase, should be ONSUBMIT or ONLOAD
// the next function must exist and will have the backend config code
function show_conf($row, $id, $form_id, $option)
{
global $mainframe;
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_chronocontact'
.DS.'helpers'.DS.'plugin.php');
$helper = new ChronoContactHelperPlugin();
// identify and initialise the parameters used in this plugin
$params_array = array(
'public_key' => '6LfNoAUAAAAAAKi8QZmjv-QHOvlGtyh509SG3FzG',
'private_key' => '6LfNoAUAAAAAABX7Dfw_9Pp4K4KVtKNCUHsIWG7O',
'ssl_server' => '0',
'theme' => 'red',
'lang' => 'en',
'api_server' => 'http://api.recaptcha.net',
'api_secure_server' => 'https://api-secure.recaptcha.net',
'verify_server' => 'api-verify.recaptcha.net',
'onsubmit' => 'before_email');
$params = $helper->loadParams($row, $params_array);
?>
";
$script = "
var RecaptchaOptions = {
theme : '".$params->get('theme')."',
lang : '".$params->get('lang')."'
};
";
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration($script);
$html_string = str_replace("{ReCaptcha}", $recaptcha_load, $html_string);
return $html_string;
}
function onsubmit( $option, $params, $plugin )
{
define('RECAPTCHA_VERIFY_SERVER', $params->get('verify_server'));
$MyForm =& CFChronoForm::getInstance();
$posted = JRequest::get( 'post' , JREQUEST_ALLOWRAW );
$resp = cf_recaptcha::recaptcha_check_answer(
$params->get('private_key'),
$_SERVER["REMOTE_ADDR"],
JRequest::getVar("recaptcha_challenge_field"),
JRequest::getVar("recaptcha_response_field") );
if ( !$resp->is_valid ) {
global $mainframe;//, $errorfound, $stoprunning;
$message = "The reCAPTCHA wasn't entered correctly. Go back and try it again
( reCAPTCHA said: ".$resp->error." )";
$MyForm->addErrorMsg($message);
$MyForm->error_found = true;
$MyForm->stoprunning = true;
//$MyForm->showForm($MyForm->formrow->name, $posted);
}
}
// this function must exist and may not be changed unless you need to customize something
function save_conf( $option )
{
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_chronocontact'
.DS.'helpers'.DS.'plugin.php');
$helper = new ChronoContactHelperPlugin();
$helper->save_conf($option);
}
//** below here is the ReCaptcha code - you should not need to change this
//** the functions are called from inside this class using cf_recaptcha::functionName()
/**
* Gets the challenge HTML (javascript and non-javascript version).
* This is called from the browser, and the resulting reCAPTCHA HTML widget
* is embedded within the HTML form it was called from.
* @param string $pubkey A public key for reCAPTCHA
* @param string $error The error given by reCAPTCHA (optional, default is null)
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
* @return string - The HTML to be embedded in the user's form.
*/
function recaptcha_get_html($pubkey, $error = null, $use_ssl = false)
{
if ( $pubkey == null || $pubkey == '' ) {
die ("To use reCAPTCHA you must get an API key from
http://recaptcha.net/api/getkey");
}
if ( $use_ssl ) {
$server = RECAPTCHA_API_SECURE_SERVER;
} else {
$server = RECAPTCHA_API_SERVER;
}
$errorpart = "";
if ( $error ) {
$errorpart = "&error=" . $error;
}
return '
';
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
* @param string $privkey
* @param string $remoteip
* @param string $challenge
* @param string $response
* @param array $extra_params an array of extra variables to post to the server
* @return ReCaptchaResponse
*/
function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array())
{
if ( $privkey == null || $privkey == '' ) {
die ("To use reCAPTCHA you must get an API key from
http://recaptcha.net/api/getkey");
}
if ( $remoteip == null || $remoteip == '' ) {
die ("For security reasons, you must pass the remote ip to reCAPTCHA");
}
//discard spam submissions
if ( $challenge == null || strlen($challenge) == 0
|| $response == null || strlen($response) == 0) {
$recaptcha_response = new ReCaptchaResponse();
$recaptcha_response->is_valid = false;
$recaptcha_response->error = 'incorrect-captcha-sol';
return $recaptcha_response;
}
$response = cf_recaptcha::_recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/verify",
array ( 'privatekey' => $privkey,
'remoteip' => $remoteip,
'challenge' => $challenge,
'response' => $response ) + $extra_params
);
$answers = explode ("\n", $response [1]);
$recaptcha_response = new ReCaptchaResponse();
if (trim ($answers [0]) == 'true') {
$recaptcha_response->is_valid = true;
} else {
$recaptcha_response->is_valid = false;
$recaptcha_response->error = $answers [1];
}
return $recaptcha_response;
}
function _recaptcha_http_post($host, $path, $data, $port = 80) {
$req = cf_recaptcha::_recaptcha_qsencode ($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$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("\r\n\r\n", $response, 2);
return $response;
}
/**
* Encodes the given data into a query string format
* @param $data - array of string elements to be encoded
* @return string - encoded request
*/
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;
}
}
/**
* A ReCaptchaResponse is returned from recaptcha_check_answer()
*/
class ReCaptchaResponse {
var $is_valid;
var $error;
}
?>