func Remove(ctx *web.Context, val string) string { id, err := strconv.Atoi(val) if err != nil { return "Invalid or malformed id" } db := util.GetDb() _, submit_exists := ctx.Params["doit"] if submit_exists { db.Exec("DELETE FROM entries WHERE id=$1", id) ctx.Redirect(302, "/manage/existing") return "Redirect" } // Get the post row := db.QueryRow("SELECT title FROM entries WHERE id=$1", id) entry := new(util.Entry) row.Scan(&entry.Title) send := map[string]interface{}{ "Title": entry.Title, } return util.RenderTemplate("remove.mustache", send) }
func logout(ctx *web.Context) string { session, _ := CookieStore.Get(ctx.Request, "monet-session") session.Values["authenticated"] = false session.Save(ctx.Request, ctx.ResponseWriter) ctx.Redirect(302, "/admin/login/") return "" }
/* serve a link with extras (a path relative to the short-link and/or GET parameters) Parameters: ctx: the context of the http request hash: the short-hash of the link extras: the extra path component */ func serveLinkWithExtras(ctx *web.Context, hash string, extras string) { //make the hash all uppercase upperHash := strings.ToUpper(hash) //Check to see if a link exists for the given hash link, numReports, exists, err := db_linkForHash(upperHash) if err != nil { //There was an error in the database internalError(ctx, errors.New("Database Error: "+err.Error())) } else if exists { //Check to see if the link has been flagged for review if numReports >= NUM_REPORTS_TO_FLAG { redir := link //If there were any path extras passed to us, append them to the redir link if extras != "" { redir += "/" + extras } //If there are any GET parameters being passed to us, append them to the redir link if len(ctx.Params) > 0 { params := "?" for k, v := range ctx.Params { params += k + "=" + v + "&" } //remove the trailing ampersand and append to the redir link redir += strings.TrimSuffix(params, "&") } flaggedLink(ctx, hash, redir) return } else { //The hash=>link exists redir := link //If there were any path extras passed to us, append them to the redir link if extras != "" { redir += "/" + extras } //If there are any GET parameters being passed to us, append them to the redir link if len(ctx.Params) > 0 { params := "?" for k, v := range ctx.Params { params += k + "=" + v + "&" } //remove the trailing ampersand and append to the redir link redir += strings.TrimSuffix(params, "&") } //if the hash exists in the link table, issue a '302 Found' to the client with the link URL ctx.Redirect(302, redir) } } else { //No link exists for the hash, so serve a '404 Not Found' error page error404(ctx, hash) } }
func (c *Controller) Publish(ctx *web.Context) { if !c.sessionManager.LoggedIn(ctx) { ctx.Redirect(303, "/login") return } ctx.WriteString(c.Page("publish.html")) }
func update(ctx *web.Context) { if ctx.Params["submit"] == "Delete" { ctx.SetCookie(web.NewCookie(cookieName, "", -1)) } else { ctx.SetSecureCookie(cookieName, ctx.Params["cookie"], 0) } ctx.Redirect(301, "/") }
func RequireAuthentication(ctx *web.Context) bool { session, _ := CookieStore.Get(ctx.Request, "monet-session") if session.Values["authenticated"] != true { ctx.Redirect(302, "/admin/login/") return true } return false }
// renders / func index(ctx *web.Context) string { css, ok := ctx.Params["css"] if ok { SetCSS(ctx, css) ctx.Redirect(302, "/") return "ok" } //posts := postsForMonth(time.LocalTime()) //Db.GetLastNPosts(10) // posts := lastPosts(0xff) posts := postsForLastNDays(4) if len(posts) <= 0 { posts = lastPosts(23) } //fmt.Printf("posts: %#v\n", posts) //embedded struct - our mustache templates need a NumOfComments field to render //but we don't want to put that field into the BlogPost Struct so it won't get stored //into the DB type MyPost struct { BlogPost NumOfComments int } //posts ordered by date. this is ugly. TODO: look up if mustache hase something to handle this situation type Date struct { Date string Posts []MyPost } Db := DBGet() defer Db.Close() //loop through our posts and put them into the appropriate date structure dates := []Date{} var cur_date time.Time var date *Date for _, p := range posts { post_date := time.Unix(p.Timestamp, 0) if !(cur_date.Day == post_date.Day && cur_date.Month == post_date.Month && cur_date.Year == post_date.Year) { cur_date = *post_date dates = append(dates, Date{Date: cur_date.Format("Mon Jan _2 2006")}) date = &dates[len(dates)-1] } p.Comments, _ = Db.GetComments(p.Id) mp := MyPost{p, len(p.Comments)} date.Posts = append(date.Posts, mp) } m := map[string]interface{}{ "Dates": dates, } tmpl, _ := mustache.ParseFile("templ/index.mustache") s := tmpl.Render(&m, getCSS(ctx)) return s }
func postDelete(ctx *web.Context, slug string) string { if app.RequireAuthentication(ctx) { return "" } db.Cursor(&Post{}).Remove(M{"slug": slug}) referer := ctx.Request.Header.Get("referer") if len(referer) == 0 { referer = "/admin/" } ctx.Redirect(302, referer) return "" }
func pageDelete(ctx *web.Context, url string) string { if app.RequireAuthentication(ctx) { return "" } db.Cursor(&Page{}).Remove(M{"url": url}) referer := ctx.Request.Header.Get("referer") if len(referer) == 0 { referer = "/admin/" } ctx.Redirect(302, referer) return "" }
func S3GetHandler(ctx *web.Context, key string) (ret string) { val := FakeS3[key] if val == "" { ctx.Abort(404, "Not Found") return } else if val == "FAIL" { ctx.Redirect(301, "htttttttp://idon'twork") return } else { return val } }
func login(ctx *web.Context) string { if ctx.Params != nil { p := ctx.Params if ValidateUser(p["username"], p["password"]) { session, _ := CookieStore.Get(ctx.Request, "monet-session") session.Values["authenticated"] = true session.Save(ctx.Request, ctx.ResponseWriter) ctx.Redirect(302, "/admin/") } } return adminBase.Render("admin/login.mandira", ctx.Params, M{"login": true}) }
// Handler for adding a new feed func addNewFeed(ctx *web.Context) string { url := ctx.Params["url"] source, err := loadFeed(url) if err != nil { return err.Error() } lock.Lock() folders["uncategorized"] = append(folders["uncategorized"], source) lock.Unlock() ctx.Redirect(303, "/") return "" }
// Checks login func login(wr *web.Context) { // Login user if wr.Params["username"] != "" && wr.Params["password"] != "" { username := wr.Params["username"] password := wr.Params["password"] loginName := jailgo.Authenticate(username, password) // If you have a valid user if loginName != nil { log.Println("DEBUG Controller *Username type: ", loginName.User) wr.SetSecureCookie("user", loginName.User, 20000) //15 minutes for session wr.Redirect(303, "/jail?check=ok") // If you haven't } else { wr.Redirect(303, "/jail?check=err") } // Maybe you're going out } else if wr.Params["logout"] != "" { wr.SetSecureCookie("user", "off", 0) wr.Redirect(303, "/jail?check=out") } else { // If you wrote nothing log.Println("DEBUG Controller user: "******"username"]) log.Println("DEBUG Controller pass: "******"password"]) wr.Redirect(303, "/jail?check=err") } }
// Update database for articles and render main page func updateArt(wr *web.Context) { loginUser, err := wr.GetSecureCookie("user") if err { log.Println("DEBUG User logged updating article: ", loginUser) jailgo.Updateart(wr.Params["title"], wr.Params["description"]) // Redirect to the main page which will show the specified art wr.Redirect(303, "/jail") // We could show this art directly using show(wr, art_num) // but see: http://en.wikipedia.org/wiki/Post/Redirect/Get } else { wr.Redirect(303, "/jail?check=err") } }
func pageAdd(ctx *web.Context) string { if app.RequireAuthentication(ctx) { return "" } if ctx.Request.Method == "GET" { ctx.Params["Url"] = strings.TrimLeft(ctx.Params["Url"], "/") return adminBase.Render("blog/admin/pages-edit.mandira", ctx.Params) } var page = new(Page) page.FromParams(ctx.Params) db.Upsert(page) ctx.Redirect(302, "/admin/") return "" //ctx.Redirect(302, "/admin/posts/edit/" + post.Slug + "/") }
func (c *Controller) PublishPost(ctx *web.Context) { if !c.sessionManager.LoggedIn(ctx) { ctx.Redirect(303, "/login") return } file, head, err := ctx.Request.FormFile("publishFile") if err != nil { ctx.Abort(405, "error, post without a file") return } saveFile, err := os.Create("articles/" + head.Filename) defer saveFile.Close() io.Copy(saveFile, file) file.Close() ctx.Redirect(303, "/publish") }
func pageEdit(ctx *web.Context, url string) string { if app.RequireAuthentication(ctx) { return "" } var page *Page err := db.Find(page, M{"url": url}).One(&page) if err != nil { fmt.Println(err) ctx.Redirect(302, "/admin/") return "" } if len(ctx.Params) > 1 { page.FromParams(ctx.Params) db.Upsert(page) } return adminBase.Render("blog/admin/pages-edit.mandira", page) }
func admin(wr *web.Context) { loginUser, err := wr.GetSecureCookie("user") if err == true && loginUser != "off" { // Log username to control and render Admin page log.Println("DEBUG User logged accessing administration: ", loginUser) adminshow.Exec(wr) getdeleteart(wr) adminshow2.Exec(wr) getalldeletentry(wr) adminshow3.Exec(wr) check := "" foot1.Exec(wr, ViewCtxLogin{BlogLogin(wr, check), vinfo()}) last(wr) foot2.Exec(wr) } else { wr.Redirect(303, "/jail?check=err") } }
func postAdd(ctx *web.Context) string { if app.RequireAuthentication(ctx) { return "" } if ctx.Request.Method == "GET" { return adminBase.Render("blog/admin/posts-edit.mandira", ctx.Params, M{"Published": 0, "IsPublished": false}) } post := new(Post) post.FromParams(ctx.Params) _, err := db.Upsert(post) if err != nil { fmt.Println(err) } ctx.Redirect(302, "/admin/") return "" //ctx.Redirect(302, "/admin/posts/edit/" + post.Slug + "/") }
func postEdit(ctx *web.Context, slug string) string { if app.RequireAuthentication(ctx) { return "" } var post *Post err := db.Find(post, M{"slug": slug}).One(&post) if err != nil { fmt.Println(err) ctx.Redirect(302, "/admin/") return "" } if len(ctx.Params) > 1 { post.FromParams(ctx.Params) db.Upsert(post) } return adminBase.Render("blog/admin/posts-edit.mandira", post, M{ "IsPublished": post.Published == 1, "IdHex": post.Id.Hex()}) }
func adminPost(ctx *web.Context) { level := ctx.Params["godlevel"] godlevel := godHash(level) if ctx.Params["what"] == "login" { if godlevel == admin_pass { ctx.SetSecureCookie("godlevel", level, 3600) ctx.Redirect(301, "/admin") return } ctx.SetSecureCookie("godlevel", "fefe", 3600) ctx.Redirect(301, "/") return } if !checkGodLevel(ctx) { ctx.SetSecureCookie("godlevel", "fefe", 3600) ctx.Redirect(301, "/") return } if ctx.Params["what"] == "post" { err := createNewPost(ctx.Params["content"]) if err != nil { ctx.WriteString("couldn't post: " + err.Error()) ctx.WriteString("<br><br><A href='/'>Index</a>") return } ctx.WriteString(successpage) return } }
func editPost(ctx *web.Context) { if !checkGodLevel(ctx) { ctx.Redirect(301, "/") return } Db := DBGet() defer Db.Close() id, _ := strconv.ParseInt(ctx.Params["postid"], 10, 64) post, err := Db.GetPost(id) if err != nil { ctx.WriteString("couldn't load post with given id!") return } post.Content = ctx.Params["content"] _, err = Db.StorePost(&post) if err != nil { ctx.WriteString("couldn't store post!") return } ctx.WriteString(successpage) }
// Update database for comments and render main page func updateEntry(wr *web.Context) { // Getting captcha //captstr, err := wr.GetSecureCookie("captstr") // Patch until set another secure cookie works again captstr := wr.Params["captstr"] if captstr != "" && wr.Params["content"] != "" { if processForm(captstr, wr.Params["captchaSolution"]) { id := wr.Params["threadId"] if artid, err := strconv.Atoi(id); err != nil { log.Println("DEBUG updateEntry Invalid String", err) log.Println("DEBUG updateEntry threadID Atoi", artid) // Redirect to the comment page which will show the specified art wr.Redirect(303, "comment?threadid="+id) } else { jailgo.UpdateEntry(artid, wr.Params["content"], wr.Params["author"]) // Redirect to the comment page which will show the specified art wr.Redirect(303, "comment?threadid="+id) } // Redirect with fail if you typed incorrect captcha // We could show this comment directly using entry(wr, art_num) // but see: http://en.wikipedia.org/wiki/Post/Redirect/Get } else { id := wr.Params["threadId"] log.Println("DEBUG captcha verification failed!! (Error typed)", wr.Params["captchaSolution"]) wr.Redirect(303, "comment?fail=on&threadid="+id) } } else { id := wr.Params["threadId"] log.Println("DEBUG Secure Cookie not found!!!", captstr) wr.Redirect(303, "comment?fail=on&threadid="+id) } }
func home(ctx *web.Context) { ctx.Redirect(301, "/stat.html") }