using CAPTCHA

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
quartney
Forum Members
Forum Members
Posts: 37
Joined: Mon Oct 16, 2006 3:26 pm

using CAPTCHA

Post by quartney »

I am trying to integrate the CAPTCHA module into a sign-up form for Newsletter Made Simple.  I have inserted code from the CAPTCHA help tab:

Code: Select all

// create a reference to a Captcha module object
$captcha = &$this->getModuleInstance('Captcha'); 
// show the captcha image
echo $captcha->getCaptcha();

// create a reference to a Captcha module object
$captcha = &$this->getModuleInstance('Captcha');
// check the user input (the checkCaptcha method will return TRUE if $input is correct, FALSE if $input is incorrect)
$validated = $captcha->checkCaptcha($input);
What that does is put a captcha image on the page, but I need help:
1. putting a text box on the page where the captcha image text should be input
2. validating the input (I think)

action.default.php:

Code: Select all

<?
// create a reference to a Captcha module object
$captcha = &$this->getModuleInstance('Captcha'); 
// show the captcha image
echo $captcha->getCaptcha();

// create a reference to a Captcha module object
$captcha = &$this->getModuleInstance('Captcha');
// check the user input (the checkCaptcha method will return TRUE if $input is correct, FALSE if $input is incorrect)
$validated = $captcha->checkCaptcha($input);

    $message = '';
    if( isset($params['message']) )
      {
	$message = $params['message'];
      }
 
    $this->smarty->assign('prompt_email',$this->Lang('emailaddress'));
    $this->smarty->assign('error',$params['error']);
    $this->smarty->assign('submitbtn', 
			  $this->CreateInputSubmit($id, 'submit', 
						   $this->Lang('submit')));
    $template = 'nms_subscribeform';

    $mode = 'subscribe';
    if(isset($params['mode']) && $params['mode'] != '')
      {
	$mode = $params['mode'];
      }

    switch( $mode ) {
    case 'usersettings':
      {
	$this->smarty->assign('error',$params['error']);
	$this->smarty->assign('formstart',
			      $this->CreateFrontendFormStart($id, $returnid, 'usersettings_email' ));
	$this->smarty->assign('formend',$this->CreateFormEnd());
	$this->smarty->assign('email', $this->CreateInputText($id, 'email','',30, 150 ));
	$this->smarty->assign('formhidden', $this->CreateInputHidden($id, 'usersettings'));
	$template = 'usersettings_form';
      }
      break;

    case 'unsubscribe':
      {
	$this->smarty->assign('error',$params['error']);
	$this->smarty->assign('formstart',$this->CreateFrontendFormStart($id, $returnid, 'unsubscribe_email'));
	$this->smarty->assign('formend',$this->CreateFormEnd());
	$this->smarty->assign('email', $this->CreateInputText($id, 'email','',30, 150 ));
	$this->smarty->assign('formhidden', $this->CreateInputHidden($id, 'unsubscribe'));
	$template = 'nms_unsubscribeform';
      }
      break;

    case 'subscribe':
      {
	if( isset( $params['displayform'] ) && $params['displayform'] == 0 )
	  {
	    // nothing
	  }
	else
	  {
	    $lists = "";
	    if( isset( $params['select'] ) && $params['select'] != "" )
	      {
		$ar = explode(",",$params['select']);
		$lists = '("'.implode('","',$ar).'")';
	      }
	    $email = '';
	    if( isset( $params['email'] ) && $params['email'] != "" )
	      {
		$email = $params['email'];
	      }
	    $username = '';
	    if( isset( $params['username'] ) && $params['username'] != "" )
	      {
		$username = $params['username'];
	      }

	    $db = &$this->cms->db;
	    $result = array();
	    $temparray = array();
	
	    $query = "SELECT * FROM ".cms_db_prefix().$this->table_prefix."list 
                       WHERE active = ? 
                         AND public = ?";
	    if( $lists != "" )
	      {
		$query .= " AND name IN ".$lists;
	      }
	    $query .= " ORDER BY listid";

	    $dbresult = $db->Execute($query,array(1,1));
	
	    if ($dbresult && $dbresult->RecordCount() > 0)
	      {
		$oneonly = ($dbresult->RecordCount() == 1);
		while ($row = $dbresult->FetchRow())
		  {
		    $extratext = '';
		    if( $oneonly )
		      {
			$hidden .= $this->CreateInputHidden($id,"lists[]",$row['listid']);
			$extratext = 'checked="checked" disabled="true"';
		      }
		    $temparray[] = "<label>" . 
		      $this->CreateInputCheckbox($id, "lists[]",$row['listid'],"",$extratext).
		      $row['name'] . " - " . $row['description'] . "</label>";
		  }
		$this->smarty->assign('listids',$temparray);
		$this->smarty->assign('formstart',$this->CreateFrontendFormStart($id, $returnid, 'do_create_new_user','post','',true,'',$params));
		$this->smarty->assign('formend',$this->CreateFormEnd());
		$this->smarty->assign('email', $this->CreateInputText($id, 'email',$email,30, 150 ));
		$this->smarty->assign('prompt_username',$this->Lang('name'));
		$this->smarty->assign('username', $this->CreateInputText($id, 'username', $username, 30, 150 ));
		$this->smarty->assign('formhidden', $hidden );
	      }
	    else
	      {
		$message = $this->Lang('nolists');
	      }
	  }
      }
      break;

    default:
      $this->_DisplayErrorPage($id,$params,$returnid,
			       $this->Lang('invalidparam'));
      return;
    }

    // Display the populated template
    $smarty->assign('message',$message);
    echo $this->ProcessTemplateFromDatabase($template);
?>
Last edited by quartney on Sat Sep 06, 2008 2:39 pm, edited 1 time in total.
Dee
Power Poster
Power Poster
Posts: 1197
Joined: Sun Mar 19, 2006 8:46 pm
Location: the Netherlands

Re: using CAPTCHA

Post by Dee »

To enable captcha in NMS-2.2 (requires Captcha-0.3.2 if you use the hn_captcha library):

1. assign smarty variables for the captcha image, prompt text and form input:
In action.default.php line 139 after

Code: Select all

$this->smarty->assign('formhidden', $hidden );
add

Code: Select all

$captcha = &$this->getModuleInstance('Captcha');
if ($captcha) { 
	$this->smarty->assign('captcha', $captcha->GetCaptcha());
	$this->smarty->assign('prompt_captcha', 'Enter the text from the image');
	$this->smarty->assign('captcha_input', $this->CreateInputText($id, 'captcha_input','',30, 50 ));
}
2. add the captcha image, prompt text and input to the template:
In your NMS "Subscribe" template before

Code: Select all

        {$submitbtn}
add

Code: Select all

        {if $captcha ne ""}
	    {$captcha}<br />
            {$prompt_captcha}: {$captcha_input}<br />
        {/if}
3. validate the user input after submit:
In action.do_create_new_user.php on line 77 after

Code: Select all

  if( !isset($params['lists']) || count($params['lists']) == 0 )
    {
      $params['message'] = $this->Lang('error_selectonelist');
      $params['error'] = 1;
      $this->Redirect( $id, $action, $returnid, $params, !$admin );
      return;
    }
add

Code: Select all

if (!$admin) {
  $captcha = &$this->getModuleInstance('Captcha');
  if ($captcha && $captcha->checkCaptcha($params['captcha_input']) != TRUE)
  {
    $params['message'] = 'Captcha error';
    $params['error'] = 1;
    $this->Redirect( $id, $action, $returnid, $params, !$admin );
    return;
  }
}
4. register the captcha_input parameter:
In NMS.module.php on line 385 after     

Code: Select all

$this->SetParameterType('usersettings',CLEAN_STRING);
add

Code: Select all

$this->SetParameterType('captcha_input',CLEAN_STRING);
Regards,
D
Last edited by Anonymous on Thu Nov 27, 2008 11:01 am, edited 1 time in total.
quartney
Forum Members
Forum Members
Posts: 37
Joined: Mon Oct 16, 2006 3:26 pm

Re: using CAPTCHA

Post by quartney »

D, thank you so much for your reply!  However, it's still not working.  There is not a box on the page for user input:
http://www.winyahrivers.org/newsletter.html

Do you have a suggestion on how to fix that?

thanks again,
Courtney
quartney
Forum Members
Forum Members
Posts: 37
Joined: Mon Oct 16, 2006 3:26 pm

Re: using CAPTCHA

Post by quartney »

Sorry!  I misunderstood your directions.  For clarification, in case anyone else needs this information, the instructions above reading:
In your NMS "Subscribe" template before
mean make the changes inside CMS Newsletter Made Simple under the "subscribe" tab ... not in the actual file on the server.

You rock, Dee!!!
saltydog
Power Poster
Power Poster
Posts: 281
Joined: Fri Mar 04, 2005 9:06 am

Re: using CAPTCHA

Post by saltydog »

The modification in action_do_create_new_user.php breaks the possibility to manually add a new user from inside the CMS!
Dee
Power Poster
Power Poster
Posts: 1197
Joined: Sun Mar 19, 2006 8:46 pm
Location: the Netherlands

Re: using CAPTCHA

Post by Dee »

saltydog wrote: The modification in action_do_create_new_user.php breaks the possibility to manually add a new user from inside the CMS!
Replace this line added to action.do_create_new_user.php

Code: Select all

if ($captcha && $captcha->checkCaptcha($params['captcha_input']) != TRUE)
by

Code: Select all

if (!$admin && $captcha && $captcha->checkCaptcha($params['captcha_input']) != TRUE)
(I modified the instructions)

Regards,
D
saltydog
Power Poster
Power Poster
Posts: 281
Joined: Fri Mar 04, 2005 9:06 am

Re: using CAPTCHA

Post by saltydog »

very good. Thanks.
tristan
Dev Team Member
Dev Team Member
Posts: 375
Joined: Tue May 02, 2006 10:58 am
Location: The Netherlands

Re: using CAPTCHA

Post by tristan »

Does anyone know if this still works on the latest version of NMS and Captcha or is this already integrated into NMS?
beattie
Forum Members
Forum Members
Posts: 98
Joined: Sun Dec 21, 2008 11:35 pm

Re: using CAPTCHA

Post by beattie »

Hi Dee, can the above code be modified to work with News module's form template? so that human users on the front end can submit news articles and not spammers? I use the {news action="fesubmit"} smarty tag extensively in one of my sites and have tried different means to integrate captcha but without success. Would really appreciate your suggestions.
Locked

Return to “Modules/Add-Ons”