<?php 
session_start();
/*
 * cChangepasswd.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.'header.php');

$putilizador     = $_SESSION['utilizador'];
$psenha          = $_SESSION['senha'];
$pTipoUtilizador = $_SESSION['tipoUtilizador'];
$vUsername       = $_SESSION['username'];

require($entradaFILE.'dbLogin.php');
?>


<?php

// Vai obter alguns dados à base de dados  
$sql = "SELECT name,type 
          FROM users
         WHERE username='$vUsername'";

$resultado = mysqli_query($ligacao,$sql);

$linha = mysqli_fetch_assoc($resultado);

$vName        = $linha['name'];
$vType        = $linha['type'];
?>


<center>
<h3 class="maketitle">Change Password</h3>
</center>

<hr/>

<?php

// create short variable names
$newPasswd = $_POST['newPasswd'];
$newPasswd2 = $_POST['newPasswd2'];
 

// verifica o preenchimento das novas senhas
if (!isset($_POST['newPasswd']) || ($newPasswd == '')) {
  echo "<p>error - Password not filled</p>";
  echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."Registry/changepasswd.php' />";
 } 
elseif (!isset($_POST['newPasswd2']) || ($newPasswd2 == '')) {
  echo "<p>error - Password confirmation not filled</p>";
  echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."Registry/changepasswd.php' />";
} 
// verifica se as senha são iguais
elseif ($newPasswd!=$newPasswd2) {
  echo 'Passwords entered were not the same.  Not changed.';
  echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."Registry/changepasswd.php' />";
}
// verifica se têem o tamanho correcto
elseif (strlen($newPasswd)>16 || strlen($newPasswd)<6) {
  echo "New password must be between 6 and 16 characters. Try again.";
  echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."Registry/changepasswd.php' />";
}
 else {

   // attempt update
   if (!get_magic_quotes_gpc()) {
     $newPasswd  = addslashes($newPasswd);
     $newPasswd2 = addslashes($newPasswd2);
   }
   
   $sqlChangePasswd = 
     "UPDATE users
      SET passwd = sha1('$newPasswd')
    WHERE username = '$vUsername'";
   
   $resultadoChangePasswd = mysqli_query($ligacao,$sqlChangePasswd);
   
   if ($resultadoChangePasswd) {
     echo 'Password changed.'; 
     // depois de modificar a senha termina-se a sessão de forma
     // a que o utilizador entre já com a senha nova
     echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."logout.php' />";
   }
   else {
     echo 'Error: password not modified';
     
     if ($vTipoUtilizador=='administrator') // utilizador do MySQL
       echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."admins.php?name=$vName&type=$vType' />";
     elseif ($vType=='anonymous') 
       echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."users.php?name=$vName&type=$vType' />";
     else { // o Administrator do GeoThms não devia estar aqui.
       echo "ERROR";
       echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."logout' />";
     }
   }
 }
?>



<p>&nbsp;</p>

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