2018-05-27 09:36:35 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-09-29 05:10:05 +00:00
|
|
|
p "github.com/Azareal/Gosora/common/phrases"
|
2018-05-27 09:36:35 +00:00
|
|
|
)
|
|
|
|
|
2018-08-30 05:53:21 +00:00
|
|
|
// TODO: Link the usernames for successful registrations to the profiles
|
2019-04-19 07:25:49 +00:00
|
|
|
func LogsRegs(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "registration_logs", "logs")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
logCount := c.RegLogs.Count()
|
2018-05-27 09:36:35 +00:00
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2019-06-04 11:51:15 +00:00
|
|
|
perPage := 12
|
2019-04-19 07:25:49 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(logCount, page, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
logs, err := c.RegLogs.GetOffset(offset, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-09-29 05:10:05 +00:00
|
|
|
llist := make([]c.PageRegLogItem, len(logs))
|
2018-05-27 09:36:35 +00:00
|
|
|
for index, log := range logs {
|
2019-04-19 07:25:49 +00:00
|
|
|
llist[index] = c.PageRegLogItem{log, strings.Replace(strings.TrimSuffix(log.FailureReason, "|"), "|", " | ", -1)}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelRegLogsPage{basePage, llist, c.Paginator{pageList, page, lastPage}}
|
2019-10-26 23:11:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_reglogs", pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Log errors when something really screwy is going on?
|
2019-04-18 01:00:39 +00:00
|
|
|
// TODO: Base the slugs on the localised usernames?
|
2020-02-04 11:47:03 +00:00
|
|
|
func handleUnknownUser(u *c.User, err error) *c.User {
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return &c.User{Name: p.GetTmplPhrase("user_unknown"), Link: c.BuildProfileURL("unknown", 0)}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2020-02-04 11:47:03 +00:00
|
|
|
return u
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2020-02-04 11:47:03 +00:00
|
|
|
func handleUnknownTopic(t *c.Topic, err error) *c.Topic {
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-09-29 05:10:05 +00:00
|
|
|
return &c.Topic{Title: p.GetTmplPhrase("topic_unknown"), Link: c.BuildTopicURL("unknown", 0)}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2020-02-04 11:47:03 +00:00
|
|
|
return t
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Move the log building logic into /common/ and it's own abstraction
|
2020-02-04 11:47:03 +00:00
|
|
|
func topicElementTypeAction(action, elementType string, elementID int, actor *c.User, topic *c.Topic) (out string) {
|
2018-05-27 09:36:35 +00:00
|
|
|
if action == "delete" {
|
2019-12-05 23:57:44 +00:00
|
|
|
return p.GetTmplPhrasef("panel_logs_mod_action_topic_delete", elementID, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-04-18 01:00:39 +00:00
|
|
|
var tbit string
|
2019-04-17 10:12:35 +00:00
|
|
|
aarr := strings.Split(action, "-")
|
|
|
|
switch aarr[0] {
|
2019-10-26 23:11:09 +00:00
|
|
|
case "lock", "unlock", "stick", "unstick":
|
2019-04-18 01:00:39 +00:00
|
|
|
tbit = aarr[0]
|
2018-05-27 09:36:35 +00:00
|
|
|
case "move":
|
2019-04-17 10:12:35 +00:00
|
|
|
if len(aarr) == 2 {
|
|
|
|
fid, _ := strconv.Atoi(aarr[1])
|
2019-04-19 07:25:49 +00:00
|
|
|
forum, err := c.Forums.Get(fid)
|
2019-04-17 10:12:35 +00:00
|
|
|
if err == nil {
|
2019-12-05 23:57:44 +00:00
|
|
|
return p.GetTmplPhrasef("panel_logs_mod_action_topic_move_dest", topic.Link, topic.Title, forum.Link, forum.Name, actor.Link, actor.Name)
|
2019-04-17 10:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 01:00:39 +00:00
|
|
|
tbit = "move"
|
2018-05-27 09:36:35 +00:00
|
|
|
default:
|
2019-12-05 23:57:44 +00:00
|
|
|
return p.GetTmplPhrasef("panel_logs_mod_action_topic_unknown", action, elementType, actor.Link, actor.Name)
|
2019-04-18 01:00:39 +00:00
|
|
|
}
|
|
|
|
if tbit != "" {
|
2019-12-05 23:57:44 +00:00
|
|
|
return p.GetTmplPhrasef("panel_logs_mod_action_topic_"+tbit, topic.Link, topic.Title, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf(out, topic.Link, topic.Title, actor.Link, actor.Name)
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:47:03 +00:00
|
|
|
func modlogsElementType(action, elementType string, elementID int, actor *c.User) (out string) {
|
2018-05-27 09:36:35 +00:00
|
|
|
switch elementType {
|
|
|
|
case "topic":
|
2019-04-19 07:25:49 +00:00
|
|
|
topic := handleUnknownTopic(c.Topics.Get(elementID))
|
2018-05-27 09:36:35 +00:00
|
|
|
out = topicElementTypeAction(action, elementType, elementID, actor, topic)
|
|
|
|
case "user":
|
2019-04-19 07:25:49 +00:00
|
|
|
targetUser := handleUnknownUser(c.Users.Get(elementID))
|
2019-12-05 23:57:44 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_mod_action_user_"+action, targetUser.Link, targetUser.Name, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
case "reply":
|
|
|
|
if action == "delete" {
|
2019-04-19 07:25:49 +00:00
|
|
|
topic := handleUnknownTopic(c.TopicByReplyID(elementID))
|
2019-12-05 23:57:44 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_mod_action_reply_delete", topic.Link, topic.Title, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2020-02-04 11:47:03 +00:00
|
|
|
case "profile-reply":
|
|
|
|
if action == "delete" {
|
|
|
|
// TODO: Optimise this
|
|
|
|
var profile *c.User
|
|
|
|
profileReply, err := c.Prstore.Get(elementID)
|
|
|
|
if err != nil {
|
|
|
|
profile = &c.User{Name: p.GetTmplPhrase("user_unknown"), Link: c.BuildProfileURL("unknown", 0)}
|
|
|
|
} else {
|
|
|
|
profile = handleUnknownUser(c.Users.Get(profileReply.ParentID))
|
|
|
|
}
|
|
|
|
out = p.GetTmplPhrasef("panel_logs_mod_action_profile_reply_delete", profile.Link, profile.Name, actor.Link, actor.Name)
|
|
|
|
}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
if out == "" {
|
2019-12-05 23:57:44 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_mod_action_unknown", action, elementType, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:47:03 +00:00
|
|
|
func adminlogsElementType(action, elementType string, elementID int, actor *c.User, extra string) (out string) {
|
2019-11-06 02:00:44 +00:00
|
|
|
switch elementType {
|
|
|
|
// TODO: Record more detail for this, e.g. which field/s was changed
|
|
|
|
case "user":
|
|
|
|
targetUser := handleUnknownUser(c.Users.Get(elementID))
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_user_"+action, targetUser.Link, targetUser.Name, actor.Link, actor.Name)
|
2019-11-08 07:52:30 +00:00
|
|
|
case "group":
|
|
|
|
g, err := c.Groups.Get(elementID)
|
|
|
|
if err != nil {
|
|
|
|
g = &c.Group{Name: p.GetTmplPhrase("group_unknown")}
|
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_group_"+action, "/panel/groups/edit/"+strconv.Itoa(g.ID), g.Name, actor.Link, actor.Name)
|
2019-11-08 07:52:30 +00:00
|
|
|
case "group_promotion":
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_group_promotion_"+action, actor.Link, actor.Name)
|
2019-11-08 10:08:37 +00:00
|
|
|
case "forum":
|
|
|
|
f, err := c.Forums.Get(elementID)
|
|
|
|
if err != nil {
|
|
|
|
f = &c.Forum{Name: p.GetTmplPhrase("forum_unknown")}
|
|
|
|
}
|
|
|
|
if action == "reorder" {
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_forum_reorder", actor.Link, actor.Name)
|
2019-11-08 10:08:37 +00:00
|
|
|
} else {
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_forum_"+action, "/panel/forums/edit/"+strconv.Itoa(f.ID), f.Name, actor.Link, actor.Name)
|
2019-11-08 10:08:37 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
case "page":
|
|
|
|
pp, err := c.Pages.Get(elementID)
|
|
|
|
if err != nil {
|
|
|
|
pp = &c.CustomPage{Name: p.GetTmplPhrase("page_unknown")}
|
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_page_"+action, "/panel/pages/edit/"+strconv.Itoa(pp.ID), pp.Name, actor.Link, actor.Name)
|
2019-11-08 21:46:50 +00:00
|
|
|
case "setting":
|
|
|
|
s, err := c.SettingBox.Load().(c.SettingMap).BypassGet(action)
|
|
|
|
if err != nil {
|
|
|
|
s = &c.Setting{Name: p.GetTmplPhrase("setting_unknown")}
|
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_setting_edit", "/panel/settings/edit/"+s.Name, s.Name, actor.Link, actor.Name)
|
2019-11-08 21:46:50 +00:00
|
|
|
case "word_filter":
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_word_filter_"+action, actor.Link, actor.Name)
|
|
|
|
case "menu":
|
|
|
|
if action == "suborder" {
|
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_menu_suborder", elementID, actor.Link, actor.Name)
|
2019-11-08 21:46:50 +00:00
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
case "menu_item":
|
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_menu_item_"+action, "/panel/themes/menus/item/edit/"+strconv.Itoa(elementID), elementID, actor.Link, actor.Name)
|
|
|
|
case "widget":
|
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_widget_"+action, "/panel/themes/widgets/", elementID, actor.Link, actor.Name)
|
|
|
|
case "plugin":
|
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_plugin_"+action, extra, actor.Link, actor.Name)
|
2019-11-08 21:46:50 +00:00
|
|
|
case "backup":
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_backup_"+action, actor.Link, actor.Name)
|
2019-11-06 02:00:44 +00:00
|
|
|
}
|
|
|
|
if out == "" {
|
2019-11-10 02:37:53 +00:00
|
|
|
out = p.GetTmplPhrasef("panel_logs_admin_action_unknown", action, elementType, actor.Link, actor.Name)
|
2019-11-06 02:00:44 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
func LogsMod(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "mod_logs", "logs")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2019-06-04 11:51:15 +00:00
|
|
|
perPage := 12
|
2019-10-26 23:11:09 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(c.ModLogs.Count(), page, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
logs, err := c.ModLogs.GetOffset(offset, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-08-31 22:34:43 +00:00
|
|
|
llist := make([]c.PageLogItem, len(logs))
|
2018-05-27 09:36:35 +00:00
|
|
|
for index, log := range logs {
|
2019-04-19 07:25:49 +00:00
|
|
|
actor := handleUnknownUser(c.Users.Get(log.ActorID))
|
2018-05-27 09:36:35 +00:00
|
|
|
action := modlogsElementType(log.Action, log.ElementType, log.ElementID, actor)
|
2019-08-31 22:34:43 +00:00
|
|
|
llist[index] = c.PageLogItem{Action: template.HTML(action), IP: log.IP, DoneAt: log.DoneAt}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelLogsPage{basePage, llist, c.Paginator{pageList, page, lastPage}}
|
2019-10-26 23:11:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_modlogs", pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
func LogsAdmin(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "admin_logs", "logs")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2019-06-04 11:51:15 +00:00
|
|
|
perPage := 12
|
2019-11-06 02:00:44 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(c.AdminLogs.Count(), page, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
logs, err := c.AdminLogs.GetOffset(offset, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-08-31 22:34:43 +00:00
|
|
|
llist := make([]c.PageLogItem, len(logs))
|
2018-05-27 09:36:35 +00:00
|
|
|
for index, log := range logs {
|
2019-04-19 07:25:49 +00:00
|
|
|
actor := handleUnknownUser(c.Users.Get(log.ActorID))
|
2019-11-10 02:37:53 +00:00
|
|
|
action := adminlogsElementType(log.Action, log.ElementType, log.ElementID, actor, log.Extra)
|
2019-08-31 22:34:43 +00:00
|
|
|
llist[index] = c.PageLogItem{Action: template.HTML(action), IP: log.IP, DoneAt: log.DoneAt}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelLogsPage{basePage, llist, c.Paginator{pageList, page, lastPage}}
|
2019-10-26 23:11:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_adminlogs", pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|