Welcome to Joel Lipman .Com

Preparing Content...


Loading...

Our Website Development Notes

We hope this helps!

Articles // Web Development // Joomla

JComments 2.3.0 with ReCaptcha in Joomla 2.5.x

Tuesday, 15th April 2014
67,727 Reads
Applies to: What?
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:
  1. Open the file \plugins\captcha\recaptcha\recaptcha.php
  2. See if you have the line const RECAPTCHA_API_SERVER = "http://api.recaptcha.net";
  3. 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)
  1. Login to your Joomla Administration panel
  2. Extensions > Plug-in Manager > Captcha - ReCaptcha
  3. Check "Public Key" contains your key.

STEP #3: Enable admin option dropdown
This is a code change to allow the admin to select recaptcha.
  1. Open the file \administrator\components\com_jcomments\admin.jcomments.php (line 1066)
  2. Replace the line
    1. $captcha[] = JCommentsHTML::makeOption('kcaptcha', 'KCAPTCHA');
  3. With this
    1. $captcha[] = JCommentsHTML::makeOption('kcaptcha', 'KCAPTCHA');  
    2. $captcha[] = JCommentsHTML::makeOption('recaptcha', 'RECAPTCHA');

STEP #4: Set in admin setting
We've made the option available, now login and set it to our new captcha
  1. Login to your Joomla Administration panel
  2. Components > JComments > Settings > Layout > CAPTCHA > RECAPTCHA
  3. 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)
  1. Open the file \components\com_jcomments\tpl\default\tpl_form.php (line 114)
  2. Replace the code
    1. <?php  
    2. }  
    3. if ($this->getVar('comments-form-captcha', 0) == 1) {  
    4. $html = $this->getVar('comments-form-captcha-html');  
    5. if ($html != '') {  
    6. echo $html;  
    7. } else {  
    8. $link = JCommentsFactory::getLink('captcha');  
    9. ?>  
    10. <p>  
    11. <span>  
    12. <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 />  
    13. <span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br />  
    14. <input class="captcha" id="comments-form-captcha" type="text" name="captcha_refid" value="" size="5" tabindex="6" /><br />  
    15. </span>  
    16. </p>  
    17. <?php  
    18. }  
    19. }  
    20. ?>
  3. With this
    1. <?php  
    2. }  
    3. if ($this->getVar('comments-form-captcha', 0) == 1) {  
    4. $html = $this->getVar('comments-form-captcha-html','kcaptcha');  
    5. if ($html == 'kcaptcha') {  
    6. $link = JCommentsFactory::getLink('captcha');  
    7. ?>  
    8. <p>  
    9. <span>  
    10. <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 />  
    11. <span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br />  
    12. <input class="captcha" id="comments-form-captcha" type="text" name="captcha_refid" value="" size="5" tabindex="6" /><br />  
    13. </span>  
    14. </p>  
    15. <?php  
    16. } else {  
    17. $link = JCommentsFactory::getLink('captcha');  
    18. JPluginHelper::importPlugin('captcha');  
    19. $dispatcher = JDispatcher::getInstance();  
    20. $dispatcher->trigger('onInit','dynamic_recaptcha_1');  
    21. ?>  
    22. <p>  
    23. <span>  
    24. <div id="dynamic_recaptcha_1"></div>  
    25. </span>  
    26. </p>  
    27. <?php  
    28. }  
    29. }  
    30. ?>

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:
  1. Open the file \components\com_jcomments\jcomments.ajax.php (line 275)
  2. Replace the code
    1. if ($acl->check('enable_captcha') == 1) {  
    2. $captchaEngine = $config->get('captcha_engine', 'kcaptcha');  
    3. if ($captchaEngine == 'kcaptcha') {  
    4. require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );  
    5. if (!JCommentsCaptcha::check($values['captcha_refid'])) {  
    6. self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');  
    7. JCommentsCaptcha::destroy();  
    8. $response->addScript("jcomments.clear('captcha');");  
    9. return $response;  
    10. }  
    11. } else {  
    12. $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response));  
    13. // if all plugins returns false  
    14. if (!in_array(true, $result, true)) {  
    15. self::showErrorMessage(JText::_('ERROR_CAPTCHA'));  
    16. return $response;  
    17. }  
    18. }  
    19. }
  3. With this
    1. if ($acl->check('enable_captcha') == 1) {  
    2. $captchaEngine = $config->get('captcha_engine', 'recaptcha');  
    3.  
    4. if ($captchaEngine == 'recaptcha') {  
    5. $post = JRequest::get('post');  
    6. JPluginHelper::importPlugin('captcha');  
    7. $dispatcher = JDispatcher::getInstance();  
    8. $resp = $dispatcher->trigger('onCheckAnswer',$post['recaptcha_response_field']);  
    9.  
    10. if (!$resp[0]) {  
    11. self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');  
    12. $response->addScript('Recaptcha.reload()');  
    13. return $response;  
    14. } else {  
    15. $response->addScript('Recaptcha.reload()');  
    16. }  
    17. } elseif ($captchaEngine == 'kcaptcha') {  
    18. require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );  
    19. if (!JCommentsCaptcha::check($values['captcha_refid'])) {  
    20. self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');  
    21. JCommentsCaptcha::destroy();  
    22. $response->addScript("jcomments.clear('captcha');");  
    23. return $response;  
    24. }  
    25. } else {  
    26. $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response));  
    27. // if all plugins returns false  
    28. if (!in_array(true, $result, true)) {  
    29. self::showErrorMessage(JText::_('ERROR_CAPTCHA'));  
    30. return $response;  
    31. }  
    32. }  
    33. }

Additional
  • To center the ReCaptcha on your form:
    1. Open the file \components\com_jcomments\tpl\default\tpl_form.php (~line 130)
    2. Replace the line <div id="dynamic_recaptcha_1"></div>
    3. With <div id="dynamic_recaptcha_1" style="width:320px;margin:0 auto;"></div>

  • If you notice some CSS misalignments under the ReCaptcha input:
    1. Open the file \components\com_jcomments\tpl\default\tpl_form.php (~line 130)
    2. Find the line
      1. $dispatcher->trigger('onInit','dynamic_recaptcha_1');
    3. With this
      1. $dispatcher->trigger('onInit','dynamic_recaptcha_1');  
      2.  
      3. // for minor css tweaks  
      4. $document = JFactory::getDocument();  
      5. $jcomments_recaptcha_css='.recaptchatable .recaptcha_r7_c1,.recaptchatable .recaptcha_r8_c1{top:-2px;position: relative;}';  
      6. $document->addStyleDeclaration($jcomments_recaptcha_css);
  • An alternative to Google's ReCaptcha but modifying the same files is AreYouAHuman's PlayThru

Source(s):


Article Comments (5)

Friday, 16th December 2016
0 Votes
Gravatar for Tibbe
Tibbe
Hi there,

This looks like a great solution to get rid of spam comments.
How to integrate this in Joomla 3.x? Step 2 I did in settings.xml, but no result in frontend.

Kind Regards,
Tibbe

Tuesday, 31st May 2016
2 Votes
Gravatar for trake
trake
Still working with the last version? I cant make it work.

Monday, 23rd March 2015
3 Votes
Gravatar for Graham
Graham
Worked like a charm first time! :lol: Getting bored of the spam comments. This should stop them. :P

Wednesday, 7th May 2014
4 Votes
Gravatar for Fabien
Fabien
Hi! Thank you for the tip, it works very well!

Fab

Friday, 28th March 2014
5 Votes
Gravatar for Kman
Kman
You are a rockstar mate! thanks. Followed the steps listed and it worked!

If only all tutorials were this good :P


Recent Comments

Gravatar for Art
Oracle: order by subquery missing right parenthesis
Hello, what if I would like to add rownum to that code, but in my case it's all is subquery? How can I do this ?

20 Jan


Gravatar for Robert
CharIndex Reverse - find occurrence starting from end of string in TSQL
Very helpful thanks! This worked for me as well, and is a little shorter. Not sure if there are drawbacks. SELECT RIGHT(@Haystack,CHARINDEX(@Delimiter,REVERSE(@Haystack))-LEN(@Delimiter))

29 Dec


Gravatar for Translation

27 Dec


Gravatar for Tibbe
JComments 2.3.0 with ReCaptcha in Joomla 2.5.x
Hi there, This looks like a great solution to get rid of spam comments. How to integrate this in Joomla 3.x? Step 2 I did in settings.xml, but no result in frontend. Kind Regards, Tibbe

16 Dec


Gravatar for sach|n
SSRS Repeat Headers in PDF Report
Yes its working for me..thnx Joel.

7 Oct