local command = require("luamods/command")
local layout = require("luamods/layout")
local cgi = require("luamods/cgi")

local db = require("luamods/db")


local escape_lua_pattern
do
    local matches =
    {
        ["^"] = "%^";
        ["$"] = "%$";
        ["("] = "%(";
        [")"] = "%)";
        ["%"] = "%%";
        ["."] = "%.";
        ["["] = "%[";
        ["]"] = "%]";
        ["*"] = "%*";
        ["+"] = "%+";
        ["-"] = "%-";
        ["?"] = "%?";
        ["\0"] = "%z";
    }

    escape_lua_pattern = function(s)
        return (s:gsub(".", matches))
    end
end 

ca_cert="/etc/openvpn/ca.crt"
client_cert="/etc/openvpn/client.crt"
client_key="/etc/openvpn/client.key"

userLevel = db.getPriv(layout.username) or 1

if (userLevel == 1) then
    buttonControl = " disabled"
    inputControl = " readonly"
    scriptControl = "//"
else
    buttonControl = ""
    inputControl = ""
    scriptControl = ""
end

header = layout.getHeader()
footer = layout.getFooter()

if (cgi.post.submitButton == "Save and Apply") and (userLevel ~= 1) then
    if (cgi.post.ca ~= nil) then
        command.writefile(ca_cert, cgi.post.ca)
    end
    if (cgi.post.client ~= nil) then
        command.writefile(client_cert, cgi.post.client)
    end    
    if (cgi.post.key ~= nil) then
        command.writefile(client_key, cgi.post.key)
    end
    saved = 1
    enabled = command.ex("systemctl is-enabled \"openvpn@openvpn\""):match("(enabled)") or ""
    if enabled == "enabled" then
        results = command.ex("systemctl restart \"openvpn@openvpn\" 2>&1") or ""
    else
        results = "Not restarting; OpenVPN not activated"
    end
        
end

ca = command.ex("cat "..ca_cert) or ""
client = command.ex("cat "..client_cert) or ""
key = command.ex("cat "..client_key) or ""

content = [[
                    <div id="info">
                        <div style="background:url(images/openvpn.png); background-size:32px; background-repeat:no-repeat; background-position: 20px center;">
                            <center><h1>OpenVPN - Certificates</h1></center>
                        </div>
                    </div>
]]

if (userLevel == 1) then
    content = content .. [[
                    <center><b>Note:</B> Editing this section requires administrator privileges.</center><br>
]]                    
end

content = content .. [[
                    <br>
                    <center>
                        <b>
                            These are the certificates required for OpenVPN authentication.<br>
                            Note: CA and client certificates are public, but the client key is to be kept secret.<br><br>
                            To change the login credentials, go to <a href="openvpn_conf.lua">OpenVPN configuration</a>.
                        </b>
                    </center>
                    <br>
                    <div class="formLayoutfull">
                        <form action="openvpn_cert.lua#saved" method="post">
                            <b>Certificate Authority</b><br>
                            <small>]]..ca_cert..[[</small>
                            <BR><BR>
                            <textarea name="ca" rows="12" style="width: 100%;">
]] .. ca .. [[
                            </textarea>
                            <br><br><hr><br>
                            <b>Client Certificate</b><br>
                            <small>]]..client_cert..[[</small>
                            <BR><BR>
                            <textarea name="client" rows="12" style="width: 100%;">
]] .. client .. [[
                            </textarea>
                            <br><br><hr><br>
                            <b>Client Key</b><br>
                            <small>]]..client_key..[[</small>
                            <BR><BR>
                            <textarea name="key" rows="12" style="width: 100%;">
]] .. key .. [[
                            </textarea><br>
                            <br><hr><br>
]]
                            
    if (saved == 1) then
        content = content .. [[
                            <a id="saved"></a><b><img src="images/ok.png"/> Changes saved.</b><br><br>
]]
    end
    content = content .. [[
                            <input type="submit" name="submitButton" value="Save and Apply"]]..buttonControl..[[/><br>
                            <small>This will restart the OpenVPN service.</small>
                        </form>
                    </div>
]]

if (results ~= nil) and (results ~= "") then
        content = content .. [[
                    <br>
                    <div class="formLayoutfull">
                        <b>Restarting OpenVPN service:</b>
                        <pre>
]]..results..[[
                        </pre>
                    </div>
]]
end
 

print(header..content..footer)
