-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.py
353 lines (321 loc) · 12.5 KB
/
life.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
#!/usr/bin/env python2
from bottle import route, run, template, static_file, request, response
from bottle import redirect
import sqlite3
import smtplib
from email.mime.text import MIMEText
import hashlib
from random import randint
import sys
import pdb
import bottle_session
import bottle
def send_cryptmsg(userhandle, msg_type):
""" This send_cryptmsg functions sends user email verification during the
new user registration and sends a link to their when password reset is
required."""
smtpserver = smtplib.SMTP('localhost', 1025)
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
randomSequence = ''
for i in range(16):
randomSequence = randomSequence + str(randint(0,9))
if msg_type == 0: #email_verification
cur.execute('select email from tempusers where handle=?',(userhandle,))
email_id = cur.fetchone()[0]
hashmsg = userhandle + email_id + randomSequence
cryptcode = hashlib.sha256(hashmsg).hexdigest()
content = ''' You were received this mail because you are trying to register in our lifewebapp. By clicking the link below
<a> http://localhost:8080/confirmmail?userid={0}&cryptcode={1}</a>.
This is an auto created email.So de not replay'''.format(userhandle, cryptcode)
msg = MIMEText(content)
msg['Subject'] = 'Email verification - Lifewebapp'
msg['From'] = 'Life Web App Admin'
msg['To'] = email_id
cur.execute('insert into mail_auth_codes values(?, ?, ?)',(userhandle,
cryptcode, msg_type));
conn.commit()
smtpserver.sendmail('LifeWebAppAdmin', [email_id], msg.as_string())
elif msg_type == 1: # password_reset
cur.execute('select email from users where handle=?',(userhandle,))
email_id = cur.fetchone()[0]
hashmsg = userhandle + email_id + randomSequence
cryptcode = hashlib.sha256(hashmsg).hexdigest()
content = ''' You were received this mail because you were asked to
reset your password. By clicking the link below
<a> http://localhost:8080/changepwd?userid={0}&cryptcode={1}</a>.
This is an auto created email.So de not replay'''.format(userhandle, cryptcode)
msg = MIMEText(content)
msg['Subject'] = 'Password reset- Lifewebapp'
msg['From'] = 'Life Web App Admin'
msg['To'] = email_id
cur.execute('insert into mail_auth_codes values(?, ?, ?)',(userhandle,
cryptcode, msg_type));
conn.commit()
smtpserver.sendmail('LifeWebAppAdmin', [email_id], msg.as_string())
else:
print("Invalid msg_type is given in send_cryptmsg() function")
conn.close()
smtpserver.quit()
@route('/')
@route('/home')
def home_page():
return template('home',name="Aravindhan")
@route('/login')
def login_page():
#pdb.set_trace()
user_name = request.get_cookie("username", secret='MySecret')
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
if user_name is not None and user_name is not '':
cur.execute('select name from users where handle=?',(user_name, ))
print(cur.fetchone())
redirect('/app')
else:
return template('login',msg="")
@route('/logout')
def logout_page():
user_name = response.set_cookie("username",'',secret='MySecret')
redirect('/home')
@route('/login', method='POST')
def login_authentication():
errMsg = '<p color="red">entered username or password is incorrect.</p>'
username = request.forms.get('username')
password = request.forms.get('password')
#print(username)
#print(password)
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('select name from users where handle=? and password=?',(username,password))
#pdb.set_trace()
try:
name = cur.fetchone()[0]
print("There exists the username and password")
except Exception as e:
return template('login',msg=errMsg)
pass
user_name = response.set_cookie("username", username, secret='MySecret')
redirect('/app')
conn.close()
@route('/newuser')
def newuser():
return template('new_user')
@route('/newuser',method='POST')
def addentrytodb():
pdb.set_trace()
name = request.forms.get('name')
handle = request.forms.get('userid')
email = request.forms.get('email')
password = request.forms.get('passwd')
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
#checking on user tables preregistered user
cur.execute('select * from users where handle=? OR email=?',(handle, email))
if cur.fetchone():
return template(
'the userhandle {{uhandle}} and email {{eid}} already exists',
uhandle = handle, eid = email)
#checking ont temp_users table for non-confirmed user
cur.execute('select * from tempusers where handle=? OR email=?',(handle, email))
if cur.fetchone():
return template('''You have already tried to register confirm email accout. To resend the mail click here.<a> localhost/resendmail?={{eid}}</a>''',eid=email)
#inserting users data to tempusers
cur.execute('insert into tempusers values(?, ?, ?, ?)',
(handle, name, password, email))
conn.commit()
conn.close()
send_cryptmsg(handle, 0)
return '<b> A mail has been sent to your mail id to confirm registration </b>'
@route('/resetpwd')
def passwdresetpage():
return template('reset_password')
@route('/resetpwd', method='POST')
def sendresetlink():
email_id = request.forms.get('email_id')
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('select handle from users where email=?',(email_id,))
try:
userhandle = cur.fetchone()[0]
send_cryptmsg(userhandle, 1)
except:
raise
return '<b> The entered email addres not found</b>'
return '<b> A reset link has been sent to your email</b>'
@route('/changepwd')
def newpasswordform():
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
#checking the crypt codes
userhandle = request.query.userid
cryptcode = request.query.cryptcode
cur.execute('select * from mail_auth_codes where handle =? and cryptcode=? and type=1' ,(userhandle, cryptcode))
if cur.fetchone():
return template('new_password',handle=userhandle, cc=cryptcode)
conn.commit()
conn.close()
else:
return '<b> An invalid request has been received</b>'
conn.commit()
conn.close()
@route('/changepwd', method='POST')
def changepassword():
userhandle = request.forms.get('userhandle')
cryptcode = request.forms.get('cryptcode')
password = request.forms.get('new_pwd')
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('update users set password=? where handle=?',(password,
userhandle))
cur.execute('delete from mail_auth_codes where handle =? and cryptcode=? and type=1', (userhandle, cryptcode))
conn.commit()
conn.close()
return '<b> Your password has succesfully updated</b>'
@route('/confirmmail')
def confirmemailaddress():
cryptcode = request.query.cryptcode
userhandle = request.query.userid
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('select * from mail_auth_codes where handle =? and cryptcode=? and type=0' ,(userhandle, cryptcode))
if cur.fetchone():
cur.execute('select * from tempusers where handle=?', (userhandle,))
cur.execute('insert into users values(?, ?, ?, ?)', cur.fetchone())
cur.execute('delete from tempusers where handle=?', (userhandle,))
cur.execute('delete from mail_auth_codes where handle =? and cryptcode=? and type=0' ,(userhandle, cryptcode))
conn.commit()
conn.close()
return '<b> Your email id has successfully authenticated</b>'
else:
return '<b> An illegal request has been received </b>'
@route('/demo-lifes')
def demo_lifes_page():
req_item = request.query.req_item
if req_item == 'list':
return template('demo_life_menu.tpl')
else:
jsonLife = ""
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('select world from lifes where id = ? and type=0',(req_item,))
jsonLife = (cur.fetchone()[0]).encode('ascii', 'ignore')
conn.commit()
conn.close()
return jsonLife
@route('/getlifes')
def getlifes():
try:
#pdb.set_trace()
user_name = request.get_cookie("username", secret='MySecret')
print(user_name)
req_item = request.query.req_item
is_list = request.query.is_list
if is_list == 'true':
template_string = ''
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('select id from lifes where author=?',(user_name,))
print('This comes here and user_name =' + user_name)
for life in cur:
l = life[0].encode('ascii', 'ignore')
template_string = template_string + '<li class="pure-menu-item"><a class="pure-menu-link open-life-link">' + l + '</a></li>'
template_string = template_string + '\n'
return template(template_string)
else:
jsonLife = ""
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('select world from lifes where id = ? and author =?',(req_item,user_name))
jsonLife = (cur.fetchone()[0]).encode('ascii', 'ignore')
conn.commit()
conn.close()
return jsonLife
except Exception as e:
print(str(e))
@route('/getgallerylifes')
def getgallerylifes():
user_name = request.get_cookie("username", secret='MySecret')
if user_name != None:
req_item = request.query.req_item
is_list = request.query.is_list
if is_list == 'true':
return template('gallery_life_menu.tpl')
else:
jsonLife = ""
parsed = req_item.split(' by ')
life_id = parsed[0]
author = parsed[1]
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
cur.execute('select world from lifes where id = ? and author= ? and type=2',(life_id,author))
jsonLife = (cur.fetchone()[0]).encode('ascii', 'ignore')
conn.commit()
conn.close()
return jsonLife
@route('/savelife')
def savelife():
print("This part works")
user_name = request.get_cookie("username", secret='MySecret')
lifename =request.query.lifename
visibility = request.query.visibility
is_update = request.query.is_update
print(is_update)
_type = 1
if visibility == 'private':
_type = 1
elif visibility == 'public':
_type = 2
world = request.query.cells
print(lifename, visibility, world)
conn = sqlite3.connect('./db/life.db')
cur = conn.cursor()
#pdb.set_trace()
if is_update == 'false':
try:
cur.execute('select likes_count from lifes where id = ? and author=?',(lifename,user_name))
#print(cur.fetchone())
if cur.fetchone():
return "EXISTS"
else:
cur.execute('insert into lifes values(?,?,?,?,?)',(user_name, world, lifename, _type, 0))
conn.commit()
return "OK"
except Exception as e:
print(e)
return "SAVE FAILED"
elif is_update == 'true':
cur.execute('select likes_count from lifes where id = ? and author=?',(lifename,user_name))
likecount = cur.fetchone()[0]
try:
cur.execute('update lifes set world = ? where id = ? and author=?',(world,lifename,user_name))
conn.commit()
return "OK"
except Exception as e:
cur.execute('insert into lifes values(?,?,?,?,?)',(user_name, world, lifename, _type, 0))
conn.commit()
print(e)
return "SAVE FAILED"
conn.close()
@route('/about')
def aboutpage():
return template('about')
@route('/demo')
def demo_page():
return template('demo')
@route('/app')
def app_page():
return template('app')
@route('/images/<name>')
def image(name):
return static_file(name, root='./images/')
@route('/css/<name>')
def css(name):
return static_file(name, root='./css/')
@route('/js/<name>')
def js(name):
return static_file(name, root='./js/')
@route('/fonts/<name>')
def font(name):
return static_file(name, root='./fonts')
if __name__ == '__main__':
run(host='localhost', port =8000, debug=True, reloader=True)