-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
365 lines (300 loc) · 10.3 KB
/
main.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
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
from flask import request, Response
from mailserver import SALT
from monster import render, Flask, escapeString
import sys, json
import hashlib, base64
import resend, secrets_parser
import litedb, re
import requests
from flask_compress import Compress
accounts = litedb.get_conn("accounts")
events = litedb.get_conn("events")
state = litedb.get_conn("state")
events_raw = open("data/events.json").read()
events_json = json.loads(events_raw)
if state.get("db_state") != hashlib.sha256(events_raw.encode()).hexdigest():
import migrate
state.set("db_state", hashlib.sha256(events_raw.encode()).hexdigest())
app = Flask(__name__)
app.config["COMPRESS_LEVEL"] = 9
app.config["COMPRESS_MIN_SIZE"] = 500
Compress(app)
events_order = list(events_json["events"].keys())
DB_IP = secrets_parser.parse("variables.txt")["DB_IP"]
def order_events(events):
sorted_events = [None] * len(events)
for event_id in events:
event_name = events[event_id]["name"]
sorted_events[events_order.index(event_name)] = [event_id, events[event_id]]
return dict(sorted_events)
def redirect(path):
resp = Response(f"""<script>window.location.href="{path}"</script>""")
resp.headers["Access-Control-Allow-Origin"] = "*"
return resp
def make_response(data, mimetype=None, status=None):
if type(data) in [str, int, float, bool, list, dict]:
data = json.dumps(data)
resp = Response(data, mimetype=mimetype, status=status)
resp.headers["Access-Control-Allow-Origin"] = "*"
return resp
resend.api_key = secrets_parser.parse("variables.txt")["RESEND"]
salt = secrets_parser.parse("variables.txt")["SALT"]
def send_mail(to, subject, html, reply_to="[email protected]"):
params = {
"from": "Exun Clan <[email protected]>",
"to": [to],
"subject": subject,
"html": html,
"reply_to": reply_to,
}
email = resend.Emails.send(params)
return email
daisyui = (
"<script>"
+ open("public/pako.js").read()
+ "</script>"
+ """<script>
function decompressGzippedString(base64String) {
try {
const binaryString = atob(base64String);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
const decompressedData = pako.inflate(bytes, { to: 'string' });
return decompressedData;
} catch (err) {
console.error('An error occurred while decompressing the string:', err);
return null;
}
}
"""
+ f"""
var daisycss="{open("public/daisyui.b64").read()}";
var style=document.createElement("style");
style.textContent=decompressGzippedString(daisycss);
document.head.appendChild(style);
</script>
"""
)
tailwind = (
"<script>eval(atob(`"
+ base64.b64encode(open("public/tailwind.js").read().encode()).decode()
+ "`))</script>"
)
def auth_token(a):
if type(a) == str:
a = a.encode() + salt.encode()
return hashlib.sha256(a).hexdigest()
def otp(a):
hex_dig = auth_token(a)
array = int(hex_dig[-6:], 16) % (10**6)
otp_str = f"{array:06d}"
return [int(digit) for digit in otp_str]
def authd():
try:
if "auth_token" not in request.cookies or "email" not in request.cookies:
return False
if request.cookies["auth_token"] == auth_token(request.cookies["email"]):
return True
return False
except:
return False
def account_details():
account = accounts.get(request.cookies["email"])
return account
@app.get("/")
def home():
return render("index", locals() | globals())
@app.get("/login")
def login():
if not authd():
return render("login/login", locals() | globals())
account = account_details()
if account == None:
return redirect("/complete_signup")
return render("login/login", locals() | globals())
@app.get("/email")
def email_send():
try:
args = dict(request.args)
key = "".join([str(x) for x in otp(args["email"])])
requests.get(
"http://"
+ DB_IP
+ ":5555/mail?salt="
+ SALT
+ "&to="
+ escapeString(args["email"])
+ "&subject="
+ escapeString("Exun Registration Authentication OTP - " + key)
+ "&key="
+ key
)
except Exception as e:
return make_response(False)
return make_response(True)
@app.get("/submit_otp")
def submit_otp():
args = dict(request.args)
if "".join([str(x) for x in otp(args["email"])]) == args["otp"]:
resp = make_response(True)
resp.set_cookie("email", args["email"])
resp.set_cookie("auth_token", auth_token(args["email"]))
return resp
else:
resp = make_response(False)
resp.delete_cookie("email")
resp.delete_cookie("auth_cookie")
return resp
@app.get("/events")
def events_page():
if authd() and account_details() == None:
return redirect("/complete_signup")
return render("events/events_page", locals() | globals())
@app.get("/complete_signup")
def complete_signup():
if not authd():
return redirect("/login")
account = account_details()
if account != None:
return redirect("/")
return render("signup/complete", locals() | globals())
@app.get("/api/complete_signup")
def api_for_completing_signup():
if not authd():
return make_response(False)
account = account_details()
if account != None:
return make_response(False)
args = dict(request.args)
email = request.cookies["email"]
fullname = args["fullname"].strip().upper()
phone_number = args["phone_number"].strip()
principals_email = args["principals_email"].strip()
individual = args["individual"].strip()
for x in [fullname, phone_number, principals_email, individual]:
if x == "":
return make_response(False)
out = {
"name": fullname,
"phone_number": phone_number,
"principals_email": principals_email,
"registrations": {},
"email": email,
"individual": individual,
}
for x in ["institution_name", "address", "principals_name"]:
if args[x].strip() == "":
return make_response(False)
out[x] = args[x].upper()
accounts.set(email, out)
return make_response(True)
def name_hash(x):
return hashlib.sha256(
(x.replace(" ", "").replace("\t", "").lower()).encode()
).hexdigest()
@app.get("/event")
def event_page():
if authd() and account_details() == None:
return redirect("/complete_signup")
if authd():
account = account_details()
else:
account = {"registrations": {}}
args = dict(request.args)
try:
id = args["id"]
except:
return redirect("/events")
event = events.get(id)
ignore = []
participant_details = {}
if account != None:
for x in account["registrations"]:
for y in account["registrations"][x]:
if y["email"] in participant_details and name_hash(
participant_details[y["email"]]["name"]
) != name_hash(y["name"]):
ignore.append(y["email"])
del participant_details[y["email"]]
continue
if y["email"] not in ignore:
participant_details[y["email"]] = y
if event == None:
return redirect("/events")
return render("events/event", locals() | globals())
@app.get("/admin")
def admin():
if not authd():
return redirect("/login")
account = account_details()
if account == None:
return redirect("/complete_signup")
if request.cookies["email"] == "[email protected]":
return render("admin", locals() | globals())
else:
return redirect("/")
def is_valid_email(email):
pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
if re.match(pattern, email):
return True
return False
@app.post("/submit_registrations")
def submit_registrations():
data = request.get_json()
if not authd():
return redirect("/login")
account = account_details()
if account == None:
return redirect("/complete_signup")
event = events.get(data["id"])
if (
not event["independant_registration"]
and account != None
and "individual" in account
and account["individual"] == "true"
):
return make_response(False)
for x in data["data"]:
x["name"] = x["name"].upper()
if not is_valid_email(x["email"]):
return make_response(False)
x["class"] = int(x["class"])
if not (
event["eligibility"][1] >= x["class"]
and x["class"] >= event["eligibility"][0]
):
return make_response(False)
if len(str(x["phone"])) != 10:
return make_response(False)
account["registrations"][data["id"]] = data["data"]
accounts.set(request.cookies["email"], account)
return make_response(True)
@app.get("/brochure")
def brochure():
return render("brochure", locals() | globals())
@app.get("/invite")
def invite_redirect():
return redirect("/brochure")
@app.get("/summary")
def summary():
if not authd():
return redirect("/login")
account = account_details()
if account == None:
return redirect("/complete_signup")
registrations = account["registrations"]
return render("summary/summary", locals() | globals())
# Temperory Backlinks
@app.get("/24/schedule")
def schedule_24():
return redirect(
"https://docs.google.com/document/d/1h_sWUuvQlLoiwf4R7kGO7_VQi-mnqlbGrBu6wyAGEhg/edit?usp=sharing"
)
@app.get("/24/events")
def events_24():
return redirect(
"https://docs.google.com/document/d/1eLEZRlnXg388FWOBR1Qz2Rj7tpttf4jPmO4SucHPp4E/edit?usp=sharing"
)