from flask import Blueprint, Flask, request, flash, render_template, url_for, session, redirect, abort, make_response, send_file, escape from flask_bcrypt import Bcrypt import sqlite3 import glob, os, sys, time from tools.utils import email_disp, valid_token_register from socket import gethostname app = Flask( 'pywallter' ) app.config.from_pyfile('config.py') bcrypt = Bcrypt(app) #### Variables ################################################################################## DOSSIER_PERSO= app.config['DOSSIER_APP'] extensionimg = app.config['EXT_IMG'] DATABASE = app.config['DATABASE'] MAIL_SERVER = app.config['MAIL_SERVER'] XMMP_SERVER = app.config['XMPP_SERVER'] SETUID = app.config['SETUID'] BASE_URL = app.config['BASE_URL'] ################################################################################################# inscription = Blueprint('inscription', __name__, template_folder='templates') @inscription.route( '/inscription/', methods=['GET','POST'] ) def signin(token) : hostname = gethostname() if app.config['SIGNIN_ENABLE'] and valid_token_register(token): if 'username' in session : resp = redirect(url_for('profil.profile', _external=True)) else : if request.method == 'POST': conn = sqlite3.connect(DATABASE) # Connexion à la base de donnée cursor = conn.cursor() # Création de l'objet "curseur" user = request.form['user'] mail = request.form['mail']+'@'+hostname passwd = bcrypt.generate_password_hash(request.form['passwd']) passwdconfirm = request.form['passwdconfirm'] cursor.execute("""SELECT name FROM users WHERE name=?""", (user,)) testuser = cursor.fetchone() conn.close() if MAIL_SERVER: p = run( [ SETUID, 'set_mail_passwd', "'"+mailbox['Mail']+"'", "'"+passwd+"'" ] ) if testuser: flash(u'Non d\'utilisateur déjà utilisé, merci d\'en choisir un autre', 'error') resp = render_template('inscription.html', signin_enable=app.config['SIGNIN_ENABLE'], hostname=hostname) elif not(email_disp(mail)) or p.returncode != 0 : flash(u'Adresse email déjà utilisé, merci d\'en choisir un autre', 'error') resp = render_template('inscription.html', signin_enable=app.config['SIGNIN_ENABLE'], hostname=hostname) else: confirmation = bcrypt.check_password_hash(passwd, passwdconfirm) if confirmation is True: conn = sqlite3.connect(DATABASE) # Connexion à la base de donnée cursor = conn.cursor() # Création de l'objet "curseur" cursor.execute("""INSERT INTO users(name, mail, passwd) VALUES(?, ?, ?)""", (user, mail, passwd)) # Insérer des valeurs conn.commit() # Sauvegarder valeurs dans la bdd if XMMP_SERVER: tmp = mailbox['mail'].split('@') p = run( [ SETUID, 'prosodyctl register ', "'"+tmp[0]+"'", "'"+tmp[1]+"'", "'"+passwd+"'" ]) if p.returncode != 0: flash(u'Il y a eu un problème lors de la création du compte XMPP !', 'error') cursor.execute("""SELECT name, mail, passwd FROM users""") users = cursor.fetchall() for i in users: i = print('{0} - {1} - {2}'.format(i[0], i[1], i[2])) conn.close() userracine = DOSSIER_PERSO + user userfiles = userracine + '/files' userimages = userracine + '/images' userthumbnails = userracine + '/images/thumbnails' userprofile = userracine + '/profile' if not os.path.exists(userracine): os.makedirs(userracine) os.makedirs(userfiles) os.makedirs(userimages) os.makedirs(userthumbnails) os.makedirs(userprofile) fp = open('log.txt', 'x') fp.close() # Une fois que tout c'est bien passé pour l'inscription on détruit le jeton. cursor.execute("""SELECT name, invitations FROM users where Token=?""", (token,)) tmp = cursor.fetchone() username =tmp[0] invitations_count=tmp[1] - 1 cursor.execute("""UPDATE users set invitations=?, Token='' where name=?""", (invitations_count, username,)) conn.commit() flash(u'Inscription réalisée avec succés !', 'succes') resp = redirect(url_for('loginlogout.login')) else: flash(u'Les mots de passe ne sont pas identiques !', 'error') resp = render_template('inscription.html', signin_enable=app.config['SIGNIN_ENABLE'], hostname=hostname) else : url_inscription = BASE_URL+'invitation/'+token resp = render_template('inscription.html', signin_enable=app.config['SIGNIN_ENABLE'], token=token, hostname=hostname, url_inscription=url_inscription) else: resp = redirect(BASE_URL, code=401) return resp