mirror of
https://github.com/simon987/od-database.git
synced 2025-04-24 12:45:51 +00:00
Captcha for searches
This commit is contained in:
parent
c94cf5b313
commit
c29af180c5
@ -14,8 +14,11 @@ Create `/config.py` and fill out the parameters. Sample config:
|
|||||||
# Leave default values for no CAPTCHAs
|
# Leave default values for no CAPTCHAs
|
||||||
CAPTCHA_LOGIN = False
|
CAPTCHA_LOGIN = False
|
||||||
CAPTCHA_SUBMIT = False
|
CAPTCHA_SUBMIT = False
|
||||||
|
CAPTCHA_SEARCH = False
|
||||||
CAPTCHA_SITE_KEY = ""
|
CAPTCHA_SITE_KEY = ""
|
||||||
CAPTCHA_SECRET_KEY = ""
|
CAPTCHA_SECRET_KEY = ""
|
||||||
|
CAPTCHA_S_SITE_KEY = ""
|
||||||
|
CAPTCHA_S_SECRET_KEY = ""
|
||||||
|
|
||||||
# Flask secret key for sessions
|
# Flask secret key for sessions
|
||||||
FLASK_SECRET = ""
|
FLASK_SECRET = ""
|
||||||
|
18
app.py
18
app.py
@ -21,6 +21,12 @@ if config.CAPTCHA_SUBMIT or config.CAPTCHA_LOGIN:
|
|||||||
secret_key=config.CAPTCHA_SECRET_KEY)
|
secret_key=config.CAPTCHA_SECRET_KEY)
|
||||||
else:
|
else:
|
||||||
recaptcha = None
|
recaptcha = None
|
||||||
|
if config.CAPTCHA_SEARCH:
|
||||||
|
recaptcha_search = ReCaptcha(app=app,
|
||||||
|
site_key=config.CAPTCHA_S_SITE_KEY,
|
||||||
|
secret_key=config.CAPTCHA_S_SECRET_KEY)
|
||||||
|
else:
|
||||||
|
recaptcha_search = None
|
||||||
app.secret_key = config.FLASK_SECRET
|
app.secret_key = config.FLASK_SECRET
|
||||||
db = Database("db.sqlite3")
|
db = Database("db.sqlite3")
|
||||||
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
|
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
|
||||||
@ -243,6 +249,7 @@ def admin_rescan_website(website_id):
|
|||||||
|
|
||||||
@app.route("/search")
|
@app.route("/search")
|
||||||
def search():
|
def search():
|
||||||
|
|
||||||
q = request.args.get("q") if "q" in request.args else ""
|
q = request.args.get("q") if "q" in request.args else ""
|
||||||
sort_order = request.args.get("sort_order") if "sort_order" in request.args else "score"
|
sort_order = request.args.get("sort_order") if "sort_order" in request.args else "score"
|
||||||
|
|
||||||
@ -286,6 +293,8 @@ def search():
|
|||||||
|
|
||||||
if len(q) >= 3:
|
if len(q) >= 3:
|
||||||
|
|
||||||
|
response = request.args.get("g-recaptcha-response", "")
|
||||||
|
if not config.CAPTCHA_SEARCH or recaptcha_search.verify(response):
|
||||||
db.log_search(request.remote_addr,
|
db.log_search(request.remote_addr,
|
||||||
request.headers["X-Forwarded-For"] if "X-Forwarded-For" in request.headers else None,
|
request.headers["X-Forwarded-For"] if "X-Forwarded-For" in request.headers else None,
|
||||||
q, extensions, page)
|
q, extensions, page)
|
||||||
@ -301,6 +310,9 @@ def search():
|
|||||||
flash("Query failed, this could mean that the search server is overloaded or is not reachable. "
|
flash("Query failed, this could mean that the search server is overloaded or is not reachable. "
|
||||||
"Please try again later", "danger")
|
"Please try again later", "danger")
|
||||||
hits = None
|
hits = None
|
||||||
|
else:
|
||||||
|
flash("<strong>Error:</strong> Invalid captcha please try again", "danger")
|
||||||
|
hits = None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
hits = None
|
hits = None
|
||||||
@ -315,7 +327,8 @@ def search():
|
|||||||
size_min=size_min, size_max=size_max,
|
size_min=size_min, size_max=size_max,
|
||||||
match_all=match_all,
|
match_all=match_all,
|
||||||
field_trigram=field_trigram, field_path=field_path, field_name=field_name,
|
field_trigram=field_trigram, field_path=field_path, field_name=field_name,
|
||||||
date_min=date_min, date_max=date_max)
|
date_min=date_min, date_max=date_max,
|
||||||
|
show_captcha=config.CAPTCHA_SEARCH, recaptcha=recaptcha_search)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/contribute")
|
@app.route("/contribute")
|
||||||
@ -331,7 +344,8 @@ def home():
|
|||||||
stats["website_count"] = len(db.get_all_websites())
|
stats["website_count"] = len(db.get_all_websites())
|
||||||
except:
|
except:
|
||||||
stats = {}
|
stats = {}
|
||||||
return render_template("home.html", stats=stats)
|
return render_template("home.html", stats=stats,
|
||||||
|
show_captcha=config.CAPTCHA_SEARCH, recaptcha=recaptcha_search)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/submit")
|
@app.route("/submit")
|
||||||
|
@ -23,13 +23,23 @@
|
|||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header">Search</div>
|
<div class="card-header">Search</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form action="/search">
|
<form action="/search" id="sfrm">
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-row">
|
||||||
|
<div class="col-md-11">
|
||||||
<input class="form-control" name="q" id="q" placeholder="Query">
|
<input class="form-control" name="q" id="q" placeholder="Query">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-md-1">
|
||||||
|
{% if show_captcha %}
|
||||||
|
<script>function f(token) {document.getElementById("sfrm").submit();}</script>
|
||||||
|
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
|
||||||
|
<button class="g-recaptcha btn btn-primary btn-shadow" data-sitekey="6LcCXWkUAAAAAJo2NR9_m09Obn5YmDrI97sGrr2f" data-callback="f">Search</button>
|
||||||
|
{% else %}
|
||||||
|
<input class="btn btn-primary btn-shadow" type="submit" value="Search nocap">
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<input class="btn btn-primary btn-shadow" type="submit" value="Search">
|
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header">Search</div>
|
<div class="card-header">Search</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form action="/search">
|
<form action="/search" id="sfrm">
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
|
|
||||||
@ -92,7 +92,14 @@
|
|||||||
|
|
||||||
{# Search button #}
|
{# Search button #}
|
||||||
<div class="form-group col-md-7">
|
<div class="form-group col-md-7">
|
||||||
|
|
||||||
|
{% if show_captcha %}
|
||||||
|
<script>function f(token) {document.getElementById("sfrm").submit();}</script>
|
||||||
|
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
|
||||||
|
<button class="g-recaptcha btn btn-primary btn-shadow" data-sitekey="6LcCXWkUAAAAAJo2NR9_m09Obn5YmDrI97sGrr2f" data-callback="f" style="float: right">Search</button>
|
||||||
|
{% else %}
|
||||||
<input class="btn btn-primary btn-shadow" type="submit" value="Search" style="float: right">
|
<input class="btn btn-primary btn-shadow" type="submit" value="Search" style="float: right">
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user