Add CAPTCHA

This commit is contained in:
octospacc 2022-06-18 14:09:59 +02:00
parent 4be55cdb23
commit fd1fcc8cf8
81 changed files with 59403 additions and 24 deletions

Binary file not shown.

BIN
CAPTCHA/en/0/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/1/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/2/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/3/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/4/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/5/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/6/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/7/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/8/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/9/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/a/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/b/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/c/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/d/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/e/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/f/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/g/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/h/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/i/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/j/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/k/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/l/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/m/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/n/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/o/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/p/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/q/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/r/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/s/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/t/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/u/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/v/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/w/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/x/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/y/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/en/z/default.wav Normal file

Binary file not shown.

59334
CAPTCHA/it.txt Normal file

File diff suppressed because it is too large Load Diff

BIN
CAPTCHA/it/0/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/1/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/2/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/3/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/4/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/5/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/6/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/7/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/8/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/9/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/a/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/b/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/c/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/d/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/e/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/f/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/g/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/h/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/i/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/j/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/k/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/l/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/m/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/n/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/o/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/p/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/q/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/r/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/s/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/t/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/u/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/v/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/w/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/x/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/y/default.wav Normal file

Binary file not shown.

BIN
CAPTCHA/it/z/default.wav Normal file

Binary file not shown.

View File

@ -1,3 +1,4 @@
{
"Development": true
"Development": true,
"Port": 8081
}

View File

@ -6,6 +6,7 @@
</style>
</head>
<body>
<h3>[Locale:Comments]</h3>
<div id="StatusGoodDiv">
{StatusGood}
</div>

View File

@ -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>

View File

@ -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,22 +185,26 @@ def PatchCommentsHTML(Data):
FormMain = FormMain.replace('[Locale:{}]'.format(String), Locale[String])
FormComment = FormComment.replace('[Locale:{}]'.format(String), Locale[String])
FormMain = FormMain.format(
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 '')
if 'Action' in Data and Data['Action']:
if Data['Action'] == 'Login':
Good, Error = '', ''
elif Data['Action'] == 'Post':
Good, Error = PostCommentData(Data)
elif 'Reply' in Data and Data['Reply']:
Good, Error = PostCommentData(Data)
elif 'Delete' in Data and Data['Delete']:
Good, Error = '', ''
if 'ReadOnly' in Data and Data['ReadOnly'] == 'True':
FormMain, Good, Error = '', '', ''
else:
Good, Error = '', ''
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 '')
if 'Action' in Data and Data['Action']:
if Data['Action'] == 'Login':
Good, Error = '', ''
elif Data['Action'] == 'Post':
Good, Error = PostCommentData(Data)
elif 'Reply' in Data and Data['Reply']:
Good, Error = PostCommentData(Data)
elif 'Delete' in Data and Data['Delete']:
Good, Error = '', ''
else:
Good, Error = '', ''
Comments = ''
for ID,User,Page,Reply,Date,Comment in GetComments(Data['Site'], Data['Page']):
@ -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.

View File

@ -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>

View File

@ -1,2 +1,3 @@
flask
waitress
captcha