-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
209 lines (176 loc) · 7.09 KB
/
Copy pathapp.py
File metadata and controls
209 lines (176 loc) · 7.09 KB
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
from flask import Flask #flask
from flask import render_template, request, redirect, make_response, Response, flash ##flask functions
import sqlite3 as sql ##for database
import smtplib ##for sending verification mail
from cryptohash import sha1 ##for hashing the password
app = Flask(__name__)
@app.route("/")
def root_dir():
return render_template("register.html")
@app.route("/register")
def register():
return render_template('register.html')
@app.route("/register", methods=['GET', 'POST'])
def register_db():
if request.method == 'POST':
name = request.form['name']
username = request.form['username']
password_unhashed = request.form['password']
password = sha1(password_unhashed) ##password to sha1
con = sql.connect("database.db")
cur = con.cursor()
try:
cur.execute("INSERT INTO users (username,password,name,verified) VALUES (?,?,?,?)", (username,password,name,0))
con.commit()
con.close()
except sql.IntegrityError:
return render_template("/registered.html")
vlink = "http://127.0.0.1:5000/verify/mail=" + username + "&_hash=" + sha1(username)
sendmail(username, vlink)
return render_template("reg_success.html")
@app.route("/login", methods=['GET', 'POST'])
def login_check():
if request.method == 'POST':
username = request.form['username']
password_real = request.form['password']
password_hashed = sha1(password_real)
conn = sql.connect("database.db")
creds = conn.execute("SELECT * FROM users WHERE username = '" + username + "'").fetchall()
conn.close()
try:
if creds[0][3] == 0:
return render_template("notverified.html")
else:
pass
if password_hashed == creds[0][1]:
resp = make_response(render_template("welcome.html"))
resp.set_cookie('dont_try', str(sha1(username)))
resp.set_cookie('email', username)
return resp
else:
return render_template("loginfailed.html")
except IndexError:
return "you are not registered"
@app.route("/verify/mail=<mail>&_hash=<_hash>")
def verify_mai(mail, _hash):
print(mail)
conn = sql.connect("database.db")
creds = conn.execute("SELECT * FROM users WHERE username = '" + mail + "'").fetchall()
db_mail = creds[0][0]
name = creds[0][2]
verified_status = creds[0][3]
if verified_status == 0:
if (sha1(mail) == _hash and mail == db_mail):
conn = sql.connect("database.db")
conn.execute("UPDATE users SET verified=1 WHERE username='" + mail + "'")
conn.commit()
conn.close()
resp = make_response(render_template("welcome.html", name=name))
resp.set_cookie('dont_try', str(sha1(mail)))
resp.set_cookie('email', mail)
return resp
else:
return "Verification link broken"
else:
return render_template("/registered.html")
@app.route("/welcome")
def welcome_user():
_hash = request.cookies.get("dont_try")
username = request.cookies.get("email")
cookie_check(username, _hash)
conn = sql.connect("database.db")
creds = conn.execute("SELECT name FROM users WHERE username = '" + username + "'").fetchall()
name = creds[0][0]
conn.close()
return render_template("welcome.html", name=name) ##will contain two options -- findpool; postpool
@app.route("/postpool")
def postpool():
_hash = request.cookies.get("dont_try")
username = request.cookies.get("email")
cookie_check(username, _hash)
return render_template("postpool.html")
@app.route("/postpool", methods=['GET','POST'])
def postpoolreq():
if request.method == 'POST':
_hash = request.cookies.get("dont_try")
username = request.cookies.get("email")
cookie_check(username, _hash)
##fetch the name of the current user
conn = sql.connect("database.db")
fetch_name = conn.execute("SELECT name from users where username='" + username + "'").fetchall()
name = fetch_name[0][0]
#######################################
start_location = request.form['start-location'] ##start location
dest_location = request.form['dest-location'] ##destination location
contact = request.form['contact'] ##phone number
vehicle = request.form['vehicle-type'] ##vehicle type
conn = sql.connect("database.db")
cur = conn.cursor()
cur.execute("INSERT INTO posts (name,start_location,dest_location,contact,vehicle) VALUES (?,?,?,?,?)", (name,start_location,dest_location,contact,vehicle))
conn.commit()
conn.close()
return render_template("/postsuccess.html")
@app.route("/findpools")
def findpools():
_hash = request.cookies.get("dont_try")
username = request.cookies.get("email")
cookie_check(username, _hash)
conn = sql.connect("database.db")
all_data = conn.execute("SELECT * FROM posts").fetchall()
html_code = ""
for i in range(0, len(all_data)):
html_code += "<tr>"
for x in range(0, 5):
html_code += "<td>" + str(all_data[i][x]) + "</td>"
html_code += "</tr>"
print(html_code)
return render_template("/viewpools.html", html_code=html_code)
@app.route("/findpool")
def findpool():
_hash = request.cookies.get("dont_try")
username = request.cookies.get("email")
cookie_check(username, _hash)
conn = sql.connect("database.db")
all_data = conn.execute("SELECT * FROM posts").fetchall()
html_code = ""
for i in range(0, len(all_data)):
html_code += "<tr>"
for x in range(0, 5):
html_code += "<td>" + str(all_data[i][x]) + "</td>"
html_code += "</tr>"
print(html_code)
return render_template("/viewpools.html", html_code=html_code)
@app.route("/teamvortex")
def team():
return render_template("/teamvortex.html")
def cookie_check(username, hash):
conn = sql.connect("database.db")
creds = conn.execute("SELECT * FROM users WHERE username = '" + username + "'").fetchall()
conn.close()
if creds == []:
return render_template("/notregistered.html")
else:
pass
if hash != sha1(creds[0][1]): ##uername/mail to sha1
logout()
else:
pass
@app.route("/logout")
def logout():
resp = make_response(render_template("/register.html"))
resp.set_cookie('dont_try', expires=0)
resp.set_cookie('email', expires=0)
return resp
def sendmail(username, vlink):
server = smtplib.SMTP('smtp.gmail.com', 587) #change it according to your smtp details
server.ehlo()
server.starttls()
uid = "XXXXXXX@gmail.com" #your smtp email
passwd = "PASSSOWRD" #your smtp password
server.login(uid, passwd)
msg = ("Verify your mail \n" + str(vlink))
message = 'Subject: {}\n\n{}'.format("Pool-in registration", msg)
server.sendmail('randomxes05@gmail.com', username, message) # smtpmail, your real email
server.close()
if __name__ == "__main__":
app.run()