<?php

##################################################################################
## This is the file that handles all gateway authentications in the admin platform
## file name:index.php :a stand for admin
## author: Umaru Abbas Dikko abbasumaru44"gmail.com
## date: 14/5/2020
## date_modified:
## do not change anything on this page unless u are told to do so
##
#########################################################

session_start();
include_once '../../../db_connect/db.php';
$username = htmlspecialchars($_POST['appNumber'], ENT_QUOTES);
$type_id = $_POST['type_id'];
$password = mysqli_real_escape_string($conn_1, $_POST['password']);
$p_password = mysqli_real_escape_string($conn_1, md5($password));

$user_ip = $_SERVER['REMOTE_ADDR'];
if($type_id== 1)
{
	$sqlmg = mysqli_query($conn_1, "SELECT * FROM students_prence
							  WHERE (`number` = '$username' AND `password` = '$p_password')
							  AND user_status = '1' AND hostel_eligibility='0'
							  ");
	if ($sqlmg) {

		$sqlmg_row = mysqli_num_rows($sqlmg);

	    if ($sqlmg_row > 0) {

			$row = mysqli_fetch_assoc($sqlmg);

	        $student_id = $row['id'];

	        $_SESSION['student_id'] = $student_id;
	        $_SESSION['last_time']=time();
						$_SESSION['type_id'] = $type_id;

	        $qry = "UPDATE students_prence SET online_status = 1, last_login = NOW(), last_login_ip = '$user_ip'
																	  WHERE id = '$student_id'";

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

	        echo 1;

	    } else {

	        echo '<div class="alert alert-danger">
	  <strong>Error!</strong> Invalid Login details
	</div>';
	    }

	}
}else if($type_id== 2)
{
	$sqlmg = mysqli_query($conn_1, "SELECT * FROM students_nce
							  WHERE (`number` = '$username' AND `password` = '$p_password')
							  AND user_status = '1' AND hostel_eligibility='0'
							  ");
	if ($sqlmg) {

		$sqlmg_row = mysqli_num_rows($sqlmg);

	    if ($sqlmg_row > 0) {

			$row = mysqli_fetch_assoc($sqlmg);

	        $student_id = $row['id'];

	        $_SESSION['student_id'] = $student_id;
	        $_SESSION['last_time']=time();
					$_SESSION['type_id'] = $type_id;
	        $qry = "UPDATE students_nce SET online_status = 1, last_login = NOW(), last_login_ip = '$user_ip'
																	  WHERE id = '$student_id'";

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

	        echo 1;

	    } else {

	        echo '<div class="alert alert-danger">
	  <strong>Error!</strong> Invalid Login details
	</div>';
	    }

	}
}else if($type_id== 3)
{
	$sqlmg = mysqli_query($conn_2, "SELECT * FROM students_nce
							  WHERE (`number` = '$username' AND `password` = '$p_password')
							  AND user_status = '1' AND hostel_eligibility='0'
							  ");
	if ($sqlmg) {

		$sqlmg_row = mysqli_num_rows($sqlmg);

	    if ($sqlmg_row > 0) {

			$row = mysqli_fetch_assoc($sqlmg);

	        $student_id = $row['id'];
					$_SESSION['type_id'] = $type_id;
	        $_SESSION['student_id'] = $student_id;
	        $_SESSION['last_time']=time();

	        $qry = "UPDATE students_nce SET online_status = 1, last_login = NOW(), last_login_ip = '$user_ip'
																	  WHERE id = '$student_id'";

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

	        echo 1;

	    } else {

	        echo '<div class="alert alert-danger">
	  <strong>Error!</strong> Invalid Login details
	</div>';
	    }

	}
}else if($type_id== 4)
{
	$sqlmg = mysqli_query($conn_2, "SELECT * FROM students_udus
							  WHERE `number` = '$username' 
							  AND user_status = '1' AND hostel_eligibility='0'
							  ");
	if ($sqlmg) {

		$sqlmg_row = mysqli_num_rows($sqlmg);

	    if ($sqlmg_row > 0) {

			$row = mysqli_fetch_assoc($sqlmg);

	        $student_id = $row['id'];

	        $_SESSION['student_id'] = $student_id;
					$_SESSION['type_id'] = $type_id;
					$_SESSION['last_time']=time();
	        $qry = "UPDATE students_udus SET online_status = 1, last_login = NOW(), last_login_ip = '$user_ip'
																	  WHERE id = '$student_id'";

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

	        echo 1;

	    } else {

	        echo '<div class="alert alert-danger">
	  <strong>Error!</strong> Invalid Login details
	</div>';
	    }

	}
}else if($type_id== 5)
{
	$sqlmg = mysqli_query($conn_1, "SELECT * FROM summer_students
							  WHERE `number` = '$username'
							  AND user_status = '1' AND hostel_eligibility='0'
							  ");
	if ($sqlmg) {

		$sqlmg_row = mysqli_num_rows($sqlmg);

	    if ($sqlmg_row > 0) {

			$row = mysqli_fetch_assoc($sqlmg);

	        $student_id = $row['id'];

	        $_SESSION['student_id'] = $student_id;
			$_SESSION['type_id'] = $type_id;
			$_SESSION['last_time']=time();
	        $qry = "UPDATE summer_students SET online_status = 1, last_login = NOW(), last_login_ip = '$user_ip'
																	  WHERE id = '$student_id'";

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

	        echo 1;

	    } else {

	        echo '<div class="alert alert-danger">
	  <strong>Error!</strong> Invalid Login details
	</div>';
	    }

	}
}else if($type_id== 6)
{
	echo '<div class="alert alert-danger">
<strong>Error!</strong> Try again later
</div>';
}

?>
