From 1115c0a4c5ea66c9e5918425e861c989bb517bec Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 6 Apr 2019 11:08:49 +1000 Subject: [PATCH] Added the Plugin section to config.json Added support for ZIP files to the backups route. The post counter won't be incremented if json marshalling fails now. Added the router_pre_route hook. Added the router_end hook. Added the action_end_create_topic hook. Added the action_end_create_reply hook. --- common/extend.go | 7 +++++++ common/site.go | 4 ++++ docs/configuration.md | 12 ++++++++++-- gen_router.go | 10 +++++++++- router_gen/main.go | 10 +++++++++- routes/panel/backups.go | 33 ++++++++++++++++++++------------- routes/reply.go | 11 +++++++---- routes/topic.go | 7 ++++++- 8 files changed, 72 insertions(+), 22 deletions(-) diff --git a/common/extend.go b/common/extend.go index cb650d62..e0080f2b 100644 --- a/common/extend.go +++ b/common/extend.go @@ -76,10 +76,17 @@ var hookTable = &HookTable{ "topic_reply_row_assign": nil, "create_group_preappend": nil, // What is this? Investigate! "topic_create_pre_loop": nil, + + "router_end": nil, }, map[string]func(...interface{}) (bool, RouteError){ "simple_forum_check_pre_perms": nil, "forum_check_pre_perms": nil, + + "action_end_create_topic": nil, + "action_end_create_reply": nil, + + "router_pre_route": nil, }, map[string][]func(string) string{ "preparse_preassign": nil, diff --git a/common/site.go b/common/site.go index dc8ceef4..a80091c5 100644 --- a/common/site.go +++ b/common/site.go @@ -21,6 +21,8 @@ var Config = new(config) // Dev holds build flags and other things which should only be modified during developers or to gather additional test data var Dev = new(devConfig) +var PluginConfig = map[string]string{} + type site struct { ShortName string Name string @@ -120,6 +122,7 @@ type configHolder struct { Config *config Database *dbConfig Dev *devConfig + Plugin map[string]string } func LoadConfig() error { @@ -138,6 +141,7 @@ func LoadConfig() error { Config = config.Config DbConfig = config.Database Dev = config.Dev + PluginConfig = config.Plugin return nil } diff --git a/docs/configuration.md b/docs/configuration.md index 8492219d..41fa30fb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2,9 +2,17 @@ For configuring the system, Gosora has a file called `config/config.json` which you can tweak to change various behaviours, it also has a few settings in the Setting Manager in the Control Panel. -The configuration file has four categories you may be familiar with from poring through it's contents. Site, Config, Database and Dev. +The configuration file has five categories you may be familiar with from poring through it's contents. Site, Config, Database, Dev and Plugin. -Site is for critical settings, Config is for lower priority yet still important settings, Database contains the credentials for the database (you will be able to pass these via parameters to the binary in a future version), and Dev is for a few flags which help out with the development of Gosora. +Site is for critical settings. + +Config is for lower priority yet still important settings. + +Database contains the credentials for the database (you will be able to pass these via parameters to the binary in a future version). + +Dev is for a few flags which help out with the development of Gosora. + +Plugin which you may not have run into is a category in which plugins can define their own custom configuration settings. An example of what the file might look like: https://github.com/Azareal/Gosora/blob/master/config/config_example.json diff --git a/gen_router.go b/gen_router.go index ccf0ae02..86f366ca 100644 --- a/gen_router.go +++ b/gen_router.go @@ -994,10 +994,18 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { w = common.GzipResponseWriter{Writer: gz, ResponseWriter: w} } - ferr := r.routeSwitch(w, req, user, prefix, extraData) + // TODO: Use the same hook table as downstream + hTbl := common.GetHookTable() + skip, ferr := hTbl.VhookSkippable("router_pre_route", w, req, user, prefix, extraData) + if skip || ferr != nil { + r.handleError(ferr,w,req,user) + } + ferr = r.routeSwitch(w, req, user, prefix, extraData) if ferr != nil { r.handleError(ferr,w,req,user) } + + hTbl.VhookNoRet("router_end", w, req, user, prefix, extraData) //common.StoppedServer("Profile end") } diff --git a/router_gen/main.go b/router_gen/main.go index af2ce40d..52ba0001 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -773,10 +773,18 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { w = common.GzipResponseWriter{Writer: gz, ResponseWriter: w} } - ferr := r.routeSwitch(w, req, user, prefix, extraData) + // TODO: Use the same hook table as downstream + hTbl := common.GetHookTable() + skip, ferr := hTbl.VhookSkippable("router_pre_route", w, req, user, prefix, extraData) + if skip || ferr != nil { + r.handleError(ferr,w,req,user) + } + ferr = r.routeSwitch(w, req, user, prefix, extraData) if ferr != nil { r.handleError(ferr,w,req,user) } + + hTbl.VhookNoRet("router_end", w, req, user, prefix, extraData) //common.StoppedServer("Profile end") } diff --git a/routes/panel/backups.go b/routes/panel/backups.go index a2f8f289..f84ebe0d 100644 --- a/routes/panel/backups.go +++ b/routes/panel/backups.go @@ -21,20 +21,27 @@ func Backups(w http.ResponseWriter, r *http.Request, user common.User, backupURL backupURL = common.Stripslashes(backupURL) var ext = filepath.Ext("./backups/" + backupURL) - if ext == ".sql" { - info, err := os.Stat("./backups/" + backupURL) - if err != nil { - return common.NotFound(w, r, basePage.Header) - } - // TODO: Change the served filename to gosora_backup_%timestamp%.sql, the time the file was generated, not when it was modified aka what the name of it should be - w.Header().Set("Content-Disposition", "attachment; filename=gosora_backup.sql") - w.Header().Set("Content-Length", strconv.FormatInt(info.Size(), 10)) - w.Header().Set("Content-Type", "application/sql") - // TODO: Fix the problem where non-existent files aren't greeted with custom 404s on ServeFile()'s side - http.ServeFile(w, r, "./backups/"+backupURL) - return nil + if ext != ".sql" && ext != ".zip" { + return common.NotFound(w, r, basePage.Header) } - return common.NotFound(w, r, basePage.Header) + info, err := os.Stat("./backups/" + backupURL) + if err != nil { + return common.NotFound(w, r, basePage.Header) + } + w.Header().Set("Content-Length", strconv.FormatInt(info.Size(), 10)) + + if ext == ".sql" { + // TODO: Change the served filename to gosora_backup_%timestamp%.sql, the time the file was generated, not when it was modified aka what the name of it should be + w.Header().Set("Content-Disposition", "attachment; filename=gosora_backup.sql") + w.Header().Set("Content-Type", "application/sql") + } else { + // TODO: Change the served filename to gosora_backup_%timestamp%.zip, the time the file was generated, not when it was modified aka what the name of it should be + w.Header().Set("Content-Disposition", "attachment; filename=gosora_backup.zip") + w.Header().Set("Content-Type", "application/zip") + } + // TODO: Fix the problem where non-existent files aren't greeted with custom 404s on ServeFile()'s side + http.ServeFile(w, r, "./backups/"+backupURL) + return nil } var backupList []common.BackupItem diff --git a/routes/reply.go b/routes/reply.go index 9a2b393a..4c858ba2 100644 --- a/routes/reply.go +++ b/routes/reply.go @@ -40,7 +40,6 @@ type JsonReply struct { func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError { // TODO: Use this js := r.FormValue("js") == "1" - tid, err := strconv.Atoi(r.PostFormValue("tid")) if err != nil { return common.PreErrorJSQ("Failed to convert the Topic ID", w, r, js) @@ -54,7 +53,7 @@ func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User) } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) if ferr != nil { return ferr } @@ -189,6 +188,12 @@ func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User) page = common.LastPage(nTopic.PostCount-(len(rids)+offset), common.Config.ItemsPerPage) } + counters.PostCounter.Bump() + skip, rerr := lite.Hooks.VhookSkippable("action_end_create_reply", reply.ID) + if skip || rerr != nil { + return rerr + } + prid, _ := strconv.Atoi(r.FormValue("prid")) if js && (prid == 0 || rids[0] == prid) { outBytes, err := json.Marshal(JsonReply{common.ParseMessage(reply.Content, topic.ParentID, "forums")}) @@ -203,8 +208,6 @@ func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User) } http.Redirect(w, r, "/topic/"+strconv.Itoa(tid)+spage+"#post-"+strconv.Itoa(reply.ID), http.StatusSeeOther) } - - counters.PostCounter.Bump() return nil } diff --git a/routes/topic.go b/routes/topic.go index 5c8f89c8..6a3dcca5 100644 --- a/routes/topic.go +++ b/routes/topic.go @@ -444,7 +444,7 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, fid) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, fid) if ferr != nil { return ferr } @@ -537,6 +537,11 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) counters.PostCounter.Bump() counters.TopicCounter.Bump() + // TODO: Pass more data to this hook? + skip, rerr := lite.Hooks.VhookSkippable("action_end_create_topic", tid) + if skip || rerr != nil { + return rerr + } http.Redirect(w, r, "/topic/"+strconv.Itoa(tid), http.StatusSeeOther) return nil }