<?php

header( ' Content-Type: text/html; charset=utf-8' );

$config_file = "../cgi-bin/config";
$codes_file = "../captcha/codes.txt";
$www_output_dir = "/captcha";

$comment = "#";

$fp = fopen($config_file,"r");

while (!feof($fp)) {
   $line = trim(fgets($fp));
   if ($line && !ereg("$comment", $line)) {
      $pieces = explode("=", $line);
      $option = trim($pieces[0]);
      $value = trim($pieces[1],"\" \t\n\r\0\x0B");
      $config_values[$option] = $value;
   }
}
fclose($fp);

require($config_values[bbpress_dir] . '/bb-load.php');

require_once( BBPATH . BBINC . 'registration-functions.php');

$profile_info_keys = get_profile_info_keys();

$user_login = $user_safe = true;

if ( isset($_GET['user_login'] )) :
	$user_login = bb_user_sanitize  ( $_GET['user_login'], true );
	$user_email = bb_verify_email( $_GET['user_email'] );
	$user_url   = bb_fix_link( $_GET['user_url'] );
	$code   = $_GET['code'];
	$captcha_code   = $_GET['captcha_code'];

        $fc = fopen($codes_file,"r");

        $code_ok = false;
        while (!feof($fc)) {
           $line = trim(fgets($fc));
           if ($line && !ereg("$comment", $line)) {
              $pieces = explode("::", $line);
              $code_date = trim($pieces[0]);
              $code_md5 = trim($pieces[1]);
	      if ($code && ($code_date == 0) && ($code == $code_md5)) {
	         $code_ok = true;
	      }
           }
	   if (!$code_ok) $new_data=$new_data."\n".$line;
        }
	fclose($fc);
	
	if ( !$code_ok ) {
             echo "<font color=red>There was an error processing the request, please try again or contact the webmaster.</font><br>";
             echo "<br>code=$code";
	     exit(2);
	} else {
           $fc = fopen($codes_file,"w");
           fwrite($fc,$new_data);
	   fclose($fc);
	}

	foreach ( $profile_info_keys as $key => $label ) :
		if ( is_string($$key) ) :
			$$key = attribute_escape( $$key );
		elseif ( is_null($$key) ) :
			$$key = attribute_escape( $_POST[$key] );
		endif;
		if ( !$$key && $label[0] == 1 ) :
			$bad_input = true;
			$$key = false;
		endif;
	endforeach;

	if ( empty($user_login) || bb_user_exists($user_login) )
		$user_safe = false;
	
	if ( $user_login && $user_safe && $user_email && !$bad_input) :
		$user_id = bb_new_user( $user_login, $user_email, $user_url );
		foreach( $profile_info_keys as $key => $label )
			if ( strpos($key, 'user_') !== 0 && $$key !== '' )
				bb_update_usermeta( $user_id, $key, $$key );
		do_action('register_user', $user_id);
		
		echo "OK";
//		bb_load_template( 'register-success.php' );
		exit();
	else :
 	     echo "<input type=hidden name=crypt value=\"$captcha_code\" id=crypt><br><table border=0><tr>";
             echo "<td>Please, enter the code in the image on the right:";
	     echo "<INPUT TYPE=text name=code id=code size=5></td>";
	     echo "<td align=\"center\"><img src=\"$www_output_dir/$captcha_code.png\"><br>";
             echo "<a href=\"javascript://\" onclick=javascript:formScript.update();><font size=-1>load a new code</font></a>";
             echo "</td></tr></table>\n";
             echo "Please correct the following errors and submit the form again:";
             echo "<br><font color=red>user alias is already used; please specify a different one</font><br>";
	     exit();
	endif;
endif;

exit(1);

?>