2018-05-27 09:36:35 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-10-06 00:34:09 +00:00
|
|
|
p "github.com/Azareal/Gosora/common/phrases"
|
2018-05-27 09:36:35 +00:00
|
|
|
)
|
|
|
|
|
2020-03-18 09:21:34 +00:00
|
|
|
func Settings(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError {
|
|
|
|
basePage, ferr := buildBasePage(w, r, user, "settings", "settings")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.EditSettings {
|
2019-04-19 07:25:49 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:09:10 +00:00
|
|
|
// TODO: What if the list gets too long? How should we structure this?
|
2018-06-17 07:28:18 +00:00
|
|
|
settings, err := basePage.Settings.BypassGetAll()
|
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-10-06 00:34:09 +00:00
|
|
|
settingPhrases := p.GetAllSettingPhrases()
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
var settingList []*c.PanelSetting
|
2018-05-27 09:36:35 +00:00
|
|
|
for _, settingPtr := range settings {
|
2019-10-06 00:34:09 +00:00
|
|
|
s := settingPtr.Copy()
|
2020-03-21 07:46:43 +00:00
|
|
|
switch s.Type {
|
|
|
|
case "list":
|
2019-10-06 00:34:09 +00:00
|
|
|
llist := settingPhrases[s.Name+"_label"]
|
2018-05-27 09:36:35 +00:00
|
|
|
labels := strings.Split(llist, ",")
|
2019-10-06 00:34:09 +00:00
|
|
|
conv, err := strconv.Atoi(s.Content)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-10-06 00:34:09 +00:00
|
|
|
return c.LocalError("The setting '"+s.Name+"' can't be converted to an integer", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
s.Content = labels[conv-1]
|
|
|
|
// TODO: Localise this
|
2020-03-21 07:46:43 +00:00
|
|
|
case "bool":
|
2019-10-06 00:34:09 +00:00
|
|
|
if s.Content == "1" {
|
2020-03-21 07:46:43 +00:00
|
|
|
//s.Content = "Yes"
|
|
|
|
s.Content = p.GetTmplPhrase("option_yes")
|
2018-05-27 09:36:35 +00:00
|
|
|
} else {
|
2020-03-21 07:46:43 +00:00
|
|
|
//s.Content = "No"
|
|
|
|
s.Content = p.GetTmplPhrase("option_no")
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2020-03-21 07:46:43 +00:00
|
|
|
case "html-attribute":
|
2019-10-06 00:34:09 +00:00
|
|
|
s.Type = "textarea"
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
settingList = append(settingList, &c.PanelSetting{s, p.GetSettingPhrase(s.Name)})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelPage{basePage, tList, settingList}
|
2019-10-06 00:34:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_settings", &pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 07:46:43 +00:00
|
|
|
func SettingEdit(w http.ResponseWriter, r *http.Request, u *c.User, sname string) c.RouteError {
|
|
|
|
basePage, ferr := buildBasePage(w, r, u, "edit_setting", "settings")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-03-21 07:46:43 +00:00
|
|
|
if !u.Perms.EditSettings {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 07:46:43 +00:00
|
|
|
s, err := basePage.Settings.BypassGet(sname)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2020-03-21 07:46:43 +00:00
|
|
|
return c.LocalError("The setting you want to edit doesn't exist.", w, r, u)
|
2018-05-27 09:36:35 +00:00
|
|
|
} else 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-04-19 07:25:49 +00:00
|
|
|
var itemList []c.OptionLabel
|
2020-03-21 07:46:43 +00:00
|
|
|
if s.Type == "list" {
|
|
|
|
llist := p.GetSettingPhrase(s.Name + "_label")
|
|
|
|
conv, err := strconv.Atoi(s.Content)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2020-03-21 07:46:43 +00:00
|
|
|
return c.LocalError("The value of this setting couldn't be converted to an integer", w, r, u)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
for index, label := range strings.Split(llist, ",") {
|
2019-04-19 07:25:49 +00:00
|
|
|
itemList = append(itemList, c.OptionLabel{
|
2018-05-27 09:36:35 +00:00
|
|
|
Label: label,
|
|
|
|
Value: index + 1,
|
|
|
|
Selected: conv == (index + 1),
|
|
|
|
})
|
|
|
|
}
|
2020-03-21 07:46:43 +00:00
|
|
|
} else if s.Type == "html-attribute" {
|
|
|
|
s.Type = "textarea"
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 07:46:43 +00:00
|
|
|
pSetting := &c.PanelSetting{s, p.GetSettingPhrase(s.Name)}
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelSettingPage{basePage, itemList, pSetting}
|
2019-10-06 00:34:09 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_setting", &pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 07:46:43 +00:00
|
|
|
func SettingEditSubmit(w http.ResponseWriter, r *http.Request, u *c.User, name string) c.RouteError {
|
|
|
|
headerLite, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-03-21 07:46:43 +00:00
|
|
|
if !u.Perms.EditSettings {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 21:46:50 +00:00
|
|
|
name = c.SanitiseSingleLine(name)
|
|
|
|
content := c.SanitiseBody(r.PostFormValue("value"))
|
|
|
|
rerr := headerLite.Settings.Update(name, content)
|
2018-11-13 06:51:34 +00:00
|
|
|
if rerr != nil {
|
|
|
|
return rerr
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-11-08 21:46:50 +00:00
|
|
|
// TODO: Avoid this hack
|
2020-03-21 07:46:43 +00:00
|
|
|
err := c.AdminLogs.Create(name, 0, "setting", u.GetIP(), u.ID)
|
2019-11-08 21:46:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-05-27 09:36:35 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/settings/", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|