<?php

require_once('config/config.php');
require_once("foo/functions.php");
dbconnect();
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

//check if the current password was entered
if ($_POST['cpin'] == "") { redirect('pins.php?error=' . urlencode('You must enter your current PIN'));} 
elseif ($_POST['pin1']  == "") { redirect('pins.php?error=' . urlencode('You must enter your New PIN'));} 
elseif ($_POST['pin2']  == "") { redirect('pins.php?error=' . urlencode('You must Confirm your New PIN'));} 
elseif ($_POST['pin1'] !== $_POST['pin2'] ) {  redirect('password.php?error=' . urlencode('Your PIN entries do not Match!!'));} 

elseif (pin_exist($_SESSION['user_login'], $_POST['cpin'])) {
		update_pin($_SESSION['user_login'], $_POST['pin1']);
		log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $_POST['pin1'], 'User PIN Successfully Changed');
		session_destroy();
		redirect(SIGN_OUT_URL);
	}
	else {
		redirect('pins.php?error=' . urlencode('Wrong Existing PIN'));
	}
}

?>