6 * Contains user-related functions.
9 * @copyright 2001-2014 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package com.woltlab.wcf
13 * @category Community Framework
15 final class UserUtil
{
17 * Returns true if the given name is a valid username.
22 public static function isValidUsername($name) {
23 // minimum length is 3 characters, maximum length is 255 characters
24 if (mb_strlen($name) < 3 ||
mb_strlen($name) > 255) {
28 // check illegal characters
29 if (!preg_match('!^[^,\n]+$!', $name)) {
33 $words = preg_split('!\s+!', $name, -1, PREG_SPLIT_NO_EMPTY
);
34 foreach ($words as $word) {
35 if (mb_strlen($word) > 20) {
43 * Returns true if the given username is available.
48 public static function isAvailableUsername($name) {
49 $sql = "SELECT COUNT(username) AS count
50 FROM wcf".WCF_N
."_user
52 $statement = WCF
::getDB()->prepareStatement($sql);
53 $statement->execute(array($name));
54 $row = $statement->fetchArray();
55 return $row['count'] == 0;
59 * Returns true if the given e-mail is a valid address.
60 * @see http://www.faqs.org/rfcs/rfc821.html
62 * @param string $email
65 public static function isValidEmail($email) {
67 $c = '!#\$%&\'\*\+\-\/0-9=\?a-z\^_`\{\}\|~';
68 $string = '['.$c.']*(?:\\\\[\x00-\x7F]['.$c.']*)*';
69 $localPart = $string.'(?:\.'.$string.')*';
72 $name = '[a-z0-9](?:[a-z0-9-]*[a-z0-9])?';
73 $domain = $name.'(?:\.'.$name.')*\.[a-z]{2,}';
76 $mailbox = $localPart.'@'.$domain;
78 return preg_match('/^'.$mailbox.'$/i', $email);
82 * Returns true if the given email address is available.
84 * @param string $email
87 public static function isAvailableEmail($email) {
88 $sql = "SELECT COUNT(email) AS count
89 FROM wcf".WCF_N
."_user
91 $statement = WCF
::getDB()->prepareStatement($sql);
92 $statement->execute(array($email));
93 $row = $statement->fetchArray();
94 return $row['count'] == 0;
98 * Returns the user agent of the client.
102 public static function getUserAgent() {
103 if (isset($_SERVER['HTTP_USER_AGENT'])) {
104 $userAgent = $_SERVER['HTTP_USER_AGENT'];
105 if (!StringUtil
::isASCII($userAgent) && !StringUtil
::isUTF8($userAgent)) {
106 $userAgent = StringUtil
::convertEncoding('ISO-8859-1', 'UTF-8', $userAgent);
109 return mb_substr($userAgent, 0, 255);
115 * Returns the ipv6 address of the client.
119 public static function getIpAddress() {
121 if (isset($_SERVER['REMOTE_ADDR'])) $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
124 if ($REMOTE_ADDR == '::1' ||
$REMOTE_ADDR == 'fe80::1') {
125 $REMOTE_ADDR = '127.0.0.1';
128 $REMOTE_ADDR = self
::convertIPv4To6($REMOTE_ADDR);
134 * Converts given ipv4 to ipv6.
139 public static function convertIPv4To6($ip) {
140 if (filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) !== false) {
141 // given ip is already ipv6
145 if (filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) === false) {
150 $ipArray = array_pad(explode('.', $ip), 4, 0);
151 $part7 = base_convert(($ipArray[0] * 256) +
$ipArray[1], 10, 16);
152 $part8 = base_convert(($ipArray[2] * 256) +
$ipArray[3], 10, 16);
154 return '::ffff:'.$part7.':'.$part8;
158 * Converts IPv6 embedded IPv4 address into IPv4 or returns input if true IPv6.
163 public static function convertIPv6To4($ip) {
164 // validate if given IP is a proper IPv6 address
165 if (filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) === false) {
166 // validate if given IP is a proper IPv4 address
167 if (filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) === false) {
168 // ip address is invalid
175 // check if ip is a masked IPv4 address
176 if (substr($ip, 0, 7) == '::ffff:') {
177 $ip = substr($ip, 7);
178 if (preg_match('~^([a-f0-9]{1,4}):([a-f0-9]{1,4})$~', $ip, $matches)) {
180 base_convert($matches[1], 16, 10),
181 base_convert($matches[2], 16, 10)
186 $ipParts[] = ($ip[0] - $tmp) / 256;
189 $ipParts[] = ($ip[1] - $tmp) / 256;
192 return implode('.', $ipParts);
199 // given ip is an IPv6 address and cannot be converted
205 * Returns the request uri of the active request.
209 public static function getRequestURI() {
212 $appendQueryString = true;
213 if (!empty($_SERVER['ORIG_PATH_INFO']) && strpos($_SERVER['ORIG_PATH_INFO'], '.php') !== false) {
214 $REQUEST_URI = $_SERVER['ORIG_PATH_INFO'];
216 else if (!empty($_SERVER['ORIG_SCRIPT_NAME'])) {
217 $REQUEST_URI = $_SERVER['ORIG_SCRIPT_NAME'];
219 else if (!empty($_SERVER['SCRIPT_NAME']) && (isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO']))) {
220 $REQUEST_URI = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
222 else if (isset($_SERVER['REQUEST_URI']) && !empty($_SERVER['REQUEST_URI'])) {
223 $REQUEST_URI = $_SERVER['REQUEST_URI'];
224 $appendQueryString = false;
226 else if (!empty($_SERVER['PHP_SELF'])) {
227 $REQUEST_URI = $_SERVER['PHP_SELF'];
229 else if (!empty($_SERVER['PATH_INFO'])) {
230 $REQUEST_URI = $_SERVER['PATH_INFO'];
232 if ($appendQueryString && !empty($_SERVER['QUERY_STRING'])) {
233 $REQUEST_URI .= '?'.$_SERVER['QUERY_STRING'];
237 if (!StringUtil
::isASCII($REQUEST_URI) && !StringUtil
::isUTF8($REQUEST_URI)) {
238 $REQUEST_URI = StringUtil
::convertEncoding('ISO-8859-1', 'UTF-8', $REQUEST_URI);
241 return mb_substr(FileUtil
::unifyDirSeparator($REQUEST_URI), 0, 255);
244 private function __construct() { }