<?php 
    // continue the session
    session_start();
    
    // Determine if we are bypassing the protector or not
    if ($_SESSION['bypassProtector']==="True"){
        // Set it back to false until others deem it necessary
        $_SESSION['bypassProtector']="False";
    }
    else {
        require_once dirname(__FILE__) . '/database.php';

        /******************************************************************************/
        // Set the login failed to false at first
        $_SESSION['failedLogin']='FALSE';

        // Init some variables
        $currentUserRow=null;
        $loggingInUserRow=null;
        $userName = '';
        $password = '';

        // Create and Setup the database
        $db = new Database();
        $db->setupTmsDB();

        // Attempt to log in with this user
        $currentlyLoggedInUser = $db->getLoggedInSessionUser(session_id());

        // Determine if the session user exists
        if (mysqli_num_rows($currentlyLoggedInUser)==1){
            // Attempt to get the current user row
            $currentUserRow = $currentlyLoggedInUser->fetch_assoc();

            // Determine if the user name has been set
            if (isset($_POST['restrictedAreaLoginID'])){
                // Attempt to get the username
                $userName = htmlspecialchars($_POST['restrictedAreaLoginID'], ENT_QUOTES, 'UTF-8');

                // Check first of all to determine if they are even the same user
                if ($userName!=$currentUserRow["UserName"]){
                    showForm("You are not the same user as is currently logged in.");
                    exit();
                }

                // Determine if the password has been set
                if (isset($_POST['restrictedAreaPassword'])){
                    // Attempt to get the password
                    $password = htmlspecialchars($_POST['restrictedAreaPassword'], ENT_QUOTES, 'UTF-8');

                    // Check to see that the password matches the logged in user's password
                    if ($password!=$currentUserRow["Password"]){
                        showForm("Password does not match this user ID.");
                        exit();
                    }
                }
                else {
                    showForm("A password must be entered.");
                    exit;
                }
            }
            else {
                showForm();
                exit;
            }
        }
        else {
            showForm("There is no session currently active.");
            exit();
        }
    }
        
    function showForm($error=""){
        // continue the session
        session_start();

        include_once dirname(__FILE__) . '/php_only_scripts/global_after_session.php';
        include_once dirname(__FILE__) . '/php_only_scripts/global_functions.php';

        $loginErrorString=$error;
?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Task Management System</title>

        <meta name="keywords" content="GIS,data,testing,IT" />
        <meta name="description" content="EES Divisions." />

        <script src="./SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
        <script src="https://www.google.com/recaptcha/api.js" type="text/javascript"></script>

        <link rel="icon" href="https://www.eestms.com/images/ees_ico.ico"/> 
        <link href="./SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
        <link href="./SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
        <link rel = "stylesheet" type="text/css" href="./css/contentpage.css" >
    </head>

    <body class="thrColElsHdr">

    <div id="container">

    <!-- start of banner wrap --->

    <div id="topContent">
    </div>
    <!-- end of banner wrap --->

      <!-- end #header -->
      <br class="clearfloat" />
      <div id="sidebar1">
          <?php 
                // Add includes to PHP files that I will need to access
                require_once dirname(__FILE__) . '/presentation.php';

                // Init some variables
                $userRow=null;

                // Create and Setup the database
                $db = new Database();
                $db->setupTmsDB();

                // Attempt to log in with this user
                $currentUser = $db->getLoggedInSessionUser(session_id());

                // Determine if we only have one one user with this session ID
                if (mysqli_num_rows($currentUser)==1){
                    // Attempt to get the user row
                    $userRow = $currentUser->fetch_assoc();
                }

                // Render the appropriate presentation for the tasks sidebar
                getSidebar($userRow, 'nosubs');
          ?>       
          <br class="clearfloat" />
      <!-- end #sidebar1 -->
      </div>

      <!-- start #mainContent -->
      <div id="mainContent">
        <div class="maintitle">Task Management System</div>
        <div class="breadcrumb"> <a href="./"> Home </a> > <a href="index.php"> Task Management System </a> </div>
        <form action="<?php echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'?"https" : "http") . "://" . $_SERVER['HTTP_HOST'] .  $_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data">  
            <div>
                <br>
                <label style="color:Red; font-size: 25px;">Password restricted area.</label>   
            </div>  
            <br>
            <div>
                <label for="restrictedAreaLoginID">Login ID:  
                    <input type="text" maxlength="50" autocomplete="off" name="restrictedAreaLoginID" id="restrictedAreaLoginID"/>
                </label>  
            </div>  
            <br>
            <div>
                <label for="restrictedAreaPassword">Password:  
                    <input type="password" autocomplete="new-password" name="restrictedAreaPassword" id="restrictedAreaPassword"/>
                </label>
            </div>  
            <br>
            <div>
                <input type="submit" name="submit_pwd" id="submit_pwd" value="Login"/>
            </div>  
            <br>
        </form>

        <label style="color:Red; font-size: 25px;">
        <?php
            echo $loginErrorString;
        ?>
        </label>
      <!-- end #mainContent -->

      <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
      <br class="clearfloat" />

      <!-- start #footer --></div>
       <div id="footer">
        <p>© 2018 Evolve Engineering and Software. EES, Evolve Engineering and Software, EES Logo are registered marks of Evolve Engineering and Software.</p>
      <!-- end #footer -->
       </div>
    <!-- end #container -->
    </div>

    </body>

    </html>

<?php   
    }
?>
