FaZeBookSocialNetwork / www / src / FaZeBook / Page / Register.php
Register.php
Raw
<?php

namespace FaZeBook\Page;

require_once(__DIR__.'/../../Application/Page/PageInterface.php');

use Application\Page\PageInterface;
use FaZeBook\Data;
use FaZeBook\Session;

class Register implements PageInterface {
    
    public function getTitle()
    {
        return 'Registrierung';
    }

    public function getId()
    {
        return "register";
    }

    public function hasNavigationItem()
    {
        return false;
    }

    public function getViewScript()
    {
        return __DIR__.'/../../../view/login/register.php';
    }

    public function getViewVariables()
    {
        $wasRegistrationAttempted = $this->wasRegistrationAttempted();
        $registrationAttempt = ($wasRegistrationAttempted ? $this->registrationAttempt() : false);

        return [
            'isLoggedIn' => Session::getInstance()->isLoggedIn(),
            'wasRegistrationAttempted' => $wasRegistrationAttempted,
            'registrationAttempt' => $registrationAttempt
        ];
    }

    private function wasRegistrationAttempted() : bool {
        return isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['email']) && isset($_POST['password']);
    }

    private function registrationAttempt() : bool {
        $firstname = htmlspecialchars($_POST['firstname']);
        $lastname = htmlspecialchars($_POST['lastname']);
        $email = htmlspecialchars($_POST['email']);
        $password = $_POST['password'];

        if($_POST['password'] != $_POST['password2']){
            return false;
        }

        if (Data::getInstance()->getUserByEmail($email) == null) {
            // TODO : further checks required?
            Data::getInstance()->addUser($firstname, $lastname, $email, $password);
            return true;
        } else {
            // email already exists
            return false;
        }

    }
}