From 206989bccca6282e3e9af18a7b60bd7be8f02526 Mon Sep 17 00:00:00 2001 From: xoy Date: Sun, 29 Jan 2023 15:39:38 +0100 Subject: [PATCH] =?UTF-8?q?funktionalit=C3=A4t=20der=20events=20erweitert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.go | 8 ++++++++ events.go | 10 ++++++++-- http.go | 53 +++++++++++++++++++++++++++++++++++++---------------- main.go | 38 ++------------------------------------ structs.go | 4 ---- 5 files changed, 55 insertions(+), 58 deletions(-) diff --git a/database.go b/database.go index 863c4f8..f320590 100644 --- a/database.go +++ b/database.go @@ -2,6 +2,7 @@ package main import ( "database/sql" + "strings" _ "github.com/go-sql-driver/mysql" ) @@ -29,3 +30,10 @@ func ctdoConnect() *sql.DB { return dbConnect(dbValues.username, dbValues.password, dbValues.address, dbValues.port, dbValues.database) } + +func sqlClean(sqlString string) string { + sqlString = strings.ReplaceAll(sqlString, "'", "") + sqlString = strings.ReplaceAll(sqlString, "\"", "") + + return sqlString +} diff --git a/events.go b/events.go index 9404ce2..60b97c4 100644 --- a/events.go +++ b/events.go @@ -2,6 +2,7 @@ package main func getEvents() []event { db := ctdoConnect() + defer dbClose(db) rows := dbQuerry(db, "SELECT * FROM events;") @@ -21,6 +22,7 @@ func getEvents() []event { func getEventCount() int { db := ctdoConnect() + defer dbClose(db) row := dbQuerry(db, "SELECT COUNT(*) FROM events;") @@ -35,18 +37,22 @@ func getEventCount() int { func addEvent(Event event) bool { db := ctdoConnect() + defer dbClose(db) - if len(Event.title) > 80 || len(Event.description) > 500 || len(Event.media) > 10000 || len(Event.date) > 10 { + if len(Event.title) > 80 || len(Event.description) > 500 || len(Event.media) == 10000 || len(Event.date) > 10 { return false } - dbQuerry(db, "insert into events (title, description, media, date) values ('"+Event.title+"', '"+Event.description+"', '"+Event.media+"', '"+Event.date+"');") + sqlString := "insert into events (title, description, media, date) values ('" + Event.title + "', '" + Event.description + "', '" + Event.media + "', '" + Event.date + "');" + + dbQuerry(db, sqlClean(sqlString)) return true } func getAdminKeys() []string { db := ctdoConnect() + defer dbClose(db) rows := dbQuerry(db, "select * from adminKeys;") diff --git a/http.go b/http.go index 644f8c4..7a6080d 100644 --- a/http.go +++ b/http.go @@ -21,11 +21,12 @@ func httpHandleFunc(urlPath string, filepath string, contentType string) { } func httpHandleFuncWithPOST(urlPath string, filepath string, contentType string) { + Event := new(event) logger(readHttpYML() + "/" + urlPath + " <--> " + filepath + " <" + contentType + ">") - s := new(submit) - s.data = "null" http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { + logger(r.Method + " request -> " + readHttpYML() + "/" + urlPath + " <" + contentType + ">") + r.ParseMultipartForm(10 << 20) err := r.ParseMultipartForm(200000) errorPanic(err) @@ -35,39 +36,59 @@ func httpHandleFuncWithPOST(urlPath string, filepath string, contentType string) files := formdata.File["media"] if filepath == "./web/pages/admin/dashboard.html" { + logger("----------------ADD EVENT----------------") title := formdata.Value["title"] description := formdata.Value["description"] media := formdata.File["media"] + mediaString := "" date := formdata.Value["date"] if title[0] != "" && description[0] != "" && media != nil && date[0] != "" { - logger("----------------POST----------------") logger("title: " + title[0]) logger("descrtiption: " + description[0]) logger("media: " + string(len(media))) - logger("files uploaded successfully : ") + logger("files uploaded successfully: ") for i, _ := range files { - file, err := files[i].Open() - errorPanic(err) - - out, err := os.Create("./web/images/" + files[i].Filename) - errorPanic(err, "unable to create the file '"+"./web/images/"+files[i].Filename+"' for writing. Check your write access privilege") - - _, err = io.Copy(out, file) - errorPanic(err) + if len(media) > 0 { + mediaString += "," + } + mediaString += "./web/images/" + files[i].Filename logger("./web/images/" + files[i].Filename) } + + Event.id = -1 + Event.title = title[0] + Event.description = description[0] + Event.media = mediaString + Event.date = date[0] + + logger("date: " + date[0]) + + if addEvent(*Event) { + for i, _ := range files { + file, err := files[i].Open() + errorPanic(err) + + out, err := os.Create("./web/images/" + files[i].Filename) + errorPanic(err, "unable to create the file -> '"+"./web/images/"+files[i].Filename+"' : check your write access privilege") + + _, err = io.Copy(out, file) + errorPanic(err) + } + logger("event added!") + } else { + logger("event not added!") + } + } else { + logger("no formdata") } - logger("date: " + date[0]) - logger("----------------POST END----------------") + logger("----------------ADD END----------------") } } - logger(r.Method + " request -> " + readHttpYML() + "/" + urlPath + " <" + contentType + ">") - w.Header().Add("Content-Type", contentType) io.WriteString(w, htmlReplacer(fileRead(filepath), urlPath)) diff --git a/main.go b/main.go index 1f71381..ef42fe7 100644 --- a/main.go +++ b/main.go @@ -28,34 +28,14 @@ func main() { } func handler() { - logger("Pages:") + logger("----------------HANDLE PAGES----------------") //pages httpHandleFunc("", "./web/pages/home.html", "text/html") handleFilesInFolder("", "./web/pages/", true) - /* - httpHandleFunc("home", "./web/pages/home.html", "text/html") - httpHandleFunc("treff", "./web/pages/treff.html", "text/html") - httpHandleFunc("events", "./web/pages/events.html", "text/html") - httpHandleFunc("about", "./web/pages/about.html", "text/html") - - //contact pages - httpHandleFunc("kontakt", "./web/pages/kontakt.html", "text/html") - httpHandleFunc("kontakt/adresse", "./web/pages/kontakt/adresse.html", "text/html") - httpHandleFunc("kontakt/irc", "./web/pages/kontakt/irc.html", "text/html") - httpHandleFunc("kontakt/mail", "./web/pages/kontakt/mail.html", "text/html") - httpHandleFunc("kontakt/tel", "./web/pages/kontakt/tel.html", "text/html") - - //pages - httpHandleFunc("verein", "./web/pages/verein.html", "text/html") - httpHandleFunc("support", "./web/pages/support.html", "text/html") - httpHandleFunc("impressum", "./web/pages/impressum.html", "text/html") - httpHandleFunc("datenschutz", "./web/pages/datenschutz.html", "text/html") - */ //admin pages keys := getAdminKeys() - if len(keys) > 0 { for _, key := range keys { httpHandleFunc("admin/"+key, "./web/pages/admin/dashboard.html", "text/html") @@ -65,24 +45,10 @@ func handler() { //styles handleFilesInFolder("style/", "./web/styles/", false) - /* - httpHandleFunc("style/main.css", "./web/styles/main.css", "text/css") - httpHandleFunc("style/kontakt.css", "./web/styles/kontakt.css", "text/css") - httpHandleFunc("style/home.css", "./web/styles/home.css", "text/css") - httpHandleFunc("style/events.css", "./web/styles/events.css", "text/css") - httpHandleFunc("style/dashboard.css", "./web/styles/dashboard.css", "text/css") - */ //images handleFilesInFolder("image/", "./web/images/", false) - /* - httpHandleFunc("image/logo_ctdo.svg", "./web/images/logo_ctdo.svg", "image/svg+xml") - httpHandleFunc("image/header.jpg", "./web/images/header.jpg", "image/jpeg") - httpHandleFunc("image/adresse_knopf.webp", "./web/images/adresse_knopf.webp", "image/webp") - httpHandleFunc("image/chat_knopf.webp", "./web/images/chat_knopf.webp", "image/webp") - httpHandleFunc("image/mail_knopf.webp", "./web/images/mail_knopf.webp", "image/webp") - httpHandleFunc("image/tel_knopf.webp", "./web/images/tel_knopf.webp", "image/webp") - */ + logger("----------------HANDLE END----------------") } var alreadyHandledFiles []string = []string{"./web/pages/admin/dashboard.html"} diff --git a/structs.go b/structs.go index 6a3ce74..da7b257 100644 --- a/structs.go +++ b/structs.go @@ -1,9 +1,5 @@ package main -type submit struct { - data string -} - type status struct { state bool lastchange int64