<?php 
/*
 * forgotPasswd.php
 * 
 * Copyright 2009, 2018 Pedro Quaresma <pedro@mat.uc.pt>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

require_once("servidor.php");

require($entradaFILE.'headerHome.php');

require_once($entradaFILE.'auxFunctions.php');

?>

<center>
<h2 class="maketitle">GeoThms - Geometry Framework</h2>
</center>

<hr/>

<?php
// creating short variable name
$username = $_POST['username'];

// set password for username to a random value
// return the new password or false on failure
 
// get a random dictionary word b/w 6 and 13 chars in length
$new_password = get_random_word(6, 13);
  
if($new_password==false) {
  echo 'Could not generate new password.';
  exit;
 }
 else {
   // add a number  between 0 and 999 to it
   // to make it a slightly better password
   srand ((double) microtime() * 1000000);
   $rand_number = rand(0, 999); 
   $new_password .= $rand_number;
   
   // set user's password to this in database or return false
   require($entradaFILE.'dbLogin.php');

   $sqlUpdateUser = "UPDATE users
                        SET passwd = sha1('$new_password')
                      WHERE username = '$username'";
   $resultadoUpdateUser = mysqli_query($ligacao,$sqlUpdateUser);
   if (!$resultadoUpdateUser) {
     echo 'Your password could not be reset - please try again later.';
     exit;
   }
   // sem erros na actualização da senha
   // notify the user that their password has been changed
   $sqlEmailUser = "SELECT email 
                      FROM users
                     WHERE username='$username'";
   $resultadoEmailUser = mysqli_query($ligacao,$sqlEmailUser);
   if (!$resultadoEmailUser) {
     echo 'Could not find email address.';  
     exit;
   }
   $nlinhas = mysqli_num_rows($resultadoEmailUser);
   if ($nlinhas==0) {
     echo 'Could not find email address.';   // username not in db
     exit;
   }
   $linha = mysqli_fetch_assoc($resultadoEmailUser);
   $pEmail = $linha['email'];
   $from = "From: geothms@hilbert.mat.uc.pt\r\n";
   $mesg = "Your GeoThms password has been changed to $new_password \r\n"
     ."Please change it next time you log in. \r\n";
   if (mail($pEmail, 'GeoThms login information', $mesg, $from))
     echo '<h3>Your new password has been sent to your email.<h3>';
   else {
     echo 'Could not send email.';
     exit;
   }
 }

echo "<meta http-equiv='refresh' content='5; url=".$entradaFILE."index.php' />";
?>

<hr/>

<p>&nbsp;</p>

<?php
require($entradaFILE.'footer.php');
?>
