-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
145 lines (133 loc) · 4.27 KB
/
script.py
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
from bottle import route, run, template, request
import bottle as b
import api
# import logging
# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# logging.debug('This is a log message.')
# a = api.Connector()
# l = api.Logger()
# u = api.Usermethods()
# p = api.Package()
def setLanguage(req):
"""
Set language depending on (in order of importance, most->least important):
* recent choice to change/set language
* previous choice to set language (read from cookie)
* HTTP headers (accept-language)
"""
currentLang = ""
if req.query.currentLang.strip() != "":
currentLang = str(req.query.currentLang.strip())
b.response.set_cookie('currentLang', currentLang)
elif req.get_cookie('currentLang') != "":
currentLang = req.get_cookie('currentLang')
else:
if req.headers.get('Accept-Language')[:2] == 'en':
currentLang = 'en'
else:
currentLang = 'de'
return currentLang
def cookieUserName(req):
l = api.Logger()
logOnCookie = req.get_cookie('logged_on', secret=l.skey)
if logOnCookie:
logTuple = l.checkExists(logOnCookie)
if logTuple:
return logTuple[0]
else:
return ""
else:
return ""
@b.route("/", method=['GET', 'POST'])
def index():
a = api.Connector()
l = api.Logger()
u = api.Usermethods()
p = api.Package(currentLang=setLanguage(b.request))
logOnCookie = b.request.get_cookie('logged_on', secret=l.skey)
if logOnCookie:
# A cookie exists
logTuple = l.checkExists(logOnCookie) # TODO change this to two variables
if logTuple:
# Cookie has been found in array
if b.request.query.get('logout'):
# Log out
l.removeEntry(logOnCookie)
b.response.delete_cookie('logged_on')
return template('index', p=p)
elif b.time.time() - logTuple[1] < l.ttl:
# Successful entry with old cookie
p.uName = logTuple[0]
p.loggedOn = True
if b.request.forms.get('changePassword'):
if b.request.forms.get('newPassword') == b.request.forms.get('newPasswordConfirm'):
if u.checkCredentials(logTuple[0], b.request.forms.get('oldPassword')):
u.changePassword(logTuple[0], b.request.forms.get('newPassword'))
p.passwordChanged = True
else:
p.passwordChangeError = True
else:
p.passwordMismatch = True
return template('index', p=p)
else:
# Fail because cookie has timed out
l.removeEntry(logOnCookie)
b.response.delete_cookie('logged_on')
p.cookieTimeout = True
return template('index', p=p)
else:
# Cookie has not been found: malicious or cookie timed out
p.cookieTimeout = True
b.response.delete_cookie('logged_on')
return template('index', p=p)
elif b.request.forms.get('username') and b.request.forms.get('password'):
# An attempt at logging on has been made
if u.checkCredentials(b.request.forms.get('username'), b.request.forms.get('password')):
# Successfully logged on
p.uName = b.request.forms.username
p.loggedOn = True
hashedCookieValue = l.setCookieForLogon(p.uName)
b.response.set_cookie('logged_on', hashedCookieValue, secret=l.skey, maxage=l.ttl)
return template('index', p=p)
else:
# Fail at logging on
p.loginFail = True
return template('index', p=p)
else:
# No attempt at logging on, no cookie, serve default site
return template('index', p=p)
@route('/xapi', method=['GET', 'POST'])
def xapi():
a = api.Connector()
apiName = b.request.GET.apiName.strip()
apiInput = b.request.GET.apiInput.strip()
apiProg = b.request.POST.apiProg
if apiName != "" and apiInput != "":
if apiInput == "0":
# Return progress
return a.getProgress(apiName)
elif apiInput == "1":
# Update progress with input
a.dumpProgress(apiName, apiProg)
return apiProg
else:
return 'bad parameter'
else:
return 'APIc Fail'
@route('/jstest')
def jstest():
return template('./game/k.tpl', uName=b.request.query.get('col'))
@route('/static/<filename>')
def server_static(filename):
return b.static_file(filename, root='./static')
@route('/game/<filename>')
def server_game(filename):
if 'tpl' in filename:
# logging.debug(cookieUserName(b.request))
b.time.sleep(0.1)
return template('./game/KameWeb0-3-2.tpl', uName=cookieUserName(b.request))
else:
return b.static_file(filename, root='./game')
if __name__ == '__main__':
run(host='localhost', reloader=True, debug=True)
app = b.default_app()