<?php

function captcha_showpage(){

	force_authentication();
	$canIhaveAccess = 0;
	$canIhaveAccess = $canIhaveAccess + checklevel('god');

	if($canIhaveAccess == 1)
	{

		global $main_smarty, $the_template;

		$navwhere['text1'] = 'Captcha';
		$navwhere['link1'] = URL_captcha;

		define('pagename', 'captcha'); 
		$main_smarty->assign('pagename', pagename);

		$main_smarty = do_sidebar($main_smarty, $navwhere);

		if(isset($_REQUEST['action'])){$action = $_REQUEST['action'];}else{$action = '';}

		if($action == 'enable'){
			if(isset($_REQUEST['captcha'])){$captcha = $_REQUEST['captcha'];}else{$captcha = '';}
			enable_captcha($captcha);
		}

		if($action == 'configure'){

			if(isset($_REQUEST['captcha'])){$captcha = $_REQUEST['captcha'];}else{$captcha = '';}

			include_once(captcha_captchas_path . '/' . $captcha . '/main.php');
			captcha_configure();

			$main_smarty->assign('tpl_center', captcha_tpl_path . '../captchas/' . $captcha . '/captcha_configure');
			$main_smarty->display($the_template . '/pligg.tpl');
			die();
		}


		if($action == 'EnableReg'){

			$value = (isset($_REQUEST['value'])) ? $_REQUEST['value'] : '';

			if($value != ''){
				misc_data_update('captcha_reg_en', $value);
			}

			header('Location: ' . URL_captcha);

		}

		$captcha = get_misc_data('captcha_method');
		if($captcha == ''){$captcha = 'default';}
		$main_smarty->assign('captcha_method', $captcha);

		$main_smarty->assign('tpl_center', captcha_tpl_path . '/captcha_home');
		$main_smarty->display($the_template . '/pligg.tpl');
	}
}

function enable_captcha($captcha){

	include_once(captcha_captchas_path . '/' . $captcha . '/main.php');

	if(captcha_can_we_use()){
		misc_data_update('captcha_method', $captcha);
		//captcha_admin();
	}	

}

function captcha_register(&$vars){

	if(captcha_reg_enabled == true){

		global $main_smarty, $the_template;

		$captcha = get_misc_data('captcha_method');
		if($captcha == ''){$captcha = 'default';}

		include_once(captcha_captchas_path . '/' . $captcha . '/main.php');
		captcha_create('', 0);

	}

}

function captcha_register_check_errors(&$vars){
	if(captcha_reg_enabled == true){

		global $main_smarty, $the_template;

		$captcha = get_misc_data('captcha_method');
		if($captcha == ''){$captcha = 'default';}

		$username = $vars['username'];
		$email = $vars['email'];
		$password = $vars['password'];

		$main_smarty->assign('username', $username);
		$main_smarty->assign('email', $email);
		$main_smarty->assign('password', $password);

		include_once(captcha_captchas_path . '/' . $captcha . '/main.php');
		if(captcha_check($vars, 2)){
		} else {
			$vars['error'] = true;
		}

	}
}
?>
