<?php

require 'init.php';
if (!isset($_COOKIE["login_cookie"])) {
    $login_token = lib::generateRandomString();
    $login_attempts = 0;
    setcookie("login_cookie", $login_token, time() + 3600 * 3, '/');
}
$get_post;
if (lib::ldap_login($get_post['username'], $get_post['password'])) {
    $email = $get_post['username'] . '@mx.lakeforest.edu';
    $query = "SELECT * FROM  office_fac_prof_table";
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);
    if ($num_rows > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            if ($email == $row['fac_email']) {
                $fac_id = $row['fac_id'];
            }
        }
    }
    $task = $get_post['attempt'];
    $started = time();
    switch ($task) {
        case 'login':
            if (isset($_COOKIE["login_cookie"])) {
                $login_token = $_COOKIE['login_cookie'];
                $database = "" . FACULTY_SESSION;
                $query = "INSERT INTO {$database} VALUES ('','{$fac_id}', '{$started}','','','{$login_attempts}','{$login_token}')";
                //create a class to make the inserting and other things easier.
                $result = mysql_query($query);