/controllers/admin.go

http://github.com/TheOnly92/bloody.go · Go · 345 lines · 303 code · 40 blank · 2 comment · 88 complexity · 9c93ba708eb7b85a45b36113402cf6f8 MD5 · raw file

  1. package main
  2. import (
  3. "mustache"
  4. "time"
  5. "web"
  6. "session"
  7. "crypto/sha1"
  8. "hash"
  9. "strconv"
  10. "encoding/hex"
  11. "os/exec"
  12. "os"
  13. //"fmt"
  14. //"reflect"
  15. )
  16. type Admin struct {}
  17. func (c *Admin) render(output string, title string) string {
  18. template, err := mustache.ParseFile("templates/admin/layout.mustache")
  19. if err != nil {
  20. panic(err)
  21. }
  22. vars := make(map[string]interface{})
  23. vars["Body"] = output
  24. if title != "" {
  25. vars["Title"] = map[string]string {"Name": title}
  26. }
  27. if blogConfig.Get("markdown") == "1" {
  28. vars["markdown"] = "1"
  29. }
  30. return template.Render(vars)
  31. }
  32. func (c *Admin) IndexGet(ctx *web.Context) string {
  33. sessionH := session.Start(ctx, h)
  34. defer sessionH.Save()
  35. if sessionH.Data["logged"] != nil {
  36. ctx.Redirect(302, "/admin/post/list")
  37. return ""
  38. }
  39. ctx.Redirect(302, "/admin/login")
  40. return ""
  41. }
  42. func (c *Admin) LoginGet(ctx *web.Context) string {
  43. sessionH := session.Start(ctx, h)
  44. defer sessionH.Save()
  45. if sessionH.Data["logged"] != nil {
  46. ctx.Redirect(302, "/admin/post/list")
  47. return ""
  48. }
  49. output := mustache.RenderFile("templates/admin/login.mustache")
  50. return render(output, "Login")
  51. }
  52. func (c *Admin) LoginPost(ctx *web.Context) {
  53. sessionH := session.Start(ctx, h)
  54. defer sessionH.Save()
  55. if sessionH.Data["logged"] != nil {
  56. ctx.Redirect(302, "/admin/post/list")
  57. return
  58. }
  59. if ctx.Params["username"] == config.Get("adminuser") && ctx.Params["password"] == config.Get("adminpasswd") {
  60. t := time.Now()
  61. var h hash.Hash = sha1.New()
  62. h.Write([]byte(strconv.FormatInt(t.Unix(), 10)))
  63. sessionH.Data["logged"] = hex.EncodeToString(h.Sum(nil))
  64. }
  65. ctx.Redirect(302, "/admin/post/list")
  66. }
  67. func (c *Admin) PreferencesGet(ctx *web.Context) string {
  68. sessionH := session.Start(ctx, h)
  69. defer sessionH.Save()
  70. if sessionH.Data["logged"] == nil {
  71. ctx.Redirect(302, "/admin/login")
  72. return ""
  73. }
  74. vars := map[string]string {"DateFormat": blogConfig.Get("dateFormat"), "PostsPerPage": blogConfig.Get("postsPerPage")}
  75. comment := blogConfig.Get("enableComment")
  76. if comment != "" {
  77. vars["EnableComment"] = comment
  78. }
  79. if blogConfig.Get("markdown") == "1" {
  80. vars["markdown"] = "1"
  81. }
  82. output := mustache.RenderFile("templates/admin/preferences.mustache", vars)
  83. return c.render(output, "Blog Preferernces")
  84. }
  85. func (c *Admin) PreferencesPost(ctx *web.Context) {
  86. sessionH := session.Start(ctx, h)
  87. defer sessionH.Save()
  88. if sessionH.Data["logged"] == nil {
  89. ctx.Redirect(302, "/admin/login")
  90. return
  91. }
  92. blogConfig.Update("dateFormat", ctx.Params["dateFormat"])
  93. blogConfig.Update("postsPerPage", ctx.Params["postsPerPage"])
  94. blogConfig.Update("enableComment", ctx.Params["enableComment"])
  95. markdown := "0"
  96. if ctx.Params["markdown"] == "on" {
  97. markdown = "1"
  98. }
  99. blogConfig.Update("markdown", markdown)
  100. ctx.Redirect(302, "/admin/preferences")
  101. }
  102. func (c *Admin) NewPostGet(ctx *web.Context) string {
  103. sessionH := session.Start(ctx, h)
  104. defer sessionH.Save()
  105. if sessionH.Data["logged"] == nil {
  106. ctx.Redirect(302, "/admin/login")
  107. return ""
  108. }
  109. var output string
  110. if _, exists := ctx.Params["markdown"]; exists {
  111. output = mustache.RenderFile("templates/admin/new-post-markdown.mustache")
  112. } else {
  113. output = mustache.RenderFile("templates/admin/new-post.mustache")
  114. }
  115. return c.render(output, "New Post")
  116. }
  117. func (c *Admin) NewPostPost(ctx *web.Context) {
  118. sessionH := session.Start(ctx, h)
  119. defer sessionH.Save()
  120. if sessionH.Data["logged"] == nil {
  121. ctx.Redirect(302, "/admin/login")
  122. return
  123. }
  124. p := PostModelInit()
  125. var markdown uint
  126. markdown = 0
  127. if _, exists := ctx.Params["markdown"]; exists {
  128. markdown = 1
  129. }
  130. p.Create(ctx.Params["title"], ctx.Params["content"], ctx.Params["status"], markdown)
  131. ctx.Redirect(302, "/admin/post/list")
  132. }
  133. func (c *Admin) ListPost(ctx *web.Context) string {
  134. sessionH := session.Start(ctx, h)
  135. defer sessionH.Save()
  136. if sessionH.Data["logged"] == nil {
  137. ctx.Redirect(302, "/admin/login")
  138. return ""
  139. }
  140. page := 0
  141. if temp, exists := ctx.Params["page"]; exists {
  142. page, _ = strconv.Atoi(temp)
  143. }
  144. p := PostModelInit()
  145. results := p.PostListing(page)
  146. vars := map[string]interface{} {"posts":results}
  147. output := mustache.RenderFile("templates/admin/list-post.mustache", vars)
  148. return c.render(output, "List Posts")
  149. }
  150. func (c *Admin) EditPostGet(ctx *web.Context, postId string) string {
  151. sessionH := session.Start(ctx, h)
  152. defer sessionH.Save()
  153. if sessionH.Data["logged"] == nil {
  154. ctx.Redirect(302, "/admin/login")
  155. return ""
  156. }
  157. p := PostModelInit()
  158. result := p.Get(postId)
  159. templateVars := map[string]interface{} {"Title":result.Title, "Content":result.Content, "id":objectIdHex(result.Id.String())}
  160. if result.Status == 0 {
  161. templateVars["Draft"] = 1
  162. }
  163. if result.Status == 1 {
  164. templateVars["Publish"] = 1
  165. }
  166. var output string
  167. if result.Type == 1 {
  168. output = mustache.RenderFile("templates/admin/edit-post-markdown.mustache", templateVars)
  169. } else {
  170. output = mustache.RenderFile("templates/admin/edit-post.mustache", templateVars)
  171. }
  172. return c.render(output, "Edit Post")
  173. }
  174. func (c *Admin) EditPostPost(ctx *web.Context, postId string) {
  175. sessionH := session.Start(ctx, h)
  176. defer sessionH.Save()
  177. if sessionH.Data["logged"] == nil {
  178. ctx.Redirect(302, "/admin/login")
  179. return
  180. }
  181. p := PostModelInit()
  182. p.Update(ctx.Params["title"], ctx.Params["content"], ctx.Params["status"], postId)
  183. ctx.Redirect(302, "/admin/post/list")
  184. }
  185. func (c *Admin) DelPost(ctx *web.Context, postId string) {
  186. sessionH := session.Start(ctx, h)
  187. defer sessionH.Save()
  188. if sessionH.Data["logged"] == nil {
  189. ctx.Redirect(302, "/admin/login")
  190. return
  191. }
  192. p := PostModelInit()
  193. p.Delete(postId)
  194. ctx.Redirect(302, "/admin/post/list")
  195. }
  196. func (c *Admin) BulkActions(ctx *web.Context) {
  197. sessionH := session.Start(ctx, h)
  198. defer sessionH.Save()
  199. if sessionH.Data["logged"] == nil {
  200. ctx.Redirect(302, "/admin/login")
  201. return
  202. }
  203. p := PostModelInit()
  204. switch ctx.Params["action"] {
  205. case "delete":
  206. p.DeleteBulk(ctx.FullParams["posts[]"])
  207. case "publish":
  208. p.PublishBulk(ctx.FullParams["posts[]"])
  209. }
  210. ctx.Redirect(302, "/admin/post/list")
  211. }
  212. func (c *Admin) NewPageGet(ctx *web.Context) string {
  213. sessionH := session.Start(ctx, h)
  214. defer sessionH.Save()
  215. if sessionH.Data["logged"] == nil {
  216. ctx.Redirect(302, "/admin/login")
  217. return ""
  218. }
  219. output := mustache.RenderFile("templates/admin/new-page.mustache")
  220. return c.render(output, "New Page")
  221. }
  222. func (c *Admin) NewPagePost(ctx *web.Context) {
  223. sessionH := session.Start(ctx, h)
  224. defer sessionH.Save()
  225. if sessionH.Data["logged"] == nil {
  226. ctx.Redirect(302, "/admin/login")
  227. return
  228. }
  229. p := PageModelInit()
  230. p.Create(ctx.Params["title"], ctx.Params["content"])
  231. ctx.Redirect(302, "/admin/page/list")
  232. }
  233. func (c *Admin) ListPagesGet(ctx *web.Context) string {
  234. sessionH := session.Start(ctx, h)
  235. defer sessionH.Save()
  236. if sessionH.Data["logged"] == nil {
  237. ctx.Redirect(302, "/admin/login")
  238. return ""
  239. }
  240. p := PageModelInit()
  241. results := p.List()
  242. output := mustache.RenderFile("templates/admin/list-pages.mustache", map[string][]map[string]string {"pages":results})
  243. return c.render(output, "List Pages")
  244. }
  245. func (c *Admin) EditPageGet(ctx *web.Context, id string) string {
  246. sessionH := session.Start(ctx, h)
  247. defer sessionH.Save()
  248. if sessionH.Data["logged"] == nil {
  249. ctx.Redirect(302, "/admin/login")
  250. return ""
  251. }
  252. p := PageModelInit()
  253. result := p.Get(id)
  254. output := mustache.RenderFile("templates/admin/edit-page.mustache", map[string]string {"Title":result.Title, "Content":result.Content, "id":objectIdHex(result.Id.String())})
  255. return c.render(output, "Edit Post")
  256. }
  257. func (c *Admin) EditPagePost(ctx *web.Context, id string) {
  258. sessionH := session.Start(ctx, h)
  259. defer sessionH.Save()
  260. if sessionH.Data["logged"] == nil {
  261. ctx.Redirect(302, "/admin/login")
  262. return
  263. }
  264. p := PageModelInit()
  265. p.Update(ctx.Params["title"], ctx.Params["content"], id)
  266. ctx.Redirect(302, "/admin/page/list")
  267. }
  268. func (c *Admin) DelPage(ctx *web.Context, id string) {
  269. sessionH := session.Start(ctx, h)
  270. defer sessionH.Save()
  271. if sessionH.Data["logged"] == nil {
  272. ctx.Redirect(302, "/admin/login")
  273. return
  274. }
  275. p := PageModelInit()
  276. p.Delete(id)
  277. ctx.Redirect(302, "/admin/page/list")
  278. }
  279. func (c *Admin) DelComment(ctx *web.Context, postId string, id string) {
  280. sessionH := session.Start(ctx, h)
  281. defer sessionH.Save()
  282. if sessionH.Data["logged"] == nil {
  283. ctx.Redirect(302, "/admin/login")
  284. return
  285. }
  286. p := PostModelInit()
  287. p.DeleteComment(postId, id)
  288. ctx.Redirect(302, "/post/"+postId)
  289. }
  290. func (c *Admin) RestartBloody(ctx *web.Context) string {
  291. sessionH := session.Start(ctx, h)
  292. defer sessionH.Save()
  293. if sessionH.Data["logged"] == nil {
  294. ctx.Redirect(302, "/admin/login")
  295. return ""
  296. }
  297. pid := os.Getpid()
  298. dir, _ := os.Getwd()
  299. command1 := exec.Command("kill",strconv.Itoa(pid))
  300. command2 := exec.Command(dir+"/bloody")
  301. command2.Start()
  302. command1.Start()
  303. output := mustache.RenderFile("templates/admin/restart.mustache")
  304. return c.render(output, "Restarting Bloody")
  305. }