#!/usr/local/bin/python3.3 import cgi import cgitb import pymysql import hashlib import http.cookies as Cookie from functions import get_cookies cgitb.enable() """ The following issues need to be addressed: -reset password -create account """ data = [] #data should be in the order: user, passwd, host, db with open('./dbinfo.txt') as f: for line in f: line = line.strip('\n') data.append(line) info = [] for item in data: item = item.split(':') info.append(item) user=info[0][1] passwd=info[1][1] host=info[2][1] db=info[3][1] def runQuery(query, user, passwd): connection = pymysql.connect(host=host,db=db,user=user,passwd=passwd) cursor = connection.cursor() cursor.execute(query) res = cursor.fetchall() cursor.close() connection.close() if len(res) > 0: return res else: return None #get the form data (it will all be "nonetype" until someone hits 'submit') form = cgi.FieldStorage() cookie = get_cookies() if cookie != None: print("Location: home.py") print("Content-type: text/html\n") else: #extract specific fields from the form uname = form.getvalue("uname") pword = form.getvalue("pword") if pword != None: pword = hashlib.md5(pword.encode()).hexdigest() q1 = """select uid, username, pword_hash, firstname, lastname, admin, approved from User WHERE username LIKE '%s' and pword_hash LIKE '%s';""" %(uname, pword) if uname == None or pword == None: print("Content-type: text/html\n") print(""" DAPR """) print("""

Already registered? Sign In

""") else: validuser = runQuery(q1, user, passwd) if validuser == None: print("Content-type: text/html\n") print("Content-type: text/html\n") print(""" DAPR """) print("""
""") else: #get the latest experiment ID created by this user q2="select eid from Experiment where creator='%s' order by create_date desc limit 1 "%validuser[0][1]; latest_eid = runQuery(q2, user, passwd) if latest_eid == None: eid="0" else: ((eid,),)=latest_eid cookie = Cookie.SimpleCookie() cookie["uid"] = validuser[0][0] cookie["uid"]["max-age"] = 2*60*60 cookie["uname"] = validuser[0][1] cookie["uname"]["max-age"] = 2*60*60 cookie["pword"] = validuser[0][2] cookie["pword"]["max-age"] = 2*60*60 cookie["fname"] = validuser[0][3] cookie["fname"]["max-age"] = 2*60*60 cookie["lname"] = validuser[0][4] cookie["lname"]["max-age"] = 2*60*60 cookie["admin"] = validuser[0][5] cookie["admin"]["max-age"] = 2*60*60 cookie["appro"] = validuser[0][6] cookie["appro"]["max-age"] = 2*60*60 cookie["ck_eid"] = eid cookie["ck_eid"]["max-age"] = 2*60*60 print(cookie) print("Location: home.py") print("Content-type: text/html\n")