<?php
session_start();
date_default_timezone_set("Europe/Athens"); 
?>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    require "dbconnect.php";

    $usr = $_POST['username']; 
    $pas = trim($_POST['password']);
    $usr = stripslashes($usr); 
    $pas = stripslashes($pas);
	$usr = mysqli_real_escape_string($con, $usr);
    $pas = mysqli_real_escape_string($con, $pas);
	$dt=date('Y-m-d');
    $_SESSION['islogin']='1';
	//check parameter table for parameters
	$sql1 = $dbcon->prepare("SELECT * FROM parameters " );
	$sql1->execute();
	$row1=$sql1->fetch(PDO::FETCH_ASSOC);
	$hpass=$row1['hpass'];
	$atetrs=$row1['atetrs'];
	$atetre=$row1['atetre'];
	$btetrs=$row1['btetrs'];
	$btetre=$row1['btetre'];
        $gm=$row1['gymnasio'];
	
	
    $sql = $dbcon->prepare("SELECT * FROM teachers WHERE PMP=? " ); 
	$sql->execute([$usr]);
	$count=$sql->rowCount();
	if ($count > 0)
	{
	$row=$sql->fetch(PDO::FETCH_ASSOC);
	$hash=rtrim(ltrim($row['pass']));
	
	if ($hpass==1)
	{	
		$fl=password_verify($pas, $hash);
	}
	else
	{
		if ($pas==$hash) {$fl=true;} else {$fl=false;}
	}
	
  if ($fl) 
  {
        echo "**Επιτυχής επαλήθευση χρήστη";
        $name= $row['Name'];
	$ac=$row['access'];
	
	switch ($gm) 
	{
	  case 1:    $gym= 8;    break;
	  case 2:    $gym= 7;    break;
	  case 3:    $gym= 9;    break;
	}
	if ($row1['pedagogikes']==0) {$_SESSION['pedagogikes']=0;} else {$_SESSION['pedagogikes']=1;}
	$year = date('Y', strtotime($dt));
    $day = date('d', strtotime($dt));
	$month = date('m', strtotime($dt));
	
	$result  = $dbcon->prepare("SHOW TABLES LIKE 'timetablea'");
	$result->execute();
        $tableyes = $result !== false && $result->rowCount() > 0;
		
	if ( $dt >= $atetrs and $dt <= $atetre ) 
	{
		$tetramino = 'Α'; 
		
		if($tableyes)
		{
		$oldn="timetable";
		$newn="timetableb";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		$oldn="timetablea";
		$newn="timetable";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		}
	} 
	elseif ( $dt >= $btetrs and $dt <= $btetre ) 
	{
		$tetramino = 'Β';
		
		if(!$tableyes)
		{
		$oldn="timetable";
		$newn="timetablea";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		$oldn="timetableb";
		$newn="timetable";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		}
	}
	else
		$tetramino = '**';
	
	$result  = $dbcon->prepare("SHOW TABLES LIKE 'stutimetabla'");
	$result->execute();
        $tableyes = $result !== false && $result->rowCount() > 0;
		echo $tableyes;
	if ( $dt >= $atetrs and $dt <= $atetre ) 
	{
		$tetramino = 'Α';
		
		if($tableyes)
		{
		$oldn="stutimetable";
		$newn="stutimetableb";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		$oldn="stutimetablea";
		$newn="stutimetable";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		}
	} 
	elseif ( $dt >= $btetrs and $dt <= $btetre ) 
	{
		$tetramino = 'Β';
		
		if(!$tableyes)
		{
		$oldn="stutimetable";
		$newn="stutimetablea";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		$oldn="stutimetableb";
		$newn="stutimetable";
		mysqli_query($con,"RENAME TABLE `" . $oldn . "` TO `" . $newn . "`");
		}
	}
	
	/////////////////////////////////////// GLOBAL VARIABLES //////////////////
	$_SESSION['tetramino']=$tetramino;
	$_SESSION['access']=$ac;
	$_SESSION['n']=$name;
	$_SESSION['usr']=$usr;
	$_SESSION['pmp']=$usr;
	$_SESSION['dt']=$dt;
	$_SESSION['gym']=$gym;
	$_SESSION['islogin']='1';
	//////////////////////////////////////////////////////////////////////////
	switch ($ac) {
	  case "1":    $menu= "indexteach.php";$menu1= "indexteach1.php";    break;
	  case "2":    $menu= "indexvd.php"; $menu1= "index1.php";   break;
	  case "3":    $menu= "indexvda.php";$menu1= "indexvda1.php";    break;
	  case "4":    $menu= "indexd.php"; $menu1= "indexd1.php";   break;
	  case "5":    $menu= "indexsimv.php";    break;
	  case "6":    $menu= "indexsecretary.php";    break;
	  case "7":    $menu= "indexteachanapl.php";    break;
	  case "8":    $menu= "indexsimvbd.php";    break;
	  case "9":    $menu= "indexadmin.php";    break;
	}
    $_SESSION['menu']=$menu;
	$_SESSION['menu1']=$menu1;
    include $menu;
	
    $_SESSION['anapl']=0;
	$dt2 = date("Y-m-d H:i:s");
	$ip=$_SERVER['REMOTE_ADDR'];
	$sql2 = $dbcon->prepare("INSERT INTO reacons (ip,user,datelog,nots) VALUES (?,?,?,?)");
	$sql2->execute([$ip,$usr,$dt2,1]);
	if ($ac <= 5) {
		include "newmath.php";
		}
	mysqli_close($con);
	$dbcon=null;
  }
  elseif ($usr=='admin')
  {
    
	$sql = mysqli_query($con,"SELECT * FROM admin WHERE pass=$pas" ); 
	$count=mysqli_num_rows($sql);
	$row=mysqli_fetch_array($sql);
	if ( $count > 0)
	{
		echo $row['Name']." : ";
		$sql = $dbcon->prepare("update admin set pass='!2dfgt890876/>' " ); 
	    $sql->execute();
	  $_SESSION['islogin']='1';
	  $menu= "indexadmin.php";
	  $_SESSION['menu']=$menu;
	  $_SESSION['gym']=$gym;
	  $dt2 = date("Y-m-d H:i:s");
	  $ip=$_SERVER['REMOTE_ADDR'];
	  $sql2 = $dbcon->prepare("INSERT INTO reacons (ip,user,datelog,nots) VALUES (?,?,?,?)");
	  $sql2->execute([$ip,$usr,$dt2,1]);
	  echo "ΔΕΝ ΘΑ ΜΠΟΡΕΙΤΕ ΝΑ ΚΑΝΕΤΕ ΞΑΝΑ LOGIN ΜΕ ΑΥΤΟ ΤΟΝ ΚΩΔΙΚΟ. ΤΟ ΣΥΣΤΗΜΑ ΘΑ ΧΡΗΣΙΜΟΠΟΙΕΙ ΤΟ TEACHERS file";
	  include $menu;
	  }
	  else
	  {
		echo " ΛΑΘΟΣ ΚΩΔΙΚΟΣ ADMIN - ΔΟΚΙΜΑΣΤΕ ΞΑΝΑ";
	    $_SESSION['islogin']='';
		include "index.php"; 
	    mysqli_close($con);
	    $dbcon=null;	
	  }
  }
  else
  {
	echo " ΛΑΘΟΣ ΟΝΟΜΑ ΧΡΗΣΤΗ/ΚΩΔΙΚΟΣ - ΔΟΚΙΜΑΣΤΕ ΞΑΝΑ";
	$_SESSION['islogin']='';
	$dt2 = date("Y-m-d H:i:s");
	$ip=$_SERVER['REMOTE_ADDR'];
	$sql2 = $dbcon->prepare("INSERT INTO reacons (ip,user,datelog,nots) VALUES (?,?,?,?)");
	$sql2->execute([$ip,$usr,$dt2,0]);
	include "index.php"; 
	mysqli_close($con);
	$dbcon=null;
    } 
}
else
{
    echo " ΛΑΘΟΣ ΟΝΟΜΑ ΧΡΗΣΤΗ/ΚΩΔΙΚΟΣ - ΔΟΚΙΜΑΣΤΕ ΞΑΝΑ";
	$_SESSION['islogin']='';
	$dt2 = date("Y-m-d H:i:s");
	$ip=$_SERVER['REMOTE_ADDR'];
	$sql2 = $dbcon->prepare("INSERT INTO reacons (ip,user,datelog,nots) VALUES (?,?,?,?)");
	$sql2->execute([$ip,$usr,$dt2,0]);
	
 	include "index.php"; 
	mysqli_close($con);
	$dbcon=null;	
}

}
?>
</body>
</html>