Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / util / UserRegistrationUtil.class.php
1 <?php
2
3 namespace wcf\util;
4
5 /**
6 * Contains user registration related functions.
7 *
8 * @author Marcel Werk
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\Util
12 */
13 final class UserRegistrationUtil
14 {
15 /**
16 * Forbid creation of UserRegistrationUtil objects.
17 */
18 private function __construct()
19 {
20 // does nothing
21 }
22
23 /**
24 * Returns true if the given name is a valid username.
25 *
26 * @param string $name username
27 * @return bool
28 */
29 public static function isValidUsername($name)
30 {
31 if (!UserUtil::isValidUsername($name)) {
32 return false;
33 }
34
35 $length = \mb_strlen($name);
36 if ($length < REGISTER_USERNAME_MIN_LENGTH || $length > REGISTER_USERNAME_MAX_LENGTH) {
37 return false;
38 }
39
40 if (!self::checkForbiddenUsernames($name)) {
41 return false;
42 }
43
44 if (REGISTER_USERNAME_FORCE_ASCII) {
45 if (!\preg_match('/^[\x20-\x7E]+$/', $name)) {
46 return false;
47 }
48 }
49
50 return true;
51 }
52
53 /**
54 * Returns true if the given e-mail is a valid address.
55 *
56 * @param string $email
57 * @return bool
58 */
59 public static function isValidEmail($email)
60 {
61 return UserUtil::isValidEmail($email) && self::checkForbiddenEmails($email);
62 }
63
64 /**
65 * Returns false if the given name is a forbidden username.
66 *
67 * @param string $name
68 * @return bool
69 */
70 public static function checkForbiddenUsernames($name)
71 {
72 return StringUtil::executeWordFilter($name, REGISTER_FORBIDDEN_USERNAMES);
73 }
74
75 /**
76 * Returns false if the given email is a forbidden email.
77 *
78 * @param string $email
79 * @return bool
80 */
81 public static function checkForbiddenEmails($email)
82 {
83 return StringUtil::executeWordFilter(
84 $email,
85 REGISTER_FORBIDDEN_EMAILS
86 ) && (!StringUtil::trim(REGISTER_ALLOWED_EMAILS) || !StringUtil::executeWordFilter(
87 $email,
88 REGISTER_ALLOWED_EMAILS
89 ));
90 }
91
92 /**
93 * Always returns true.
94 *
95 * @deprecated 5.3 - Take a look at the zxcvbn verdict from WoltLabSuite/Core/Ui/User/PasswordStrength.
96 */
97 public static function isSecurePassword($password)
98 {
99 return true;
100 }
101
102 /**
103 * Returns the `passwordrules` attribute value.
104 *
105 * @see https://developer.apple.com/password-rules/
106 * @return string
107 */
108 public static function getPasswordRulesAttributeValue()
109 {
110 return "minlength:8;";
111 }
112
113 /**
114 * Generates a random activation code with the given length.
115 * Warning: A length greater than 9 is out of integer range.
116 *
117 * @param int $length
118 * @return int
119 */
120 public static function getActivationCode($length = 9)
121 {
122 return \random_int(10 ** ($length - 1), 10 ** $length - 1);
123 }
124
125 /**
126 * Generates a random field name.
127 *
128 * @param string $fieldName
129 * @return string
130 */
131 public static function getRandomFieldName($fieldName)
132 {
133 $hash = StringUtil::getRandomID();
134
135 return \substr($hash, 0, \mt_rand(8, 16));
136 }
137 }