<?php
	require_once('source/session.php');
	require_once('source/error.php');
	require_once('source/dbconfig.php');
	require_once('source/function.php');
	
	if(isset($_POST['signin']))
	{
		try {
			// INSERT RECORD
			$email	   		= strip_tags($_POST['username']);
			$pass	   		= strip_tags($_POST['password']);
			$password  		= md5(md5($salt . md5($pass)) . $salt);

			
			//Get Duplicate Username OR Email
			$STH = $DBH->prepare("SELECT * FROM tbl_user WHERE email = :email");
			$STH->bindParam(':email', $email);
			$STH->execute();
			$row = $STH->fetch(PDO::FETCH_ASSOC);
			
			
			if($STH->rowCount() == 1)
			{
				
				if($password == $row['password'])
				{
					
					$rows2 = sql($DBH, "SELECT * FROM tbl_user WHERE email = ?", array($email), "rows");							
					foreach($rows2 as $row2)
					{	
						$user_status 	  		= $row2['status'];
						$user_t 	  		= $row2['teacher'];
						if($user_status == "Inactive"){
							redirect('index2.php?msg=inactive');
						}else if($user_t == "true"){
							redirect('index.php?msg=st_failed');
						}
					}
					
					switch($row['access_level'])
					#1) User
					{
						###########Case No:1
						#1) User
						case "User":
						
						session_regenerate_id();
						#MD5 Database Details
						$_SESSION['SESS_ID_MD']  				= md5($salt . $row['id']);
						$_SESSION['SESS_PROFILE_PIC_MD']  		= md5($salt . $row['profile_pic']);
						$_SESSION['SESS_FULLNAME_MD']  		    = md5($salt . $row['fullname']);
						$_SESSION['SESS_EMAIL_MD']  	  	    = md5($salt . $row['email']);
						$_SESSION['SESS_PASSWORD_MD']  			= md5($salt . $row['password']);
						$_SESSION['SESS_BITCOIN_WALLET_MD']   	= md5($salt . $row['bitcoin_wallet']);
						$_SESSION['SESS_BANK_NAME_MD']   	 	= md5($salt . $row['bank_name']);
						$_SESSION['SESS_ACCOUNT_NAME_MD']   	= md5($salt . $row['account_name']);
						$_SESSION['SESS_ACCOUNT_NUMBER_MD']   	= md5($salt . $row['account_number']);
						$_SESSION['SESS_PHONE_MD']   	 		= md5($salt . $row['phone']);
						$_SESSION['SESS_COUNTRY_MD']   	 		= md5($salt . $row['country']);
						$_SESSION['SESS_REGISTRATION_DATE_MD']  = md5($salt . $row['registration_date']);
						$_SESSION['SESS_ACCESS_LEVEL_MD']   	= md5($salt . $row['access_level']);
						$_SESSION['SESS_HASH_MD']   	 	  	= md5($salt . $row['hash']);
						$_SESSION['SESS_EMAIL_VERIFIED_MD']   	= md5($salt . $row['email_verified']);
						$_SESSION['SESS_STATUS_MD']   	 		= md5($salt . $row['status']);
						$_SESSION['SESS_TOKEN_MD']   	 		= md5($salt . $token);
						
						#NORMAL Database Details
						$_SESSION['SESS_ID']  					= $row['id'];
						$_SESSION['SESS_PROFILE_PIC']  			= $row['profile_pic'];
						$_SESSION['SESS_FULLNAME']  		    = $row['fullname'];
						$_SESSION['SESS_EMAIL']  	  	    	= $row['email'];
						$_SESSION['SESS_PASSWORD']  			= $row['password'];
						$_SESSION['SESS_BITCOIN_WALLET']   		= $row['bitcoin_wallet'];
						$_SESSION['SESS_BANK_NAME']   	 		= $row['bank_name'];
						$_SESSION['SESS_ACCOUNT_NAME']   		= $row['account_name'];
						$_SESSION['SESS_ACCOUNT_NUMBER']   		= $row['account_number'];
						$_SESSION['SESS_PHONE']   	 			= $row['phone'];
						$_SESSION['SESS_COUNTRY']   	 		= $row['country'];
						$_SESSION['SESS_REGISTRATION_DATE']  	= $row['registration_date'];
						$_SESSION['SESS_ACCESS_LEVEL']   		= $row['access_level'];
						$_SESSION['SESS_HASH']   	 	  		= $row['hash'];
						$_SESSION['SESS_EMAIL_VERIFIED']   		= $row['email_verified'];
						$_SESSION['SESS_STATUS']   	 			= $row['status'];
                        $_SESSION['SESS_S_TYPE']   	 			= $row['s_type'];
						$_SESSION['SESS_TOKEN']   	 			= $token;
						session_write_close();
						
						#Redirect
						$_SESSION['msg'] = "signin";
						redirect('main.php');
						break;

						###########Default Case:
						default:
						#Redirect
						$_SESSION['msg'] = "failed";
						redirect('index.php');
						break;
					}
					//Switch Close
				}
				else
				{
					#Redirect
					$_SESSION['msg'] = "inactive";
					redirect('index2.php');
					//echo "Not Verify";
				}
			}
			else
			{
					$_SESSION['msg'] = "failed";
					redirect('index.php');
					//echo "No Record Found";
			}
		}
		catch(PDOException $e) { 
			file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND); # Errors Log File
				redirect('index.php?msg=failed');
		}
	}
	else
	{
				redirect('index.php?msg=failed');
	}
?>