<?php

include './core.php';

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

if( $_REQUEST['mode'] == 'edit' )
{
	if( isset( $_POST['send_form'] ) )
    {
        $form_errors = array();

	    if( !check_fields( 'email', 'style' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	    else
	    {
            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' );
            if( strlen( $_POST['away_msg'] ) > 255 )
            	$form_errors[] = form_error( 'toolong_away_msg' );
	    }

	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	    }
	    else
	    {
        	if( $user->style_id != intval( $_POST['style'] ) && !isset( $_SESSION['style_id'] ) )
            {
            	$style = $MySQL->query_first( sprintf("SELECT * FROM styles WHERE styleID=%s", intval( $_POST['style'] ) ));
            	$Smarty->assign( 'style', $style );
            }

            $user->email			= $_POST['email'];
            $user->show_email		= ( isset( $_POST['show_email'] ) && $_POST['show_email'] == "1" );
            $user->icq				= $_POST['icq'];
            $user->get_newsletter	= ( isset( $_POST['get_newsletter'] ) && $_POST['get_newsletter'] == "1" );
            $user->style_id			= intval( $_POST['style'] );
            $user->away_msg			= $_POST['away_msg'];

            $user->save( 'email', 'icq', 'style_id', 'away_msg', 'flags' );
        }

        //assign_formvalues( 'email', 'show_email', 'icq', 'get_newsletter', 'style', 'away_msg' );
    }
    //else
    //{
    	$formvalues = array();
	    $formvalues['email'] = $user->email;
	    $formvalues['show_email'] = $user->show_email;
	    $formvalues['icq'] = $user->icq;
	    $formvalues['get_newsletter'] = $user->get_newsletter;
	    $formvalues['style'] = $user->style_id;
	    $formvalues['away_msg'] = $user->away_msg;
	    
		if( $user->access['teamspeak'] || $user->rank >= 5 )
		{
	    	$formvalues['teamspeak'] = 1;
		}
		else
		{	
			$formvalues['teamspeak'] = 0;
		}	    	
	    
	    $Smarty->assign( 'formvalues', $formvalues );
    //}

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

	$Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
}
elseif( $_REQUEST['mode'] == 'change_boardlink' )
{
    if( isset( $_POST['send_form'] ) )
    {
        $form_errors = array();

	    if( !check_fields( 'bb_username', 'bb_password' ) )
	        $form_errors[] = form_error( 'uncomplete' );
        else
        {
            if( ( $user->last_boardlink_change + intval( $settings['boardlink_change_interval'] ) ) > time() )
           		$form_errors[] = form_error( 'bbchange_interval' );

            if( $user->rank > 4 )
            	$form_errors[] = form_error( 'bbchange_staff' );

            $bb_account = $MySQL->query_first( sprintf("SELECT userid, password, blocked, activation FROM %s.bb%d_users WHERE username='%s'", $settings['wbb_db'], $n, me_escape( $_POST['bb_username'] ) ));

            if( $bb_account['userid'] > 0 )
            {
            	if( $bb_account['blocked'] == 1 || $bb_account['activation'] != 1 )
                	$form_errors[] = form_error( 'bad_bbuser' );

                if( $bb_account['password'] != md5( $_POST['bb_password'] ) )
                	$form_errors[] = form_error( 'bad_bbpassword' );

                list( $userlinks ) = $MySQL->query_first( sprintf("SELECT COUNT(*) FROM server.accounts WHERE bb_userid = %d", $bb_account['userid'] ));
        		if( $userlinks > 0 )
            		$form_errors[] = form_error( 'bb_already_linked' );
            }
            else
            	$form_errors[] = form_error( 'no_bbuser' );
        }

	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	    }
	    else
	    {
            $user->bb_userid = $bb_account['userid'];
            $user->last_boardlink_change = time();
            $user->refresh_wbb_access();
            $user->refresh_wbb_groupdata();
            $user->save( 'bb_userid', 'last_boardlink_change' );

            display_message( 'bbedit_success', false );
        }

        assign_formvalues( 'bb_username' );
    }

    if( isset( $_POST['delete_link'] ) )
    {
    	if( $user->bb_userid > 0 )
        {
        	resetWBBAccount( $user->bb_userid );
            $user->bb_userid = 0;
        	$user->save( 'bb_userid' );

            display_message( 'bbdelete_success', false );
        }
    }

    $Smarty->display( 'account_edit_change_boardlink.tpl' );
}
elseif( $_REQUEST['mode'] == 'change_password' )
{
    if( isset( $_POST['send_form'] ) )
    {
        $form_errors = array();

	    if( !check_fields( 'old_password', 'new_password', 'new_password_ver' ) )
	        $form_errors[] = form_error( 'uncomplete' );
        else
        {
            if( md5( $_POST['old_password'] ) != $user->password )
            	$form_errors[] = form_error( 'bad_oldpassword' );

            if( $_POST['new_password'] != $_POST['new_password_ver'] )
            	$form_errors[] = form_error( 'password_nomatch' );
        }

	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	    }
	    else
	    {
            $user->password = md5( $_POST['new_password'] );
            $user->save( 'password' );

            $_SESSION['password'] = $user->password;

            if( isset( $_COOKIE['password'] ) && $_COOKIE['password'] != null )
            {
                setcookie( "password", $user->password, ( time() + intval( $settings['cookie_lifetime'] ) ) );
            }

            display_message( 'passwordchange_success', false );
        }
    }

    $Smarty->display( 'account_edit_change_password.tpl' );
}
elseif( $_REQUEST['mode'] == 'change_teamspeakpassword' )
{
	if( isset( $_POST['send_form'] ) )
    {
    	$form_errors = array();
    	
    	if( !( $user->access['teamspeak'] || $user->rank >= 5 ) )
    	{
    		$form_errors[] = form_error( 'Keine Rechte.' );
    		$Smarty->assign( 'form_errors', $form_errors );
    		$Smarty->display( 'main.tpl' );
    		die();
    	}
    	
    	if( !check_fields( 'teamspeak_password' ) )
    	{
	        $form_errors[] = form_error( 'uncomplete' );
	        $Smarty->assign( 'form_errors', $form_errors );
    	}
    	else
    	{
			$dbhandle = sqlite_open( $settings['teamspeak_path'] );	
			if( $error )
			{
				exit( $error );
			}
			
  			$res = sqlite_query( $dbhandle, "SELECT COUNT(*) FROM ts2_clients WHERE s_client_name='".$user->name."' LIMIT 1" );
  			$count = intval( sqlite_fetch_single( $res ) );
  			
			if( $count > 0 )
			{				
				sqlite_query( $dbhandle, "UPDATE ts2_clients SET s_client_password = '".$_REQUEST['teamspeak_password']."' WHERE s_client_name='".$user->name."'" );
			}
			else
			{				
				$tsadmin = 0;
				if( $user->rank > 12 )
				{
					$tsadmin = -1;
				}
				
				sqlite_query( $dbhandle, "INSERT INTO ts2_clients (i_client_server_id, b_client_privilege_serveradmin, s_client_name, s_client_password ) VALUES ( '1', '".$tsadmin."', '".$user->name."', '".$_REQUEST['teamspeak_password']."' )" );
			}
			
    		display_message( 'passwordchange_success', false );	
    		sqlite_close( $dbhandle );
    	}	      
    }
    $Smarty->display( 'account_edit_change_teamspeakpassword.tpl' );
}


?>