<?php
session_start();
include "config.php";
// Generate token




 //$localIP = getHostByName(getHostName()); 
//echo  $localIP;

//echo $_SERVER['REMOTE_ADDR'];
   
   

if(isset($_POST['login'])){

 $uname = mysqli_real_escape_string($link,$_POST['idnumber']);
 $password = mysqli_real_escape_string($link,$_POST['password']);
 $class_name = mysqli_real_escape_string($link,$_POST['classname']);
 $subject_name = mysqli_real_escape_string($link,$_POST['subjectname']);
//echo $uname;
//echo $password;

 if ($uname != "" && $password != "" && $class_name != "" && $subject_name != ""){

  $sql_query = "select count(*) as cntUser from account_class where idNumber='$uname' and idAcess='$password' ";
  $result = mysqli_query($link,$sql_query);
  $row = mysqli_fetch_array($result);

  $count = $row['cntUser'];
  


  if($count > 0){
    $token = getToken(10);
    $_SESSION['username'] = $uname;
    $_SESSION['token'] = $token;

    // Update user token 
	
    $result_token = mysqli_query($link, "select count(*) as allcount from user_token_class where st_id='$uname'");
    $row_token = mysqli_fetch_assoc($result_token);
	//echo $row_token['allcount'];
    if($row_token['allcount'] > 0){
       mysqli_query($link,"update user_token_class set token='$token' where st_id='$uname'");
    }else{
       mysqli_query($link,"insert into user_token_class (st_id,token) values('$uname','$token')");
    }
    header('Location: home.php');
  }else{
    //echo "Invalid username and password";
   $_SESSION['err'] = "<div class='alert alert-danger' role='alert'> <strong>Warning!</strong> Invalid ID and Access Code.</div>";
  }

 } else {
  
     $_SESSION['err'] = "<div class='alert alert-danger' role='alert'>Pls Fill all option.</div>"; 
 }









//include 'config.php';

	$username = $_POST['idnumber'];
	$password = $_POST['password'];
	$classname = $_POST['classname'];
	$subjectname = $_POST['subjectname'];
	
$_SESSION['class_name'] = $classname;
$_SESSION['subject_name'] = $subjectname;
$_SESSION['idnumber'] = $username;

}	

//echo $_SESSION['subject_name'];


mysqli_close($link);
?>




 
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
    <title>Login</title>
     
<!---<meta name="viewport" content="width=device-width, initial-scale=1">--->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
 <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>



<?php



function crypto_rand_secure($min, $max)
{
    $range = $max - $min;
    if ($range < 1) return $min; // not so random...
    $log = ceil(log($range, 2));
    $bytes = (int) ($log / 8) + 1; // length in bytes
    $bits = (int) $log + 1; // length in bits
    $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
    do {
        $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
        $rnd = $rnd & $filter; // discard irrelevant bits
    } while ($rnd > $range);
    return $min + $rnd;
}

function getToken($length)
{
    $token = "";
    $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
    $codeAlphabet.= "0123456789";
    $max = strlen($codeAlphabet); // edited

    for ($i=0; $i < $length; $i++) {
        $token .= $codeAlphabet[crypto_rand_secure(0, $max-1)];
    }

    return $token;
}
?>

    
    <style type="text/css">
   
    
    
        body{ font: 14px sans-serif; }
        
		
		.wrapper {
			width: 470px;
  height: 600px;
  padding: 12px 20px;
  border: 5px outset green;
  background-color: #fefbd8;    
  text-align: center;
  margin-left: 0px;
   margin-top: 5px;
}
.form-control{
	width: 60%;
	 margin-left: 90px;
	 
}

img.frontpic{
	margin-top:-100px;
	margin-left:460px;
	
}
img.logo{
	margin-top: 0px;
	margin-left: 0px;
	
}
body {
  background-image: url('back.png');
  background-repeat: no-repeat;
  background-attachment: fixed;  
  background-size: cover;
}
p.admin{
margin-left: 80px;
margin-top: 15px;
}



.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #4CAF50;
  color: white;
}


.class_routine{
			width: 790px;
  height: 680px;
  padding: 12px 20px;
  
  
  text-align: center;
  margin-left: 500px;
   margin-top: -600px;
}


    </style>
   
</head>
<body>
    
    
<div class="topnav">
  <a href="index.php">Home</a>
  <a class="active" href="index.php">Join Live Class</a>
  <a href="class_routine_show.php">Online Class Routine</a>
  <a href="my_present.php">My Present</a>
  <a href="admin.php">Admin</a>
  <a href="#about">About</a>
</div>

    
    
    
  <!----<img class="logo" src="logo.png" alt="logo" width="120" height="120">--->  
<!---<img class="frontpic" src="college.png" alt="head" width="600" height="80"--->

<center>

    <div class="wrapper">
        <h2>Join Live Class</h2>
        <!----<img class="logo" src="logo.png" alt="logo" width="50" height="50">-->
        <p>Please fill in your credentials to login.</p>
		
        <form id="form_1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="form-horizontal">
		
            <div class="form-group">
 <label>Select Class</label>              	
<select name="classname" class="form-control" >
<option value="">--Select Class--</option>
<option value="ELEVEN">ELEVEN</option>
<option value="TWELVE">TWELVE</option>

</select>
				
</div> 				
		
 <div class="form-group">
<label>Select Subject</label>             	
<select name="subjectname" class="form-control"  >
<option value="">--Select Subject--</option>
<option value="BANGLA">BANGLA</option>
<option value="ENGLISH">ENGLISH</option>
<option value="PHYSICS">PHYSICS</option>
<option value="CHEMISTRY">CHEMISTRY</option>
<option value="HIGHER MATHEMATICS">HIGHER MATHEMATICS</option>
<option value="ICT">ICT</option>
<option value="BIOLOGY">BIOLOGY</option>
<option value="ACCOUNTING">ACCOUNTING</option>
<option value="FINANCE BANKING & INSURANCE">FINANCE BANKING & INSURANCE</option>
<option value="BUSINESS ORGANIZATION AND MANAGEMENT">BUSINESS ORGANIZATION AND MANAGEMENT</option>
<option value="SOCIAL WORK">SOCIAL WORK</option>
<option value="ISLAMIC STUDIES">ISLAMIC STUDIES</option>
<option value="ECONOMICS">ECONOMICS</option>
<option value="GEOGRAPHY">GEOGRAPHY</option>
<option value="STATISTICS">STATISTICS</option>
</select>
			
</div> 				
		

		
			<div class="form-group">
               	
				<label>ID Number</label>
                <input type="text" name="idnumber" class="form-control" placeholder="Please enter ID Number"">
                
            </div>    
            <div class="form-group">
                <label>Access Code</label>
                <input type="password" name="password" class="form-control" placeholder="Please enter the Access Code">
                
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-primary" name ="login" value="Login">
				<span class="help-block"><?php if(isset($_SESSION['err'])){ echo  $_SESSION['err']; }?></span>
			
				<!---<button type="submit" class="btn btn-info btn-block" name="login">Submit</button>--->
            </div>
            
        </form>
    </div>   
	
</center>
</body>
</html>