<?php
require 'include/head.inc';
require 'include/prefs.inc';

if(isset($_POST['submit']) && $_POST['firstname']!="" && $_POST['lastname']!="" 
			   && $_POST['mnumber']!="" && $_POST['email']!="" && $_POST['password']!="")
{
  $firstname = $_POST['firstname'];
  $lastname = $_POST['lastname'];
  $mnumber = $_POST['mnumber'];
  $email = $_POST['email'];
  $password = $_POST['password'];

  require 'include/db.inc';

  // user already exists //
  if($db->query("SELECT COUNT(*) FROM students WHERE mnumber = $mnumber")->fetch(PDO::FETCH_NUM)[0] == 1) {
    print "<table bgcolor=\"red\">";
      print "<tr><th><b><font color=\"white\">user already registered</font></b></th><tr>";
    print "</table>";
    exit;
  }

  $db->query("INSERT INTO students (mnumber,firstname,lastname,email,password) 
        VALUES (\"$mnumber\",\"$firstname\",\"$lastname\",\"$email\",\"$password\")");

  print "<table bgcolor=\"lightgreen\">";
    print "<tr><th><b><font color=\"white\">registration successfull</font></b></th><tr>";
  print "</table>";

  echo "$firstname $lastname was successfully registered.<br>";

  // send password-email //
  $nachricht = "Hello $firstname($mnumber)!\nYou registered for " . $PREFS['exname'] . " (" . $PREFS['groupname'] . ").\nRemember your password: $password\n\nYours,\nBanana.";
  $empfaenger = 'niemand@example.com';
  $betreff = 'Der Betreff';
  $header = 'From: banana@treefish.org' . "\r\n" .
    'Reply-To: noreply@treefish.org' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
  mail($email, "Banana Registration: " . $PREFS['exname'] . " (" . $PREFS['groupname'] . ")", $nachricht, $header);

  exit;
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <table bgcolor="black">
    <tr><th><b><font color="white">registration</font></b></th><tr>
  </table>

  <table>
    <tr>  
      <th align="left">First name:</th> <th><input type="text" name="firstname"></th>
    </tr>
    <tr>  
      <th align="left">Last name:</th> <th><input type="text" name="lastname"></th>
    </tr>
    <tr>  
      <th align="left">Mat.nummer:</th> <th><input type="text" name="mnumber"></th>
    </tr>
    <tr>  
      <th align="left">Email:</th> <th><input type="text" name="email"></th>
    </tr>
    <tr>  
      <th align="left">Password:</th> <th><input type="text" name="password"></th>
    </tr>
  
    <tr>
      <th></th> <th align="right"><input type="submit" name="submit" value="Register"></th>
    </tr>
  </table>
</form>