2018-12-24 17:45:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 A Bunch Tell LLC.
|
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 06:05:26 +00:00
|
|
|
|
2018-11-14 20:03:22 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
2019-04-11 17:56:07 +00:00
|
|
|
"database/sql"
|
2018-11-14 20:03:22 +00:00
|
|
|
"fmt"
|
2018-11-19 01:18:22 +00:00
|
|
|
"github.com/gogits/gogs/pkg/tool"
|
2018-11-19 02:58:50 +00:00
|
|
|
"github.com/gorilla/mux"
|
2018-11-14 20:03:22 +00:00
|
|
|
"github.com/writeas/impart"
|
|
|
|
"github.com/writeas/web-core/auth"
|
2018-12-10 21:02:42 +00:00
|
|
|
"github.com/writeas/web-core/log"
|
2018-12-03 22:30:31 +00:00
|
|
|
"github.com/writeas/writefreely/config"
|
2018-11-14 20:03:22 +00:00
|
|
|
"net/http"
|
2018-11-19 01:18:22 +00:00
|
|
|
"runtime"
|
2018-12-03 22:30:31 +00:00
|
|
|
"strconv"
|
2018-11-19 01:18:22 +00:00
|
|
|
"time"
|
2018-11-14 20:03:22 +00:00
|
|
|
)
|
|
|
|
|
2018-11-19 01:18:22 +00:00
|
|
|
var (
|
|
|
|
appStartTime = time.Now()
|
|
|
|
sysStatus systemStatus
|
|
|
|
)
|
|
|
|
|
2019-01-05 14:37:53 +00:00
|
|
|
const adminUsersPerPage = 30
|
|
|
|
|
2018-11-19 01:18:22 +00:00
|
|
|
type systemStatus struct {
|
|
|
|
Uptime string
|
|
|
|
NumGoroutine int
|
|
|
|
|
|
|
|
// General statistics.
|
|
|
|
MemAllocated string // bytes allocated and still in use
|
|
|
|
MemTotal string // bytes allocated (even if freed)
|
|
|
|
MemSys string // bytes obtained from system (sum of XxxSys below)
|
|
|
|
Lookups uint64 // number of pointer lookups
|
|
|
|
MemMallocs uint64 // number of mallocs
|
|
|
|
MemFrees uint64 // number of frees
|
|
|
|
|
|
|
|
// Main allocation heap statistics.
|
|
|
|
HeapAlloc string // bytes allocated and still in use
|
|
|
|
HeapSys string // bytes obtained from system
|
|
|
|
HeapIdle string // bytes in idle spans
|
|
|
|
HeapInuse string // bytes in non-idle span
|
|
|
|
HeapReleased string // bytes released to the OS
|
|
|
|
HeapObjects uint64 // total number of allocated objects
|
|
|
|
|
|
|
|
// Low-level fixed-size structure allocator statistics.
|
|
|
|
// Inuse is bytes used now.
|
|
|
|
// Sys is bytes obtained from system.
|
|
|
|
StackInuse string // bootstrap stacks
|
|
|
|
StackSys string
|
|
|
|
MSpanInuse string // mspan structures
|
|
|
|
MSpanSys string
|
|
|
|
MCacheInuse string // mcache structures
|
|
|
|
MCacheSys string
|
|
|
|
BuckHashSys string // profiling bucket hash table
|
|
|
|
GCSys string // GC metadata
|
|
|
|
OtherSys string // other system allocations
|
|
|
|
|
|
|
|
// Garbage collector statistics.
|
|
|
|
NextGC string // next run in HeapAlloc time (bytes)
|
|
|
|
LastGC string // last run in absolute time (ns)
|
|
|
|
PauseTotalNs string
|
|
|
|
PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
|
|
|
|
NumGC uint32
|
|
|
|
}
|
|
|
|
|
2019-01-05 03:28:29 +00:00
|
|
|
type inspectedCollection struct {
|
|
|
|
CollectionObj
|
|
|
|
Followers int
|
|
|
|
LastPost string
|
|
|
|
}
|
|
|
|
|
2019-04-06 17:23:22 +00:00
|
|
|
type instanceContent struct {
|
|
|
|
ID string
|
|
|
|
Type string
|
2019-04-11 17:56:07 +00:00
|
|
|
Title sql.NullString
|
2019-04-06 17:23:22 +00:00
|
|
|
Content string
|
|
|
|
Updated time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c instanceContent) UpdatedFriendly() string {
|
|
|
|
/*
|
|
|
|
// TODO: accept a locale in this method and use that for the format
|
|
|
|
var loc monday.Locale = monday.LocaleEnUS
|
|
|
|
return monday.Format(u.Created, monday.DateTimeFormatsByLocale[loc], loc)
|
|
|
|
*/
|
|
|
|
return c.Updated.Format("January 2, 2006, 3:04 PM")
|
|
|
|
}
|
|
|
|
|
2018-11-19 01:18:22 +00:00
|
|
|
func handleViewAdminDash(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
updateAppStats()
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
|
|
|
SysStatus systemStatus
|
2018-12-03 22:30:31 +00:00
|
|
|
Config config.AppCfg
|
|
|
|
|
|
|
|
Message, ConfigMessage string
|
2018-11-19 01:18:22 +00:00
|
|
|
}{
|
2018-11-19 02:58:50 +00:00
|
|
|
UserPage: NewUserPage(app, r, u, "Admin", nil),
|
|
|
|
SysStatus: sysStatus,
|
2018-12-03 22:30:31 +00:00
|
|
|
Config: app.cfg.App,
|
|
|
|
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
ConfigMessage: r.FormValue("cm"),
|
2018-11-19 02:58:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 01:18:22 +00:00
|
|
|
showUserPage(w, "admin", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-05 03:28:29 +00:00
|
|
|
func handleViewAdminUsers(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
|
|
|
|
2019-01-05 14:37:53 +00:00
|
|
|
Users *[]User
|
|
|
|
CurPage int
|
|
|
|
TotalUsers int64
|
|
|
|
TotalPages []int
|
2019-01-05 03:28:29 +00:00
|
|
|
}{
|
|
|
|
UserPage: NewUserPage(app, r, u, "Users", nil),
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
}
|
|
|
|
|
2019-01-05 14:37:53 +00:00
|
|
|
p.TotalUsers = app.db.GetAllUsersCount()
|
|
|
|
ttlPages := p.TotalUsers / adminUsersPerPage
|
|
|
|
p.TotalPages = []int{}
|
|
|
|
for i := 1; i <= int(ttlPages); i++ {
|
|
|
|
p.TotalPages = append(p.TotalPages, i)
|
|
|
|
}
|
|
|
|
|
2019-01-05 03:28:29 +00:00
|
|
|
var err error
|
2019-01-05 14:37:53 +00:00
|
|
|
p.CurPage, err = strconv.Atoi(r.FormValue("p"))
|
|
|
|
if err != nil || p.CurPage < 1 {
|
|
|
|
p.CurPage = 1
|
|
|
|
} else if p.CurPage > int(ttlPages) {
|
|
|
|
p.CurPage = int(ttlPages)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Users, err = app.db.GetAllUsers(uint(p.CurPage))
|
2019-01-05 03:28:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get users: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "users", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleViewAdminUser(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
username := vars["username"]
|
|
|
|
if username == "" {
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/users"}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
|
|
|
|
|
|
|
User *User
|
|
|
|
Colls []inspectedCollection
|
|
|
|
LastPost string
|
|
|
|
|
|
|
|
TotalPosts int64
|
|
|
|
}{
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
Colls: []inspectedCollection{},
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p.User, err = app.db.GetUserForAuth(username)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user: %v", err)}
|
|
|
|
}
|
|
|
|
p.UserPage = NewUserPage(app, r, u, p.User.Username, nil)
|
|
|
|
p.TotalPosts = app.db.GetUserPostsCount(p.User.ID)
|
|
|
|
lp, err := app.db.GetUserLastPostTime(p.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's last post time: %v", err)}
|
|
|
|
}
|
|
|
|
if lp != nil {
|
|
|
|
p.LastPost = lp.Format("January 2, 2006, 3:04 PM")
|
|
|
|
}
|
|
|
|
|
|
|
|
colls, err := app.db.GetCollections(p.User)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's collections: %v", err)}
|
|
|
|
}
|
|
|
|
for _, c := range *colls {
|
|
|
|
ic := inspectedCollection{
|
|
|
|
CollectionObj: CollectionObj{Collection: c},
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.cfg.App.Federation {
|
|
|
|
folls, err := app.db.GetAPFollowers(&c)
|
|
|
|
if err == nil {
|
|
|
|
// TODO: handle error here (at least log it)
|
|
|
|
ic.Followers = len(*folls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.db.GetPostsCount(&ic.CollectionObj, true)
|
|
|
|
|
|
|
|
lp, err := app.db.GetCollectionLastPostTime(c.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Didn't get last post time for collection %d: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
if lp != nil {
|
|
|
|
ic.LastPost = lp.Format("January 2, 2006, 3:04 PM")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Colls = append(p.Colls, ic)
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "view-user", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-06 17:23:22 +00:00
|
|
|
func handleViewAdminPages(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
|
|
|
|
|
|
|
Pages []*instanceContent
|
|
|
|
}{
|
|
|
|
UserPage: NewUserPage(app, r, u, "Pages", nil),
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p.Pages, err = app.db.GetInstancePages()
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get pages: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in default pages
|
|
|
|
var hasAbout, hasPrivacy bool
|
2019-04-11 17:56:07 +00:00
|
|
|
for i, c := range p.Pages {
|
2019-04-06 17:23:22 +00:00
|
|
|
if hasAbout && hasPrivacy {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if c.ID == "about" {
|
|
|
|
hasAbout = true
|
2019-04-11 17:56:07 +00:00
|
|
|
if !c.Title.Valid {
|
|
|
|
p.Pages[i].Title = defaultAboutTitle(app.cfg)
|
|
|
|
}
|
2019-04-06 17:23:22 +00:00
|
|
|
} else if c.ID == "privacy" {
|
|
|
|
hasPrivacy = true
|
2019-04-11 17:56:07 +00:00
|
|
|
if !c.Title.Valid {
|
|
|
|
p.Pages[i].Title = defaultPrivacyTitle()
|
|
|
|
}
|
2019-04-06 17:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasAbout {
|
|
|
|
p.Pages = append(p.Pages, &instanceContent{
|
|
|
|
ID: "about",
|
2019-04-11 17:56:07 +00:00
|
|
|
Title: defaultAboutTitle(app.cfg),
|
2019-04-06 17:23:22 +00:00
|
|
|
Content: defaultAboutPage(app.cfg),
|
|
|
|
Updated: defaultPageUpdatedTime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !hasPrivacy {
|
|
|
|
p.Pages = append(p.Pages, &instanceContent{
|
|
|
|
ID: "privacy",
|
2019-04-11 17:56:07 +00:00
|
|
|
Title: defaultPrivacyTitle(),
|
2019-04-06 17:23:22 +00:00
|
|
|
Content: defaultPrivacyPolicy(app.cfg),
|
|
|
|
Updated: defaultPageUpdatedTime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "pages", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleViewAdminPage(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
slug := vars["slug"]
|
|
|
|
if slug == "" {
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/pages"}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
|
|
|
|
|
|
|
Content *instanceContent
|
|
|
|
}{
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
// Get pre-defined pages, or select slug
|
|
|
|
if slug == "about" {
|
|
|
|
p.Content, err = getAboutPage(app)
|
|
|
|
} else if slug == "privacy" {
|
|
|
|
p.Content, err = getPrivacyPage(app)
|
|
|
|
} else {
|
|
|
|
p.Content, err = app.db.GetDynamicContent(slug)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get page: %v", err)}
|
|
|
|
}
|
2019-04-11 17:56:07 +00:00
|
|
|
title := "New page"
|
|
|
|
if p.Content != nil {
|
|
|
|
title = "Edit " + p.Content.ID
|
|
|
|
} else {
|
|
|
|
p.Content = &instanceContent{}
|
|
|
|
}
|
|
|
|
p.UserPage = NewUserPage(app, r, u, title, nil)
|
2019-04-06 17:23:22 +00:00
|
|
|
|
|
|
|
showUserPage(w, "view-page", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-19 02:58:50 +00:00
|
|
|
func handleAdminUpdateSite(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["page"]
|
|
|
|
|
|
|
|
// Validate
|
|
|
|
if id != "about" && id != "privacy" {
|
|
|
|
return impart.HTTPError{http.StatusNotFound, "No such page."}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update page
|
|
|
|
m := ""
|
2019-04-11 17:56:07 +00:00
|
|
|
err := app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "page")
|
2018-11-19 02:58:50 +00:00
|
|
|
if err != nil {
|
|
|
|
m = "?m=" + err.Error()
|
|
|
|
}
|
2019-04-06 17:23:22 +00:00
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
|
2018-11-19 02:58:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 22:30:31 +00:00
|
|
|
func handleAdminUpdateConfig(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
app.cfg.App.SiteName = r.FormValue("site_name")
|
2018-12-03 23:36:33 +00:00
|
|
|
app.cfg.App.SiteDesc = r.FormValue("site_desc")
|
2018-12-03 22:30:31 +00:00
|
|
|
app.cfg.App.OpenRegistration = r.FormValue("open_registration") == "on"
|
|
|
|
mul, err := strconv.Atoi(r.FormValue("min_username_len"))
|
|
|
|
if err == nil {
|
|
|
|
app.cfg.App.MinUsernameLen = mul
|
|
|
|
}
|
|
|
|
mb, err := strconv.Atoi(r.FormValue("max_blogs"))
|
|
|
|
if err == nil {
|
|
|
|
app.cfg.App.MaxBlogs = mb
|
|
|
|
}
|
|
|
|
app.cfg.App.Federation = r.FormValue("federation") == "on"
|
|
|
|
app.cfg.App.PublicStats = r.FormValue("public_stats") == "on"
|
|
|
|
app.cfg.App.Private = r.FormValue("private") == "on"
|
2018-12-10 21:02:42 +00:00
|
|
|
app.cfg.App.LocalTimeline = r.FormValue("local_timeline") == "on"
|
|
|
|
if app.cfg.App.LocalTimeline && app.timeline == nil {
|
|
|
|
log.Info("Initializing local timeline...")
|
|
|
|
initLocalTimeline(app)
|
|
|
|
}
|
2019-01-18 05:05:50 +00:00
|
|
|
app.cfg.App.UserInvites = r.FormValue("user_invites")
|
|
|
|
if app.cfg.App.UserInvites == "none" {
|
|
|
|
app.cfg.App.UserInvites = ""
|
|
|
|
}
|
2018-12-03 22:30:31 +00:00
|
|
|
|
|
|
|
m := "?cm=Configuration+saved."
|
2018-12-08 22:49:19 +00:00
|
|
|
err = config.Save(app.cfg, app.cfgFile)
|
2018-12-03 22:30:31 +00:00
|
|
|
if err != nil {
|
|
|
|
m = "?cm=" + err.Error()
|
|
|
|
}
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin" + m + "#config"}
|
|
|
|
}
|
|
|
|
|
2018-11-19 01:18:22 +00:00
|
|
|
func updateAppStats() {
|
|
|
|
sysStatus.Uptime = tool.TimeSincePro(appStartTime)
|
|
|
|
|
|
|
|
m := new(runtime.MemStats)
|
|
|
|
runtime.ReadMemStats(m)
|
|
|
|
sysStatus.NumGoroutine = runtime.NumGoroutine()
|
|
|
|
|
|
|
|
sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
|
|
|
|
sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
|
|
|
|
sysStatus.MemSys = tool.FileSize(int64(m.Sys))
|
|
|
|
sysStatus.Lookups = m.Lookups
|
|
|
|
sysStatus.MemMallocs = m.Mallocs
|
|
|
|
sysStatus.MemFrees = m.Frees
|
|
|
|
|
|
|
|
sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
|
|
|
|
sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
|
|
|
|
sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
|
|
|
|
sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
|
|
|
|
sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
|
|
|
|
sysStatus.HeapObjects = m.HeapObjects
|
|
|
|
|
|
|
|
sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
|
|
|
|
sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
|
|
|
|
sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
|
|
|
|
sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
|
|
|
|
sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
|
|
|
|
sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
|
|
|
|
sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
|
|
|
|
sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
|
|
|
|
sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
|
|
|
|
|
|
|
|
sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
|
|
|
|
sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
|
|
|
|
sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
|
|
|
|
sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
|
|
|
|
sysStatus.NumGC = m.NumGC
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:03:22 +00:00
|
|
|
func adminResetPassword(app *app, u *User, newPass string) error {
|
|
|
|
hashedPass, err := auth.HashPass([]byte(newPass))
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.db.ChangePassphrase(u.ID, true, "", hashedPass)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|