A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

527 lines
15 KiB

  1. /*
  2. * Copyright © 2018-2019 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. "database/sql"
  13. "fmt"
  14. "net/http"
  15. "runtime"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "github.com/gorilla/mux"
  20. "github.com/writeas/impart"
  21. "github.com/writeas/web-core/auth"
  22. "github.com/writeas/web-core/log"
  23. "github.com/writeas/web-core/passgen"
  24. "github.com/writeas/writefreely/appstats"
  25. "github.com/writeas/writefreely/config"
  26. )
  27. var (
  28. appStartTime = time.Now()
  29. sysStatus systemStatus
  30. )
  31. const adminUsersPerPage = 30
  32. type systemStatus struct {
  33. Uptime string
  34. NumGoroutine int
  35. // General statistics.
  36. MemAllocated string // bytes allocated and still in use
  37. MemTotal string // bytes allocated (even if freed)
  38. MemSys string // bytes obtained from system (sum of XxxSys below)
  39. Lookups uint64 // number of pointer lookups
  40. MemMallocs uint64 // number of mallocs
  41. MemFrees uint64 // number of frees
  42. // Main allocation heap statistics.
  43. HeapAlloc string // bytes allocated and still in use
  44. HeapSys string // bytes obtained from system
  45. HeapIdle string // bytes in idle spans
  46. HeapInuse string // bytes in non-idle span
  47. HeapReleased string // bytes released to the OS
  48. HeapObjects uint64 // total number of allocated objects
  49. // Low-level fixed-size structure allocator statistics.
  50. // Inuse is bytes used now.
  51. // Sys is bytes obtained from system.
  52. StackInuse string // bootstrap stacks
  53. StackSys string
  54. MSpanInuse string // mspan structures
  55. MSpanSys string
  56. MCacheInuse string // mcache structures
  57. MCacheSys string
  58. BuckHashSys string // profiling bucket hash table
  59. GCSys string // GC metadata
  60. OtherSys string // other system allocations
  61. // Garbage collector statistics.
  62. NextGC string // next run in HeapAlloc time (bytes)
  63. LastGC string // last run in absolute time (ns)
  64. PauseTotalNs string
  65. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  66. NumGC uint32
  67. }
  68. type inspectedCollection struct {
  69. CollectionObj
  70. Followers int
  71. LastPost string
  72. }
  73. type instanceContent struct {
  74. ID string
  75. Type string
  76. Title sql.NullString
  77. Content string
  78. Updated time.Time
  79. }
  80. func (c instanceContent) UpdatedFriendly() string {
  81. /*
  82. // TODO: accept a locale in this method and use that for the format
  83. var loc monday.Locale = monday.LocaleEnUS
  84. return monday.Format(u.Created, monday.DateTimeFormatsByLocale[loc], loc)
  85. */
  86. return c.Updated.Format("January 2, 2006, 3:04 PM")
  87. }
  88. func handleViewAdminDash(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  89. updateAppStats()
  90. p := struct {
  91. *UserPage
  92. SysStatus systemStatus
  93. Config config.AppCfg
  94. Message, ConfigMessage string
  95. }{
  96. UserPage: NewUserPage(app, r, u, "Admin", nil),
  97. SysStatus: sysStatus,
  98. Config: app.cfg.App,
  99. Message: r.FormValue("m"),
  100. ConfigMessage: r.FormValue("cm"),
  101. }
  102. showUserPage(w, "admin", p)
  103. return nil
  104. }
  105. func handleViewAdminUsers(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  106. p := struct {
  107. *UserPage
  108. Config config.AppCfg
  109. Message string
  110. Users *[]User
  111. CurPage int
  112. TotalUsers int64
  113. TotalPages []int
  114. }{
  115. UserPage: NewUserPage(app, r, u, "Users", nil),
  116. Config: app.cfg.App,
  117. Message: r.FormValue("m"),
  118. }
  119. p.TotalUsers = app.db.GetAllUsersCount()
  120. ttlPages := p.TotalUsers / adminUsersPerPage
  121. p.TotalPages = []int{}
  122. for i := 1; i <= int(ttlPages); i++ {
  123. p.TotalPages = append(p.TotalPages, i)
  124. }
  125. var err error
  126. p.CurPage, err = strconv.Atoi(r.FormValue("p"))
  127. if err != nil || p.CurPage < 1 {
  128. p.CurPage = 1
  129. } else if p.CurPage > int(ttlPages) {
  130. p.CurPage = int(ttlPages)
  131. }
  132. p.Users, err = app.db.GetAllUsers(uint(p.CurPage))
  133. if err != nil {
  134. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get users: %v", err)}
  135. }
  136. showUserPage(w, "users", p)
  137. return nil
  138. }
  139. func handleViewAdminUser(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  140. vars := mux.Vars(r)
  141. username := vars["username"]
  142. if username == "" {
  143. return impart.HTTPError{http.StatusFound, "/admin/users"}
  144. }
  145. p := struct {
  146. *UserPage
  147. Config config.AppCfg
  148. Message string
  149. User *User
  150. Colls []inspectedCollection
  151. LastPost string
  152. NewPassword string
  153. TotalPosts int64
  154. ClearEmail string
  155. }{
  156. Config: app.cfg.App,
  157. Message: r.FormValue("m"),
  158. Colls: []inspectedCollection{},
  159. }
  160. var err error
  161. p.User, err = app.db.GetUserForAuth(username)
  162. if err != nil {
  163. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user: %v", err)}
  164. }
  165. flashes, _ := getSessionFlashes(app, w, r, nil)
  166. for _, flash := range flashes {
  167. if strings.HasPrefix(flash, "SUCCESS: ") {
  168. p.NewPassword = strings.TrimPrefix(flash, "SUCCESS: ")
  169. p.ClearEmail = p.User.EmailClear(app.keys)
  170. }
  171. }
  172. p.UserPage = NewUserPage(app, r, u, p.User.Username, nil)
  173. p.TotalPosts = app.db.GetUserPostsCount(p.User.ID)
  174. lp, err := app.db.GetUserLastPostTime(p.User.ID)
  175. if err != nil {
  176. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's last post time: %v", err)}
  177. }
  178. if lp != nil {
  179. p.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  180. }
  181. colls, err := app.db.GetCollections(p.User, app.cfg.App.Host)
  182. if err != nil {
  183. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's collections: %v", err)}
  184. }
  185. for _, c := range *colls {
  186. ic := inspectedCollection{
  187. CollectionObj: CollectionObj{Collection: c},
  188. }
  189. if app.cfg.App.Federation {
  190. folls, err := app.db.GetAPFollowers(&c)
  191. if err == nil {
  192. // TODO: handle error here (at least log it)
  193. ic.Followers = len(*folls)
  194. }
  195. }
  196. app.db.GetPostsCount(&ic.CollectionObj, true)
  197. lp, err := app.db.GetCollectionLastPostTime(c.ID)
  198. if err != nil {
  199. log.Error("Didn't get last post time for collection %d: %v", c.ID, err)
  200. }
  201. if lp != nil {
  202. ic.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  203. }
  204. p.Colls = append(p.Colls, ic)
  205. }
  206. showUserPage(w, "view-user", p)
  207. return nil
  208. }
  209. func handleAdminToggleUserStatus(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  210. vars := mux.Vars(r)
  211. username := vars["username"]
  212. if username == "" {
  213. return impart.HTTPError{http.StatusFound, "/admin/users"}
  214. }
  215. user, err := app.db.GetUserForAuth(username)
  216. if err != nil {
  217. log.Error("failed to get user: %v", err)
  218. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user from username: %v", err)}
  219. }
  220. if user.IsSilenced() {
  221. err = app.db.SetUserStatus(user.ID, UserActive)
  222. } else {
  223. err = app.db.SetUserStatus(user.ID, UserSilenced)
  224. }
  225. if err != nil {
  226. log.Error("toggle user suspended: %v", err)
  227. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not toggle user status: %v", err)}
  228. }
  229. return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s#status", username)}
  230. }
  231. func handleAdminResetUserPass(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  232. vars := mux.Vars(r)
  233. username := vars["username"]
  234. if username == "" {
  235. return impart.HTTPError{http.StatusFound, "/admin/users"}
  236. }
  237. // Generate new random password since none supplied
  238. pass := passgen.NewWordish()
  239. hashedPass, err := auth.HashPass([]byte(pass))
  240. if err != nil {
  241. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  242. }
  243. userIDVal := r.FormValue("user")
  244. log.Info("ADMIN: Changing user %s password", userIDVal)
  245. id, err := strconv.Atoi(userIDVal)
  246. if err != nil {
  247. return impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Invalid user ID: %v", err)}
  248. }
  249. err = app.db.ChangePassphrase(int64(id), true, "", hashedPass)
  250. if err != nil {
  251. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  252. }
  253. log.Info("ADMIN: Successfully changed.")
  254. addSessionFlash(app, w, r, fmt.Sprintf("SUCCESS: %s", pass), nil)
  255. return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s", username)}
  256. }
  257. func handleViewAdminPages(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  258. p := struct {
  259. *UserPage
  260. Config config.AppCfg
  261. Message string
  262. Pages []*instanceContent
  263. }{
  264. UserPage: NewUserPage(app, r, u, "Pages", nil),
  265. Config: app.cfg.App,
  266. Message: r.FormValue("m"),
  267. }
  268. var err error
  269. p.Pages, err = app.db.GetInstancePages()
  270. if err != nil {
  271. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get pages: %v", err)}
  272. }
  273. // Add in default pages
  274. var hasAbout, hasPrivacy bool
  275. for i, c := range p.Pages {
  276. if hasAbout && hasPrivacy {
  277. break
  278. }
  279. if c.ID == "about" {
  280. hasAbout = true
  281. if !c.Title.Valid {
  282. p.Pages[i].Title = defaultAboutTitle(app.cfg)
  283. }
  284. } else if c.ID == "privacy" {
  285. hasPrivacy = true
  286. if !c.Title.Valid {
  287. p.Pages[i].Title = defaultPrivacyTitle()
  288. }
  289. }
  290. }
  291. if !hasAbout {
  292. p.Pages = append(p.Pages, &instanceContent{
  293. ID: "about",
  294. Title: defaultAboutTitle(app.cfg),
  295. Content: defaultAboutPage(app.cfg),
  296. Updated: defaultPageUpdatedTime,
  297. })
  298. }
  299. if !hasPrivacy {
  300. p.Pages = append(p.Pages, &instanceContent{
  301. ID: "privacy",
  302. Title: defaultPrivacyTitle(),
  303. Content: defaultPrivacyPolicy(app.cfg),
  304. Updated: defaultPageUpdatedTime,
  305. })
  306. }
  307. showUserPage(w, "pages", p)
  308. return nil
  309. }
  310. func handleViewAdminPage(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  311. vars := mux.Vars(r)
  312. slug := vars["slug"]
  313. if slug == "" {
  314. return impart.HTTPError{http.StatusFound, "/admin/pages"}
  315. }
  316. p := struct {
  317. *UserPage
  318. Config config.AppCfg
  319. Message string
  320. Banner *instanceContent
  321. Content *instanceContent
  322. }{
  323. Config: app.cfg.App,
  324. Message: r.FormValue("m"),
  325. }
  326. var err error
  327. // Get pre-defined pages, or select slug
  328. if slug == "about" {
  329. p.Content, err = getAboutPage(app)
  330. } else if slug == "privacy" {
  331. p.Content, err = getPrivacyPage(app)
  332. } else if slug == "landing" {
  333. p.Banner, err = getLandingBanner(app)
  334. if err != nil {
  335. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get banner: %v", err)}
  336. }
  337. p.Content, err = getLandingBody(app)
  338. p.Content.ID = "landing"
  339. } else if slug == "reader" {
  340. p.Content, err = getReaderSection(app)
  341. } else {
  342. p.Content, err = app.db.GetDynamicContent(slug)
  343. }
  344. if err != nil {
  345. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get page: %v", err)}
  346. }
  347. title := "New page"
  348. if p.Content != nil {
  349. title = "Edit " + p.Content.ID
  350. } else {
  351. p.Content = &instanceContent{}
  352. }
  353. p.UserPage = NewUserPage(app, r, u, title, nil)
  354. showUserPage(w, "view-page", p)
  355. return nil
  356. }
  357. func handleAdminUpdateSite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  358. vars := mux.Vars(r)
  359. id := vars["page"]
  360. // Validate
  361. if id != "about" && id != "privacy" && id != "landing" && id != "reader" {
  362. return impart.HTTPError{http.StatusNotFound, "No such page."}
  363. }
  364. var err error
  365. m := ""
  366. if id == "landing" {
  367. // Handle special landing page
  368. err = app.db.UpdateDynamicContent("landing-banner", "", r.FormValue("banner"), "section")
  369. if err != nil {
  370. m = "?m=" + err.Error()
  371. return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
  372. }
  373. err = app.db.UpdateDynamicContent("landing-body", "", r.FormValue("content"), "section")
  374. } else if id == "reader" {
  375. // Update sections with titles
  376. err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "section")
  377. } else {
  378. // Update page
  379. err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "page")
  380. }
  381. if err != nil {
  382. m = "?m=" + err.Error()
  383. }
  384. return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
  385. }
  386. func handleAdminUpdateConfig(apper Apper, u *User, w http.ResponseWriter, r *http.Request) error {
  387. apper.App().cfg.App.SiteName = r.FormValue("site_name")
  388. apper.App().cfg.App.SiteDesc = r.FormValue("site_desc")
  389. apper.App().cfg.App.Landing = r.FormValue("landing")
  390. apper.App().cfg.App.OpenRegistration = r.FormValue("open_registration") == "on"
  391. mul, err := strconv.Atoi(r.FormValue("min_username_len"))
  392. if err == nil {
  393. apper.App().cfg.App.MinUsernameLen = mul
  394. }
  395. mb, err := strconv.Atoi(r.FormValue("max_blogs"))
  396. if err == nil {
  397. apper.App().cfg.App.MaxBlogs = mb
  398. }
  399. apper.App().cfg.App.Federation = r.FormValue("federation") == "on"
  400. apper.App().cfg.App.PublicStats = r.FormValue("public_stats") == "on"
  401. apper.App().cfg.App.Private = r.FormValue("private") == "on"
  402. apper.App().cfg.App.LocalTimeline = r.FormValue("local_timeline") == "on"
  403. if apper.App().cfg.App.LocalTimeline && apper.App().timeline == nil {
  404. log.Info("Initializing local timeline...")
  405. initLocalTimeline(apper.App())
  406. }
  407. apper.App().cfg.App.UserInvites = r.FormValue("user_invites")
  408. if apper.App().cfg.App.UserInvites == "none" {
  409. apper.App().cfg.App.UserInvites = ""
  410. }
  411. apper.App().cfg.App.DefaultVisibility = r.FormValue("default_visibility")
  412. m := "?cm=Configuration+saved."
  413. err = apper.SaveConfig(apper.App().cfg)
  414. if err != nil {
  415. m = "?cm=" + err.Error()
  416. }
  417. return impart.HTTPError{http.StatusFound, "/admin" + m + "#config"}
  418. }
  419. func updateAppStats() {
  420. sysStatus.Uptime = appstats.TimeSincePro(appStartTime)
  421. m := new(runtime.MemStats)
  422. runtime.ReadMemStats(m)
  423. sysStatus.NumGoroutine = runtime.NumGoroutine()
  424. sysStatus.MemAllocated = appstats.FileSize(int64(m.Alloc))
  425. sysStatus.MemTotal = appstats.FileSize(int64(m.TotalAlloc))
  426. sysStatus.MemSys = appstats.FileSize(int64(m.Sys))
  427. sysStatus.Lookups = m.Lookups
  428. sysStatus.MemMallocs = m.Mallocs
  429. sysStatus.MemFrees = m.Frees
  430. sysStatus.HeapAlloc = appstats.FileSize(int64(m.HeapAlloc))
  431. sysStatus.HeapSys = appstats.FileSize(int64(m.HeapSys))
  432. sysStatus.HeapIdle = appstats.FileSize(int64(m.HeapIdle))
  433. sysStatus.HeapInuse = appstats.FileSize(int64(m.HeapInuse))
  434. sysStatus.HeapReleased = appstats.FileSize(int64(m.HeapReleased))
  435. sysStatus.HeapObjects = m.HeapObjects
  436. sysStatus.StackInuse = appstats.FileSize(int64(m.StackInuse))
  437. sysStatus.StackSys = appstats.FileSize(int64(m.StackSys))
  438. sysStatus.MSpanInuse = appstats.FileSize(int64(m.MSpanInuse))
  439. sysStatus.MSpanSys = appstats.FileSize(int64(m.MSpanSys))
  440. sysStatus.MCacheInuse = appstats.FileSize(int64(m.MCacheInuse))
  441. sysStatus.MCacheSys = appstats.FileSize(int64(m.MCacheSys))
  442. sysStatus.BuckHashSys = appstats.FileSize(int64(m.BuckHashSys))
  443. sysStatus.GCSys = appstats.FileSize(int64(m.GCSys))
  444. sysStatus.OtherSys = appstats.FileSize(int64(m.OtherSys))
  445. sysStatus.NextGC = appstats.FileSize(int64(m.NextGC))
  446. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  447. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  448. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  449. sysStatus.NumGC = m.NumGC
  450. }
  451. func adminResetPassword(app *App, u *User, newPass string) error {
  452. hashedPass, err := auth.HashPass([]byte(newPass))
  453. if err != nil {
  454. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  455. }
  456. err = app.db.ChangePassphrase(u.ID, true, "", hashedPass)
  457. if err != nil {
  458. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  459. }
  460. return nil
  461. }