<?php

##################################################################################
## This is the file that handles all gateway authentications in to student portal platform
## file name:index.php :a stand for admin
## author: Aslapai asuleimanlapai@yahoo.com
## date: 14/5/2015
## date_modified:
## do not change anything on this page unless u are told to do so
##
#########################################################





//clear the buffer start
ob_start();



//start session
session_start();
//  This notice MUST stay intact for legal use





$time_out = time() + 216000;



//Connect to database from here
require_once('../../../includes/configuration.php');


// Get the user current ip
$user_ip = getenv('REMOTE_ADDR');



//get the posted values
$username=htmlspecialchars($_POST['username'],ENT_QUOTES);
$password= $_POST['password'];
//$hashed_password=md5($password);


// $sq = "SELECT swapped_prog_status, programme_type_applied_for, first_name FROM applicants_nce WHERE application_number = '$username' AND session = '1'";
//
// $sr = mysqli_query($dbc, $sq);
//
// if ($sr) {
// 	$row = mysqli_fetch_assoc($sr);
// 	$swap_status = $row['swapped_prog_status'];
//     $fname = $row['first_name'];
//     $pty = $row['programme_type_applied_for'];
//
// 	//if applicant programme type has not been changed
// 	if ( $pty == 2 ) {
//

		//now validating the username and password
		$q = "SELECT * FROM applicants_nce WHERE (jamb_no = '$username') AND  (first_name = '$password' OR middle_name = '$password' OR other_names = '$password') AND user_status = '1'
			 AND session = '1'"; //cart_id = 0 for now




		$r = mysqli_query($dbc, $q);

		if(@mysqli_num_rows($r) == 1)
		{


			//return yes

			//echo $username;


			//pass the values to coookies and sessions
			$_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
			$user_id =$_SESSION['id'];



			//pass some to cookies
			setcookie('verify-studentttt', md5($username), $time_out, '/');

			//collect user id
			setcookie("listdntttT", $user_id ,  $time_out, '/');



			//update the db info
			$qry = "UPDATE applicants_nce SET online_status = 1, last_login = NOW(), last_login_ip = '$user_ip'
				  WHERE id = '$user_id'";

			$rst = mysqli_query($dbc, $qry);


			echo 1;



			//So need to clear password protector variables
			//unset($_POST['username']);
			//unset($_POST['password']);
			//unset($_POST['act']);

			//$url = 'gateway.php';
			//header("Location: gateway.php");
			//head the guy from the js page



		}
		else
		{

			//return no
			echo 0;
		}
	// }else if($swap_status == 1 && $pty == 1){
	// 	$fname = $row['first_name'];
	// 	echo "<br>
	// 		<h5>Dear <b>$fname</b>, You have been admitted into <b>PRENCE</b></h5>.
	//
	// 		<li class='list-group-item'>
	// 			<a class='btn btn-primary'  style='width:100%;' href='http://coeminna-edu.ng/prence/app_2/' data-toggle='tooltip' title='Click here to Log into the PRENCE portal'><i class='glyphicon glyphicon-info'> </i> &nbsp; Kindly Click here to Log into the PRENCE portal
	// 			</a>
	// 		</li>
	// 		";
	//
	// }else{
	//     	echo 0;
	// }
//}
?>
