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.
 
 
 
 
 

603 lines
17 KiB

  1. /*
  2. * Copyright © 2018-2020 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. p := struct {
  90. *UserPage
  91. Message string
  92. UsersCount, CollectionsCount, PostsCount int64
  93. }{
  94. UserPage: NewUserPage(app, r, u, "Admin", nil),
  95. Message: r.FormValue("m"),
  96. }
  97. // Get user stats
  98. p.UsersCount = app.db.GetAllUsersCount()
  99. var err error
  100. p.CollectionsCount, err = app.db.GetTotalCollections()
  101. if err != nil {
  102. return err
  103. }
  104. p.PostsCount, err = app.db.GetTotalPosts()
  105. if err != nil {
  106. return err
  107. }
  108. showUserPage(w, "admin", p)
  109. return nil
  110. }
  111. func handleViewAdminMonitor(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  112. updateAppStats()
  113. p := struct {
  114. *UserPage
  115. SysStatus systemStatus
  116. Config config.AppCfg
  117. Message, ConfigMessage string
  118. }{
  119. UserPage: NewUserPage(app, r, u, "Admin", nil),
  120. SysStatus: sysStatus,
  121. Config: app.cfg.App,
  122. Message: r.FormValue("m"),
  123. ConfigMessage: r.FormValue("cm"),
  124. }
  125. showUserPage(w, "monitor", p)
  126. return nil
  127. }
  128. func handleViewAdminSettings(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  129. p := struct {
  130. *UserPage
  131. Config config.AppCfg
  132. Message, ConfigMessage string
  133. }{
  134. UserPage: NewUserPage(app, r, u, "Admin", nil),
  135. Config: app.cfg.App,
  136. Message: r.FormValue("m"),
  137. ConfigMessage: r.FormValue("cm"),
  138. }
  139. showUserPage(w, "app-settings", p)
  140. return nil
  141. }
  142. func handleViewAdminUsers(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  143. p := struct {
  144. *UserPage
  145. Config config.AppCfg
  146. Message string
  147. Users *[]User
  148. CurPage int
  149. TotalUsers int64
  150. TotalPages []int
  151. }{
  152. UserPage: NewUserPage(app, r, u, "Users", nil),
  153. Config: app.cfg.App,
  154. Message: r.FormValue("m"),
  155. }
  156. p.TotalUsers = app.db.GetAllUsersCount()
  157. ttlPages := p.TotalUsers / adminUsersPerPage
  158. p.TotalPages = []int{}
  159. for i := 1; i <= int(ttlPages); i++ {
  160. p.TotalPages = append(p.TotalPages, i)
  161. }
  162. var err error
  163. p.CurPage, err = strconv.Atoi(r.FormValue("p"))
  164. if err != nil || p.CurPage < 1 {
  165. p.CurPage = 1
  166. } else if p.CurPage > int(ttlPages) {
  167. p.CurPage = int(ttlPages)
  168. }
  169. p.Users, err = app.db.GetAllUsers(uint(p.CurPage))
  170. if err != nil {
  171. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get users: %v", err)}
  172. }
  173. showUserPage(w, "users", p)
  174. return nil
  175. }
  176. func handleViewAdminUser(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  177. vars := mux.Vars(r)
  178. username := vars["username"]
  179. if username == "" {
  180. return impart.HTTPError{http.StatusFound, "/admin/users"}
  181. }
  182. p := struct {
  183. *UserPage
  184. Config config.AppCfg
  185. Message string
  186. User *User
  187. Colls []inspectedCollection
  188. LastPost string
  189. NewPassword string
  190. TotalPosts int64
  191. ClearEmail string
  192. }{
  193. Config: app.cfg.App,
  194. Message: r.FormValue("m"),
  195. Colls: []inspectedCollection{},
  196. }
  197. var err error
  198. p.User, err = app.db.GetUserForAuth(username)
  199. if err != nil {
  200. if err == ErrUserNotFound {
  201. return err
  202. }
  203. log.Error("Could not get user: %v", err)
  204. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  205. }
  206. flashes, _ := getSessionFlashes(app, w, r, nil)
  207. for _, flash := range flashes {
  208. if strings.HasPrefix(flash, "SUCCESS: ") {
  209. p.NewPassword = strings.TrimPrefix(flash, "SUCCESS: ")
  210. p.ClearEmail = p.User.EmailClear(app.keys)
  211. }
  212. }
  213. p.UserPage = NewUserPage(app, r, u, p.User.Username, nil)
  214. p.TotalPosts = app.db.GetUserPostsCount(p.User.ID)
  215. lp, err := app.db.GetUserLastPostTime(p.User.ID)
  216. if err != nil {
  217. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's last post time: %v", err)}
  218. }
  219. if lp != nil {
  220. p.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  221. }
  222. colls, err := app.db.GetCollections(p.User, app.cfg.App.Host)
  223. if err != nil {
  224. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's collections: %v", err)}
  225. }
  226. for _, c := range *colls {
  227. ic := inspectedCollection{
  228. CollectionObj: CollectionObj{Collection: c},
  229. }
  230. if app.cfg.App.Federation {
  231. folls, err := app.db.GetAPFollowers(&c)
  232. if err == nil {
  233. // TODO: handle error here (at least log it)
  234. ic.Followers = len(*folls)
  235. }
  236. }
  237. app.db.GetPostsCount(&ic.CollectionObj, true)
  238. lp, err := app.db.GetCollectionLastPostTime(c.ID)
  239. if err != nil {
  240. log.Error("Didn't get last post time for collection %d: %v", c.ID, err)
  241. }
  242. if lp != nil {
  243. ic.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  244. }
  245. p.Colls = append(p.Colls, ic)
  246. }
  247. showUserPage(w, "view-user", p)
  248. return nil
  249. }
  250. func handleAdminToggleUserStatus(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  251. vars := mux.Vars(r)
  252. username := vars["username"]
  253. if username == "" {
  254. return impart.HTTPError{http.StatusFound, "/admin/users"}
  255. }
  256. user, err := app.db.GetUserForAuth(username)
  257. if err != nil {
  258. log.Error("failed to get user: %v", err)
  259. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user from username: %v", err)}
  260. }
  261. if user.IsSilenced() {
  262. err = app.db.SetUserStatus(user.ID, UserActive)
  263. } else {
  264. err = app.db.SetUserStatus(user.ID, UserSilenced)
  265. }
  266. if err != nil {
  267. log.Error("toggle user silenced: %v", err)
  268. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not toggle user status: %v", err)}
  269. }
  270. return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s#status", username)}
  271. }
  272. func handleAdminResetUserPass(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  273. vars := mux.Vars(r)
  274. username := vars["username"]
  275. if username == "" {
  276. return impart.HTTPError{http.StatusFound, "/admin/users"}
  277. }
  278. // Generate new random password since none supplied
  279. pass := passgen.NewWordish()
  280. hashedPass, err := auth.HashPass([]byte(pass))
  281. if err != nil {
  282. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  283. }
  284. userIDVal := r.FormValue("user")
  285. log.Info("ADMIN: Changing user %s password", userIDVal)
  286. id, err := strconv.Atoi(userIDVal)
  287. if err != nil {
  288. return impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Invalid user ID: %v", err)}
  289. }
  290. err = app.db.ChangePassphrase(int64(id), true, "", hashedPass)
  291. if err != nil {
  292. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  293. }
  294. log.Info("ADMIN: Successfully changed.")
  295. addSessionFlash(app, w, r, fmt.Sprintf("SUCCESS: %s", pass), nil)
  296. return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s", username)}
  297. }
  298. func handleViewAdminPages(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  299. p := struct {
  300. *UserPage
  301. Config config.AppCfg
  302. Message string
  303. Pages []*instanceContent
  304. }{
  305. UserPage: NewUserPage(app, r, u, "Pages", nil),
  306. Config: app.cfg.App,
  307. Message: r.FormValue("m"),
  308. }
  309. var err error
  310. p.Pages, err = app.db.GetInstancePages()
  311. if err != nil {
  312. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get pages: %v", err)}
  313. }
  314. // Add in default pages
  315. var hasAbout, hasPrivacy bool
  316. for i, c := range p.Pages {
  317. if hasAbout && hasPrivacy {
  318. break
  319. }
  320. if c.ID == "about" {
  321. hasAbout = true
  322. if !c.Title.Valid {
  323. p.Pages[i].Title = defaultAboutTitle(app.cfg)
  324. }
  325. } else if c.ID == "privacy" {
  326. hasPrivacy = true
  327. if !c.Title.Valid {
  328. p.Pages[i].Title = defaultPrivacyTitle()
  329. }
  330. }
  331. }
  332. if !hasAbout {
  333. p.Pages = append(p.Pages, &instanceContent{
  334. ID: "about",
  335. Title: defaultAboutTitle(app.cfg),
  336. Content: defaultAboutPage(app.cfg),
  337. Updated: defaultPageUpdatedTime,
  338. })
  339. }
  340. if !hasPrivacy {
  341. p.Pages = append(p.Pages, &instanceContent{
  342. ID: "privacy",
  343. Title: defaultPrivacyTitle(),
  344. Content: defaultPrivacyPolicy(app.cfg),
  345. Updated: defaultPageUpdatedTime,
  346. })
  347. }
  348. showUserPage(w, "pages", p)
  349. return nil
  350. }
  351. func handleViewAdminPage(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  352. vars := mux.Vars(r)
  353. slug := vars["slug"]
  354. if slug == "" {
  355. return impart.HTTPError{http.StatusFound, "/admin/pages"}
  356. }
  357. p := struct {
  358. *UserPage
  359. Config config.AppCfg
  360. Message string
  361. Banner *instanceContent
  362. Content *instanceContent
  363. }{
  364. Config: app.cfg.App,
  365. Message: r.FormValue("m"),
  366. }
  367. var err error
  368. // Get pre-defined pages, or select slug
  369. if slug == "about" {
  370. p.Content, err = getAboutPage(app)
  371. } else if slug == "privacy" {
  372. p.Content, err = getPrivacyPage(app)
  373. } else if slug == "landing" {
  374. p.Banner, err = getLandingBanner(app)
  375. if err != nil {
  376. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get banner: %v", err)}
  377. }
  378. p.Content, err = getLandingBody(app)
  379. p.Content.ID = "landing"
  380. } else if slug == "reader" {
  381. p.Content, err = getReaderSection(app)
  382. } else {
  383. p.Content, err = app.db.GetDynamicContent(slug)
  384. }
  385. if err != nil {
  386. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get page: %v", err)}
  387. }
  388. title := "New page"
  389. if p.Content != nil {
  390. title = "Edit " + p.Content.ID
  391. } else {
  392. p.Content = &instanceContent{}
  393. }
  394. p.UserPage = NewUserPage(app, r, u, title, nil)
  395. showUserPage(w, "view-page", p)
  396. return nil
  397. }
  398. func handleAdminUpdateSite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  399. vars := mux.Vars(r)
  400. id := vars["page"]
  401. // Validate
  402. if id != "about" && id != "privacy" && id != "landing" && id != "reader" {
  403. return impart.HTTPError{http.StatusNotFound, "No such page."}
  404. }
  405. var err error
  406. m := ""
  407. if id == "landing" {
  408. // Handle special landing page
  409. err = app.db.UpdateDynamicContent("landing-banner", "", r.FormValue("banner"), "section")
  410. if err != nil {
  411. m = "?m=" + err.Error()
  412. return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
  413. }
  414. err = app.db.UpdateDynamicContent("landing-body", "", r.FormValue("content"), "section")
  415. } else if id == "reader" {
  416. // Update sections with titles
  417. err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "section")
  418. } else {
  419. // Update page
  420. err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "page")
  421. }
  422. if err != nil {
  423. m = "?m=" + err.Error()
  424. }
  425. return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
  426. }
  427. func handleAdminUpdateConfig(apper Apper, u *User, w http.ResponseWriter, r *http.Request) error {
  428. apper.App().cfg.App.SiteName = r.FormValue("site_name")
  429. apper.App().cfg.App.SiteDesc = r.FormValue("site_desc")
  430. apper.App().cfg.App.Landing = r.FormValue("landing")
  431. apper.App().cfg.App.OpenRegistration = r.FormValue("open_registration") == "on"
  432. mul, err := strconv.Atoi(r.FormValue("min_username_len"))
  433. if err == nil {
  434. apper.App().cfg.App.MinUsernameLen = mul
  435. }
  436. mb, err := strconv.Atoi(r.FormValue("max_blogs"))
  437. if err == nil {
  438. apper.App().cfg.App.MaxBlogs = mb
  439. }
  440. apper.App().cfg.App.Federation = r.FormValue("federation") == "on"
  441. apper.App().cfg.App.PublicStats = r.FormValue("public_stats") == "on"
  442. apper.App().cfg.App.Private = r.FormValue("private") == "on"
  443. apper.App().cfg.App.LocalTimeline = r.FormValue("local_timeline") == "on"
  444. if apper.App().cfg.App.LocalTimeline && apper.App().timeline == nil {
  445. log.Info("Initializing local timeline...")
  446. initLocalTimeline(apper.App())
  447. }
  448. apper.App().cfg.App.UserInvites = r.FormValue("user_invites")
  449. if apper.App().cfg.App.UserInvites == "none" {
  450. apper.App().cfg.App.UserInvites = ""
  451. }
  452. apper.App().cfg.App.DefaultVisibility = r.FormValue("default_visibility")
  453. m := "?cm=Configuration+saved."
  454. err = apper.SaveConfig(apper.App().cfg)
  455. if err != nil {
  456. m = "?cm=" + err.Error()
  457. }
  458. return impart.HTTPError{http.StatusFound, "/admin/settings" + m + "#config"}
  459. }
  460. func updateAppStats() {
  461. sysStatus.Uptime = appstats.TimeSincePro(appStartTime)
  462. m := new(runtime.MemStats)
  463. runtime.ReadMemStats(m)
  464. sysStatus.NumGoroutine = runtime.NumGoroutine()
  465. sysStatus.MemAllocated = appstats.FileSize(int64(m.Alloc))
  466. sysStatus.MemTotal = appstats.FileSize(int64(m.TotalAlloc))
  467. sysStatus.MemSys = appstats.FileSize(int64(m.Sys))
  468. sysStatus.Lookups = m.Lookups
  469. sysStatus.MemMallocs = m.Mallocs
  470. sysStatus.MemFrees = m.Frees
  471. sysStatus.HeapAlloc = appstats.FileSize(int64(m.HeapAlloc))
  472. sysStatus.HeapSys = appstats.FileSize(int64(m.HeapSys))
  473. sysStatus.HeapIdle = appstats.FileSize(int64(m.HeapIdle))
  474. sysStatus.HeapInuse = appstats.FileSize(int64(m.HeapInuse))
  475. sysStatus.HeapReleased = appstats.FileSize(int64(m.HeapReleased))
  476. sysStatus.HeapObjects = m.HeapObjects
  477. sysStatus.StackInuse = appstats.FileSize(int64(m.StackInuse))
  478. sysStatus.StackSys = appstats.FileSize(int64(m.StackSys))
  479. sysStatus.MSpanInuse = appstats.FileSize(int64(m.MSpanInuse))
  480. sysStatus.MSpanSys = appstats.FileSize(int64(m.MSpanSys))
  481. sysStatus.MCacheInuse = appstats.FileSize(int64(m.MCacheInuse))
  482. sysStatus.MCacheSys = appstats.FileSize(int64(m.MCacheSys))
  483. sysStatus.BuckHashSys = appstats.FileSize(int64(m.BuckHashSys))
  484. sysStatus.GCSys = appstats.FileSize(int64(m.GCSys))
  485. sysStatus.OtherSys = appstats.FileSize(int64(m.OtherSys))
  486. sysStatus.NextGC = appstats.FileSize(int64(m.NextGC))
  487. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  488. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  489. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  490. sysStatus.NumGC = m.NumGC
  491. }
  492. func adminResetPassword(app *App, u *User, newPass string) error {
  493. hashedPass, err := auth.HashPass([]byte(newPass))
  494. if err != nil {
  495. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  496. }
  497. err = app.db.ChangePassphrase(u.ID, true, "", hashedPass)
  498. if err != nil {
  499. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  500. }
  501. return nil
  502. }
  503. func handleViewAdminUpdates(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  504. check := r.URL.Query().Get("check")
  505. if check == "now" && app.cfg.App.UpdateChecks {
  506. app.updates.CheckNow()
  507. }
  508. p := struct {
  509. *UserPage
  510. LastChecked string
  511. LatestVersion string
  512. LatestReleaseNotesURL string
  513. UpdateAvailable bool
  514. }{
  515. UserPage: NewUserPage(app, r, u, "Updates", nil),
  516. }
  517. if app.cfg.App.UpdateChecks {
  518. p.LastChecked = app.updates.lastCheck.Format("January 2, 2006, 3:04 PM")
  519. p.LatestVersion = app.updates.LatestVersion()
  520. p.LatestReleaseNotesURL = app.updates.ReleaseNotesURL()
  521. p.UpdateAvailable = app.updates.AreAvailable()
  522. }
  523. showUserPage(w, "app-updates", p)
  524. return nil
  525. }