<?php

session_start();

function registerUser($user,$pass1,$pass2,$secretpassword){
	$errorText = '';
	$secretpassword2 = 'JimmyCleans';
	
	// Check passwords
	if ($pass1 != $pass2) $errorText = '<div class="messages">
				<div id="message-error" class="message message-error">
					<div class="image">
						<img src="images/icons/error.png" alt="Error" height="32" />
					</div>
					<div class="text">
						<h6>Error Message</h6>
						<span>Passwords are not identical!</span>
					</div>
					<div class="dismiss">
						<a href="#message-error"></a>
					</div>
				</div>
			</div>';	
	elseif ($secretpassword != $secretpassword2) $errorText = '<div class="messages">
				<div id="message-error" class="message message-error">
					<div class="image">
						<img src="images/icons/error.png" alt="Error" height="32" />
					</div>
					<div class="text">
						<h6>Error Message</h6>
						<span>You forgot the secret password!</span>
					</div>
					<div class="dismiss">
						<a href="#message-error"></a>
					</div>
				</div>
			</div>';	
	elseif (strlen($pass1) < 6) $errorText = '<div class="messages">
				<div id="message-error" class="message message-error">
					<div class="image">
						<img src="images/icons/error.png" alt="Error" height="32" />
					</div>
					<div class="text">
						<h6>Error Message</h6>
						<span>Password is to short!</span>
					</div>
					<div class="dismiss">
						<a href="#message-error"></a>
					</div>
				</div>
			</div>';	
	// Check user existance	
	$pfile = fopen("userpwd.txt","a+");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            $errorText = '<div class="messages">
				<div id="message-error" class="message message-error">
					<div class="image">
						<img src="images/icons/error.png" alt="Error" height="32" />
					</div>
					<div class="text">
						<h6>Error Message</h6>
						<span>The selected user name is taken!</span>
					</div>
					<div class="dismiss">
						<a href="#message-error"></a>
					</div>
				</div>
			</div>';
            break;
        }
    }
	
    // If everything is OK -> store user data
    if ($errorText == ''){
		// Secure password string
		$userpass = md5($pass1);
    	
		fwrite($pfile, "\r\n$user:$userpass");
		
		$IPaddress = $_SERVER['REMOTE_ADDR'];
		$timezone = new DateTimeZone( "America/New_York" );
		$date = new DateTime();
		$date->setTimezone( $timezone );
		echo  $date->format( 'H:i:s A  /  D, M jS, Y' ); 
		$lfile = fopen("log.txt","a+");
		rewind($lfile);
		fwrite($lfile, "\r\nUser Created - " . $date->format( 'm-d-y H:i:sa T e' ) ." - ". $user ." was created from Ip Address: " . $IPaddress ."."); 
		fclose($lfile);
    }
    
    fclose($pfile);
	

	
	return $errorText;
}
















function loginUser($user,$pass){
	$errorText = '';
	$validUser = false;
	
	// Check user existance	
	$pfile = fopen("userpwd.txt","r");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            // User exists, check password
            if (trim($tmp[1]) == trim(md5($pass))){
            	$validUser= true;
            	$_SESSION['userName'] = $user;
            }
            break;
        }
		
		
    }
    fclose($pfile);
	
	
	
    if ($validUser != true) $errorText = '<div class="messages">
				<div id="message-error" class="message message-error">
					<div class="image">
						<img src="images/icons/error.png" alt="Error" height="32" />
					</div>
					<div class="text">
						<h6>Error Message</h6>
						<span>Invalid username or password!</span>
					</div>
					<div class="dismiss">
						<a href="#message-error"></a>
					</div>
				</div>
			</div>';	

    if ($validUser == true) $_SESSION['validUser'] = true;

    else $_SESSION['validUser'] = false;
	
	
	if($validUser != true){
		$IPaddress = $_SERVER['REMOTE_ADDR'];
		$timezone = new DateTimeZone( "America/New_York" );
		$date = new DateTime();
		$date->setTimezone( $timezone );
		echo  $date->format( 'H:i:s A  /  D, M jS, Y' ); 
		$lfile = fopen("log.txt","a+");
		rewind($lfile);
		fwrite($lfile, "\r\nLogin Error - " . $date->format( 'm-d-y H:i:sa T e' ) ." - Failed Login Attemp from IP Address: " . $IPaddress ."."); 
		fclose($lfile);
	}
	
	if($validUser == true){
		$IPaddress = $_SERVER['REMOTE_ADDR'];
		$timezone = new DateTimeZone( "America/New_York" );
		$date = new DateTime();
		$date->setTimezone( $timezone );
		echo  $date->format( 'H:i:s A  /  D, M jS, Y' ); 
		$lfile = fopen("log.txt","a+");
		rewind($lfile);
		fwrite($lfile, "\r\nLogin Successful - " . $date->format( 'm-d-y H:i:sa T e' ) ." - ". $user ." logged in from IP Address: " . $IPaddress ."."); 
		fclose($lfile);
	}
	
	return $errorText;	
	
}

function logoutUser(){
	unset($_SESSION['validUser']);
	unset($_SESSION['userName']);
	
	
}

function checkUser(){
	if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
		
		header('Location: login.php');
		
	}
	
	
	
}

function renameUser(){
     
			if ($_SESSION['userName'] == "helixson"){$_SESSION['userName'] = 'Harold';}
			if ($_SESSION['userName'] == "plyswthsqurles"){$_SESSION['userName'] = 'David';}
	}

?>