diff --git a/daemon.py b/daemon.py index 77642227..d52c6b29 100644 --- a/daemon.py +++ b/daemon.py @@ -135,9 +135,9 @@ from webapp_timeline import htmlInboxNews from webapp_timeline import htmlOutbox from webapp_timeline import htmlModeration from webapp_create_post import htmlNewPost -from webapp import htmlLogin +from webapp_login import htmlLogin +from webapp_login import htmlGetLoginCredentials from webapp import htmlSuspended -from webapp import htmlGetLoginCredentials from webapp import htmlFollowConfirm from webapp import htmlUnfollowConfirm from webapp import htmlEditNewsPost diff --git a/webapp.py b/webapp.py index f51ff06f..ee0f0200 100644 --- a/webapp.py +++ b/webapp.py @@ -6,14 +6,12 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" -import time import os from shutil import copyfile from utils import getCSS from utils import getNicknameFromActor from utils import getDomainFromActor from utils import locatePost -from utils import noOfAccounts from utils import loadJson from utils import getConfigParam from posts import isEditor @@ -195,160 +193,6 @@ def htmlEditNewsPost(cssCache: {}, translate: {}, baseDir: str, path: str, return editNewsPostForm -def htmlGetLoginCredentials(loginParams: str, - lastLoginTime: int) -> (str, str, bool): - """Receives login credentials via HTTPServer POST - """ - if not loginParams.startswith('username='): - return None, None, None - # minimum time between login attempts - currTime = int(time.time()) - if currTime < lastLoginTime+10: - return None, None, None - if '&' not in loginParams: - return None, None, None - loginArgs = loginParams.split('&') - nickname = None - password = None - register = False - for arg in loginArgs: - if '=' in arg: - if arg.split('=', 1)[0] == 'username': - nickname = arg.split('=', 1)[1] - elif arg.split('=', 1)[0] == 'password': - password = arg.split('=', 1)[1] - elif arg.split('=', 1)[0] == 'register': - register = True - return nickname, password, register - - -def htmlLogin(cssCache: {}, translate: {}, - baseDir: str, autocomplete=True) -> str: - """Shows the login screen - """ - accounts = noOfAccounts(baseDir) - - loginImage = 'login.png' - loginImageFilename = None - if os.path.isfile(baseDir + '/accounts/' + loginImage): - loginImageFilename = baseDir + '/accounts/' + loginImage - elif os.path.isfile(baseDir + '/accounts/login.jpg'): - loginImage = 'login.jpg' - loginImageFilename = baseDir + '/accounts/' + loginImage - elif os.path.isfile(baseDir + '/accounts/login.jpeg'): - loginImage = 'login.jpeg' - loginImageFilename = baseDir + '/accounts/' + loginImage - elif os.path.isfile(baseDir + '/accounts/login.gif'): - loginImage = 'login.gif' - loginImageFilename = baseDir + '/accounts/' + loginImage - elif os.path.isfile(baseDir + '/accounts/login.webp'): - loginImage = 'login.webp' - loginImageFilename = baseDir + '/accounts/' + loginImage - elif os.path.isfile(baseDir + '/accounts/login.avif'): - loginImage = 'login.avif' - loginImageFilename = baseDir + '/accounts/' + loginImage - - if not loginImageFilename: - loginImageFilename = baseDir + '/accounts/' + loginImage - copyfile(baseDir + '/img/login.png', loginImageFilename) - - if os.path.isfile(baseDir + '/accounts/login-background-custom.jpg'): - if not os.path.isfile(baseDir + '/accounts/login-background.jpg'): - copyfile(baseDir + '/accounts/login-background-custom.jpg', - baseDir + '/accounts/login-background.jpg') - - if accounts > 0: - loginText = \ - '

' + \ - translate['Welcome. Please enter your login details below.'] + \ - '

' - else: - loginText = \ - '

' + \ - translate['Please enter some credentials'] + '

' - loginText += \ - '

' + \ - translate['You will become the admin of this site.'] + \ - '

' - if os.path.isfile(baseDir + '/accounts/login.txt'): - # custom login message - with open(baseDir + '/accounts/login.txt', 'r') as file: - loginText = '

' + file.read() + '

' - - cssFilename = baseDir + '/epicyon-login.css' - if os.path.isfile(baseDir + '/login.css'): - cssFilename = baseDir + '/login.css' - - loginCSS = getCSS(baseDir, cssFilename, cssCache) - if not loginCSS: - print('ERROR: login css file missing ' + cssFilename) - return None - - # show the register button - registerButtonStr = '' - if getConfigParam(baseDir, 'registration') == 'open': - if int(getConfigParam(baseDir, 'registrationsRemaining')) > 0: - if accounts > 0: - idx = 'Welcome. Please login or register a new account.' - loginText = \ - '

' + \ - translate[idx] + \ - '

' - registerButtonStr = \ - '' - - TOSstr = \ - '

' + \ - translate['Terms of Service'] + '

' - TOSstr += \ - '

' + \ - translate['About this Instance'] + '

' - - loginButtonStr = '' - if accounts > 0: - loginButtonStr = \ - '' - - autocompleteStr = '' - if not autocomplete: - autocompleteStr = 'autocomplete="off" value=""' - - loginForm = htmlHeader(cssFilename, loginCSS) - loginForm += '
\n' - loginForm += '
\n' - loginForm += '
\n' - loginForm += \ - ' login image\n' - loginForm += loginText + TOSstr + '\n' - loginForm += '
\n' - loginForm += '\n' - loginForm += '
\n' - loginForm += ' \n' - loginForm += \ - ' \n' - loginForm += '\n' - loginForm += ' \n' - loginForm += \ - ' \n' - loginForm += loginButtonStr + registerButtonStr + '\n' - loginForm += '
\n' - loginForm += '
\n' - loginForm += \ - '' + \ - '' + \
-        translate['Get the source code'] + '\n' - loginForm += htmlFooter() - return loginForm - - def htmlTermsOfService(cssCache: {}, baseDir: str, httpPrefix: str, domainFull: str) -> str: """Show the terms of service screen diff --git a/webapp_login.py b/webapp_login.py new file mode 100644 index 00000000..6ee41fc5 --- /dev/null +++ b/webapp_login.py @@ -0,0 +1,170 @@ +__filename__ = "webapp_login.py" +__author__ = "Bob Mottram" +__license__ = "AGPL3+" +__version__ = "1.1.0" +__maintainer__ = "Bob Mottram" +__email__ = "bob@freedombone.net" +__status__ = "Production" + +import os +import time +from shutil import copyfile +from utils import getConfigParam +from utils import noOfAccounts +from utils import getCSS +from webapp_utils import htmlHeader +from webapp_utils import htmlFooter + + +def htmlGetLoginCredentials(loginParams: str, + lastLoginTime: int) -> (str, str, bool): + """Receives login credentials via HTTPServer POST + """ + if not loginParams.startswith('username='): + return None, None, None + # minimum time between login attempts + currTime = int(time.time()) + if currTime < lastLoginTime+10: + return None, None, None + if '&' not in loginParams: + return None, None, None + loginArgs = loginParams.split('&') + nickname = None + password = None + register = False + for arg in loginArgs: + if '=' in arg: + if arg.split('=', 1)[0] == 'username': + nickname = arg.split('=', 1)[1] + elif arg.split('=', 1)[0] == 'password': + password = arg.split('=', 1)[1] + elif arg.split('=', 1)[0] == 'register': + register = True + return nickname, password, register + + +def htmlLogin(cssCache: {}, translate: {}, + baseDir: str, autocomplete=True) -> str: + """Shows the login screen + """ + accounts = noOfAccounts(baseDir) + + loginImage = 'login.png' + loginImageFilename = None + if os.path.isfile(baseDir + '/accounts/' + loginImage): + loginImageFilename = baseDir + '/accounts/' + loginImage + elif os.path.isfile(baseDir + '/accounts/login.jpg'): + loginImage = 'login.jpg' + loginImageFilename = baseDir + '/accounts/' + loginImage + elif os.path.isfile(baseDir + '/accounts/login.jpeg'): + loginImage = 'login.jpeg' + loginImageFilename = baseDir + '/accounts/' + loginImage + elif os.path.isfile(baseDir + '/accounts/login.gif'): + loginImage = 'login.gif' + loginImageFilename = baseDir + '/accounts/' + loginImage + elif os.path.isfile(baseDir + '/accounts/login.webp'): + loginImage = 'login.webp' + loginImageFilename = baseDir + '/accounts/' + loginImage + elif os.path.isfile(baseDir + '/accounts/login.avif'): + loginImage = 'login.avif' + loginImageFilename = baseDir + '/accounts/' + loginImage + + if not loginImageFilename: + loginImageFilename = baseDir + '/accounts/' + loginImage + copyfile(baseDir + '/img/login.png', loginImageFilename) + + if os.path.isfile(baseDir + '/accounts/login-background-custom.jpg'): + if not os.path.isfile(baseDir + '/accounts/login-background.jpg'): + copyfile(baseDir + '/accounts/login-background-custom.jpg', + baseDir + '/accounts/login-background.jpg') + + if accounts > 0: + loginText = \ + '

' + \ + translate['Welcome. Please enter your login details below.'] + \ + '

' + else: + loginText = \ + '

' + \ + translate['Please enter some credentials'] + '

' + loginText += \ + '

' + \ + translate['You will become the admin of this site.'] + \ + '

' + if os.path.isfile(baseDir + '/accounts/login.txt'): + # custom login message + with open(baseDir + '/accounts/login.txt', 'r') as file: + loginText = '

' + file.read() + '

' + + cssFilename = baseDir + '/epicyon-login.css' + if os.path.isfile(baseDir + '/login.css'): + cssFilename = baseDir + '/login.css' + + loginCSS = getCSS(baseDir, cssFilename, cssCache) + if not loginCSS: + print('ERROR: login css file missing ' + cssFilename) + return None + + # show the register button + registerButtonStr = '' + if getConfigParam(baseDir, 'registration') == 'open': + if int(getConfigParam(baseDir, 'registrationsRemaining')) > 0: + if accounts > 0: + idx = 'Welcome. Please login or register a new account.' + loginText = \ + '

' + \ + translate[idx] + \ + '

' + registerButtonStr = \ + '' + + TOSstr = \ + '

' + \ + translate['Terms of Service'] + '

' + TOSstr += \ + '

' + \ + translate['About this Instance'] + '

' + + loginButtonStr = '' + if accounts > 0: + loginButtonStr = \ + '' + + autocompleteStr = '' + if not autocomplete: + autocompleteStr = 'autocomplete="off" value=""' + + loginForm = htmlHeader(cssFilename, loginCSS) + loginForm += '
\n' + loginForm += '
\n' + loginForm += '
\n' + loginForm += \ + ' login image\n' + loginForm += loginText + TOSstr + '\n' + loginForm += '
\n' + loginForm += '\n' + loginForm += '
\n' + loginForm += ' \n' + loginForm += \ + ' \n' + loginForm += '\n' + loginForm += ' \n' + loginForm += \ + ' \n' + loginForm += loginButtonStr + registerButtonStr + '\n' + loginForm += '
\n' + loginForm += '
\n' + loginForm += \ + '' + \ + '' + \
+        translate['Get the source code'] + '\n' + loginForm += htmlFooter() + return loginForm