Skip to content
Snippets Groups Projects
newurl.py 5.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • from grecaptcha_verify import grecaptcha_verify
    from flask import render_template
    
    Jonas Leder's avatar
    Jonas Leder committed
    from sqlite3 import connect
    
    from makeqr import makeQR
    
    
    def newurl(request, skipCaptcha, recaptchaPrivateKey, recaptchaPublicKey, builddate, version, domain_prepared, domain_to_index, showDomainSelect, cookieNotice, s, url_scheme, loginEnabled, passwordProtected, password):
    
        try:
            userID = s.loads(request.cookies.get('userID'))
    
            loginbar = "Hello " + s.loads(request.cookies.get('username')) + ' (<a href="/user/links" >your links</a>, <a href="/user/logout" >logout</a>)'
    
        except:
            userID = "null"
    
            loginbar = '<a href="/user/login" >login</a>'
    
        if not grecaptcha_verify(request, skipCaptcha, recaptchaPrivateKey):
    
            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, loginEnabled=loginEnabled, loginbar=loginbar, cookieNotice=cookieNotice, passwordProtected=passwordProtected) #return the user the prefilled form with an error message, because recaptcha failed
        if passwordProtected and (request.form.get("password") != password):
            return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="Wrong password, 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, loginEnabled=loginEnabled, loginbar=loginbar, cookieNotice=cookieNotice, passwordProtected=passwordProtected) #return the user the prefilled form with an error message, because the password was wrong
    
        if (request.form.get('url').replace(" ", "") == ""):
    
            return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="Please enter a url to short, before submitting this form", 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, loginEnabled=loginEnabled, loginbar=loginbar, cookieNotice=cookieNotice, passwordProtected=passwordProtected) #return the user the prefilled form with an error message, because no url to short was provided
    
        if (request.form.get('short').replace(" ", "") == ""):
    
            return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="Please enter a short name, before submitting this form", 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, loginEnabled=loginEnabled, loginbar=loginbar, cookieNotice=cookieNotice, passwordProtected=passwordProtected) #return the user the prefilled form with an error message, because no short link was provided
    
    Jonas Leder's avatar
    Jonas Leder committed
        shorturl = (request.form.get('domain') + "/" + request.form.get('short').replace(" ", "_").replace("/", "").replace("?","")).lower()
    
    
        url = request.form.get('url')
    
    Jonas Leder's avatar
    Jonas Leder committed
        with connect('db/urls.db') as conn: #Check if another user already used the short link
    
            cursor = conn.cursor()
            res = cursor.execute('SELECT LONG_URL FROM WEB_URL WHERE SHORT_URL=?', [shorturl])
            try:
                short = res.fetchone()
                already_used = False
                if short is not None:
                    already_used = True
            except:
                pass
    
            if not already_used: #If short link wasn't used before, insert the link in the Database.
                res = cursor.execute(
                    '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(url_scheme + "://" + shorturl), loginbar=loginbar, cookieNotice=cookieNotice, loginEnabled=loginEnabled) #return the shorten link to the user
    
                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, cookieNotice=cookieNotice, loginEnabled=loginEnabled) #return the user the prefilled form with an error message, because the url was already used
    
    
    if (__name__ == "__main__"):
        print("This file is not made fore direct call, please run the main.py")
        exit()