A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

328 Zeilen
9.2 KiB

  1. /*
  2. * Copyright © 2018 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "fmt"
  13. "github.com/gogits/gogs/pkg/tool"
  14. "github.com/gorilla/mux"
  15. "github.com/writeas/impart"
  16. "github.com/writeas/web-core/auth"
  17. "github.com/writeas/web-core/log"
  18. "github.com/writeas/writefreely/config"
  19. "net/http"
  20. "runtime"
  21. "strconv"
  22. "time"
  23. )
  24. var (
  25. appStartTime = time.Now()
  26. sysStatus systemStatus
  27. )
  28. const adminUsersPerPage = 30
  29. type systemStatus struct {
  30. Uptime string
  31. NumGoroutine int
  32. // General statistics.
  33. MemAllocated string // bytes allocated and still in use
  34. MemTotal string // bytes allocated (even if freed)
  35. MemSys string // bytes obtained from system (sum of XxxSys below)
  36. Lookups uint64 // number of pointer lookups
  37. MemMallocs uint64 // number of mallocs
  38. MemFrees uint64 // number of frees
  39. // Main allocation heap statistics.
  40. HeapAlloc string // bytes allocated and still in use
  41. HeapSys string // bytes obtained from system
  42. HeapIdle string // bytes in idle spans
  43. HeapInuse string // bytes in non-idle span
  44. HeapReleased string // bytes released to the OS
  45. HeapObjects uint64 // total number of allocated objects
  46. // Low-level fixed-size structure allocator statistics.
  47. // Inuse is bytes used now.
  48. // Sys is bytes obtained from system.
  49. StackInuse string // bootstrap stacks
  50. StackSys string
  51. MSpanInuse string // mspan structures
  52. MSpanSys string
  53. MCacheInuse string // mcache structures
  54. MCacheSys string
  55. BuckHashSys string // profiling bucket hash table
  56. GCSys string // GC metadata
  57. OtherSys string // other system allocations
  58. // Garbage collector statistics.
  59. NextGC string // next run in HeapAlloc time (bytes)
  60. LastGC string // last run in absolute time (ns)
  61. PauseTotalNs string
  62. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  63. NumGC uint32
  64. }
  65. type inspectedCollection struct {
  66. CollectionObj
  67. Followers int
  68. LastPost string
  69. }
  70. func handleViewAdminDash(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
  71. updateAppStats()
  72. p := struct {
  73. *UserPage
  74. SysStatus systemStatus
  75. Config config.AppCfg
  76. Message, ConfigMessage string
  77. AboutPage, PrivacyPage string
  78. }{
  79. UserPage: NewUserPage(app, r, u, "Admin", nil),
  80. SysStatus: sysStatus,
  81. Config: app.cfg.App,
  82. Message: r.FormValue("m"),
  83. ConfigMessage: r.FormValue("cm"),
  84. }
  85. var err error
  86. p.AboutPage, err = getAboutPage(app)
  87. if err != nil {
  88. return err
  89. }
  90. p.PrivacyPage, _, err = getPrivacyPage(app)
  91. if err != nil {
  92. return err
  93. }
  94. showUserPage(w, "admin", p)
  95. return nil
  96. }
  97. func handleViewAdminUsers(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
  98. p := struct {
  99. *UserPage
  100. Config config.AppCfg
  101. Message string
  102. Users *[]User
  103. CurPage int
  104. TotalUsers int64
  105. TotalPages []int
  106. }{
  107. UserPage: NewUserPage(app, r, u, "Users", nil),
  108. Config: app.cfg.App,
  109. Message: r.FormValue("m"),
  110. }
  111. p.TotalUsers = app.db.GetAllUsersCount()
  112. ttlPages := p.TotalUsers / adminUsersPerPage
  113. p.TotalPages = []int{}
  114. for i := 1; i <= int(ttlPages); i++ {
  115. p.TotalPages = append(p.TotalPages, i)
  116. }
  117. var err error
  118. p.CurPage, err = strconv.Atoi(r.FormValue("p"))
  119. if err != nil || p.CurPage < 1 {
  120. p.CurPage = 1
  121. } else if p.CurPage > int(ttlPages) {
  122. p.CurPage = int(ttlPages)
  123. }
  124. p.Users, err = app.db.GetAllUsers(uint(p.CurPage))
  125. if err != nil {
  126. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get users: %v", err)}
  127. }
  128. showUserPage(w, "users", p)
  129. return nil
  130. }
  131. func handleViewAdminUser(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
  132. vars := mux.Vars(r)
  133. username := vars["username"]
  134. if username == "" {
  135. return impart.HTTPError{http.StatusFound, "/admin/users"}
  136. }
  137. p := struct {
  138. *UserPage
  139. Config config.AppCfg
  140. Message string
  141. User *User
  142. Colls []inspectedCollection
  143. LastPost string
  144. TotalPosts int64
  145. }{
  146. Config: app.cfg.App,
  147. Message: r.FormValue("m"),
  148. Colls: []inspectedCollection{},
  149. }
  150. var err error
  151. p.User, err = app.db.GetUserForAuth(username)
  152. if err != nil {
  153. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user: %v", err)}
  154. }
  155. p.UserPage = NewUserPage(app, r, u, p.User.Username, nil)
  156. p.TotalPosts = app.db.GetUserPostsCount(p.User.ID)
  157. lp, err := app.db.GetUserLastPostTime(p.User.ID)
  158. if err != nil {
  159. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's last post time: %v", err)}
  160. }
  161. if lp != nil {
  162. p.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  163. }
  164. colls, err := app.db.GetCollections(p.User)
  165. if err != nil {
  166. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's collections: %v", err)}
  167. }
  168. for _, c := range *colls {
  169. ic := inspectedCollection{
  170. CollectionObj: CollectionObj{Collection: c},
  171. }
  172. if app.cfg.App.Federation {
  173. folls, err := app.db.GetAPFollowers(&c)
  174. if err == nil {
  175. // TODO: handle error here (at least log it)
  176. ic.Followers = len(*folls)
  177. }
  178. }
  179. app.db.GetPostsCount(&ic.CollectionObj, true)
  180. lp, err := app.db.GetCollectionLastPostTime(c.ID)
  181. if err != nil {
  182. log.Error("Didn't get last post time for collection %d: %v", c.ID, err)
  183. }
  184. if lp != nil {
  185. ic.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  186. }
  187. p.Colls = append(p.Colls, ic)
  188. }
  189. showUserPage(w, "view-user", p)
  190. return nil
  191. }
  192. func handleAdminUpdateSite(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
  193. vars := mux.Vars(r)
  194. id := vars["page"]
  195. // Validate
  196. if id != "about" && id != "privacy" {
  197. return impart.HTTPError{http.StatusNotFound, "No such page."}
  198. }
  199. // Update page
  200. m := ""
  201. err := app.db.UpdateDynamicContent(id, r.FormValue("content"))
  202. if err != nil {
  203. m = "?m=" + err.Error()
  204. }
  205. return impart.HTTPError{http.StatusFound, "/admin" + m + "#page-" + id}
  206. }
  207. func handleAdminUpdateConfig(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
  208. app.cfg.App.SiteName = r.FormValue("site_name")
  209. app.cfg.App.SiteDesc = r.FormValue("site_desc")
  210. app.cfg.App.OpenRegistration = r.FormValue("open_registration") == "on"
  211. mul, err := strconv.Atoi(r.FormValue("min_username_len"))
  212. if err == nil {
  213. app.cfg.App.MinUsernameLen = mul
  214. }
  215. mb, err := strconv.Atoi(r.FormValue("max_blogs"))
  216. if err == nil {
  217. app.cfg.App.MaxBlogs = mb
  218. }
  219. app.cfg.App.Federation = r.FormValue("federation") == "on"
  220. app.cfg.App.PublicStats = r.FormValue("public_stats") == "on"
  221. app.cfg.App.Private = r.FormValue("private") == "on"
  222. app.cfg.App.LocalTimeline = r.FormValue("local_timeline") == "on"
  223. if app.cfg.App.LocalTimeline && app.timeline == nil {
  224. log.Info("Initializing local timeline...")
  225. initLocalTimeline(app)
  226. }
  227. app.cfg.App.UserInvites = r.FormValue("user_invites")
  228. if app.cfg.App.UserInvites == "none" {
  229. app.cfg.App.UserInvites = ""
  230. }
  231. m := "?cm=Configuration+saved."
  232. err = config.Save(app.cfg, app.cfgFile)
  233. if err != nil {
  234. m = "?cm=" + err.Error()
  235. }
  236. return impart.HTTPError{http.StatusFound, "/admin" + m + "#config"}
  237. }
  238. func updateAppStats() {
  239. sysStatus.Uptime = tool.TimeSincePro(appStartTime)
  240. m := new(runtime.MemStats)
  241. runtime.ReadMemStats(m)
  242. sysStatus.NumGoroutine = runtime.NumGoroutine()
  243. sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
  244. sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
  245. sysStatus.MemSys = tool.FileSize(int64(m.Sys))
  246. sysStatus.Lookups = m.Lookups
  247. sysStatus.MemMallocs = m.Mallocs
  248. sysStatus.MemFrees = m.Frees
  249. sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
  250. sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
  251. sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
  252. sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
  253. sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
  254. sysStatus.HeapObjects = m.HeapObjects
  255. sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
  256. sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
  257. sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
  258. sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
  259. sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  260. sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  261. sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  262. sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  263. sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  264. sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
  265. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  266. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  267. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  268. sysStatus.NumGC = m.NumGC
  269. }
  270. func adminResetPassword(app *app, u *User, newPass string) error {
  271. hashedPass, err := auth.HashPass([]byte(newPass))
  272. if err != nil {
  273. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  274. }
  275. err = app.db.ChangePassphrase(u.ID, true, "", hashedPass)
  276. if err != nil {
  277. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  278. }
  279. return nil
  280. }