<?php
include('timeUtils.php');
include('XMLUsers.php');
include('XMLMessages.php');
include('XMLNews.php');
include('XMLLectures.php');
include('XMLLabs.php');
include('XMLAssignments.php');
include('XMLExams.php');
include('XMLFAQ.php');
include('XMLDocuments.php');
include('misc/base.php');

function logout() {
    if (loginCheck()) {
        setcookie('id', 0, time() - 3600);
        setcookie('password', 0, time() - 3600);
        setcookie('accountType', 0, time() - 3600);
        header('location: index.php');
    }
}

function loginCheck() {
    if (isset($_COOKIE['id']) && isset($_COOKIE['password']) && isset($_COOKIE['accountType'])) {
        $u = new XMLUsers("data/sensitive/users.xml");
        if ($u->check($_COOKIE['id'], $_COOKIE['password'], $_COOKIE['accountType'])) {
            return true;
        }
    }
    return false;
}

if (isset($_GET['q'])) {
    $q = $_GET['q'];
} else {
    $q = "Home";
}
$user = "";
$pass = "";
$accountType = "";
$loginStatus = "";
if (isset($_POST['btnGo']) && isset($_POST['user']) && isset($_POST['pass'])) {

    $user = $_POST['user'];
    $pass = sha1($_POST['pass']);
    $accountType = 0;
    $u = new XMLUsers("data/sensitive/users.xml");
    if ($u->check($user, $pass, $accountType)) {
        $uv = $u->getUserByUserID($user);
        setcookie("id", $user, time() + 3600);
        setcookie("password", $pass, time() + 3600);
        setcookie("accountType", $accountType, time() + 3600);
        $u->updateUserDetail($uv['id'], "", "", "", "", "now", getClientIP(), "");
        $q = "Home";
        $loginStatus = "success";
    } else {
        $loginStatus = "failed";
        $q = "Admin";
    }
}

function getCurrentUserName() {
    global $loginStatus;
    global $user;
    global $pass;
    global $accountType;
    if (isset($_COOKIE['id']) && isset($_COOKIE['password']) && isset($_COOKIE['accountType'])) {
        $u = new XMLUsers("data/sensitive/users.xml");
        if ($u->check($_COOKIE['id'], $_COOKIE['password'], $_COOKIE['accountType'])) {
            $uxser = $u->getUserByUserID($_COOKIE['id']);
            return $uxser['name'];
        } else {
            return "*";
        }
    } else {
        if (isset($loginStatus) && $loginStatus == "success") {
            $u = new XMLUsers("data/sensitive/users.xml");
            if ($u->check($user, $pass, $accountType)) {
                $uxser = $u->getUserByUserID($user);
                return $uxser['name'];
            } else {
                return "*";
            }
        } else {
            return "**";
        }
    }
    return '-';
}
?>

<!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">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="keywords" content="university of texas at arlington computer science and engineering cse" />
        <meta name="description" content="CSE - Computer Science and Engineering" />
        <link href="<?php echo $BASE_URL; ?>/misc/style.css" rel="stylesheet" type="text/css" />
        <script language="javascript" type="text/javascript" src="<?php echo $BASE_URL; ?>/misc/jquery.min.js"></script>
        <?php if (loginCheck() || (isset($loginStatus) && $loginStatus == "success")) { ?>
            <script language="javascript" type="text/javascript" src="<?php echo $BASE_URL; ?>/misc/jquery.jeditable.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {                    
                    $('.edit_area').editable("driver.php", { 
                        indicator : "<img src='misc/loader_small.gif'>",
                        submit : 'OK',
                        cancel : 'Cancel',
                        type : 'textarea',
                        rows: 3
                    });
                    $('.edit_area1').editable("driver.php", { 
                        indicator : "<img src='misc/loader_small.gif'>",
                        type   : 'text',         
                        submit : 'OK',
                        cancel : 'Cancel'
                    });                      
                });
            </script>



        <?php } ?>

        <title>==:: <?php echo $COURSE_CODE; ?> | <?php  echo $COURSE_TITLE; ?>::==</title>
    </head>

    <body>
        <div id="top-header" align="right">
            <table>
                <tr>
                    <td width="1024">
                        <center>
                            <?php
                                echo "<font size=8>".$COURSE_CODE."</font><BR>";
                                echo "<font size=5>".$COURSE_TITLE."</font><BR>";
                                echo "<font size=3>".$COURSE_SEMESTER."</font>";
                            ?>
                        </center>
                    </td>
                    <td>
                        <?php
                        if (loginCheck() || (isset($loginStatus) && $loginStatus == "success")) {
                            echo "Hi, " . getCurrentUserName() . " | <a href=\"" . $BASE_URL . "/?q=Logout\">Sign Out</a>";
                        } else {
                            echo "<a href=\"" . $BASE_URL . "/?q=Admin\">Admin Login</a>";
                        }
                        ?>
                </tr>
            </table>
        </div>

        <ul id="menu">
            <li><a href="<?php echo $BASE_URL; ?>/index.php" title="Home" <?php if ($q == "Home") echo "class=\"active\""; ?> >Home</a></li>	
            <li><a href="<?php echo $BASE_URL; ?>/?q=NewsArchive" title="Announcements" <?php if ($q == "NewsArchive") echo "class=\"active\""; ?> >Announcements</a></li>	
            <li><a href="<?php echo $BASE_URL; ?>/?q=Lectures" title="Lectures" <?php if ($q == "Lectures") echo "class=\"active\""; ?>>Lectures</a></li>
            <li><a href="<?php echo $BASE_URL; ?>/?q=Labs" title="Labs" <?php if ($q == "Labs") echo "class=\"active\""; ?>>Labs</a></li>
            <li><a href="<?php echo $BASE_URL; ?>/?q=HomeWorks" title="Home Works" <?php if ($q == "HomeWorks") echo "class=\"active\""; ?>>Assignments</a></li>
            <li><a href="<?php echo $BASE_URL; ?>/?q=Examinations" title="Examinations" <?php if ($q == "Examinations") echo "class=\"active\""; ?>>Examinations</a></li>
            <li><a href="<?php echo $BASE_URL; ?>/?q=Documents" title="Documents" <?php if ($q == "Documents") echo "class=\"active\""; ?>>Documents</a></li>
            <li><a href="<?php echo $BASE_URL; ?>/?q=FAQ" title="FAQ" <?php if ($q == "FAQ") echo "class=\"active\""; ?>>FAQ</a></li>
            <?php if (loginCheck() || (isset($loginStatus) && $loginStatus == "success")) { ?>
                <li><a href="<?php echo $BASE_URL; ?>/?q=Administration" title="Administration" <?php if ($q == "Administration") echo "class=\"active\""; ?>>Administration</a></li>
            <?php }else { ?>
            <?php } ?>

        </ul>
        <div id="container">
            <div id="content-wrapper">            
                <div id="content-top"><h2>Welcome to <?php echo $COURSE_CODE; ?> course website!</h2></div>
                <div id="content">
                    <div style="padding:20px 30px 0px 20px; position: relative; top: -40px;">
                        <?php
                        if ($q == "Home") {
                            include('pages/Home.php');
                        } else if ($q == "Lectures") {
                            include('pages/Lectures.php');
                        } else if ($q == "Labs") {
                            include('pages/Labs.php');
                        } else if ($q == "HomeWorks") {
                            include('pages/HomeWorks.php');
                        } else if ($q == "Examinations") {
                            include('pages/Examinations.php');
                        } else if ($q == "FinalProjects") {
                            include('pages/FinalProjects.php');
                        } else if ($q == "TA") {
                            include('pages/TA.php');
                        } else if ($q == "FAQ") {
                            include('pages/FAQ.php');
                        } else if ($q == "Forum") {
                            include('pages/Forum.php');
                        } else if ($q == "NewsArchive") {
                            include('pages/NewsArchive.php');
                        } else if ($q == "Admin") {
                            include('pages/Login.php');
                        } else if ($q == "Administration") {
                            include('pages/Administration.php');
                        } else if ($q == "Logout") {
                            echo "<p>Logged out successfully!...</p>";
                            logout();
                        } else if ($q == "UpdateLectures") {
                            echo "<iframe src=\"pages/UpdateLectures.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "UpdateNews") {
                            echo "<iframe src=\"pages/UpdateNews.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "UpdateAssignments") {
                            echo "<iframe src=\"pages/UpdateAssignments.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "UpdateLabs") {
                            echo "<iframe src=\"pages/UpdateLabs.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "UpdateExams") {
                            echo "<iframe src=\"pages/UpdateExams.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "UpdateFAQs") {
                            echo "<iframe src=\"pages/UpdateFAQs.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "UpdateUsers") {
                            echo "<iframe src=\"pages/UpdateUsers.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "UpdateStaticTexts") {
                            echo "<iframe src=\"pages/UpdateStaticTexts.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else if ($q == "Documents") {
                            include('pages/Documents.php');
                        }else if ($q == "UpdateDocuments") {
                            echo "<iframe src=\"pages/UpdateDocuments.php\" width=\"1024\" height=\"1024\"></iframe>";
                        } else {
                            echo "Oooops! page request not understood :D";
                        }
                        ?>
                    </div>
                </div>
                <div id="content-bottom"></div>
            </div>
        </div>
    </body>
</html>
