-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubmission.go
162 lines (133 loc) · 3.41 KB
/
submission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package formailer
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"mime/multipart"
"net/url"
"sort"
"strings"
)
// Submission is the unmarshaled version on the form submission.
// It contains the submitted values and the form settings needed for sending emails.
type Submission struct {
// Form is the form this submission submitted as.
Form *Form
// Order is a list of the fields in the original order of submisson.
// Maps in go are sorted alphabetically which causes readability issues in the generated emails. Note only first level elements are ordered.
// The fields set using Form.Ignore will be removed from this list.
Order []string
// Values contains the submitted form data.
Values map[string]interface{}
// Attachments is a list of files to be attached to the email
Attachments []Attachment
}
// Attachment contains file data for an email attachment
type Attachment struct {
Filename string
MimeType string
Data []byte
}
var forceStringFields = []string{
"_form_name", "g-recaptcha-response",
}
func (s *Submission) forceString(vals url.Values) {
for _, key := range forceStringFields {
s.Values[key] = vals.Get(key)
}
}
func (s *Submission) removeIgnored() {
for i := 0; i < len(s.Order); i++ {
if s.Form.ignore[s.Order[i]] {
s.Order = append(s.Order[:i], s.Order[i+1:]...)
i--
}
}
}
func (s *Submission) parseJSON(body string) error {
b := []byte(body)
err := json.Unmarshal(b, &s.Values)
if err != nil {
return err
}
index := make(map[string]int)
for key := range s.Values {
s.Order = append(s.Order, key)
esc, _ := json.Marshal(key)
index[key] = bytes.Index(b, append(esc, ':'))
}
sort.Slice(s.Order, func(i, j int) bool {
return index[s.Order[i]] < index[s.Order[j]]
})
return nil
}
func (s *Submission) parseURLEncoded(body string) error {
vals, err := url.ParseQuery(body)
if err != nil {
return err
}
index := make(map[string]int)
for key := range vals {
s.Values[key] = vals[key]
index[key] = strings.Index(body, key+"=")
s.Order = append(s.Order, key)
}
sort.Slice(s.Order, func(i, j int) bool {
return index[s.Order[i]] < index[s.Order[j]]
})
s.forceString(vals)
return nil
}
func (s *Submission) parseMultipartForm(boundary, body string) error {
decodedBody, err := base64.StdEncoding.DecodeString(body)
if err != nil {
decodedBody = []byte(body)
}
decodedBody = append(decodedBody, '\n')
values := make(url.Values)
reader := multipart.NewReader(bytes.NewReader(decodedBody), boundary)
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
return err
}
key := part.FormName()
value := new(bytes.Buffer)
value.ReadFrom(part)
filename := part.FileName()
if len(filename) > 0 {
attachment := Attachment{
Filename: part.FileName(),
MimeType: part.Header.Get("Content-Type"),
Data: value.Bytes(),
}
s.Attachments = append(s.Attachments, attachment)
values[key] = append(values[key], part.FileName())
} else {
values[key] = append(values[key], value.String())
}
if _, ok := s.Values[key]; !ok {
s.Order = append(s.Order, key)
}
s.Values[key] = values[key]
}
s.forceString(values)
return nil
}
// Send sends all the emails for this form
func (s *Submission) Send() error {
for _, e := range s.Form.Emails {
email, err := e.Email(s)
if err != nil {
return err
}
if err := e.Send(email); err != nil {
return err
}
}
return nil
}