<?php
session_start();
/*
 * Administation/tTransformUsers.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");

$pTipoUtilizador = $_SESSION['tipoUtilizador'];
if ($pTipoUtilizador=="anonymous" || $pTipoUtilizador=="regular") { // separa por tipo de utilizador
  require($entradaFILE.'headerUsers.php');
  require($entradaFILE.'menuAdmins.php');
  echo "<meta http-equiv='refresh' content='0; url=".$entradaURL."users.php' />";
  exit;  // os anonimos não têem acesso a esta página
}
else {
  require($entradaFILE.'headerAdmins.php');
  require($entradaFILE.'menuAdmins.php');
}

// coloca o valor para a página de ajuda apropriada
$_SESSION['helpsection']='logs';

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

?>

<hr/>

<center>
<h3 class="maketitle">Transform the status of a User</h3>
</center>

<hr/>

<?php
$vName      = $_POST['Name'];
$vEmail     = $_POST['Email'];
$vUsername  = $_POST['Username'];
$vTransType = $_POST['TransType'];

$from="From: geothms@hilbert.mat.uc.pt";

if ($vTransType=="RegToContrib") 
  $vType="Contributer";
else
  $vType="Regular";

if ($vTransType=='ContribToReg') {
  echo "<h2>From Contributer to Regular User</h2>";
  
  $vType=addslashes($vType);

  $sqlContribToUser = "UPDATE users
                          SET type = '$vType'
                        WHERE username = '$vUsername'";

  $resultadoContribToUser = mysqli_query($ligacao,$sqlContribToUser);

  if ($resultadoContribToUser) {
    // enviar mensagem para mim.
    ob_start();
    echo "The status of $vName ($vUsername)\n";
    echo "has been changed  by the Administrator\n";
    echo "from Contributer to Regular User\n"; 
    
    $messageToAdmin = ob_get_contents();
    
    mail('pedro@mat.uc.pt', 'GeoThms - change of status', $messageToAdmin, $from); 
    ob_end_clean();
    
    ob_start();
    echo "Dear $vName ($vUsername)\n\n";
    echo "Yous status in GeoThms has been changed from\n";
    echo "Contributer to Regular User\n\n"; 
    echo "Best Regards\n";
    
    $messageToNewUser = ob_get_contents();    
    mail($vEmail, 'Change of status in GeoThms', $messageToNewUser, $from);
    ob_end_clean();
    echo '<h2>Status Changed.</h2>'; 
    echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."admins.php' />";
  }
  else
    echo 'Error - Status not updated!';
 }
 else {
   echo "<h2>From Regular User to Contributer</h2>";

  $vType=addslashes($vType);

  $sqlContribToUser = "UPDATE users
                          SET type = '$vType'
                        WHERE 
                              username = '$vUsername'";

  $resultadoContribToUser = mysqli_query($ligacao,$sqlContribToUser);
 
  if ($resultadoContribToUser) {
    // enviar mensagem para mim.
    ob_start();
    echo "The status of $vName ($vUsername)\n";
    echo "has been changed  by the Administrator\n";
    echo "from Regular User to Contributer \n"; 
    
    $messageToAdmin = ob_get_contents();
    
    mail('pedro@mat.uc.pt', 'GeoThms - change of status',$messageToAdmin,$from); 
    ob_end_clean();
    
    ob_start();
    echo "Dear $vName ($vUsername)\n\n";
    echo "Yous status in GeoThms has been changed from\n";
    echo "Regular User to Contributer\n\n"; 
    echo "Best regards\n";
    
    $messageToNewUser = ob_get_contents();    
    mail($vEmail, 'Change of status in GeoThms', $messageToNewUser,$from);
    ob_end_clean(); 
    echo '<h2>Status Changed.</h2>'; 
    echo "<meta http-equiv='refresh' content='2; url=".$entradaURL."admins.php' />";
  }
  else
    echo 'Error - Status not updated!';
 }
//echo "</tr> \n";
//echo "<tr>\n";
//echo "</table>\n";
?>

<hr/>

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