-
Notifications
You must be signed in to change notification settings - Fork 2
/
page_admin.go
259 lines (245 loc) · 6.44 KB
/
page_admin.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"crypto/md5"
"errors"
"exp/html"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
)
func newArticleHandler(w http.ResponseWriter, r *http.Request) {
var feedback string
var article = &Article{}
r.ParseForm()
old, ok := getOld(r.URL.Path)
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
if r.Method == "POST" {
if ok := checkVerifiCode(r); ok == false {
feedback = "Oops...! " + "Verification code error"
goto out
}
if err := checkArticle(r); err != nil {
article = &Article{
Title: r.Form.Get("title"),
Author: r.Form.Get("author"),
Src: r.Form.Get("content"),
Tags: genTags(r.Form.Get("tags")),
}
feedback = "Oops...! " + err.Error()
goto out
}
if temp, err := genArticle(r); err != nil {
feedback = "Oops...! " + err.Error()
goto out
} else {
article = temp
}
if r.Form.Get("post") == "preview" {
if err := tmpl.ExecuteTemplate(w, "preview", map[string]interface{}{
"config": config,
"article": article,
"comments": make([]int, 0),
"header": article.Title + "(Preview)",
"code": genVerifiCode(w),
}); err != nil {
logger.Println("new:", err.Error())
}
return
}
if ok {
if old.Email != article.Email {
feedback = "Oops..! " + "This email address can't be the author."
goto out
}
article.Id = old.Id
article.Date = old.Date
} else {
article.Id = allocArticleId()
}
newArticleAuth(article, old)
feedback = "Welcome my dear admin! Mail request has been sent, please check your mail."
} else {
if ok {
article = old
}
}
out:
if err := tmpl.ExecuteTemplate(w, "new", map[string]interface{}{
"config": config,
"feedback": feedback,
"form": map[string]string{
"Title": html.EscapeString(article.Title),
"Author": html.EscapeString(article.Author),
"Content": html.EscapeString(article.Src),
"Email": html.EscapeString(article.Email),
},
"tagsNow": strings.Join(article.Tags, ", "),
"allTags": getAllTags(),
"header": "Admin Panel",
"code": genVerifiCode(w),
}); err != nil {
logger.Println("new:", err.Error())
}
}
func getOld(url string) (old *Article, ex bool) {
if url[len(config["AdminUrl"]):] == "" {
return nil, false
}
id64, err := strconv.ParseUint(url[len(config["AdminUrl"]):], 10, 32)
if err != nil {
return nil, false
}
ret := getArticle(aid(id64))
if ret == nil {
return nil, false
}
return ret, true
}
func newArticleAuth(a, old *Article) {
url := notifRegister(func(w http.ResponseWriter, r *http.Request) {
// EventStart: newArticle
setArticle(a)
if old != nil {
updateTags(a.Id, old.Tags, a.Tags)
updateAuthor(a.Id, old.Author, a.Author)
} else {
updateTags(a.Id, nil, a.Tags)
updateAuthor(a.Id, "", a.Author)
}
go updateIndexAndFeed()
// EventEnd: newArticle
http.Redirect(w, r, config["ArticleUrl"]+fmt.Sprint(a.Id), http.StatusFound)
})
send(a.Email, "New article authentication", fmt.Sprintf(
`<p>Dear %s, you have an article to be authenticated for publishment:
<p>
%s
</p></p>
<p>If you know what you are doing, please click <a href="%s">here</a>.</p>`, a.Author, a.Content, url))
}
func genTags(tagList string) []string { // tags shouldn't contain quote marks, please don't ask why...
m := map[string]struct{}{}
for _, s := range strings.Split(tagList, ",") {
strings.Replace(s, "'", "", -1)
strings.Replace(s, "\"", "", -1)
if t := strings.TrimSpace(s); len(t) != 0 {
m[t] = struct{}{}
}
}
ret := []string{}
for t, _ := range m {
ret = append(ret, t)
}
sortSlice(ret, func(a, b interface{}) bool {
return a.(string) < b.(string)
})
return ret
}
func genArticle(r *http.Request) (*Article, error) {
tagList := genTags(r.Form.Get("tags"))
// may we need a filter?
return &Article{
Author: html.EscapeString(r.Form.Get("author")),
Email: html.EscapeString(r.Form.Get("email")),
RemoteAddr: r.RemoteAddr,
Date: time.Now(),
Title: html.EscapeString(r.Form.Get("title")),
Src: r.Form.Get("content"),
Content: process(r.Form.Get("content")),
Notif: r.Form.Get("notify") == "on",
Tags: tagList,
}, nil
}
func process(content string) string {
ret := ""
t := html.NewTokenizer(strings.NewReader(content))
latex := false
latexSrc := ""
L:
for {
t.Next()
token := t.Token()
str := token.String()
if latex {
switch token.Type {
case html.ErrorToken:
break L
case html.EndTagToken:
if token.Data == "latex" {
latex = false
ret += fmt.Sprintf("<img src=\"%s\" alt=\"%s\"/>", genLaTeX(html.UnescapeString(latexSrc)), latexSrc)
latexSrc = ""
} else {
latexSrc += str
}
default:
latexSrc += str
}
} else {
switch token.Type {
case html.ErrorToken:
break L
case html.StartTagToken:
if token.Data == "latex" {
latex = true
} else {
ret += str
}
default:
ret += str
}
}
}
return ret
}
var latexMutex sync.Mutex
func genLaTeX(src string) string {
logger.Println("LaTeX", src)
cmd := exec.Command("/usr/bin/latex", "-output-directory=/tmp")
cmd.Stdin = strings.NewReader(fmt.Sprintf(`
\documentclass{article}
\pagestyle{empty}
\begin{document}
{\huge %s}
\end{document}`, src))
latexMutex.Lock()
defer latexMutex.Unlock()
if err := cmd.Run(); err != nil {
logger.Println("latex:", err.Error())
return ""
}
fileName := getLaTeXFileName(src)
filePath := config["DocumentPath"] + "static/image/latex/" + fileName
logger.Println("LaTeX", filePath)
if _, err := os.Stat(filePath); err != nil {
if err := exec.Command("/usr/bin/convert", "-trim", "/tmp/texput.dvi", filePath).Run(); err != nil {
logger.Println("latex:", err.Error())
return ""
}
}
return config["RootUrl"] + "image/latex/" + fileName
}
func getLaTeXFileName(src string) string {
h := md5.New()
io.WriteString(h, src)
return fmt.Sprintf("%x", h.Sum(nil)) + ".png" // FIXME collision
}
func checkArticle(a *http.Request) error {
if a.Form.Get("author") == "" || a.Form.Get("email") == "" || a.Form.Get("content") == "" || a.Form.Get("title") == "" {
return errors.New("Name, email, content and title can't be blank.")
}
if _, ex := adminList[a.Form.Get("email")]; !ex {
return errors.New("This email address can't be the author.")
}
return nil
}
func initPageAdmin() {
config["AdminUrl"] = config["RootUrl"] + "admin/"
http.HandleFunc(config["AdminUrl"], getGzipHandler(newArticleHandler))
}