SW/panel_user/includes/formChangeEmail.php

95 lines
5.0 KiB
PHP
Raw Normal View History

2021-05-03 15:11:59 +02:00
<?php
2021-05-09 16:27:21 +02:00
require_once('../assets/php/form.php');
include_once('../assets/php/common/user.php');
2021-05-03 15:11:59 +02:00
2021-05-09 16:27:21 +02:00
class FormChangeEmail extends Form {
2021-05-09 16:46:12 +02:00
//Constants:
const HTML5_EMAIL_REGEXP = '^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$';
2021-05-09 16:27:21 +02:00
public function __construct() {
$options = array("action" => "./?option=manage_profile");
parent::__construct('formChangeUserEmail', $options);
}
protected function generaCamposFormulario($datos, $errores = array()){
2021-05-11 17:36:06 +02:00
$email = $datos['email'] ?? '';
2021-05-09 16:27:21 +02:00
// Se generan los mensajes de error si existen.
$htmlErroresGlobales = self::generaListaErroresGlobales($errores);
2021-05-10 12:30:15 +02:00
$errorEmail = self::createMensajeError($errores, 'new_email', 'span', array('class' => 'error'));
$errorEmail2 = self::createMensajeError($errores, 'remail', 'span', array('class' => 'error'));
2021-05-09 16:27:21 +02:00
$errorPassword = self::createMensajeError($errores, 'pass', 'span', array('class' => 'error'));
2021-05-10 12:30:15 +02:00
$html = "<div class='row'>
<fieldset id='email_usuario'><pre>".$htmlErroresGlobales."</pre>
2021-05-09 20:31:54 +02:00
<legend>Nuevo email de usuario</legend>
2021-05-10 12:30:15 +02:00
<input type='text' name='new_email' id='new_email' value='' placeholder='Nuevo Email' required/><pre>".$errorEmail."</pre>
<input type='text' name='remail' id='remail' value='' placeholder='Repita el email' required/><pre>".$errorEmail2."</pre>
<input type='password' name='pass' id='pass' value='' placeholder='Contraseña' required/><pre>".$errorPassword."</pre>
2021-05-09 20:31:54 +02:00
</fieldset>
2021-05-10 12:30:15 +02:00
<div class='actions'>
<input type='submit' id='submit' value='Cambiar Email de Usuario' class='primary' />
<input type='reset' id='reset' value='Borrar' />
2021-05-09 20:31:54 +02:00
</div>
2021-05-10 12:30:15 +02:00
</div>";
2021-05-09 16:27:21 +02:00
return $html;
}
protected function procesaFormulario($datos){
$result = array();
2021-05-12 10:40:22 +02:00
$email = $this->test_input($datos['new_email']) ?? null;
2021-05-09 16:46:12 +02:00
if ( empty($email) || !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $email) ) {
$result['new_email'] = "El nuevo email no es válido.";
2021-05-09 16:27:21 +02:00
}
2021-05-10 12:30:15 +02:00
2021-05-12 10:40:22 +02:00
$email2 = $this->test_input($datos['remail']) ?? null;
2021-05-10 12:30:15 +02:00
if ( empty($email2) || strcmp($email, $email2) !== 0 ) {
$result['remail'] = "Los emails deben coincidir";
}
2021-05-09 16:27:21 +02:00
2021-05-12 10:40:22 +02:00
$password = $this->test_input($datos['pass']) ?? null;
2021-05-09 21:59:52 +02:00
if ( empty($password) || mb_strlen($password) < 4 ) {
2021-05-10 11:52:45 +02:00
$result['pass'] = "El password tiene que tener\n una longitud de al menos\n 4 caracteres.";
2021-05-09 16:27:21 +02:00
}
if (count($result) === 0) {
$bd = new UserDAO("complucine");
$user = $bd->selectUser(unserialize($_SESSION['user'])->getName(), $password);
if (!$user) {
$result[] = "El usuario no existe.";
2021-05-09 21:59:52 +02:00
$_SESSION['message'] = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1>Ha ocurrido un probrema</h1><hr />
2021-05-10 12:30:15 +02:00
<p>No hemos podido actualizar su email de usuario.
Comprueba que la contraseña introducida sea correcta.</p>
2021-05-09 21:59:52 +02:00
<a href=''><button>Cerrar Mensaje</button></a>
</div>
</div>
<div class='column side'></div>
</div>
";
2021-05-10 12:30:15 +02:00
} else {
$bd->changeUserEmail(unserialize($_SESSION['user'])->getId(), $email);
2021-05-09 16:46:12 +02:00
$_SESSION['message'] = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1>Operacion realizada con exito</h1><hr />
<p>Se ha modificado su email correctamente.</p>
<a href=''><button>Cerrar Mensaje</button></a>
</div>
</div>
<div class='column side'></div>
</div>
";
2021-05-09 16:27:21 +02:00
$result = './?option=manage_profile';
}
}
return $result;
}
}
2021-05-03 15:11:59 +02:00
?>