Add files via upload
This commit is contained in:
		@@ -9,94 +9,117 @@ class FormRegister extends Form {
 | 
			
		||||
 | 
			
		||||
    //Atributes:
 | 
			
		||||
    private $user;      // User who is going to log-in.
 | 
			
		||||
    private $reply;     // Validation response
 | 
			
		||||
 | 
			
		||||
    //Constructor:
 | 
			
		||||
    public function __construct() {
 | 
			
		||||
        parent::__construct('formRegister');
 | 
			
		||||
        $this->reply = array();
 | 
			
		||||
        $options = array("action" => "../register/register.php");
 | 
			
		||||
        parent::__construct('formRegister', $options);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //Methods:
 | 
			
		||||
 | 
			
		||||
    protected function generaCamposFormulario($datos, $errores = array()){
 | 
			
		||||
        $nombre = $datos['name'] ?? '';
 | 
			
		||||
 | 
			
		||||
        // Se generan los mensajes de error si existen.
 | 
			
		||||
        $htmlErroresGlobales = self::generaListaErroresGlobales($errores);
 | 
			
		||||
        $errorNombre = self::createMensajeError($errores, 'name', 'span', array('class' => 'error'));
 | 
			
		||||
        $errorEmail = self::createMensajeError($errores, 'email', 'span', array('class' => 'error'));
 | 
			
		||||
        $errorPassword = self::createMensajeError($errores, 'pass', 'span', array('class' => 'error'));
 | 
			
		||||
        $errorPassword2 = self::createMensajeError($errores, 'repass', 'span', array('class' => 'error'));
 | 
			
		||||
 | 
			
		||||
        $html = "<div class='row'>
 | 
			
		||||
                            <fieldset id='datos_personales'><pre>".$htmlErroresGlobales."</pre>
 | 
			
		||||
                                <legend>Datos personales</legend>
 | 
			
		||||
                                <input type='text' name='name' id='name' value='' placeholder='Nombre' required/><pre>".$errorNombre."</pre>
 | 
			
		||||
                                <input type='email' name='email' id='email' value='' placeholder='Email' required/><pre>".$errorEmail."</pre>
 | 
			
		||||
                                <input type='password' name='pass' id='pass' value='' placeholder='Contraseña' required/><pre>".$errorPassword."</pre>
 | 
			
		||||
                                <input type='password' name='repass' id='repass' value='' placeholder='Repita la contraseña' required/><pre>".$errorPassword2."</pre>
 | 
			
		||||
                            </fieldset>
 | 
			
		||||
                            <div class='verify'>
 | 
			
		||||
                                <input type='checkbox' id='checkbox' name='terms' required>
 | 
			
		||||
                                <label for='terms'><a href ='../fdicines/terms_conditions/' target='_blank'>Marque esta casilla para verificar que ha leído nuestros términos y condiciones del servicio.</a></label>
 | 
			
		||||
                            </div>
 | 
			
		||||
                            <div class='actions'> 
 | 
			
		||||
                                <input type='submit' id='submit' value='Registrarse' class='primary' />
 | 
			
		||||
                                <input type='reset' id='reset' value='Borrar' />       
 | 
			
		||||
                            </div>
 | 
			
		||||
                        </div>";
 | 
			
		||||
 | 
			
		||||
        return $html;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function procesaFormulario($datos){
 | 
			
		||||
        $result = array();
 | 
			
		||||
        
 | 
			
		||||
        $nombre = $this->test_input($datos['name']) ?? null;
 | 
			
		||||
        $nombre = strtolower($nombre);
 | 
			
		||||
        if ( empty($nombre) || mb_strlen($nombre) < 3 || mb_strlen($nombre) > 8 ) {
 | 
			
		||||
            $result['name'] = "El nombre tiene que tener\n una longitud de al menos\n 3 caracteres\n y menos de 8 caracteres.";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $email = $this->test_input($datos['email']) ?? null;
 | 
			
		||||
        if ( empty($email) || !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $email) ) {
 | 
			
		||||
            $result['email'] = "El email no es válido.";
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $password = $this->test_input($datos['pass']) ?? null;
 | 
			
		||||
        if ( empty($password) || mb_strlen($password) < 4 ) {
 | 
			
		||||
            $result['pass'] = "El password tiene que tener\n una longitud de al menos\n 4 caracteres.";
 | 
			
		||||
        }
 | 
			
		||||
        $password2 = $this->test_input($datos['repass']) ?? null;
 | 
			
		||||
        if ( empty($password2) || strcmp($password, $password2) !== 0 ) {
 | 
			
		||||
            $result['repass'] = "Los passwords deben coincidir";
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        if (count($result) === 0) {
 | 
			
		||||
            $bd = new UserDAO('complucine');
 | 
			
		||||
            if($bd){
 | 
			
		||||
                $this->user = $bd->selectUserName($nombre);
 | 
			
		||||
                if (!$this->user) {
 | 
			
		||||
                    $bd->createUser("", $nombre, $email, $password, "user");
 | 
			
		||||
                    $this->user = $bd->selectUser($nombre, $password);
 | 
			
		||||
                    if ($this->user) {
 | 
			
		||||
                        $this->user->setPass(null);
 | 
			
		||||
                        $_SESSION["user"] = serialize($this->user);
 | 
			
		||||
                        $_SESSION["nombre"] = $this->user->getName();
 | 
			
		||||
                        $_SESSION["rol"] = $this->user->getRol();
 | 
			
		||||
                        $_SESSION["login"] = true;
 | 
			
		||||
                        $result = '../register/register.php';
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else{
 | 
			
		||||
                    $result[] = "El nombre de usuario ya existe.";
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                $result[] = "Error al conectar con la BD.";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //Returns validation response:
 | 
			
		||||
    public function getReply() {
 | 
			
		||||
    static public function getReply() {
 | 
			
		||||
        
 | 
			
		||||
        if(isset($_SESSION["login"])){
 | 
			
		||||
            $name = strtoupper($_SESSION['nombre']);
 | 
			
		||||
            $this->reply = "<h1>Bienvenido {$_SESSION['nombre']}</h1><hr />
 | 
			
		||||
            $reply = "<h1>Bienvenido {$_SESSION['nombre']}</h1><hr />
 | 
			
		||||
                        <p>{$name}, has creado tu cuenta de usuario correctamente.</p>
 | 
			
		||||
                        <p>Usa los botones para navegar</p>
 | 
			
		||||
                        <a href='../'><button>Inicio</button></a>
 | 
			
		||||
                        <a href='../../panel_{$_SESSION["rol"]}'><button>Mi Panel</button></a>\n";
 | 
			
		||||
        }   
 | 
			
		||||
        else if(!isset($_SESSION["login"])){
 | 
			
		||||
            $this->reply = "<h1>ERROR</h1><hr />".
 | 
			
		||||
            $reply = "<h1>ERROR</h1><hr />".
 | 
			
		||||
                        "<p>Ha ocurrido un problema y no hemos podido completar el registro.</p>
 | 
			
		||||
                        <p>Puede que el nombre de usuario ya esté registrado.</p>
 | 
			
		||||
                        <p>Vuelve a intetarlo o inicia sesión si tienes una cuenta de usuario.</p>
 | 
			
		||||
                        <a href='../login/'><button>Iniciar Sesión</button></a>
 | 
			
		||||
                        <form method='post' action='../login/'><button name='register' id='register'>Registro</button></form>\n";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->reply;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //Process form:
 | 
			
		||||
    public function processesForm($name, $mail, $pass, $repass) {
 | 
			
		||||
        $register = true;
 | 
			
		||||
        $name = $this->test_input($name);
 | 
			
		||||
        $mail = $this->test_input($mail);
 | 
			
		||||
        $pass = $this->test_input($pass);
 | 
			
		||||
        $repass = $this->test_input($repass);
 | 
			
		||||
 | 
			
		||||
        $name = strtolower($name);
 | 
			
		||||
        $username = isset($name) ? $name : null ;
 | 
			
		||||
        if (!$username || mb_strlen($username) < 3 || mb_strlen($username) > 8) {
 | 
			
		||||
          $register = false;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $email = isset($mail) ? $mail : null ;
 | 
			
		||||
        if (!$email || !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $email)) {
 | 
			
		||||
          $register = false;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $password = isset($pass) ? $pass : null ;
 | 
			
		||||
        $repassword = isset($repass) ? $repass : null ;
 | 
			
		||||
        if (!$password || mb_strlen($password) < 4) {
 | 
			
		||||
            $register = false;
 | 
			
		||||
        }
 | 
			
		||||
        if(!$repassword || mb_strlen($repassword) < 4){
 | 
			
		||||
            $register = false;
 | 
			
		||||
        }
 | 
			
		||||
        if($password != $repassword){
 | 
			
		||||
            $register = false;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        if ($register) {
 | 
			
		||||
            $bd = new UserDAO('complucine');
 | 
			
		||||
            if($bd){
 | 
			
		||||
                try{
 | 
			
		||||
                    $this->user = $bd->selectUser($username, $password);
 | 
			
		||||
                    if (!$this->user) {
 | 
			
		||||
                        $bd->createUser("", $username, $email, $password, "user");
 | 
			
		||||
                        $this->user = $bd->selectUser($username, $password);
 | 
			
		||||
                        if ($this->user) {
 | 
			
		||||
                            $this->user->setPass(null);
 | 
			
		||||
                            $_SESSION["user"] = serialize($this->user);
 | 
			
		||||
                            $_SESSION["nombre"] = $this->user->getName();
 | 
			
		||||
                            $_SESSION["rol"] = $this->user->getRol();
 | 
			
		||||
                            $_SESSION["login"] = $register;
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    else{
 | 
			
		||||
                        $_SESSION["login"] = false;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                catch (Exception $e){
 | 
			
		||||
                    $_SESSION["login"] = $register;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return $reply;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function test_input($input){
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user