-
Notifications
You must be signed in to change notification settings - Fork 19
/
posting.go
executable file
·703 lines (628 loc) · 22.7 KB
/
posting.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// functions for handling posting, uploading, and bans
package main
import (
"bytes"
"crypto/md5"
"database/sql"
"errors"
"fmt"
"html"
"image"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/aquilax/tripcode"
"github.com/disintegration/imaging"
)
const (
whitespaceMatch = "[\000-\040]"
gt = ">"
yearInSeconds = 31536000
)
var (
allSections []interface{}
allBoards []interface{}
)
// bumps the given thread on the given board and returns true if there were no errors
func bumpThread(postID, boardID int) error {
_, err := execSQL("UPDATE `"+config.DBprefix+"posts` SET `bumped` = ? WHERE `id` = ? AND `boardid` = ?",
time.Now(), postID, boardID,
)
return err
}
// Checks check poster's name/tripcode/file checksum (from PostTable post) for banned status
// returns ban table if the user is banned or errNotBanned if they aren't
func getBannedStatus(request *http.Request) (BanlistTable, error) {
var banEntry BanlistTable
formName := request.FormValue("postname")
var tripcode string
if formName != "" {
parsedName := parseName(formName)
tripcode += parsedName["name"]
if tc, ok := parsedName["tripcode"]; ok {
tripcode += "!" + tc
}
}
ip := getRealIP(request)
var filename string
var checksum string
file, fileHandler, err := request.FormFile("imagefile")
defer func() {
if file != nil {
file.Close()
}
}()
if err == nil {
html.EscapeString(fileHandler.Filename)
if data, err2 := ioutil.ReadAll(file); err2 == nil {
checksum = fmt.Sprintf("%x", md5.Sum(data))
}
}
in := []interface{}{ip}
query := "SELECT `id`,`ip`,`name`,`boards`,`timestamp`,`expires`,`permaban`,`reason`,`type`,`appeal_at`,`can_appeal` FROM `" + config.DBprefix + "banlist` WHERE `ip` = ? "
if tripcode != "" {
in = append(in, tripcode)
query += "OR `name` = ? "
}
if filename != "" {
in = append(in, filename)
query += "OR `filename` = ? "
}
if checksum != "" {
in = append(in, checksum)
query += "OR `file_checksum` = ? "
}
query += " ORDER BY `id` DESC LIMIT 1"
err = queryRowSQL(query, in, []interface{}{
&banEntry.ID, &banEntry.IP, &banEntry.Name, &banEntry.Boards, &banEntry.Timestamp,
&banEntry.Expires, &banEntry.Permaban, &banEntry.Reason, &banEntry.Type,
&banEntry.AppealAt, &banEntry.CanAppeal},
)
return banEntry, err
}
func isBanned(ban BanlistTable, board string) bool {
if ban.Boards == "" && (ban.Expires.After(time.Now()) || ban.Permaban) {
return true
}
boardsArr := strings.Split(ban.Boards, ",")
for _, b := range boardsArr {
if b == board && (ban.Expires.After(time.Now()) || ban.Permaban) {
return true
}
}
return false
}
func sinceLastPost(post *PostTable) int {
var lastPostTime time.Time
if err := queryRowSQL("SELECT `timestamp` FROM `"+config.DBprefix+"posts` WHERE `ip` = '?' ORDER BY `timestamp` DESC LIMIT 1",
[]interface{}{post.IP},
[]interface{}{&lastPostTime},
); err == sql.ErrNoRows {
// no posts by that IP.
return -1
}
return int(time.Since(lastPostTime).Seconds())
}
func createImageThumbnail(image_obj image.Image, size string) image.Image {
var thumbWidth int
var thumbHeight int
switch size {
case "op":
thumbWidth = config.ThumbWidth
thumbHeight = config.ThumbHeight
case "reply":
thumbWidth = config.ThumbWidth_reply
thumbHeight = config.ThumbHeight_reply
case "catalog":
thumbWidth = config.ThumbWidth_catalog
thumbHeight = config.ThumbHeight_catalog
}
old_rect := image_obj.Bounds()
if thumbWidth >= old_rect.Max.X && thumbHeight >= old_rect.Max.Y {
return image_obj
}
thumbW, thumbH := getThumbnailSize(old_rect.Max.X, old_rect.Max.Y, size)
image_obj = imaging.Resize(image_obj, thumbW, thumbH, imaging.CatmullRom) // resize to 600x400 px using CatmullRom cubic filter
return image_obj
}
func createVideoThumbnail(video, thumb string, size int) error {
sizeStr := strconv.Itoa(size)
outputBytes, err := exec.Command("ffmpeg", "-y", "-itsoffset", "-1", "-i", video, "-vframes", "1", "-filter:v", "scale='min("+sizeStr+"\\, "+sizeStr+"):-1'", thumb).CombinedOutput()
println(2, "ffmpeg output: \n"+string(outputBytes))
if err != nil {
outputStringArr := strings.Split(string(outputBytes), "\n")
if len(outputStringArr) > 1 {
outputString := outputStringArr[len(outputStringArr)-2]
err = errors.New(outputString)
}
}
return err
}
func getVideoInfo(path string) (map[string]int, error) {
vidInfo := make(map[string]int)
outputBytes, err := exec.Command("ffprobe", "-v quiet", "-show_format", "-show_streams", path).CombinedOutput()
if err == nil && outputBytes != nil {
outputStringArr := strings.Split(string(outputBytes), "\n")
for _, line := range outputStringArr {
lineArr := strings.Split(line, "=")
if len(lineArr) < 2 {
continue
}
if lineArr[0] == "width" || lineArr[0] == "height" || lineArr[0] == "size" {
value, _ := strconv.Atoi(lineArr[1])
vidInfo[lineArr[0]] = value
}
}
}
return vidInfo, err
}
func getNewFilename() string {
now := time.Now().Unix()
rand.Seed(now)
return strconv.Itoa(int(now)) + strconv.Itoa(rand.Intn(98)+1)
}
// find out what out thumbnail's width and height should be, partially ripped from Kusaba X
func getThumbnailSize(w int, h int, size string) (newWidth int, newHeight int) {
var thumbWidth int
var thumbHeight int
switch {
case size == "op":
thumbWidth = config.ThumbWidth
thumbHeight = config.ThumbHeight
case size == "reply":
thumbWidth = config.ThumbWidth_reply
thumbHeight = config.ThumbHeight_reply
case size == "catalog":
thumbWidth = config.ThumbWidth_catalog
thumbHeight = config.ThumbHeight_catalog
}
if w == h {
newWidth = thumbWidth
newHeight = thumbHeight
} else {
var percent float32
if w > h {
percent = float32(thumbWidth) / float32(w)
} else {
percent = float32(thumbHeight) / float32(h)
}
newWidth = int(float32(w) * percent)
newHeight = int(float32(h) * percent)
}
return
}
func parseName(name string) map[string]string {
parsed := make(map[string]string)
if !strings.Contains(name, "#") {
parsed["name"] = name
parsed["tripcode"] = ""
} else if strings.Index(name, "#") == 0 {
parsed["tripcode"] = tripcode.Tripcode(name[1:])
} else if strings.Index(name, "#") > 0 {
postNameArr := strings.SplitN(name, "#", 2)
parsed["name"] = postNameArr[0]
parsed["tripcode"] = tripcode.Tripcode(postNameArr[1])
}
return parsed
}
// inserts prepared post object into the SQL table so that it can be rendered
func insertPost(post PostTable, bump bool) (sql.Result, error) {
result, err := execSQL(
"INSERT INTO `"+config.DBprefix+"posts` "+
"(`boardid`,`parentid`,`name`,`tripcode`,`email`,`subject`,`message`,`message_raw`,`password`,`filename`,`filename_original`,`file_checksum`,`filesize`,`image_w`,`image_h`,`thumb_w`,`thumb_h`,`ip`,`tag`,`timestamp`,`autosage`,`poster_authority`,`deleted_timestamp`,`bumped`,`stickied`,`locked`,`reviewed`,`sillytag`)"+
"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
post.BoardID, post.ParentID, post.Name, post.Tripcode, post.Email,
post.Subject, post.MessageHTML, post.MessageText, post.Password,
post.Filename, post.FilenameOriginal, post.FileChecksum, post.Filesize,
post.ImageW, post.ImageH, post.ThumbW, post.ThumbH, post.IP, post.Tag,
post.Timestamp, post.Autosage, post.PosterAuthority, post.DeletedTimestamp,
post.Bumped, post.Stickied, post.Locked, post.Reviewed, post.Sillytag,
)
if err != nil {
return result, err
}
// Bump parent post if requested.
if post.ParentID != 0 && bump {
err = bumpThread(post.ParentID, post.BoardID)
if err != nil {
return nil, err
}
}
return result, err
}
// called when a user accesses /post. Parse form data, then insert and build
func makePost(writer http.ResponseWriter, request *http.Request) {
startTime := benchmarkTimer("makePost", time.Now(), true)
var maxMessageLength int
var post PostTable
domain := request.Host
var formName string
var nameCookie string
var formEmail string
// fix new cookie domain for when you use a port number
chopPortNumRegex := regexp.MustCompile(`(.+|\w+):(\d+)$`)
domain = chopPortNumRegex.Split(domain, -1)[0]
post.ParentID, _ = strconv.Atoi(request.FormValue("threadid"))
post.BoardID, _ = strconv.Atoi(request.FormValue("boardid"))
var emailCommand string
formName = request.FormValue("postname")
parsedName := parseName(formName)
post.Name = parsedName["name"]
post.Tripcode = parsedName["tripcode"]
formEmail = request.FormValue("postemail")
http.SetCookie(writer, &http.Cookie{Name: "email", Value: formEmail, Path: "/", Domain: domain, RawExpires: getSpecificSQLDateTime(time.Now().Add(time.Duration(yearInSeconds))), MaxAge: yearInSeconds})
if !strings.Contains(formEmail, "noko") && !strings.Contains(formEmail, "sage") {
post.Email = formEmail
} else if strings.Index(formEmail, "#") > 1 {
formEmailArr := strings.SplitN(formEmail, "#", 2)
post.Email = formEmailArr[0]
emailCommand = formEmailArr[1]
} else if formEmail == "noko" || formEmail == "sage" {
emailCommand = formEmail
post.Email = ""
}
post.Subject = request.FormValue("postsubject")
post.MessageText = strings.Trim(request.FormValue("postmsg"), "\r\n")
if err := queryRowSQL("SELECT `max_message_length` from `"+config.DBprefix+"boards` WHERE `id` = ?",
[]interface{}{post.BoardID},
[]interface{}{&maxMessageLength},
); err != nil {
serveErrorPage(writer, handleError(0, "Error getting board info: "+err.Error()))
return
}
if len(post.MessageText) > maxMessageLength {
serveErrorPage(writer, "Post body is too long")
return
}
post.MessageHTML = formatMessage(post.MessageText)
password := request.FormValue("postpassword")
if password == "" {
rand.Shuffle(len(chars), func(i, j int) {
password += fmt.Sprintf("%c", chars[j])
})
password = password[:8]
}
post.Password = md5Sum(password)
// Reverse escapes
nameCookie = strings.Replace(formName, "&", "&", -1)
nameCookie = strings.Replace(nameCookie, "\\'", "'", -1)
nameCookie = strings.Replace(url.QueryEscape(nameCookie), "+", "%20", -1)
// add name and email cookies that will expire in a year (31536000 seconds)
http.SetCookie(writer, &http.Cookie{Name: "name", Value: nameCookie, Path: "/", Domain: domain, RawExpires: getSpecificSQLDateTime(time.Now().Add(time.Duration(yearInSeconds))), MaxAge: yearInSeconds})
http.SetCookie(writer, &http.Cookie{Name: "password", Value: password, Path: "/", Domain: domain, RawExpires: getSpecificSQLDateTime(time.Now().Add(time.Duration(yearInSeconds))), MaxAge: yearInSeconds})
post.IP = getRealIP(request)
post.Timestamp = time.Now()
post.PosterAuthority = getStaffRank(request)
post.Bumped = time.Now()
post.Stickied = request.FormValue("modstickied") == "on"
post.Locked = request.FormValue("modlocked") == "on"
//post has no referrer, or has a referrer from a different domain, probably a spambot
if !validReferrer(request) {
accessLog.Print("Rejected post from possible spambot @ " + post.IP)
return
}
switch checkPostForSpam(post.IP, request.Header["User-Agent"][0], request.Referer(),
post.Name, post.Email, post.MessageText) {
case "discard":
serveErrorPage(writer, "Your post looks like spam.")
accessLog.Print("Akismet recommended discarding post from: " + post.IP)
return
case "spam":
serveErrorPage(writer, "Your post looks like spam.")
accessLog.Print("Akismet suggested post is spam from " + post.IP)
return
default:
}
file, handler, err := request.FormFile("imagefile")
defer func() {
if file != nil {
file.Close()
}
}()
if err != nil || handler.Size == 0 {
// no file was uploaded
post.Filename = ""
accessLog.Print("Receiving post from " + post.IP + ", referred from: " + request.Referer())
} else {
data, err := ioutil.ReadAll(file)
if err != nil {
serveErrorPage(writer, handleError(1, "Couldn't read file: "+err.Error()))
} else {
post.FilenameOriginal = html.EscapeString(handler.Filename)
filetype := getFileExtension(post.FilenameOriginal)
thumbFiletype := filetype
if thumbFiletype == "gif" || thumbFiletype == "webm" {
thumbFiletype = "jpg"
}
post.Filename = getNewFilename() + "." + getFileExtension(post.FilenameOriginal)
boardArr, _ := getBoardArr(map[string]interface{}{"id": request.FormValue("boardid")}, "")
if len(boardArr) == 0 {
serveErrorPage(writer, "No boards have been created yet")
return
}
_boardDir, _ := getBoardArr(map[string]interface{}{"id": request.FormValue("boardid")}, "")
boardDir := _boardDir[0].Dir
filePath := path.Join(config.DocumentRoot, "/"+boardDir+"/src/", post.Filename)
thumbPath := path.Join(config.DocumentRoot, "/"+boardDir+"/thumb/", strings.Replace(post.Filename, "."+filetype, "t."+thumbFiletype, -1))
catalogThumbPath := path.Join(config.DocumentRoot, "/"+boardDir+"/thumb/", strings.Replace(post.Filename, "."+filetype, "c."+thumbFiletype, -1))
if err = ioutil.WriteFile(filePath, data, 0777); err != nil {
handleError(0, "Couldn't write file \""+post.Filename+"\""+err.Error())
serveErrorPage(writer, "Couldn't write file \""+post.FilenameOriginal+"\"")
return
}
// Calculate image checksum
post.FileChecksum = fmt.Sprintf("%x", md5.Sum(data))
var allowsVids bool
if err = queryRowSQL("SELECT `embeds_allowed` FROM `"+config.DBprefix+"boards` WHERE `id` = ? LIMIT 1",
[]interface{}{post.BoardID},
[]interface{}{&allowsVids},
); err != nil {
serveErrorPage(writer, handleError(1, "Couldn't get board info: "+err.Error()))
return
}
if filetype == "webm" {
if !allowsVids || !config.AllowVideoUploads {
serveErrorPage(writer, "Video uploading is not currently enabled for this board.")
os.Remove(filePath)
return
}
accessLog.Print("Receiving post with video: " + handler.Filename + " from " + request.RemoteAddr + ", referrer: " + request.Referer())
if post.ParentID == 0 {
err := createVideoThumbnail(filePath, thumbPath, config.ThumbWidth)
if err != nil {
serveErrorPage(writer, handleError(1, err.Error()))
return
}
} else {
err := createVideoThumbnail(filePath, thumbPath, config.ThumbWidth_reply)
if err != nil {
serveErrorPage(writer, handleError(1, err.Error()))
return
}
}
if err := createVideoThumbnail(filePath, catalogThumbPath, config.ThumbWidth_catalog); err != nil {
serveErrorPage(writer, handleError(1, err.Error()))
return
}
outputBytes, err := exec.Command("ffprobe", "-v", "quiet", "-show_format", "-show_streams", filePath).CombinedOutput()
if err != nil {
serveErrorPage(writer, handleError(1, "Error getting video info: "+err.Error()))
return
}
if outputBytes != nil {
outputStringArr := strings.Split(string(outputBytes), "\n")
for _, line := range outputStringArr {
lineArr := strings.Split(line, "=")
if len(lineArr) < 2 {
continue
}
value, _ := strconv.Atoi(lineArr[1])
switch lineArr[0] {
case "width":
post.ImageW = value
case "height":
post.ImageH = value
case "size":
post.Filesize = value
}
}
if post.ParentID == 0 {
post.ThumbW, post.ThumbH = getThumbnailSize(post.ImageW, post.ImageH, "op")
} else {
post.ThumbW, post.ThumbH = getThumbnailSize(post.ImageW, post.ImageH, "reply")
}
}
} else {
// Attempt to load uploaded file with imaging library
img, err := imaging.Open(filePath)
if err != nil {
os.Remove(filePath)
handleError(1, "Couldn't open uploaded file \""+post.Filename+"\""+err.Error())
serveErrorPage(writer, "Upload filetype not supported")
return
} else {
// Get image filesize
stat, err := os.Stat(filePath)
if err != nil {
serveErrorPage(writer, handleError(1, "Couldn't get image filesize: "+err.Error()))
return
} else {
post.Filesize = int(stat.Size())
}
// Get image width and height, as well as thumbnail width and height
post.ImageW = img.Bounds().Max.X
post.ImageH = img.Bounds().Max.Y
if post.ParentID == 0 {
post.ThumbW, post.ThumbH = getThumbnailSize(post.ImageW, post.ImageH, "op")
} else {
post.ThumbW, post.ThumbH = getThumbnailSize(post.ImageW, post.ImageH, "reply")
}
accessLog.Print("Receiving post with image: " + handler.Filename + " from " + request.RemoteAddr + ", referrer: " + request.Referer())
if request.FormValue("spoiler") == "on" {
// If spoiler is enabled, symlink thumbnail to spoiler image
if _, err := os.Stat(path.Join(config.DocumentRoot, "spoiler.png")); err != nil {
serveErrorPage(writer, "missing /spoiler.png")
return
} else {
err = syscall.Symlink(path.Join(config.DocumentRoot, "spoiler.png"), thumbPath)
if err != nil {
serveErrorPage(writer, err.Error())
return
}
}
} else if config.ThumbWidth >= post.ImageW && config.ThumbHeight >= post.ImageH {
// If image fits in thumbnail size, symlink thumbnail to original
post.ThumbW = img.Bounds().Max.X
post.ThumbH = img.Bounds().Max.Y
if err := syscall.Symlink(filePath, thumbPath); err != nil {
serveErrorPage(writer, err.Error())
return
}
} else {
var thumbnail image.Image
var catalogThumbnail image.Image
if post.ParentID == 0 {
// If this is a new thread, generate thumbnail and catalog thumbnail
thumbnail = createImageThumbnail(img, "op")
catalogThumbnail = createImageThumbnail(img, "catalog")
if err = imaging.Save(catalogThumbnail, catalogThumbPath); err != nil {
serveErrorPage(writer, handleError(1, "Couldn't generate catalog thumbnail: "+err.Error()))
return
}
} else {
thumbnail = createImageThumbnail(img, "reply")
}
if err = imaging.Save(thumbnail, thumbPath); err != nil {
serveErrorPage(writer, handleError(1, "Couldn't save thumbnail: "+err.Error()))
return
}
}
}
}
}
}
if strings.TrimSpace(post.MessageText) == "" && post.Filename == "" {
serveErrorPage(writer, "Post must contain a message if no image is uploaded.")
return
}
postDelay := sinceLastPost(&post)
if postDelay > -1 {
if post.ParentID == 0 && postDelay < config.NewThreadDelay {
serveErrorPage(writer, "Please wait before making a new thread.")
return
} else if post.ParentID > 0 && postDelay < config.ReplyDelay {
serveErrorPage(writer, "Please wait before making a reply.")
return
}
}
banStatus, err := getBannedStatus(request)
if err != nil && err != sql.ErrNoRows {
handleError(1, "Error in getBannedStatus: "+err.Error())
serveErrorPage(writer, err.Error())
return
}
boards, _ := getBoardArr(nil, "")
if isBanned(banStatus, boards[post.BoardID-1].Dir) {
var banpageBuffer bytes.Buffer
banpageBuffer.Write([]byte(""))
if err = banpage_tmpl.Execute(&banpageBuffer, map[string]interface{}{
"config": config, "ban": banStatus, "banBoards": boards[post.BoardID-1].Dir,
}); err != nil {
fmt.Fprintf(writer, handleError(1, err.Error()))
return
}
fmt.Fprintf(writer, banpageBuffer.String())
return
}
post.Sanitize()
result, err := insertPost(post, emailCommand != "sage")
if err != nil {
serveErrorPage(writer, handleError(1, err.Error()))
return
}
postid, _ := result.LastInsertId()
post.ID = int(postid)
// rebuild the board page
buildBoards(false, post.BoardID)
buildFrontPage()
if emailCommand == "noko" {
if post.ParentID == 0 {
http.Redirect(writer, request, config.SiteWebfolder+boards[post.BoardID-1].Dir+"/res/"+strconv.Itoa(post.ID)+".html", http.StatusFound)
} else {
http.Redirect(writer, request, config.SiteWebfolder+boards[post.BoardID-1].Dir+"/res/"+strconv.Itoa(post.ParentID)+".html#"+strconv.Itoa(post.ID), http.StatusFound)
}
} else {
http.Redirect(writer, request, config.SiteWebfolder+boards[post.BoardID-1].Dir+"/", http.StatusFound)
}
benchmarkTimer("makePost", startTime, false)
}
func formatMessage(message string) string {
message = bbcompiler.Compile(message)
// prepare each line to be formatted
postLines := strings.Split(message, "<br>")
for i, line := range postLines {
trimmedLine := strings.TrimSpace(line)
lineWords := strings.Split(trimmedLine, " ")
isGreentext := false // if true, append </span> to end of line
for w, word := range lineWords {
if strings.LastIndex(word, gt+gt) == 0 {
//word is a backlink
if _, err := strconv.Atoi(word[8:]); err == nil {
// the link is in fact, a valid int
var boardDir string
var linkParent int
if err = queryRowSQL("SELECT `dir`,`parentid` FROM "+config.DBprefix+"posts,"+config.DBprefix+"boards WHERE "+config.DBprefix+"posts.id = ?",
[]interface{}{word[8:]},
[]interface{}{&boardDir, &linkParent},
); err != nil {
handleError(1, customError(err))
}
// get post board dir
if boardDir == "" {
lineWords[w] = "<a href=\"javascript:;\"><strike>" + word + "</strike></a>"
} else if linkParent == 0 {
lineWords[w] = "<a href=\"" + config.SiteWebfolder + boardDir + "/res/" + word[8:] + ".html\" class=\"postref\">" + word + "</a>"
} else {
lineWords[w] = "<a href=\"" + config.SiteWebfolder + boardDir + "/res/" + strconv.Itoa(linkParent) + ".html#" + word[8:] + "\" class=\"postref\">" + word + "</a>"
}
}
} else if strings.Index(word, gt) == 0 && w == 0 {
// word is at the beginning of a line, and is greentext
isGreentext = true
lineWords[w] = "<span class=\"greentext\">" + word
}
}
line = strings.Join(lineWords, " ")
if isGreentext {
line += "</span>"
}
postLines[i] = line
}
return strings.Join(postLines, "<br />")
}
func bannedForever(ban BanlistTable) bool {
return ban.Permaban && !ban.CanAppeal && ban.Type == 3 && ban.Boards == ""
}
func banHandler(writer http.ResponseWriter, request *http.Request) {
appealMsg := request.FormValue("appealmsg")
banStatus, err := getBannedStatus(request)
if appealMsg != "" {
if bannedForever(banStatus) {
fmt.Fprint(writer, "No.")
return
}
escapedMsg := html.EscapeString(appealMsg)
if _, err = execSQL("INSERT INTO `"+config.DBprefix+"appeals` (`ban`,`message`) VALUES(?,?)",
banStatus.ID, escapedMsg,
); err != nil {
serveErrorPage(writer, err.Error())
}
fmt.Fprint(writer,
"Appeal sent. It will (hopefully) be read by a staff member. check "+config.SiteWebfolder+"banned occasionally for a response",
)
return
}
if err != nil && err != sql.ErrNoRows {
handleError(1, "Error in getBannedStatus: "+err.Error())
serveErrorPage(writer, err.Error())
return
}
var banpageBuffer bytes.Buffer
banpageBuffer.Write([]byte(""))
if err = banpage_tmpl.Execute(&banpageBuffer, map[string]interface{}{
"config": config, "ban": banStatus, "banBoards": banStatus.Boards, "post": PostTable{},
}); err != nil {
fmt.Fprintf(writer, handleError(1, err.Error())+"\n</body>\n</html>")
return
}
fmt.Fprintf(writer, banpageBuffer.String())
}