ReCaptcha alternative: Integrate PlayThru in JComments 3.0 for Joomla 3.2

Applies to: What?
This article is a follow on from my article JComments 2.3 with PlayThru (Joomla 2.5.x) but this is for a later version of the components using the alternative called PlayThru by AreYouAHuman.Com.

Why?
The kCaptcha used by the jComments extension is easily automated and no longer blocks spam comments. Google's ReCaptcha is overrun by click farms and is unable to authenticate engaging visitors.

How?
So I have come up with what I consider a pretty stable solution. I adapted it from various legacy solutions when using Google's ReCaptcha.


Note: There is a Joomla plugin for AYAH and this works for the user registration form. I installed it but the below details how to integrate the AYAH library with jComments. I will look at a solution using the installed plugin later.

STEP #1: Signup at AYAH
You need to have a publisher and scoring key, sign up for FREE at AreYouAHuman.Com: Basic Signup:
  1. Signup and you will be redirected to a dashboard.
  2. On the dashboard, note the Publisher's key (eg. "c518c95f01312c9ac97ace9ace199ace0c38d9d")
  3. On the dashboard, note the Scoring key (eg. "64d6d442b7e846acfaacef642e1ad173b35503")
  4. On the dashboard, check the site URL matches the domain you are trying this on (eg. if you are testing on "http://test.mycompany.com" then the site specified in the AYAH dashboard should be "test.mycompany.com" and not "www.mycompany.com")

STEP #2: Download AYAH files
Download the latest files from https://portal.areyouahuman.com/installation/php (If you want my cached download - v1.1.8: click here)
  1. Extract the ZIP file to a folder (should be about 4 php files)
  2. Modify the file ayah_config.php to match the publisher and scoring key given to you. > Save
  3. Upload the files to \components\com_jcomments\libraries\ayah

STEP #3: Enable admin option dropdown
This is a code change to allow the admin to select PlayThru.
  1. Open the file \administrator\components\com_jcomments\models\forms\settings.xml (line 398)
  2. Replace the line
    copyraw
    <field
                    name="captcha_engine"
                    type="jcommentscaptcha"
                    class="inputbox"
                    default="recaptcha"
                    label="AP_FORM_FIELD_CAPTCHA"
                    description="">
                <option value="kcaptcha">KCAPTCHA</option>
    	 </field>
    1.  <field 
    2.                  name="captcha_engine" 
    3.                  type="jcommentscaptcha" 
    4.                  class="inputbox" 
    5.                  default="recaptcha" 
    6.                  label="AP_FORM_FIELD_CAPTCHA" 
    7.                  description=""> 
    8.              <option value="kcaptcha">KCAPTCHA</option> 
    9.       </field> 
  3. With this
    copyraw
    <field
                    name="captcha_engine"
                    type="jcommentscaptcha"
                    class="inputbox"
                    default="recaptcha"
                    label="AP_FORM_FIELD_CAPTCHA"
                    description="">
                <option value="kcaptcha">KCAPTCHA</option>
    	  <option value="playthru">PLAYTHRU</option>
            </field>
    1.  <field 
    2.                  name="captcha_engine" 
    3.                  type="jcommentscaptcha" 
    4.                  class="inputbox" 
    5.                  default="recaptcha" 
    6.                  label="AP_FORM_FIELD_CAPTCHA" 
    7.                  description=""> 
    8.              <option value="kcaptcha">KCAPTCHA</option> 
    9.        <option value="playthru">PLAYTHRU</option> 
    10.          </field> 

STEP #4: Set to PlayThru in admin setting
We've made the change, now login and set it to PlayThru
  1. Login to your Joomla Administration panel
  2. Components > JComments > Settings > Layout > CAPTCHA > PLAYTHRU
  3. Save the change.

STEP #5: Display PlayThru in comment form
This step displays the PlayThru on the form (note this code may need to be reverted if the admin does not want to use PlayThru anymore)
  1. Open the file \components\com_jcomments\tpl\default\tpl_form.php (line 126)
  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.   
    4.          if ($this->getVar('comments-form-captcha', 0) == 1) { 
    5.              $html = $this->getVar('comments-form-captcha-html')
    6.              if ($html != '') { 
    7.                  echo $html
    8.              } else { 
    9.                  $link = JCommentsFactory::getLink('captcha')
    10.  ?> 
    11.  <p> 
    12.      <span> 
    13.          <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 /> 
    14.          <span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br /> 
    15.          <input class="captcha" id="comments-form-captcha" type="text" name="captcha_refid" value="" size="5" tabindex="6" /><br /> 
    16.      </span> 
    17.  </p> 
    18.  <?php 
  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 {
                     JPluginHelper::importPlugin('captcha'); 
                     require_once(JCOMMENTS_SITE.'/libraries/ayah/'.'ayah.php'); 
                     $ayah = new AYAH(); 
                     echo $ayah->getPublisherHTML(); 
                }
            }
    ?>
    1.  <?php 
    2.          } 
    3.   
    4.          if ($this->getVar('comments-form-captcha', 0) == 1) { 
    5.              $html = $this->getVar('comments-form-captcha-html','kcaptcha')
    6.              if ($html == 'kcaptcha') { 
    7.                  $link = JCommentsFactory::getLink('captcha')
    8.  ?> 
    9.  <p> 
    10.      <span> 
    11.          <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 /> 
    12.          <span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br /> 
    13.          <input class="captcha" id="comments-form-captcha" type="text" name="captcha_refid" value="" size="5" tabindex="6" /><br /> 
    14.      </span> 
    15.  </p> 
    16.  <?php 
    17.              } else { 
    18.                   JPluginHelper::importPlugin('captcha')
    19.                   require_once(JCOMMENTS_SITE.'/libraries/ayah/'.'ayah.php')
    20.                   $ayah = new AYAH()
    21.                   echo $ayah->getPublisherHTML()
    22.              } 
    23.          } 
    24.  ?> 

STEP #6: Process submitted PlayThru
  1. Open the file \components\com_jcomments\jcomments.ajax.php (line 227)
  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.  { 
    3.      $captchaEngine = $config->get('captcha_engine', 'kcaptcha')
    4.      if ($captchaEngine == 'kcaptcha') 
    5.      { 
    6.          require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' )
    7.          if (!JCommentsCaptcha::check($values['captcha_refid'])) 
    8.          { 
    9.              self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha')
    10.              JCommentsCaptcha::destroy()
    11.              $response->addScript("jcomments.clear('captcha');")
    12.              return $response
    13.          } 
    14.      } 
    15.      else 
    16.      { 
    17.          $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response))
    18.          // if all plugins returns false 
    19.          if (!in_array(true, $result, true)) 
    20.          { 
    21.              self::showErrorMessage(JText::_('ERROR_CAPTCHA'))
    22.              return $response
    23.          } 
    24.      } 
    25.  } 
  3. With this
    copyraw
    if ($acl->check('enable_captcha') == 1) {
    	$captchaEngine = $config->get('captcha_engine', 'playthru');
    
    	if ($captchaEngine == 'playthru'){
    		$post = JRequest::get('post');
    		JPluginHelper::importPlugin('captcha');
    		$dispatcher = JDispatcher::getInstance();
    		$score=false;
    
    		// AreYouAHuman stuff
    		require_once(JCOMMENTS_SITE.'/libraries/ayah/'.'ayah.php');
    		$ayah = new AYAH();
    		$score = $ayah->scoreResult();
    		if ($score===false){
    			self::showErrorMessage('Sorry, but we were not able to verify you as human. Please refresh the page and play the game below.');
    			return $response;
    		}
    	} else if ($captchaEngine == 'kcaptcha') {
    		require_once( JCOMMENTS_SITE.'jcomments.captcha.php' );
    		if (!JCommentsCaptcha::check($values['captcha_refid']))	{
    			self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
    			JCommentsCaptcha::destroy();
    			$response->addScript("jcomments.clear('captcha');");
    			return $response;
    		}
    	}
    }
    1.  if ($acl->check('enable_captcha') == 1) { 
    2.      $captchaEngine = $config->get('captcha_engine', 'playthru')
    3.   
    4.      if ($captchaEngine == 'playthru'){ 
    5.          $post = JRequest::get('post')
    6.          JPluginHelper::importPlugin('captcha')
    7.          $dispatcher = JDispatcher::getInstance()
    8.          $score=false
    9.   
    10.          // AreYouAHuman stuff 
    11.          require_once(JCOMMENTS_SITE.'/libraries/ayah/'.'ayah.php')
    12.          $ayah = new AYAH()
    13.          $score = $ayah->scoreResult()
    14.          if ($score===false){ 
    15.              self::showErrorMessage('Sorry, but we were not able to verify you as human. Please refresh the page and play the game below.')
    16.              return $response
    17.          } 
    18.      } else if ($captchaEngine == 'kcaptcha') { 
    19.          require_once( JCOMMENTS_SITE.'jcomments.captcha.php' )
    20.          if (!JCommentsCaptcha::check($values['captcha_refid']))    { 
    21.              self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha')
    22.              JCommentsCaptcha::destroy()
    23.              $response->addScript("jcomments.clear('captcha');")
    24.              return $response
    25.          } 
    26.      } 
    27.  } 

Additional:
  • Need to look at a more integrated solution using the AYAH plugin
  • BUG: If a user attempts to submit the form without playing the game, and then attempts to complete the game, it will not register. The user needs to refresh the page, play the game and then re-submit the form.
  • Joomla 3.4.x: Note the slash between JCOMMENTS_SITE. and '/libraries/... Previously not required but as of Joomla 3.4.x, this will not work without it as it refers to /jcommentslibraries/ instead of /jcomments/libraries.
Category: Joomla :: Article: 563

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.