--package.path = package.path..";/var/www/lighttpd/test/luamods/?.lua"
require("luamods/db")
require("luamods/cgi")

errormessage = ""

------------------------------
-- Modified 16 October 2012 --
-- dylan@kses.net           --
----------------------------------------------------------------------
-- If session is still valid, don't allow the user to log in again. --
-- Redirect to home page.                                           --
----------------------------------------------------------------------
if cgi.cookie ~= nil then
    username = cgi.cookie.userID or ""
    session = cgi.cookie.sessionID or ""
    local result = db.verifySession(session,username)
    -- 1 is good, 2 and 3 are failures
    if result == 1 then
        print("Location: home.lua\n\n")
        os.exit(0)
    end
end

--****** If login button is pressed then login if valid else display error *********--
if cgi.post.loginbutton == "Login" then
	check = db.verifyUser(cgi.post.usrname, cgi.post.pswrd)
	if check == true then
		ipadd = os.getenv("REMOTE_ADDR") or "N/A"
		sesid = db.createSession( cgi.post.usrname, ipadd )
		--Create cookies and location to direct page to
		header = "Set-Cookie: userID="..cgi.post.usrname.."\nSet-Cookie: sessionID="..sesid.."\nLocation: home.lua\n\n"
		print(header)
	 else
		errormessage = "Wrong username / password, please try again."
	 end
end

------------------------------
-- Modified 16 October 2012 --
-- dylan@kses.net           --
----------------------------------------------------------------------------------
-- Expired / invalid session information is passed to this page from layout.lua --
-- inside a CGI GET. All this does is to display the appropriate message to the --
-- user who suddenly finds himself back at the login page.                      --
----------------------------------------------------------------------------------
if cgi.get.error == "2" then
    errormessage="Your session has expired, please login again."
elseif cgi.get.error == "3" then
    errormessage="Unknown session."
end

print([[

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
    <link rel="stylesheet" href="styles/mainstyle.css" type="text/css" />
    <link rel="shortcut icon" href="styles/img/favicon.ico">
    <meta http-equiv="content-type" content="text/html; charset=windows-1251" />
    <title>Guinnux Config Site</title>
</head>

<body>
    <div id="main">
        <div id="logo"><a href="http://www.kses.net/" target="window"><img src="images/kses-header-logo.png"/></a></div>
        <div id="content" style="text-align: center;">
            <div style="display:inline-block; background: url(styles/img/box_2.png) repeat-x; border: solid 1px #aaaaaa; margin:25px;padding: 25px; width: 400px; height: 240px; border-radius:10px;">
                <center><h2>Please enter your credentials</h2></center><br>
                <form action="index.lua" method="post">
                    <div style="background: url(images/users.png) no-repeat; background-position: left center; minimum-height: 64px;">
                        <input type="text" id="fname" name="usrname" value="" placeholder="Username"/><br>
                        <input type="password" name="pswrd" value="" placeholder="Password"/><br><br>
                    </div>
                    <input type="submit" name="loginbutton" value="Login"/>
                </form>
                <br>
]])

if errormessage ~= "" then
    print([[
                    <img src="images/warning.png"> ]]..errormessage..[[<br>
]])
end

print([[
                
            </div>
        </div>
        <div id="footer">
            Copyright 2013 Keystone Electronic Solutions Version 2.5 | <a href="http://www.kses.net/" target="window">www.kses.net</a>
        </div>
    </div>
</body>

</html>

]]
)
