Print

JComments 2.3.0 with ReCaptcha in Joomla 2.5.x

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
    copyraw
    $captcha[] = JCommentsHTML::makeOption('kcaptcha', 'KCAPTCHA');
    1.  $captcha[] = JCommentsHTML::makeOption('kcaptcha', 'KCAPTCHA')
  3. With this
    copyraw
    $captcha[] = JCommentsHTML::makeOption('kcaptcha', 'KCAPTCHA');
    $captcha[] = JCommentsHTML::makeOption('recaptcha', 'RECAPTCHA');
    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
    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
            }
    }
    ?>
    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
    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
            }
    }
    ?>
    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
    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;
            }
        }
    }
    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
    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;
            }
        }
    }
    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
Source(s):
Category: Joomla :: Article: 553