<?php

include './core.php';

if( isset( $user ) ) access_error();

if( isset( $_POST['send_form'] ) )
{
	flood_protect( 'register' );

    $form_errors = array();

    if( !check_fields( 'name', 'password', 'password_ver', 'email', 'style' ) )
        $form_errors[] = form_error( 'uncomplete' );
    else
    {
    	$account_test = $MySQL->query( sprintf("SELECT accountid FROM server.accounts WHERE name = '%s'", me_escape( $_POST['name'] ) ));
        $file_fu = file( 'forbidden_accountnames.txt' );

        if( strlen( $_POST['name'] ) < 3 || strlen( $_POST['name'] ) > 20 )
        	$form_errors[] = form_error( 'name_length' );
        if( mysql_num_rows( $account_test ) > 0 )
        	$form_errors[] = form_error( 'name_chosen_already' );
        if( !preg_match( "=^[a-zA-Z0-9-_']+$=i", stripslashes( $_POST['name'] ) ) )
        	$form_errors[] = form_error( 'name_chars' );
        if( strlen( $_POST['password'] ) < 6 || strlen( $_POST['password'] ) > 20 )
        	$form_errors[] = form_error( 'password_length' );
        if( $_POST['password'] != $_POST['password_ver'] )
        	$form_errors[] = form_error( 'password_nomatch' );
        if( strlen( $_POST['email'] ) > 150 )
            $form_errors[] = form_error( 'toolong_email' );
        if( !eregi( '@' , $_POST['email'] ) || !eregi( '.' ,$_POST['email'] ) )
            $form_errors[] = form_error( 'unvalid_email' );
        if( isset( $_POST['icq'] ) && strlen( $_POST['icq'] ) > 12 )
            $form_errors[] = form_error( 'toolong_icq' );
        for ( $i = 0; $i < count( $file_fu ); $i++ )
        {
			if( trim( $file_fu[$i] ) == stripslashes( $_POST['name'] ) || strcasecmp( stripslashes( $_POST['name'] ), $file_fu[$i]) == 0 )
        		$form_errors[] = form_error( 'forbidden_name' );
        }
    }

    if( count( $form_errors ) > 0 )
    {
        $Smarty->assign( 'form_errors', $form_errors );
    	assign_formvalues( 'name', 'email', 'style', 'icq', 'show_email', 'get_newsletter' );
    }
    else
    {
    	$naccount = new Account( 0, true );
        $naccount->name = $_POST['name'];
        $naccount->password = md5( $_POST['password'] );
        $naccount->email = $_POST['email'];
        $naccount->show_email = ( isset( $_POST['show_email'] ) && $_POST['show_email'] == '1' );
        if( isset( $_POST['icq'] ) && !empty( $_POST['icq'] ) )
        	$naccount->icq = $_POST['icq'];
        $naccount->get_newsletter = ( isset( $_POST['get_newsletter'] ) && $_POST['get_newsletter'] == '1' );
        $naccount->style_id = intval( $_POST['style'] );
        $naccount->access['block'] = false;
        $naccount->access['activation'] = false;
        $naccount->rank = 1;

        $naccount->insert();

        $exp_time = time() + intval( $settings['flood_protect_register'] );
        $MySQL->query( sprintf("INSERT INTO flood_protect (ip_address, performed_action, expire) VALUES ('%s','register',%d)", $_SERVER['REMOTE_ADDR'], $exp_time ));

        $activation_code = generate_code();
        $MySQL->query( sprintf("INSERT INTO account_activation_codes (accountid, activationID) VALUES (%d, %d)", $naccount->ID, $activation_code ));

        $mailenv = array();
        $mailenv['name'] = $naccount->name;
        $mailenv['activation_code'] = $activation_code;
        $mailenv['activation_url'] = combine_path( CORE_URL, 'register.php?activation=1&account='.$naccount->ID.'&code='.$activation_code );

        if( send_mail( $naccount->email, 'activation' ) )
        	display_message( 'account_created' );
        else {
        	// TODO: Logging
        	display_message( 'account_created_noemail' );
        }
    }
}
elseif( isset( $_REQUEST['activation'] ) && $_REQUEST['activation'] == '1' )
{
	$account_activation = $MySQL->query_first( sprintf("SELECT * FROM account_activation_codes WHERE accountid = %d && activationID = %d", intval( $_REQUEST['account'] ), intval( $_REQUEST['code'] ) ));
	if( $account_activation['accountid'] > 0 )
    {
    	$aaccount = new Account( (int) $account_activation['accountid'] );
        $aaccount->activation = true;
        $aaccount->save( 'flags' );

        $MySQL->query( sprintf("DELETE FROM account_activation_codes WHERE accountid = %d", $aaccount->ID ));

        display_message( 'activation_success' );
    }
    else
    	display_message( 'unvalid_activation' );
}

$page['content'] = $Smarty->fetch( 'register.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>