Location: PHPKode > scripts > NuCaptcha phpBB Plugin > nucaptcha_plugin_1_0_0_pl1/root/includes/captcha/plugins/phpbb_nucaptcha_plugin.php
<?php
/**
*
* @package VC
* @copyright (c) 2011 Patrick Webster
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

if (!class_exists('phpbb_default_captcha', false))
{
	// we need the classic captcha code for tracking solutions and attempts
	include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx);
}

/**
* @package VC
*/
class phpbb_nucaptcha extends phpbb_default_captcha
{
	var $answer;
	var $answer_enter;
	var $answer_exit;

	function init($type)
	{
		global $config, $db, $user;

		$user->add_lang('captcha_nucaptcha');
		parent::init($type);
		
		$this->answer		= request_var('nucaptcha-answer', '');
		$this->answer_enter	= request_var('nucaptcha-answer-enter', 0);
		$this->answer_exit	= request_var('nucaptcha-answer-exit', 0);
	}

	function &get_instance()
	{
		$instance =& new phpbb_nucaptcha();
		return $instance;
	}

	/**
	* Requirements:
	* Windows: PHP 5.3.0+
	* *nix: PHP 5.1.0+
	* mcrypt
	*/
	function is_available()
	{
		global $config, $user, $phpbb_root_path, $phpEx;
		$user->add_lang('captcha_nucaptcha');
		
		if (!isset($config['nucaptcha_clientkey']) || empty($config['nucaptcha_clientkey']))
		{
			return false;
		}

		if ((strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && version_compare(PHP_VERSION, '5.3.0') < 0) || version_compare(PHP_VERSION, '5.1.0') < 0)
		{
			return false;
		}
		
		if (!@extension_loaded('mcrypt'))
		{
			if (!function_exists('can_load_dll'))
			{
				include($phpbb_root_path . 'includes/functions_install.' . $phpEx);
			}

			return can_load_dll('mcrypt');
		}

		if(!class_exists('Leap'))
		{
			include($phpbb_root_path . 'includes/captcha/plugins/nucaptcha_lib/leapmarketingclient.' . $phpEx);
		}
		
		Leap::SetClientKey($config['nucaptcha_clientkey']);
		if(Leap::GetErrorCode() == LMEC_CLIENTKEYNOTSET)
		{
			return false;
		}
		
		return true;
	}

	/**
	*  API function
	*/
	function has_config()
	{
		return true;
	}

	function get_name()
	{
		return 'CAPTCHA_NUCAPTCHA';
	}

	function get_class_name()
	{
		return 'phpbb_nucaptcha';
	}

	function acp_page($id, &$module)
	{
		global $config, $db, $template, $user;

		$captcha_vars = array(
			'nucaptcha_clientkey'	=> 'NUCAPTCHA_CLIENTKEY',
			'nucaptcha_ssl'			=> 'NUCAPTCHA_SSL',
		);

		$module->tpl_name = 'captcha_nucaptcha_acp';
		$module->page_title = 'ACP_VC_SETTINGS';
		$form_key = 'acp_captcha';
		add_form_key($form_key);

		$submit = request_var('submit', '');

		if ($submit && check_form_key($form_key))
		{
			set_config('nucaptcha_clientkey',	request_var('nucaptcha_clientkey', ''));
			set_config('nucaptcha_ssl',			request_var('nucaptcha_ssl', false));

			add_log('admin', 'LOG_CONFIG_VISUAL');
			trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($module->u_action));
		}
		else if ($submit)
		{
			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($module->u_action));
		}
		else
		{
			$template->assign_vars(array(
				'CAPTCHA_PREVIEW'		=> $this->get_demo_template($id),
				'CAPTCHA_NAME'			=> $this->get_class_name(),
				'NUCAPTCHA_CLIENTKEY'	=> (isset($_REQUEST['nucaptcha_clientkey'])) ? request_var('nucaptcha_clientkey', '') : ((isset($config['nucaptcha_clientkey'])) ? $config['nucaptcha_clientkey'] : ''),
				'NUCAPTCHA_SSL'			=> (isset($_REQUEST['nucaptcha_ssl'])) ? request_var('nucaptcha_ssl', '') : ((isset($config['nucaptcha_ssl'])) ? $config['nucaptcha_ssl'] : ''),
				'USE_SSL'				=> (isset($config['nucaptcha_ssl'])) ? $config['nucaptcha_ssl'] : false,
				'U_ACTION'				=> $module->u_action,
			));

		}
	}

	// not needed
	function execute_demo()
	{
	}

	// not needed
	function execute()
	{
	}

	function get_template()
	{
		global $config, $user, $template, $phpbb_root_path, $phpEx;

		if ($this->is_solved())
		{
			return false;
		}
		else
		{
			$available = $this->is_available();
			$player_output = '';
			$persistant_data = '';
			if($available)
			{
				if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
				{
					$use_ssl = true;
				}
				else if(isset($config['nucaptcha_ssl']))
				{
					$use_ssl = (bool) $config['nucaptcha_ssl'];
				}
				else
				{
					$use_ssl = false;
				}

				$t = Leap::InitializeTransaction(null, $use_ssl, null, Leap::PURPOSE_CREATE_ACCOUNT);
				// check if the transaction initialization was successful
				if(LMEC_OK == Leap::GetErrorCode())
				{
					// store the persistent data in the session for validation later
					$persistant_data = $t->GetPersistentDataForPublicStorage('anonymous_user');

					// and get the actual player code
					$player_output = $t->GetWidget();
				}
				else
				{
					// report a problem to the user if transaction initialization failed for some reason
					$player_output = sprintf($user->lang['NUCAPTCHA_ERROR_TRANSACTION'], Leap::GetErrorString());
				}
			}
			
			$explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');

			$template->assign_vars(array(
				'NUCAPTCHA_HTML'			=> $player_output,
				'PERSISTANT_DATA'			=> $persistant_data,
				'S_NUCAPTCHA_AVAILABLE'		=> $available,
				'S_CONFIRM_CODE'			=> true,
				'S_TYPE'					=> $this->type,
				'L_CONFIRM_EXPLAIN'			=> $explain,
			));

			return 'captcha_nucaptcha.html';
		}
	}

	function get_demo_template($id)
	{
		return $this->get_template();
	}

	function get_hidden_fields()
	{
		$hidden_fields = array();

		// this is required for posting.php - otherwise we would forget about the captcha being already solved
		if ($this->solved)
		{
			$hidden_fields['confirm_code'] = $this->code;
		}
		$hidden_fields['confirm_id'] = $this->confirm_id;
		return $hidden_fields;
	}

	function uninstall()
	{
		$this->garbage_collect(0);
	}

	function install()
	{
		return;
	}

	function validate()
	{
		global $config, $phpbb_root_path, $phpEx, $user;

		if (!parent::validate())
		{
			return false;
		}
		
		if ($this->answer == null || strlen($this->answer) == 0 || $this->answer_enter == 0 || $this->answer_exit == 0)
		{
			return $user->lang['NUCAPTCHA_INCORRECT'];
		}
		
		if(!class_exists('Leap'))
		{
			include($phpbb_root_path . 'includes/captcha/plugins/nucaptcha_lib/leapmarketingclient.' . $phpEx);
		}

		if(isset($_POST['leap']) && Leap::WasSubmitted() === true)
		{
			Leap::SetClientKey($config['nucaptcha_clientkey']);
			if(Leap::GetErrorCode() == LMEC_CLIENTKEYNOTSET)
			{
				return false;
			}

			// validate the transaction
			$persistant_data = request_var('leap', '');
			$valid = Leap::ValidateTransaction($persistant_data, false, 'anonymous_user');

			// check for error
			// by default, ValidateTransaction will return true on error
			$error_code = Leap::GetErrorCode();
			if( $error_code != LMEC_CORRECT &&
				$error_code != LMEC_WRONG &&
				$error_code != LMEC_EMPTY )
			{
				add_log('admin', 'NUCAPTCHA_ERROR', Leap::GetErrorCode(), Leap::GetErrorString());
			}

			// check the result
			if ($valid === true)
			{
				$this->solved = true;
				return false;
			}
		}
		
		return $user->lang['NUCAPTCHA_INCORRECT'];
	}
}
?>
Return current item: NuCaptcha phpBB Plugin