2023-11-08 17:55:49 +11:00
|
|
|
from flask import Flask, make_response, redirect, request, jsonify, render_template, send_from_directory
|
|
|
|
import os
|
|
|
|
import dotenv
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import schedule
|
|
|
|
import time
|
|
|
|
from email_validator import validate_email, EmailNotValidError
|
|
|
|
import accounts
|
2023-11-08 19:02:24 +11:00
|
|
|
import db
|
2023-11-08 17:55:49 +11:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
dotenv.load_dotenv()
|
|
|
|
|
2023-11-08 19:02:24 +11:00
|
|
|
# Database connection
|
|
|
|
dbargs = {
|
|
|
|
'host':os.getenv('DB_HOST'),
|
|
|
|
'user':os.getenv('DB_USER'),
|
|
|
|
'password':os.getenv('DB_PASSWORD'),
|
|
|
|
'database':os.getenv('DB_NAME')
|
|
|
|
}
|
2023-11-08 17:55:49 +11:00
|
|
|
|
|
|
|
#Assets routes
|
|
|
|
@app.route('/assets/<path:path>')
|
|
|
|
def assets(path):
|
|
|
|
return send_from_directory('templates/assets', path)
|
|
|
|
|
2023-11-08 19:02:24 +11:00
|
|
|
|
2023-11-08 17:55:49 +11:00
|
|
|
def error(message):
|
|
|
|
return jsonify({'success': False, 'message': message}), 400
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
|
|
|
if 'token' in request.cookies:
|
|
|
|
token = request.cookies['token']
|
|
|
|
# Verify token
|
|
|
|
user = accounts.validate_token(token)
|
|
|
|
if not user:
|
|
|
|
# Remove cookie
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('token', '', expires=0)
|
|
|
|
return resp
|
|
|
|
return render_template('index.html',account=user['email'],account_link="account")
|
|
|
|
return render_template('index.html',account="Login",account_link="login")
|
|
|
|
|
|
|
|
@app.route('/signup', methods=['POST'])
|
|
|
|
def signup():
|
|
|
|
email = request.form['email']
|
|
|
|
domain = request.form['domain']
|
|
|
|
password = request.form['password']
|
|
|
|
try:
|
|
|
|
valid = validate_email(email)
|
|
|
|
email = valid.email
|
|
|
|
user = accounts.create_user(email, domain, password)
|
|
|
|
if not user['success']:
|
|
|
|
return error(user['message'])
|
|
|
|
|
|
|
|
# Redirect to dashboard with cookie
|
|
|
|
resp = make_response(redirect('/edit'))
|
|
|
|
resp.set_cookie('token', user['token'])
|
|
|
|
return resp
|
|
|
|
|
|
|
|
except EmailNotValidError as e:
|
|
|
|
return jsonify({'success': False, 'message': 'Invalid email'}), 400
|
2023-11-08 20:06:47 +11:00
|
|
|
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
|
|
def login():
|
|
|
|
email=request.form['email']
|
|
|
|
password=request.form['password']
|
|
|
|
user = accounts.login(email,password)
|
|
|
|
if not user['success']:
|
|
|
|
return error(user['message'])
|
|
|
|
# Redirect to dashboard with cookie
|
|
|
|
resp = make_response(redirect('/edit'))
|
|
|
|
resp.set_cookie('token', user['token'])
|
|
|
|
return resp
|
|
|
|
|
2023-11-08 20:43:33 +11:00
|
|
|
@app.route('/edit')
|
|
|
|
def edit():
|
|
|
|
token = request.cookies['token']
|
|
|
|
if not accounts.validate_token(token):
|
|
|
|
return error('Invalid token')
|
|
|
|
# Verify token
|
|
|
|
user = accounts.validate_token(token)
|
|
|
|
if not user:
|
|
|
|
# Remove cookie
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('token', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
data = db.get_website_data(user['domain'])
|
|
|
|
return render_template('edit.html',account=user['email'],account_link="account",data=data)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/edit', methods=['POST'])
|
|
|
|
def send_edit():
|
|
|
|
token = request.cookies['token']
|
|
|
|
if not accounts.validate_token(token):
|
|
|
|
return error('Invalid token')
|
|
|
|
# Verify token
|
|
|
|
user = accounts.validate_token(token)
|
|
|
|
if not user:
|
|
|
|
# Remove cookie
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('token', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
data = request.form['data']
|
|
|
|
db.update_website_data(user['domain'],data)
|
|
|
|
return redirect('/edit')
|
|
|
|
|
2023-11-08 20:06:47 +11:00
|
|
|
|
2023-11-08 17:55:49 +11:00
|
|
|
|
2023-11-08 19:02:24 +11:00
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
|
|
|
token = request.cookies['token']
|
|
|
|
if not accounts.logout(token)['success']:
|
|
|
|
return error('Invalid token')
|
|
|
|
|
|
|
|
# Remove cookie
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('token', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
2023-11-08 17:55:49 +11:00
|
|
|
@app.route('/<path:path>')
|
|
|
|
def catch_all(path):
|
2023-11-08 20:06:47 +11:00
|
|
|
account = "Login"
|
|
|
|
account_link = "login"
|
|
|
|
site = "Null"
|
|
|
|
if 'token' in request.cookies:
|
|
|
|
token = request.cookies['token']
|
|
|
|
# Verify token
|
|
|
|
user = accounts.validate_token(token)
|
|
|
|
if not user:
|
|
|
|
# Remove cookie
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('token', '', expires=0)
|
|
|
|
return resp
|
|
|
|
account = user['email']
|
|
|
|
account_link = "account"
|
|
|
|
site = user['domain'] + ".exampledomainnathan1"
|
2023-11-08 20:43:33 +11:00
|
|
|
elif path != "signup" and path != "login":
|
|
|
|
return redirect('/')
|
2023-11-08 20:06:47 +11:00
|
|
|
|
2023-11-08 17:55:49 +11:00
|
|
|
# If file exists, load it
|
|
|
|
if os.path.isfile('templates/' + path):
|
2023-11-08 20:06:47 +11:00
|
|
|
return render_template(path,account=account,account_link=account_link,site=site)
|
2023-11-08 17:55:49 +11:00
|
|
|
|
|
|
|
# Try with .html
|
|
|
|
if os.path.isfile('templates/' + path + '.html'):
|
2023-11-08 20:06:47 +11:00
|
|
|
return render_template(path + '.html',account=account,account_link=account_link,site=site)
|
2023-11-08 17:55:49 +11:00
|
|
|
return redirect('/') # 404 catch all
|
|
|
|
|
|
|
|
# 404 catch all
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def not_found(e):
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
|
2023-11-08 19:02:24 +11:00
|
|
|
|
2023-11-08 17:55:49 +11:00
|
|
|
if __name__ == '__main__':
|
2023-11-08 19:02:24 +11:00
|
|
|
db.check_tables()
|
2023-11-08 17:55:49 +11:00
|
|
|
app.run(debug=False, port=5000, host='0.0.0.0')
|