--This modules returns the header and footer of web page--
module( "layout" , package.seeall )
require("luamods/cgi")
require("config/main_conf")
require("luamods/command")
require("luamods/db")

------------------------------
-- Modified 16 October 2012 --
-- dylan@kses.net           --
------------------------------------------------------------------------------------
-- Since layout is included in practically all the pages, this is as good a place --
-- as any to verify the user's session.                                           --
-- If either the userID or sessionID cookie is blank, redirect to login page.     --
-- Verify that the session exists in the database and has not expired yet.        --
-- Redirect to login page with appropriate message if session is invalid or has   --
-- since expired.                                                                 --
------------------------------------------------------------------------------------
if cgi.cookie == nil or cgi.cookie.userID == nil or cgi.cookie.sessionID == nil then
    print("Location: index.lua\n\n")
    os.exit(0)
else
    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 == 2 then
        print("Location: index.lua?error=2\n\n")
        os.exit(0)
    elseif result == 3 then
        print("Location: index.lua?error=3\n\n")
        os.exit(0)
    end
    priv = db.getPriv(cgi.cookie.userID) or 1
end

--this function returns the header string
function getHeader()

--Initialize mods table
mods = {}
--insert all .xml mod files in mods table
mods = command.getFilesInTable( main_conf.mods_xml_path )
--get number of mods in table
number_of_mods = #(mods)

menuItems = {}
for i = 1, number_of_mods do
    local modXML = command.returnFileString(main_conf.mods_xml_path..mods[i])
    if (modXML ~= nil) then
        menuItem = {}
        menuItem.name = modXML:match("<name>%s*(.-)%s*</name>") or "unknown MOD"
        menuItem.exe = modXML:match("<exe>%s*(.-)%s*</exe>")
        menuItem.expanded = "false"
        menuItem.subItems = {}
        if (menuItem.exe == "#") then
            for linkXML in modXML:gmatch("<link>%s*(.-)%s*</link>") do
                subMenuItem = {}
                subMenuItem.name = linkXML:match("<link_name>%s*(.-)%s*</link_name>")
                subMenuItem.exe = linkXML:match("<link_exe>%s*(.-)%s*</link_exe>")
                if cgi.script_name:match("([^%?%/%_]+)") == subMenuItem.exe:match("([^%?%/%_]+)") then menuItem.expanded = "true" end
                table.insert(menuItem.subItems, subMenuItem)
            end
        end
    end
    table.insert(menuItems, menuItem)
end

content = [[

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
    <title>Guinnux Config Site</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="styles/mainstyle.css" type="text/css" />
    <link rel="shortcut icon" href="styles/img/favicon.ico">
        
    <script>
        function toggle(i)
        {
            var div = i.parentNode;
            var lists = div.getElementsByTagName("ul");
            var labels = div.getElementsByTagName("a");
            
            var list = lists[0];
            if (list.style.display != "block")
            {
                list.style.display = "block";
                labels[0].id = "grouplabel_checked";
            }
            else
            {
                list.style.display = "none";
                labels[0].id = "grouplabel";
            }
        }
    </script>        

</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><!--****** Image is set in .css file with logo id ******-->
        <div id="content">
            <div id="column1">

                <div id="menu">
                    <div style="padding-left: 15px;">
                        <h1>MENU</h1>
                    </div>
                    <br>
                    
                    <ul class="links">
                        <li><a href="home.lua">Home</a></li>
                        <li><a href="system.lua">System</a></li>
]]

for i,m in pairs(menuItems) do
    --print(i,m.name,m.exe)
    if (m.exe == "#") then
        content = content .. [[
                        <div class="group">
]]
        if (m.expanded == "true") then
            content = content .. [[
                            <a id="grouplabel_checked" href="javascript:void(0)" onclick="toggle(this);">]]..m.name..[[</a>
                            <ul style="display: block;">
]]
        else
            content = content .. [[
                            <a id="grouplabel" href="javascript:void(0)" onclick="toggle(this);">]]..m.name..[[</a>
                            <ul style="display: none;">
]]
        end
        for a,z in pairs(m.subItems) do
            --print(z.name)
            content = content .. [[
                                <li><a href="]]..z.exe..[[">]]..z.name..[[</a></li>
]]                
        end
        content = content .. [[
                            </ul>
                        </div>
]]                    
    else
        content = content .. [[
                        <li><a href="]]..m.exe..[[">]]..m.name..[[</a></li>
]]            
    end
end

content = content .. [[
                    </ul>
                    <br><br><br>
                </div>
                <div id="menu">
                    <h1><img src="styles/img/user.png" alt="user" width="24" height="24"/> ]]..username..[[ </h1>
                    <div class="usersmenu">
                        <a href="users_edit.lua?username=]]..username..[[">My Account</a><br>
                        <a href="logout.lua">Logout</a>
                    </div>
                </div>
            </div>
            <div id="page_effect" style="">
                <div id="column2">
                <!-- ******* END OF HEADER, CONTENT START ******* -->
]]
return content
end


function getFooter()
    content = [[
                <!-- ******* END OF CONTENT, FOOTER START ******* -->
                </div>
            </div>
        </div>
]]

    if (priv == 2) then
        content = content..[[
        <div id="info" style="padding:10px">
            <B>DEVELOPER INFO</B>
            <pre>
]]..cgi.stringofALLenv()..[[
            </pre>
            <pre>
]]..cgi.printPostTable()..[[
            </pre>
            <pre>
]]..cgi.printGetTable()..[[
            </pre>
        </div>]]
    end

content = content..[[

        <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>
]]
    return content
end

