2018-02-10 22:36:21 +00:00
|
|
|
package contact
|
2017-12-23 02:34:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2017-12-24 20:46:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2017-12-23 02:34:58 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-12-24 00:21:50 +00:00
|
|
|
"github.com/kirsle/blog/src/forms"
|
|
|
|
"github.com/kirsle/blog/src/log"
|
|
|
|
"github.com/kirsle/blog/src/mail"
|
|
|
|
"github.com/kirsle/blog/src/markdown"
|
|
|
|
"github.com/kirsle/blog/src/render"
|
|
|
|
"github.com/kirsle/blog/src/responses"
|
2018-04-12 01:59:30 +00:00
|
|
|
"github.com/kirsle/blog/models/settings"
|
2017-12-23 02:34:58 +00:00
|
|
|
)
|
|
|
|
|
2018-02-10 22:36:21 +00:00
|
|
|
// Register attaches the contact URL to the app.
|
2018-02-10 23:07:10 +00:00
|
|
|
func Register(r *mux.Router) {
|
2017-12-23 02:34:58 +00:00
|
|
|
r.HandleFunc("/contact", func(w http.ResponseWriter, r *http.Request) {
|
2018-04-12 01:59:30 +00:00
|
|
|
// Allow ?next= to redirect to other local pages.
|
|
|
|
nextURL := r.FormValue("next")
|
|
|
|
if nextURL != "" && nextURL[0] != '/' {
|
|
|
|
log.Error("/contact?next=: URL must be a local page beginning with /")
|
|
|
|
nextURL = ""
|
|
|
|
}
|
|
|
|
|
2018-02-10 22:05:41 +00:00
|
|
|
form := &forms.Contact{}
|
|
|
|
v := map[string]interface{}{
|
|
|
|
"Form": form,
|
|
|
|
}
|
2017-12-23 02:34:58 +00:00
|
|
|
|
|
|
|
// If there is no site admin, show an error.
|
|
|
|
cfg, err := settings.Load()
|
|
|
|
if err != nil {
|
2018-02-10 23:07:10 +00:00
|
|
|
responses.Error(w, r, "Error loading site configuration!")
|
2017-12-23 02:34:58 +00:00
|
|
|
return
|
|
|
|
} else if cfg.Site.AdminEmail == "" {
|
2018-02-10 23:07:10 +00:00
|
|
|
responses.Error(w, r, "There is no admin email configured for this website!")
|
2017-12-23 02:34:58 +00:00
|
|
|
return
|
|
|
|
} else if !cfg.Mail.Enabled {
|
2018-02-10 23:07:10 +00:00
|
|
|
responses.Error(w, r, "This website doesn't have an e-mail gateway configured.")
|
2017-12-23 02:34:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Posting?
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
form.ParseForm(r)
|
|
|
|
if err = form.Validate(); err != nil {
|
2018-04-12 01:59:30 +00:00
|
|
|
// If they're not from the /contact front-end, redirect them
|
|
|
|
// with the flash.
|
|
|
|
if len(nextURL) > 0 {
|
|
|
|
responses.FlashAndRedirect(w, r, nextURL, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise flash and let the /contact page render to retain
|
|
|
|
// their form fields so far.
|
2018-02-10 19:20:27 +00:00
|
|
|
responses.Flash(w, r, err.Error())
|
2017-12-23 02:34:58 +00:00
|
|
|
} else {
|
2018-02-10 22:36:21 +00:00
|
|
|
go mail.SendEmail(mail.Email{
|
2017-12-23 02:34:58 +00:00
|
|
|
To: cfg.Site.AdminEmail,
|
|
|
|
Admin: true,
|
|
|
|
ReplyTo: form.Email,
|
|
|
|
Subject: fmt.Sprintf("Contact Form on %s: %s", cfg.Site.Title, form.Subject),
|
|
|
|
Template: ".email/contact.gohtml",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Name": form.Name,
|
2018-02-10 03:01:56 +00:00
|
|
|
"Message": template.HTML(markdown.RenderMarkdown(form.Message)),
|
2017-12-23 02:34:58 +00:00
|
|
|
"Email": form.Email,
|
|
|
|
},
|
|
|
|
})
|
2017-12-24 20:46:15 +00:00
|
|
|
|
|
|
|
// Log it to disk, too.
|
2018-02-10 22:36:21 +00:00
|
|
|
fh, err := os.OpenFile(filepath.Join(*render.UserRoot, ".contact.log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
2017-12-24 20:46:15 +00:00
|
|
|
if err != nil {
|
2018-02-10 19:20:27 +00:00
|
|
|
responses.Flash(w, r, "Error logging the message to disk: %s", err)
|
2017-12-24 20:46:15 +00:00
|
|
|
} else {
|
|
|
|
fh.WriteString(fmt.Sprintf(
|
|
|
|
"Date: %s\nName: %s\nEmail: %s\nSubject: %s\n\n%s\n\n--------------------\n\n",
|
|
|
|
time.Now().Format(time.UnixDate),
|
|
|
|
form.Name,
|
|
|
|
form.Email,
|
|
|
|
form.Subject,
|
|
|
|
form.Message,
|
|
|
|
))
|
|
|
|
fh.Close()
|
|
|
|
}
|
2018-04-12 01:59:30 +00:00
|
|
|
|
|
|
|
if len(nextURL) > 0 {
|
|
|
|
responses.FlashAndRedirect(w, r, nextURL, "Your message has been sent.")
|
|
|
|
} else {
|
|
|
|
responses.FlashAndRedirect(w, r, "/contact", "Your message has been sent.")
|
|
|
|
}
|
|
|
|
return
|
2017-12-23 02:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 22:05:41 +00:00
|
|
|
render.Template(w, r, "contact", v)
|
2017-12-23 02:34:58 +00:00
|
|
|
})
|
|
|
|
}
|