i'm not a robot i'm robert

Briefly about the events that led me to the idea of the subject. I originally used kcaptcha - the native captcha of the JComments component. At some point, I turned this captcha off as it has annoyed commentators. Immediately a lot of spam started to appear in the comments. I started using CleanTalk - a nominally paid (just $9 a year) plug-in that protects from spambots not only comments, but also login/registration, contacts and forms of many third-party extensions. Spam has ceased, but recently spammers began posting garbage comments, disguised as normal ones and even containing article headers in their text. CleanTalk cannot filter them out yet. 4-5 such "comments" began to arrive every day - not very critical, and they can be easily deleted right from notifications in the mail. But still I wanted to find a compromise solution to the problem - a captcha which will block the spam not catched by CleanTalk but at the same time not irritating potential commentators.

NO CAPTCHA reCAPTCHA from Google (or just reCAPTCHA v2.0) fits perfectly. Starting with Joomla 3.4, the second version is included in the reCAPTCHA plugin that comes with the CMS. At the time of this writing, almost two years have passed since that release, but JComments developers still do not support this plugin in their component. This does not deprecate its merits - JComments was and remains the best free solution for comments on Joomla-site. Moreover, it's quite easy to integrate the subject into the comments on your own. To do this, you need to perform a number of simple actions.

  1. If your website is not registered in reCAPTCHA, go to the service page, click the Get reCAPTCHA button and add the domain name of the site. (Of course, you need to have an account with Google.) You will receive two keys - public and private. Note that if you previously used the first version of reCAPTCHA, the keys for it do not fit the second version.
  2. In the site administration area, open Extensions > Plugins, find the CAPTCHA - reCAPTCHA plugin, and click on its name. In the plug-in settings, select version 2.0, enter both keys obtained from Google, set the style and size. Enable the plugin if it's off. Save the settings.
  3. Edit two files of the JComments component. You will need to find the specified original pieces of code and replace them with customized ones. The clickable screenshots of the comparison results in WinMerge with line numbers and highlighted changes will clearly show where and what to change (original code is on the right panel, the modified one is on the left panel).

    Modification 1.

    Edit the file components\com_jcomments\tpl\default\tpl_form.php. Find the following piece of code:

    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
        }
    }
    ?>

    Replace it with:

    if ($this->getVar('comments-form-captcha', 0) == 1) {
     
      $captchaEngine = 'recaptcha'; //or 'kcaptcha'
     
      if ($captchaEngine == 'recaptcha') {
        JPluginHelper::importPlugin('captcha');
        $dispatcher = JDispatcher::getInstance();
        $dispatcher->trigger('onInit','dynamic_recaptcha_1');
        $recaptcha = $dispatcher->trigger('onDisplay', array(null, 'dynamic_recaptcha_1', 'class=""'));
    ?>
    <div id="comments-form-captcha-holder">
      <?php echo (isset($recaptcha[0])) ? $recaptcha[0] : ''; ?>
    </div>
    <?php	
      } else if ($captchaEngine == 'kcaptcha') {
        $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
        }
      }
    }
    ?>

    The comparison result:

    Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments - modification 1

    Modification 2.

    Edit the code of the file components\com_jcomments\jcomments.ajax.php. There is only one line to replace:

    $captchaEngine = $config->get('captcha_engine', 'kcaptcha');

    Replacement code:

    $captchaEngine = 'recaptcha'; //or 'kcaptcha'
     
    if ($captchaEngine == 'recaptcha') {
      $post = JRequest::get('post');  
      JPluginHelper::importPlugin('captcha');
      $dispatcher = JDispatcher::getInstance();
      $result = $dispatcher->trigger('onCheckAnswer', $post['recaptcha_response_field']);
      $response->addScript('grecaptcha.reset();');
     
      if (!in_array(true, $result, true)) {
        self::showErrorMessage(JText::_('ERROR_RECAPTCHA_V2'), 'captcha');
        return $response;
      }
    } else

    The comparison result:

  4. If the commentator forgot to click on the captcha, an error message appears. For the message text, the default is the ERROR_CAPTCHA constant from the component language file. Its value is - "Please enter security code displayed in the image!". Since picture with the code is not presented anymore, I replaced the message with a more appropriate one - "Please verify you're not a robot!" and assigned its text to a new constant ERROR_RECAPTCHA_V2. Why I decided to establish a new language constant rather than to override the value of the existing one, and how and where it should be added, is explained in the next article.
  5. To fit the captcha into the comment block on the page, set the style of the <div id="comments-form-captcha-holder"> element in the stylesheet file of your active template. For example, I just aligned it vertically by adding the following code to the css file of my template:
    div#comments-form-captcha-holder {
        margin-top: 16px !important;
        margin-bottom: 20px !important;
    }
  6. To enable captcha output for the corresponding user groups, open the Components > JComments menu, go to Settings, and then click the Permissions tab. For the selected user groups (I recommend for ALL), check the Enable CAPTCHA checkbox. In the Layout tab, the only value in the CAPTCHA drop-down list is KCAPTCHA. Here it does not affect anything, the choice between the old kcaptcha and the new convenient reCAPTCHA v2.0 is implemented in the modified code. If for some reason you want to return to the native captcha, just replace the following line:
    $captchaEngine = 'recaptcha'; //or 'kcaptcha'

    in both files with different line:

    $captchaEngine = 'kcaptcha'; //or 'recaptcha'

In conclusion, let me remind you that customizations will have to be restored if new builds or versions of the JComments component are released. But, in my opinion, this is not too much inconvenience. Firstly, it takes a few minutes to recover the changes, and secondly, the developers do not update their product too often. Nonetheless, be attentive.

Add comment


By working with this site, you agree to our use of cookies necessary to keep the settings you select, as well as for the normal operation of Google services.
More Information OK