Skip to content
Snippets Groups Projects
Commit 23629c5d authored by Jonas Leder's avatar Jonas Leder
Browse files

Merge branch 'beta'

parents 3b6613be 817d4d2e
No related branches found
No related tags found
No related merge requests found
Showing
with 18073 additions and 31 deletions
......@@ -71,7 +71,7 @@ try:
app.config['GITHUB_CLIENT_ID'] = os.environ['GITHUB_CLIENT_ID']
app.config['GITHUB_CLIENT_SECRET'] = os.environ['GITHUB_CLIENT_SECRET']
except:
print("github client id sor client secret is not set, please set these and run again.")
print("github client id or client secret is not set, please set these and run again.")
exit()
github = GitHub(app)
......@@ -121,6 +121,7 @@ def makeQR(text): #This function is used to create a QR code and encode it base6
def grecaptcha_verify(request): #This function is used to verify the google recaptcha code, that is send to the server after submitting a new link
if(skipCaptcha): return True #If recaptcha is disabled alwas return at this point true, which means response is verified
captcha_rs = request.form.get('g-recaptcha-response')
url = "https://www.google.com/recaptcha/api/siteverify" #The baseurl
headers = {'User-Agent': 'DebuguearApi-Browser',} #Useragent doesn't matters, but is set here
......@@ -151,7 +152,7 @@ def home_post():
except:
userID = "null"
loginbar = '<a href="/user/login" style="color:white">login</a>'
if not grecaptcha_verify(request) and not skipCaptcha:
if not grecaptcha_verify(request):
return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="There was an error validating, that you are a human, please try again.", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginbar=loginbar) #return the user the prefilled form with an error message, because no url to short was provided
if (request.form.get('url').replace(" ", "") == ""):
......@@ -177,7 +178,7 @@ def home_post():
'INSERT INTO WEB_URL (LONG_URL, SHORT_URL, USERNAME) VALUES (?, ?, ?)',
[url, shorturl, userID]
)
return render_template('home.html', short_url=shorturl, recaptchaPublicKey=recaptchaPublicKey, builddate=builddate, version=version, domain=domain_prepared, qrcode=makeQR("http://" + shorturl), loginbar=loginbar) #return the shorten link to the user
return render_template('home.html', short_url=shorturl, recaptchaPublicKey=recaptchaPublicKey, builddate=builddate, version=version, domain=domain_prepared, qrcode=makeQR(url_scheme + "://" + shorturl), loginbar=loginbar) #return the shorten link to the user
else:
return render_template('home.html', builddate=builddate, version=version, recaptchaPublicKey=recaptchaPublicKey, domain=domain_prepared, snackbar="URL already used, please try another one", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], showDomainSelect=showDomainSelect, loginbar=loginbar) #return the user the prefilled form with an error message, because the url was already used
......@@ -210,72 +211,76 @@ def redirect_short_url(short_url):
@app.route('/user/login')
def login():
return github.authorize(scope="user")
return github.authorize(scope="user") #redirect the user to the github login page and ask for access to user data (name, email, ...)
@app.route('/user/github-callback')
@app.route('/user/github-callback') #Github redirects to this link after the user authenticated. Then we use the Token we get from github and request via the github api the username and the userid
@github.authorized_handler
def authorized(oauth_token):
if oauth_token is None:
return "oauth failed, please try again"
return "oauth failed, please try again" #If you call this page manual you get this error
headers = {'Authorization': 'token ' + oauth_token,} #Useragent doesn't matters, but is set here
githubResponse = get("https://api.github.com/user", headers=headers).text
userID = str(json.loads(githubResponse)['id'])
username = str(json.loads(githubResponse)['login'])
resp = make_response(redirect('/'))
resp.set_cookie('userID', userID)
resp = make_response(redirect('/')) #redirect the user at the end back to the main page
resp.set_cookie('userID', userID) #set the cookies with username and userid
resp.set_cookie('username', username)
return resp
@app.route('/user/logout')
def logout():
resp = make_response("logout successful")
resp.set_cookie('userID', "", max_age=0)
resp.set_cookie('userID', "", max_age=0) #Set the max age of the cookies to 0, this means delete the cookies.
resp.set_cookie('username', "", max_age=0)
return resp
@app.route('/user/links')
@app.route('/user/links')#This function gives the user the posibility to see and delete his links
def ownLinks():
try:
userID = request.cookies.get('userID')
loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)'
userID = request.cookies.get('userID') #Get the userid from the cookie
loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' #This is the loginbar
except:
return redirect("/user/login")
return redirect("/user/login") #If user is not logged in redirect him to the login page
with sqlite3.connect('db/urls.db') as conn: #Get the original URL from the database
with sqlite3.connect('db/urls.db') as conn:
cursor = conn.cursor()
res = cursor.execute('SELECT LONG_URL, SHORT_URL FROM WEB_URL WHERE USERNAME=?', [userID])
response = '<table id="t01">\n<tr>\n<th>Long URL</th>\n<th>Short URL</th>\n<th>Action</th>\n</tr>\n'
res = cursor.execute('SELECT LONG_URL, SHORT_URL FROM WEB_URL WHERE USERNAME=?', [userID]) #Get all entries from the database, that are created by this user
response = '<table id="t01">\n<tr>\n<th>Long URL</th>\n<th>Short URL</th>\n<th>Action</th>\n</tr>\n' #This is the layout of the table
try:
entriesList = res.fetchall()
for entries in entriesList:
response = response + "<tr>\n<td>" + entries[0] + "</td>\n<td>" + entries[1] + '</td>\n<td><a id="red" href="/user/delete?link=' + escape(entries[1]) + '">delete</a></tr>\n'
if(len(entriesList) == 0): response = 'you have no shorten links. <a href="/">back</a>'
for entries in entriesList: #for every entrie in the database add a line to the table
response = response + "<tr>\n<td>" + entries[0] + "</td>\n<td>" + entries[1] + '</td>\n<td><a id="red" href="/user/delete?link=' + escape(entries[1]) + '">delete</a> <a href="#" id="dialog-link" onclick="buttonListener(\'' + entries[1] + '\', this)">QR</a></tr>\n'
response = response + "</table>" #Close the table
if(len(entriesList) == 0): response = 'you have no shorten links. <a href="/">back</a>' #If user has no shorten links make this message with a back button
except:
abort(500)
response = response + "</table>"
return render_template('editEntries.html', content=response, loginbar=loginbar)
abort(500) #Shouldn't happen, 500 means internal server error
return render_template('editEntries.html', content=response, loginbar=loginbar) #Put the table and the login div inside the template and server it to the user
@app.route('/user/delete')
@app.route('/user/delete') #This function is called if a user deletes an entrie
def delete():
try:
userID = request.cookies.get('userID')
loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)'
userID = request.cookies.get('userID') #get the userid from the cookie
loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' # generate the login form
except:
return redirect("/user/login")
linkToDelete = request.args.get('link')
return redirect("/user/login") # if user is not logged in redirect him to the login page
linkToDelete = request.args.get('link') #get the link, which the user want's to delete from the parameter in the url.
with sqlite3.connect('db/urls.db') as conn: #Get the original URL from the database
with sqlite3.connect('db/urls.db') as conn:
cursor = conn.cursor()
try:
cursor.execute('DELETE FROM WEB_URL WHERE SHORT_URL=? AND USERNAME=?', [linkToDelete, userID])
return redirect('/user/links')
cursor.execute('DELETE FROM WEB_URL WHERE SHORT_URL=? AND USERNAME=?', [linkToDelete, userID]) #Delete the entrie
return redirect('/user/links') #redirect the user back to the table.
except:
abort(500)
@app.route('/user/makeqr')
def makeQrCode():
link = request.args.get('link')
return "data:image/jpeg;base64," + makeQR(url_scheme + "://" + link)
if __name__ == '__main__':
table_check()# This code checks whether database table is created or not
......
This diff is collapsed.
static/images/ui-bg_glass_55_fcf0ba_1x400.png

333 B

static/images/ui-bg_gloss-wave_100_ece8da_500x100.png

5.62 KiB

static/images/ui-bg_highlight-hard_100_f5f3e5_1x100.png

336 B

static/images/ui-bg_highlight-hard_100_fafaf4_1x100.png

321 B

static/images/ui-bg_highlight-hard_15_459e00_1x100.png

328 B

static/images/ui-bg_highlight-hard_95_cccccc_1x100.png

252 B

static/images/ui-bg_highlight-soft_25_67b021_1x100.png

353 B

static/images/ui-bg_highlight-soft_95_ffedad_1x100.png

351 B

static/images/ui-bg_inset-soft_15_2b2922_1x100.png

349 B

static/images/ui-icons_808080_256x240.png

6.84 KiB

static/images/ui-icons_847e71_256x240.png

4.44 KiB

static/images/ui-icons_8DC262_256x240.png

4.44 KiB

static/images/ui-icons_cd0a0a_256x240.png

4.44 KiB

static/images/ui-icons_eeeeee_256x240.png

6.75 KiB

static/images/ui-icons_ffffff_256x240.png

6.15 KiB

This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment