Fixed time zone calculation issue
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / util / UserUtil.class.php
1 <?php
2 namespace wcf\util;
3 use wcf\system\WCF;
4
5 /**
6 * Contains user-related functions.
7 *
8 * @author Marcel Werk
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
12 * @subpackage util
13 * @category Community Framework
14 */
15 final class UserUtil {
16 /**
17 * Returns true if the given name is a valid username.
18 *
19 * @param string $name
20 * @return boolean
21 */
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) {
25 return false;
26 }
27
28 // check illegal characters
29 if (!preg_match('!^[^,\n]+$!', $name)) {
30 return false;
31 }
32 // check long words
33 $words = preg_split('!\s+!', $name, -1, PREG_SPLIT_NO_EMPTY);
34 foreach ($words as $word) {
35 if (mb_strlen($word) > 20) {
36 return false;
37 }
38 }
39 return true;
40 }
41
42 /**
43 * Returns true if the given username is available.
44 *
45 * @param string $name
46 * @return boolean
47 */
48 public static function isAvailableUsername($name) {
49 $sql = "SELECT COUNT(username) AS count
50 FROM wcf".WCF_N."_user
51 WHERE username = ?";
52 $statement = WCF::getDB()->prepareStatement($sql);
53 $statement->execute(array($name));
54 $row = $statement->fetchArray();
55 return $row['count'] == 0;
56 }
57
58 /**
59 * Returns true if the given e-mail is a valid address.
60 * @see http://www.faqs.org/rfcs/rfc821.html
61 *
62 * @param string $email
63 * @return boolean
64 */
65 public static function isValidEmail($email) {
66 // local-part
67 $c = '!#\$%&\'\*\+\-\/0-9=\?a-z\^_`\{\}\|~';
68 $string = '['.$c.']*(?:\\\\[\x00-\x7F]['.$c.']*)*';
69 $localPart = $string.'(?:\.'.$string.')*';
70
71 // domain
72 $name = '[a-z0-9](?:[a-z0-9-]*[a-z0-9])?';
73 $domain = $name.'(?:\.'.$name.')*\.[a-z]{2,}';
74
75 // mailbox
76 $mailbox = $localPart.'@'.$domain;
77
78 return preg_match('/^'.$mailbox.'$/i', $email);
79 }
80
81 /**
82 * Returns true if the given email address is available.
83 *
84 * @param string $email
85 * @return boolean
86 */
87 public static function isAvailableEmail($email) {
88 $sql = "SELECT COUNT(email) AS count
89 FROM wcf".WCF_N."_user
90 WHERE email = ?";
91 $statement = WCF::getDB()->prepareStatement($sql);
92 $statement->execute(array($email));
93 $row = $statement->fetchArray();
94 return $row['count'] == 0;
95 }
96
97 /**
98 * Returns the user agent of the client.
99 *
100 * @return string
101 */
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);
107 }
108
109 return mb_substr($userAgent, 0, 255);
110 }
111 return '';
112 }
113
114 /**
115 * Returns the ipv6 address of the client.
116 *
117 * @return string
118 */
119 public static function getIpAddress() {
120 $REMOTE_ADDR = '';
121 if (isset($_SERVER['REMOTE_ADDR'])) $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
122
123 // darwin fix
124 if ($REMOTE_ADDR == '::1' || $REMOTE_ADDR == 'fe80::1') {
125 $REMOTE_ADDR = '127.0.0.1';
126 }
127
128 $REMOTE_ADDR = self::convertIPv4To6($REMOTE_ADDR);
129
130 return $REMOTE_ADDR;
131 }
132
133 /**
134 * Converts given ipv4 to ipv6.
135 *
136 * @param string $ip
137 * @return string
138 */
139 public static function convertIPv4To6($ip) {
140 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
141 // given ip is already ipv6
142 return $ip;
143 }
144
145 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
146 // invalid ip given
147 return '';
148 }
149
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);
153
154 return '::ffff:'.$part7.':'.$part8;
155 }
156
157 /**
158 * Converts IPv6 embedded IPv4 address into IPv4 or returns input if true IPv6.
159 *
160 * @param string $ip
161 * @return string
162 */
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
169 return '';
170 }
171
172 return $ip;
173 }
174
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)) {
179 $ip = array(
180 base_convert($matches[1], 16, 10),
181 base_convert($matches[2], 16, 10)
182 );
183
184 $ipParts = array();
185 $tmp = $ip[0] % 256;
186 $ipParts[] = ($ip[0] - $tmp) / 256;
187 $ipParts[] = $tmp;
188 $tmp = $ip[1] % 256;
189 $ipParts[] = ($ip[1] - $tmp) / 256;
190 $ipParts[] = $tmp;
191
192 return implode('.', $ipParts);
193 }
194 else {
195 return $ip;
196 }
197 }
198 else {
199 // given ip is an IPv6 address and cannot be converted
200 return $ip;
201 }
202 }
203
204 /**
205 * Returns the request uri of the active request.
206 *
207 * @return string
208 */
209 public static function getRequestURI() {
210 $REQUEST_URI = '';
211
212 $appendQueryString = true;
213 if (!empty($_SERVER['ORIG_PATH_INFO']) && strpos($_SERVER['ORIG_PATH_INFO'], '.php') !== false) {
214 $REQUEST_URI = $_SERVER['ORIG_PATH_INFO'];
215 }
216 else if (!empty($_SERVER['ORIG_SCRIPT_NAME'])) {
217 $REQUEST_URI = $_SERVER['ORIG_SCRIPT_NAME'];
218 }
219 else if (!empty($_SERVER['SCRIPT_NAME']) && (isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO']))) {
220 $REQUEST_URI = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
221 }
222 else if (isset($_SERVER['REQUEST_URI']) && !empty($_SERVER['REQUEST_URI'])) {
223 $REQUEST_URI = $_SERVER['REQUEST_URI'];
224 $appendQueryString = false;
225 }
226 else if (!empty($_SERVER['PHP_SELF'])) {
227 $REQUEST_URI = $_SERVER['PHP_SELF'];
228 }
229 else if (!empty($_SERVER['PATH_INFO'])) {
230 $REQUEST_URI = $_SERVER['PATH_INFO'];
231 }
232 if ($appendQueryString && !empty($_SERVER['QUERY_STRING'])) {
233 $REQUEST_URI .= '?'.$_SERVER['QUERY_STRING'];
234 }
235
236 // fix encoding
237 if (!StringUtil::isASCII($REQUEST_URI) && !StringUtil::isUTF8($REQUEST_URI)) {
238 $REQUEST_URI = StringUtil::convertEncoding('ISO-8859-1', 'UTF-8', $REQUEST_URI);
239 }
240
241 return mb_substr(FileUtil::unifyDirSeparator($REQUEST_URI), 0, 255);
242 }
243
244 private function __construct() { }
245 }