Hi there,
I figured out how to integrate ReCaptcha into JComments (instead of the build in KCaptcha).
I noticed a thread on
http://joomlaforum.ru/index.php?topic=80311.0 but as it is written in Russian I decided to open a new thread here.
What you have to do to add ReCaptcha support:
<html><body bgcolor="#FFFFFF"></body></html>
- Modifiy jcomments.ajax.php (components/com_jcomments/jcomments.ajax.php):
Replace (lines 215 - 226):
if ($acl->check('enable_captcha') == 1) {
require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );
if (!JCommentsCaptcha::check($values['captcha-refid'])) {
JCommentsAJAX::showErrorMessage($response, 'captcha', JText::_('ERROR_CAPTCHA'), true);
if (JCommentsCaptcha::attempts() > 3) {
JCommentsCaptcha::destroy();
$response->addScript("jcomments.clear('captcha');");
}
return $response;
}
}
with:
if ($acl->check('enable_captcha') == 1) {
require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );
$captcha = 'recaptcha';
if ($captcha == 'recaptcha') {
require_once(JCOMMENTS_BASE.DS.'libraries/recaptcha/'.'recaptchalib.php');
$privatekey = "<place your own private key here>";
$resp = null;
$error = null;
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$error = $resp->error;
JCommentsAJAX::showErrorMessage($response, 'captcha', JText::_('ERROR_CAPTCHA'), true);
# if (JCommentsCaptcha::attempts() > 3) {
# JCommentsCaptcha::destroy();
# $response->addScript("jcomments.clear('captcha');");
# }
return $response;
}
} elseif ($captcha == 'kcaptcha') {
if (!JCommentsCaptcha::check($values['captcha-refid'])) {
JCommentsAJAX::showErrorMessage($response, 'captcha', JText::_('ERROR_CAPTCHA'), true);
if (JCommentsCaptcha::attempts() > 3) {
JCommentsCaptcha::destroy();
$response->addScript("jcomments.clear('captcha');");
}
return $response;
}
}
}
- Modifiy tpl_form.php (components/com_jcomments/tpl/default/tpl_form.php):
Replace (lines 111 - 120):
if ($this->getVar('comments-form-captcha', 0) == 1) {
$link = JCommentsFactory::getLink('captcha');
?>
<p>
<img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" name="captcha-image" src="<?php echo $link; ?>" width="120" height="60" alt="<?php echo JText::_('FORM_CAPTCHA'); ?>" /><br />
<span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br />
<input class="captcha" id="comments-form-captcha" type="text" name="captcha-refid" value="" size="5" tabindex="6" /><br />
</p>
<?php
}
with:
if ($this->getVar('comments-form-captcha', 0) == 1) {
$captcha = 'recaptcha';
if ($captcha == 'recaptcha') {
$publickey = "<place your own pubilc key here>";
$theme = "white";
$lang = "de";
require_once(JCOMMENTS_BASE.DS.'libraries/recaptcha/'.'recaptchalib.php');
$js = "var RecaptchaOptions = {"."\n";
$js .= "theme : '".$theme."',"."\n";
$js .= "lang : '".$lang."'"."\n";
$js .= "};";
$recaptchaOptions =& JFactory::getDocument();
$recaptchaOptions->addScriptDeclaration($js);
?>
<span id="comments-form-captcha" name="captcha-refid"><?php echo recaptcha_get_html($publickey, $error); ?></span>
<?php
} elseif ($captcha == 'kcaptcha') {
$link = JCommentsFactory::getLink('captcha');
?>
<p>
<img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" name="captcha-image" src="<?php echo $link; ?>" width="120" height="60" alt="<?php echo JText::_('FORM_CAPTCHA'); ?>" /><br />
<span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br />
<input class="captcha" id="comments-form-captcha" type="text" name="captcha-refid" value="" size="5" tabindex="6" /><br />
</p>
<?php
}
}
You can change $lang to any of the predefined values:
- English: en
- Dutch: nl
- French: fr
- German: de
- Portuguese: pt
- Russian: ru
- Spanish: es
- Turkish: tr
You also can change $theme to any of the predefined values:
- red
- white
- blackglass
- clean
- custom
You also can change $captcha (in both files!) to any of the predefined values:
- recaptcha: Recaptcha validation
- kcaptcha: build in KCaptcha validation
What can be improved in the future:
- Automatic detection of the used site language (but somehow my site language parameter seems to be empty?).
- Ability to change the parameters ($publickey, $privatekey, $lang, $theme, $captcha, custom theme configuration) within the administration site.
Bugs:
- I noticed that when you type in one word correct and the other is wrong, the challange succeed. Also when you type in nothing the challange fails but don't tells you why. But when you type in both words correct it succeeds and when you type in nothing right the challange fails.
- The field does not refresh when you make a misstake.
I try to fix the bugs so the addon works in the right way. Maybe someone can help?
Have fun
Grinse