<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

eval ("\$title_register = \"".gettemplate("title_register")."\";");
echo $title_register;

if($_POST['save']) {

  //register_globals = off

  $username = substr(trim(htmlspecialchars($_POST['username'])), 0, 30);
  $nickname = substr(trim(htmlspecialchars($_POST['nickname'])), 0, 30);
  $pwd1 = $_POST['pwd1'];
  $pwd2 = $_POST['pwd2'];
  $mail = $_POST['mail'];
	$CAPCLASS = new Captcha;
	if(!$CAPCLASS->check_captcha($_POST['captcha'], $_POST['captcha_hash'])) $error[]="The security code was wrong!";

  // prüfung username
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."user WHERE username = '$username' ");
	$num = mysql_num_rows($ergebnis);
	if($num) $error[]="username already in use!";
  //prüfung nickname1
         $ergebnis = safe_query("SELECT * FROM ".PREFIX."user WHERE nickname = '$nickname' ");
	$num = mysql_num_rows($ergebnis);
	if($num) $error[]="nickname already in use!";

  // prüfung mail
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."user WHERE email = '$mail' ");
	$num = mysql_num_rows($ergebnis);
	if($num) $error[]="mailadress already in use!";

	if(!(strlen(trim($username)))) $error[]="you have to enter a username!";
	elseif( strlen(trim($username)) > 30 ) $error[]="your username is too long! (max 30 chars)";

	// prüfung passwort
	if($pwd1 == $pwd2) {
	    if(!(strlen(trim($pwd1)))) $error[]="you have to enter a password!";
	}
	else $error[]="your repeated password is not valid!";

	// prüfung e-mail
	$sem = '^[a-z0-9_\.-]+@[a-z0-9_-]+\.[a-z0-9_\.-]+$';
	if(!(eregi($sem, $mail))) $error[]="your e-mail is not valid!";

	// prüfung nickname2
	if(!(strlen(trim($nickname)))) $error[]="you have to enter your nickname!";

	if(is_array($error)) {
		echo'<b>There has been errors!</b><br><br>';
		foreach($error as $err) {
			echo'<li>'.$err.'</li>';
		}
		echo'<br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">';
	}
	else {
		// insert in db
		$md5pwd = md5($pwd1);
		$registerdate=time();

	  $activationkey = createkey(20);

		safe_query("INSERT INTO `".PREFIX."user` (`registerdate`, `lastlogin`, `username`, `password`, `nickname`, `email`, `newsletter`, `activated`) VALUES ('$registerdate', '$registerdate', '$username', '$md5pwd', '$nickname', '$mail', '1', '".$activationkey."')");

		$insertid = mysql_insert_id();

		// insert in user_groups
		safe_query("INSERT INTO ".PREFIX."user_groups ( userID ) values('$insertid' )");

		// mail an user
		$ToEmail = $mail;
		$ToName = $username;
		$header = "Account Information: ".PAGETITLE;
		$Message = 'Hello '.$username.'!

Your registration was successful.

Your Username: '.$username.'
Your Passwort: '.$pwd1.'

To complete your registration please click on the following link:
http://'.$hp_url.'/index.php?site=register&key='.$activationkey.'

Thank you for registration
'.PAGETITLE.' - '.$hp_url;

		if(mail($ToEmail,$header, $Message, "From:".$admin_email."\r\nX-Mailer: PHP/" . phpversion())) echo'Your registration has been successful, you will get an information-mail in seconds.<br>You have to activate your account still by activation link from E-Mail. After that you can log in.';
		else 'There was a problem while sending mail. Please contact the webmaster.';
	}
}
elseif($_GET['key']) {

	safe_query("UPDATE `".PREFIX."user` SET activated='1' WHERE activated='".$_GET['key']."'");
	if(mysql_affected_rows()) redirect('index.php?site=login','Your account has been activated successfully.<br>You are now able to login.');
	else redirect('index.php?site=login','Your activation key ist wrong!');

}
else {
	$bg1=BG_1;
	$bg2=BG_2;
	$bg3=BG_3;
	$bg4=BG_4;

	$CAPCLASS = new Captcha;
	$captcha = $CAPCLASS->create_captcha();
	$hash = $CAPCLASS->get_hash();
	$CAPCLASS->clear_oldcaptcha();

	eval ("\$register = \"".gettemplate("register")."\";");
    echo $register;
}

?>