Example #1
0
    e-valUAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with e-valUAM.  If not, see <http://www.gnu.org/licenses/>.
-->

<?php 
include 'funciones.php';
if (isset($_POST['token']) && isset($_POST['email']) && isset($_POST['nueva'])) {
    $con = connect() or die('Error. Prueba de nuevo más tarde');
    $result = pg_query_params($con, "\n\t\t\tSELECT nombre\n\t\t\tFROM alumnos\n\t\t\tWHERE nombre =  \$1\n\t\t\tAND token_creation + interval '3 hours' > NOW()\n\t\t\tAND token = \$2\n\t\t\t", array($_POST['email'], $_POST['token']));
    if (pg_num_rows($result) == 1) {
        $salt = md5(devurandom_rand());
        $hashed_password = crypt($_POST['nueva'], $salt);
        if (crypt($_POST['nueva'], $hashed_password) == $hashed_password) {
            $result = pg_query_params($con, "UPDATE alumnos SET pass = \$1, token = NULL, token_creation = NULL WHERE nombre = \$2", array($hashed_password, $_POST['email']));
            if (pg_affected_rows($result) == 1) {
                set_mensaje('ok', 'Tu contraseña se ha cambiado. Ya puedes usarla.');
                header('Location: index.php');
                exit;
            }
        }
    }
    set_mensaje('error', 'No se ha podido completar tu solicitud. Repite el proceso.');
    header('Location: recuperarContrasenya.php');
    exit;
}
?>
-->

<?php 
include 'funciones.php';
$error_captcha = FALSE;
// Si llegamos a la página con el POST, es que se ha rellenado ya el formulario
if (isset($_POST['email']) && isset($_POST['g-recaptcha-response'])) {
    if (verificar_captcha()) {
        // Hemos validado el captcha correctamente
        $con = connect() or die('Error. Prueba de nuevo más tarde');
        if ($con) {
            // Comprbamos en la base de datos que hay un usuario con ese email
            $result = pg_query_params($con, 'SELECT * FROM alumnos WHERE nombre =  $1', array($_POST['email'])) or die('Error. Prueba de nuevo más tarde.');
            if (pg_num_rows($result) == 1) {
                // Si el usuario está en la base de datos, generamos un token aleatorio
                $token = md5(devurandom_rand() . $_POST['email'] . time());
                // Lo guardamos en la BD
                $result = pg_query_params($con, 'UPDATE alumnos SET token = $1, token_creation = now() WHERE nombre = $2', array($token, $_POST['email']));
                if (pg_affected_rows($result) != 1) {
                    die('Error. Prueba de nuevo más tarde.');
                }
                $message = '<html><head><title>Recuperación de contraseña</title></head>
							<body>
								<p>Recientemente has solicitado una nueva contraseña para tu cuenta de e-valUAM.</p>
								<p>En el siguiente enlace podrás recuperar tu contraseña:
								<a href="https://e-valuam.ii.uam.es/token.php?token=' . urlencode($token) . '">
								https://e-valuam.ii.uam.es/token.php?token=' . urlencode($token) . '</a></p>
								<p>Si no has sido tú, sencillamente ignora este mensaje.</p>
							</body>
						    </html>';
                $mensaje_plano = 'Recientemente has solicitado una nueva contraseña para tu cuenta de e-valUAM.