Page 1 of 1

using CAPTCHA

Posted: Wed Sep 03, 2008 5:39 pm
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);
?>

Re: using CAPTCHA

Posted: Mon Nov 24, 2008 12:18 am
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

Re: using CAPTCHA

Posted: Mon Nov 24, 2008 4:39 pm
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

Re: using CAPTCHA

Posted: Mon Nov 24, 2008 5:05 pm
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!!!

Re: using CAPTCHA

Posted: Thu Nov 27, 2008 10:10 am
by saltydog
The modification in action_do_create_new_user.php breaks the possibility to manually add a new user from inside the CMS!

Re: using CAPTCHA

Posted: Thu Nov 27, 2008 10:59 am
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

Re: using CAPTCHA

Posted: Thu Nov 27, 2008 1:10 pm
by saltydog
very good. Thanks.

Re: using CAPTCHA

Posted: Mon Jan 10, 2011 3:28 pm
by tristan
Does anyone know if this still works on the latest version of NMS and Captcha or is this already integrated into NMS?

Re: using CAPTCHA

Posted: Mon Feb 07, 2011 12:42 am
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.