2 /***************************************************************************
3 * functions_validate.php
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: functions_validate.php,v 1.6.2.12 2003/06/09 19:13:05 psotfx Exp $
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 ***************************************************************************/
24 // Check to see if the username has been taken, or if it is disallowed.
25 // Also checks if it includes the " character, which we don't allow in usernames.
26 // Used for registering, changing names, and posting anonymously with a username
28 function validate_username($username)
30 global $db, $lang, $userdata;
32 // Remove doubled up spaces
33 $username = preg_replace('#\s+#', ' ', $username);
34 // Limit username length
35 $username = substr(str_replace("\'", "'", $username), 0, 25);
36 $username = str_replace("'", "''", $username);
38 $sql = "SELECT username
39 FROM " . USERS_TABLE . "
40 WHERE LOWER(username) = '" . strtolower($username) . "'";
41 if ($result = $db->sql_query($sql))
43 if ($row = $db->sql_fetchrow($result))
45 if (($userdata['session_logged_in'] && $row['username'] != $userdata['username']) || !$userdata['session_logged_in'])
47 $db->sql_freeresult($result);
48 return array('error' => true, 'error_msg' => $lang['Username_taken']);
52 $db->sql_freeresult($result);
54 $sql = "SELECT group_name
55 FROM " . GROUPS_TABLE . "
56 WHERE LOWER(group_name) = '" . strtolower($username) . "'";
57 if ($result = $db->sql_query($sql))
59 if ($row = $db->sql_fetchrow($result))
61 $db->sql_freeresult($result);
62 return array('error' => true, 'error_msg' => $lang['Username_taken']);
65 $db->sql_freeresult($result);
67 $sql = "SELECT disallow_username
68 FROM " . DISALLOW_TABLE;
69 if ($result = $db->sql_query($sql))
71 if ($row = $db->sql_fetchrow($result))
75 if (preg_match("#\b(" . str_replace("\*", ".*?", phpbb_preg_quote($row['disallow_username'], '#')) . ")\b#i", $username))
77 $db->sql_freeresult($result);
78 return array('error' => true, 'error_msg' => $lang['Username_disallowed']);
81 while($row = $db->sql_fetchrow($result));
84 $db->sql_freeresult($result);
88 if ($result = $db->sql_query($sql))
90 if ($row = $db->sql_fetchrow($result))
94 if (preg_match("#\b(" . str_replace("\*", ".*?", phpbb_preg_quote($row['word'], '#')) . ")\b#i", $username))
96 $db->sql_freeresult($result);
97 return array('error' => true, 'error_msg' => $lang['Username_disallowed']);
100 while ($row = $db->sql_fetchrow($result));
103 $db->sql_freeresult($result);
105 // Don't allow " and ALT-255 in username.
106 if (strstr($username, '"') || strstr($username, '"') || strstr($username, chr(160)))
108 return array('error' => true, 'error_msg' => $lang['Username_invalid']);
111 return array('error' => false, 'error_msg' => '');
115 // Check to see if email address is banned
116 // or already present in the DB
118 function validate_email($email)
124 if (preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*?[a-z]+$/is', $email))
126 $sql = "SELECT ban_email
127 FROM " . BANLIST_TABLE;
128 if ($result = $db->sql_query($sql))
130 if ($row = $db->sql_fetchrow($result))
134 $match_email = str_replace('*', '.*?', $row['ban_email']);
135 if (preg_match('/^' . $match_email . '$/is', $email))
137 $db->sql_freeresult($result);
138 return array('error' => true, 'error_msg' => $lang['Email_banned']);
141 while($row = $db->sql_fetchrow($result));
144 $db->sql_freeresult($result);
146 $sql = "SELECT user_email
147 FROM " . USERS_TABLE . "
148 WHERE user_email = '" . str_replace("\'", "''", $email) . "'";
149 if (!($result = $db->sql_query($sql)))
151 message_die(GENERAL_ERROR, "Couldn't obtain user email information.", "", __LINE__, __FILE__, $sql);
154 if ($row = $db->sql_fetchrow($result))
156 return array('error' => true, 'error_msg' => $lang['Email_taken']);
158 $db->sql_freeresult($result);
160 return array('error' => false, 'error_msg' => '');
164 return array('error' => true, 'error_msg' => $lang['Email_invalid']);
168 // Does supplementary validation of optional profile fields. This expects common stuff like trim() and strip_tags()
169 // to have already been run. Params are passed by-ref, so we can set them to the empty string if they fail.
171 function validate_optional_fields(&$icq, &$aim, &$msnm, &$yim, &$website, &$location, &$occupation, &$interests, &$sig)
173 $check_var_length = array('aim', 'msnm', 'yim', 'location', 'occupation', 'interests', 'sig');
175 for($i = 0; $i < count($check_var_length); $i++)
177 if (strlen($$check_var_length[$i]) < 2)
179 $$check_var_length[$i] = '';
183 // ICQ number has to be only numbers.
184 if (!preg_match('/^[0-9]+$/', $icq))
189 // website has to start with http://, followed by something with length at least 3 that
190 // contains at least one dot.
193 if (!preg_match('#^http[s]?:\/\/#i', $website))
195 $website = 'http://' . $website;
198 if (!preg_match('#^http[s]?\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $website))