2020-04-29 17:44:29 -07:00
|
|
|
package gctemplates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/gochan-org/gochan/pkg/config"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-29 23:47:13 -08:00
|
|
|
Banpage *template.Template
|
|
|
|
Captcha *template.Template
|
|
|
|
Catalog *template.Template
|
|
|
|
ErrorPage *template.Template
|
|
|
|
FrontPage *template.Template
|
|
|
|
BoardPage *template.Template
|
|
|
|
JsConsts *template.Template
|
2022-11-30 10:20:31 -08:00
|
|
|
ManageAppeals *template.Template
|
2022-01-29 23:47:13 -08:00
|
|
|
ManageBans *template.Template
|
|
|
|
ManageBoards *template.Template
|
2023-01-28 15:23:44 -08:00
|
|
|
ManageThreadAttrs *template.Template
|
2022-08-07 22:02:06 -07:00
|
|
|
ManageSections *template.Template
|
2022-01-29 23:47:13 -08:00
|
|
|
ManageConfig *template.Template
|
|
|
|
ManageDashboard *template.Template
|
2022-09-15 21:37:56 -07:00
|
|
|
ManageFileBans *template.Template
|
2022-12-06 16:52:16 -08:00
|
|
|
ManageNameBans *template.Template
|
2022-08-27 23:37:59 -07:00
|
|
|
ManageIPSearch *template.Template
|
2022-01-29 23:47:13 -08:00
|
|
|
ManageRecentPosts *template.Template
|
2022-01-31 09:47:19 -08:00
|
|
|
ManageWordfilters *template.Template
|
2022-01-29 23:47:13 -08:00
|
|
|
ManageLogin *template.Template
|
2022-07-26 12:13:27 -07:00
|
|
|
ManageReports *template.Template
|
2022-01-29 23:47:13 -08:00
|
|
|
ManageStaff *template.Template
|
2022-09-23 15:50:18 -07:00
|
|
|
MoveThreadPage *template.Template
|
2022-01-29 23:47:13 -08:00
|
|
|
PageHeader *template.Template
|
|
|
|
PageFooter *template.Template
|
|
|
|
PostEdit *template.Template
|
|
|
|
ThreadPage *template.Template
|
2020-04-29 17:44:29 -07:00
|
|
|
)
|
|
|
|
|
2023-01-06 20:13:58 -08:00
|
|
|
func LoadTemplate(files ...string) (*template.Template, error) {
|
2020-04-29 17:44:29 -07:00
|
|
|
var templates []string
|
2021-07-11 11:51:29 -07:00
|
|
|
templateDir := config.GetSystemCriticalConfig().TemplateDir
|
2020-04-29 17:44:29 -07:00
|
|
|
for i, file := range files {
|
|
|
|
templates = append(templates, file)
|
2021-07-11 11:51:29 -07:00
|
|
|
tmplPath := path.Join(templateDir, "override", file)
|
2020-04-29 17:44:29 -07:00
|
|
|
|
|
|
|
if _, err := os.Stat(tmplPath); !os.IsNotExist(err) {
|
|
|
|
files[i] = tmplPath
|
|
|
|
} else {
|
2021-07-11 11:51:29 -07:00
|
|
|
files[i] = path.Join(templateDir, file)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 09:28:45 -07:00
|
|
|
return template.New(templates[0]).Funcs(funcMap).ParseFiles(files...)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
|
2023-01-06 20:13:58 -08:00
|
|
|
func ParseTemplate(name, tmplStr string) (*template.Template, error) {
|
|
|
|
return template.New(name).Funcs(funcMap).Parse(tmplStr)
|
|
|
|
}
|
|
|
|
|
2020-06-06 09:28:45 -07:00
|
|
|
func templateError(name string, err error) error {
|
2020-04-29 17:44:29 -07:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-11 11:51:29 -07:00
|
|
|
templateDir := config.GetSystemCriticalConfig().TemplateDir
|
|
|
|
|
2021-03-26 11:10:05 -07:00
|
|
|
return fmt.Errorf("failed loading template '%s/%s': %s",
|
2021-07-11 11:51:29 -07:00
|
|
|
templateDir, name, err.Error())
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitTemplates loads the given templates by name. If no parameters are given,
|
|
|
|
// or the first one is "all", all templates are (re)loaded
|
2020-06-06 09:28:45 -07:00
|
|
|
func InitTemplates(which ...string) error {
|
2020-04-29 17:44:29 -07:00
|
|
|
gcsql.ResetBoardSectionArrays()
|
2020-05-22 17:26:06 +02:00
|
|
|
if len(which) == 0 || which[0] == "all" {
|
|
|
|
return templateLoading("", true)
|
|
|
|
}
|
2020-04-29 17:44:29 -07:00
|
|
|
for _, t := range which {
|
2020-05-22 17:26:06 +02:00
|
|
|
err := templateLoading(t, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-06 09:28:45 -07:00
|
|
|
func templateLoading(t string, buildAll bool) error {
|
|
|
|
var err error
|
2020-05-22 17:26:06 +02:00
|
|
|
if buildAll || t == "banpage" {
|
2023-01-06 20:13:58 -08:00
|
|
|
Banpage, err = LoadTemplate("banpage.html", "page_footer.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("banpage.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "captcha" {
|
2023-01-06 20:13:58 -08:00
|
|
|
Captcha, err = LoadTemplate("captcha.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("captcha.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "catalog" {
|
2023-01-06 20:13:58 -08:00
|
|
|
Catalog, err = LoadTemplate("catalog.html", "page_header.html", "page_footer.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("catalog.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "error" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ErrorPage, err = LoadTemplate("error.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("error.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "front" {
|
2023-01-06 20:13:58 -08:00
|
|
|
FrontPage, err = LoadTemplate("front.html", "front_intro.html", "page_header.html", "page_footer.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("front.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "boardpage" {
|
2023-01-06 20:13:58 -08:00
|
|
|
BoardPage, err = LoadTemplate("boardpage.html", "post.html", "page_header.html", "postbox.html", "page_footer.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("boardpage.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "threadpage" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ThreadPage, err = LoadTemplate("threadpage.html", "post.html", "page_header.html", "postbox.html", "page_footer.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("threadpage.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "postedit" {
|
2023-01-06 20:13:58 -08:00
|
|
|
PostEdit, err = LoadTemplate("post_edit.html", "page_header.html", "page_footer.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("threadpage.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
2022-11-30 10:20:31 -08:00
|
|
|
if buildAll || t == "manageappeals" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageAppeals, err = LoadTemplate("manage_appeals.html")
|
2022-11-30 10:20:31 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_appeals.html", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
if buildAll || t == "managebans" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageBans, err = LoadTemplate("manage_bans.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_bans.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
|
|
|
if buildAll || t == "manageboards" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageBoards, err = LoadTemplate("manage_boards.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_boards.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
2023-01-28 15:23:44 -08:00
|
|
|
if buildAll || t == "managethreadattrs" {
|
|
|
|
ManageThreadAttrs, err = LoadTemplate("manage_threadattrs.html")
|
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_threadattrs.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-08-07 22:02:06 -07:00
|
|
|
if buildAll || t == "managesections" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageSections, err = LoadTemplate("manage_sections.html")
|
2022-08-07 22:02:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_sections.html", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
if buildAll || t == "manageconfig" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageConfig, err = LoadTemplate("manage_config.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_config.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
2021-11-29 21:53:47 -08:00
|
|
|
if buildAll || t == "managedashboard" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageDashboard, err = LoadTemplate("manage_dashboard.html")
|
2021-11-29 21:53:47 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_dashboard.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 17:48:46 -08:00
|
|
|
if buildAll || t == "managelogin" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageLogin, err = LoadTemplate("manage_login.html")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
2022-01-04 17:48:46 -08:00
|
|
|
return templateError("manage_login.html", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
}
|
2022-07-26 12:13:27 -07:00
|
|
|
if buildAll || t == "managereports" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageReports, err = LoadTemplate("manage_reports.html")
|
2022-07-26 12:13:27 -07:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_reports.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 21:37:56 -07:00
|
|
|
if buildAll || t == "managefilebans" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageFileBans, err = LoadTemplate("manage_filebans.html")
|
2022-09-15 21:37:56 -07:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_filebans.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 16:52:16 -08:00
|
|
|
if buildAll || t == "managenamebans" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageNameBans, err = LoadTemplate("manage_namebans.html")
|
2022-12-06 16:52:16 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_namebans.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 23:37:59 -07:00
|
|
|
if buildAll || t == "manageipsearch" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageIPSearch, err = LoadTemplate("manage_ipsearch.html")
|
2022-08-27 23:37:59 -07:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_ipsearch.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 23:47:13 -08:00
|
|
|
if buildAll || t == "managerecents" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageRecentPosts, err = LoadTemplate("manage_recentposts.html")
|
2022-01-29 23:47:13 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_recentposts.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-31 09:47:19 -08:00
|
|
|
if buildAll || t == "managewordfilters" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageWordfilters, err = LoadTemplate("manage_wordfilters.html")
|
2022-01-31 09:47:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_wordfilters.html", err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 21:53:47 -08:00
|
|
|
if buildAll || t == "managestaff" {
|
2023-01-06 20:13:58 -08:00
|
|
|
ManageStaff, err = LoadTemplate("manage_staff.html")
|
2021-11-29 21:53:47 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("manage_staff.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 15:50:18 -07:00
|
|
|
if buildAll || t == "movethreadpage" {
|
2023-01-06 20:13:58 -08:00
|
|
|
MoveThreadPage, err = LoadTemplate("movethreadpage.html", "page_header.html", "page_footer.html")
|
2022-09-23 15:50:18 -07:00
|
|
|
if err != nil {
|
|
|
|
return templateError("movethreadpage.html", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 17:48:46 -08:00
|
|
|
if buildAll || t == "pageheader" {
|
2023-01-06 20:13:58 -08:00
|
|
|
PageHeader, err = LoadTemplate("page_header.html")
|
2022-01-04 17:48:46 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("page_header.html", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if buildAll || t == "pagefooter" {
|
2023-01-06 20:13:58 -08:00
|
|
|
PageFooter, err = LoadTemplate("page_footer.html")
|
2022-01-04 17:48:46 -08:00
|
|
|
if err != nil {
|
|
|
|
return templateError("page_footer.html", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 17:26:06 +02:00
|
|
|
if buildAll || t == "js" {
|
2023-01-06 20:13:58 -08:00
|
|
|
JsConsts, err = LoadTemplate("consts.js")
|
2020-05-22 17:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return templateError("consts.js", err)
|
2020-04-29 17:44:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|