From 76627d27de3252ecff759f46ca0c0478b23db178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20M=C3=A9ndez?= <45081533+FerMdez@users.noreply.github.com> Date: Tue, 13 Apr 2021 17:20:46 +0200 Subject: [PATCH] Add files via upload --- login/includes/formLogin.php | 103 ++++++++++++++++++++ login/includes/user_dao.php | 85 +++++++++++++++++ login/includes/user_dto.php | 37 ++++++++ login/includes/users_dto_interface.php | 14 +++ login/index.php | 58 ++++++++++++ login/login_register_view.php | 125 +++++++++++++++++++++++++ login/validate.php | 59 ++++++++++++ 7 files changed, 481 insertions(+) create mode 100644 login/includes/formLogin.php create mode 100644 login/includes/user_dao.php create mode 100644 login/includes/user_dto.php create mode 100644 login/includes/users_dto_interface.php create mode 100644 login/index.php create mode 100644 login/login_register_view.php create mode 100644 login/validate.php diff --git a/login/includes/formLogin.php b/login/includes/formLogin.php new file mode 100644 index 0000000..4fc1eae --- /dev/null +++ b/login/includes/formLogin.php @@ -0,0 +1,103 @@ +reply = array(); + } + + //Methods: + + //Returns validation response: + public function getReply() { + + if(isset($_SESSION["login"])){ + $name = strtoupper($_SESSION['nombre']); + $this->reply = "

Bienvenido {$_SESSION['nombre']}


+

{$name}, has iniciado sesión correctamente.

+

Usa los botones para navegar

+ + \n"; + } + else if(!isset($_SESSION["login"])){ + $this->reply = "

ERROR


". + "

El usuario o contraseña no son válidos.

+

Vuelve a intetarlo o regístrate si no lo habías hecho previamente.

+ +
\n"; + } + + return $this->reply; + } + + //Process form: + public function processesForm($name, $pass) { + $login = true; + $name = $this->test_input($name); + $pass = $this->test_input($pass); + + $username = isset($name) ? $name : null ; + if (!$username) { + $login = false; + } + + /* + $email = isset($mail) ? $mail : null ; + if (!$email || !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $email)) { + $login = false; + } + */ + + $password = isset($pass) ? $pass : null ; + if (!$password || mb_strlen($password) < 4) { + $login = false; + } + + if ($login) { + $bd = new UserDAO('complucine'); + if($bd){ + $selectUser = $bd->selectUser($username); + $selectUser->data_seek(0); + while ($fila = $selectUser->fetch_assoc()) { + if($username === $fila['username'] && $bd->verifyPass($password, $fila['passwd'])){ + $this->user = $bd->loadUser($fila['id'], $fila['username'], $fila['email'], $fila['passwd'], $fila['rol']); + } + } + + try{ + if ($this->user) { + $_SESSION['user'] = $this->user; + $_SESSION["nombre"] = $this->user->getName(); + $_SESSION["login"] = $login; + $_SESSION["rol"] = $this->user->getRol(); + } + } + catch (Exception $e){ + $_SESSION["login"] = $login; + } + + mysqli_free_result($selectUser); + //$selectUser->free(); + } + + } + + } + + protected function test_input($input){ + return htmlspecialchars(trim(strip_tags($input))); + } + +} +?> \ No newline at end of file diff --git a/login/includes/user_dao.php b/login/includes/user_dao.php new file mode 100644 index 0000000..62ddc36 --- /dev/null +++ b/login/includes/user_dao.php @@ -0,0 +1,85 @@ +encryptPass($password); + + $sql = sprintf( "INSERT INTO users( id, username, email, passwd, rol) + VALUES ( '%s', '%s', '%s', '%s', '%s')", + $id, $username, $email, $password, $rol ); + + $resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database'); + + return $resul; + } + + //Returns a query to check if the user name exists. + public function selectUser($username){ + $username = $this->mysqli->real_escape_string($username); + + $sql = sprintf( "SELECT * FROM users WHERE username = '%s'", $username ); + $resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database'); + + return $resul; + } + + //Returns a query to check if the user pass matches. + public function selectPass($username, $password){ + $username = $this->mysqli->real_escape_string($username); + $password = $this->mysqli->real_escape_string($password); + $password = $this->encryptPass($password); + + $sql = sprintf( "SELECT * FROM users WHERE username = '%s' AND passwd = '%s'", $username, $password); + $resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database'); + + //return $this->mysqli->query($sql); + return $resul; + } + + //Returns a query to get the user's data. + public function userData($id){ + $sql = sprintf( "SELECT * FROM users WHERE id = '%d'", $id ); + $resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database'); + + return $resul; + } + + //Create a new User Data Transfer Object. + public function loadUser($id, $username, $email, $password, $rol){ + return new UserDTO($id, $username, $email, $password, $rol); + } + + } + +?> \ No newline at end of file diff --git a/login/includes/user_dto.php b/login/includes/user_dto.php new file mode 100644 index 0000000..18e7a4e --- /dev/null +++ b/login/includes/user_dto.php @@ -0,0 +1,37 @@ + Será eliminado en la siguiente práctica para usar el modelo relacional de nuestra BD. + + //Constructor: + function __construct($id, $username, $email, $password, $rol){ + $this->_id = $id; + $this->_username = $username; + $this->_email = $email; + $this->_password = $password; + $this->_rol = $rol; + } + + //Methods: + + //Getters && Setters: + public function setId($id){ $this->_id = $id; } + public function getId(){ return $this->_id; } + public function setName($username){ $this->_username = $username; } + public function getName(){ return $this->_username; } + public function setEmail($email){ $this->_email = $email; } + public function getEmail(){ return $this->_email; } + public function setPass($passwd){ $this->_password = $passwd; } + public function getPass(){ return $this->_password; } + public function setRol($rol){ $this->_rol = $rol; } + public function getRol(){ return $this->_rol; } + + } +?> \ No newline at end of file diff --git a/login/includes/users_dto_interface.php b/login/includes/users_dto_interface.php new file mode 100644 index 0000000..4705352 --- /dev/null +++ b/login/includes/users_dto_interface.php @@ -0,0 +1,14 @@ + \ No newline at end of file diff --git a/login/index.php b/login/index.php new file mode 100644 index 0000000..eb1c5ee --- /dev/null +++ b/login/index.php @@ -0,0 +1,58 @@ + +getIsLogin(); + $login = $view->getLogin(); + $register = $view->getRegister(); +?> + + + + print_head(); + ?> + + + print_header(); + ?> + + +
+
+

Acceso

+
+
+ + +
+
+ +
+
+ + + print_footer(); + ?> + + + + diff --git a/login/login_register_view.php b/login/login_register_view.php new file mode 100644 index 0000000..054299a --- /dev/null +++ b/login/login_register_view.php @@ -0,0 +1,125 @@ +setIsLogin(true); + + if(array_key_exists('register', $_POST)){ + $this->setIsLogin(false); + } + else if(array_key_exists('login', $_POST)){ + $this->setIsLogin(true); + } + + $this->initLoginRegister(); + + } + + //Methods: + private function setIsLogin($set){ + return $this->isLogin = $set; + } + + public function getIsLogin(){ + return $this->isLogin; + } + + public function getLogin(){ + return $this->login; + } + + public function getRegister(){ + return $this->register; + } + + private function initLoginRegister(){ + + $this->register = ' +
+

Registro

+
+
+
+ Datos personales +
+ +
+
+ +
+
+ +
+
+ +
+
+
+ + +
+
+ + +
+
+
+
+
+
+

¿Ya estás registrado?

+
+

Si dispones de una cuenta de usuario, no es necesario que rellenes este formulario nuevamente

+

Haz click en el botón para iniciar sesión.

+
+ +
+
+
'."\n"; + + $this->login = ' +
+
+

¿No tienes una cuenta?

+
+

Para crear una cuenta de usuario es necesario haber rellenado el formulario de registro previamente

+

Haz click en el botón para registrate.

+
+ +
+
+
+
+

Iniciar Sesión

+
+
+
+ Datos personales +
+ +
+ +
+ +
+
+
+ + +
+
+
+
'."\n"; + } + } +?> \ No newline at end of file diff --git a/login/validate.php b/login/validate.php new file mode 100644 index 0000000..d2bd37b --- /dev/null +++ b/login/validate.php @@ -0,0 +1,59 @@ +get_prefix(); + + //Login form validate: + require_once('./includes/formLogin.php'); + $login = new FormLogin(); + $login->processesForm($_POST["name"], $_POST["pass"]); + $reply = $login->getReply(); + +?> + + + + + print_head(); + ?> + + + print_header(); + ?> + + +
+
+
+ + +
+
+
+
+
+ +
+
+
+
+
+ + + print_footer(); + ?> + + + + \ No newline at end of file