summaryrefslogtreecommitdiff
path: root/includes/form/RegisterForm.php
blob: c1c1966bf92dbd0413652e54d38d62fbb7617620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php

include_once './includes/form/Form.php';
include_once './includes/model/User.php';

class RegisterForm extends Form
{
	const USERNAME_REGEX = "/^[a-zA-Z0-9_]*$/";
	const PASSWORD_REGEX = "/^[a-zA-Z0-9\W]*$/";

    public function validate_username($username): ?string
    {
		$result = null;

    	if (empty($username)) {
			$this->report_error("You must provide a username.");
		} else {
			$result = filter_var($username, FILTER_SANITIZE_STRING);

			if (!preg_match(self::USERNAME_REGEX, $result)) {
				$this->report_error("Username can only contain letters, numbers and underscores.");
			}

			if (strlen($result) > 30) {
				$this->report_error("Your username must be 30 characters or less.");
			}

			if (User::username_exists($result)) {
				$this->report_error("The username '" . $result . "' has already been taken by another user.");
			}
		}
    	return $result;
    }

    public function validate_password($password, $password_check): ?string
    {
		$result = null;

    	if (empty($password)) {
			$this->report_error("You must provide a password.");
		} else {
			$result = filter_var($password, FILTER_SANITIZE_STRING);
			$pass_check = filter_var($password_check, FILTER_SANITIZE_STRING);

			if (preg_match(self::PASSWORD_REGEX, $result) === false) {
				$this->report_error("Password contains invalid characters!");
			}

			if ($result !== $pass_check) {
				$this->report_error("The two passwords do not match.");
			}
		}

    	return $result;
    }

	public function validate_invite_code($invite_code): ?string
	{
		$result = null;

		if (empty($invite_code)) {
			$this->report_error("You need an invite code to register an account.");
		} else {
			$result = filter_var($invite_code, FILTER_SANITIZE_STRING);

			if (!User::invite_code_exists($result)) {
				$this->report_error("Your invite code is invalid.");
			}
		}
		return $result;
	}
}