diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1a3efc1 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module codeberg.org/opennet/PoC/MSodt + +go 1.23 + +require github.com/go-chi/chi/v5 v5.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..823cdbb --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= diff --git a/handyausgabe.odt b/handyausgabe.odt new file mode 100644 index 0000000..8f68d0b Binary files /dev/null and b/handyausgabe.odt differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..b4508ad --- /dev/null +++ b/main.go @@ -0,0 +1,188 @@ +package main + +import ( + "archive/zip" + "bytes" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "os" + "os/exec" + "strings" + + "github.com/go-chi/chi/v5" +) + +// ------------------------------------------------------------------------------------------------- +func main() { + fmt.Printf("Programm wurde gestartet :)") + router := chi.NewRouter() + router.Use(jsonHeaderMiddleware) + router.Post("/api/passwort", postPasswort) + router.Post("/api/handy", postHandy) + router.Handle("/*", http.StripPrefix("/", http.FileServer(http.Dir("www")))) + log.Fatal(http.ListenAndServe(":8000", router)) +} + +func postPasswort(w http.ResponseWriter, r *http.Request) { + + felder := make(map[string]string) + + err := json.NewDecoder(r.Body).Decode(&felder) + check(err) + fmt.Printf("Username is %s\n", felder) + + odtdatei := "www/data/passwort.odt" + + fillODTForm("vorlagen/passwortschreiben.odt", odtdatei, felder) + makepdf(odtdatei) + + json.NewEncoder(w).Encode("OK") +} + +func postHandy(w http.ResponseWriter, r *http.Request) { + + felder := make(map[string]string) + + err := json.NewDecoder(r.Body).Decode(&felder) + check(err) + fmt.Printf("Username is %s\n", felder) + + odtdatei := "www/data/handy.odt" + + fillODTForm("vorlagen/handyausgabe.odt", odtdatei, felder) + makepdf(odtdatei) + + json.NewEncoder(w).Encode("OK") +} + +func fillODTForm(inputPath, outputPath string, fields map[string]string) error { + // Open the ODT file + reader, err := zip.OpenReader(inputPath) + if err != nil { + return err + } + defer reader.Close() + + // Create a new ODT file + writer, err := os.Create(outputPath) + if err != nil { + return err + } + defer writer.Close() + + zipWriter := zip.NewWriter(writer) + defer zipWriter.Close() + + // Process each file in the ODT + for _, file := range reader.File { + if file.Name == "content.xml" { + // Parse and modify content.xml + rc, err := file.Open() + if err != nil { + return err + } + defer rc.Close() + + content, err := io.ReadAll(rc) + if err != nil { + return err + } + + // Parse XML and fill form fields + // This is a simplified example - you'll need to implement proper XML parsing + for field, value := range fields { + content = bytes.Replace(content, []byte("%"+field+"%"), []byte(value), -1) + } + + // Write modified content.xml to new ODT + w, err := zipWriter.Create(file.Name) + if err != nil { + return err + } + _, err = w.Write(content) + if err != nil { + return err + } + } else { + // Copy other files as-is + w, err := zipWriter.Create(file.Name) + if err != nil { + return err + } + rc, err := file.Open() + if err != nil { + return err + } + _, err = io.Copy(w, rc) + rc.Close() + if err != nil { + return err + } + } + } + + return nil +} + +//-------------------------------------------------------------------------- +// HTTP-Middelwares +//-------------------------------------------------------------------------- + +func jsonHeaderMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL.Path) + if strings.HasPrefix(r.URL.Path, "/api/") { + w.Header().Add("Content-Type", "application/json") + } + next.ServeHTTP(w, r) + }) +} + +//-------------------------------------------------------------------------- +// Hilfsfunktionen +//-------------------------------------------------------------------------- + +func check(e error) bool { + if e != nil { + fmt.Println(e) + return false + } + return true +} + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +func fileToString(filename string) string { + b, err := os.ReadFile(filename) + check(err) + str := string(b) + return str +} + +//-------------------------------------------------------------------------- +// PDF -Konvertirung +//-------------------------------------------------------------------------- + +func makepdf(odtdatei string) { + // Der Befehl, den du ausführen möchtest + cmd := exec.Command("/usr/lib/libreoffice/program/soffice.bin", "--headless", "--convert-to", "pdf", "./www/data", "--outdir", "./www/data", odtdatei) + + // Führe den Befehl aus und erfasse die Ausgabe + output, err := cmd.Output() + if err != nil { + fmt.Println("Fehler beim Ausführen des Befehls:", err) + return + } + + // Gib die Ausgabe des Befehls aus + fmt.Println(string(output)) +} diff --git a/passwortschreiben.odt b/passwortschreiben.odt new file mode 100644 index 0000000..289697d Binary files /dev/null and b/passwortschreiben.odt differ