1
0

Removed old CAPTCHA and guestbook admin handlers from Handlers struct.

This commit is contained in:
Alexander Andreev 2023-05-06 23:11:04 +04:00
parent 42846b8d7a
commit a736cc508f
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -1,8 +1,6 @@
package http
import (
"fmt"
"image/jpeg"
"math"
"net/http"
"strconv"
@ -13,7 +11,6 @@ import (
"git.arav.su/Arav/dwelling-home/pkg/servicestat"
"git.arav.su/Arav/dwelling-home/pkg/util"
"git.arav.su/Arav/dwelling-home/web"
"git.arav.su/Arav/justcaptcha/pkg/captcha"
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
"git.arav.su/Arav/justcaptcha/pkg/dwcaptcha"
gbsqlite "git.arav.su/Arav/justguestbook/database/sqlite"
@ -131,43 +128,6 @@ func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) {
web.Guestbook("/ Guestbook", h.guestbookOwner, string(id), pageCount, entries, r, w)
}
func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) {
var entry *guestbook.Entry
var err error
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
r.ParseForm()
if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) {
h.ForbiddenError("Wrong answer given.", "Here's your message:"+r.FormValue("message"), w)
return
}
if r.FormValue("name") == "" {
r.Form.Set("name", "Anonymous")
}
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
message = strings.ReplaceAll(message, "\n\r", "\n")
message = strings.ReplaceAll(message, "\r", "\n")
entry, err = guestbook.NewEntry(r.FormValue("name"), message,
r.FormValue("website"), r.FormValue("hide_website") != "")
if err != nil {
h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
return
}
}
if err = h.guestbookDB.NewEntry(entry); err != nil {
h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
return
}
http.Redirect(w, r, "/guestbook", http.StatusMovedPermanently)
}
func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) {
entriesCount, _ := h.guestbookDB.Count()
entries, err := h.guestbookDB.Entries(1, entriesCount)
@ -181,70 +141,6 @@ func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) {
web.GuestbookAdmin("/ Guestbook Administration", h.guestbookOwner, entries, r, w)
}
func (h *Handlers) GuestbookAdminDeleteEntry(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = h.guestbookDB.DeleteEntry(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h *Handlers) GuestbookAdminUpdateEntry(w http.ResponseWriter, r *http.Request) {
}
func (h *Handlers) GuestbookAdminDeleteReply(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = h.guestbookDB.DeleteReply(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h *Handlers) GuestbookAdminUpdateReply(w http.ResponseWriter, r *http.Request) {
}
func (h *Handlers) GuestbookAdminReply(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id, err := strconv.ParseInt(r.FormValue("entry_id"), 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reply, err := guestbook.NewReply(id, r.FormValue("message"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.URL.Query().Has("update") {
if err = h.guestbookDB.UpdateReply(reply); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
err = h.guestbookDB.NewReply(reply)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) {
posts := []mindflow.Post{
{Category: "Kek", Date: time.Now(), Title: "LMAO", Body: "Testing!"},
@ -273,44 +169,3 @@ func InternalError(err, msg string, w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
web.InternalError("/ Internal Error", err, msg, w)
}
/**** CAPTCHA *****************************************************************/
func (h *Handlers) CaptchaNew(w http.ResponseWriter, r *http.Request) {
dwc := dwcaptcha.NewDwellingCaptcha(h.captchaExpire)
_, id := inmemdb.New(r.RemoteAddr, dwc)
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, id)
}
func (h *Handlers) CaptchaImage(w http.ResponseWriter, r *http.Request) {
id := captcha.ID(GetURLParam(r, "id"))
image := inmemdb.Image(id, r.URL.Query().Get("style"))
if image == nil {
http.Error(w, "image not found", http.StatusNotFound)
return
}
w.Header().Add("Content-Disposition", "inline; filename=\""+string(id)+"\"")
jpeg.Encode(w, *image, &jpeg.Options{Quality: 20})
}
func (h *Handlers) CaptchaSolve(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(GetURLParam(r, "id"))
isJustRemove := r.URL.Query().Has("remove")
if isJustRemove {
inmemdb.Remove(captchaID)
w.WriteHeader(http.StatusNoContent)
return
}
if solved := inmemdb.IsSolved(captchaID); !solved {
http.Error(w, "wrong answer", http.StatusForbidden)
return
}
w.WriteHeader(http.StatusNoContent)
}