includes/functions_user.php
changeset 0 ceef6d64c21d
child 18 36b05a593b41
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_user.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,3590 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Obtain user_ids from usernames or vice versa. Returns false on
    1.24 +* success else the error string
    1.25 +*
    1.26 +* @param array &$user_id_ary The user ids to check or empty if usernames used
    1.27 +* @param array &$username_ary The usernames to check or empty if user ids used
    1.28 +* @param mixed $user_type Array of user types to check, false if not restricting by user type
    1.29 +*/
    1.30 +function user_get_id_name(&$user_id_ary, &$username_ary, $user_type = false)
    1.31 +{
    1.32 +	global $db;
    1.33 +
    1.34 +	// Are both arrays already filled? Yep, return else
    1.35 +	// are neither array filled?
    1.36 +	if ($user_id_ary && $username_ary)
    1.37 +	{
    1.38 +		return false;
    1.39 +	}
    1.40 +	else if (!$user_id_ary && !$username_ary)
    1.41 +	{
    1.42 +		return 'NO_USERS';
    1.43 +	}
    1.44 +
    1.45 +	$which_ary = ($user_id_ary) ? 'user_id_ary' : 'username_ary';
    1.46 +
    1.47 +	if ($$which_ary && !is_array($$which_ary))
    1.48 +	{
    1.49 +		$$which_ary = array($$which_ary);
    1.50 +	}
    1.51 +
    1.52 +	$sql_in = ($which_ary == 'user_id_ary') ? array_map('intval', $$which_ary) : array_map('utf8_clean_string', $$which_ary);
    1.53 +	unset($$which_ary);
    1.54 +
    1.55 +	$user_id_ary = $username_ary = array();
    1.56 +
    1.57 +	// Grab the user id/username records
    1.58 +	$sql_where = ($which_ary == 'user_id_ary') ? 'user_id' : 'username_clean';
    1.59 +	$sql = 'SELECT user_id, username
    1.60 +		FROM ' . USERS_TABLE . '
    1.61 +		WHERE ' . $db->sql_in_set($sql_where, $sql_in);
    1.62 +
    1.63 +	if ($user_type !== false && !empty($user_type))
    1.64 +	{
    1.65 +		$sql .= ' AND ' . $db->sql_in_set('user_type', $user_type);
    1.66 +	}
    1.67 +
    1.68 +	$result = $db->sql_query($sql);
    1.69 +
    1.70 +	if (!($row = $db->sql_fetchrow($result)))
    1.71 +	{
    1.72 +		$db->sql_freeresult($result);
    1.73 +		return 'NO_USERS';
    1.74 +	}
    1.75 +
    1.76 +	do
    1.77 +	{
    1.78 +		$username_ary[$row['user_id']] = $row['username'];
    1.79 +		$user_id_ary[] = $row['user_id'];
    1.80 +	}
    1.81 +	while ($row = $db->sql_fetchrow($result));
    1.82 +	$db->sql_freeresult($result);
    1.83 +
    1.84 +	return false;
    1.85 +}
    1.86 +
    1.87 +/**
    1.88 +* Get latest registered username and update database to reflect it
    1.89 +*/
    1.90 +function update_last_username()
    1.91 +{
    1.92 +	global $db;
    1.93 +
    1.94 +	// Get latest username
    1.95 +	$sql = 'SELECT user_id, username, user_colour
    1.96 +		FROM ' . USERS_TABLE . '
    1.97 +		WHERE user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
    1.98 +		ORDER BY user_id DESC';
    1.99 +	$result = $db->sql_query_limit($sql, 1);
   1.100 +	$row = $db->sql_fetchrow($result);
   1.101 +	$db->sql_freeresult($result);
   1.102 +
   1.103 +	if ($row)
   1.104 +	{
   1.105 +		set_config('newest_user_id', $row['user_id'], true);
   1.106 +		set_config('newest_username', $row['username'], true);
   1.107 +		set_config('newest_user_colour', $row['user_colour'], true);
   1.108 +	}
   1.109 +}
   1.110 +
   1.111 +/**
   1.112 +* Updates a username across all relevant tables/fields
   1.113 +*
   1.114 +* @param string $old_name the old/current username
   1.115 +* @param string $new_name the new username
   1.116 +*/
   1.117 +function user_update_name($old_name, $new_name)
   1.118 +{
   1.119 +	global $config, $db, $cache;
   1.120 +
   1.121 +	$update_ary = array(
   1.122 +		FORUMS_TABLE			=> array('forum_last_poster_name'),
   1.123 +		MODERATOR_CACHE_TABLE	=> array('username'),
   1.124 +		POSTS_TABLE				=> array('post_username'),
   1.125 +		TOPICS_TABLE			=> array('topic_first_poster_name', 'topic_last_poster_name'),
   1.126 +	);
   1.127 +
   1.128 +	foreach ($update_ary as $table => $field_ary)
   1.129 +	{
   1.130 +		foreach ($field_ary as $field)
   1.131 +		{
   1.132 +			$sql = "UPDATE $table
   1.133 +				SET $field = '" . $db->sql_escape($new_name) . "'
   1.134 +				WHERE $field = '" . $db->sql_escape($old_name) . "'";
   1.135 +			$db->sql_query($sql);
   1.136 +		}
   1.137 +	}
   1.138 +
   1.139 +	if ($config['newest_username'] == $old_name)
   1.140 +	{
   1.141 +		set_config('newest_username', $new_name, true);
   1.142 +	}
   1.143 +
   1.144 +	// Because some tables/caches use username-specific data we need to purge this here.
   1.145 +	$cache->destroy('sql', MODERATOR_CACHE_TABLE);
   1.146 +}
   1.147 +
   1.148 +/**
   1.149 +* Adds an user
   1.150 +*
   1.151 +* @param mixed $user_row An array containing the following keys (and the appropriate values): username, group_id (the group to place the user in), user_email and the user_type(usually 0). Additional entries not overridden by defaults will be forwarded.
   1.152 +* @param string $cp_data custom profile fields, see custom_profile::build_insert_sql_array
   1.153 +* @return the new user's ID.
   1.154 +*/
   1.155 +function user_add($user_row, $cp_data = false)
   1.156 +{
   1.157 +	global $db, $user, $auth, $config, $phpbb_root_path, $phpEx;
   1.158 +
   1.159 +	if (empty($user_row['username']) || !isset($user_row['group_id']) || !isset($user_row['user_email']) || !isset($user_row['user_type']))
   1.160 +	{
   1.161 +		return false;
   1.162 +	}
   1.163 +
   1.164 +	$username_clean = utf8_clean_string($user_row['username']);
   1.165 +
   1.166 +	if (empty($username_clean))
   1.167 +	{
   1.168 +		return false;
   1.169 +	}
   1.170 +
   1.171 +	$sql_ary = array(
   1.172 +		'username'			=> $user_row['username'],
   1.173 +		'username_clean'	=> $username_clean,
   1.174 +		'user_password'		=> (isset($user_row['user_password'])) ? $user_row['user_password'] : '',
   1.175 +		'user_pass_convert'	=> 0,
   1.176 +		'user_email'		=> strtolower($user_row['user_email']),
   1.177 +		'user_email_hash'	=> phpbb_email_hash($user_row['user_email']),
   1.178 +		'group_id'			=> $user_row['group_id'],
   1.179 +		'user_type'			=> $user_row['user_type'],
   1.180 +	);
   1.181 +
   1.182 +	// These are the additional vars able to be specified
   1.183 +	$additional_vars = array(
   1.184 +		'user_permissions'	=> '',
   1.185 +		'user_timezone'		=> $config['board_timezone'],
   1.186 +		'user_dateformat'	=> $config['default_dateformat'],
   1.187 +		'user_lang'			=> $config['default_lang'],
   1.188 +		'user_style'		=> (int) $config['default_style'],
   1.189 +		'user_actkey'		=> '',
   1.190 +		'user_ip'			=> '',
   1.191 +		'user_regdate'		=> time(),
   1.192 +		'user_passchg'		=> time(),
   1.193 +		'user_options'		=> 230271,
   1.194 +		// We do not set the new flag here - registration scripts need to specify it
   1.195 +		'user_new'			=> 0,
   1.196 +
   1.197 +		'user_inactive_reason'	=> 0,
   1.198 +		'user_inactive_time'	=> 0,
   1.199 +		'user_lastmark'			=> time(),
   1.200 +		'user_lastvisit'		=> 0,
   1.201 +		'user_lastpost_time'	=> 0,
   1.202 +		'user_lastpage'			=> '',
   1.203 +		'user_posts'			=> 0,
   1.204 +		'user_dst'				=> (int) $config['board_dst'],
   1.205 +		'user_colour'			=> '',
   1.206 +		'user_occ'				=> '',
   1.207 +		'user_interests'		=> '',
   1.208 +		'user_avatar'			=> '',
   1.209 +		'user_avatar_type'		=> 0,
   1.210 +		'user_avatar_width'		=> 0,
   1.211 +		'user_avatar_height'	=> 0,
   1.212 +		'user_new_privmsg'		=> 0,
   1.213 +		'user_unread_privmsg'	=> 0,
   1.214 +		'user_last_privmsg'		=> 0,
   1.215 +		'user_message_rules'	=> 0,
   1.216 +		'user_full_folder'		=> PRIVMSGS_NO_BOX,
   1.217 +		'user_emailtime'		=> 0,
   1.218 +
   1.219 +		'user_notify'			=> 0,
   1.220 +		'user_notify_pm'		=> 1,
   1.221 +		'user_notify_type'		=> NOTIFY_EMAIL,
   1.222 +		'user_allow_pm'			=> 1,
   1.223 +		'user_allow_viewonline'	=> 1,
   1.224 +		'user_allow_viewemail'	=> 1,
   1.225 +		'user_allow_massemail'	=> 1,
   1.226 +
   1.227 +		'user_sig'					=> '',
   1.228 +		'user_sig_bbcode_uid'		=> '',
   1.229 +		'user_sig_bbcode_bitfield'	=> '',
   1.230 +
   1.231 +		'user_form_salt'			=> unique_id(),
   1.232 +	);
   1.233 +
   1.234 +	// Now fill the sql array with not required variables
   1.235 +	foreach ($additional_vars as $key => $default_value)
   1.236 +	{
   1.237 +		$sql_ary[$key] = (isset($user_row[$key])) ? $user_row[$key] : $default_value;
   1.238 +	}
   1.239 +
   1.240 +	// Any additional variables in $user_row not covered above?
   1.241 +	$remaining_vars = array_diff(array_keys($user_row), array_keys($sql_ary));
   1.242 +
   1.243 +	// Now fill our sql array with the remaining vars
   1.244 +	if (sizeof($remaining_vars))
   1.245 +	{
   1.246 +		foreach ($remaining_vars as $key)
   1.247 +		{
   1.248 +			$sql_ary[$key] = $user_row[$key];
   1.249 +		}
   1.250 +	}
   1.251 +
   1.252 +	$sql = 'INSERT INTO ' . USERS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
   1.253 +	$db->sql_query($sql);
   1.254 +
   1.255 +	$user_id = $db->sql_nextid();
   1.256 +
   1.257 +	// Insert Custom Profile Fields
   1.258 +	if ($cp_data !== false && sizeof($cp_data))
   1.259 +	{
   1.260 +		$cp_data['user_id'] = (int) $user_id;
   1.261 +
   1.262 +		if (!class_exists('custom_profile'))
   1.263 +		{
   1.264 +			include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
   1.265 +		}
   1.266 +
   1.267 +		$sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' .
   1.268 +			$db->sql_build_array('INSERT', custom_profile::build_insert_sql_array($cp_data));
   1.269 +		$db->sql_query($sql);
   1.270 +	}
   1.271 +
   1.272 +	// Place into appropriate group...
   1.273 +	$sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' ' . $db->sql_build_array('INSERT', array(
   1.274 +		'user_id'		=> (int) $user_id,
   1.275 +		'group_id'		=> (int) $user_row['group_id'],
   1.276 +		'user_pending'	=> 0)
   1.277 +	);
   1.278 +	$db->sql_query($sql);
   1.279 +
   1.280 +	// Now make it the users default group...
   1.281 +	group_set_user_default($user_row['group_id'], array($user_id), false);
   1.282 +
   1.283 +	// Add to newly registered users group if user_new is 1
   1.284 +	if ($config['new_member_post_limit'] && $sql_ary['user_new'])
   1.285 +	{
   1.286 +		$sql = 'SELECT group_id
   1.287 +			FROM ' . GROUPS_TABLE . "
   1.288 +			WHERE group_name = 'NEWLY_REGISTERED'
   1.289 +				AND group_type = " . GROUP_SPECIAL;
   1.290 +		$result = $db->sql_query($sql);
   1.291 +		$add_group_id = (int) $db->sql_fetchfield('group_id');
   1.292 +		$db->sql_freeresult($result);
   1.293 +
   1.294 +		if ($add_group_id)
   1.295 +		{
   1.296 +			// Because these actions only fill the log unneccessarily we skip the add_log() entry with a little hack. :/
   1.297 +			$GLOBALS['skip_add_log'] = true;
   1.298 +
   1.299 +			// Add user to "newly registered users" group and set to default group if admin specified so.
   1.300 +			if ($config['new_member_group_default'])
   1.301 +			{
   1.302 +				group_user_add($add_group_id, $user_id, false, false, true);
   1.303 +				$user_row['group_id'] = $add_group_id;
   1.304 +			}
   1.305 +			else
   1.306 +			{
   1.307 +				group_user_add($add_group_id, $user_id);
   1.308 +			}
   1.309 +
   1.310 +			unset($GLOBALS['skip_add_log']);
   1.311 +		}
   1.312 +	}
   1.313 +
   1.314 +	// set the newest user and adjust the user count if the user is a normal user and no activation mail is sent
   1.315 +	if ($user_row['user_type'] == USER_NORMAL || $user_row['user_type'] == USER_FOUNDER)
   1.316 +	{
   1.317 +		set_config('newest_user_id', $user_id, true);
   1.318 +		set_config('newest_username', $user_row['username'], true);
   1.319 +		set_config_count('num_users', 1, true);
   1.320 +
   1.321 +		$sql = 'SELECT group_colour
   1.322 +			FROM ' . GROUPS_TABLE . '
   1.323 +			WHERE group_id = ' . (int) $user_row['group_id'];
   1.324 +		$result = $db->sql_query_limit($sql, 1);
   1.325 +		$row = $db->sql_fetchrow($result);
   1.326 +		$db->sql_freeresult($result);
   1.327 +
   1.328 +		set_config('newest_user_colour', $row['group_colour'], true);
   1.329 +	}
   1.330 +
   1.331 +	return $user_id;
   1.332 +}
   1.333 +
   1.334 +/**
   1.335 +* Remove User
   1.336 +*/
   1.337 +function user_delete($mode, $user_id, $post_username = false)
   1.338 +{
   1.339 +	global $cache, $config, $db, $user, $auth;
   1.340 +	global $phpbb_root_path, $phpEx;
   1.341 +
   1.342 +	$sql = 'SELECT *
   1.343 +		FROM ' . USERS_TABLE . '
   1.344 +		WHERE user_id = ' . $user_id;
   1.345 +	$result = $db->sql_query($sql);
   1.346 +	$user_row = $db->sql_fetchrow($result);
   1.347 +	$db->sql_freeresult($result);
   1.348 +
   1.349 +	if (!$user_row)
   1.350 +	{
   1.351 +		return false;
   1.352 +	}
   1.353 +
   1.354 +	// Before we begin, we will remove the reports the user issued.
   1.355 +	$sql = 'SELECT r.post_id, p.topic_id
   1.356 +		FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . ' p
   1.357 +		WHERE r.user_id = ' . $user_id . '
   1.358 +			AND p.post_id = r.post_id';
   1.359 +	$result = $db->sql_query($sql);
   1.360 +
   1.361 +	$report_posts = $report_topics = array();
   1.362 +	while ($row = $db->sql_fetchrow($result))
   1.363 +	{
   1.364 +		$report_posts[] = $row['post_id'];
   1.365 +		$report_topics[] = $row['topic_id'];
   1.366 +	}
   1.367 +	$db->sql_freeresult($result);
   1.368 +
   1.369 +	if (sizeof($report_posts))
   1.370 +	{
   1.371 +		$report_posts = array_unique($report_posts);
   1.372 +		$report_topics = array_unique($report_topics);
   1.373 +
   1.374 +		// Get a list of topics that still contain reported posts
   1.375 +		$sql = 'SELECT DISTINCT topic_id
   1.376 +			FROM ' . POSTS_TABLE . '
   1.377 +			WHERE ' . $db->sql_in_set('topic_id', $report_topics) . '
   1.378 +				AND post_reported = 1
   1.379 +				AND ' . $db->sql_in_set('post_id', $report_posts, true);
   1.380 +		$result = $db->sql_query($sql);
   1.381 +
   1.382 +		$keep_report_topics = array();
   1.383 +		while ($row = $db->sql_fetchrow($result))
   1.384 +		{
   1.385 +			$keep_report_topics[] = $row['topic_id'];
   1.386 +		}
   1.387 +		$db->sql_freeresult($result);
   1.388 +
   1.389 +		if (sizeof($keep_report_topics))
   1.390 +		{
   1.391 +			$report_topics = array_diff($report_topics, $keep_report_topics);
   1.392 +		}
   1.393 +		unset($keep_report_topics);
   1.394 +
   1.395 +		// Now set the flags back
   1.396 +		$sql = 'UPDATE ' . POSTS_TABLE . '
   1.397 +			SET post_reported = 0
   1.398 +			WHERE ' . $db->sql_in_set('post_id', $report_posts);
   1.399 +		$db->sql_query($sql);
   1.400 +
   1.401 +		if (sizeof($report_topics))
   1.402 +		{
   1.403 +			$sql = 'UPDATE ' . TOPICS_TABLE . '
   1.404 +				SET topic_reported = 0
   1.405 +				WHERE ' . $db->sql_in_set('topic_id', $report_topics);
   1.406 +			$db->sql_query($sql);
   1.407 +		}
   1.408 +	}
   1.409 +
   1.410 +	// Remove reports
   1.411 +	$db->sql_query('DELETE FROM ' . REPORTS_TABLE . ' WHERE user_id = ' . $user_id);
   1.412 +
   1.413 +	if ($user_row['user_avatar'] && $user_row['user_avatar_type'] == AVATAR_UPLOAD)
   1.414 +	{
   1.415 +		avatar_delete('user', $user_row);
   1.416 +	}
   1.417 +
   1.418 +	switch ($mode)
   1.419 +	{
   1.420 +		case 'retain':
   1.421 +
   1.422 +			$db->sql_transaction('begin');
   1.423 +
   1.424 +			if ($post_username === false)
   1.425 +			{
   1.426 +				$post_username = $user->lang['GUEST'];
   1.427 +			}
   1.428 +
   1.429 +			// If the user is inactive and newly registered we assume no posts from this user being there...
   1.430 +			if ($user_row['user_type'] == USER_INACTIVE && $user_row['user_inactive_reason'] == INACTIVE_REGISTER && !$user_row['user_posts'])
   1.431 +			{
   1.432 +			}
   1.433 +			else
   1.434 +			{
   1.435 +				$sql = 'UPDATE ' . FORUMS_TABLE . '
   1.436 +					SET forum_last_poster_id = ' . ANONYMOUS . ", forum_last_poster_name = '" . $db->sql_escape($post_username) . "', forum_last_poster_colour = ''
   1.437 +					WHERE forum_last_poster_id = $user_id";
   1.438 +				$db->sql_query($sql);
   1.439 +
   1.440 +				$sql = 'UPDATE ' . POSTS_TABLE . '
   1.441 +					SET poster_id = ' . ANONYMOUS . ", post_username = '" . $db->sql_escape($post_username) . "'
   1.442 +					WHERE poster_id = $user_id";
   1.443 +				$db->sql_query($sql);
   1.444 +
   1.445 +				$sql = 'UPDATE ' . POSTS_TABLE . '
   1.446 +					SET post_edit_user = ' . ANONYMOUS . "
   1.447 +					WHERE post_edit_user = $user_id";
   1.448 +				$db->sql_query($sql);
   1.449 +
   1.450 +				$sql = 'UPDATE ' . TOPICS_TABLE . '
   1.451 +					SET topic_poster = ' . ANONYMOUS . ", topic_first_poster_name = '" . $db->sql_escape($post_username) . "', topic_first_poster_colour = ''
   1.452 +					WHERE topic_poster = $user_id";
   1.453 +				$db->sql_query($sql);
   1.454 +
   1.455 +				$sql = 'UPDATE ' . TOPICS_TABLE . '
   1.456 +					SET topic_last_poster_id = ' . ANONYMOUS . ", topic_last_poster_name = '" . $db->sql_escape($post_username) . "', topic_last_poster_colour = ''
   1.457 +					WHERE topic_last_poster_id = $user_id";
   1.458 +				$db->sql_query($sql);
   1.459 +
   1.460 +				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
   1.461 +					SET poster_id = ' . ANONYMOUS . "
   1.462 +					WHERE poster_id = $user_id";
   1.463 +				$db->sql_query($sql);
   1.464 +
   1.465 +				// Since we change every post by this author, we need to count this amount towards the anonymous user
   1.466 +
   1.467 +				// Update the post count for the anonymous user
   1.468 +				if ($user_row['user_posts'])
   1.469 +				{
   1.470 +					$sql = 'UPDATE ' . USERS_TABLE . '
   1.471 +						SET user_posts = user_posts + ' . $user_row['user_posts'] . '
   1.472 +						WHERE user_id = ' . ANONYMOUS;
   1.473 +					$db->sql_query($sql);
   1.474 +				}
   1.475 +			}
   1.476 +
   1.477 +			$db->sql_transaction('commit');
   1.478 +
   1.479 +		break;
   1.480 +
   1.481 +		case 'remove':
   1.482 +
   1.483 +			if (!function_exists('delete_posts'))
   1.484 +			{
   1.485 +				include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
   1.486 +			}
   1.487 +
   1.488 +			// Delete posts, attachments, etc.
   1.489 +			delete_posts('poster_id', $user_id);
   1.490 +
   1.491 +		break;
   1.492 +	}
   1.493 +
   1.494 +	$db->sql_transaction('begin');
   1.495 +
   1.496 +	$table_ary = array(USERS_TABLE, USER_GROUP_TABLE, TOPICS_WATCH_TABLE, FORUMS_WATCH_TABLE, ACL_USERS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, FORUMS_TRACK_TABLE, PROFILE_FIELDS_DATA_TABLE, MODERATOR_CACHE_TABLE, DRAFTS_TABLE, BOOKMARKS_TABLE, SESSIONS_KEYS_TABLE, PRIVMSGS_FOLDER_TABLE, PRIVMSGS_RULES_TABLE);
   1.497 +
   1.498 +	foreach ($table_ary as $table)
   1.499 +	{
   1.500 +		$sql = "DELETE FROM $table
   1.501 +			WHERE user_id = $user_id";
   1.502 +		$db->sql_query($sql);
   1.503 +	}
   1.504 +
   1.505 +	$cache->destroy('sql', MODERATOR_CACHE_TABLE);
   1.506 +
   1.507 +	// Delete user log entries about this user
   1.508 +	$sql = 'DELETE FROM ' . LOG_TABLE . '
   1.509 +		WHERE reportee_id = ' . $user_id;
   1.510 +	$db->sql_query($sql);
   1.511 +
   1.512 +	// Change user_id to anonymous for this users triggered events
   1.513 +	$sql = 'UPDATE ' . LOG_TABLE . '
   1.514 +		SET user_id = ' . ANONYMOUS . '
   1.515 +		WHERE user_id = ' . $user_id;
   1.516 +	$db->sql_query($sql);
   1.517 +
   1.518 +	// Delete the user_id from the zebra table
   1.519 +	$sql = 'DELETE FROM ' . ZEBRA_TABLE . '
   1.520 +		WHERE user_id = ' . $user_id . '
   1.521 +			OR zebra_id = ' . $user_id;
   1.522 +	$db->sql_query($sql);
   1.523 +
   1.524 +	// Delete the user_id from the banlist
   1.525 +	$sql = 'DELETE FROM ' . BANLIST_TABLE . '
   1.526 +		WHERE ban_userid = ' . $user_id;
   1.527 +	$db->sql_query($sql);
   1.528 +
   1.529 +	// Delete the user_id from the session table
   1.530 +	$sql = 'DELETE FROM ' . SESSIONS_TABLE . '
   1.531 +		WHERE session_user_id = ' . $user_id;
   1.532 +	$db->sql_query($sql);
   1.533 +
   1.534 +	// Remove any undelivered mails...
   1.535 +	$sql = 'SELECT msg_id, user_id
   1.536 +		FROM ' . PRIVMSGS_TO_TABLE . '
   1.537 +		WHERE author_id = ' . $user_id . '
   1.538 +			AND folder_id = ' . PRIVMSGS_NO_BOX;
   1.539 +	$result = $db->sql_query($sql);
   1.540 +
   1.541 +	$undelivered_msg = $undelivered_user = array();
   1.542 +	while ($row = $db->sql_fetchrow($result))
   1.543 +	{
   1.544 +		$undelivered_msg[] = $row['msg_id'];
   1.545 +		$undelivered_user[$row['user_id']][] = true;
   1.546 +	}
   1.547 +	$db->sql_freeresult($result);
   1.548 +
   1.549 +	if (sizeof($undelivered_msg))
   1.550 +	{
   1.551 +		$sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
   1.552 +			WHERE ' . $db->sql_in_set('msg_id', $undelivered_msg);
   1.553 +		$db->sql_query($sql);
   1.554 +	}
   1.555 +
   1.556 +	$sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . '
   1.557 +		WHERE author_id = ' . $user_id . '
   1.558 +			AND folder_id = ' . PRIVMSGS_NO_BOX;
   1.559 +	$db->sql_query($sql);
   1.560 +
   1.561 +	// Delete all to-information
   1.562 +	$sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . '
   1.563 +		WHERE user_id = ' . $user_id;
   1.564 +	$db->sql_query($sql);
   1.565 +
   1.566 +	// Set the remaining author id to anonymous - this way users are still able to read messages from users being removed
   1.567 +	$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
   1.568 +		SET author_id = ' . ANONYMOUS . '
   1.569 +		WHERE author_id = ' . $user_id;
   1.570 +	$db->sql_query($sql);
   1.571 +
   1.572 +	$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
   1.573 +		SET author_id = ' . ANONYMOUS . '
   1.574 +		WHERE author_id = ' . $user_id;
   1.575 +	$db->sql_query($sql);
   1.576 +
   1.577 +	foreach ($undelivered_user as $_user_id => $ary)
   1.578 +	{
   1.579 +		if ($_user_id == $user_id)
   1.580 +		{
   1.581 +			continue;
   1.582 +		}
   1.583 +
   1.584 +		$sql = 'UPDATE ' . USERS_TABLE . '
   1.585 +			SET user_new_privmsg = user_new_privmsg - ' . sizeof($ary) . ',
   1.586 +				user_unread_privmsg = user_unread_privmsg - ' . sizeof($ary) . '
   1.587 +			WHERE user_id = ' . $_user_id;
   1.588 +		$db->sql_query($sql);
   1.589 +	}
   1.590 +
   1.591 +	$db->sql_transaction('commit');
   1.592 +
   1.593 +	// Reset newest user info if appropriate
   1.594 +	if ($config['newest_user_id'] == $user_id)
   1.595 +	{
   1.596 +		update_last_username();
   1.597 +	}
   1.598 +
   1.599 +	// Decrement number of users if this user is active
   1.600 +	if ($user_row['user_type'] != USER_INACTIVE && $user_row['user_type'] != USER_IGNORE)
   1.601 +	{
   1.602 +		set_config_count('num_users', -1, true);
   1.603 +	}
   1.604 +
   1.605 +	return false;
   1.606 +}
   1.607 +
   1.608 +/**
   1.609 +* Flips user_type from active to inactive and vice versa, handles group membership updates
   1.610 +*
   1.611 +* @param string $mode can be flip for flipping from active/inactive, activate or deactivate
   1.612 +*/
   1.613 +function user_active_flip($mode, $user_id_ary, $reason = INACTIVE_MANUAL)
   1.614 +{
   1.615 +	global $config, $db, $user, $auth;
   1.616 +
   1.617 +	$deactivated = $activated = 0;
   1.618 +	$sql_statements = array();
   1.619 +
   1.620 +	if (!is_array($user_id_ary))
   1.621 +	{
   1.622 +		$user_id_ary = array($user_id_ary);
   1.623 +	}
   1.624 +
   1.625 +	if (!sizeof($user_id_ary))
   1.626 +	{
   1.627 +		return;
   1.628 +	}
   1.629 +
   1.630 +	$sql = 'SELECT user_id, group_id, user_type, user_inactive_reason
   1.631 +		FROM ' . USERS_TABLE . '
   1.632 +		WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
   1.633 +	$result = $db->sql_query($sql);
   1.634 +
   1.635 +	while ($row = $db->sql_fetchrow($result))
   1.636 +	{
   1.637 +		$sql_ary = array();
   1.638 +
   1.639 +		if ($row['user_type'] == USER_IGNORE || $row['user_type'] == USER_FOUNDER ||
   1.640 +			($mode == 'activate' && $row['user_type'] != USER_INACTIVE) ||
   1.641 +			($mode == 'deactivate' && $row['user_type'] == USER_INACTIVE))
   1.642 +		{
   1.643 +			continue;
   1.644 +		}
   1.645 +
   1.646 +		if ($row['user_type'] == USER_INACTIVE)
   1.647 +		{
   1.648 +			$activated++;
   1.649 +		}
   1.650 +		else
   1.651 +		{
   1.652 +			$deactivated++;
   1.653 +
   1.654 +			// Remove the users session key...
   1.655 +			$user->reset_login_keys($row['user_id']);
   1.656 +		}
   1.657 +
   1.658 +		$sql_ary += array(
   1.659 +			'user_type'				=> ($row['user_type'] == USER_NORMAL) ? USER_INACTIVE : USER_NORMAL,
   1.660 +			'user_inactive_time'	=> ($row['user_type'] == USER_NORMAL) ? time() : 0,
   1.661 +			'user_inactive_reason'	=> ($row['user_type'] == USER_NORMAL) ? $reason : 0,
   1.662 +		);
   1.663 +
   1.664 +		$sql_statements[$row['user_id']] = $sql_ary;
   1.665 +	}
   1.666 +	$db->sql_freeresult($result);
   1.667 +
   1.668 +	if (sizeof($sql_statements))
   1.669 +	{
   1.670 +		foreach ($sql_statements as $user_id => $sql_ary)
   1.671 +		{
   1.672 +			$sql = 'UPDATE ' . USERS_TABLE . '
   1.673 +				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
   1.674 +				WHERE user_id = ' . $user_id;
   1.675 +			$db->sql_query($sql);
   1.676 +		}
   1.677 +
   1.678 +		$auth->acl_clear_prefetch(array_keys($sql_statements));
   1.679 +	}
   1.680 +
   1.681 +	if ($deactivated)
   1.682 +	{
   1.683 +		set_config_count('num_users', $deactivated * (-1), true);
   1.684 +	}
   1.685 +
   1.686 +	if ($activated)
   1.687 +	{
   1.688 +		set_config_count('num_users', $activated, true);
   1.689 +	}
   1.690 +
   1.691 +	// Update latest username
   1.692 +	update_last_username();
   1.693 +}
   1.694 +
   1.695 +/**
   1.696 +* Add a ban or ban exclusion to the banlist. Bans either a user, an IP or an email address
   1.697 +*
   1.698 +* @param string $mode Type of ban. One of the following: user, ip, email
   1.699 +* @param mixed $ban Banned entity. Either string or array with usernames, ips or email addresses
   1.700 +* @param int $ban_len Ban length in minutes
   1.701 +* @param string $ban_len_other Ban length as a date (YYYY-MM-DD)
   1.702 +* @param boolean $ban_exclude Exclude these entities from banning?
   1.703 +* @param string $ban_reason String describing the reason for this ban
   1.704 +* @return boolean
   1.705 +*/
   1.706 +function user_ban($mode, $ban, $ban_len, $ban_len_other, $ban_exclude, $ban_reason, $ban_give_reason = '')
   1.707 +{
   1.708 +	global $db, $user, $auth, $cache;
   1.709 +
   1.710 +	// Delete stale bans
   1.711 +	$sql = 'DELETE FROM ' . BANLIST_TABLE . '
   1.712 +		WHERE ban_end < ' . time() . '
   1.713 +			AND ban_end <> 0';
   1.714 +	$db->sql_query($sql);
   1.715 +
   1.716 +	$ban_list = (!is_array($ban)) ? array_unique(explode("\n", $ban)) : $ban;
   1.717 +	$ban_list_log = implode(', ', $ban_list);
   1.718 +
   1.719 +	$current_time = time();
   1.720 +
   1.721 +	// Set $ban_end to the unix time when the ban should end. 0 is a permanent ban.
   1.722 +	if ($ban_len)
   1.723 +	{
   1.724 +		if ($ban_len != -1 || !$ban_len_other)
   1.725 +		{
   1.726 +			$ban_end = max($current_time, $current_time + ($ban_len) * 60);
   1.727 +		}
   1.728 +		else
   1.729 +		{
   1.730 +			$ban_other = explode('-', $ban_len_other);
   1.731 +			if (sizeof($ban_other) == 3 && ((int)$ban_other[0] < 9999) &&
   1.732 +				(strlen($ban_other[0]) == 4) && (strlen($ban_other[1]) == 2) && (strlen($ban_other[2]) == 2))
   1.733 +			{
   1.734 +				$time_offset = (isset($user->timezone) && isset($user->dst)) ? (int) $user->timezone + (int) $user->dst : 0;
   1.735 +				$ban_end = max($current_time, gmmktime(0, 0, 0, (int)$ban_other[1], (int)$ban_other[2], (int)$ban_other[0]) - $time_offset);
   1.736 +			}
   1.737 +			else
   1.738 +			{
   1.739 +				trigger_error('LENGTH_BAN_INVALID', E_USER_WARNING);
   1.740 +			}
   1.741 +		}
   1.742 +	}
   1.743 +	else
   1.744 +	{
   1.745 +		$ban_end = 0;
   1.746 +	}
   1.747 +
   1.748 +	$founder = $founder_names = array();
   1.749 +
   1.750 +	if (!$ban_exclude)
   1.751 +	{
   1.752 +		// Create a list of founder...
   1.753 +		$sql = 'SELECT user_id, user_email, username_clean
   1.754 +			FROM ' . USERS_TABLE . '
   1.755 +			WHERE user_type = ' . USER_FOUNDER;
   1.756 +		$result = $db->sql_query($sql);
   1.757 +
   1.758 +		while ($row = $db->sql_fetchrow($result))
   1.759 +		{
   1.760 +			$founder[$row['user_id']] = $row['user_email'];
   1.761 +			$founder_names[$row['user_id']] = $row['username_clean'];
   1.762 +		}
   1.763 +		$db->sql_freeresult($result);
   1.764 +	}
   1.765 +
   1.766 +	$banlist_ary = array();
   1.767 +
   1.768 +	switch ($mode)
   1.769 +	{
   1.770 +		case 'user':
   1.771 +			$type = 'ban_userid';
   1.772 +
   1.773 +			// At the moment we do not support wildcard username banning
   1.774 +
   1.775 +			// Select the relevant user_ids.
   1.776 +			$sql_usernames = array();
   1.777 +
   1.778 +			foreach ($ban_list as $username)
   1.779 +			{
   1.780 +				$username = trim($username);
   1.781 +				if ($username != '')
   1.782 +				{
   1.783 +					$clean_name = utf8_clean_string($username);
   1.784 +					if ($clean_name == $user->data['username_clean'])
   1.785 +					{
   1.786 +						trigger_error('CANNOT_BAN_YOURSELF', E_USER_WARNING);
   1.787 +					}
   1.788 +					if (in_array($clean_name, $founder_names))
   1.789 +					{
   1.790 +						trigger_error('CANNOT_BAN_FOUNDER', E_USER_WARNING);
   1.791 +					}
   1.792 +					$sql_usernames[] = $clean_name;
   1.793 +				}
   1.794 +			}
   1.795 +
   1.796 +			// Make sure we have been given someone to ban
   1.797 +			if (!sizeof($sql_usernames))
   1.798 +			{
   1.799 +				trigger_error('NO_USER_SPECIFIED', E_USER_WARNING);
   1.800 +			}
   1.801 +
   1.802 +			$sql = 'SELECT user_id
   1.803 +				FROM ' . USERS_TABLE . '
   1.804 +				WHERE ' . $db->sql_in_set('username_clean', $sql_usernames);
   1.805 +
   1.806 +			// Do not allow banning yourself, the guest account, or founders.
   1.807 +			$non_bannable = array($user->data['user_id'], ANONYMOUS);
   1.808 +			if (sizeof($founder))
   1.809 +			{
   1.810 +				$sql .= ' AND ' . $db->sql_in_set('user_id', array_merge(array_keys($founder), $non_bannable), true);
   1.811 +			}
   1.812 +			else
   1.813 +			{
   1.814 +				$sql .= ' AND ' . $db->sql_in_set('user_id', $non_bannable, true);
   1.815 +			}
   1.816 +
   1.817 +			$result = $db->sql_query($sql);
   1.818 +
   1.819 +			if ($row = $db->sql_fetchrow($result))
   1.820 +			{
   1.821 +				do
   1.822 +				{
   1.823 +					$banlist_ary[] = (int) $row['user_id'];
   1.824 +				}
   1.825 +				while ($row = $db->sql_fetchrow($result));
   1.826 +			}
   1.827 +			else
   1.828 +			{
   1.829 +				$db->sql_freeresult($result);
   1.830 +				trigger_error('NO_USERS', E_USER_WARNING);
   1.831 +			}
   1.832 +			$db->sql_freeresult($result);
   1.833 +		break;
   1.834 +
   1.835 +		case 'ip':
   1.836 +			$type = 'ban_ip';
   1.837 +
   1.838 +			foreach ($ban_list as $ban_item)
   1.839 +			{
   1.840 +				if (preg_match('#^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[ ]*\-[ ]*([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$#', trim($ban_item), $ip_range_explode))
   1.841 +				{
   1.842 +					// This is an IP range
   1.843 +					// Don't ask about all this, just don't ask ... !
   1.844 +					$ip_1_counter = $ip_range_explode[1];
   1.845 +					$ip_1_end = $ip_range_explode[5];
   1.846 +
   1.847 +					while ($ip_1_counter <= $ip_1_end)
   1.848 +					{
   1.849 +						$ip_2_counter = ($ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[2] : 0;
   1.850 +						$ip_2_end = ($ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[6];
   1.851 +
   1.852 +						if ($ip_2_counter == 0 && $ip_2_end == 254)
   1.853 +						{
   1.854 +							$ip_2_counter = 256;
   1.855 +							$ip_2_fragment = 256;
   1.856 +
   1.857 +							$banlist_ary[] = "$ip_1_counter.*";
   1.858 +						}
   1.859 +
   1.860 +						while ($ip_2_counter <= $ip_2_end)
   1.861 +						{
   1.862 +							$ip_3_counter = ($ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[3] : 0;
   1.863 +							$ip_3_end = ($ip_2_counter < $ip_2_end || $ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[7];
   1.864 +
   1.865 +							if ($ip_3_counter == 0 && $ip_3_end == 254)
   1.866 +							{
   1.867 +								$ip_3_counter = 256;
   1.868 +								$ip_3_fragment = 256;
   1.869 +
   1.870 +								$banlist_ary[] = "$ip_1_counter.$ip_2_counter.*";
   1.871 +							}
   1.872 +
   1.873 +							while ($ip_3_counter <= $ip_3_end)
   1.874 +							{
   1.875 +								$ip_4_counter = ($ip_3_counter == $ip_range_explode[3] && $ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[4] : 0;
   1.876 +								$ip_4_end = ($ip_3_counter < $ip_3_end || $ip_2_counter < $ip_2_end) ? 254 : $ip_range_explode[8];
   1.877 +
   1.878 +								if ($ip_4_counter == 0 && $ip_4_end == 254)
   1.879 +								{
   1.880 +									$ip_4_counter = 256;
   1.881 +									$ip_4_fragment = 256;
   1.882 +
   1.883 +									$banlist_ary[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.*";
   1.884 +								}
   1.885 +
   1.886 +								while ($ip_4_counter <= $ip_4_end)
   1.887 +								{
   1.888 +									$banlist_ary[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.$ip_4_counter";
   1.889 +									$ip_4_counter++;
   1.890 +								}
   1.891 +								$ip_3_counter++;
   1.892 +							}
   1.893 +							$ip_2_counter++;
   1.894 +						}
   1.895 +						$ip_1_counter++;
   1.896 +					}
   1.897 +				}
   1.898 +				else if (preg_match('#^([0-9]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})$#', trim($ban_item)) || preg_match('#^[a-f0-9:]+\*?$#i', trim($ban_item)))
   1.899 +				{
   1.900 +					// Normal IP address
   1.901 +					$banlist_ary[] = trim($ban_item);
   1.902 +				}
   1.903 +				else if (preg_match('#^\*$#', trim($ban_item)))
   1.904 +				{
   1.905 +					// Ban all IPs
   1.906 +					$banlist_ary[] = '*';
   1.907 +				}
   1.908 +				else if (preg_match('#^([\w\-_]\.?){2,}$#is', trim($ban_item)))
   1.909 +				{
   1.910 +					// hostname
   1.911 +					$ip_ary = gethostbynamel(trim($ban_item));
   1.912 +
   1.913 +					if (!empty($ip_ary))
   1.914 +					{
   1.915 +						foreach ($ip_ary as $ip)
   1.916 +						{
   1.917 +							if ($ip)
   1.918 +							{
   1.919 +								if (strlen($ip) > 40)
   1.920 +								{
   1.921 +									continue;
   1.922 +								}
   1.923 +
   1.924 +								$banlist_ary[] = $ip;
   1.925 +							}
   1.926 +						}
   1.927 +					}
   1.928 +				}
   1.929 +
   1.930 +				if (empty($banlist_ary))
   1.931 +				{
   1.932 +					trigger_error('NO_IPS_DEFINED', E_USER_WARNING);
   1.933 +				}
   1.934 +			}
   1.935 +		break;
   1.936 +
   1.937 +		case 'email':
   1.938 +			$type = 'ban_email';
   1.939 +
   1.940 +			foreach ($ban_list as $ban_item)
   1.941 +			{
   1.942 +				$ban_item = trim($ban_item);
   1.943 +
   1.944 +				if (preg_match('#^.*?@*|(([a-z0-9\-]+\.)+([a-z]{2,3}))$#i', $ban_item))
   1.945 +				{
   1.946 +					if (strlen($ban_item) > 100)
   1.947 +					{
   1.948 +						continue;
   1.949 +					}
   1.950 +
   1.951 +					if (!sizeof($founder) || !in_array($ban_item, $founder))
   1.952 +					{
   1.953 +						$banlist_ary[] = $ban_item;
   1.954 +					}
   1.955 +				}
   1.956 +			}
   1.957 +
   1.958 +			if (sizeof($ban_list) == 0)
   1.959 +			{
   1.960 +				trigger_error('NO_EMAILS_DEFINED', E_USER_WARNING);
   1.961 +			}
   1.962 +		break;
   1.963 +
   1.964 +		default:
   1.965 +			trigger_error('NO_MODE', E_USER_WARNING);
   1.966 +		break;
   1.967 +	}
   1.968 +
   1.969 +	// Fetch currently set bans of the specified type and exclude state. Prevent duplicate bans.
   1.970 +	$sql_where = ($type == 'ban_userid') ? 'ban_userid <> 0' : "$type <> ''";
   1.971 +
   1.972 +	$sql = "SELECT $type
   1.973 +		FROM " . BANLIST_TABLE . "
   1.974 +		WHERE $sql_where
   1.975 +			AND ban_exclude = " . (int) $ban_exclude;
   1.976 +	$result = $db->sql_query($sql);
   1.977 +
   1.978 +	// Reset $sql_where, because we use it later...
   1.979 +	$sql_where = '';
   1.980 +
   1.981 +	if ($row = $db->sql_fetchrow($result))
   1.982 +	{
   1.983 +		$banlist_ary_tmp = array();
   1.984 +		do
   1.985 +		{
   1.986 +			switch ($mode)
   1.987 +			{
   1.988 +				case 'user':
   1.989 +					$banlist_ary_tmp[] = $row['ban_userid'];
   1.990 +				break;
   1.991 +
   1.992 +				case 'ip':
   1.993 +					$banlist_ary_tmp[] = $row['ban_ip'];
   1.994 +				break;
   1.995 +
   1.996 +				case 'email':
   1.997 +					$banlist_ary_tmp[] = $row['ban_email'];
   1.998 +				break;
   1.999 +			}
  1.1000 +		}
  1.1001 +		while ($row = $db->sql_fetchrow($result));
  1.1002 +
  1.1003 +		$banlist_ary_tmp = array_intersect($banlist_ary, $banlist_ary_tmp);
  1.1004 +
  1.1005 +		if (sizeof($banlist_ary_tmp))
  1.1006 +		{
  1.1007 +			// One or more entities are already banned/excluded, delete the existing bans, so they can be re-inserted with the given new length
  1.1008 +			$sql = 'DELETE FROM ' . BANLIST_TABLE . '
  1.1009 +				WHERE ' . $db->sql_in_set($type, $banlist_ary_tmp) . '
  1.1010 +					AND ban_exclude = ' . (int) $ban_exclude;
  1.1011 +			$db->sql_query($sql);
  1.1012 +		}
  1.1013 +
  1.1014 +		unset($banlist_ary_tmp);
  1.1015 +	}
  1.1016 +	$db->sql_freeresult($result);
  1.1017 +
  1.1018 +	// We have some entities to ban
  1.1019 +	if (sizeof($banlist_ary))
  1.1020 +	{
  1.1021 +		$sql_ary = array();
  1.1022 +
  1.1023 +		foreach ($banlist_ary as $ban_entry)
  1.1024 +		{
  1.1025 +			$sql_ary[] = array(
  1.1026 +				$type				=> $ban_entry,
  1.1027 +				'ban_start'			=> (int) $current_time,
  1.1028 +				'ban_end'			=> (int) $ban_end,
  1.1029 +				'ban_exclude'		=> (int) $ban_exclude,
  1.1030 +				'ban_reason'		=> (string) $ban_reason,
  1.1031 +				'ban_give_reason'	=> (string) $ban_give_reason,
  1.1032 +			);
  1.1033 +		}
  1.1034 +
  1.1035 +		$db->sql_multi_insert(BANLIST_TABLE, $sql_ary);
  1.1036 +
  1.1037 +		// If we are banning we want to logout anyone matching the ban
  1.1038 +		if (!$ban_exclude)
  1.1039 +		{
  1.1040 +			switch ($mode)
  1.1041 +			{
  1.1042 +				case 'user':
  1.1043 +					$sql_where = 'WHERE ' . $db->sql_in_set('session_user_id', $banlist_ary);
  1.1044 +				break;
  1.1045 +
  1.1046 +				case 'ip':
  1.1047 +					$sql_where = 'WHERE ' . $db->sql_in_set('session_ip', $banlist_ary);
  1.1048 +				break;
  1.1049 +
  1.1050 +				case 'email':
  1.1051 +					$banlist_ary_sql = array();
  1.1052 +
  1.1053 +					foreach ($banlist_ary as $ban_entry)
  1.1054 +					{
  1.1055 +						$banlist_ary_sql[] = (string) str_replace('*', '%', $ban_entry);
  1.1056 +					}
  1.1057 +
  1.1058 +					$sql = 'SELECT user_id
  1.1059 +						FROM ' . USERS_TABLE . '
  1.1060 +						WHERE ' . $db->sql_in_set('user_email', $banlist_ary_sql);
  1.1061 +					$result = $db->sql_query($sql);
  1.1062 +
  1.1063 +					$sql_in = array();
  1.1064 +
  1.1065 +					if ($row = $db->sql_fetchrow($result))
  1.1066 +					{
  1.1067 +						do
  1.1068 +						{
  1.1069 +							$sql_in[] = $row['user_id'];
  1.1070 +						}
  1.1071 +						while ($row = $db->sql_fetchrow($result));
  1.1072 +
  1.1073 +						$sql_where = 'WHERE ' . $db->sql_in_set('session_user_id', $sql_in);
  1.1074 +					}
  1.1075 +					$db->sql_freeresult($result);
  1.1076 +				break;
  1.1077 +			}
  1.1078 +
  1.1079 +			if (isset($sql_where) && $sql_where)
  1.1080 +			{
  1.1081 +				$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
  1.1082 +					$sql_where";
  1.1083 +				$db->sql_query($sql);
  1.1084 +
  1.1085 +				if ($mode == 'user')
  1.1086 +				{
  1.1087 +					$sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . ' ' . ((in_array('*', $banlist_ary)) ? '' : 'WHERE ' . $db->sql_in_set('user_id', $banlist_ary));
  1.1088 +					$db->sql_query($sql);
  1.1089 +				}
  1.1090 +			}
  1.1091 +		}
  1.1092 +
  1.1093 +		// Update log
  1.1094 +		$log_entry = ($ban_exclude) ? 'LOG_BAN_EXCLUDE_' : 'LOG_BAN_';
  1.1095 +
  1.1096 +		// Add to moderator log, admin log and user notes
  1.1097 +		add_log('admin', $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
  1.1098 +		add_log('mod', 0, 0, $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
  1.1099 +		if ($mode == 'user')
  1.1100 +		{
  1.1101 +			foreach ($banlist_ary as $user_id)
  1.1102 +			{
  1.1103 +				add_log('user', $user_id, $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
  1.1104 +			}
  1.1105 +		}
  1.1106 +
  1.1107 +		$cache->destroy('sql', BANLIST_TABLE);
  1.1108 +
  1.1109 +		return true;
  1.1110 +	}
  1.1111 +
  1.1112 +	// There was nothing to ban/exclude. But destroying the cache because of the removal of stale bans.
  1.1113 +	$cache->destroy('sql', BANLIST_TABLE);
  1.1114 +
  1.1115 +	return false;
  1.1116 +}
  1.1117 +
  1.1118 +/**
  1.1119 +* Unban User
  1.1120 +*/
  1.1121 +function user_unban($mode, $ban)
  1.1122 +{
  1.1123 +	global $db, $user, $auth, $cache;
  1.1124 +
  1.1125 +	// Delete stale bans
  1.1126 +	$sql = 'DELETE FROM ' . BANLIST_TABLE . '
  1.1127 +		WHERE ban_end < ' . time() . '
  1.1128 +			AND ban_end <> 0';
  1.1129 +	$db->sql_query($sql);
  1.1130 +
  1.1131 +	if (!is_array($ban))
  1.1132 +	{
  1.1133 +		$ban = array($ban);
  1.1134 +	}
  1.1135 +
  1.1136 +	$unban_sql = array_map('intval', $ban);
  1.1137 +
  1.1138 +	if (sizeof($unban_sql))
  1.1139 +	{
  1.1140 +		// Grab details of bans for logging information later
  1.1141 +		switch ($mode)
  1.1142 +		{
  1.1143 +			case 'user':
  1.1144 +				$sql = 'SELECT u.username AS unban_info, u.user_id
  1.1145 +					FROM ' . USERS_TABLE . ' u, ' . BANLIST_TABLE . ' b
  1.1146 +					WHERE ' . $db->sql_in_set('b.ban_id', $unban_sql) . '
  1.1147 +						AND u.user_id = b.ban_userid';
  1.1148 +			break;
  1.1149 +
  1.1150 +			case 'email':
  1.1151 +				$sql = 'SELECT ban_email AS unban_info
  1.1152 +					FROM ' . BANLIST_TABLE . '
  1.1153 +					WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
  1.1154 +			break;
  1.1155 +
  1.1156 +			case 'ip':
  1.1157 +				$sql = 'SELECT ban_ip AS unban_info
  1.1158 +					FROM ' . BANLIST_TABLE . '
  1.1159 +					WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
  1.1160 +			break;
  1.1161 +		}
  1.1162 +		$result = $db->sql_query($sql);
  1.1163 +
  1.1164 +		$l_unban_list = '';
  1.1165 +		$user_ids_ary = array();
  1.1166 +		while ($row = $db->sql_fetchrow($result))
  1.1167 +		{
  1.1168 +			$l_unban_list .= (($l_unban_list != '') ? ', ' : '') . $row['unban_info'];
  1.1169 +			if ($mode == 'user')
  1.1170 +			{
  1.1171 +				$user_ids_ary[] = $row['user_id'];
  1.1172 +			}
  1.1173 +		}
  1.1174 +		$db->sql_freeresult($result);
  1.1175 +
  1.1176 +		$sql = 'DELETE FROM ' . BANLIST_TABLE . '
  1.1177 +			WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
  1.1178 +		$db->sql_query($sql);
  1.1179 +
  1.1180 +		// Add to moderator log, admin log and user notes
  1.1181 +		add_log('admin', 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
  1.1182 +		add_log('mod', 0, 0, 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
  1.1183 +		if ($mode == 'user')
  1.1184 +		{
  1.1185 +			foreach ($user_ids_ary as $user_id)
  1.1186 +			{
  1.1187 +				add_log('user', $user_id, 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
  1.1188 +			}
  1.1189 +		}
  1.1190 +	}
  1.1191 +
  1.1192 +	$cache->destroy('sql', BANLIST_TABLE);
  1.1193 +
  1.1194 +	return false;
  1.1195 +}
  1.1196 +
  1.1197 +/**
  1.1198 +* Internet Protocol Address Whois
  1.1199 +* RFC3912: WHOIS Protocol Specification
  1.1200 +*
  1.1201 +* @param string $ip		Ip address, either IPv4 or IPv6.
  1.1202 +*
  1.1203 +* @return string		Empty string if not a valid ip address.
  1.1204 +*						Otherwise make_clickable()'ed whois result.
  1.1205 +*/
  1.1206 +function user_ipwhois($ip)
  1.1207 +{
  1.1208 +	if (empty($ip))
  1.1209 +	{
  1.1210 +		return '';
  1.1211 +	}
  1.1212 +
  1.1213 +	if (preg_match(get_preg_expression('ipv4'), $ip))
  1.1214 +	{
  1.1215 +		// IPv4 address
  1.1216 +		$whois_host = 'whois.arin.net.';
  1.1217 +	}
  1.1218 +	else if (preg_match(get_preg_expression('ipv6'), $ip))
  1.1219 +	{
  1.1220 +		// IPv6 address
  1.1221 +		$whois_host = 'whois.sixxs.net.';
  1.1222 +	}
  1.1223 +	else
  1.1224 +	{
  1.1225 +		return '';
  1.1226 +	}
  1.1227 +
  1.1228 +	$ipwhois = '';
  1.1229 +
  1.1230 +	if (($fsk = @fsockopen($whois_host, 43)))
  1.1231 +	{
  1.1232 +		// CRLF as per RFC3912
  1.1233 +		fputs($fsk, "$ip\r\n");
  1.1234 +		while (!feof($fsk))
  1.1235 +		{
  1.1236 +			$ipwhois .= fgets($fsk, 1024);
  1.1237 +		}
  1.1238 +		@fclose($fsk);
  1.1239 +	}
  1.1240 +
  1.1241 +	$match = array();
  1.1242 +
  1.1243 +	// Test for referrals from $whois_host to other whois databases, roll on rwhois
  1.1244 +	if (preg_match('#ReferralServer: whois://(.+)#im', $ipwhois, $match))
  1.1245 +	{
  1.1246 +		if (strpos($match[1], ':') !== false)
  1.1247 +		{
  1.1248 +			$pos	= strrpos($match[1], ':');
  1.1249 +			$server	= substr($match[1], 0, $pos);
  1.1250 +			$port	= (int) substr($match[1], $pos + 1);
  1.1251 +			unset($pos);
  1.1252 +		}
  1.1253 +		else
  1.1254 +		{
  1.1255 +			$server	= $match[1];
  1.1256 +			$port	= 43;
  1.1257 +		}
  1.1258 +
  1.1259 +		$buffer = '';
  1.1260 +
  1.1261 +		if (($fsk = @fsockopen($server, $port)))
  1.1262 +		{
  1.1263 +			fputs($fsk, "$ip\r\n");
  1.1264 +			while (!feof($fsk))
  1.1265 +			{
  1.1266 +				$buffer .= fgets($fsk, 1024);
  1.1267 +			}
  1.1268 +			@fclose($fsk);
  1.1269 +		}
  1.1270 +
  1.1271 +		// Use the result from $whois_host if we don't get any result here
  1.1272 +		$ipwhois = (empty($buffer)) ? $ipwhois : $buffer;
  1.1273 +	}
  1.1274 +
  1.1275 +	$ipwhois = htmlspecialchars($ipwhois);
  1.1276 +
  1.1277 +	// Magic URL ;)
  1.1278 +	return trim(make_clickable($ipwhois, false, ''));
  1.1279 +}
  1.1280 +
  1.1281 +/**
  1.1282 +* Data validation ... used primarily but not exclusively by ucp modules
  1.1283 +*
  1.1284 +* "Master" function for validating a range of data types
  1.1285 +*/
  1.1286 +function validate_data($data, $val_ary)
  1.1287 +{
  1.1288 +	global $user;
  1.1289 +
  1.1290 +	$error = array();
  1.1291 +
  1.1292 +	foreach ($val_ary as $var => $val_seq)
  1.1293 +	{
  1.1294 +		if (!is_array($val_seq[0]))
  1.1295 +		{
  1.1296 +			$val_seq = array($val_seq);
  1.1297 +		}
  1.1298 +
  1.1299 +		foreach ($val_seq as $validate)
  1.1300 +		{
  1.1301 +			$function = array_shift($validate);
  1.1302 +			array_unshift($validate, $data[$var]);
  1.1303 +
  1.1304 +			if ($result = call_user_func_array('validate_' . $function, $validate))
  1.1305 +			{
  1.1306 +				// Since errors are checked later for their language file existence, we need to make sure custom errors are not adjusted.
  1.1307 +				$error[] = (empty($user->lang[$result . '_' . strtoupper($var)])) ? $result : $result . '_' . strtoupper($var);
  1.1308 +			}
  1.1309 +		}
  1.1310 +	}
  1.1311 +
  1.1312 +	return $error;
  1.1313 +}
  1.1314 +
  1.1315 +/**
  1.1316 +* Validate String
  1.1317 +*
  1.1318 +* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1.1319 +*/
  1.1320 +function validate_string($string, $optional = false, $min = 0, $max = 0)
  1.1321 +{
  1.1322 +	if (empty($string) && $optional)
  1.1323 +	{
  1.1324 +		return false;
  1.1325 +	}
  1.1326 +
  1.1327 +	if ($min && utf8_strlen(htmlspecialchars_decode($string)) < $min)
  1.1328 +	{
  1.1329 +		return 'TOO_SHORT';
  1.1330 +	}
  1.1331 +	else if ($max && utf8_strlen(htmlspecialchars_decode($string)) > $max)
  1.1332 +	{
  1.1333 +		return 'TOO_LONG';
  1.1334 +	}
  1.1335 +
  1.1336 +	return false;
  1.1337 +}
  1.1338 +
  1.1339 +/**
  1.1340 +* Validate Number
  1.1341 +*
  1.1342 +* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1.1343 +*/
  1.1344 +function validate_num($num, $optional = false, $min = 0, $max = 1E99)
  1.1345 +{
  1.1346 +	if (empty($num) && $optional)
  1.1347 +	{
  1.1348 +		return false;
  1.1349 +	}
  1.1350 +
  1.1351 +	if ($num < $min)
  1.1352 +	{
  1.1353 +		return 'TOO_SMALL';
  1.1354 +	}
  1.1355 +	else if ($num > $max)
  1.1356 +	{
  1.1357 +		return 'TOO_LARGE';
  1.1358 +	}
  1.1359 +
  1.1360 +	return false;
  1.1361 +}
  1.1362 +
  1.1363 +/**
  1.1364 +* Validate Date
  1.1365 +* @param String $string a date in the dd-mm-yyyy format
  1.1366 +* @return	boolean
  1.1367 +*/
  1.1368 +function validate_date($date_string, $optional = false)
  1.1369 +{
  1.1370 +	$date = explode('-', $date_string);
  1.1371 +	if ((empty($date) || sizeof($date) != 3) && $optional)
  1.1372 +	{
  1.1373 +		return false;
  1.1374 +	}
  1.1375 +	else if ($optional)
  1.1376 +	{
  1.1377 +		for ($field = 0; $field <= 1; $field++)
  1.1378 +		{
  1.1379 +			$date[$field] = (int) $date[$field];
  1.1380 +			if (empty($date[$field]))
  1.1381 +			{
  1.1382 +				$date[$field] = 1;
  1.1383 +			}
  1.1384 +		}
  1.1385 +		$date[2] = (int) $date[2];
  1.1386 +		// assume an arbitrary leap year
  1.1387 +		if (empty($date[2]))
  1.1388 +		{
  1.1389 +			$date[2] = 1980;
  1.1390 +		}
  1.1391 +	}
  1.1392 +
  1.1393 +	if (sizeof($date) != 3 || !checkdate($date[1], $date[0], $date[2]))
  1.1394 +	{
  1.1395 +		return 'INVALID';
  1.1396 +	}
  1.1397 +
  1.1398 +	return false;
  1.1399 +}
  1.1400 +
  1.1401 +
  1.1402 +/**
  1.1403 +* Validate Match
  1.1404 +*
  1.1405 +* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1.1406 +*/
  1.1407 +function validate_match($string, $optional = false, $match = '')
  1.1408 +{
  1.1409 +	if (empty($string) && $optional)
  1.1410 +	{
  1.1411 +		return false;
  1.1412 +	}
  1.1413 +
  1.1414 +	if (empty($match))
  1.1415 +	{
  1.1416 +		return false;
  1.1417 +	}
  1.1418 +
  1.1419 +	if (!preg_match($match, $string))
  1.1420 +	{
  1.1421 +		return 'WRONG_DATA';
  1.1422 +	}
  1.1423 +
  1.1424 +	return false;
  1.1425 +}
  1.1426 +
  1.1427 +/**
  1.1428 +* Validate Language Pack ISO Name
  1.1429 +*
  1.1430 +* Tests whether a language name is valid and installed
  1.1431 +*
  1.1432 +* @param string $lang_iso	The language string to test
  1.1433 +*
  1.1434 +* @return bool|string		Either false if validation succeeded or
  1.1435 +*							a string which will be used as the error message
  1.1436 +*							(with the variable name appended)
  1.1437 +*/
  1.1438 +function validate_language_iso_name($lang_iso)
  1.1439 +{
  1.1440 +	global $db;
  1.1441 +
  1.1442 +	$sql = 'SELECT lang_id
  1.1443 +		FROM ' . LANG_TABLE . "
  1.1444 +		WHERE lang_iso = '" . $db->sql_escape($lang_iso) . "'";
  1.1445 +	$result = $db->sql_query($sql);
  1.1446 +	$lang_id = (int) $db->sql_fetchfield('lang_id');
  1.1447 +	$db->sql_freeresult($result);
  1.1448 +
  1.1449 +	return ($lang_id) ? false : 'WRONG_DATA';
  1.1450 +}
  1.1451 +
  1.1452 +/**
  1.1453 +* Check to see if the username has been taken, or if it is disallowed.
  1.1454 +* Also checks if it includes the " character, which we don't allow in usernames.
  1.1455 +* Used for registering, changing names, and posting anonymously with a username
  1.1456 +*
  1.1457 +* @param string $username The username to check
  1.1458 +* @param string $allowed_username An allowed username, default being $user->data['username']
  1.1459 +*
  1.1460 +* @return	mixed	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1.1461 +*/
  1.1462 +function validate_username($username, $allowed_username = false)
  1.1463 +{
  1.1464 +	global $config, $db, $user, $cache;
  1.1465 +
  1.1466 +	$clean_username = utf8_clean_string($username);
  1.1467 +	$allowed_username = ($allowed_username === false) ? $user->data['username_clean'] : utf8_clean_string($allowed_username);
  1.1468 +
  1.1469 +	if ($allowed_username == $clean_username)
  1.1470 +	{
  1.1471 +		return false;
  1.1472 +	}
  1.1473 +
  1.1474 +	// ... fast checks first.
  1.1475 +	if (strpos($username, '&quot;') !== false || strpos($username, '"') !== false || empty($clean_username))
  1.1476 +	{
  1.1477 +		return 'INVALID_CHARS';
  1.1478 +	}
  1.1479 +
  1.1480 +	$mbstring = $pcre = false;
  1.1481 +
  1.1482 +	// generic UTF-8 character types supported?
  1.1483 +	if ((version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>='))) && @preg_match('/\p{L}/u', 'a') !== false)
  1.1484 +	{
  1.1485 +		$pcre = true;
  1.1486 +	}
  1.1487 +	else if (function_exists('mb_ereg_match'))
  1.1488 +	{
  1.1489 +		mb_regex_encoding('UTF-8');
  1.1490 +		$mbstring = true;
  1.1491 +	}
  1.1492 +
  1.1493 +	switch ($config['allow_name_chars'])
  1.1494 +	{
  1.1495 +		case 'USERNAME_CHARS_ANY':
  1.1496 +			$pcre = true;
  1.1497 +			$regex = '.+';
  1.1498 +		break;
  1.1499 +
  1.1500 +		case 'USERNAME_ALPHA_ONLY':
  1.1501 +			$pcre = true;
  1.1502 +			$regex = '[A-Za-z0-9]+';
  1.1503 +		break;
  1.1504 +
  1.1505 +		case 'USERNAME_ALPHA_SPACERS':
  1.1506 +			$pcre = true;
  1.1507 +			$regex = '[A-Za-z0-9-[\]_+ ]+';
  1.1508 +		break;
  1.1509 +
  1.1510 +		case 'USERNAME_LETTER_NUM':
  1.1511 +			if ($pcre)
  1.1512 +			{
  1.1513 +				$regex = '[\p{Lu}\p{Ll}\p{N}]+';
  1.1514 +			}
  1.1515 +			else if ($mbstring)
  1.1516 +			{
  1.1517 +				$regex = '[[:upper:][:lower:][:digit:]]+';
  1.1518 +			}
  1.1519 +			else
  1.1520 +			{
  1.1521 +				$pcre = true;
  1.1522 +				$regex = '[a-zA-Z0-9]+';
  1.1523 +			}
  1.1524 +		break;
  1.1525 +
  1.1526 +		case 'USERNAME_LETTER_NUM_SPACERS':
  1.1527 +			if ($pcre)
  1.1528 +			{
  1.1529 +				$regex = '[-\]_+ [\p{Lu}\p{Ll}\p{N}]+';
  1.1530 +			}
  1.1531 +			else if ($mbstring)
  1.1532 +			{
  1.1533 +				$regex = '[-\]_+ \[[:upper:][:lower:][:digit:]]+';
  1.1534 +			}
  1.1535 +			else
  1.1536 +			{
  1.1537 +				$pcre = true;
  1.1538 +				$regex = '[-\]_+ [a-zA-Z0-9]+';
  1.1539 +			}
  1.1540 +		break;
  1.1541 +
  1.1542 +		case 'USERNAME_ASCII':
  1.1543 +		default:
  1.1544 +			$pcre = true;
  1.1545 +			$regex = '[\x01-\x7F]+';
  1.1546 +		break;
  1.1547 +	}
  1.1548 +
  1.1549 +	if ($pcre)
  1.1550 +	{
  1.1551 +		if (!preg_match('#^' . $regex . '$#u', $username))
  1.1552 +		{
  1.1553 +			return 'INVALID_CHARS';
  1.1554 +		}
  1.1555 +	}
  1.1556 +	else if ($mbstring)
  1.1557 +	{
  1.1558 +		mb_ereg_search_init($username, '^' . $regex . '$');
  1.1559 +		if (!mb_ereg_search())
  1.1560 +		{
  1.1561 +			return 'INVALID_CHARS';
  1.1562 +		}
  1.1563 +	}
  1.1564 +
  1.1565 +	$sql = 'SELECT username
  1.1566 +		FROM ' . USERS_TABLE . "
  1.1567 +		WHERE username_clean = '" . $db->sql_escape($clean_username) . "'";
  1.1568 +	$result = $db->sql_query($sql);
  1.1569 +	$row = $db->sql_fetchrow($result);
  1.1570 +	$db->sql_freeresult($result);
  1.1571 +
  1.1572 +	if ($row)
  1.1573 +	{
  1.1574 +		return 'USERNAME_TAKEN';
  1.1575 +	}
  1.1576 +
  1.1577 +	$sql = 'SELECT group_name
  1.1578 +		FROM ' . GROUPS_TABLE . "
  1.1579 +		WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($username)) . "'";
  1.1580 +	$result = $db->sql_query($sql);
  1.1581 +	$row = $db->sql_fetchrow($result);
  1.1582 +	$db->sql_freeresult($result);
  1.1583 +
  1.1584 +	if ($row)
  1.1585 +	{
  1.1586 +		return 'USERNAME_TAKEN';
  1.1587 +	}
  1.1588 +
  1.1589 +	$bad_usernames = $cache->obtain_disallowed_usernames();
  1.1590 +
  1.1591 +	foreach ($bad_usernames as $bad_username)
  1.1592 +	{
  1.1593 +		if (preg_match('#^' . $bad_username . '$#', $clean_username))
  1.1594 +		{
  1.1595 +			return 'USERNAME_DISALLOWED';
  1.1596 +		}
  1.1597 +	}
  1.1598 +
  1.1599 +	return false;
  1.1600 +}
  1.1601 +
  1.1602 +/**
  1.1603 +* Check to see if the password meets the complexity settings
  1.1604 +*
  1.1605 +* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1.1606 +*/
  1.1607 +function validate_password($password)
  1.1608 +{
  1.1609 +	global $config, $db, $user;
  1.1610 +
  1.1611 +	if ($password === '' || $config['pass_complex'] === 'PASS_TYPE_ANY')
  1.1612 +	{
  1.1613 +		// Password empty or no password complexity required.
  1.1614 +		return false;
  1.1615 +	}
  1.1616 +
  1.1617 +	$pcre = $mbstring = false;
  1.1618 +
  1.1619 +	// generic UTF-8 character types supported?
  1.1620 +	if ((version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>='))) && @preg_match('/\p{L}/u', 'a') !== false)
  1.1621 +	{
  1.1622 +		$upp = '\p{Lu}';
  1.1623 +		$low = '\p{Ll}';
  1.1624 +		$num = '\p{N}';
  1.1625 +		$sym = '[^\p{Lu}\p{Ll}\p{N}]';
  1.1626 +		$pcre = true;
  1.1627 +	}
  1.1628 +	else if (function_exists('mb_ereg_match'))
  1.1629 +	{
  1.1630 +		mb_regex_encoding('UTF-8');
  1.1631 +		$upp = '[[:upper:]]';
  1.1632 +		$low = '[[:lower:]]';
  1.1633 +		$num = '[[:digit:]]';
  1.1634 +		$sym = '[^[:upper:][:lower:][:digit:]]';
  1.1635 +		$mbstring = true;
  1.1636 +	}
  1.1637 +	else
  1.1638 +	{
  1.1639 +		$upp = '[A-Z]';
  1.1640 +		$low = '[a-z]';
  1.1641 +		$num = '[0-9]';
  1.1642 +		$sym = '[^A-Za-z0-9]';
  1.1643 +		$pcre = true;
  1.1644 +	}
  1.1645 +
  1.1646 +	$chars = array();
  1.1647 +
  1.1648 +	switch ($config['pass_complex'])
  1.1649 +	{
  1.1650 +		// No break statements below ...
  1.1651 +		// We require strong passwords in case pass_complex is not set or is invalid
  1.1652 +		default:
  1.1653 +
  1.1654 +		// Require mixed case letters, numbers and symbols
  1.1655 +		case 'PASS_TYPE_SYMBOL':
  1.1656 +			$chars[] = $sym;
  1.1657 +
  1.1658 +		// Require mixed case letters and numbers
  1.1659 +		case 'PASS_TYPE_ALPHA':
  1.1660 +			$chars[] = $num;
  1.1661 +
  1.1662 +		// Require mixed case letters
  1.1663 +		case 'PASS_TYPE_CASE':
  1.1664 +			$chars[] = $low;
  1.1665 +			$chars[] = $upp;
  1.1666 +	}
  1.1667 +
  1.1668 +	if ($pcre)
  1.1669 +	{
  1.1670 +		foreach ($chars as $char)
  1.1671 +		{
  1.1672 +			if (!preg_match('#' . $char . '#u', $password))
  1.1673 +			{
  1.1674 +				return 'INVALID_CHARS';
  1.1675 +			}
  1.1676 +		}
  1.1677 +	}
  1.1678 +	else if ($mbstring)
  1.1679 +	{
  1.1680 +		foreach ($chars as $char)
  1.1681 +		{
  1.1682 +			if (mb_ereg($char, $password) === false)
  1.1683 +			{
  1.1684 +				return 'INVALID_CHARS';
  1.1685 +			}
  1.1686 +		}
  1.1687 +	}
  1.1688 +
  1.1689 +	return false;
  1.1690 +}
  1.1691 +
  1.1692 +/**
  1.1693 +* Check to see if email address is banned or already present in the DB
  1.1694 +*
  1.1695 +* @param string $email The email to check
  1.1696 +* @param string $allowed_email An allowed email, default being $user->data['user_email']
  1.1697 +*
  1.1698 +* @return mixed Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1.1699 +*/
  1.1700 +function validate_email($email, $allowed_email = false)
  1.1701 +{
  1.1702 +	global $config, $db, $user;
  1.1703 +
  1.1704 +	$email = strtolower($email);
  1.1705 +	$allowed_email = ($allowed_email === false) ? strtolower($user->data['user_email']) : strtolower($allowed_email);
  1.1706 +
  1.1707 +	if ($allowed_email == $email)
  1.1708 +	{
  1.1709 +		return false;
  1.1710 +	}
  1.1711 +
  1.1712 +	if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
  1.1713 +	{
  1.1714 +		return 'EMAIL_INVALID';
  1.1715 +	}
  1.1716 +
  1.1717 +	// Check MX record.
  1.1718 +	// The idea for this is from reading the UseBB blog/announcement. :)
  1.1719 +	if ($config['email_check_mx'])
  1.1720 +	{
  1.1721 +		list(, $domain) = explode('@', $email);
  1.1722 +
  1.1723 +		if (phpbb_checkdnsrr($domain, 'A') === false && phpbb_checkdnsrr($domain, 'MX') === false)
  1.1724 +		{
  1.1725 +			return 'DOMAIN_NO_MX_RECORD';
  1.1726 +		}
  1.1727 +	}
  1.1728 +
  1.1729 +	if (($ban_reason = $user->check_ban(false, false, $email, true)) !== false)
  1.1730 +	{
  1.1731 +		return ($ban_reason === true) ? 'EMAIL_BANNED' : $ban_reason;
  1.1732 +	}
  1.1733 +
  1.1734 +	if (!$config['allow_emailreuse'])
  1.1735 +	{
  1.1736 +		$sql = 'SELECT user_email_hash
  1.1737 +			FROM ' . USERS_TABLE . "
  1.1738 +			WHERE user_email_hash = " . $db->sql_escape(phpbb_email_hash($email));
  1.1739 +		$result = $db->sql_query($sql);
  1.1740 +		$row = $db->sql_fetchrow($result);
  1.1741 +		$db->sql_freeresult($result);
  1.1742 +
  1.1743 +		if ($row)
  1.1744 +		{
  1.1745 +			return 'EMAIL_TAKEN';
  1.1746 +		}
  1.1747 +	}
  1.1748 +
  1.1749 +	return false;
  1.1750 +}
  1.1751 +
  1.1752 +/**
  1.1753 +* Validate jabber address
  1.1754 +* Taken from the jabber class within flyspray (see author notes)
  1.1755 +*
  1.1756 +* @author flyspray.org
  1.1757 +*/
  1.1758 +function validate_jabber($jid)
  1.1759 +{
  1.1760 +	if (!$jid)
  1.1761 +	{
  1.1762 +		return false;
  1.1763 +	}
  1.1764 +
  1.1765 +	$seperator_pos = strpos($jid, '@');
  1.1766 +
  1.1767 +	if ($seperator_pos === false)
  1.1768 +	{
  1.1769 +		return 'WRONG_DATA';
  1.1770 +	}
  1.1771 +
  1.1772 +	$username = substr($jid, 0, $seperator_pos);
  1.1773 +	$realm = substr($jid, $seperator_pos + 1);
  1.1774 +
  1.1775 +	if (strlen($username) == 0 || strlen($realm) < 3)
  1.1776 +	{
  1.1777 +		return 'WRONG_DATA';
  1.1778 +	}
  1.1779 +
  1.1780 +	$arr = explode('.', $realm);
  1.1781 +
  1.1782 +	if (sizeof($arr) == 0)
  1.1783 +	{
  1.1784 +		return 'WRONG_DATA';
  1.1785 +	}
  1.1786 +
  1.1787 +	foreach ($arr as $part)
  1.1788 +	{
  1.1789 +		if (substr($part, 0, 1) == '-' || substr($part, -1, 1) == '-')
  1.1790 +		{
  1.1791 +			return 'WRONG_DATA';
  1.1792 +		}
  1.1793 +
  1.1794 +		if (!preg_match("@^[a-zA-Z0-9-.]+$@", $part))
  1.1795 +		{
  1.1796 +			return 'WRONG_DATA';
  1.1797 +		}
  1.1798 +	}
  1.1799 +
  1.1800 +	$boundary = array(array(0, 127), array(192, 223), array(224, 239), array(240, 247), array(248, 251), array(252, 253));
  1.1801 +
  1.1802 +	// Prohibited Characters RFC3454 + RFC3920
  1.1803 +	$prohibited = array(
  1.1804 +		// Table C.1.1
  1.1805 +		array(0x0020, 0x0020),		// SPACE
  1.1806 +		// Table C.1.2
  1.1807 +		array(0x00A0, 0x00A0),		// NO-BREAK SPACE
  1.1808 +		array(0x1680, 0x1680),		// OGHAM SPACE MARK
  1.1809 +		array(0x2000, 0x2001),		// EN QUAD
  1.1810 +		array(0x2001, 0x2001),		// EM QUAD
  1.1811 +		array(0x2002, 0x2002),		// EN SPACE
  1.1812 +		array(0x2003, 0x2003),		// EM SPACE
  1.1813 +		array(0x2004, 0x2004),		// THREE-PER-EM SPACE
  1.1814 +		array(0x2005, 0x2005),		// FOUR-PER-EM SPACE
  1.1815 +		array(0x2006, 0x2006),		// SIX-PER-EM SPACE
  1.1816 +		array(0x2007, 0x2007),		// FIGURE SPACE
  1.1817 +		array(0x2008, 0x2008),		// PUNCTUATION SPACE
  1.1818 +		array(0x2009, 0x2009),		// THIN SPACE
  1.1819 +		array(0x200A, 0x200A),		// HAIR SPACE
  1.1820 +		array(0x200B, 0x200B),		// ZERO WIDTH SPACE
  1.1821 +		array(0x202F, 0x202F),		// NARROW NO-BREAK SPACE
  1.1822 +		array(0x205F, 0x205F),		// MEDIUM MATHEMATICAL SPACE
  1.1823 +		array(0x3000, 0x3000),		// IDEOGRAPHIC SPACE
  1.1824 +		// Table C.2.1
  1.1825 +		array(0x0000, 0x001F),		// [CONTROL CHARACTERS]
  1.1826 +		array(0x007F, 0x007F),		// DELETE
  1.1827 +		// Table C.2.2
  1.1828 +		array(0x0080, 0x009F),		// [CONTROL CHARACTERS]
  1.1829 +		array(0x06DD, 0x06DD),		// ARABIC END OF AYAH
  1.1830 +		array(0x070F, 0x070F),		// SYRIAC ABBREVIATION MARK
  1.1831 +		array(0x180E, 0x180E),		// MONGOLIAN VOWEL SEPARATOR
  1.1832 +		array(0x200C, 0x200C), 		// ZERO WIDTH NON-JOINER
  1.1833 +		array(0x200D, 0x200D),		// ZERO WIDTH JOINER
  1.1834 +		array(0x2028, 0x2028),		// LINE SEPARATOR
  1.1835 +		array(0x2029, 0x2029),		// PARAGRAPH SEPARATOR
  1.1836 +		array(0x2060, 0x2060),		// WORD JOINER
  1.1837 +		array(0x2061, 0x2061),		// FUNCTION APPLICATION
  1.1838 +		array(0x2062, 0x2062),		// INVISIBLE TIMES
  1.1839 +		array(0x2063, 0x2063),		// INVISIBLE SEPARATOR
  1.1840 +		array(0x206A, 0x206F),		// [CONTROL CHARACTERS]
  1.1841 +		array(0xFEFF, 0xFEFF),		// ZERO WIDTH NO-BREAK SPACE
  1.1842 +		array(0xFFF9, 0xFFFC),		// [CONTROL CHARACTERS]
  1.1843 +		array(0x1D173, 0x1D17A),	// [MUSICAL CONTROL CHARACTERS]
  1.1844 +		// Table C.3
  1.1845 +		array(0xE000, 0xF8FF),		// [PRIVATE USE, PLANE 0]
  1.1846 +		array(0xF0000, 0xFFFFD),	// [PRIVATE USE, PLANE 15]
  1.1847 +		array(0x100000, 0x10FFFD),	// [PRIVATE USE, PLANE 16]
  1.1848 +		// Table C.4
  1.1849 +		array(0xFDD0, 0xFDEF),		// [NONCHARACTER CODE POINTS]
  1.1850 +		array(0xFFFE, 0xFFFF),		// [NONCHARACTER CODE POINTS]
  1.1851 +		array(0x1FFFE, 0x1FFFF),	// [NONCHARACTER CODE POINTS]
  1.1852 +		array(0x2FFFE, 0x2FFFF),	// [NONCHARACTER CODE POINTS]
  1.1853 +		array(0x3FFFE, 0x3FFFF),	// [NONCHARACTER CODE POINTS]
  1.1854 +		array(0x4FFFE, 0x4FFFF),	// [NONCHARACTER CODE POINTS]
  1.1855 +		array(0x5FFFE, 0x5FFFF),	// [NONCHARACTER CODE POINTS]
  1.1856 +		array(0x6FFFE, 0x6FFFF),	// [NONCHARACTER CODE POINTS]
  1.1857 +		array(0x7FFFE, 0x7FFFF),	// [NONCHARACTER CODE POINTS]
  1.1858 +		array(0x8FFFE, 0x8FFFF),	// [NONCHARACTER CODE POINTS]
  1.1859 +		array(0x9FFFE, 0x9FFFF),	// [NONCHARACTER CODE POINTS]
  1.1860 +		array(0xAFFFE, 0xAFFFF),	// [NONCHARACTER CODE POINTS]
  1.1861 +		array(0xBFFFE, 0xBFFFF),	// [NONCHARACTER CODE POINTS]
  1.1862 +		array(0xCFFFE, 0xCFFFF),	// [NONCHARACTER CODE POINTS]
  1.1863 +		array(0xDFFFE, 0xDFFFF),	// [NONCHARACTER CODE POINTS]
  1.1864 +		array(0xEFFFE, 0xEFFFF),	// [NONCHARACTER CODE POINTS]
  1.1865 +		array(0xFFFFE, 0xFFFFF),	// [NONCHARACTER CODE POINTS]
  1.1866 +		array(0x10FFFE, 0x10FFFF),	// [NONCHARACTER CODE POINTS]
  1.1867 +		// Table C.5
  1.1868 +		array(0xD800, 0xDFFF),		// [SURROGATE CODES]
  1.1869 +		// Table C.6
  1.1870 +		array(0xFFF9, 0xFFF9),		// INTERLINEAR ANNOTATION ANCHOR
  1.1871 +		array(0xFFFA, 0xFFFA),		// INTERLINEAR ANNOTATION SEPARATOR
  1.1872 +		array(0xFFFB, 0xFFFB),		// INTERLINEAR ANNOTATION TERMINATOR
  1.1873 +		array(0xFFFC, 0xFFFC),		// OBJECT REPLACEMENT CHARACTER
  1.1874 +		array(0xFFFD, 0xFFFD),		// REPLACEMENT CHARACTER
  1.1875 +		// Table C.7
  1.1876 +		array(0x2FF0, 0x2FFB),		// [IDEOGRAPHIC DESCRIPTION CHARACTERS]
  1.1877 +		// Table C.8
  1.1878 +		array(0x0340, 0x0340),		// COMBINING GRAVE TONE MARK
  1.1879 +		array(0x0341, 0x0341),		// COMBINING ACUTE TONE MARK
  1.1880 +		array(0x200E, 0x200E),		// LEFT-TO-RIGHT MARK
  1.1881 +		array(0x200F, 0x200F),		// RIGHT-TO-LEFT MARK
  1.1882 +		array(0x202A, 0x202A),		// LEFT-TO-RIGHT EMBEDDING
  1.1883 +		array(0x202B, 0x202B),		// RIGHT-TO-LEFT EMBEDDING
  1.1884 +		array(0x202C, 0x202C),		// POP DIRECTIONAL FORMATTING
  1.1885 +		array(0x202D, 0x202D),		// LEFT-TO-RIGHT OVERRIDE
  1.1886 +		array(0x202E, 0x202E),		// RIGHT-TO-LEFT OVERRIDE
  1.1887 +		array(0x206A, 0x206A),		// INHIBIT SYMMETRIC SWAPPING
  1.1888 +		array(0x206B, 0x206B),		// ACTIVATE SYMMETRIC SWAPPING
  1.1889 +		array(0x206C, 0x206C),		// INHIBIT ARABIC FORM SHAPING
  1.1890 +		array(0x206D, 0x206D),		// ACTIVATE ARABIC FORM SHAPING
  1.1891 +		array(0x206E, 0x206E),		// NATIONAL DIGIT SHAPES
  1.1892 +		array(0x206F, 0x206F),		// NOMINAL DIGIT SHAPES
  1.1893 +		// Table C.9
  1.1894 +		array(0xE0001, 0xE0001),	// LANGUAGE TAG
  1.1895 +		array(0xE0020, 0xE007F),	// [TAGGING CHARACTERS]
  1.1896 +		// RFC3920
  1.1897 +		array(0x22, 0x22),			// "
  1.1898 +		array(0x26, 0x26),			// &
  1.1899 +		array(0x27, 0x27),			// '
  1.1900 +		array(0x2F, 0x2F),			// /
  1.1901 +		array(0x3A, 0x3A),			// :
  1.1902 +		array(0x3C, 0x3C),			// <
  1.1903 +		array(0x3E, 0x3E),			// >
  1.1904 +		array(0x40, 0x40)			// @
  1.1905 +	);
  1.1906 +
  1.1907 +	$pos = 0;
  1.1908 +	$result = true;
  1.1909 +
  1.1910 +	while ($pos < strlen($username))
  1.1911 +	{
  1.1912 +		$len = $uni = 0;
  1.1913 +		for ($i = 0; $i <= 5; $i++)
  1.1914 +		{
  1.1915 +			if (ord($username[$pos]) >= $boundary[$i][0] && ord($username[$pos]) <= $boundary[$i][1])
  1.1916 +			{
  1.1917 +				$len = $i + 1;
  1.1918 +				$uni = (ord($username[$pos]) - $boundary[$i][0]) * pow(2, $i * 6);
  1.1919 +
  1.1920 +				for ($k = 1; $k < $len; $k++)
  1.1921 +				{
  1.1922 +					$uni += (ord($username[$pos + $k]) - 128) * pow(2, ($i - $k) * 6);
  1.1923 +				}
  1.1924 +
  1.1925 +				break;
  1.1926 +			}
  1.1927 +		}
  1.1928 +
  1.1929 +		if ($len == 0)
  1.1930 +		{
  1.1931 +			return 'WRONG_DATA';
  1.1932 +		}
  1.1933 +
  1.1934 +		foreach ($prohibited as $pval)
  1.1935 +		{
  1.1936 +			if ($uni >= $pval[0] && $uni <= $pval[1])
  1.1937 +			{
  1.1938 +				$result = false;
  1.1939 +				break 2;
  1.1940 +			}
  1.1941 +		}
  1.1942 +
  1.1943 +		$pos = $pos + $len;
  1.1944 +	}
  1.1945 +
  1.1946 +	if (!$result)
  1.1947 +	{
  1.1948 +		return 'WRONG_DATA';
  1.1949 +	}
  1.1950 +
  1.1951 +	return false;
  1.1952 +}
  1.1953 +
  1.1954 +/**
  1.1955 +* Remove avatar
  1.1956 +*/
  1.1957 +function avatar_delete($mode, $row, $clean_db = false)
  1.1958 +{
  1.1959 +	global $phpbb_root_path, $config, $db, $user;
  1.1960 +
  1.1961 +	// Check if the users avatar is actually *not* a group avatar
  1.1962 +	if ($mode == 'user')
  1.1963 +	{
  1.1964 +		if (strpos($row['user_avatar'], 'g') === 0 || (((int)$row['user_avatar'] !== 0) && ((int)$row['user_avatar'] !== (int)$row['user_id'])))
  1.1965 +		{
  1.1966 +			return false;
  1.1967 +		}
  1.1968 +	}
  1.1969 +
  1.1970 +	if ($clean_db)
  1.1971 +	{
  1.1972 +		avatar_remove_db($row[$mode . '_avatar']);
  1.1973 +	}
  1.1974 +	$filename = get_avatar_filename($row[$mode . '_avatar']);
  1.1975 +	if (file_exists($phpbb_root_path . $config['avatar_path'] . '/' . $filename))
  1.1976 +	{
  1.1977 +		@unlink($phpbb_root_path . $config['avatar_path'] . '/' . $filename);
  1.1978 +		return true;
  1.1979 +	}
  1.1980 +
  1.1981 +	return false;
  1.1982 +}
  1.1983 +
  1.1984 +/**
  1.1985 +* Remote avatar linkage
  1.1986 +*/
  1.1987 +function avatar_remote($data, &$error)
  1.1988 +{
  1.1989 +	global $config, $db, $user, $phpbb_root_path, $phpEx;
  1.1990 +
  1.1991 +	if (!preg_match('#^(http|https|ftp)://#i', $data['remotelink']))
  1.1992 +	{
  1.1993 +		$data['remotelink'] = 'http://' . $data['remotelink'];
  1.1994 +	}
  1.1995 +	if (!preg_match('#^(http|https|ftp)://(?:(.*?\.)*?[a-z0-9\-]+?\.[a-z]{2,4}|(?:\d{1,3}\.){3,5}\d{1,3}):?([0-9]*?).*?\.(gif|jpg|jpeg|png)$#i', $data['remotelink']))
  1.1996 +	{
  1.1997 +		$error[] = $user->lang['AVATAR_URL_INVALID'];
  1.1998 +		return false;
  1.1999 +	}
  1.2000 +
  1.2001 +	// Make sure getimagesize works...
  1.2002 +	if (($image_data = @getimagesize($data['remotelink'])) === false && (empty($data['width']) || empty($data['height'])))
  1.2003 +	{
  1.2004 +		$error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
  1.2005 +		return false;
  1.2006 +	}
  1.2007 +
  1.2008 +	if (!empty($image_data) && ($image_data[0] < 2 || $image_data[1] < 2))
  1.2009 +	{
  1.2010 +		$error[] = $user->lang['AVATAR_NO_SIZE'];
  1.2011 +		return false;
  1.2012 +	}
  1.2013 +
  1.2014 +	$width = ($data['width'] && $data['height']) ? $data['width'] : $image_data[0];
  1.2015 +	$height = ($data['width'] && $data['height']) ? $data['height'] : $image_data[1];
  1.2016 +
  1.2017 +	if ($width < 2 || $height < 2)
  1.2018 +	{
  1.2019 +		$error[] = $user->lang['AVATAR_NO_SIZE'];
  1.2020 +		return false;
  1.2021 +	}
  1.2022 +
  1.2023 +	// Check image type
  1.2024 +	include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
  1.2025 +	$types = fileupload::image_types();
  1.2026 +	$extension = strtolower(filespec::get_extension($data['remotelink']));
  1.2027 +
  1.2028 +	if (!empty($image_data) && (!isset($types[$image_data[2]]) || !in_array($extension, $types[$image_data[2]])))
  1.2029 +	{
  1.2030 +		if (!isset($types[$image_data[2]]))
  1.2031 +		{
  1.2032 +			$error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
  1.2033 +		}
  1.2034 +		else
  1.2035 +		{
  1.2036 +			$error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$image_data[2]][0], $extension);
  1.2037 +		}
  1.2038 +		return false;
  1.2039 +	}
  1.2040 +
  1.2041 +	if ($config['avatar_max_width'] || $config['avatar_max_height'])
  1.2042 +	{
  1.2043 +		if ($width > $config['avatar_max_width'] || $height > $config['avatar_max_height'])
  1.2044 +		{
  1.2045 +			$error[] = sprintf($user->lang['AVATAR_WRONG_SIZE'], $config['avatar_min_width'], $config['avatar_min_height'], $config['avatar_max_width'], $config['avatar_max_height'], $width, $height);
  1.2046 +			return false;
  1.2047 +		}
  1.2048 +	}
  1.2049 +
  1.2050 +	if ($config['avatar_min_width'] || $config['avatar_min_height'])
  1.2051 +	{
  1.2052 +		if ($width < $config['avatar_min_width'] || $height < $config['avatar_min_height'])
  1.2053 +		{
  1.2054 +			$error[] = sprintf($user->lang['AVATAR_WRONG_SIZE'], $config['avatar_min_width'], $config['avatar_min_height'], $config['avatar_max_width'], $config['avatar_max_height'], $width, $height);
  1.2055 +			return false;
  1.2056 +		}
  1.2057 +	}
  1.2058 +
  1.2059 +	return array(AVATAR_REMOTE, $data['remotelink'], $width, $height);
  1.2060 +}
  1.2061 +
  1.2062 +/**
  1.2063 +* Avatar upload using the upload class
  1.2064 +*/
  1.2065 +function avatar_upload($data, &$error)
  1.2066 +{
  1.2067 +	global $phpbb_root_path, $config, $db, $user, $phpEx;
  1.2068 +
  1.2069 +	// Init upload class
  1.2070 +	include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
  1.2071 +	$upload = new fileupload('AVATAR_', array('jpg', 'jpeg', 'gif', 'png'), $config['avatar_filesize'], $config['avatar_min_width'], $config['avatar_min_height'], $config['avatar_max_width'], $config['avatar_max_height'], (isset($config['mime_triggers']) ? explode('|', $config['mime_triggers']) : false));
  1.2072 +
  1.2073 +	if (!empty($_FILES['uploadfile']['name']))
  1.2074 +	{
  1.2075 +		$file = $upload->form_upload('uploadfile');
  1.2076 +	}
  1.2077 +	else
  1.2078 +	{
  1.2079 +		$file = $upload->remote_upload($data['uploadurl']);
  1.2080 +	}
  1.2081 +
  1.2082 +	$prefix = $config['avatar_salt'] . '_';
  1.2083 +	$file->clean_filename('avatar', $prefix, $data['user_id']);
  1.2084 +
  1.2085 +	$destination = $config['avatar_path'];
  1.2086 +
  1.2087 +	// Adjust destination path (no trailing slash)
  1.2088 +	if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
  1.2089 +	{
  1.2090 +		$destination = substr($destination, 0, -1);
  1.2091 +	}
  1.2092 +
  1.2093 +	$destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
  1.2094 +	if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
  1.2095 +	{
  1.2096 +		$destination = '';
  1.2097 +	}
  1.2098 +
  1.2099 +	// Move file and overwrite any existing image
  1.2100 +	$file->move_file($destination, true);
  1.2101 +
  1.2102 +	if (sizeof($file->error))
  1.2103 +	{
  1.2104 +		$file->remove();
  1.2105 +		$error = array_merge($error, $file->error);
  1.2106 +	}
  1.2107 +
  1.2108 +	return array(AVATAR_UPLOAD, $data['user_id'] . '_' . time() . '.' . $file->get('extension'), $file->get('width'), $file->get('height'));
  1.2109 +}
  1.2110 +
  1.2111 +/**
  1.2112 +* Generates avatar filename from the database entry
  1.2113 +*/
  1.2114 +function get_avatar_filename($avatar_entry)
  1.2115 +{
  1.2116 +	global $config;
  1.2117 +
  1.2118 +
  1.2119 +	if ($avatar_entry[0] === 'g')
  1.2120 +	{
  1.2121 +		$avatar_group = true;
  1.2122 +		$avatar_entry = substr($avatar_entry, 1);
  1.2123 +	}
  1.2124 +	else
  1.2125 +	{
  1.2126 +		$avatar_group = false;
  1.2127 +	}
  1.2128 +	$ext 			= substr(strrchr($avatar_entry, '.'), 1);
  1.2129 +	$avatar_entry	= intval($avatar_entry);
  1.2130 +	return $config['avatar_salt'] . '_' . (($avatar_group) ? 'g' : '') . $avatar_entry . '.' . $ext;
  1.2131 +}
  1.2132 +
  1.2133 +/**
  1.2134 +* Avatar Gallery
  1.2135 +*/
  1.2136 +function avatar_gallery($category, $avatar_select, $items_per_column, $block_var = 'avatar_row')
  1.2137 +{
  1.2138 +	global $user, $cache, $template;
  1.2139 +	global $config, $phpbb_root_path;
  1.2140 +
  1.2141 +	$avatar_list = array();
  1.2142 +
  1.2143 +	$path = $phpbb_root_path . $config['avatar_gallery_path'];
  1.2144 +
  1.2145 +	if (!file_exists($path) || !is_dir($path))
  1.2146 +	{
  1.2147 +		$avatar_list = array($user->lang['NO_AVATAR_CATEGORY'] => array());
  1.2148 +	}
  1.2149 +	else
  1.2150 +	{
  1.2151 +		// Collect images
  1.2152 +		$dp = @opendir($path);
  1.2153 +
  1.2154 +		if (!$dp)
  1.2155 +		{
  1.2156 +			return array($user->lang['NO_AVATAR_CATEGORY'] => array());
  1.2157 +		}
  1.2158 +
  1.2159 +		while (($file = readdir($dp)) !== false)
  1.2160 +		{
  1.2161 +			if ($file[0] != '.' && preg_match('#^[^&"\'<>]+$#i', $file) && is_dir("$path/$file"))
  1.2162 +			{
  1.2163 +				$avatar_row_count = $avatar_col_count = 0;
  1.2164 +
  1.2165 +				if ($dp2 = @opendir("$path/$file"))
  1.2166 +				{
  1.2167 +					while (($sub_file = readdir($dp2)) !== false)
  1.2168 +					{
  1.2169 +						if (preg_match('#^[^&\'"<>]+\.(?:gif|png|jpe?g)$#i', $sub_file))
  1.2170 +						{
  1.2171 +							$avatar_list[$file][$avatar_row_count][$avatar_col_count] = array(
  1.2172 +								'file'		=> rawurlencode($file) . '/' . rawurlencode($sub_file),
  1.2173 +								'filename'	=> rawurlencode($sub_file),
  1.2174 +								'name'		=> ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $sub_file))),
  1.2175 +							);
  1.2176 +							$avatar_col_count++;
  1.2177 +							if ($avatar_col_count == $items_per_column)
  1.2178 +							{
  1.2179 +								$avatar_row_count++;
  1.2180 +								$avatar_col_count = 0;
  1.2181 +							}
  1.2182 +						}
  1.2183 +					}
  1.2184 +					closedir($dp2);
  1.2185 +				}
  1.2186 +			}
  1.2187 +		}
  1.2188 +		closedir($dp);
  1.2189 +	}
  1.2190 +
  1.2191 +	if (!sizeof($avatar_list))
  1.2192 +	{
  1.2193 +		$avatar_list = array($user->lang['NO_AVATAR_CATEGORY'] => array());
  1.2194 +	}
  1.2195 +
  1.2196 +	@ksort($avatar_list);
  1.2197 +
  1.2198 +	$category = (!$category) ? key($avatar_list) : $category;
  1.2199 +	$avatar_categories = array_keys($avatar_list);
  1.2200 +
  1.2201 +	$s_category_options = '';
  1.2202 +	foreach ($avatar_categories as $cat)
  1.2203 +	{
  1.2204 +		$s_category_options .= '<option value="' . $cat . '"' . (($cat == $category) ? ' selected="selected"' : '') . '>' . $cat . '</option>';
  1.2205 +	}
  1.2206 +
  1.2207 +	$template->assign_vars(array(
  1.2208 +		'S_AVATARS_ENABLED'		=> true,
  1.2209 +		'S_IN_AVATAR_GALLERY'	=> true,
  1.2210 +		'S_CAT_OPTIONS'			=> $s_category_options)
  1.2211 +	);
  1.2212 +
  1.2213 +	$avatar_list = (isset($avatar_list[$category])) ? $avatar_list[$category] : array();
  1.2214 +
  1.2215 +	foreach ($avatar_list as $avatar_row_ary)
  1.2216 +	{
  1.2217 +		$template->assign_block_vars($block_var, array());
  1.2218 +
  1.2219 +		foreach ($avatar_row_ary as $avatar_col_ary)
  1.2220 +		{
  1.2221 +			$template->assign_block_vars($block_var . '.avatar_column', array(
  1.2222 +				'AVATAR_IMAGE'	=> $phpbb_root_path . $config['avatar_gallery_path'] . '/' . $avatar_col_ary['file'],
  1.2223 +				'AVATAR_NAME'	=> $avatar_col_ary['name'],
  1.2224 +				'AVATAR_FILE'	=> $avatar_col_ary['filename'])
  1.2225 +			);
  1.2226 +
  1.2227 +			$template->assign_block_vars($block_var . '.avatar_option_column', array(
  1.2228 +				'AVATAR_IMAGE'	=> $phpbb_root_path . $config['avatar_gallery_path'] . '/' . $avatar_col_ary['file'],
  1.2229 +				'S_OPTIONS_AVATAR'	=> $avatar_col_ary['filename'])
  1.2230 +			);
  1.2231 +		}
  1.2232 +	}
  1.2233 +
  1.2234 +	return $avatar_list;
  1.2235 +}
  1.2236 +
  1.2237 +
  1.2238 +/**
  1.2239 +* Tries to (re-)establish avatar dimensions
  1.2240 +*/
  1.2241 +function avatar_get_dimensions($avatar, $avatar_type, &$error, $current_x = 0, $current_y = 0)
  1.2242 +{
  1.2243 +	global $config, $phpbb_root_path, $user;
  1.2244 +
  1.2245 +	switch ($avatar_type)
  1.2246 +	{
  1.2247 +		case AVATAR_REMOTE :
  1.2248 +			break;
  1.2249 +
  1.2250 +		case AVATAR_UPLOAD :
  1.2251 +			$avatar = $phpbb_root_path . $config['avatar_path'] . '/' . get_avatar_filename($avatar);
  1.2252 +			break;
  1.2253 +
  1.2254 +		case AVATAR_GALLERY :
  1.2255 +			$avatar = $phpbb_root_path . $config['avatar_gallery_path'] . '/' . $avatar ;
  1.2256 +			break;
  1.2257 +	}
  1.2258 +
  1.2259 +	// Make sure getimagesize works...
  1.2260 +	if (($image_data = @getimagesize($avatar)) === false)
  1.2261 +	{
  1.2262 +		$error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
  1.2263 +		return false;
  1.2264 +	}
  1.2265 +
  1.2266 +	if ($image_data[0] < 2 || $image_data[1] < 2)
  1.2267 +	{
  1.2268 +		$error[] = $user->lang['AVATAR_NO_SIZE'];
  1.2269 +		return false;
  1.2270 +	}
  1.2271 +
  1.2272 +	// try to maintain ratio
  1.2273 +	if (!(empty($current_x) && empty($current_y)))
  1.2274 +	{
  1.2275 +		if ($current_x != 0)
  1.2276 +		{
  1.2277 +			$image_data[1] = (int) floor(($current_x / $image_data[0]) * $image_data[1]);
  1.2278 +			$image_data[1] = min($config['avatar_max_height'], $image_data[1]);
  1.2279 +			$image_data[1] = max($config['avatar_min_height'], $image_data[1]);
  1.2280 +		}
  1.2281 +		if ($current_y != 0)
  1.2282 +		{
  1.2283 +			$image_data[0] = (int) floor(($current_y / $image_data[1]) * $image_data[0]);
  1.2284 +			$image_data[0] = min($config['avatar_max_width'], $image_data[1]);
  1.2285 +			$image_data[0] = max($config['avatar_min_width'], $image_data[1]);
  1.2286 +		}
  1.2287 +	}
  1.2288 +	return array($image_data[0], $image_data[1]);
  1.2289 +}
  1.2290 +
  1.2291 +/**
  1.2292 +* Uploading/Changing user avatar
  1.2293 +*/
  1.2294 +function avatar_process_user(&$error, $custom_userdata = false, $can_upload = null)
  1.2295 +{
  1.2296 +	global $config, $phpbb_root_path, $auth, $user, $db;
  1.2297 +
  1.2298 +	$data = array(
  1.2299 +		'uploadurl'		=> request_var('uploadurl', ''),
  1.2300 +		'remotelink'	=> request_var('remotelink', ''),
  1.2301 +		'width'			=> request_var('width', 0),
  1.2302 +		'height'		=> request_var('height', 0),
  1.2303 +	);
  1.2304 +
  1.2305 +	$error = validate_data($data, array(
  1.2306 +		'uploadurl'		=> array('string', true, 5, 255),
  1.2307 +		'remotelink'	=> array('string', true, 5, 255),
  1.2308 +		'width'			=> array('string', true, 1, 3),
  1.2309 +		'height'		=> array('string', true, 1, 3),
  1.2310 +	));
  1.2311 +
  1.2312 +	if (sizeof($error))
  1.2313 +	{
  1.2314 +		return false;
  1.2315 +	}
  1.2316 +
  1.2317 +	$sql_ary = array();
  1.2318 +
  1.2319 +	if ($custom_userdata === false)
  1.2320 +	{
  1.2321 +		$userdata = &$user->data;
  1.2322 +	}
  1.2323 +	else
  1.2324 +	{
  1.2325 +		$userdata = &$custom_userdata;
  1.2326 +	}
  1.2327 +
  1.2328 +	$data['user_id'] = $userdata['user_id'];
  1.2329 +	$change_avatar = ($custom_userdata === false) ? $auth->acl_get('u_chgavatar') : true;
  1.2330 +	$avatar_select = basename(request_var('avatar_select', ''));
  1.2331 +
  1.2332 +	// Can we upload?
  1.2333 +	if (is_null($can_upload))
  1.2334 +	{
  1.2335 +		$can_upload = ($config['allow_avatar_upload'] && file_exists($phpbb_root_path . $config['avatar_path']) && phpbb_is_writable($phpbb_root_path . $config['avatar_path']) && $change_avatar && (@ini_get('file_uploads') || strtolower(@ini_get('file_uploads')) == 'on')) ? true : false;
  1.2336 +	}
  1.2337 +
  1.2338 +	if ((!empty($_FILES['uploadfile']['name']) || $data['uploadurl']) && $can_upload)
  1.2339 +	{
  1.2340 +		list($sql_ary['user_avatar_type'], $sql_ary['user_avatar'], $sql_ary['user_avatar_width'], $sql_ary['user_avatar_height']) = avatar_upload($data, $error);
  1.2341 +	}
  1.2342 +	else if ($data['remotelink'] && $change_avatar && $config['allow_avatar_remote'])
  1.2343 +	{
  1.2344 +		list($sql_ary['user_avatar_type'], $sql_ary['user_avatar'], $sql_ary['user_avatar_width'], $sql_ary['user_avatar_height']) = avatar_remote($data, $error);
  1.2345 +	}
  1.2346 +	else if ($avatar_select && $change_avatar && $config['allow_avatar_local'])
  1.2347 +	{
  1.2348 +		$category = basename(request_var('category', ''));
  1.2349 +
  1.2350 +		$sql_ary['user_avatar_type'] = AVATAR_GALLERY;
  1.2351 +		$sql_ary['user_avatar'] = $avatar_select;
  1.2352 +
  1.2353 +		// check avatar gallery
  1.2354 +		if (!is_dir($phpbb_root_path . $config['avatar_gallery_path'] . '/' . $category))
  1.2355 +		{
  1.2356 +			$sql_ary['user_avatar'] = '';
  1.2357 +			$sql_ary['user_avatar_type'] = $sql_ary['user_avatar_width'] = $sql_ary['user_avatar_height'] = 0;
  1.2358 +		}
  1.2359 +		else
  1.2360 +		{
  1.2361 +			list($sql_ary['user_avatar_width'], $sql_ary['user_avatar_height']) = getimagesize($phpbb_root_path . $config['avatar_gallery_path'] . '/' . $category . '/' . urldecode($sql_ary['user_avatar']));
  1.2362 +			$sql_ary['user_avatar'] = $category . '/' . $sql_ary['user_avatar'];
  1.2363 +		}
  1.2364 +	}
  1.2365 +	else if (isset($_POST['delete']) && $change_avatar)
  1.2366 +	{
  1.2367 +		$sql_ary['user_avatar'] = '';
  1.2368 +		$sql_ary['user_avatar_type'] = $sql_ary['user_avatar_width'] = $sql_ary['user_avatar_height'] = 0;
  1.2369 +	}
  1.2370 +	else if (!empty($userdata['user_avatar']))
  1.2371 +	{
  1.2372 +		// Only update the dimensions
  1.2373 +
  1.2374 +		if (empty($data['width']) || empty($data['height']))
  1.2375 +		{
  1.2376 +			if ($dims = avatar_get_dimensions($userdata['user_avatar'], $userdata['user_avatar_type'], $error, $data['width'], $data['height']))
  1.2377 +			{
  1.2378 +				list($guessed_x, $guessed_y) = $dims;
  1.2379 +				if (empty($data['width']))
  1.2380 +				{
  1.2381 +					$data['width'] = $guessed_x;
  1.2382 +				}
  1.2383 +				if (empty($data['height']))
  1.2384 +				{
  1.2385 +					$data['height'] = $guessed_y;
  1.2386 +				}
  1.2387 +			}
  1.2388 +		}
  1.2389 +		if (($config['avatar_max_width'] || $config['avatar_max_height']) &&
  1.2390 +			(($data['width'] != $userdata['user_avatar_width']) || $data['height'] != $userdata['user_avatar_height']))
  1.2391 +		{
  1.2392 +			if ($data['width'] > $config['avatar_max_width'] || $data['height'] > $config['avatar_max_height'])
  1.2393 +			{
  1.2394 +				$error[] = sprintf($user->lang['AVATAR_WRONG_SIZE'], $config['avatar_min_width'], $config['avatar_min_height'], $config['avatar_max_width'], $config['avatar_max_height'], $data['width'], $data['height']);
  1.2395 +			}
  1.2396 +		}
  1.2397 +
  1.2398 +		if (!sizeof($error))
  1.2399 +		{
  1.2400 +			if ($config['avatar_min_width'] || $config['avatar_min_height'])
  1.2401 +			{
  1.2402 +				if ($data['width'] < $config['avatar_min_width'] || $data['height'] < $config['avatar_min_height'])
  1.2403 +				{
  1.2404 +					$error[] = sprintf($user->lang['AVATAR_WRONG_SIZE'], $config['avatar_min_width'], $config['avatar_min_height'], $config['avatar_max_width'], $config['avatar_max_height'], $data['width'], $data['height']);
  1.2405 +				}
  1.2406 +			}
  1.2407 +		}
  1.2408 +
  1.2409 +		if (!sizeof($error))
  1.2410 +		{
  1.2411 +			$sql_ary['user_avatar_width'] = $data['width'];
  1.2412 +			$sql_ary['user_avatar_height'] = $data['height'];
  1.2413 +		}
  1.2414 +	}
  1.2415 +
  1.2416 +	if (!sizeof($error))
  1.2417 +	{
  1.2418 +		// Do we actually have any data to update?
  1.2419 +		if (sizeof($sql_ary))
  1.2420 +		{
  1.2421 +			$ext_new = $ext_old = '';
  1.2422 +			if (isset($sql_ary['user_avatar']))
  1.2423 +			{
  1.2424 +				$userdata = ($custom_userdata === false) ? $user->data : $custom_userdata;
  1.2425 +				$ext_new = (empty($sql_ary['user_avatar'])) ? '' : substr(strrchr($sql_ary['user_avatar'], '.'), 1);
  1.2426 +				$ext_old = (empty($userdata['user_avatar'])) ? '' : substr(strrchr($userdata['user_avatar'], '.'), 1);
  1.2427 +
  1.2428 +				if ($userdata['user_avatar_type'] == AVATAR_UPLOAD)
  1.2429 +				{
  1.2430 +					// Delete old avatar if present
  1.2431 +					if ((!empty($userdata['user_avatar']) && empty($sql_ary['user_avatar']))
  1.2432 +					   || ( !empty($userdata['user_avatar']) && !empty($sql_ary['user_avatar']) && $ext_new !== $ext_old))
  1.2433 +					{
  1.2434 +						avatar_delete('user', $userdata);
  1.2435 +					}
  1.2436 +				}
  1.2437 +			}
  1.2438 +
  1.2439 +			$sql = 'UPDATE ' . USERS_TABLE . '
  1.2440 +				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1.2441 +				WHERE user_id = ' . (($custom_userdata === false) ? $user->data['user_id'] : $custom_userdata['user_id']);
  1.2442 +			$db->sql_query($sql);
  1.2443 +
  1.2444 +		}
  1.2445 +	}
  1.2446 +
  1.2447 +	return (sizeof($error)) ? false : true;
  1.2448 +}
  1.2449 +
  1.2450 +//
  1.2451 +// Usergroup functions
  1.2452 +//
  1.2453 +
  1.2454 +/**
  1.2455 +* Add or edit a group. If we're editing a group we only update user
  1.2456 +* parameters such as rank, etc. if they are changed
  1.2457 +*/
  1.2458 +function group_create(&$group_id, $type, $name, $desc, $group_attributes, $allow_desc_bbcode = false, $allow_desc_urls = false, $allow_desc_smilies = false)
  1.2459 +{
  1.2460 +	global $phpbb_root_path, $config, $db, $user, $file_upload;
  1.2461 +
  1.2462 +	$error = array();
  1.2463 +
  1.2464 +	// Attributes which also affect the users table
  1.2465 +	$user_attribute_ary = array('group_colour', 'group_rank', 'group_avatar', 'group_avatar_type', 'group_avatar_width', 'group_avatar_height');
  1.2466 +
  1.2467 +	// Check data. Limit group name length.
  1.2468 +	if (!utf8_strlen($name) || utf8_strlen($name) > 60)
  1.2469 +	{
  1.2470 +		$error[] = (!utf8_strlen($name)) ? $user->lang['GROUP_ERR_USERNAME'] : $user->lang['GROUP_ERR_USER_LONG'];
  1.2471 +	}
  1.2472 +
  1.2473 +	$err = group_validate_groupname($group_id, $name);
  1.2474 +	if (!empty($err))
  1.2475 +	{
  1.2476 +		$error[] = $user->lang[$err];
  1.2477 +	}
  1.2478 +
  1.2479 +	if (!in_array($type, array(GROUP_OPEN, GROUP_CLOSED, GROUP_HIDDEN, GROUP_SPECIAL, GROUP_FREE)))
  1.2480 +	{
  1.2481 +		$error[] = $user->lang['GROUP_ERR_TYPE'];
  1.2482 +	}
  1.2483 +
  1.2484 +	if (!sizeof($error))
  1.2485 +	{
  1.2486 +		$user_ary = array();
  1.2487 +		$sql_ary = array(
  1.2488 +			'group_name'			=> (string) $name,
  1.2489 +			'group_desc'			=> (string) $desc,
  1.2490 +			'group_desc_uid'		=> '',
  1.2491 +			'group_desc_bitfield'	=> '',
  1.2492 +			'group_type'			=> (int) $type,
  1.2493 +		);
  1.2494 +
  1.2495 +		// Parse description
  1.2496 +		if ($desc)
  1.2497 +		{
  1.2498 +			generate_text_for_storage($sql_ary['group_desc'], $sql_ary['group_desc_uid'], $sql_ary['group_desc_bitfield'], $sql_ary['group_desc_options'], $allow_desc_bbcode, $allow_desc_urls, $allow_desc_smilies);
  1.2499 +		}
  1.2500 +
  1.2501 +		if (sizeof($group_attributes))
  1.2502 +		{
  1.2503 +			// Merge them with $sql_ary to properly update the group
  1.2504 +			$sql_ary = array_merge($sql_ary, $group_attributes);
  1.2505 +		}
  1.2506 +
  1.2507 +		// Setting the log message before we set the group id (if group gets added)
  1.2508 +		$log = ($group_id) ? 'LOG_GROUP_UPDATED' : 'LOG_GROUP_CREATED';
  1.2509 +
  1.2510 +		$query = '';
  1.2511 +
  1.2512 +		if ($group_id)
  1.2513 +		{
  1.2514 +			$sql = 'SELECT user_id
  1.2515 +				FROM ' . USERS_TABLE . '
  1.2516 +				WHERE group_id = ' . $group_id;
  1.2517 +			$result = $db->sql_query($sql);
  1.2518 +
  1.2519 +			while ($row = $db->sql_fetchrow($result))
  1.2520 +			{
  1.2521 +				$user_ary[] = $row['user_id'];
  1.2522 +			}
  1.2523 +			$db->sql_freeresult($result);
  1.2524 +
  1.2525 +			if (isset($sql_ary['group_avatar']) && !$sql_ary['group_avatar'])
  1.2526 +			{
  1.2527 +				remove_default_avatar($group_id, $user_ary);
  1.2528 +			}
  1.2529 +
  1.2530 +			if (isset($sql_ary['group_rank']) && !$sql_ary['group_rank'])
  1.2531 +			{
  1.2532 +				remove_default_rank($group_id, $user_ary);
  1.2533 +			}
  1.2534 +
  1.2535 +			$sql = 'UPDATE ' . GROUPS_TABLE . '
  1.2536 +				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
  1.2537 +				WHERE group_id = $group_id";
  1.2538 +			$db->sql_query($sql);
  1.2539 +
  1.2540 +			// Since we may update the name too, we need to do this on other tables too...
  1.2541 +			$sql = 'UPDATE ' . MODERATOR_CACHE_TABLE . "
  1.2542 +				SET group_name = '" . $db->sql_escape($sql_ary['group_name']) . "'
  1.2543 +				WHERE group_id = $group_id";
  1.2544 +			$db->sql_query($sql);
  1.2545 +
  1.2546 +			// One special case is the group skip auth setting. If this was changed we need to purge permissions for this group
  1.2547 +			if (isset($group_attributes['group_skip_auth']))
  1.2548 +			{
  1.2549 +				// Get users within this group...
  1.2550 +				$sql = 'SELECT user_id
  1.2551 +					FROM ' . USER_GROUP_TABLE . '
  1.2552 +					WHERE group_id = ' . $group_id . '
  1.2553 +						AND user_pending = 0';
  1.2554 +				$result = $db->sql_query($sql);
  1.2555 +
  1.2556 +				$user_id_ary = array();
  1.2557 +				while ($row = $db->sql_fetchrow($result))
  1.2558 +				{
  1.2559 +					$user_id_ary[] = $row['user_id'];
  1.2560 +				}
  1.2561 +				$db->sql_freeresult($result);
  1.2562 +
  1.2563 +				if (!empty($user_id_ary))
  1.2564 +				{
  1.2565 +					global $auth;
  1.2566 +
  1.2567 +					// Clear permissions cache of relevant users
  1.2568 +					$auth->acl_clear_prefetch($user_id_ary);
  1.2569 +				}
  1.2570 +			}
  1.2571 +		}
  1.2572 +		else
  1.2573 +		{
  1.2574 +			$sql = 'INSERT INTO ' . GROUPS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  1.2575 +			$db->sql_query($sql);
  1.2576 +		}
  1.2577 +
  1.2578 +		if (!$group_id)
  1.2579 +		{
  1.2580 +			$group_id = $db->sql_nextid();
  1.2581 +
  1.2582 +			if (isset($sql_ary['group_avatar_type']) && $sql_ary['group_avatar_type'] == AVATAR_UPLOAD)
  1.2583 +			{
  1.2584 +				group_correct_avatar($group_id, $sql_ary['group_avatar']);
  1.2585 +			}
  1.2586 +		}
  1.2587 +
  1.2588 +		// Set user attributes
  1.2589 +		$sql_ary = array();
  1.2590 +		if (sizeof($group_attributes))
  1.2591 +		{
  1.2592 +			// Go through the user attributes array, check if a group attribute matches it and then set it. ;)
  1.2593 +			foreach ($user_attribute_ary as $attribute)
  1.2594 +			{
  1.2595 +				if (!isset($group_attributes[$attribute]))
  1.2596 +				{
  1.2597 +					continue;
  1.2598 +				}
  1.2599 +
  1.2600 +				// If we are about to set an avatar, we will not overwrite user avatars if no group avatar is set...
  1.2601 +				if (strpos($attribute, 'group_avatar') === 0 && !$group_attributes[$attribute])
  1.2602 +				{
  1.2603 +					continue;
  1.2604 +				}
  1.2605 +
  1.2606 +				$sql_ary[$attribute] = $group_attributes[$attribute];
  1.2607 +			}
  1.2608 +		}
  1.2609 +
  1.2610 +		if (sizeof($sql_ary) && sizeof($user_ary))
  1.2611 +		{
  1.2612 +			group_set_user_default($group_id, $user_ary, $sql_ary);
  1.2613 +		}
  1.2614 +
  1.2615 +		$name = ($type == GROUP_SPECIAL) ? $user->lang['G_' . $name] : $name;
  1.2616 +		add_log('admin', $log, $name);
  1.2617 +
  1.2618 +		group_update_listings($group_id);
  1.2619 +	}
  1.2620 +
  1.2621 +	return (sizeof($error)) ? $error : false;
  1.2622 +}
  1.2623 +
  1.2624 +
  1.2625 +/**
  1.2626 +* Changes a group avatar's filename to conform to the naming scheme
  1.2627 +*/
  1.2628 +function group_correct_avatar($group_id, $old_entry)
  1.2629 +{
  1.2630 +	global $config, $db, $phpbb_root_path;
  1.2631 +
  1.2632 +	$group_id		= (int)$group_id;
  1.2633 +	$ext 			= substr(strrchr($old_entry, '.'), 1);
  1.2634 +	$old_filename 	= get_avatar_filename($old_entry);
  1.2635 +	$new_filename 	= $config['avatar_salt'] . "_g$group_id.$ext";
  1.2636 +	$new_entry 		= 'g' . $group_id . '_' . substr(time(), -5) . ".$ext";
  1.2637 +
  1.2638 +	$avatar_path = $phpbb_root_path . $config['avatar_path'];
  1.2639 +	if (@rename($avatar_path . '/'. $old_filename, $avatar_path . '/' . $new_filename))
  1.2640 +	{
  1.2641 +		$sql = 'UPDATE ' . GROUPS_TABLE . '
  1.2642 +			SET group_avatar = \'' . $db->sql_escape($new_entry) . "'
  1.2643 +			WHERE group_id = $group_id";
  1.2644 +		$db->sql_query($sql);
  1.2645 +	}
  1.2646 +}
  1.2647 +
  1.2648 +
  1.2649 +/**
  1.2650 +* Remove avatar also for users not having the group as default
  1.2651 +*/
  1.2652 +function avatar_remove_db($avatar_name)
  1.2653 +{
  1.2654 +	global $config, $db;
  1.2655 +
  1.2656 +	$sql = 'UPDATE ' . USERS_TABLE . "
  1.2657 +		SET user_avatar = '',
  1.2658 +		user_avatar_type = 0
  1.2659 +		WHERE user_avatar = '" . $db->sql_escape($avatar_name) . '\'';
  1.2660 +	$db->sql_query($sql);
  1.2661 +}
  1.2662 +
  1.2663 +
  1.2664 +/**
  1.2665 +* Group Delete
  1.2666 +*/
  1.2667 +function group_delete($group_id, $group_name = false)
  1.2668 +{
  1.2669 +	global $db, $phpbb_root_path, $phpEx;
  1.2670 +
  1.2671 +	if (!$group_name)
  1.2672 +	{
  1.2673 +		$group_name = get_group_name($group_id);
  1.2674 +	}
  1.2675 +
  1.2676 +	$start = 0;
  1.2677 +
  1.2678 +	do
  1.2679 +	{
  1.2680 +		$user_id_ary = $username_ary = array();
  1.2681 +
  1.2682 +		// Batch query for group members, call group_user_del
  1.2683 +		$sql = 'SELECT u.user_id, u.username
  1.2684 +			FROM ' . USER_GROUP_TABLE . ' ug, ' . USERS_TABLE . " u
  1.2685 +			WHERE ug.group_id = $group_id
  1.2686 +				AND u.user_id = ug.user_id";
  1.2687 +		$result = $db->sql_query_limit($sql, 200, $start);
  1.2688 +
  1.2689 +		if ($row = $db->sql_fetchrow($result))
  1.2690 +		{
  1.2691 +			do
  1.2692 +			{
  1.2693 +				$user_id_ary[] = $row['user_id'];
  1.2694 +				$username_ary[] = $row['username'];
  1.2695 +
  1.2696 +				$start++;
  1.2697 +			}
  1.2698 +			while ($row = $db->sql_fetchrow($result));
  1.2699 +
  1.2700 +			group_user_del($group_id, $user_id_ary, $username_ary, $group_name);
  1.2701 +		}
  1.2702 +		else
  1.2703 +		{
  1.2704 +			$start = 0;
  1.2705 +		}
  1.2706 +		$db->sql_freeresult($result);
  1.2707 +	}
  1.2708 +	while ($start);
  1.2709 +
  1.2710 +	// Delete group
  1.2711 +	$sql = 'DELETE FROM ' . GROUPS_TABLE . "
  1.2712 +		WHERE group_id = $group_id";
  1.2713 +	$db->sql_query($sql);
  1.2714 +
  1.2715 +	// Delete auth entries from the groups table
  1.2716 +	$sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . "
  1.2717 +		WHERE group_id = $group_id";
  1.2718 +	$db->sql_query($sql);
  1.2719 +
  1.2720 +	// Re-cache moderators
  1.2721 +	if (!function_exists('cache_moderators'))
  1.2722 +	{
  1.2723 +		include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1.2724 +	}
  1.2725 +
  1.2726 +	cache_moderators();
  1.2727 +
  1.2728 +	add_log('admin', 'LOG_GROUP_DELETE', $group_name);
  1.2729 +
  1.2730 +	// Return false - no error
  1.2731 +	return false;
  1.2732 +}
  1.2733 +
  1.2734 +/**
  1.2735 +* Add user(s) to group
  1.2736 +*
  1.2737 +* @return mixed false if no errors occurred, else the user lang string for the relevant error, for example 'NO_USER'
  1.2738 +*/
  1.2739 +function group_user_add($group_id, $user_id_ary = false, $username_ary = false, $group_name = false, $default = false, $leader = 0, $pending = 0, $group_attributes = false)
  1.2740 +{
  1.2741 +	global $db, $auth;
  1.2742 +
  1.2743 +	// We need both username and user_id info
  1.2744 +	$result = user_get_id_name($user_id_ary, $username_ary);
  1.2745 +
  1.2746 +	if (!sizeof($user_id_ary) || $result !== false)
  1.2747 +	{
  1.2748 +		return 'NO_USER';
  1.2749 +	}
  1.2750 +
  1.2751 +	// Remove users who are already members of this group
  1.2752 +	$sql = 'SELECT user_id, group_leader
  1.2753 +		FROM ' . USER_GROUP_TABLE . '
  1.2754 +		WHERE ' . $db->sql_in_set('user_id', $user_id_ary) . "
  1.2755 +			AND group_id = $group_id";
  1.2756 +	$result = $db->sql_query($sql);
  1.2757 +
  1.2758 +	$add_id_ary = $update_id_ary = array();
  1.2759 +	while ($row = $db->sql_fetchrow($result))
  1.2760 +	{
  1.2761 +		$add_id_ary[] = (int) $row['user_id'];
  1.2762 +
  1.2763 +		if ($leader && !$row['group_leader'])
  1.2764 +		{
  1.2765 +			$update_id_ary[] = (int) $row['user_id'];
  1.2766 +		}
  1.2767 +	}
  1.2768 +	$db->sql_freeresult($result);
  1.2769 +
  1.2770 +	// Do all the users exist in this group?
  1.2771 +	$add_id_ary = array_diff($user_id_ary, $add_id_ary);
  1.2772 +
  1.2773 +	// If we have no users
  1.2774 +	if (!sizeof($add_id_ary) && !sizeof($update_id_ary))
  1.2775 +	{
  1.2776 +		return 'GROUP_USERS_EXIST';
  1.2777 +	}
  1.2778 +
  1.2779 +	$db->sql_transaction('begin');
  1.2780 +
  1.2781 +	// Insert the new users
  1.2782 +	if (sizeof($add_id_ary))
  1.2783 +	{
  1.2784 +		$sql_ary = array();
  1.2785 +
  1.2786 +		foreach ($add_id_ary as $user_id)
  1.2787 +		{
  1.2788 +			$sql_ary[] = array(
  1.2789 +				'user_id'		=> (int) $user_id,
  1.2790 +				'group_id'		=> (int) $group_id,
  1.2791 +				'group_leader'	=> (int) $leader,
  1.2792 +				'user_pending'	=> (int) $pending,
  1.2793 +			);
  1.2794 +		}
  1.2795 +
  1.2796 +		$db->sql_multi_insert(USER_GROUP_TABLE, $sql_ary);
  1.2797 +	}
  1.2798 +
  1.2799 +	if (sizeof($update_id_ary))
  1.2800 +	{
  1.2801 +		$sql = 'UPDATE ' . USER_GROUP_TABLE . '
  1.2802 +			SET group_leader = 1
  1.2803 +			WHERE ' . $db->sql_in_set('user_id', $update_id_ary) . "
  1.2804 +				AND group_id = $group_id";
  1.2805 +		$db->sql_query($sql);
  1.2806 +	}
  1.2807 +
  1.2808 +	if ($default)
  1.2809 +	{
  1.2810 +		group_user_attributes('default', $group_id, $user_id_ary, false, $group_name, $group_attributes);
  1.2811 +	}
  1.2812 +
  1.2813 +	$db->sql_transaction('commit');
  1.2814 +
  1.2815 +	// Clear permissions cache of relevant users
  1.2816 +	$auth->acl_clear_prefetch($user_id_ary);
  1.2817 +
  1.2818 +	if (!$group_name)
  1.2819 +	{
  1.2820 +		$group_name = get_group_name($group_id);
  1.2821 +	}
  1.2822 +
  1.2823 +	$log = ($leader) ? 'LOG_MODS_ADDED' : (($pending) ? 'LOG_USERS_PENDING' : 'LOG_USERS_ADDED');
  1.2824 +
  1.2825 +	add_log('admin', $log, $group_name, implode(', ', $username_ary));
  1.2826 +
  1.2827 +	group_update_listings($group_id);
  1.2828 +
  1.2829 +	// Return false - no error
  1.2830 +	return false;
  1.2831 +}
  1.2832 +
  1.2833 +/**
  1.2834 +* Remove a user/s from a given group. When we remove users we update their
  1.2835 +* default group_id. We do this by examining which "special" groups they belong
  1.2836 +* to. The selection is made based on a reasonable priority system
  1.2837 +*
  1.2838 +* @return false if no errors occurred, else the user lang string for the relevant error, for example 'NO_USER'
  1.2839 +*/
  1.2840 +function group_user_del($group_id, $user_id_ary = false, $username_ary = false, $group_name = false)
  1.2841 +{
  1.2842 +	global $db, $auth, $config;
  1.2843 +
  1.2844 +	if ($config['coppa_enable'])
  1.2845 +	{
  1.2846 +		$group_order = array('ADMINISTRATORS', 'GLOBAL_MODERATORS', 'NEWLY_REGISTERED', 'REGISTERED_COPPA', 'REGISTERED', 'BOTS', 'GUESTS');
  1.2847 +	}
  1.2848 +	else
  1.2849 +	{
  1.2850 +		$group_order = array('ADMINISTRATORS', 'GLOBAL_MODERATORS', 'NEWLY_REGISTERED', 'REGISTERED', 'BOTS', 'GUESTS');
  1.2851 +	}
  1.2852 +
  1.2853 +	// We need both username and user_id info
  1.2854 +	$result = user_get_id_name($user_id_ary, $username_ary);
  1.2855 +
  1.2856 +	if (!sizeof($user_id_ary) || $result !== false)
  1.2857 +	{
  1.2858 +		return 'NO_USER';
  1.2859 +	}
  1.2860 +
  1.2861 +	$sql = 'SELECT *
  1.2862 +		FROM ' . GROUPS_TABLE . '
  1.2863 +		WHERE ' . $db->sql_in_set('group_name', $group_order);
  1.2864 +	$result = $db->sql_query($sql);
  1.2865 +
  1.2866 +	$group_order_id = $special_group_data = array();
  1.2867 +	while ($row = $db->sql_fetchrow($result))
  1.2868 +	{
  1.2869 +		$group_order_id[$row['group_name']] = $row['group_id'];
  1.2870 +
  1.2871 +		$special_group_data[$row['group_id']] = array(
  1.2872 +			'group_colour'			=> $row['group_colour'],
  1.2873 +			'group_rank'				=> $row['group_rank'],
  1.2874 +		);
  1.2875 +
  1.2876 +		// Only set the group avatar if one is defined...
  1.2877 +		if ($row['group_avatar'])
  1.2878 +		{
  1.2879 +			$special_group_data[$row['group_id']] = array_merge($special_group_data[$row['group_id']], array(
  1.2880 +				'group_avatar'			=> $row['group_avatar'],
  1.2881 +				'group_avatar_type'		=> $row['group_avatar_type'],
  1.2882 +				'group_avatar_width'		=> $row['group_avatar_width'],
  1.2883 +				'group_avatar_height'	=> $row['group_avatar_height'])
  1.2884 +			);
  1.2885 +		}
  1.2886 +	}
  1.2887 +	$db->sql_freeresult($result);
  1.2888 +
  1.2889 +	// Get users default groups - we only need to reset default group membership if the group from which the user gets removed is set as default
  1.2890 +	$sql = 'SELECT user_id, group_id
  1.2891 +		FROM ' . USERS_TABLE . '
  1.2892 +		WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
  1.2893 +	$result = $db->sql_query($sql);
  1.2894 +
  1.2895 +	$default_groups = array();
  1.2896 +	while ($row = $db->sql_fetchrow($result))
  1.2897 +	{
  1.2898 +		$default_groups[$row['user_id']] = $row['group_id'];
  1.2899 +	}
  1.2900 +	$db->sql_freeresult($result);
  1.2901 +
  1.2902 +	// What special group memberships exist for these users?
  1.2903 +	$sql = 'SELECT g.group_id, g.group_name, ug.user_id
  1.2904 +		FROM ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g
  1.2905 +		WHERE ' . $db->sql_in_set('ug.user_id', $user_id_ary) . "
  1.2906 +			AND g.group_id = ug.group_id
  1.2907 +			AND g.group_id <> $group_id
  1.2908 +			AND g.group_type = " . GROUP_SPECIAL . '
  1.2909 +		ORDER BY ug.user_id, g.group_id';
  1.2910 +	$result = $db->sql_query($sql);
  1.2911 +
  1.2912 +	$temp_ary = array();
  1.2913 +	while ($row = $db->sql_fetchrow($result))
  1.2914 +	{
  1.2915 +		if ($default_groups[$row['user_id']] == $group_id && (!isset($temp_ary[$row['user_id']]) || $group_order_id[$row['group_name']] < $temp_ary[$row['user_id']]))
  1.2916 +		{
  1.2917 +			$temp_ary[$row['user_id']] = $row['group_id'];
  1.2918 +		}
  1.2919 +	}
  1.2920 +	$db->sql_freeresult($result);
  1.2921 +
  1.2922 +	// sql_where_ary holds the new default groups and their users
  1.2923 +	$sql_where_ary = array();
  1.2924 +	foreach ($temp_ary as $uid => $gid)
  1.2925 +	{
  1.2926 +		$sql_where_ary[$gid][] = $uid;
  1.2927 +	}
  1.2928 +	unset($temp_ary);
  1.2929 +
  1.2930 +	foreach ($special_group_data as $gid => $default_data_ary)
  1.2931 +	{
  1.2932 +		if (isset($sql_where_ary[$gid]) && sizeof($sql_where_ary[$gid]))
  1.2933 +		{
  1.2934 +			remove_default_rank($group_id, $sql_where_ary[$gid]);
  1.2935 +			remove_default_avatar($group_id, $sql_where_ary[$gid]);
  1.2936 +			group_set_user_default($gid, $sql_where_ary[$gid], $default_data_ary);
  1.2937 +		}
  1.2938 +	}
  1.2939 +	unset($special_group_data);
  1.2940 +
  1.2941 +	$sql = 'DELETE FROM ' . USER_GROUP_TABLE . "
  1.2942 +		WHERE group_id = $group_id
  1.2943 +			AND " . $db->sql_in_set('user_id', $user_id_ary);
  1.2944 +	$db->sql_query($sql);
  1.2945 +
  1.2946 +	// Clear permissions cache of relevant users
  1.2947 +	$auth->acl_clear_prefetch($user_id_ary);
  1.2948 +
  1.2949 +	if (!$group_name)
  1.2950 +	{
  1.2951 +		$group_name = get_group_name($group_id);
  1.2952 +	}
  1.2953 +
  1.2954 +	$log = 'LOG_GROUP_REMOVE';
  1.2955 +
  1.2956 +	if ($group_name)
  1.2957 +	{
  1.2958 +		add_log('admin', $log, $group_name, implode(', ', $username_ary));
  1.2959 +	}
  1.2960 +
  1.2961 +	group_update_listings($group_id);
  1.2962 +
  1.2963 +	// Return false - no error
  1.2964 +	return false;
  1.2965 +}
  1.2966 +
  1.2967 +
  1.2968 +/**
  1.2969 +* Removes the group avatar of the default group from the users in user_ids who have that group as default.
  1.2970 +*/
  1.2971 +function remove_default_avatar($group_id, $user_ids)
  1.2972 +{
  1.2973 +	global $db;
  1.2974 +
  1.2975 +	if (!is_array($user_ids))
  1.2976 +	{
  1.2977 +		$user_ids = array($user_ids);
  1.2978 +	}
  1.2979 +	if (empty($user_ids))
  1.2980 +	{
  1.2981 +		return false;
  1.2982 +	}
  1.2983 +
  1.2984 +	$user_ids = array_map('intval', $user_ids);
  1.2985 +
  1.2986 +	$sql = 'SELECT *
  1.2987 +		FROM ' . GROUPS_TABLE . '
  1.2988 +		WHERE group_id = ' . (int)$group_id;
  1.2989 +	$result = $db->sql_query($sql);
  1.2990 +	if (!$row = $db->sql_fetchrow($result))
  1.2991 +	{
  1.2992 +		$db->sql_freeresult($result);
  1.2993 +		return false;
  1.2994 +	}
  1.2995 +	$db->sql_freeresult($result);
  1.2996 +
  1.2997 +	$sql = 'UPDATE ' . USERS_TABLE . "
  1.2998 +		SET user_avatar = '',
  1.2999 +			user_avatar_type = 0,
  1.3000 +			user_avatar_width = 0,
  1.3001 +			user_avatar_height = 0
  1.3002 +		WHERE group_id = " . (int) $group_id . "
  1.3003 +		AND user_avatar = '" . $db->sql_escape($row['group_avatar']) . "'
  1.3004 +		AND " . $db->sql_in_set('user_id', $user_ids);
  1.3005 +
  1.3006 +	$db->sql_query($sql);
  1.3007 +}
  1.3008 +
  1.3009 +/**
  1.3010 +* Removes the group rank of the default group from the users in user_ids who have that group as default.
  1.3011 +*/
  1.3012 +function remove_default_rank($group_id, $user_ids)
  1.3013 +{
  1.3014 +	global $db;
  1.3015 +
  1.3016 +	if (!is_array($user_ids))
  1.3017 +	{
  1.3018 +		$user_ids = array($user_ids);
  1.3019 +	}
  1.3020 +	if (empty($user_ids))
  1.3021 +	{
  1.3022 +		return false;
  1.3023 +	}
  1.3024 +
  1.3025 +	$user_ids = array_map('intval', $user_ids);
  1.3026 +
  1.3027 +	$sql = 'SELECT *
  1.3028 +		FROM ' . GROUPS_TABLE . '
  1.3029 +		WHERE group_id = ' . (int)$group_id;
  1.3030 +	$result = $db->sql_query($sql);
  1.3031 +	if (!$row = $db->sql_fetchrow($result))
  1.3032 +	{
  1.3033 +		$db->sql_freeresult($result);
  1.3034 +		return false;
  1.3035 +	}
  1.3036 +	$db->sql_freeresult($result);
  1.3037 +
  1.3038 +	$sql = 'UPDATE ' . USERS_TABLE . '
  1.3039 +		SET user_rank = 0
  1.3040 +		WHERE group_id = ' . (int)$group_id . '
  1.3041 +		AND user_rank <> 0
  1.3042 +		AND user_rank = ' . (int)$row['group_rank'] . '
  1.3043 +		AND ' . $db->sql_in_set('user_id', $user_ids);
  1.3044 +	$db->sql_query($sql);
  1.3045 +}
  1.3046 +
  1.3047 +/**
  1.3048 +* This is used to promote (to leader), demote or set as default a member/s
  1.3049 +*/
  1.3050 +function group_user_attributes($action, $group_id, $user_id_ary = false, $username_ary = false, $group_name = false, $group_attributes = false)
  1.3051 +{
  1.3052 +	global $db, $auth, $phpbb_root_path, $phpEx, $config;
  1.3053 +
  1.3054 +	// We need both username and user_id info
  1.3055 +	$result = user_get_id_name($user_id_ary, $username_ary);
  1.3056 +
  1.3057 +	if (!sizeof($user_id_ary) || $result !== false)
  1.3058 +	{
  1.3059 +		return 'NO_USERS';
  1.3060 +	}
  1.3061 +
  1.3062 +	if (!$group_name)
  1.3063 +	{
  1.3064 +		$group_name = get_group_name($group_id);
  1.3065 +	}
  1.3066 +
  1.3067 +	switch ($action)
  1.3068 +	{
  1.3069 +		case 'demote':
  1.3070 +		case 'promote':
  1.3071 +
  1.3072 +			$sql = 'SELECT user_id FROM ' . USER_GROUP_TABLE . "
  1.3073 +				WHERE group_id = $group_id
  1.3074 +					AND user_pending = 1
  1.3075 +					AND " . $db->sql_in_set('user_id', $user_id_ary);
  1.3076 +			$result = $db->sql_query_limit($sql, 1);
  1.3077 +			$not_empty = ($db->sql_fetchrow($result));
  1.3078 +			$db->sql_freeresult($result);
  1.3079 +			if ($not_empty)
  1.3080 +			{
  1.3081 +				return 'NO_VALID_USERS';
  1.3082 +			}
  1.3083 +
  1.3084 +			$sql = 'UPDATE ' . USER_GROUP_TABLE . '
  1.3085 +				SET group_leader = ' . (($action == 'promote') ? 1 : 0) . "
  1.3086 +				WHERE group_id = $group_id
  1.3087 +					AND user_pending = 0
  1.3088 +					AND " . $db->sql_in_set('user_id', $user_id_ary);
  1.3089 +			$db->sql_query($sql);
  1.3090 +
  1.3091 +			$log = ($action == 'promote') ? 'LOG_GROUP_PROMOTED' : 'LOG_GROUP_DEMOTED';
  1.3092 +		break;
  1.3093 +
  1.3094 +		case 'approve':
  1.3095 +			// Make sure we only approve those which are pending ;)
  1.3096 +			$sql = 'SELECT u.user_id, u.user_email, u.username, u.username_clean, u.user_notify_type, u.user_jabber, u.user_lang
  1.3097 +				FROM ' . USERS_TABLE . ' u, ' . USER_GROUP_TABLE . ' ug
  1.3098 +				WHERE ug.group_id = ' . $group_id . '
  1.3099 +					AND ug.user_pending = 1
  1.3100 +					AND ug.user_id = u.user_id
  1.3101 +					AND ' . $db->sql_in_set('ug.user_id', $user_id_ary);
  1.3102 +			$result = $db->sql_query($sql);
  1.3103 +
  1.3104 +			$user_id_ary = $email_users = array();
  1.3105 +			while ($row = $db->sql_fetchrow($result))
  1.3106 +			{
  1.3107 +				$user_id_ary[] = $row['user_id'];
  1.3108 +				$email_users[] = $row;
  1.3109 +			}
  1.3110 +			$db->sql_freeresult($result);
  1.3111 +
  1.3112 +			if (!sizeof($user_id_ary))
  1.3113 +			{
  1.3114 +				return false;
  1.3115 +			}
  1.3116 +
  1.3117 +			$sql = 'UPDATE ' . USER_GROUP_TABLE . "
  1.3118 +				SET user_pending = 0
  1.3119 +				WHERE group_id = $group_id
  1.3120 +					AND " . $db->sql_in_set('user_id', $user_id_ary);
  1.3121 +			$db->sql_query($sql);
  1.3122 +
  1.3123 +			// Send approved email to users...
  1.3124 +			include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
  1.3125 +			$messenger = new messenger();
  1.3126 +
  1.3127 +			foreach ($email_users as $row)
  1.3128 +			{
  1.3129 +				$messenger->template('group_approved', $row['user_lang']);
  1.3130 +
  1.3131 +				$messenger->to($row['user_email'], $row['username']);
  1.3132 +				$messenger->im($row['user_jabber'], $row['username']);
  1.3133 +
  1.3134 +				$messenger->assign_vars(array(
  1.3135 +					'USERNAME'		=> htmlspecialchars_decode($row['username']),
  1.3136 +					'GROUP_NAME'	=> htmlspecialchars_decode($group_name),
  1.3137 +					'U_GROUP'		=> generate_board_url() . "/ucp.$phpEx?i=groups&mode=membership")
  1.3138 +				);
  1.3139 +
  1.3140 +				$messenger->send($row['user_notify_type']);
  1.3141 +			}
  1.3142 +
  1.3143 +			$messenger->save_queue();
  1.3144 +
  1.3145 +			$log = 'LOG_USERS_APPROVED';
  1.3146 +		break;
  1.3147 +
  1.3148 +		case 'default':
  1.3149 +			// We only set default group for approved members of the group
  1.3150 +			$sql = 'SELECT user_id
  1.3151 +				FROM ' . USER_GROUP_TABLE . "
  1.3152 +				WHERE group_id = $group_id
  1.3153 +					AND user_pending = 0
  1.3154 +					AND " . $db->sql_in_set('user_id', $user_id_ary);
  1.3155 +			$result = $db->sql_query($sql);
  1.3156 +
  1.3157 +			$user_id_ary = $username_ary = array();
  1.3158 +			while ($row = $db->sql_fetchrow($result))
  1.3159 +			{
  1.3160 +				$user_id_ary[] = $row['user_id'];
  1.3161 +			}
  1.3162 +			$db->sql_freeresult($result);
  1.3163 +
  1.3164 +			$result = user_get_id_name($user_id_ary, $username_ary);
  1.3165 +			if (!sizeof($user_id_ary) || $result !== false)
  1.3166 +			{
  1.3167 +				return 'NO_USERS';
  1.3168 +			}
  1.3169 +
  1.3170 +			$sql = 'SELECT user_id, group_id FROM ' . USERS_TABLE . '
  1.3171 +				WHERE ' . $db->sql_in_set('user_id', $user_id_ary, false, true);
  1.3172 +			$result = $db->sql_query($sql);
  1.3173 +
  1.3174 +			$groups = array();
  1.3175 +			while ($row = $db->sql_fetchrow($result))
  1.3176 +			{
  1.3177 +				if (!isset($groups[$row['group_id']]))
  1.3178 +				{
  1.3179 +					$groups[$row['group_id']] = array();
  1.3180 +				}
  1.3181 +				$groups[$row['group_id']][] = $row['user_id'];
  1.3182 +			}
  1.3183 +			$db->sql_freeresult($result);
  1.3184 +
  1.3185 +			foreach ($groups as $gid => $uids)
  1.3186 +			{
  1.3187 +				remove_default_rank($gid, $uids);
  1.3188 +				remove_default_avatar($gid, $uids);
  1.3189 +			}
  1.3190 +			group_set_user_default($group_id, $user_id_ary, $group_attributes);
  1.3191 +			$log = 'LOG_GROUP_DEFAULTS';
  1.3192 +		break;
  1.3193 +	}
  1.3194 +
  1.3195 +	// Clear permissions cache of relevant users
  1.3196 +	$auth->acl_clear_prefetch($user_id_ary);
  1.3197 +
  1.3198 +	add_log('admin', $log, $group_name, implode(', ', $username_ary));
  1.3199 +
  1.3200 +	group_update_listings($group_id);
  1.3201 +
  1.3202 +	return false;
  1.3203 +}
  1.3204 +
  1.3205 +/**
  1.3206 +* A small version of validate_username to check for a group name's existence. To be called directly.
  1.3207 +*/
  1.3208 +function group_validate_groupname($group_id, $group_name)
  1.3209 +{
  1.3210 +	global $config, $db;
  1.3211 +
  1.3212 +	$group_name =  utf8_clean_string($group_name);
  1.3213 +
  1.3214 +	if (!empty($group_id))
  1.3215 +	{
  1.3216 +		$sql = 'SELECT group_name
  1.3217 +			FROM ' . GROUPS_TABLE . '
  1.3218 +			WHERE group_id = ' . (int) $group_id;
  1.3219 +		$result = $db->sql_query($sql);
  1.3220 +		$row = $db->sql_fetchrow($result);
  1.3221 +		$db->sql_freeresult($result);
  1.3222 +
  1.3223 +		if (!$row)
  1.3224 +		{
  1.3225 +			return false;
  1.3226 +		}
  1.3227 +
  1.3228 +		$allowed_groupname = utf8_clean_string($row['group_name']);
  1.3229 +
  1.3230 +		if ($allowed_groupname == $group_name)
  1.3231 +		{
  1.3232 +			return false;
  1.3233 +		}
  1.3234 +	}
  1.3235 +
  1.3236 +	$sql = 'SELECT group_name
  1.3237 +		FROM ' . GROUPS_TABLE . "
  1.3238 +		WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($group_name)) . "'";
  1.3239 +	$result = $db->sql_query($sql);
  1.3240 +	$row = $db->sql_fetchrow($result);
  1.3241 +	$db->sql_freeresult($result);
  1.3242 +
  1.3243 +	if ($row)
  1.3244 +	{
  1.3245 +		return 'GROUP_NAME_TAKEN';
  1.3246 +	}
  1.3247 +
  1.3248 +	return false;
  1.3249 +}
  1.3250 +
  1.3251 +/**
  1.3252 +* Set users default group
  1.3253 +*
  1.3254 +* @access private
  1.3255 +*/
  1.3256 +function group_set_user_default($group_id, $user_id_ary, $group_attributes = false, $update_listing = false)
  1.3257 +{
  1.3258 +	global $cache, $db;
  1.3259 +
  1.3260 +	if (empty($user_id_ary))
  1.3261 +	{
  1.3262 +		return;
  1.3263 +	}
  1.3264 +
  1.3265 +	$attribute_ary = array(
  1.3266 +		'group_colour'			=> 'string',
  1.3267 +		'group_rank'			=> 'int',
  1.3268 +		'group_avatar'			=> 'string',
  1.3269 +		'group_avatar_type'		=> 'int',
  1.3270 +		'group_avatar_width'	=> 'int',
  1.3271 +		'group_avatar_height'	=> 'int',
  1.3272 +	);
  1.3273 +
  1.3274 +	$sql_ary = array(
  1.3275 +		'group_id'		=> $group_id
  1.3276 +	);
  1.3277 +
  1.3278 +	// Were group attributes passed to the function? If not we need to obtain them
  1.3279 +	if ($group_attributes === false)
  1.3280 +	{
  1.3281 +		$sql = 'SELECT ' . implode(', ', array_keys($attribute_ary)) . '
  1.3282 +			FROM ' . GROUPS_TABLE . "
  1.3283 +			WHERE group_id = $group_id";
  1.3284 +		$result = $db->sql_query($sql);
  1.3285 +		$group_attributes = $db->sql_fetchrow($result);
  1.3286 +		$db->sql_freeresult($result);
  1.3287 +	}
  1.3288 +
  1.3289 +	foreach ($attribute_ary as $attribute => $type)
  1.3290 +	{
  1.3291 +		if (isset($group_attributes[$attribute]))
  1.3292 +		{
  1.3293 +			// If we are about to set an avatar or rank, we will not overwrite with empty, unless we are not actually changing the default group
  1.3294 +			if ((strpos($attribute, 'group_avatar') === 0 || strpos($attribute, 'group_rank') === 0) && !$group_attributes[$attribute])
  1.3295 +			{
  1.3296 +				continue;
  1.3297 +			}
  1.3298 +
  1.3299 +			settype($group_attributes[$attribute], $type);
  1.3300 +			$sql_ary[str_replace('group_', 'user_', $attribute)] = $group_attributes[$attribute];
  1.3301 +		}
  1.3302 +	}
  1.3303 +
  1.3304 +	// Before we update the user attributes, we will make a list of those having now the group avatar assigned
  1.3305 +	if (isset($sql_ary['user_avatar']))
  1.3306 +	{
  1.3307 +		// Ok, get the original avatar data from users having an uploaded one (we need to remove these from the filesystem)
  1.3308 +		$sql = 'SELECT user_id, group_id, user_avatar
  1.3309 +			FROM ' . USERS_TABLE . '
  1.3310 +			WHERE ' . $db->sql_in_set('user_id', $user_id_ary) . '
  1.3311 +				AND user_avatar_type = ' . AVATAR_UPLOAD;
  1.3312 +		$result = $db->sql_query($sql);
  1.3313 +
  1.3314 +		while ($row = $db->sql_fetchrow($result))
  1.3315 +		{
  1.3316 +			avatar_delete('user', $row);
  1.3317 +		}
  1.3318 +		$db->sql_freeresult($result);
  1.3319 +	}
  1.3320 +	else
  1.3321 +	{
  1.3322 +		unset($sql_ary['user_avatar_type']);
  1.3323 +		unset($sql_ary['user_avatar_height']);
  1.3324 +		unset($sql_ary['user_avatar_width']);
  1.3325 +	}
  1.3326 +
  1.3327 +	$sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1.3328 +		WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
  1.3329 +	$db->sql_query($sql);
  1.3330 +
  1.3331 +	if (isset($sql_ary['user_colour']))
  1.3332 +	{
  1.3333 +		// Update any cached colour information for these users
  1.3334 +		$sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
  1.3335 +			WHERE " . $db->sql_in_set('forum_last_poster_id', $user_id_ary);
  1.3336 +		$db->sql_query($sql);
  1.3337 +
  1.3338 +		$sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_first_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
  1.3339 +			WHERE " . $db->sql_in_set('topic_poster', $user_id_ary);
  1.3340 +		$db->sql_query($sql);
  1.3341 +
  1.3342 +		$sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
  1.3343 +			WHERE " . $db->sql_in_set('topic_last_poster_id', $user_id_ary);
  1.3344 +		$db->sql_query($sql);
  1.3345 +
  1.3346 +		global $config;
  1.3347 +
  1.3348 +		if (in_array($config['newest_user_id'], $user_id_ary))
  1.3349 +		{
  1.3350 +			set_config('newest_user_colour', $sql_ary['user_colour'], true);
  1.3351 +		}
  1.3352 +	}
  1.3353 +
  1.3354 +	if ($update_listing)
  1.3355 +	{
  1.3356 +		group_update_listings($group_id);
  1.3357 +	}
  1.3358 +
  1.3359 +	// Because some tables/caches use usercolour-specific data we need to purge this here.
  1.3360 +	$cache->destroy('sql', MODERATOR_CACHE_TABLE);
  1.3361 +}
  1.3362 +
  1.3363 +/**
  1.3364 +* Get group name
  1.3365 +*/
  1.3366 +function get_group_name($group_id)
  1.3367 +{
  1.3368 +	global $db, $user;
  1.3369 +
  1.3370 +	$sql = 'SELECT group_name, group_type
  1.3371 +		FROM ' . GROUPS_TABLE . '
  1.3372 +		WHERE group_id = ' . (int) $group_id;
  1.3373 +	$result = $db->sql_query($sql);
  1.3374 +	$row = $db->sql_fetchrow($result);
  1.3375 +	$db->sql_freeresult($result);
  1.3376 +
  1.3377 +	if (!$row || ($row['group_type'] == GROUP_SPECIAL && empty($user->lang)))
  1.3378 +	{
  1.3379 +		return '';
  1.3380 +	}
  1.3381 +
  1.3382 +	return ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
  1.3383 +}
  1.3384 +
  1.3385 +/**
  1.3386 +* Obtain either the members of a specified group, the groups the specified user is subscribed to
  1.3387 +* or checking if a specified user is in a specified group. This function does not return pending memberships.
  1.3388 +*
  1.3389 +* Note: Never use this more than once... first group your users/groups
  1.3390 +*/
  1.3391 +function group_memberships($group_id_ary = false, $user_id_ary = false, $return_bool = false)
  1.3392 +{
  1.3393 +	global $db;
  1.3394 +
  1.3395 +	if (!$group_id_ary && !$user_id_ary)
  1.3396 +	{
  1.3397 +		return true;
  1.3398 +	}
  1.3399 +
  1.3400 +	if ($user_id_ary)
  1.3401 +	{
  1.3402 +		$user_id_ary = (!is_array($user_id_ary)) ? array($user_id_ary) : $user_id_ary;
  1.3403 +	}
  1.3404 +
  1.3405 +	if ($group_id_ary)
  1.3406 +	{
  1.3407 +		$group_id_ary = (!is_array($group_id_ary)) ? array($group_id_ary) : $group_id_ary;
  1.3408 +	}
  1.3409 +
  1.3410 +	$sql = 'SELECT ug.*, u.username, u.username_clean, u.user_email
  1.3411 +		FROM ' . USER_GROUP_TABLE . ' ug, ' . USERS_TABLE . ' u
  1.3412 +		WHERE ug.user_id = u.user_id
  1.3413 +			AND ug.user_pending = 0 AND ';
  1.3414 +
  1.3415 +	if ($group_id_ary)
  1.3416 +	{
  1.3417 +		$sql .= ' ' . $db->sql_in_set('ug.group_id', $group_id_ary);
  1.3418 +	}
  1.3419 +
  1.3420 +	if ($user_id_ary)
  1.3421 +	{
  1.3422 +		$sql .= ($group_id_ary) ? ' AND ' : ' ';
  1.3423 +		$sql .= $db->sql_in_set('ug.user_id', $user_id_ary);
  1.3424 +	}
  1.3425 +
  1.3426 +	$result = ($return_bool) ? $db->sql_query_limit($sql, 1) : $db->sql_query($sql);
  1.3427 +
  1.3428 +	$row = $db->sql_fetchrow($result);
  1.3429 +
  1.3430 +	if ($return_bool)
  1.3431 +	{
  1.3432 +		$db->sql_freeresult($result);
  1.3433 +		return ($row) ? true : false;
  1.3434 +	}
  1.3435 +
  1.3436 +	if (!$row)
  1.3437 +	{
  1.3438 +		return false;
  1.3439 +	}
  1.3440 +
  1.3441 +	$return = array();
  1.3442 +
  1.3443 +	do
  1.3444 +	{
  1.3445 +		$return[] = $row;
  1.3446 +	}
  1.3447 +	while ($row = $db->sql_fetchrow($result));
  1.3448 +
  1.3449 +	$db->sql_freeresult($result);
  1.3450 +
  1.3451 +	return $return;
  1.3452 +}
  1.3453 +
  1.3454 +/**
  1.3455 +* Re-cache moderators and foes if group has a_ or m_ permissions
  1.3456 +*/
  1.3457 +function group_update_listings($group_id)
  1.3458 +{
  1.3459 +	global $auth;
  1.3460 +
  1.3461 +	$hold_ary = $auth->acl_group_raw_data($group_id, array('a_', 'm_'));
  1.3462 +
  1.3463 +	if (!sizeof($hold_ary))
  1.3464 +	{
  1.3465 +		return;
  1.3466 +	}
  1.3467 +
  1.3468 +	$mod_permissions = $admin_permissions = false;
  1.3469 +
  1.3470 +	foreach ($hold_ary as $g_id => $forum_ary)
  1.3471 +	{
  1.3472 +		foreach ($forum_ary as $forum_id => $auth_ary)
  1.3473 +		{
  1.3474 +			foreach ($auth_ary as $auth_option => $setting)
  1.3475 +			{
  1.3476 +				if ($mod_permissions && $admin_permissions)
  1.3477 +				{
  1.3478 +					break 3;
  1.3479 +				}
  1.3480 +
  1.3481 +				if ($setting != ACL_YES)
  1.3482 +				{
  1.3483 +					continue;
  1.3484 +				}
  1.3485 +
  1.3486 +				if ($auth_option == 'm_')
  1.3487 +				{
  1.3488 +					$mod_permissions = true;
  1.3489 +				}
  1.3490 +
  1.3491 +				if ($auth_option == 'a_')
  1.3492 +				{
  1.3493 +					$admin_permissions = true;
  1.3494 +				}
  1.3495 +			}
  1.3496 +		}
  1.3497 +	}
  1.3498 +
  1.3499 +	if ($mod_permissions)
  1.3500 +	{
  1.3501 +		if (!function_exists('cache_moderators'))
  1.3502 +		{
  1.3503 +			global $phpbb_root_path, $phpEx;
  1.3504 +			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1.3505 +		}
  1.3506 +		cache_moderators();
  1.3507 +	}
  1.3508 +
  1.3509 +	if ($mod_permissions || $admin_permissions)
  1.3510 +	{
  1.3511 +		if (!function_exists('update_foes'))
  1.3512 +		{
  1.3513 +			global $phpbb_root_path, $phpEx;
  1.3514 +			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1.3515 +		}
  1.3516 +		update_foes(array($group_id));
  1.3517 +	}
  1.3518 +}
  1.3519 +
  1.3520 +
  1.3521 +
  1.3522 +/**
  1.3523 +* Funtion to make a user leave the NEWLY_REGISTERED system group.
  1.3524 +* @access public
  1.3525 +* @param $user_id The id of the user to remove from the group
  1.3526 +*/
  1.3527 +function remove_newly_registered($user_id, $user_data = false)
  1.3528 +{
  1.3529 +	global $db;
  1.3530 +
  1.3531 +	if ($user_data === false)
  1.3532 +	{
  1.3533 +		$sql = 'SELECT *
  1.3534 +			FROM ' . USERS_TABLE . '
  1.3535 +			WHERE user_id = ' . $user_id;
  1.3536 +		$result = $db->sql_query($sql);
  1.3537 +		$user_row = $db->sql_fetchrow($result);
  1.3538 +		$db->sql_freeresult($result);
  1.3539 +
  1.3540 +		if (!$user_row)
  1.3541 +		{
  1.3542 +			return false;
  1.3543 +		}
  1.3544 +		else
  1.3545 +		{
  1.3546 +			$user_data  = $user_row;
  1.3547 +		}
  1.3548 +	}
  1.3549 +
  1.3550 +	if (empty($user_data['user_new']))
  1.3551 +	{
  1.3552 +		return false;
  1.3553 +	}
  1.3554 +
  1.3555 +	$sql = 'SELECT group_id
  1.3556 +		FROM ' . GROUPS_TABLE . "
  1.3557 +		WHERE group_name = 'NEWLY_REGISTERED'
  1.3558 +			AND group_type = " . GROUP_SPECIAL;
  1.3559 +	$result = $db->sql_query($sql);
  1.3560 +	$group_id = (int) $db->sql_fetchfield('group_id');
  1.3561 +	$db->sql_freeresult($result);
  1.3562 +
  1.3563 +	if (!$group_id)
  1.3564 +	{
  1.3565 +		return false;
  1.3566 +	}
  1.3567 +
  1.3568 +	// We need to call group_user_del here, because this function makes sure everything is correctly changed.
  1.3569 +	// A downside for a call within the session handler is that the language is not set up yet - so no log entry
  1.3570 +	group_user_del($group_id, $user_id);
  1.3571 +
  1.3572 +	// Set user_new to 0 to let this not be triggered again
  1.3573 +	$sql = 'UPDATE ' . USERS_TABLE . '
  1.3574 +		SET user_new = 0
  1.3575 +		WHERE user_id = ' . $user_id;
  1.3576 +	$db->sql_query($sql);
  1.3577 +
  1.3578 +	// The new users group was the users default group?
  1.3579 +	if ($user_data['group_id'] == $group_id)
  1.3580 +	{
  1.3581 +		// Which group is now the users default one?
  1.3582 +		$sql = 'SELECT group_id
  1.3583 +			FROM ' . USERS_TABLE . '
  1.3584 +			WHERE user_id = ' . $user_id;
  1.3585 +		$result = $db->sql_query($sql);
  1.3586 +		$user_data['group_id'] = $db->sql_fetchfield('group_id');
  1.3587 +		$db->sql_freeresult($result);
  1.3588 +	}
  1.3589 +
  1.3590 +	return $user_data['group_id'];
  1.3591 +}
  1.3592 +
  1.3593 +?>
  1.3594 \ No newline at end of file