Add CAPTCHA
This commit is contained in:
parent
4be55cdb23
commit
fd1fcc8cf8
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
"Development": true
|
||||
"Development": true,
|
||||
"Port": 8081
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h3>[Locale:Comments]</h3>
|
||||
<div id="StatusGoodDiv">
|
||||
{StatusGood}
|
||||
</div>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<div>
|
||||
<h3>Comments</h3>
|
||||
<button type="submit" name="Action" value="Login">Admin Login</button>
|
||||
</div>
|
||||
<br>
|
||||
|
@ -9,8 +8,7 @@
|
|||
</div>
|
||||
<br>
|
||||
<div>
|
||||
<label>CAPTCHA:</label>
|
||||
<span><input type="text" name="CAPTCHA"></span>
|
||||
{CAPTCHAHTML}
|
||||
</div>
|
||||
<br>
|
||||
<div>
|
||||
|
|
|
@ -7,18 +7,23 @@
|
|||
| =============================== """
|
||||
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import secrets
|
||||
import sqlite3
|
||||
import time
|
||||
from ast import literal_eval
|
||||
from base64 import b64encode
|
||||
from flask import Flask, request, send_file
|
||||
from pathlib import Path
|
||||
from captcha.audio import AudioCaptcha
|
||||
from captcha.image import ImageCaptcha
|
||||
|
||||
App = Flask(__name__)
|
||||
|
||||
def ReadFile(p):
|
||||
def ReadFile(p, m='r'):
|
||||
try:
|
||||
with open(p, 'r') as f:
|
||||
with open(p, m) as f:
|
||||
return f.read()
|
||||
except Exception:
|
||||
print("Error reading file {}".format(p))
|
||||
|
@ -53,7 +58,7 @@ def GetConfig():
|
|||
'Development': False,
|
||||
'Port': 8080,
|
||||
'Default Locale': 'en',
|
||||
'Antispam Time': 0}
|
||||
'CAPTCHA': True}
|
||||
File = ReadFile('Config.json')
|
||||
if File:
|
||||
File = json.loads(File)
|
||||
|
@ -71,6 +76,40 @@ def GetDB():
|
|||
DB = sqlite3.connect('Comments.db')
|
||||
return DB
|
||||
|
||||
def RandomWord():
|
||||
Words = ReadFile('CAPTCHA/it.txt')
|
||||
Word = '#'
|
||||
while Word.startswith('#'):
|
||||
Word = random.choice(Words.splitlines())
|
||||
return Word.lower()
|
||||
|
||||
def MakeCAPTCHA(ID):
|
||||
#ID = time.time()
|
||||
String = RandomWord()
|
||||
Audio = AudioCaptcha(voicedir='CAPTCHA/it')
|
||||
Image = ImageCaptcha(fonts=['CAPTCHA/OpenDyslexic3-Regular.ttf'],width=len(String)*40, height=90)
|
||||
CAPTCHA = Audio.generate(String)
|
||||
Audio.write(String, 'CAPTCHA.{}.wav'.format(ID))
|
||||
CAPTCHA = Image.generate(String)
|
||||
Image.write(String, 'CAPTCHA.{}.png'.format(ID))
|
||||
#return ID
|
||||
|
||||
def CAPTCHAHTML(ID):
|
||||
#ID = MakeCAPTCHA()
|
||||
MakeCAPTCHA(ID)
|
||||
Audio = b64encode(ReadFile('CAPTCHA.{}.wav'.format(ID), 'rb'))
|
||||
Image = b64encode(ReadFile('CAPTCHA.{}.png'.format(ID), 'rb'))
|
||||
os.remove('CAPTCHA.{}.wav'.format(ID))
|
||||
os.remove('CAPTCHA.{}.png'.format(ID))
|
||||
return """\
|
||||
<label>CAPTCHA:</label>
|
||||
<br>
|
||||
<img src="data:image/png;base64,{}">
|
||||
<audio src="data:audio/wav;base64,{}"controls="controls"></audio>
|
||||
<br>
|
||||
<span><input type="text" name="CAPTCHA"></span>
|
||||
""".format(Image.decode('UTF-8'), Audio.decode('UTF-8'))
|
||||
|
||||
def GetComments(Site, Page):
|
||||
DB = GetDB()
|
||||
Comments = []
|
||||
|
@ -134,6 +173,7 @@ def PostCommentData(Data):
|
|||
return Good, Error
|
||||
|
||||
def PatchCommentsHTML(Data):
|
||||
ReqID = time.time()
|
||||
FormBase = ReadFile('Source/Form.Base.html')
|
||||
FormMain = ReadFile('Source/Form.Main.html')
|
||||
FormComment = ReadFile('Source/Form.Comment.html')
|
||||
|
@ -145,7 +185,11 @@ def PatchCommentsHTML(Data):
|
|||
FormMain = FormMain.replace('[Locale:{}]'.format(String), Locale[String])
|
||||
FormComment = FormComment.replace('[Locale:{}]'.format(String), Locale[String])
|
||||
|
||||
if 'ReadOnly' in Data and Data['ReadOnly'] == 'True':
|
||||
FormMain, Good, Error = '', '', ''
|
||||
else:
|
||||
FormMain = FormMain.format(
|
||||
CAPTCHAHTML=CAPTCHAHTML(ReqID) if Config['CAPTCHA'] else '',
|
||||
SecKey=Data['SecKey'] if 'SecKey' in Data and Data['SecKey'] else '',
|
||||
User=Data['User'] if 'User' in Data and Data['User'] else '',
|
||||
Comment=Data['Comment'] if 'Comment' in Data and Data['Comment'] else '')
|
||||
|
@ -185,7 +229,7 @@ def Comments():
|
|||
Req = request
|
||||
Data = {}
|
||||
if Req.method == 'GET':
|
||||
for i in ['Lang','StyleFile','Site','Page']:
|
||||
for i in ['Lang','StyleFile','Site','Page','ReadOnly']:
|
||||
Data.update({i:Req.args.get(i)})
|
||||
if Req.method == 'POST':
|
||||
for i in ['Lang','StyleFile','Site','Page','User','CAPTCHA','Comment','SecKey','Action','Reply','Report','Delete']:
|
||||
|
|
Binary file not shown.
|
@ -2,6 +2,6 @@
|
|||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<iframe src="http://localhost:8080/Comments?Lang=it&Site=5dGQi5vT0hxWULCprKW7Og9g0xRNkHqxxEKI3cGdh-A&Page=/index.html" width="100%" height="80%"></iframe>
|
||||
<iframe src="http://localhost:8081/Comments?Lang=it&Site=5dGQi5vT0hxWULCprKW7Og9g0xRNkHqxxEKI3cGdh-A&Page=/index.html" width="100%" height="80%"></iframe>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
flask
|
||||
waitress
|
||||
captcha
|
||||
|
|
Loading…
Reference in New Issue