summaryrefslogtreecommitdiff
path: root/hw4/cgi-bin/post.py
blob: 2a0bf8c84ac35dbcf197945422565affeb590a03 (plain) (blame)
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
#!/usr/bin/env python2.7
import Cookie, os, time
import re
import uuid
import cgi
import cgitb

from output import *

cgitb.enable() # allows for debugging errors from the cgi scripts in the browser

cookie = Cookie.SimpleCookie() # for writing cookies
form = cgi.FieldStorage() # for reading POST data

message = form.getvalue('message')

user = Login()
if not user:
    ShowError()

if message == None: # to prevent posting empty messages
    RedirectToBoard()

# csrf protection -- check for csrfToken
csrfToken = form.getvalue('csrfToken')

if csrfToken is None:
    ShowError()

with open('sessions', 'r') as s_file:
    for line in s_file:
        if user in line and not csrfToken in line:
            ShowError()

message = message.replace('\n','\n')
with open('messages','a') as m:
    m.write(user + ': ' + message + '\n')

RedirectToBoard()