-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
358 lines (254 loc) · 12 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
from flask import Flask, render_template, request, redirect, url_for, session, abort, Response
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re
import json
import psutil
app = Flask(__name__)
# ====== SIMPLE APP CONFIG ======
app_port = 80
is_maintenance_mode = False
# ===============================
# ------- API CONFIG --------
app.secret_key = 'cn9m89tyy3b789vryn0v9gybhm98vyc097tng' # highly encourage you to change this.
app.config['MYSQL_HOST'] = '' # mysql hostname ex: localhost or 293.423.562.52
app.config['MYSQL_USER'] = '' # mysql login username
app.config['MYSQL_PASSWORD'] = '' # mysql login password
app.config['MYSQL_DB'] = '' # mysql database where its supposed be the tables you inserted before.
# Intialize MySQL
mysql = MySQL(app)
# ----------------------------
# ===== SERVER SCRIPTS INCLUDES
import server_scripts.database_lib as dbhandlerlib
import server_scripts.web_api
# ===== MAIN INCLUDE END
# =================== MAIN WEBSITE ROUTES ===================
# General checks
@app.before_request
def general_check():
loadtime = psutil.getloadavg()[0] + psutil.getloadavg()[1] + psutil.getloadavg()[2]
if loadtime > 3:
return render_template("Error/maintenance.html"), 503
if is_maintenance_mode:
return render_template("Error/maintenance.html"), 503
@app.route('/home')
def homepage():
if 'loggedin' in session:
return render_template("Home.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/users/<userid>/profile')
def userprofile(userid):
if 'loggedin' in session:
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE id = %s', (userid,))
# Fetch one record and return result
account = cursor.fetchone()
if account:
return render_template("User/profile.html", username=session['username'], userid=session['id'], profilename=account['username'], profileid=account['id'], profilefollowers=account['followers'], profilefollowing=account['following'], profilejoindate=account['created'])
else:
abort(404)
return redirect(url_for('login'))
@app.route('/catalog/<itemid>/')
def itempage(itemid):
if 'loggedin' in session:
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM items WHERE id = %s', (itemid,))
# Fetch one record and return result
item = cursor.fetchone()
if item:
return render_template("new/catalogitem.html", username=session['username'], userbalance=session['balance'], userid=session['id'], itemid=item['id'], itemname=item['itemname'], itemtype=item['type'], itemprice=item['price'], itemdesc=item['itemdesc'], thumburl="/static/funny_blob.png", itemfav=item['favs'])
else:
abort(404)
return redirect(url_for('login'))
@app.route('/catalog')
def catalog():
if 'loggedin' in session:
return render_template("new/catalog.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/users/friends')
def user_friends():
if 'loggedin' in session:
return render_template("User/friends.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/my/messages/')
def user_messages():
if 'loggedin' in session:
return render_template("User/messages.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/trades')
def user_trades():
if 'loggedin' in session:
return render_template("User/trades.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/discover')
def games():
if 'loggedin' in session:
return render_template("new/games.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/develop')
@app.route('/create')
def createpage():
if 'loggedin' in session:
return render_template("user/create.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/upgrades')
@app.route('/robux')
def robuxpage():
if 'loggedin' in session:
return render_template("market/robux.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/premium/membership')
def premiumpage():
if 'loggedin' in session:
return render_template("market/premium.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/my/avatar')
def avatar():
if 'loggedin' in session:
return render_template("User/avatar.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/transactions')
def moneypage():
if 'loggedin' in session:
return render_template("User/money.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
@app.route('/my/groups')
@app.route('/search/groups')
def groups():
if 'loggedin' in session:
return render_template("new/groups.html", username=session['username'], userid=session['id'])
return redirect(url_for('login'))
# =========================================================
# =================== AUTH ROUTES ===================
@app.route('/')
@app.route('/account/signupredir')
def singup():
if 'loggedin' in session:
return redirect(url_for('homepage'))
return render_template("new/signup.html")
@app.route('/api/v1/auth/v2/signup', methods=['POST'])
def signupapi():
if 'loggedin' in session:
return redirect(url_for('homepage'))
parsedata = json.loads(request.data)
try:
username = parsedata["username"]
password = parsedata["password"]
gender = parsedata["gender"]
birthday = parsedata["birthday"]
except:
return '{"errors":[{"code":1,"message":"Please fill out the form!"}]}'
username = str(parsedata["username"])
password = str(parsedata["password"])
gender = str(parsedata["gender"])
birthday = str(parsedata["birthday"])
birthday = birthday.replace(" ", "")
checkacc = dbhandlerlib.check_existing_account_by_name(username)
if checkacc:
return '{"errors":[{"code":1,"message":"Username is already in use!"}]}'
elif not re.match(r'[A-Za-z0-9]+', birthday):
return '{"errors":[{"code":1,"message":"Invalid birthday!"}]}'
elif not re.match(r'[A-Za-z0-9]+', username):
return '{"errors":[{"code":1,"message":"Invalid username!"}]}'
else:
dbhandlerlib.create_account(username, password, "Undefined", gender, birthday)
return '[]'
@app.route('/api/v1/auth/v2/login', methods=['POST'])
def loginapi():
if 'loggedin' in session:
return redirect(url_for('homepage'))
parsedata = json.loads(request.data)
try:
username = parsedata["cvalue"]
password = parsedata["password"]
except:
return '{"errors":[{"code":1,"message":"Please fill out the form!"}]}'
username = str(parsedata["cvalue"])
password = str(parsedata["password"])
checkacc = dbhandlerlib.check_account_login(username, password)
if not checkacc:
return Response('{"errors":[{"code":1,"message":"Incorrect username or password. Please try again.","userFacingMessage":"Something went wrong"}]}', status=403, mimetype='application/json')
else:
logindata = dbhandlerlib.get_account_login_data(username)
# Create session data, we can access this data in other routes
session['loggedin'] = True
session['balance'] = logindata[2]
session['id'] = logindata[1]
session['username'] = logindata[0]
return "[]"
@app.route('/login')
def loginpage():
if 'loggedin' in session:
return redirect(url_for('homepage'))
return render_template("new/login.html")
@app.route('/login/old', methods=['GET', 'POST'])
def login():
return redirect(url_for('loginpage'))
if 'loggedin' in session:
return redirect(url_for('homepage'))
# Output message if something goes wrong...
msg = ''
# Check if "username" and "password" POST requests exist (user submitted form)
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
# Create variables for easy access
username = request.form['username']
password = request.form['password']
# Check if account exists using MySQL
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = %s AND password = %s', (username, password,))
# Fetch one record and return result
account = cursor.fetchone()
# If account exists in accounts table in out database
if account:
# Create session data, we can access this data in other routes
session['loggedin'] = True
session['id'] = account['id']
session['username'] = account['username']
# Redirect to home page
return redirect(url_for('homepage'))
else:
# Account doesnt exist or username/password incorrect
msg = 'Incorrect username/password!'
# Show the login form with message (if any)
return render_template('hidden/login.html', msg=msg)
@app.route('/register/old', methods=['GET', 'POST'])
def oldregister():
return "depricated."
if 'loggedin' in session:
return redirect(url_for('homepage'))
# Output message if something goes wrong...
msg = ''
# Check if "username", "password" and "email" POST requests exist (user submitted form)
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
# Create variables for easy access
username = request.form['username']
password = request.form['password']
email = request.form['email']
# Check if account exists using MySQL
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = %s', (username,))
account = cursor.fetchone()
# If account exists show error and validation checks
if account:
msg = 'Account already exists!'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
msg = 'Invalid email address!'
elif not re.match(r'[A-Za-z0-9]+', username):
msg = 'Username must contain only characters and numbers!'
elif not username or not password or not email:
msg = 'Please fill out the form!'
else:
# Account doesnt exists and the form data is valid, now insert new account into accounts table
#cursor.execute('INSERT INTO accounts VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, NULL)', (username, password, email, 0, 15, "Yeah...", 0, 0))
#mysql.connection.commit()
dbhandlerlib.create_account(username, password, email)
msg = 'You have successfully registered!'
return redirect(url_for('login'))
elif request.method == 'POST':
# Form is empty... (no POST data)
msg = 'Please fill out the form!'
# Show registration form with message (if any)
return render_template('hidden/register.html', msg=msg)
# =================== =================== ===================
if __name__ == '__main__':
app.run(host='0.0.0.0', port=app_port)