<?php if (MING != "merciless") require ('index.php'); else {

////////////////////////////////////////////
//
// CAPTCHA Module
// for Planet Mongo
//
// Purpose: Administer a CAPTCHA to keep robots out of certain pages or from spamming.
// Author: Michael Bethencourt
// License: GPL 2.0
//
// License URL: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
// Planet Mongo URL: http://planetmongo.sf.net/
// Author URL: http://michaelb.org/
////////////////////////////////////////////




////////////////////////////////
// Rejection message.
$captcha_reject_message = 'You entered an incorrect value for the human-validation system. 
To continue, hit the "back" button on your browser and try again.';
////////////////////////////////

/********************** Not very useful settings are contained here. *****************************/

$captcha_weak_default_field_name = 'mailaddress'; // cleverly disguised as something else
$captcha_weak_default_value = 'Dale Arden';
$captcha_blank_code = '[blank]';
$captcha_hidden_input_html = '
<input type="hidden" name="captcha_type" value="%type%" />
<input type="hidden" name="captcha_hash" value="%hash%" />';
$captcha_invisible_template = $captcha_hidden_input_html.'
<span style="display: none; visible: none;">
	<strong>Do not change this value. Leave it as it is.</strong>
	<textarea name="%name%" rows="2" cols="60">%value%</textarea>
</span>
';
$captcha_weak_template = $captcha_hidden_input_html.'
<label for="%name%-field">Enter the text "%value%" into the blank to prove that you are not a robot: 
</label><input id="%name%-field" type="text" name="%name%" value="" />
</span>
';
/*************************************************************************************************/





// Generates hash. $value is the correct answer, $type is the captcha type
function captcha_generate_hash ($value, $type){
	global $main_salt;
	return md5("$main_salt|$value|$type");
}

function captcha_output_captcha($template, $type, $correct_value, $name=FALSE){
	if($correct_value == '') {
		global $captcha_blank_code;
		$correct_value = $captcha_blank_code;
	}
	if($name===FALSE) {
		global $captcha_weak_default_field_name;
		$name = $captcha_weak_default_field_name;
	}
	$hash = captcha_generate_hash($correct_value, $type);
	$replace_array = Array(
		'%type%' => $type,
		'%value%' => $correct_value,
		'%hash%' => $hash,
		'%name%' => $name
	);
	$output = main_replace($template, $replace_array);
	outblurt($output);
}



function captcha_weak_or_invisible($default_value, $type) {
	global $captcha_weak_default_value;
	if($default_value === FALSE) $default_value = $captcha_weak_default_value;
	captcha_output_captcha($GLOBALS["captcha_$type".'_template'], 'weak', trim($default_value));
}

function captcha_weak_doaction($default_value = FALSE) { 
	captcha_weak_or_invisible($default_value, 'weak');
}

function captcha_invisible_doaction($default_value = FALSE){
	captcha_weak_or_invisible($default_value, 'invisible');
	//global $captcha_invisible_template, $captcha_weak_default_value;
	//if($default_value === FALSE) $default_value = $captcha_weak_default_value;
	//captcha_output_captcha($captcha_invisible_template, 'invisible', trim($default_value));
}

function captcha_catch_doaction($type='hashed'){
	$type = trim($type);
	if($type=='hashed') {
		global $captcha_reject_message, $captcha_weak_default_field_name;
		$value = $_POST[$captcha_weak_default_field_name];
		$hash = $_POST['captcha_hash'];
		$type = $_POST['captcha_type'];
		if(trim($hash) == trim(captcha_generate_hash($value, $type))) captcha_accept();
		else captcha_reject($captcha_reject_message);
	}
}


// Currently, this function does nothing.
function captcha_accept ($mesg='') { return TRUE; }

function captcha_reject ($mesg='') {
	global $user_be_extra_secure_with_protected_page;
	if(strlen($mesg)>0)	outblurt($mesg);
	if($user_be_extra_secure_with_protected_page!==FALSE) main_stop_all();
	else main_stop_outblurt();
	return FALSE;
}





} ?>
