-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
279 lines (212 loc) · 9.81 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
from flask import Flask, flash, request, redirect, render_template, session, url_for
from werkzeug.utils import secure_filename
from smtplib import SMTP
import os, json, base64, time, smtplib, requests, string, random
if not os.path.exists('data'):
os.makedirs('data')
app = Flask(__name__)
app.secret_key = os.urandom(24)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
path = os.getcwd()
UPLOAD_FOLDER = os.path.join(path, 'static/uploads')
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
if 'username' in session:
return render_template('index.html', value=session['username'], url='/userinfo', color='success', text='Hi, {}'.format(session['username']))
return render_template('index.html', value='Sign In', url='/signin', color='danger', text="You're not sign in.")
@app.route('/signin')
def signin():
flash('')
return render_template('signin.html')
@app.route('/signin', methods=['POST'])
def signin_post():
name = request.form['name']
passwd = request.form['passwd']
passwd_bytes = passwd.encode('utf-8')
base64_bytes = base64.b64encode(passwd_bytes)
base64_passwd = base64_bytes.decode('utf-8')
# os check file
if os.path.exists('data/{}_data.json'.format(name)):
with open('data/{}_data.json'.format(name), 'r') as f:
i = json.load(f)
elif not os.path.exists('data/{}_data.json'.format(name)):
flash('User Not Found')
return render_template('signin.html')
if i["username"] == name and i["passwd"] == base64_passwd:
session['username'] = i["username"]
return "<script>window.location.href = '/';</script>"
else:
flash('Invalid username or password')
return render_template('signin.html')
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/signup', methods=['POST'])
def signup_post():
name = request.form['name']
passwd = request.form['passwd']
email = request.form['email']
passwd_bytes = passwd.encode('utf-8')
base64_bytes = base64.b64encode(passwd_bytes)
base64_passwd = base64_bytes.decode('utf-8')
# os search file
if os.path.exists('data/{}_data.json'.format(name)):
return 'user already exists <a href="/signup">Sign Up</a>'
elif not os.path.exists('data/{}_data.json'.format(name)):
with open('data/{}_data.json'.format(name), 'w') as outfile:
json.dump({'username': name, 'passwd': base64_passwd,
'email': email, 'img': 'uploads/{}.jpg'.format(name)}, outfile)
return "<script>window.location.href = '/signin';</script>"
@app.route('/signout')
def logout():
# remove the username from the session if it's there
session.pop('username', None)
return "<script>window.location.href = '/';</script>" # redirect to home page
@app.route('/changepasswd')
def changepasswd():
if 'username' in session:
return render_template('changepasswd.html', error='')
return "You're not logged in"
@app.route('/changepasswd', methods=['POST'])
def changepasswd_post():
name = session['username']
new_passwd = request.form['newpasswd']
passwd = request.form['passwd']
passwd_bytes = passwd.encode('utf-8')
base64_bytes = base64.b64encode(passwd_bytes)
base64_passwd = base64_bytes.decode('utf-8')
# import json file
with open('data/{}_data.json'.format(name), 'r') as f:
i = json.load(f)
if base64_passwd == i["passwd"]:
new_passwd_bytes = new_passwd.encode('utf-8')
new_base64_bytes = base64.b64encode(new_passwd_bytes)
new_base64_passwd = new_base64_bytes.decode('utf-8')
with open('data/{}_data.json'.format(name), 'w') as outfile:
json.dump({'username': name, 'passwd': new_base64_passwd,
'email': i['email'], 'img': 'uploads/{}.jpg'.format(name)}, outfile)
return "<script>window.location.href = '/userinfo';</script>"
else:
return render_template('changepasswd.html', error='Wrong Password')
@app.route('/userinfo/')
def userinfo():
name = session['username']
if 'username' in session:
if os.path.exists('data/{}_data.json'.format(name)):
with open('data/{}_data.json'.format(name), 'r') as f:
i = json.load(f)
elif not os.path.exists('data/{}_data.json'.format(name)):
return 'user not found'
return render_template('userinfo.html', name=name, img=i["img"], email=i["email"])
return "You're not logged in"
@app.route('/userinfo/upload/')
def upload_form():
name = session['username']
if 'username' in session:
return render_template('upload.html', name=name)
return "You're not logged in"
@app.route('/userinfo/upload/', methods=['POST'])
def upload_file():
name = session['username']
# check if the post request has the file part
file = request.files['file']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if os.path.exists('static/uploads/{}.jpg'.format(name)):
os.remove('static/uploads/{}.jpg'.format(name))
os.rename(os.path.join(app.config['UPLOAD_FOLDER'], filename), os.path.join(app.config['UPLOAD_FOLDER'], '{}.jpg'.format(name)))
return redirect('/userinfo/')
else:
return redirect(request.url)
@app.route('/delete')
def delete():
if 'username' in session:
name = session['username']
if os.path.exists('data/{}_data.json'.format(name)):
if os.path.exists('static/uploads/{}.jpg'.format(name)):
os.remove('static/uploads/{}.jpg'.format(name))
os.remove('data/{}_data.json'.format(name))
session.pop('username', None)
return "<script>window.location.href = '/';</script>"
return "You're not logged in"
#user RESTAPI
@app.route('/api/users/<name>')
def api(name):
if os.path.exists('data/{}_data.json'.format(name)):
with open('data/{}_data.json'.format(name), 'r') as f:
i = json.load(f)
return json.dumps({'username': i["username"], 'email': i["email"]})
@app.route('/forget')
def forget():
return render_template('forget.html', error='')
@app.route('/forget', methods=['POST'])
def forget_post():
name = request.form['name']
if os.path.exists('data/{}_data.json'.format(name)):
with open('data/{}_data.json'.format(name), 'r') as f:
i = json.load(f)
return render_template('confirm_forget.html', name=name, email=i["email"])
elif not os.path.exists('data/{}_data.json'.format(name)):
return render_template('forget.html', error="user not found")
@app.route('/forget/send/<name>')
def forget_send(name):
if os.path.exists('data/{}_data.json'.format(name)):
with open('data/{}_data.json'.format(name), 'r') as f:
i = json.load(f)
#open config.json file
with open('config.json', 'r') as f:
config = json.load(f)
passwd = base64.b64decode(i["passwd"])
passwd = passwd.decode('utf-8')
receiver = i["email"]
smtp = SMTP()
smtp.set_debuglevel(0)
smtp.connect(config['stmp_host'], int(config['stmp_port']))
smtp.login(config['stmp_auth'], config['stmp_pass'])
message_text = "FJSL Password Forgot \n\nHi! {}\nthis is a mail from Flask JSON Login\nYour password is {}\n".format(name,passwd)
msg = "From: FJSL <{}>\nTo: meck22772@gmail.com\nSubject: {}\n".format(config['stmp_sender'],message_text)
smtp.sendmail(config['stmp_sender'], receiver, msg)
smtp.quit()
return redirect('/')
@app.route('/config')
def config():
return render_template('config.html')
@app.route('/config', methods=['POST'])
def config_post():
host = request.form['host']
port = request.form['port']
sender = request.form['sender']
auth = request.form['auth']
passwd = request.form['passwd']
sender = request.form['sender']
with open('config.json', 'w') as f:
json.dump({"stmp_host": host, "stmp_port": port, "stmp_auth": auth, "stmp_pass": passwd, "stmp_sender": sender}, f)
return "<script>window.location.href = '/';</script>" # redirect to home page
#sign up RESTAPI
"""@app.route('/api/signup/<username>&<email>&<password>&<confirm_password>')
def api_signin(username, email, password, confirm_password):
if os.path.exists('data/{}_data.json'.format(username)):
return 'user already exists'
else:
password_bytes = password.encode('utf-8')
confirm_password_bytes = confirm_password.encode('utf-8')
base64_bytes = base64.b64encode(password_bytes)
base64_passwd = base64_bytes.decode('utf-8')
confirm_base64_bytes = base64.b64encode(confirm_password_bytes)
base64_confirm_passwd = base64_bytes.decode('utf-8')
if base64_confirm_passwd == base64_passwd:
with open('data/{}_data.json'.format(username), 'w') as outfile:
json.dump({'username': username, 'passwd': base64_passwd,
'email': email, 'img': 'uploads/{}.jpg'.format(username)}, outfile)
return 'user created'
"""