- Joomla CMS v2.5.11 (Joomla.org)
- jComments v2.3.0 (JoomlaTune.com)
Posting this as it was rather rewarding to achieve and I hope will be of some use to others in the same boat. This article details how to replace jComments built-in kCaptcha with Google's ReCaptcha. Note that I have an article on how to do this with a ReCaptcha alternative called PlayThru by AreYouAHuman.com
Why?
The kCaptcha used by the jComments extension is easily automated and no longer blocks spam comments.
How?
So the main bulk came from the JoomlaTune website but as it was a forum post that may get moved/deleted, I've copied most of it and put it here with my minor revisions/adjustments to get it actually working.
STEP #1: Check ReCaptcha API
Check your recaptcha plugin is up-to-date as some Joomla 2.5.x systems will not have updated this:
- Open the file \plugins\captcha\recaptcha\recaptcha.php
- See if you have the line const RECAPTCHA_API_SERVER = "http://api.recaptcha.net";
- If you do, change it to const RECAPTCHA_API_SERVER = "http://www.google.com/recaptcha/api";
STEP #2: Check ReCaptcha Private Key
This is assuming you have already signed up for a ReCaptcha key (Google ReCaptcha)
- Login to your Joomla Administration panel
- Extensions > Plug-in Manager > Captcha - ReCaptcha
- Check "Public Key" contains your key.
STEP #3: Enable admin option dropdown
This is a code change to allow the admin to select recaptcha.
- Open the file \administrator\components\com_jcomments\admin.jcomments.php (line 1066)
- Replace the line
- With this
STEP #4: Set in admin setting
We've made the option available, now login and set it to our new captcha
- Login to your Joomla Administration panel
- Components > JComments > Settings > Layout > CAPTCHA > RECAPTCHA
- Save the change.
STEP #5: Display recaptcha in comment form
This step displays the ReCaptcha on the form (note this code may need to be reverted if the admin does not want to use recaptcha anymore)
- Open the file \components\com_jcomments\tpl\default\tpl_form.php (line 114)
- Replace the code copyraw
<?php } if ($this->getVar('comments-form-captcha', 0) == 1) { $html = $this->getVar('comments-form-captcha-html'); if ($html != '') { echo $html; } else { $link = JCommentsFactory::getLink('captcha'); ?> <p> <span> <img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" src="/<?php echo $link; ?>" width="121" 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 /> </span> </p> <?php } } ?>
- <?php
- }
- if ($this->getVar('comments-form-captcha', 0) == 1) {
- $html = $this->getVar('comments-form-captcha-html');
- if ($html != '') {
- echo $html;
- } else {
- $link = JCommentsFactory::getLink('captcha');
- ?>
- <p>
- <span>
- <img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" src="/<?php echo $link; ?>" width="121" 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 />
- </span>
- </p>
- <?php
- }
- }
- ?>
- With this copyraw
<?php } if ($this->getVar('comments-form-captcha', 0) == 1) { $html = $this->getVar('comments-form-captcha-html','kcaptcha'); if ($html == 'kcaptcha') { $link = JCommentsFactory::getLink('captcha'); ?> <p> <span> <img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" src="/<?php echo $link; ?>" width="121" 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 /> </span> </p> <?php } else { $link = JCommentsFactory::getLink('captcha'); JPluginHelper::importPlugin('captcha'); $dispatcher = JDispatcher::getInstance(); $dispatcher->trigger('onInit','dynamic_recaptcha_1'); ?> <p> <span> <div id="dynamic_recaptcha_1"></div> </span> </p> <?php } } ?>
- <?php
- }
- if ($this->getVar('comments-form-captcha', 0) == 1) {
- $html = $this->getVar('comments-form-captcha-html','kcaptcha');
- if ($html == 'kcaptcha') {
- $link = JCommentsFactory::getLink('captcha');
- ?>
- <p>
- <span>
- <img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" src="/<?php echo $link; ?>" width="121" 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 />
- </span>
- </p>
- <?php
- } else {
- $link = JCommentsFactory::getLink('captcha');
- JPluginHelper::importPlugin('captcha');
- $dispatcher = JDispatcher::getInstance();
- $dispatcher->trigger('onInit','dynamic_recaptcha_1');
- ?>
- <p>
- <span>
- <div id="dynamic_recaptcha_1"></div>
- </span>
- </p>
- <?php
- }
- }
- ?>
STEP #6: Process submitted recaptcha
This step will add the functionality for your Joomla website to process the submitted captcha and verify it during form submission:
- Open the file \components\com_jcomments\jcomments.ajax.php (line 275)
- Replace the code copyraw
if ($acl->check('enable_captcha') == 1) { $captchaEngine = $config->get('captcha_engine', 'kcaptcha'); if ($captchaEngine == 'kcaptcha') { require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' ); if (!JCommentsCaptcha::check($values['captcha_refid'])) { self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha'); JCommentsCaptcha::destroy(); $response->addScript("jcomments.clear('captcha');"); return $response; } } else { $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response)); // if all plugins returns false if (!in_array(true, $result, true)) { self::showErrorMessage(JText::_('ERROR_CAPTCHA')); return $response; } } }
- if ($acl->check('enable_captcha') == 1) {
- $captchaEngine = $config->get('captcha_engine', 'kcaptcha');
- if ($captchaEngine == 'kcaptcha') {
- require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );
- if (!JCommentsCaptcha::check($values['captcha_refid'])) {
- self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
- JCommentsCaptcha::destroy();
- $response->addScript("jcomments.clear('captcha');");
- return $response;
- }
- } else {
- $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response));
- // if all plugins returns false
- if (!in_array(true, $result, true)) {
- self::showErrorMessage(JText::_('ERROR_CAPTCHA'));
- return $response;
- }
- }
- }
- With this copyraw
if ($acl->check('enable_captcha') == 1) { $captchaEngine = $config->get('captcha_engine', 'recaptcha'); if ($captchaEngine == 'recaptcha') { $post = JRequest::get('post'); JPluginHelper::importPlugin('captcha'); $dispatcher = JDispatcher::getInstance(); $resp = $dispatcher->trigger('onCheckAnswer',$post['recaptcha_response_field']); if (!$resp[0]) { self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha'); $response->addScript('Recaptcha.reload()'); return $response; } else { $response->addScript('Recaptcha.reload()'); } } elseif ($captchaEngine == 'kcaptcha') { require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' ); if (!JCommentsCaptcha::check($values['captcha_refid'])) { self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha'); JCommentsCaptcha::destroy(); $response->addScript("jcomments.clear('captcha');"); return $response; } } else { $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response)); // if all plugins returns false if (!in_array(true, $result, true)) { self::showErrorMessage(JText::_('ERROR_CAPTCHA')); return $response; } } }
- if ($acl->check('enable_captcha') == 1) {
- $captchaEngine = $config->get('captcha_engine', 'recaptcha');
- if ($captchaEngine == 'recaptcha') {
- $post = JRequest::get('post');
- JPluginHelper::importPlugin('captcha');
- $dispatcher = JDispatcher::getInstance();
- $resp = $dispatcher->trigger('onCheckAnswer',$post['recaptcha_response_field']);
- if (!$resp[0]) {
- self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
- $response->addScript('Recaptcha.reload()');
- return $response;
- } else {
- $response->addScript('Recaptcha.reload()');
- }
- } elseif ($captchaEngine == 'kcaptcha') {
- require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );
- if (!JCommentsCaptcha::check($values['captcha_refid'])) {
- self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
- JCommentsCaptcha::destroy();
- $response->addScript("jcomments.clear('captcha');");
- return $response;
- }
- } else {
- $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response));
- // if all plugins returns false
- if (!in_array(true, $result, true)) {
- self::showErrorMessage(JText::_('ERROR_CAPTCHA'));
- return $response;
- }
- }
- }
Additional
- To center the ReCaptcha on your form:
- Open the file \components\com_jcomments\tpl\default\tpl_form.php (~line 130)
- Replace the line <div id="dynamic_recaptcha_1"></div>
- With <div id="dynamic_recaptcha_1" style="width:320px;margin:0 auto;"></div>
- If you notice some CSS misalignments under the ReCaptcha input:
- Open the file \components\com_jcomments\tpl\default\tpl_form.php (~line 130)
- Find the line
- With this copyraw
$dispatcher->trigger('onInit','dynamic_recaptcha_1'); // for minor css tweaks $document = JFactory::getDocument(); $jcomments_recaptcha_css='.recaptchatable .recaptcha_r7_c1,.recaptchatable .recaptcha_r8_c1{top:-2px;position: relative;}'; $document->addStyleDeclaration($jcomments_recaptcha_css);
- $dispatcher->trigger('onInit','dynamic_recaptcha_1');
- // for minor css tweaks
- $document = JFactory::getDocument();
- $jcomments_recaptcha_css='.recaptchatable .recaptcha_r7_c1,.recaptchatable .recaptcha_r8_c1{top:-2px;position: relative;}';
- $document->addStyleDeclaration($jcomments_recaptcha_css);
- An alternative to Google's ReCaptcha but modifying the same files is AreYouAHuman's PlayThru
Source(s):
- www.joomlatune.com/forum/index.php?topic=8495.0
- www.joomlatune.com/forum/index.php?topic=942.0
- http://joomlacode.org/gf/project/joomla/
- http://stackoverflow.com/questions/3371314/how-to-reload-recaptcha-using-javascript
Category: Joomla :: Article: 553