1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-19 16:46:23 -07:00

Use generic page header for manage pages, add login template

This commit is contained in:
Eggbertx 2022-01-04 17:48:46 -08:00
parent c8d8af077b
commit b2318af7a3
12 changed files with 132 additions and 44 deletions

View file

@ -3,6 +3,7 @@ package building
import (
"encoding/json"
"errors"
"io"
"os"
"path"
@ -97,6 +98,28 @@ func BuildBoardListJSON() error {
return nil
}
// BuildPageHeader is a convenience function for automatically generating the top part
// of every normal HTML page
func BuildPageHeader(writer io.Writer) error {
return serverutil.MinifyTemplate(gctemplates.PageHeader,
map[string]interface{}{
"webroot": config.GetSystemCriticalConfig().WebRoot,
"site_config": config.GetSiteConfig(),
"sections": gcsql.AllSections,
"boards": gcsql.AllBoards,
"board_config": config.GetBoardConfig(""),
}, writer, "text/html")
}
// BuildPageFooter is a convenience function for automatically generating the bottom
// of every normal HTML page
func BuildPageFooter(writer io.Writer) (err error) {
return serverutil.MinifyTemplate(gctemplates.PageFooter,
map[string]interface{}{
"webroot": config.GetSystemCriticalConfig().WebRoot,
}, writer, "text/html")
}
// BuildJS minifies (if enabled) consts.js, which is built from a template
func BuildJS() error {
// build consts.js from template

View file

@ -22,8 +22,10 @@ var (
ManageBoards *template.Template
ManageConfig *template.Template
ManageDashboard *template.Template
ManageHeader *template.Template
ManageLogin *template.Template
ManageStaff *template.Template
PageHeader *template.Template
PageFooter *template.Template
PostEdit *template.Template
ThreadPage *template.Template
)
@ -145,10 +147,10 @@ func templateLoading(t string, buildAll bool) error {
return templateError("manage_dashboard.html", err)
}
}
if buildAll || t == "manageheader" {
ManageHeader, err = loadTemplate("manage_header.html")
if buildAll || t == "managelogin" {
ManageLogin, err = loadTemplate("manage_login.html")
if err != nil {
return templateError("manage_header.html", err)
return templateError("manage_login.html", err)
}
}
if buildAll || t == "managestaff" {
@ -157,6 +159,18 @@ func templateLoading(t string, buildAll bool) error {
return templateError("manage_staff.html", err)
}
}
if buildAll || t == "pageheader" {
PageHeader, err = loadTemplate("page_header.html")
if err != nil {
return templateError("page_header.html", err)
}
}
if buildAll || t == "pagefooter" {
PageFooter, err = loadTemplate("page_footer.html")
if err != nil {
return templateError("page_footer.html", err)
}
}
if buildAll || t == "js" {
JsConsts, err = loadTemplate("consts.js")
if err != nil {

View file

@ -344,17 +344,26 @@ var actions = []Action{
username := request.FormValue("username")
password := request.FormValue("password")
redirectAction := request.FormValue("action")
if redirectAction == "" {
if redirectAction == "" || redirectAction == "logout" {
redirectAction = "announcements"
}
if username == "" || password == "" {
//assume that they haven't logged in
output = `<form method="POST" action="` + systemCritical.WebRoot + `manage?action=login" id="login-box" class="staff-form">` +
`<input type="hidden" name="redirect" value="` + redirectAction + `" />` +
`<input type="text" name="username" class="logindata" /><br />` +
`<input type="password" name="password" class="logindata" /><br />` +
`<input type="submit" value="Login" />` +
`</form>`
manageLoginBuffer := bytes.NewBufferString("")
if err = serverutil.MinifyTemplate(gctemplates.ManageLogin,
map[string]interface{}{
"webroot": config.GetSystemCriticalConfig().WebRoot,
"site_config": config.GetSiteConfig(),
"sections": gcsql.AllSections,
"boards": gcsql.AllBoards,
"board_config": config.GetBoardConfig(""),
"redirect": redirectAction,
}, manageLoginBuffer, "text/html"); err != nil {
return "", errors.New(gclog.Print(gclog.LErrorLog,
"Error executing staff login page template: "+err.Error()))
}
output = manageLoginBuffer.String()
} else {
key := gcutil.Md5Sum(request.RemoteAddr + username + password + systemCritical.RandomSeed + gcutil.RandomString(3))[0:10]
createSession(key, username, password, request, writer)
@ -371,7 +380,7 @@ var actions = []Action{
cookie.MaxAge = 0
cookie.Expires = time.Now().Add(-7 * 24 * time.Hour)
http.SetCookie(writer, cookie)
return "Logged out successfully", nil
return "<br />Logged out successfully", nil
}},
{
ID: "announcements",

View file

@ -5,9 +5,8 @@ import (
"fmt"
"net/http"
"github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/building"
"github.com/gochan-org/gochan/pkg/gclog"
"github.com/gochan-org/gochan/pkg/gctemplates"
"github.com/gochan-org/gochan/pkg/gcutil"
"github.com/gochan-org/gochan/pkg/serverutil"
)
@ -116,18 +115,16 @@ func CallManageFunction(writer http.ResponseWriter, request *http.Request) {
serverutil.MinifyWriter(writer, []byte(outputJSON), "application/json")
return
}
managePageBuffer.WriteString("<!DOCTYPE html><html><head>")
criticalCfg := config.GetSystemCriticalConfig()
if err = serverutil.MinifyTemplate(gctemplates.ManageHeader,
map[string]interface{}{
"webroot": criticalCfg.WebRoot,
},
&managePageBuffer, "text/html"); err != nil {
serverutil.ServeErrorPage(writer, gclog.Print(gclog.LErrorLog|gclog.LStaffLog,
"Error executing manage page header template: ", err.Error()))
if err = building.BuildPageHeader(&managePageBuffer); err != nil {
serveError(writer, "error", actionID,
gclog.Print(gclog.LErrorLog, "Failed writing page header: ", err.Error()), false)
return
}
managePageBuffer.WriteString("<br />" + fmt.Sprint(output) + "<br /><br />")
if err = building.BuildPageFooter(&managePageBuffer); err != nil {
serveError(writer, "error", actionID,
gclog.Print(gclog.LErrorLog, "Failed writing page footer: ", err.Error()), false)
return
}
managePageBuffer.WriteString(fmt.Sprint(output, "</body></html>"))
writer.Write(managePageBuffer.Bytes())
}