<?php 
session_start();
/*
 * Registry/uUpdateRegistration.php
 * 
 * Copyright 2009, 2017 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');
require($entradaFILE.'menuHome.php');

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

?>





<center>
<h3 class="maketitle">Geometric theorems - Update the Users Info</h3>
</center>

<hr/>



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

// Obtêm os valores do formulário.
$vName           = $_POST['name'];
$vAffiliation    = $_POST['affiliation'];
$vURL            = $_POST['url'];
$vEmail          = $_POST['email'];
$vType           = $_POST['type'];
$vDateSubmission = $_POST['dateSubmission'];


// Valida o preenchimento dos campos obrigatórios.
if (!isset($_POST['email']) || ($vEmail == '')) {
  echo "<p>error - email not filled</p>";
  exit;
 }


if (!preg_match("#^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}#", $vEmail,$matches)) {
    echo "<p>error - incorrect email address - $vEmail</p>";
    exit;
}

// Formata os valores para posterior inserção na Tabela
if (!get_magic_quotes_gpc()) { 
  $vName           = addslashes($vName);
  $vAffiliation    = addslashes($vAffiliation);
  $vURL            = addslashes($vURL);	 
  $vEmail          = addslashes($vEmail);
  $vType           = addslashes($vType);
  $vDateSubmission = addslashes($vDateSubmission);
 }

$sqlUpdateUserInfo = 
    "UPDATE users
        SET name           = '$vName',       
            affiliation    = '$vAffiliation',       
            url            = '$vURL',      
            dateSubmission = '$vDateSubmission',
            email          = '$vEmail'
       WHERE
             username = '$vUsername'";

$resultadoUpdateUserInfo = mysqli_query($ligacao,$sqlUpdateUserInfo);

if ($resultadoUpdateUserInfo) {
  echo 'Information Updated.'; 
  // Se houve mudança de tipo envia uma mensagem para mim
  $sqlUserType = "SELECT type 
                    FROM users
                   WHERE username='$vUsername'";

  $resultadoUserType = mysqli_query($ligacao,$sqlUserType);

  $linha = mysqli_fetch_assoc($resultadoUserType);

  $vTypeOrig = $linha['type'];

  if ($vType!=$vTypeOrig) {
    // enviar mensagem para mim.
    ob_start();
    echo "The User $vName ($vUsername)\n";
    echo "wants to change it status\n";
    echo "from $vTypeOrig to $vType\n"; 
    
    $messageToAdmin = ob_get_contents();
    $from = "From: geothms@hilbert.mat.uc.pt";
    mail('pedro@mat.uc.pt', 'GeoThms - change of status', $messageToAdmin, $from); 
    ob_end_clean();
    // Se a mudança é Contributer -> Regular faz-se a modificação
    if ($vType=='Regular') {
      $sqlUpdateUserType = 
	"UPDATE users
            SET type = '$vType',       
          WHERE username = '$vUsername'";

      $resultadoUpdateUserType = mysqli_query($ligacao,$sqlUpdateUserType);
      if (!$resultadoUpdateUserInfo) {
	echo "Error: type not modified";
      }
    }
    else { // Senão envia-se mensagem ao utilizador (para mim já foi)
      ob_start();
      echo "You have solicited changing\n";
      echo "from $vTypeOrig to $vType\n\n";
      echo "The administrator will do this change soon\n";
      $messageToOldUser = ob_get_contents();    
      $from="From: geothms@hilbert.mat.uc.pt";
      mail($vEmail, 'Changing status in GeoThms', $messageToOldUser,$from);
      ob_end_clean();
      // e volta a colocar o tipo a "Regular"
      $vType="Regular";
    }
  }
}
else {
    echo "Error - User's Data not updated!";
}

if ($vType=='Contributer') {
    echo "<meta http-equiv='refresh' content='1; url=".$entradaURL."contributers.php?name=$vName&type=$vType' />";
}
else {
    echo "<meta http-equiv='refresh' content='1; url=".$entradaURL."users.php?name=$vName&type=$vType' />";
}

?>

<hr/>

<p>&nbsp;</p>

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