<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}
	
	global $Handler;
	
	
	//Final html being returned
	$string = null;
	if( isset( $_POST['cp_settings'] ) ) 
	{
		if( isset( $_POST['password'] ) && isset( $_POST['pwchange'] )  && isset( $_POST['password1'] ) ) 
		{
			$pass = $_POST['password'];
			$passNew = $_POST['password1'];
			if( $pass <> $passNew ) 
			{
				if(  strlen( $pass ) >= 5 && strlen( $pass) <= 16 && strlen( $passNew ) >= 5 && strlen( $passNew ) )
				{
					if( $Handler->user->CheckPWCorrect( $_SESSION['UserID'] , $pass ) == true )
					{
                                            $this->Handler->WriteLog( 'Pass Change' , 'Success' , md5( $pass . SALT ) , md5( $passNew . SALT ) );
						$Handler->user->UpdatePass( md5( $passNew . SALT ) , null );
						$Handler->theme->SetValue( 'MESSAGE' , $Handler->theme->GetValue( 'SUCCESS_CHANGE_PW' ) );
						$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
						$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=cp&do=settings" );
						$Handler->theme->Parse( 'success.html' );
						exit;
					}
					else
					{
                                            $this->Handler->WriteLog( 'Pass Change' , 'Failed' , md5( $pass . SALT ) , md5( $passNew . SALT ) , "Incorrect Password" );
						$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BAD_INFO' ) );
						$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
						$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=cp&do=settings&action=pw" );
						$Handler->theme->Parse( 'error.html' );
						exit;
					}
				} 
				else
				{
					$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BAD_LENGTH_PASS' ) );
					$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
					$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=cp&do=settings&action=pw" );
					$Handler->theme->Parse( 'error.html' );
					exit;
				}
			}
			else
			{
				$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_PW_SAME' ) );
				$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
				$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=cp&do=settings&action=pw" );
				$Handler->theme->Parse( 'error.html' );
				exit;
			}
		}
	}
	elseif( isset( $_GET['action'] ) )
	{
		switch( $_GET['action'] )
		{
			default:
				$string .= '<div style="text-align:center">';
				$string .= '<a href="index.php?view=cp&do=settings&action=pw"><h3 style="font-family:Verdana;">Change Password</h3></a>';
				$string .= '</div>';
				$Handler->theme->SetValue( 'CONTENT' , $string );
				break;
			case "pw":
					$Handler->theme->SetValue( 'CONTENT' ,  $Handler->theme->QuietParse( 'pwchange.html' ) );
				break;
		}
	}
	else
	{
		$string .= '<div style="text-align:center">';
		$string .= '<a href="index.php?view=cp&do=settings&action=pw"><h3 style="font-family:Verdana;">Change Password</h3></a>';
		$string .= '</div>';
		$Handler->theme->SetValue( 'CONTENT' , $string );
	}
	
?>