-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
82 lines (69 loc) · 2.86 KB
/
User.php
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
73
74
75
76
77
78
79
80
81
82
<?php
class User {
public $address;
public $username;
public $domain;
public static function get_random_address(array $domains, $ip): string {
$wordLength = rand(3, 8);
$container = new PronounceableWord_DependencyInjectionContainer();
$generator = $container->getGenerator();
$word = $generator->generateWordOfGivenLength($wordLength);
$nr = rand(51, 91);
$name = $word . $nr;
$name = base64_encode($ip);
$name = str_replace('=','',$name);
$domain = $domains[array_rand($domains)];
return "$name@$domain";
}
public function isInvalid(array $config_domains): bool {
if (empty($this->username) || empty($this->domain)) {
return true;
} elseif (!in_array($this->domain, $config_domains)) {
return true;
} else {
return false;
}
}
public static function parseDomain(string $address, array $config_blocked_usernames): User {
$clean_address = User::_clean_address($address);
$user = new User();
$user->username = User::_clean_username($clean_address, $config_blocked_usernames);
$user->domain = User::_clean_domain($clean_address);
$user->address = $user->username . '@' . $user->domain;
return $user;
}
public static function parseUsernameAndDomain(string $username, string $domain, $config_blocked_usernames): User {
$user = new User();
$user->username = User::_clean_username($username, $config_blocked_usernames);
$user->domain = User::_clean_domain($domain);
$user->address = $user->username . '@' . $user->domain;
return $user;
}
/**
* Remove illegal characters from address.
* @return string clean address
*/
private static function _clean_address(string $address) {
return strtolower(filter_var($address, FILTER_SANITIZE_EMAIL));
}
/**
* Remove illegal characters from username and remove everything after the @-sign. You may extend it if your server supports them.
*
* @return string clean username
*/
private static function _clean_username(string $address, array $config_blocked_usernames) {
$username = strtolower($address);
$username = preg_replace('/@.*$/', "", $username); // remove part after @
$username = preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters
if (in_array($username, $config_blocked_usernames)) {
// Forbidden name!
return '';
}
return $username;
}
private static function _clean_domain(string $address) {
$username = strtolower($address);
$username = preg_replace('/^.*@/', "", $username); // remove part before @
return preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters
}
}