<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Portal: Registrieren</title>
</head>
<body>
    <h2>Registrieren</h2>
    <form>
        <label>E-Mail: <input name="email"></label><br>
        <label>Passwort: <input type="password" name="pw" > </label>
        <button type="submit">Registrieren</button>
    </form>
</body>
</html>


<?php 
require_once '../inc/db_connection_function.php';
require_once 'functions.php';
$dbh = db_connect();
$errors = '';

if(!empty($_REQUEST)) { // Button wurde gedrückt?
    if($_REQUEST['email'] != '' AND $_REQUEST['pw'] != ''){ // Alle Felder gedrückt?
        $email = $_REQUEST['email'];
        $pw = $_REQUEST['pw'];

        if(checkPasswordStrength($pw) AND isValidEmail($email)){
            $pw = password_hash($pw, PASSWORD_DEFAULT);

            $query = "INSERT INTO user VALUES (NULL, :email, :pw)";
            $stmt = $dbh->prepare($query);

            $stmt->bindParam(':email', $email);
            $stmt->bindParam(':pw', $pw);

            try {
                $stmt->execute();
                $errors = "Alles malocht! :)";
            } catch(PDOException $e){
                $errMsg = $e->getMessage();
                $errCode = $e->getCode();

                echo $e;

                switch($errCode) {
                    case "23000": $custErrMsg = "<p>Email-Adress already exists!</p>"; break;
                    default: $custErrMsg = "<p>Oooops, something went wrong!</p>";
                }
                echo $custErrMsg;
            }
        } else {
            $errors .= "Passwort zu schwach oder Email ist ungültig !<br>";
        }
    } else {
        $errors .= "Beide Felder müssen ausgefüllt werden<br>";
    } // Ende Felder müssen ausgefüllt werden.
}

if ($errors != "") echo $errors;