Blame Identity/Webenv/App/phpBB/3.0.4/includes/functions_user.php

f2e824
f2e824
/**
f2e824
*
f2e824
* @package phpBB3
f2e824
* @version $Id: functions_user.php 8949 2008-09-26 21:29:05Z toonarmy $
f2e824
* @copyright (c) 2005 phpBB Group
f2e824
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
f2e824
*
f2e824
*/
f2e824
f2e824
/**
f2e824
* @ignore
f2e824
*/
f2e824
if (!defined('IN_PHPBB'))
f2e824
{
f2e824
	exit;
f2e824
}
f2e824
f2e824
/**
f2e824
* Obtain user_ids from usernames or vice versa. Returns false on
f2e824
* success else the error string
f2e824
*
f2e824
* @param array &$user_id_ary The user ids to check or empty if usernames used
f2e824
* @param array &$username_ary The usernames to check or empty if user ids used
f2e824
* @param mixed $user_type Array of user types to check, false if not restricting by user type
f2e824
*/
f2e824
function user_get_id_name(&$user_id_ary, &$username_ary, $user_type = false)
f2e824
{
f2e824
	global $db;
f2e824
f2e824
	// Are both arrays already filled? Yep, return else
f2e824
	// are neither array filled?
f2e824
	if ($user_id_ary && $username_ary)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
	else if (!$user_id_ary && !$username_ary)
f2e824
	{
f2e824
		return 'NO_USERS';
f2e824
	}
f2e824
f2e824
	$which_ary = ($user_id_ary) ? 'user_id_ary' : 'username_ary';
f2e824
f2e824
	if ($$which_ary && !is_array($$which_ary))
f2e824
	{
f2e824
		$$which_ary = array($$which_ary);
f2e824
	}
f2e824
f2e824
	$sql_in = ($which_ary == 'user_id_ary') ? array_map('intval', $$which_ary) : array_map('utf8_clean_string', $$which_ary);
f2e824
	unset($$which_ary);
f2e824
f2e824
	$user_id_ary = $username_ary = array();
f2e824
f2e824
	// Grab the user id/username records
f2e824
	$sql_where = ($which_ary == 'user_id_ary') ? 'user_id' : 'username_clean';
f2e824
	$sql = 'SELECT user_id, username
f2e824
		FROM ' . USERS_TABLE . '
f2e824
		WHERE ' . $db->sql_in_set($sql_where, $sql_in);
f2e824
f2e824
	if ($user_type !== false && !empty($user_type))
f2e824
	{
f2e824
		$sql .= ' AND ' . $db->sql_in_set('user_type', $user_type);
f2e824
	}
f2e824
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	if (!($row = $db->sql_fetchrow($result)))
f2e824
	{
f2e824
		$db->sql_freeresult($result);
f2e824
		return 'NO_USERS';
f2e824
	}
f2e824
f2e824
	do
f2e824
	{
f2e824
		$username_ary[$row['user_id']] = $row['username'];
f2e824
		$user_id_ary[] = $row['user_id'];
f2e824
	}
f2e824
	while ($row = $db->sql_fetchrow($result));
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Get latest registered username and update database to reflect it
f2e824
*/
f2e824
function update_last_username()
f2e824
{
f2e824
	global $db;
f2e824
f2e824
	// Get latest username
f2e824
	$sql = 'SELECT user_id, username, user_colour
f2e824
		FROM ' . USERS_TABLE . '
f2e824
		WHERE user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
f2e824
		ORDER BY user_id DESC';
f2e824
	$result = $db->sql_query_limit($sql, 1);
f2e824
	$row = $db->sql_fetchrow($result);
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if ($row)
f2e824
	{
f2e824
		set_config('newest_user_id', $row['user_id'], true);
f2e824
		set_config('newest_username', $row['username'], true);
f2e824
		set_config('newest_user_colour', $row['user_colour'], true);
f2e824
	}
f2e824
}
f2e824
f2e824
/**
f2e824
* Updates a username across all relevant tables/fields
f2e824
*
f2e824
* @param string $old_name the old/current username
f2e824
* @param string $new_name the new username
f2e824
*/
f2e824
function user_update_name($old_name, $new_name)
f2e824
{
f2e824
	global $config, $db, $cache;
f2e824
f2e824
	$update_ary = array(
f2e824
		FORUMS_TABLE			=> array('forum_last_poster_name'),
f2e824
		MODERATOR_CACHE_TABLE	=> array('username'),
f2e824
		POSTS_TABLE				=> array('post_username'),
f2e824
		TOPICS_TABLE			=> array('topic_first_poster_name', 'topic_last_poster_name'),
f2e824
	);
f2e824
f2e824
	foreach ($update_ary as $table => $field_ary)
f2e824
	{
f2e824
		foreach ($field_ary as $field)
f2e824
		{
f2e824
			$sql = "UPDATE $table
f2e824
				SET $field = '" . $db->sql_escape($new_name) . "'
f2e824
				WHERE $field = '" . $db->sql_escape($old_name) . "'";
f2e824
			$db->sql_query($sql);
f2e824
		}
f2e824
	}
f2e824
f2e824
	if ($config['newest_username'] == $old_name)
f2e824
	{
f2e824
		set_config('newest_username', $new_name, true);
f2e824
	}
f2e824
f2e824
	// Because some tables/caches use username-specific data we need to purge this here.
f2e824
	$cache->destroy('sql', MODERATOR_CACHE_TABLE);
f2e824
}
f2e824
f2e824
/**
f2e824
* Adds an user
f2e824
*
f2e824
* @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.
f2e824
* @param string $cp_data custom profile fields, see custom_profile::build_insert_sql_array
f2e824
* @return the new user's ID.
f2e824
*/
f2e824
function user_add($user_row, $cp_data = false)
f2e824
{
f2e824
	global $db, $user, $auth, $config, $phpbb_root_path, $phpEx;
f2e824
f2e824
	if (empty($user_row['username']) || !isset($user_row['group_id']) || !isset($user_row['user_email']) || !isset($user_row['user_type']))
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$username_clean = utf8_clean_string($user_row['username']);
f2e824
f2e824
	if (empty($username_clean))
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$sql_ary = array(
f2e824
		'username'			=> $user_row['username'],
f2e824
		'username_clean'	=> $username_clean,
f2e824
		'user_password'		=> (isset($user_row['user_password'])) ? $user_row['user_password'] : '',
f2e824
		'user_pass_convert'	=> 0,
f2e824
		'user_email'		=> strtolower($user_row['user_email']),
f2e824
		'user_email_hash'	=> crc32(strtolower($user_row['user_email'])) . strlen($user_row['user_email']),
f2e824
		'group_id'			=> $user_row['group_id'],
f2e824
		'user_type'			=> $user_row['user_type'],
f2e824
	);
f2e824
f2e824
	// These are the additional vars able to be specified
f2e824
	$additional_vars = array(
f2e824
		'user_permissions'	=> '',
f2e824
		'user_timezone'		=> $config['board_timezone'],
f2e824
		'user_dateformat'	=> $config['default_dateformat'],
f2e824
		'user_lang'			=> $config['default_lang'],
f2e824
		'user_style'		=> (int) $config['default_style'],
f2e824
		'user_actkey'		=> '',
f2e824
		'user_ip'			=> '',
f2e824
		'user_regdate'		=> time(),
f2e824
		'user_passchg'		=> time(),
f2e824
		'user_options'		=> 895,
f2e824
f2e824
		'user_inactive_reason'	=> 0,
f2e824
		'user_inactive_time'	=> 0,
f2e824
		'user_lastmark'			=> time(),
f2e824
		'user_lastvisit'		=> 0,
f2e824
		'user_lastpost_time'	=> 0,
f2e824
		'user_lastpage'			=> '',
f2e824
		'user_posts'			=> 0,
f2e824
		'user_dst'				=> (int) $config['board_dst'],
f2e824
		'user_colour'			=> '',
f2e824
		'user_occ'				=> '',
f2e824
		'user_interests'		=> '',
f2e824
		'user_avatar'			=> '',
f2e824
		'user_avatar_type'		=> 0,
f2e824
		'user_avatar_width'		=> 0,
f2e824
		'user_avatar_height'	=> 0,
f2e824
		'user_new_privmsg'		=> 0,
f2e824
		'user_unread_privmsg'	=> 0,
f2e824
		'user_last_privmsg'		=> 0,
f2e824
		'user_message_rules'	=> 0,
f2e824
		'user_full_folder'		=> PRIVMSGS_NO_BOX,
f2e824
		'user_emailtime'		=> 0,
f2e824
f2e824
		'user_notify'			=> 0,
f2e824
		'user_notify_pm'		=> 1,
f2e824
		'user_notify_type'		=> NOTIFY_EMAIL,
f2e824
		'user_allow_pm'			=> 1,
f2e824
		'user_allow_viewonline'	=> 1,
f2e824
		'user_allow_viewemail'	=> 1,
f2e824
		'user_allow_massemail'	=> 1,
f2e824
f2e824
		'user_sig'					=> '',
f2e824
		'user_sig_bbcode_uid'		=> '',
f2e824
		'user_sig_bbcode_bitfield'	=> '',
f2e824
f2e824
		'user_form_salt'			=> unique_id(),
f2e824
	);
f2e824
f2e824
	// Now fill the sql array with not required variables
f2e824
	foreach ($additional_vars as $key => $default_value)
f2e824
	{
f2e824
		$sql_ary[$key] = (isset($user_row[$key])) ? $user_row[$key] : $default_value;
f2e824
	}
f2e824
f2e824
	// Any additional variables in $user_row not covered above?
f2e824
	$remaining_vars = array_diff(array_keys($user_row), array_keys($sql_ary));
f2e824
f2e824
	// Now fill our sql array with the remaining vars
f2e824
	if (sizeof($remaining_vars))
f2e824
	{
f2e824
		foreach ($remaining_vars as $key)
f2e824
		{
f2e824
			$sql_ary[$key] = $user_row[$key];
f2e824
		}
f2e824
	}
f2e824
f2e824
	$sql = 'INSERT INTO ' . USERS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	$user_id = $db->sql_nextid();
f2e824
f2e824
	// Insert Custom Profile Fields
f2e824
	if ($cp_data !== false && sizeof($cp_data))
f2e824
	{
f2e824
		$cp_data['user_id'] = (int) $user_id;
f2e824
f2e824
		if (!class_exists('custom_profile'))
f2e824
		{
f2e824
			include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
f2e824
		}
f2e824
f2e824
		$sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' .
f2e824
			$db->sql_build_array('INSERT', custom_profile::build_insert_sql_array($cp_data));
f2e824
		$db->sql_query($sql);
f2e824
	}
f2e824
f2e824
	// Place into appropriate group...
f2e824
	$sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' ' . $db->sql_build_array('INSERT', array(
f2e824
		'user_id'		=> (int) $user_id,
f2e824
		'group_id'		=> (int) $user_row['group_id'],
f2e824
		'user_pending'	=> 0)
f2e824
	);
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	// Now make it the users default group...
f2e824
	group_set_user_default($user_row['group_id'], array($user_id), false);
f2e824
f2e824
	// set the newest user and adjust the user count if the user is a normal user and no activation mail is sent
f2e824
	if ($user_row['user_type'] == USER_NORMAL)
f2e824
	{
f2e824
		set_config('newest_user_id', $user_id, true);
f2e824
		set_config('newest_username', $user_row['username'], true);
f2e824
		set_config('num_users', $config['num_users'] + 1, true);
f2e824
f2e824
		$sql = 'SELECT group_colour
f2e824
			FROM ' . GROUPS_TABLE . '
f2e824
			WHERE group_id = ' . (int) $user_row['group_id'];
f2e824
		$result = $db->sql_query_limit($sql, 1);
f2e824
		$row = $db->sql_fetchrow($result);
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		set_config('newest_user_colour', $row['group_colour'], true);
f2e824
	}
f2e824
f2e824
	return $user_id;
f2e824
}
f2e824
f2e824
/**
f2e824
* Remove User
f2e824
*/
f2e824
function user_delete($mode, $user_id, $post_username = false)
f2e824
{
f2e824
	global $cache, $config, $db, $user, $auth;
f2e824
	global $phpbb_root_path, $phpEx;
f2e824
f2e824
	$sql = 'SELECT *
f2e824
		FROM ' . USERS_TABLE . '
f2e824
		WHERE user_id = ' . $user_id;
f2e824
	$result = $db->sql_query($sql);
f2e824
	$user_row = $db->sql_fetchrow($result);
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if (!$user_row)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Before we begin, we will remove the reports the user issued.
f2e824
	$sql = 'SELECT r.post_id, p.topic_id
f2e824
		FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . ' p
f2e824
		WHERE r.user_id = ' . $user_id . '
f2e824
			AND p.post_id = r.post_id';
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	$report_posts = $report_topics = array();
f2e824
	while ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$report_posts[] = $row['post_id'];
f2e824
		$report_topics[] = $row['topic_id'];
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if (sizeof($report_posts))
f2e824
	{
f2e824
		$report_posts = array_unique($report_posts);
f2e824
		$report_topics = array_unique($report_topics);
f2e824
f2e824
		// Get a list of topics that still contain reported posts
f2e824
		$sql = 'SELECT DISTINCT topic_id
f2e824
			FROM ' . POSTS_TABLE . '
f2e824
			WHERE ' . $db->sql_in_set('topic_id', $report_topics) . '
f2e824
				AND post_reported = 1
f2e824
				AND ' . $db->sql_in_set('post_id', $report_posts, true);
f2e824
		$result = $db->sql_query($sql);
f2e824
f2e824
		$keep_report_topics = array();
f2e824
		while ($row = $db->sql_fetchrow($result))
f2e824
		{
f2e824
			$keep_report_topics[] = $row['topic_id'];
f2e824
		}
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		if (sizeof($keep_report_topics))
f2e824
		{
f2e824
			$report_topics = array_diff($report_topics, $keep_report_topics);
f2e824
		}
f2e824
		unset($keep_report_topics);
f2e824
f2e824
		// Now set the flags back
f2e824
		$sql = 'UPDATE ' . POSTS_TABLE . '
f2e824
			SET post_reported = 0
f2e824
			WHERE ' . $db->sql_in_set('post_id', $report_posts);
f2e824
		$db->sql_query($sql);
f2e824
f2e824
		if (sizeof($report_topics))
f2e824
		{
f2e824
			$sql = 'UPDATE ' . TOPICS_TABLE . '
f2e824
				SET topic_reported = 0
f2e824
				WHERE ' . $db->sql_in_set('topic_id', $report_topics);
f2e824
			$db->sql_query($sql);
f2e824
		}
f2e824
	}
f2e824
f2e824
	// Remove reports
f2e824
	$db->sql_query('DELETE FROM ' . REPORTS_TABLE . ' WHERE user_id = ' . $user_id);
f2e824
f2e824
	if ($user_row['user_avatar'] && $user_row['user_avatar_type'] == AVATAR_UPLOAD)
f2e824
	{
f2e824
		avatar_delete('user', $user_row);
f2e824
	}
f2e824
f2e824
	switch ($mode)
f2e824
	{
f2e824
		case 'retain':
f2e824
f2e824
			$db->sql_transaction('begin');
f2e824
f2e824
			if ($post_username === false)
f2e824
			{
f2e824
				$post_username = $user->lang['GUEST'];
f2e824
			}
f2e824
f2e824
			// If the user is inactive and newly registered we assume no posts from this user being there...
f2e824
			if ($user_row['user_type'] == USER_INACTIVE && $user_row['user_inactive_reason'] == INACTIVE_REGISTER && !$user_row['user_posts'])
f2e824
			{
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$sql = 'UPDATE ' . FORUMS_TABLE . '
f2e824
					SET forum_last_poster_id = ' . ANONYMOUS . ", forum_last_poster_name = '" . $db->sql_escape($post_username) . "', forum_last_poster_colour = ''
f2e824
					WHERE forum_last_poster_id = $user_id";
f2e824
				$db->sql_query($sql);
f2e824
f2e824
				$sql = 'UPDATE ' . POSTS_TABLE . '
f2e824
					SET poster_id = ' . ANONYMOUS . ", post_username = '" . $db->sql_escape($post_username) . "'
f2e824
					WHERE poster_id = $user_id";
f2e824
				$db->sql_query($sql);
f2e824
f2e824
				$sql = 'UPDATE ' . POSTS_TABLE . '
f2e824
					SET post_edit_user = ' . ANONYMOUS . "
f2e824
					WHERE post_edit_user = $user_id";
f2e824
				$db->sql_query($sql);
f2e824
f2e824
				$sql = 'UPDATE ' . TOPICS_TABLE . '
f2e824
					SET topic_poster = ' . ANONYMOUS . ", topic_first_poster_name = '" . $db->sql_escape($post_username) . "', topic_first_poster_colour = ''
f2e824
					WHERE topic_poster = $user_id";
f2e824
				$db->sql_query($sql);
f2e824
f2e824
				$sql = 'UPDATE ' . TOPICS_TABLE . '
f2e824
					SET topic_last_poster_id = ' . ANONYMOUS . ", topic_last_poster_name = '" . $db->sql_escape($post_username) . "', topic_last_poster_colour = ''
f2e824
					WHERE topic_last_poster_id = $user_id";
f2e824
				$db->sql_query($sql);
f2e824
f2e824
				// Since we change every post by this author, we need to count this amount towards the anonymous user
f2e824
f2e824
				// Update the post count for the anonymous user
f2e824
				if ($user_row['user_posts'])
f2e824
				{
f2e824
					$sql = 'UPDATE ' . USERS_TABLE . '
f2e824
						SET user_posts = user_posts + ' . $user_row['user_posts'] . '
f2e824
						WHERE user_id = ' . ANONYMOUS;
f2e824
					$db->sql_query($sql);
f2e824
				}
f2e824
			}
f2e824
f2e824
			$db->sql_transaction('commit');
f2e824
f2e824
		break;
f2e824
f2e824
		case 'remove':
f2e824
f2e824
			if (!function_exists('delete_posts'))
f2e824
			{
f2e824
				include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
f2e824
			}
f2e824
f2e824
			$sql = 'SELECT topic_id, COUNT(post_id) AS total_posts
f2e824
				FROM ' . POSTS_TABLE . "
f2e824
				WHERE poster_id = $user_id
f2e824
				GROUP BY topic_id";
f2e824
			$result = $db->sql_query($sql);
f2e824
f2e824
			$topic_id_ary = array();
f2e824
			while ($row = $db->sql_fetchrow($result))
f2e824
			{
f2e824
				$topic_id_ary[$row['topic_id']] = $row['total_posts'];
f2e824
			}
f2e824
			$db->sql_freeresult($result);
f2e824
f2e824
			if (sizeof($topic_id_ary))
f2e824
			{
f2e824
				$sql = 'SELECT topic_id, topic_replies, topic_replies_real
f2e824
					FROM ' . TOPICS_TABLE . '
f2e824
					WHERE ' . $db->sql_in_set('topic_id', array_keys($topic_id_ary));
f2e824
				$result = $db->sql_query($sql);
f2e824
f2e824
				$del_topic_ary = array();
f2e824
				while ($row = $db->sql_fetchrow($result))
f2e824
				{
f2e824
					if (max($row['topic_replies'], $row['topic_replies_real']) + 1 == $topic_id_ary[$row['topic_id']])
f2e824
					{
f2e824
						$del_topic_ary[] = $row['topic_id'];
f2e824
					}
f2e824
				}
f2e824
				$db->sql_freeresult($result);
f2e824
f2e824
				if (sizeof($del_topic_ary))
f2e824
				{
f2e824
					$sql = 'DELETE FROM ' . TOPICS_TABLE . '
f2e824
						WHERE ' . $db->sql_in_set('topic_id', $del_topic_ary);
f2e824
					$db->sql_query($sql);
f2e824
				}
f2e824
			}
f2e824
f2e824
			// Delete posts, attachments, etc.
f2e824
			delete_posts('poster_id', $user_id);
f2e824
f2e824
		break;
f2e824
	}
f2e824
f2e824
	$db->sql_transaction('begin');
f2e824
f2e824
	$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);
f2e824
f2e824
	foreach ($table_ary as $table)
f2e824
	{
f2e824
		$sql = "DELETE FROM $table
f2e824
			WHERE user_id = $user_id";
f2e824
		$db->sql_query($sql);
f2e824
	}
f2e824
f2e824
	$cache->destroy('sql', MODERATOR_CACHE_TABLE);
f2e824
f2e824
	// Remove any undelivered mails...
f2e824
	$sql = 'SELECT msg_id, user_id
f2e824
		FROM ' . PRIVMSGS_TO_TABLE . '
f2e824
		WHERE author_id = ' . $user_id . '
f2e824
			AND folder_id = ' . PRIVMSGS_NO_BOX;
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	$undelivered_msg = $undelivered_user = array();
f2e824
	while ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$undelivered_msg[] = $row['msg_id'];
f2e824
		$undelivered_user[$row['user_id']][] = true;
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if (sizeof($undelivered_msg))
f2e824
	{
f2e824
		$sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
f2e824
			WHERE ' . $db->sql_in_set('msg_id', $undelivered_msg);
f2e824
		$db->sql_query($sql);
f2e824
	}
f2e824
f2e824
	$sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . '
f2e824
		WHERE author_id = ' . $user_id . '
f2e824
			AND folder_id = ' . PRIVMSGS_NO_BOX;
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	// Delete all to-information
f2e824
	$sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . '
f2e824
		WHERE user_id = ' . $user_id;
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	// Set the remaining author id to anonymous - this way users are still able to read messages from users being removed
f2e824
	$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
f2e824
		SET author_id = ' . ANONYMOUS . '
f2e824
		WHERE author_id = ' . $user_id;
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
f2e824
		SET author_id = ' . ANONYMOUS . '
f2e824
		WHERE author_id = ' . $user_id;
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	foreach ($undelivered_user as $_user_id => $ary)
f2e824
	{
f2e824
		if ($_user_id == $user_id)
f2e824
		{
f2e824
			continue;
f2e824
		}
f2e824
f2e824
		$sql = 'UPDATE ' . USERS_TABLE . '
f2e824
			SET user_new_privmsg = user_new_privmsg - ' . sizeof($ary) . ',
f2e824
				user_unread_privmsg = user_unread_privmsg - ' . sizeof($ary) . '
f2e824
			WHERE user_id = ' . $_user_id;
f2e824
		$db->sql_query($sql);
f2e824
	}
f2e824
f2e824
	$db->sql_transaction('commit');
f2e824
f2e824
	// Reset newest user info if appropriate
f2e824
	if ($config['newest_user_id'] == $user_id)
f2e824
	{
f2e824
		update_last_username();
f2e824
	}
f2e824
f2e824
	// Decrement number of users if this user is active
f2e824
	if ($user_row['user_type'] != USER_INACTIVE && $user_row['user_type'] != USER_IGNORE)
f2e824
	{
f2e824
		set_config('num_users', $config['num_users'] - 1, true);
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Flips user_type from active to inactive and vice versa, handles group membership updates
f2e824
*
f2e824
* @param string $mode can be flip for flipping from active/inactive, activate or deactivate
f2e824
*/
f2e824
function user_active_flip($mode, $user_id_ary, $reason = INACTIVE_MANUAL)
f2e824
{
f2e824
	global $config, $db, $user, $auth;
f2e824
f2e824
	$deactivated = $activated = 0;
f2e824
	$sql_statements = array();
f2e824
f2e824
	if (!is_array($user_id_ary))
f2e824
	{
f2e824
		$user_id_ary = array($user_id_ary);
f2e824
	}
f2e824
f2e824
	if (!sizeof($user_id_ary))
f2e824
	{
f2e824
		return;
f2e824
	}
f2e824
f2e824
	$sql = 'SELECT user_id, group_id, user_type, user_inactive_reason
f2e824
		FROM ' . USERS_TABLE . '
f2e824
		WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	while ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$sql_ary = array();
f2e824
f2e824
		if ($row['user_type'] == USER_IGNORE || $row['user_type'] == USER_FOUNDER ||
f2e824
			($mode == 'activate' && $row['user_type'] != USER_INACTIVE) ||
f2e824
			($mode == 'deactivate' && $row['user_type'] == USER_INACTIVE))
f2e824
		{
f2e824
			continue;
f2e824
		}
f2e824
f2e824
		if ($row['user_type'] == USER_INACTIVE)
f2e824
		{
f2e824
			$activated++;
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$deactivated++;
f2e824
f2e824
			// Remove the users session key...
f2e824
			$user->reset_login_keys($row['user_id']);
f2e824
		}
f2e824
f2e824
		$sql_ary += array(
f2e824
			'user_type'				=> ($row['user_type'] == USER_NORMAL) ? USER_INACTIVE : USER_NORMAL,
f2e824
			'user_inactive_time'	=> ($row['user_type'] == USER_NORMAL) ? time() : 0,
f2e824
			'user_inactive_reason'	=> ($row['user_type'] == USER_NORMAL) ? $reason : 0,
f2e824
		);
f2e824
f2e824
		$sql_statements[$row['user_id']] = $sql_ary;
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if (sizeof($sql_statements))
f2e824
	{
f2e824
		foreach ($sql_statements as $user_id => $sql_ary)
f2e824
		{
f2e824
			$sql = 'UPDATE ' . USERS_TABLE . '
f2e824
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
f2e824
				WHERE user_id = ' . $user_id;
f2e824
			$db->sql_query($sql);
f2e824
		}
f2e824
f2e824
		$auth->acl_clear_prefetch(array_keys($sql_statements));
f2e824
	}
f2e824
f2e824
	if ($deactivated)
f2e824
	{
f2e824
		set_config('num_users', $config['num_users'] - $deactivated, true);
f2e824
	}
f2e824
f2e824
	if ($activated)
f2e824
	{
f2e824
		set_config('num_users', $config['num_users'] + $activated, true);
f2e824
	}
f2e824
f2e824
	// Update latest username
f2e824
	update_last_username();
f2e824
}
f2e824
f2e824
/**
f2e824
* Add a ban or ban exclusion to the banlist. Bans either a user, an IP or an email address
f2e824
*
f2e824
* @param string $mode Type of ban. One of the following: user, ip, email
f2e824
* @param mixed $ban Banned entity. Either string or array with usernames, ips or email addresses
f2e824
* @param int $ban_len Ban length in minutes
f2e824
* @param string $ban_len_other Ban length as a date (YYYY-MM-DD)
f2e824
* @param boolean $ban_exclude Exclude these entities from banning?
f2e824
* @param string $ban_reason String describing the reason for this ban
f2e824
* @return boolean
f2e824
*/
f2e824
function user_ban($mode, $ban, $ban_len, $ban_len_other, $ban_exclude, $ban_reason, $ban_give_reason = '')
f2e824
{
f2e824
	global $db, $user, $auth, $cache;
f2e824
f2e824
	// Delete stale bans
f2e824
	$sql = 'DELETE FROM ' . BANLIST_TABLE . '
f2e824
		WHERE ban_end < ' . time() . '
f2e824
			AND ban_end <> 0';
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	$ban_list = (!is_array($ban)) ? array_unique(explode("\n", $ban)) : $ban;
f2e824
	$ban_list_log = implode(', ', $ban_list);
f2e824
f2e824
	$current_time = time();
f2e824
f2e824
	// Set $ban_end to the unix time when the ban should end. 0 is a permanent ban.
f2e824
	if ($ban_len)
f2e824
	{
f2e824
		if ($ban_len != -1 || !$ban_len_other)
f2e824
		{
f2e824
			$ban_end = max($current_time, $current_time + ($ban_len) * 60);
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$ban_other = explode('-', $ban_len_other);
f2e824
			if (sizeof($ban_other) == 3 && ((int)$ban_other[0] < 9999) &&
f2e824
				(strlen($ban_other[0]) == 4) && (strlen($ban_other[1]) == 2) && (strlen($ban_other[2]) == 2))
f2e824
			{
f2e824
				$ban_end = max($current_time, gmmktime(0, 0, 0, (int)$ban_other[1], (int)$ban_other[2], (int)$ban_other[0]));
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				trigger_error('LENGTH_BAN_INVALID');
f2e824
			}
f2e824
		}
f2e824
	}
f2e824
	else
f2e824
	{
f2e824
		$ban_end = 0;
f2e824
	}
f2e824
f2e824
	$founder = $founder_names = array();
f2e824
f2e824
	if (!$ban_exclude)
f2e824
	{
f2e824
		// Create a list of founder...
f2e824
		$sql = 'SELECT user_id, user_email, username_clean
f2e824
			FROM ' . USERS_TABLE . '
f2e824
			WHERE user_type = ' . USER_FOUNDER;
f2e824
		$result = $db->sql_query($sql);
f2e824
f2e824
		while ($row = $db->sql_fetchrow($result))
f2e824
		{
f2e824
			$founder[$row['user_id']] = $row['user_email'];
f2e824
			$founder_names[$row['user_id']] = $row['username_clean'];
f2e824
		}
f2e824
		$db->sql_freeresult($result);
f2e824
	}
f2e824
f2e824
	$banlist_ary = array();
f2e824
f2e824
	switch ($mode)
f2e824
	{
f2e824
		case 'user':
f2e824
			$type = 'ban_userid';
f2e824
f2e824
			// At the moment we do not support wildcard username banning
f2e824
f2e824
			// Select the relevant user_ids.
f2e824
			$sql_usernames = array();
f2e824
f2e824
			foreach ($ban_list as $username)
f2e824
			{
f2e824
				$username = trim($username);
f2e824
				if ($username != '')
f2e824
				{
f2e824
					$clean_name = utf8_clean_string($username);
f2e824
					if ($clean_name == $user->data['username_clean'])
f2e824
					{
f2e824
						trigger_error('CANNOT_BAN_YOURSELF', E_USER_WARNING);
f2e824
					}
f2e824
					if (in_array($clean_name, $founder_names))
f2e824
					{
f2e824
						trigger_error('CANNOT_BAN_FOUNDER', E_USER_WARNING);
f2e824
					}
f2e824
					$sql_usernames[] = $clean_name;
f2e824
				}
f2e824
			}
f2e824
f2e824
			// Make sure we have been given someone to ban
f2e824
			if (!sizeof($sql_usernames))
f2e824
			{
f2e824
				trigger_error('NO_USER_SPECIFIED');
f2e824
			}
f2e824
f2e824
			$sql = 'SELECT user_id
f2e824
				FROM ' . USERS_TABLE . '
f2e824
				WHERE ' . $db->sql_in_set('username_clean', $sql_usernames);
f2e824
f2e824
			// Do not allow banning yourself
f2e824
			if (sizeof($founder))
f2e824
			{
f2e824
				$sql .= ' AND ' . $db->sql_in_set('user_id', array_merge(array_keys($founder), array($user->data['user_id'])), true);
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$sql .= ' AND user_id <> ' . $user->data['user_id'];
f2e824
			}
f2e824
f2e824
			$result = $db->sql_query($sql);
f2e824
f2e824
			if ($row = $db->sql_fetchrow($result))
f2e824
			{
f2e824
				do
f2e824
				{
f2e824
					$banlist_ary[] = (int) $row['user_id'];
f2e824
				}
f2e824
				while ($row = $db->sql_fetchrow($result));
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$db->sql_freeresult($result);
f2e824
				trigger_error('NO_USERS');
f2e824
			}
f2e824
			$db->sql_freeresult($result);
f2e824
		break;
f2e824
f2e824
		case 'ip':
f2e824
			$type = 'ban_ip';
f2e824
f2e824
			foreach ($ban_list as $ban_item)
f2e824
			{
f2e824
				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))
f2e824
				{
f2e824
					// This is an IP range
f2e824
					// Don't ask about all this, just don't ask ... !
f2e824
					$ip_1_counter = $ip_range_explode[1];
f2e824
					$ip_1_end = $ip_range_explode[5];
f2e824
f2e824
					while ($ip_1_counter <= $ip_1_end)
f2e824
					{
f2e824
						$ip_2_counter = ($ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[2] : 0;
f2e824
						$ip_2_end = ($ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[6];
f2e824
f2e824
						if ($ip_2_counter == 0 && $ip_2_end == 254)
f2e824
						{
f2e824
							$ip_2_counter = 256;
f2e824
							$ip_2_fragment = 256;
f2e824
f2e824
							$banlist_ary[] = "$ip_1_counter.*";
f2e824
						}
f2e824
f2e824
						while ($ip_2_counter <= $ip_2_end)
f2e824
						{
f2e824
							$ip_3_counter = ($ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[3] : 0;
f2e824
							$ip_3_end = ($ip_2_counter < $ip_2_end || $ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[7];
f2e824
f2e824
							if ($ip_3_counter == 0 && $ip_3_end == 254)
f2e824
							{
f2e824
								$ip_3_counter = 256;
f2e824
								$ip_3_fragment = 256;
f2e824
f2e824
								$banlist_ary[] = "$ip_1_counter.$ip_2_counter.*";
f2e824
							}
f2e824
f2e824
							while ($ip_3_counter <= $ip_3_end)
f2e824
							{
f2e824
								$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;
f2e824
								$ip_4_end = ($ip_3_counter < $ip_3_end || $ip_2_counter < $ip_2_end) ? 254 : $ip_range_explode[8];
f2e824
f2e824
								if ($ip_4_counter == 0 && $ip_4_end == 254)
f2e824
								{
f2e824
									$ip_4_counter = 256;
f2e824
									$ip_4_fragment = 256;
f2e824
f2e824
									$banlist_ary[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.*";
f2e824
								}
f2e824
f2e824
								while ($ip_4_counter <= $ip_4_end)
f2e824
								{
f2e824
									$banlist_ary[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.$ip_4_counter";
f2e824
									$ip_4_counter++;
f2e824
								}
f2e824
								$ip_3_counter++;
f2e824
							}
f2e824
							$ip_2_counter++;
f2e824
						}
f2e824
						$ip_1_counter++;
f2e824
					}
f2e824
				}
f2e824
				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)))
f2e824
				{
f2e824
					// Normal IP address
f2e824
					$banlist_ary[] = trim($ban_item);
f2e824
				}
f2e824
				else if (preg_match('#^\*$#', trim($ban_item)))
f2e824
				{
f2e824
					// Ban all IPs
f2e824
					$banlist_ary[] = '*';
f2e824
				}
f2e824
				else if (preg_match('#^([\w\-_]\.?){2,}$#is', trim($ban_item)))
f2e824
				{
f2e824
					// hostname
f2e824
					$ip_ary = gethostbynamel(trim($ban_item));
f2e824
f2e824
					if (!empty($ip_ary))
f2e824
					{
f2e824
						foreach ($ip_ary as $ip)
f2e824
						{
f2e824
							if ($ip)
f2e824
							{
f2e824
								if (strlen($ip) > 40)
f2e824
								{
f2e824
									continue;
f2e824
								}
f2e824
f2e824
								$banlist_ary[] = $ip;
f2e824
							}
f2e824
						}
f2e824
					}
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					trigger_error('NO_IPS_DEFINED');
f2e824
				}
f2e824
			}
f2e824
		break;
f2e824
f2e824
		case 'email':
f2e824
			$type = 'ban_email';
f2e824
f2e824
			foreach ($ban_list as $ban_item)
f2e824
			{
f2e824
				$ban_item = trim($ban_item);
f2e824
f2e824
				if (preg_match('#^.*?@*|(([a-z0-9\-]+\.)+([a-z]{2,3}))$#i', $ban_item))
f2e824
				{
f2e824
					if (strlen($ban_item) > 100)
f2e824
					{
f2e824
						continue;
f2e824
					}
f2e824
f2e824
					if (!sizeof($founder) || !in_array($ban_item, $founder))
f2e824
					{
f2e824
						$banlist_ary[] = $ban_item;
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
f2e824
			if (sizeof($ban_list) == 0)
f2e824
			{
f2e824
				trigger_error('NO_EMAILS_DEFINED');
f2e824
			}
f2e824
		break;
f2e824
f2e824
		default:
f2e824
			trigger_error('NO_MODE');
f2e824
		break;
f2e824
	}
f2e824
f2e824
	// Fetch currently set bans of the specified type and exclude state. Prevent duplicate bans.
f2e824
	$sql_where = ($type == 'ban_userid') ? 'ban_userid <> 0' : "$type <> ''";
f2e824
f2e824
	$sql = "SELECT $type
f2e824
		FROM " . BANLIST_TABLE . "
f2e824
		WHERE $sql_where
f2e824
			AND ban_exclude = " . (int) $ban_exclude;
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	// Reset $sql_where, because we use it later...
f2e824
	$sql_where = '';
f2e824
f2e824
	if ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$banlist_ary_tmp = array();
f2e824
		do
f2e824
		{
f2e824
			switch ($mode)
f2e824
			{
f2e824
				case 'user':
f2e824
					$banlist_ary_tmp[] = $row['ban_userid'];
f2e824
				break;
f2e824
f2e824
				case 'ip':
f2e824
					$banlist_ary_tmp[] = $row['ban_ip'];
f2e824
				break;
f2e824
f2e824
				case 'email':
f2e824
					$banlist_ary_tmp[] = $row['ban_email'];
f2e824
				break;
f2e824
			}
f2e824
		}
f2e824
		while ($row = $db->sql_fetchrow($result));
f2e824
f2e824
		$banlist_ary = array_unique(array_diff($banlist_ary, $banlist_ary_tmp));
f2e824
		unset($banlist_ary_tmp);
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	// We have some entities to ban
f2e824
	if (sizeof($banlist_ary))
f2e824
	{
f2e824
		$sql_ary = array();
f2e824
f2e824
		foreach ($banlist_ary as $ban_entry)
f2e824
		{
f2e824
			$sql_ary[] = array(
f2e824
				$type				=> $ban_entry,
f2e824
				'ban_start'			=> (int) $current_time,
f2e824
				'ban_end'			=> (int) $ban_end,
f2e824
				'ban_exclude'		=> (int) $ban_exclude,
f2e824
				'ban_reason'		=> (string) $ban_reason,
f2e824
				'ban_give_reason'	=> (string) $ban_give_reason,
f2e824
			);
f2e824
		}
f2e824
f2e824
		$db->sql_multi_insert(BANLIST_TABLE, $sql_ary);
f2e824
f2e824
		// If we are banning we want to logout anyone matching the ban
f2e824
		if (!$ban_exclude)
f2e824
		{
f2e824
			switch ($mode)
f2e824
			{
f2e824
				case 'user':
f2e824
					$sql_where = 'WHERE ' . $db->sql_in_set('session_user_id', $banlist_ary);
f2e824
				break;
f2e824
f2e824
				case 'ip':
f2e824
					$sql_where = 'WHERE ' . $db->sql_in_set('session_ip', $banlist_ary);
f2e824
				break;
f2e824
f2e824
				case 'email':
f2e824
					$banlist_ary_sql = array();
f2e824
f2e824
					foreach ($banlist_ary as $ban_entry)
f2e824
					{
f2e824
						$banlist_ary_sql[] = (string) str_replace('*', '%', $ban_entry);
f2e824
					}
f2e824
f2e824
					$sql = 'SELECT user_id
f2e824
						FROM ' . USERS_TABLE . '
f2e824
						WHERE ' . $db->sql_in_set('user_email', $banlist_ary_sql);
f2e824
					$result = $db->sql_query($sql);
f2e824
f2e824
					$sql_in = array();
f2e824
f2e824
					if ($row = $db->sql_fetchrow($result))
f2e824
					{
f2e824
						do
f2e824
						{
f2e824
							$sql_in[] = $row['user_id'];
f2e824
						}
f2e824
						while ($row = $db->sql_fetchrow($result));
f2e824
f2e824
						$sql_where = 'WHERE ' . $db->sql_in_set('session_user_id', $sql_in);
f2e824
					}
f2e824
					$db->sql_freeresult($result);
f2e824
				break;
f2e824
			}
f2e824
f2e824
			if (isset($sql_where) && $sql_where)
f2e824
			{
f2e824
				$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
f2e824
					$sql_where";
f2e824
				$db->sql_query($sql);
f2e824
f2e824
				if ($mode == 'user')
f2e824
				{
f2e824
					$sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . ' ' . ((in_array('*', $banlist_ary)) ? '' : 'WHERE ' . $db->sql_in_set('user_id', $banlist_ary));
f2e824
					$db->sql_query($sql);
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Update log
f2e824
		$log_entry = ($ban_exclude) ? 'LOG_BAN_EXCLUDE_' : 'LOG_BAN_';
f2e824
f2e824
		// Add to moderator and admin log
f2e824
		add_log('admin', $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
f2e824
		add_log('mod', 0, 0, $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
f2e824
f2e824
		$cache->destroy('sql', BANLIST_TABLE);
f2e824
f2e824
		return true;
f2e824
	}
f2e824
f2e824
	// There was nothing to ban/exclude. But destroying the cache because of the removal of stale bans.
f2e824
	$cache->destroy('sql', BANLIST_TABLE);
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Unban User
f2e824
*/
f2e824
function user_unban($mode, $ban)
f2e824
{
f2e824
	global $db, $user, $auth, $cache;
f2e824
f2e824
	// Delete stale bans
f2e824
	$sql = 'DELETE FROM ' . BANLIST_TABLE . '
f2e824
		WHERE ban_end < ' . time() . '
f2e824
			AND ban_end <> 0';
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	if (!is_array($ban))
f2e824
	{
f2e824
		$ban = array($ban);
f2e824
	}
f2e824
f2e824
	$unban_sql = array_map('intval', $ban);
f2e824
f2e824
	if (sizeof($unban_sql))
f2e824
	{
f2e824
		// Grab details of bans for logging information later
f2e824
		switch ($mode)
f2e824
		{
f2e824
			case 'user':
f2e824
				$sql = 'SELECT u.username AS unban_info
f2e824
					FROM ' . USERS_TABLE . ' u, ' . BANLIST_TABLE . ' b
f2e824
					WHERE ' . $db->sql_in_set('b.ban_id', $unban_sql) . '
f2e824
						AND u.user_id = b.ban_userid';
f2e824
			break;
f2e824
f2e824
			case 'email':
f2e824
				$sql = 'SELECT ban_email AS unban_info
f2e824
					FROM ' . BANLIST_TABLE . '
f2e824
					WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
f2e824
			break;
f2e824
f2e824
			case 'ip':
f2e824
				$sql = 'SELECT ban_ip AS unban_info
f2e824
					FROM ' . BANLIST_TABLE . '
f2e824
					WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
f2e824
			break;
f2e824
		}
f2e824
		$result = $db->sql_query($sql);
f2e824
f2e824
		$l_unban_list = '';
f2e824
		while ($row = $db->sql_fetchrow($result))
f2e824
		{
f2e824
			$l_unban_list .= (($l_unban_list != '') ? ', ' : '') . $row['unban_info'];
f2e824
		}
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		$sql = 'DELETE FROM ' . BANLIST_TABLE . '
f2e824
			WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
f2e824
		$db->sql_query($sql);
f2e824
f2e824
		// Add to moderator and admin log
f2e824
		add_log('admin', 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
f2e824
		add_log('mod', 0, 0, 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
f2e824
	}
f2e824
f2e824
	$cache->destroy('sql', BANLIST_TABLE);
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Whois facility
f2e824
*/
f2e824
function user_ipwhois($ip)
f2e824
{
f2e824
	$ipwhois = '';
f2e824
f2e824
	// Check IP
f2e824
	// Only supporting IPv4 at the moment...
f2e824
	if (empty($ip) || !preg_match(get_preg_expression('ipv4'), $ip))
f2e824
	{
f2e824
		return '';
f2e824
	}
f2e824
f2e824
	$match = array(
f2e824
		'#RIPE\.NET#is'				=> 'whois.ripe.net',
f2e824
		'#whois\.apnic\.net#is'		=> 'whois.apnic.net',
f2e824
		'#nic\.ad\.jp#is'			=> 'whois.nic.ad.jp',
f2e824
		'#whois\.registro\.br#is'	=> 'whois.registro.br'
f2e824
	);
f2e824
f2e824
	if (($fsk = @fsockopen('whois.arin.net', 43)))
f2e824
	{
f2e824
		fputs($fsk, "$ip\n");
f2e824
		while (!feof($fsk))
f2e824
		{
f2e824
			$ipwhois .= fgets($fsk, 1024);
f2e824
		}
f2e824
		@fclose($fsk);
f2e824
	}
f2e824
f2e824
	foreach (array_keys($match) as $server)
f2e824
	{
f2e824
		if (preg_match($server, $ipwhois))
f2e824
		{
f2e824
			$ipwhois = '';
f2e824
			if (($fsk = @fsockopen($match[$server], 43)))
f2e824
			{
f2e824
				fputs($fsk, "$ip\n");
f2e824
				while (!feof($fsk))
f2e824
				{
f2e824
					$ipwhois .= fgets($fsk, 1024);
f2e824
				}
f2e824
				@fclose($fsk);
f2e824
			}
f2e824
			break;
f2e824
		}
f2e824
	}
f2e824
f2e824
	$ipwhois = htmlspecialchars($ipwhois);
f2e824
f2e824
	// Magic URL ;)
f2e824
	return trim(make_clickable($ipwhois, false, ''));
f2e824
}
f2e824
f2e824
/**
f2e824
* Data validation ... used primarily but not exclusively by ucp modules
f2e824
*
f2e824
* "Master" function for validating a range of data types
f2e824
*/
f2e824
function validate_data($data, $val_ary)
f2e824
{
f2e824
	global $user;
f2e824
f2e824
	$error = array();
f2e824
f2e824
	foreach ($val_ary as $var => $val_seq)
f2e824
	{
f2e824
		if (!is_array($val_seq[0]))
f2e824
		{
f2e824
			$val_seq = array($val_seq);
f2e824
		}
f2e824
f2e824
		foreach ($val_seq as $validate)
f2e824
		{
f2e824
			$function = array_shift($validate);
f2e824
			array_unshift($validate, $data[$var]);
f2e824
f2e824
			if ($result = call_user_func_array('validate_' . $function, $validate))
f2e824
			{
f2e824
				// Since errors are checked later for their language file existence, we need to make sure custom errors are not adjusted.
f2e824
				$error[] = (empty($user->lang[$result . '_' . strtoupper($var)])) ? $result : $result . '_' . strtoupper($var);
f2e824
			}
f2e824
		}
f2e824
	}
f2e824
f2e824
	return $error;
f2e824
}
f2e824
f2e824
/**
f2e824
* Validate String
f2e824
*
f2e824
* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
f2e824
*/
f2e824
function validate_string($string, $optional = false, $min = 0, $max = 0)
f2e824
{
f2e824
	if (empty($string) && $optional)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if ($min && utf8_strlen(htmlspecialchars_decode($string)) < $min)
f2e824
	{
f2e824
		return 'TOO_SHORT';
f2e824
	}
f2e824
	else if ($max && utf8_strlen(htmlspecialchars_decode($string)) > $max)
f2e824
	{
f2e824
		return 'TOO_LONG';
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Validate Number
f2e824
*
f2e824
* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
f2e824
*/
f2e824
function validate_num($num, $optional = false, $min = 0, $max = 1E99)
f2e824
{
f2e824
	if (empty($num) && $optional)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if ($num < $min)
f2e824
	{
f2e824
		return 'TOO_SMALL';
f2e824
	}
f2e824
	else if ($num > $max)
f2e824
	{
f2e824
		return 'TOO_LARGE';
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Validate Date
f2e824
* @param String $string a date in the dd-mm-yyyy format
f2e824
* @return	boolean
f2e824
*/
f2e824
function validate_date($date_string, $optional = false)
f2e824
{
f2e824
	$date = explode('-', $date_string);
f2e824
	if ((empty($date) || sizeof($date) != 3) && $optional)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
	else if ($optional)
f2e824
	{
f2e824
		for ($field = 0; $field <= 1; $field++)
f2e824
		{
f2e824
			$date[$field] = (int) $date[$field];
f2e824
			if (empty($date[$field]))
f2e824
			{
f2e824
				$date[$field] = 1;
f2e824
			}
f2e824
		}
f2e824
		$date[2] = (int) $date[2];
f2e824
		// assume an arbitrary leap year
f2e824
		if (empty($date[2]))
f2e824
		{
f2e824
			$date[2] = 1980;
f2e824
		}
f2e824
	}
f2e824
f2e824
	if (sizeof($date) != 3 || !checkdate($date[1], $date[0], $date[2]))
f2e824
	{
f2e824
		return 'INVALID';
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
f2e824
/**
f2e824
* Validate Match
f2e824
*
f2e824
* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
f2e824
*/
f2e824
function validate_match($string, $optional = false, $match = '')
f2e824
{
f2e824
	if (empty($string) && $optional)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if (empty($match))
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if (!preg_match($match, $string))
f2e824
	{
f2e824
		return 'WRONG_DATA';
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Check to see if the username has been taken, or if it is disallowed.
f2e824
* Also checks if it includes the " character, which we don't allow in usernames.
f2e824
* Used for registering, changing names, and posting anonymously with a username
f2e824
*
f2e824
* @param string $username The username to check
f2e824
* @param string $allowed_username An allowed username, default being $user->data['username']
f2e824
*
f2e824
* @return	mixed	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
f2e824
*/
f2e824
function validate_username($username, $allowed_username = false)
f2e824
{
f2e824
	global $config, $db, $user, $cache;
f2e824
f2e824
	$clean_username = utf8_clean_string($username);
f2e824
	$allowed_username = ($allowed_username === false) ? $user->data['username_clean'] : utf8_clean_string($allowed_username);
f2e824
f2e824
	if ($allowed_username == $clean_username)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// ... fast checks first.
f2e824
	if (strpos($username, '"') !== false || strpos($username, '"') !== false || empty($clean_username))
f2e824
	{
f2e824
		return 'INVALID_CHARS';
f2e824
	}
f2e824
f2e824
	$mbstring = $pcre = false;
f2e824
f2e824
	// generic UTF-8 character types supported?
f2e824
	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)
f2e824
	{
f2e824
		$pcre = true;
f2e824
	}
f2e824
	else if (function_exists('mb_ereg_match'))
f2e824
	{
f2e824
		mb_regex_encoding('UTF-8');
f2e824
		$mbstring = true;
f2e824
	}
f2e824
f2e824
	switch ($config['allow_name_chars'])
f2e824
	{
f2e824
		case 'USERNAME_CHARS_ANY':
f2e824
			$pcre = true;
f2e824
			$regex = '.+';
f2e824
		break;
f2e824
f2e824
		case 'USERNAME_ALPHA_ONLY':
f2e824
			$pcre = true;
f2e824
			$regex = '[A-Za-z0-9]+';
f2e824
		break;
f2e824
f2e824
		case 'USERNAME_ALPHA_SPACERS':
f2e824
			$pcre = true;
f2e824
			$regex = '[A-Za-z0-9-[\]_+ ]+';
f2e824
		break;
f2e824
f2e824
		case 'USERNAME_LETTER_NUM':
f2e824
			if ($pcre)
f2e824
			{
f2e824
				$regex = '[\p{Lu}\p{Ll}\p{N}]+';
f2e824
			}
f2e824
			else if ($mbstring)
f2e824
			{
f2e824
				$regex = '[[:upper:][:lower:][:digit:]]+';
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$pcre = true;
f2e824
				$regex = '[a-zA-Z0-9]+';
f2e824
			}
f2e824
		break;
f2e824
f2e824
		case 'USERNAME_LETTER_NUM_SPACERS':
f2e824
			if ($pcre)
f2e824
			{
f2e824
				$regex = '[-\]_+ [\p{Lu}\p{Ll}\p{N}]+';
f2e824
			}
f2e824
			else if ($mbstring)
f2e824
			{
f2e824
				$regex = '[-\]_+ [[:upper:][:lower:][:digit:]]+';
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$pcre = true;
f2e824
				$regex = '[-\]_+ [a-zA-Z0-9]+';
f2e824
			}
f2e824
		break;
f2e824
f2e824
		case 'USERNAME_ASCII':
f2e824
		default:
f2e824
			$pcre = true;
f2e824
			$regex = '[\x01-\x7F]+';
f2e824
		break;
f2e824
	}
f2e824
f2e824
	if ($pcre)
f2e824
	{
f2e824
		if (!preg_match('#^' . $regex . '$#u', $username))
f2e824
		{
f2e824
			return 'INVALID_CHARS';
f2e824
		}
f2e824
	}
f2e824
	else if ($mbstring)
f2e824
	{
f2e824
		$matches = array();
f2e824
		mb_ereg_search_init('^' . $username . '$', $regex, $matches);
f2e824
		if (!mb_ereg_search())
f2e824
		{
f2e824
			return 'INVALID_CHARS';
f2e824
		}
f2e824
	}
f2e824
f2e824
	$sql = 'SELECT username
f2e824
		FROM ' . USERS_TABLE . "
f2e824
		WHERE username_clean = '" . $db->sql_escape($clean_username) . "'";
f2e824
	$result = $db->sql_query($sql);
f2e824
	$row = $db->sql_fetchrow($result);
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if ($row)
f2e824
	{
f2e824
		return 'USERNAME_TAKEN';
f2e824
	}
f2e824
f2e824
	$sql = 'SELECT group_name
f2e824
		FROM ' . GROUPS_TABLE . "
f2e824
		WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($username)) . "'";
f2e824
	$result = $db->sql_query($sql);
f2e824
	$row = $db->sql_fetchrow($result);
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if ($row)
f2e824
	{
f2e824
		return 'USERNAME_TAKEN';
f2e824
	}
f2e824
f2e824
	$bad_usernames = $cache->obtain_disallowed_usernames();
f2e824
f2e824
	foreach ($bad_usernames as $bad_username)
f2e824
	{
f2e824
		if (preg_match('#^' . $bad_username . '$#', $clean_username))
f2e824
		{
f2e824
			return 'USERNAME_DISALLOWED';
f2e824
		}
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Check to see if the password meets the complexity settings
f2e824
*
f2e824
* @return	boolean|string	Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
f2e824
*/
f2e824
function validate_password($password)
f2e824
{
f2e824
	global $config, $db, $user;
f2e824
f2e824
	if (!$password)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$pcre = $mbstring = false;
f2e824
f2e824
	// generic UTF-8 character types supported?
f2e824
	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)
f2e824
	{
f2e824
		$upp = '\p{Lu}';
f2e824
		$low = '\p{Ll}';
f2e824
		$let = '\p{L}';
f2e824
		$num = '\p{N}';
f2e824
		$sym = '[^\p{Lu}\p{Ll}\p{N}]';
f2e824
		$pcre = true;
f2e824
	}
f2e824
	else if (function_exists('mb_ereg_match'))
f2e824
	{
f2e824
		mb_regex_encoding('UTF-8');
f2e824
		$upp = '[[:upper:]]';
f2e824
		$low = '[[:lower:]]';
f2e824
		$let = '[[:lower:][:upper:]]';
f2e824
		$num = '[[:digit:]]';
f2e824
		$sym = '[^[:upper:][:lower:][:digit:]]';
f2e824
		$mbstring = true;
f2e824
	}
f2e824
	else
f2e824
	{
f2e824
		$upp = '[A-Z]';
f2e824
		$low = '[a-z]';
f2e824
		$let = '[a-zA-Z]';
f2e824
		$num = '[0-9]';
f2e824
		$sym = '[^A-Za-z0-9]';
f2e824
		$pcre = true;
f2e824
	}
f2e824
f2e824
	$chars = array();
f2e824
f2e824
	switch ($config['pass_complex'])
f2e824
	{
f2e824
		case 'PASS_TYPE_CASE':
f2e824
			$chars[] = $low;
f2e824
			$chars[] = $upp;
f2e824
		break;
f2e824
f2e824
		case 'PASS_TYPE_ALPHA':
f2e824
			$chars[] = $let;
f2e824
			$chars[] = $num;
f2e824
		break;
f2e824
f2e824
		case 'PASS_TYPE_SYMBOL':
f2e824
			$chars[] = $low;
f2e824
			$chars[] = $upp;
f2e824
			$chars[] = $num;
f2e824
			$chars[] = $sym;
f2e824
		break;
f2e824
	}
f2e824
f2e824
	if ($pcre)
f2e824
	{
f2e824
		foreach ($chars as $char)
f2e824
		{
f2e824
			if (!preg_match('#' . $char . '#u', $password))
f2e824
			{
f2e824
				return 'INVALID_CHARS';
f2e824
			}
f2e824
		}
f2e824
	}
f2e824
	else if ($mbstring)
f2e824
	{
f2e824
		foreach ($chars as $char)
f2e824
		{
f2e824
			if (mb_ereg($char, $password) === false)
f2e824
			{
f2e824
				return 'INVALID_CHARS';
f2e824
			}
f2e824
		}
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Check to see if email address is banned or already present in the DB
f2e824
*
f2e824
* @param string $email The email to check
f2e824
* @param string $allowed_email An allowed email, default being $user->data['user_email']
f2e824
*
f2e824
* @return mixed Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
f2e824
*/
f2e824
function validate_email($email, $allowed_email = false)
f2e824
{
f2e824
	global $config, $db, $user;
f2e824
f2e824
	$email = strtolower($email);
f2e824
	$allowed_email = ($allowed_email === false) ? strtolower($user->data['user_email']) : strtolower($allowed_email);
f2e824
f2e824
	if ($allowed_email == $email)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
f2e824
	{
f2e824
		return 'EMAIL_INVALID';
f2e824
	}
f2e824
f2e824
	// Check MX record.
f2e824
	// The idea for this is from reading the UseBB blog/announcement. :)
f2e824
	if ($config['email_check_mx'])
f2e824
	{
f2e824
		list(, $domain) = explode('@', $email);
f2e824
f2e824
		if (phpbb_checkdnsrr($domain, 'A') === false && phpbb_checkdnsrr($domain, 'MX') === false)
f2e824
		{
f2e824
			return 'DOMAIN_NO_MX_RECORD';
f2e824
		}
f2e824
	}
f2e824
f2e824
	if (($ban_reason = $user->check_ban(false, false, $email, true)) !== false)
f2e824
	{
f2e824
		return ($ban_reason === true) ? 'EMAIL_BANNED' : $ban_reason;
f2e824
	}
f2e824
f2e824
	if (!$config['allow_emailreuse'])
f2e824
	{
f2e824
		$sql = 'SELECT user_email_hash
f2e824
			FROM ' . USERS_TABLE . "
f2e824
			WHERE user_email_hash = " . (crc32($email) . strlen($email));
f2e824
		$result = $db->sql_query($sql);
f2e824
		$row = $db->sql_fetchrow($result);
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		if ($row)
f2e824
		{
f2e824
			return 'EMAIL_TAKEN';
f2e824
		}
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Validate jabber address
f2e824
* Taken from the jabber class within flyspray (see author notes)
f2e824
*
f2e824
* @author flyspray.org
f2e824
*/
f2e824
function validate_jabber($jid)
f2e824
{
f2e824
	if (!$jid)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$seperator_pos = strpos($jid, '@');
f2e824
f2e824
	if ($seperator_pos === false)
f2e824
	{
f2e824
		return 'WRONG_DATA';
f2e824
	}
f2e824
f2e824
	$username = substr($jid, 0, $seperator_pos);
f2e824
	$realm = substr($jid, $seperator_pos + 1);
f2e824
f2e824
	if (strlen($username) == 0 || strlen($realm) < 3)
f2e824
	{
f2e824
		return 'WRONG_DATA';
f2e824
	}
f2e824
f2e824
	$arr = explode('.', $realm);
f2e824
f2e824
	if (sizeof($arr) == 0)
f2e824
	{
f2e824
		return 'WRONG_DATA';
f2e824
	}
f2e824
f2e824
	foreach ($arr as $part)
f2e824
	{
f2e824
		if (substr($part, 0, 1) == '-' || substr($part, -1, 1) == '-')
f2e824
		{
f2e824
			return 'WRONG_DATA';
f2e824
		}
f2e824
f2e824
		if (!preg_match("@^[a-zA-Z0-9-.]+$@", $part))
f2e824
		{
f2e824
			return 'WRONG_DATA';
f2e824
		}
f2e824
	}
f2e824
f2e824
	$boundary = array(array(0, 127), array(192, 223), array(224, 239), array(240, 247), array(248, 251), array(252, 253));
f2e824
f2e824
	// Prohibited Characters RFC3454 + RFC3920
f2e824
	$prohibited = array(
f2e824
		// Table C.1.1
f2e824
		array(0x0020, 0x0020),		// SPACE
f2e824
		// Table C.1.2
f2e824
		array(0x00A0, 0x00A0),		// NO-BREAK SPACE
f2e824
		array(0x1680, 0x1680),		// OGHAM SPACE MARK
f2e824
		array(0x2000, 0x2001),		// EN QUAD
f2e824
		array(0x2001, 0x2001),		// EM QUAD
f2e824
		array(0x2002, 0x2002),		// EN SPACE
f2e824
		array(0x2003, 0x2003),		// EM SPACE
f2e824
		array(0x2004, 0x2004),		// THREE-PER-EM SPACE
f2e824
		array(0x2005, 0x2005),		// FOUR-PER-EM SPACE
f2e824
		array(0x2006, 0x2006),		// SIX-PER-EM SPACE
f2e824
		array(0x2007, 0x2007),		// FIGURE SPACE
f2e824
		array(0x2008, 0x2008),		// PUNCTUATION SPACE
f2e824
		array(0x2009, 0x2009),		// THIN SPACE
f2e824
		array(0x200A, 0x200A),		// HAIR SPACE
f2e824
		array(0x200B, 0x200B),		// ZERO WIDTH SPACE
f2e824
		array(0x202F, 0x202F),		// NARROW NO-BREAK SPACE
f2e824
		array(0x205F, 0x205F),		// MEDIUM MATHEMATICAL SPACE
f2e824
		array(0x3000, 0x3000),		// IDEOGRAPHIC SPACE
f2e824
		// Table C.2.1
f2e824
		array(0x0000, 0x001F),		// [CONTROL CHARACTERS]
f2e824
		array(0x007F, 0x007F),		// DELETE
f2e824
		// Table C.2.2
f2e824
		array(0x0080, 0x009F),		// [CONTROL CHARACTERS]
f2e824
		array(0x06DD, 0x06DD),		// ARABIC END OF AYAH
f2e824
		array(0x070F, 0x070F),		// SYRIAC ABBREVIATION MARK
f2e824
		array(0x180E, 0x180E),		// MONGOLIAN VOWEL SEPARATOR
f2e824
		array(0x200C, 0x200C), 		// ZERO WIDTH NON-JOINER
f2e824
		array(0x200D, 0x200D),		// ZERO WIDTH JOINER
f2e824
		array(0x2028, 0x2028),		// LINE SEPARATOR
f2e824
		array(0x2029, 0x2029),		// PARAGRAPH SEPARATOR
f2e824
		array(0x2060, 0x2060),		// WORD JOINER
f2e824
		array(0x2061, 0x2061),		// FUNCTION APPLICATION
f2e824
		array(0x2062, 0x2062),		// INVISIBLE TIMES
f2e824
		array(0x2063, 0x2063),		// INVISIBLE SEPARATOR
f2e824
		array(0x206A, 0x206F),		// [CONTROL CHARACTERS]
f2e824
		array(0xFEFF, 0xFEFF),		// ZERO WIDTH NO-BREAK SPACE
f2e824
		array(0xFFF9, 0xFFFC),		// [CONTROL CHARACTERS]
f2e824
		array(0x1D173, 0x1D17A),	// [MUSICAL CONTROL CHARACTERS]
f2e824
		// Table C.3
f2e824
		array(0xE000, 0xF8FF),		// [PRIVATE USE, PLANE 0]
f2e824
		array(0xF0000, 0xFFFFD),	// [PRIVATE USE, PLANE 15]
f2e824
		array(0x100000, 0x10FFFD),	// [PRIVATE USE, PLANE 16]
f2e824
		// Table C.4
f2e824
		array(0xFDD0, 0xFDEF),		// [NONCHARACTER CODE POINTS]
f2e824
		array(0xFFFE, 0xFFFF),		// [NONCHARACTER CODE POINTS]
f2e824
		array(0x1FFFE, 0x1FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x2FFFE, 0x2FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x3FFFE, 0x3FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x4FFFE, 0x4FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x5FFFE, 0x5FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x6FFFE, 0x6FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x7FFFE, 0x7FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x8FFFE, 0x8FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x9FFFE, 0x9FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0xAFFFE, 0xAFFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0xBFFFE, 0xBFFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0xCFFFE, 0xCFFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0xDFFFE, 0xDFFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0xEFFFE, 0xEFFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0xFFFFE, 0xFFFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		array(0x10FFFE, 0x10FFFF),	// [NONCHARACTER CODE POINTS]
f2e824
		// Table C.5
f2e824
		array(0xD800, 0xDFFF),		// [SURROGATE CODES]
f2e824
		// Table C.6
f2e824
		array(0xFFF9, 0xFFF9),		// INTERLINEAR ANNOTATION ANCHOR
f2e824
		array(0xFFFA, 0xFFFA),		// INTERLINEAR ANNOTATION SEPARATOR
f2e824
		array(0xFFFB, 0xFFFB),		// INTERLINEAR ANNOTATION TERMINATOR
f2e824
		array(0xFFFC, 0xFFFC),		// OBJECT REPLACEMENT CHARACTER
f2e824
		array(0xFFFD, 0xFFFD),		// REPLACEMENT CHARACTER
f2e824
		// Table C.7
f2e824
		array(0x2FF0, 0x2FFB),		// [IDEOGRAPHIC DESCRIPTION CHARACTERS]
f2e824
		// Table C.8
f2e824
		array(0x0340, 0x0340),		// COMBINING GRAVE TONE MARK
f2e824
		array(0x0341, 0x0341),		// COMBINING ACUTE TONE MARK
f2e824
		array(0x200E, 0x200E),		// LEFT-TO-RIGHT MARK
f2e824
		array(0x200F, 0x200F),		// RIGHT-TO-LEFT MARK
f2e824
		array(0x202A, 0x202A),		// LEFT-TO-RIGHT EMBEDDING
f2e824
		array(0x202B, 0x202B),		// RIGHT-TO-LEFT EMBEDDING
f2e824
		array(0x202C, 0x202C),		// POP DIRECTIONAL FORMATTING
f2e824
		array(0x202D, 0x202D),		// LEFT-TO-RIGHT OVERRIDE
f2e824
		array(0x202E, 0x202E),		// RIGHT-TO-LEFT OVERRIDE
f2e824
		array(0x206A, 0x206A),		// INHIBIT SYMMETRIC SWAPPING
f2e824
		array(0x206B, 0x206B),		// ACTIVATE SYMMETRIC SWAPPING
f2e824
		array(0x206C, 0x206C),		// INHIBIT ARABIC FORM SHAPING
f2e824
		array(0x206D, 0x206D),		// ACTIVATE ARABIC FORM SHAPING
f2e824
		array(0x206E, 0x206E),		// NATIONAL DIGIT SHAPES
f2e824
		array(0x206F, 0x206F),		// NOMINAL DIGIT SHAPES
f2e824
		// Table C.9
f2e824
		array(0xE0001, 0xE0001),	// LANGUAGE TAG
f2e824
		array(0xE0020, 0xE007F),	// [TAGGING CHARACTERS]
f2e824
		// RFC3920
f2e824
		array(0x22, 0x22),			// "
f2e824
		array(0x26, 0x26),			// &
f2e824
		array(0x27, 0x27),			// '
f2e824
		array(0x2F, 0x2F),			// /
f2e824
		array(0x3A, 0x3A),			// :
f2e824
		array(0x3C, 0x3C),			// <
f2e824
		array(0x3E, 0x3E),			// >
f2e824
		array(0x40, 0x40)			// @
f2e824
	);
f2e824
f2e824
	$pos = 0;
f2e824
	$result = true;
f2e824
f2e824
	while ($pos < strlen($username))
f2e824
	{
f2e824
		$len = $uni = 0;
f2e824
		for ($i = 0; $i <= 5; $i++)
f2e824
		{
f2e824
			if (ord($username[$pos]) >= $boundary[$i][0] && ord($username[$pos]) <= $boundary[$i][1])
f2e824
			{
f2e824
				$len = $i + 1;
f2e824
				$uni = (ord($username[$pos]) - $boundary[$i][0]) * pow(2, $i * 6);
f2e824
f2e824
				for ($k = 1; $k < $len; $k++)
f2e824
				{
f2e824
					$uni += (ord($username[$pos + $k]) - 128) * pow(2, ($i - $k) * 6);
f2e824
				}
f2e824
f2e824
				break;
f2e824
			}
f2e824
		}
f2e824
f2e824
		if ($len == 0)
f2e824
		{
f2e824
			return 'WRONG_DATA';
f2e824
		}
f2e824
f2e824
		foreach ($prohibited as $pval)
f2e824
		{
f2e824
			if ($uni >= $pval[0] && $uni <= $pval[1])
f2e824
			{
f2e824
				$result = false;
f2e824
				break 2;
f2e824
			}
f2e824
		}
f2e824
f2e824
		$pos = $pos + $len;
f2e824
	}
f2e824
f2e824
	if (!$result)
f2e824
	{
f2e824
		return 'WRONG_DATA';
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Remove avatar
f2e824
*/
f2e824
function avatar_delete($mode, $row, $clean_db = false)
f2e824
{
f2e824
	global $phpbb_root_path, $config, $db, $user;
f2e824
f2e824
	// Check if the users avatar is actually *not* a group avatar
f2e824
	if ($mode == 'user')
f2e824
	{
f2e824
		if (strpos($row['user_avatar'], 'g') === 0 || (((int)$row['user_avatar'] !== 0) && ((int)$row['user_avatar'] !== (int)$row['user_id'])))
f2e824
		{
f2e824
			return false;
f2e824
		}
f2e824
	}
f2e824
f2e824
	if ($clean_db)
f2e824
	{
f2e824
		avatar_remove_db($row[$mode . '_avatar']);
f2e824
	}
f2e824
	$filename = get_avatar_filename($row[$mode . '_avatar']);
f2e824
	if (file_exists($phpbb_root_path . $config['avatar_path'] . '/' . $filename))
f2e824
	{
f2e824
		@unlink($phpbb_root_path . $config['avatar_path'] . '/' . $filename);
f2e824
		return true;
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Remote avatar linkage
f2e824
*/
f2e824
function avatar_remote($data, &$error)
f2e824
{
f2e824
	global $config, $db, $user, $phpbb_root_path, $phpEx;
f2e824
f2e824
	if (!preg_match('#^(http|https|ftp)://#i', $data['remotelink']))
f2e824
	{
f2e824
		$data['remotelink'] = 'http://' . $data['remotelink'];
f2e824
	}
f2e824
	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']))
f2e824
	{
f2e824
		$error[] = $user->lang['AVATAR_URL_INVALID'];
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Make sure getimagesize works...
f2e824
	if (($image_data = @getimagesize($data['remotelink'])) === false && (empty($data['width']) || empty($data['height'])))
f2e824
	{
f2e824
		$error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if (!empty($image_data) && ($image_data[0] < 2 || $image_data[1] < 2))
f2e824
	{
f2e824
		$error[] = $user->lang['AVATAR_NO_SIZE'];
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$width = ($data['width'] && $data['height']) ? $data['width'] : $image_data[0];
f2e824
	$height = ($data['width'] && $data['height']) ? $data['height'] : $image_data[1];
f2e824
f2e824
	if ($width < 2 || $height < 2)
f2e824
	{
f2e824
		$error[] = $user->lang['AVATAR_NO_SIZE'];
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Check image type
f2e824
	include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
f2e824
	$types = fileupload::image_types();
f2e824
	$extension = strtolower(filespec::get_extension($data['remotelink']));
f2e824
f2e824
	if (!empty($image_data) && (!isset($types[$image_data[2]]) || !in_array($extension, $types[$image_data[2]])))
f2e824
	{
f2e824
		if (!isset($types[$image_data[2]]))
f2e824
		{
f2e824
			$error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$image_data[2]][0], $extension);
f2e824
		}
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if ($config['avatar_max_width'] || $config['avatar_max_height'])
f2e824
	{
f2e824
		if ($width > $config['avatar_max_width'] || $height > $config['avatar_max_height'])
f2e824
		{
f2e824
			$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);
f2e824
			return false;
f2e824
		}
f2e824
	}
f2e824
f2e824
	if ($config['avatar_min_width'] || $config['avatar_min_height'])
f2e824
	{
f2e824
		if ($width < $config['avatar_min_width'] || $height < $config['avatar_min_height'])
f2e824
		{
f2e824
			$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);
f2e824
			return false;
f2e824
		}
f2e824
	}
f2e824
f2e824
	return array(AVATAR_REMOTE, $data['remotelink'], $width, $height);
f2e824
}
f2e824
f2e824
/**
f2e824
* Avatar upload using the upload class
f2e824
*/
f2e824
function avatar_upload($data, &$error)
f2e824
{
f2e824
	global $phpbb_root_path, $config, $db, $user, $phpEx;
f2e824
f2e824
	// Init upload class
f2e824
	include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
f2e824
	$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'], explode('|', $config['mime_triggers']));
f2e824
f2e824
	if (!empty($_FILES['uploadfile']['name']))
f2e824
	{
f2e824
		$file = $upload->form_upload('uploadfile');
f2e824
	}
f2e824
	else
f2e824
	{
f2e824
		$file = $upload->remote_upload($data['uploadurl']);
f2e824
	}
f2e824
f2e824
	$prefix = $config['avatar_salt'] . '_';
f2e824
	$file->clean_filename('avatar', $prefix, $data['user_id']);
f2e824
f2e824
	$destination = $config['avatar_path'];
f2e824
f2e824
	// Adjust destination path (no trailing slash)
f2e824
	if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
f2e824
	{
f2e824
		$destination = substr($destination, 0, -1);
f2e824
	}
f2e824
f2e824
	$destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
f2e824
	if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
f2e824
	{
f2e824
		$destination = '';
f2e824
	}
f2e824
f2e824
	// Move file and overwrite any existing image
f2e824
	$file->move_file($destination, true);
f2e824
f2e824
	if (sizeof($file->error))
f2e824
	{
f2e824
		$file->remove();
f2e824
		$error = array_merge($error, $file->error);
f2e824
	}
f2e824
f2e824
	return array(AVATAR_UPLOAD, $data['user_id'] . '_' . time() . '.' . $file->get('extension'), $file->get('width'), $file->get('height'));
f2e824
}
f2e824
f2e824
/**
f2e824
* Generates avatar filename from the database entry
f2e824
*/
f2e824
function get_avatar_filename($avatar_entry)
f2e824
{
f2e824
	global $config;
f2e824
f2e824
f2e824
	if ($avatar_entry[0] === 'g')
f2e824
	{
f2e824
		$avatar_group = true;
f2e824
		$avatar_entry = substr($avatar_entry, 1);
f2e824
	}
f2e824
	else
f2e824
	{
f2e824
		$avatar_group = false;
f2e824
	}
f2e824
	$ext 			= substr(strrchr($avatar_entry, '.'), 1);
f2e824
	$avatar_entry	= intval($avatar_entry);
f2e824
	return $config['avatar_salt'] . '_' . (($avatar_group) ? 'g' : '') . $avatar_entry . '.' . $ext;
f2e824
}
f2e824
f2e824
/**
f2e824
* Avatar Gallery
f2e824
*/
f2e824
function avatar_gallery($category, $avatar_select, $items_per_column, $block_var = 'avatar_row')
f2e824
{
f2e824
	global $user, $cache, $template;
f2e824
	global $config, $phpbb_root_path;
f2e824
f2e824
	$avatar_list = array();
f2e824
f2e824
	$path = $phpbb_root_path . $config['avatar_gallery_path'];
f2e824
f2e824
	if (!file_exists($path) || !is_dir($path))
f2e824
	{
f2e824
		$avatar_list = array($user->lang['NO_AVATAR_CATEGORY'] => array());
f2e824
	}
f2e824
	else
f2e824
	{
f2e824
		// Collect images
f2e824
		$dp = @opendir($path);
f2e824
f2e824
		if (!$dp)
f2e824
		{
f2e824
			return array($user->lang['NO_AVATAR_CATEGORY'] => array());
f2e824
		}
f2e824
f2e824
		while (($file = readdir($dp)) !== false)
f2e824
		{
f2e824
			if ($file[0] != '.' && preg_match('#^[^&"\'<>]+$#i', $file) && is_dir("$path/$file"))
f2e824
			{
f2e824
				$avatar_row_count = $avatar_col_count = 0;
f2e824
f2e824
				if ($dp2 = @opendir("$path/$file"))
f2e824
				{
f2e824
					while (($sub_file = readdir($dp2)) !== false)
f2e824
					{
f2e824
						if (preg_match('#^[^&\'"<>]+\.(?:gif|png|jpe?g)$#i', $sub_file))
f2e824
						{
f2e824
							$avatar_list[$file][$avatar_row_count][$avatar_col_count] = array(
f2e824
								'file'		=> "$file/$sub_file",
f2e824
								'filename'	=> $sub_file,
f2e824
								'name'		=> ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $sub_file))),
f2e824
							);
f2e824
							$avatar_col_count++;
f2e824
							if ($avatar_col_count == $items_per_column)
f2e824
							{
f2e824
								$avatar_row_count++;
f2e824
								$avatar_col_count = 0;
f2e824
							}
f2e824
						}
f2e824
					}
f2e824
					closedir($dp2);
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
		closedir($dp);
f2e824
	}
f2e824
f2e824
	if (!sizeof($avatar_list))
f2e824
	{
f2e824
		$avatar_list = array($user->lang['NO_AVATAR_CATEGORY'] => array());
f2e824
	}
f2e824
f2e824
	@ksort($avatar_list);
f2e824
f2e824
	$category = (!$category) ? key($avatar_list) : $category;
f2e824
	$avatar_categories = array_keys($avatar_list);
f2e824
f2e824
	$s_category_options = '';
f2e824
	foreach ($avatar_categories as $cat)
f2e824
	{
f2e824
		$s_category_options .= '<option value="' . $cat . '"' . (($cat == $category) ? ' selected="selected"' : '') . '>' . $cat . '</option>';
f2e824
	}
f2e824
f2e824
	$template->assign_vars(array(
f2e824
		'S_AVATARS_ENABLED'		=> true,
f2e824
		'S_IN_AVATAR_GALLERY'	=> true,
f2e824
		'S_CAT_OPTIONS'			=> $s_category_options)
f2e824
	);
f2e824
f2e824
	$avatar_list = (isset($avatar_list[$category])) ? $avatar_list[$category] : array();
f2e824
f2e824
	foreach ($avatar_list as $avatar_row_ary)
f2e824
	{
f2e824
		$template->assign_block_vars($block_var, array());
f2e824
f2e824
		foreach ($avatar_row_ary as $avatar_col_ary)
f2e824
		{
f2e824
			$template->assign_block_vars($block_var . '.avatar_column', array(
f2e824
				'AVATAR_IMAGE'	=> $phpbb_root_path . $config['avatar_gallery_path'] . '/' . $avatar_col_ary['file'],
f2e824
				'AVATAR_NAME'	=> $avatar_col_ary['name'],
f2e824
				'AVATAR_FILE'	=> $avatar_col_ary['filename'])
f2e824
			);
f2e824
f2e824
			$template->assign_block_vars($block_var . '.avatar_option_column', array(
f2e824
				'AVATAR_IMAGE'	=> $phpbb_root_path . $config['avatar_gallery_path'] . '/' . $avatar_col_ary['file'],
f2e824
				'S_OPTIONS_AVATAR'	=> $avatar_col_ary['filename'])
f2e824
			);
f2e824
		}
f2e824
	}
f2e824
f2e824
	return $avatar_list;
f2e824
}
f2e824
f2e824
f2e824
/**
f2e824
* Tries to (re-)establish avatar dimensions
f2e824
*/
f2e824
function avatar_get_dimensions($avatar, $avatar_type, &$error, $current_x = 0, $current_y = 0)
f2e824
{
f2e824
	global $config, $phpbb_root_path, $user;
f2e824
f2e824
	switch ($avatar_type)
f2e824
	{
f2e824
		case AVATAR_REMOTE :
f2e824
			break;
f2e824
f2e824
		case AVATAR_UPLOAD :
f2e824
			$avatar = $phpbb_root_path . $config['avatar_path'] . '/' . get_avatar_filename($avatar);
f2e824
			break;
f2e824
f2e824
		case AVATAR_GALLERY :
f2e824
			$avatar = $phpbb_root_path . $config['avatar_gallery_path'] . '/' . $avatar ;
f2e824
			break;
f2e824
	}
f2e824
f2e824
	// Make sure getimagesize works...
f2e824
	if (($image_data = @getimagesize($avatar)) === false)
f2e824
	{
f2e824
		$error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if ($image_data[0] < 2 || $image_data[1] < 2)
f2e824
	{
f2e824
		$error[] = $user->lang['AVATAR_NO_SIZE'];
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// try to maintain ratio
f2e824
	if (!(empty($current_x) && empty($current_y)))
f2e824
	{
f2e824
		if ($current_x != 0)
f2e824
		{
f2e824
			$image_data[1] = (int) floor(($current_x / $image_data[0]) * $image_data[1]);
f2e824
			$image_data[1] = min($config['avatar_max_height'], $image_data[1]);
f2e824
			$image_data[1] = max($config['avatar_min_height'], $image_data[1]);
f2e824
		}
f2e824
		if ($current_y != 0)
f2e824
		{
f2e824
			$image_data[0] = (int) floor(($current_y / $image_data[1]) * $image_data[0]);
f2e824
			$image_data[0] = min($config['avatar_max_width'], $image_data[1]);
f2e824
			$image_data[0] = max($config['avatar_min_width'], $image_data[1]);
f2e824
		}
f2e824
	}
f2e824
	return array($image_data[0], $image_data[1]);
f2e824
}
f2e824
f2e824
/**
f2e824
* Uploading/Changing user avatar
f2e824
*/
f2e824
function avatar_process_user(&$error, $custom_userdata = false)
f2e824
{
f2e824
	global $config, $phpbb_root_path, $auth, $user, $db;
f2e824
f2e824
	$data = array(
f2e824
		'uploadurl'		=> request_var('uploadurl', ''),
f2e824
		'remotelink'	=> request_var('remotelink', ''),
f2e824
		'width'			=> request_var('width', 0),
f2e824
		'height'		=> request_var('height', 0),
f2e824
	);
f2e824
f2e824
	$error = validate_data($data, array(
f2e824
		'uploadurl'		=> array('string', true, 5, 255),
f2e824
		'remotelink'	=> array('string', true, 5, 255),
f2e824
		'width'			=> array('string', true, 1, 3),
f2e824
		'height'		=> array('string', true, 1, 3),
f2e824
	));
f2e824
f2e824
	if (sizeof($error))
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$sql_ary = array();
f2e824
f2e824
	if ($custom_userdata === false)
f2e824
	{
f2e824
		$userdata = &$user->data;
f2e824
	}
f2e824
	else
f2e824
	{
f2e824
		$userdata = &$custom_userdata;
f2e824
	}
f2e824
f2e824
	$data['user_id'] = $userdata['user_id'];
f2e824
	$change_avatar = ($custom_userdata === false) ? $auth->acl_get('u_chgavatar') : true;
f2e824
	$avatar_select = basename(request_var('avatar_select', ''));
f2e824
f2e824
	// Can we upload?
f2e824
	$can_upload = ($config['allow_avatar_upload'] && file_exists($phpbb_root_path . $config['avatar_path']) && @is_writable($phpbb_root_path . $config['avatar_path']) && $change_avatar && (@ini_get('file_uploads') || strtolower(@ini_get('file_uploads')) == 'on')) ? true : false;
f2e824
f2e824
	if ((!empty($_FILES['uploadfile']['name']) || $data['uploadurl']) && $can_upload)
f2e824
	{
f2e824
		list($sql_ary['user_avatar_type'], $sql_ary['user_avatar'], $sql_ary['user_avatar_width'], $sql_ary['user_avatar_height']) = avatar_upload($data, $error);
f2e824
	}
f2e824
	else if ($data['remotelink'] && $change_avatar && $config['allow_avatar_remote'])
f2e824
	{
f2e824
		list($sql_ary['user_avatar_type'], $sql_ary['user_avatar'], $sql_ary['user_avatar_width'], $sql_ary['user_avatar_height']) = avatar_remote($data, $error);
f2e824
	}
f2e824
	else if ($avatar_select && $change_avatar && $config['allow_avatar_local'])
f2e824
	{
f2e824
		$category = basename(request_var('category', ''));
f2e824
f2e824
		$sql_ary['user_avatar_type'] = AVATAR_GALLERY;
f2e824
		$sql_ary['user_avatar'] = $avatar_select;
f2e824
f2e824
		// check avatar gallery
f2e824
		if (!is_dir($phpbb_root_path . $config['avatar_gallery_path'] . '/' . $category))
f2e824
		{
f2e824
			$sql_ary['user_avatar'] = '';
f2e824
			$sql_ary['user_avatar_type'] = $sql_ary['user_avatar_width'] = $sql_ary['user_avatar_height'] = 0;
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			list($sql_ary['user_avatar_width'], $sql_ary['user_avatar_height']) = getimagesize($phpbb_root_path . $config['avatar_gallery_path'] . '/' . $category . '/' . $sql_ary['user_avatar']);
f2e824
			$sql_ary['user_avatar'] = $category . '/' . $sql_ary['user_avatar'];
f2e824
		}
f2e824
	}
f2e824
	else if (isset($_POST['delete']) && $change_avatar)
f2e824
	{
f2e824
		$sql_ary['user_avatar'] = '';
f2e824
		$sql_ary['user_avatar_type'] = $sql_ary['user_avatar_width'] = $sql_ary['user_avatar_height'] = 0;
f2e824
	}
f2e824
	else if (!empty($userdata['user_avatar']))
f2e824
	{
f2e824
		// Only update the dimensions
f2e824
f2e824
		if (empty($data['width']) || empty($data['height']))
f2e824
		{
f2e824
			if ($dims = avatar_get_dimensions($userdata['user_avatar'], $userdata['user_avatar_type'], $error, $data['width'], $data['height']))
f2e824
			{
f2e824
				list($guessed_x, $guessed_y) = $dims;
f2e824
				if (empty($data['width']))
f2e824
				{
f2e824
					$data['width'] = $guessed_x;
f2e824
				}
f2e824
				if (empty($data['height']))
f2e824
				{
f2e824
					$data['height'] = $guessed_y;
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
		if (($config['avatar_max_width'] || $config['avatar_max_height']) &&
f2e824
			(($data['width'] != $userdata['user_avatar_width']) || $data['height'] != $userdata['user_avatar_height']))
f2e824
		{
f2e824
			if ($data['width'] > $config['avatar_max_width'] || $data['height'] > $config['avatar_max_height'])
f2e824
			{
f2e824
				$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']);
f2e824
			}
f2e824
		}
f2e824
f2e824
		if (!sizeof($error))
f2e824
		{
f2e824
			if ($config['avatar_min_width'] || $config['avatar_min_height'])
f2e824
			{
f2e824
				if ($data['width'] < $config['avatar_min_width'] || $data['height'] < $config['avatar_min_height'])
f2e824
				{
f2e824
					$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']);
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		if (!sizeof($error))
f2e824
		{
f2e824
			$sql_ary['user_avatar_width'] = $data['width'];
f2e824
			$sql_ary['user_avatar_height'] = $data['height'];
f2e824
		}
f2e824
	}
f2e824
f2e824
	if (!sizeof($error))
f2e824
	{
f2e824
		// Do we actually have any data to update?
f2e824
		if (sizeof($sql_ary))
f2e824
		{
f2e824
			$ext_new = $ext_old = '';
f2e824
			if (isset($sql_ary['user_avatar']))
f2e824
			{
f2e824
				$userdata = ($custom_userdata === false) ? $user->data : $custom_userdata;
f2e824
				$ext_new = (empty($sql_ary['user_avatar'])) ? '' : substr(strrchr($sql_ary['user_avatar'], '.'), 1);
f2e824
				$ext_old = (empty($userdata['user_avatar'])) ? '' : substr(strrchr($userdata['user_avatar'], '.'), 1);
f2e824
f2e824
				if ($userdata['user_avatar_type'] == AVATAR_UPLOAD)
f2e824
				{
f2e824
					// Delete old avatar if present
f2e824
					if ((!empty($userdata['user_avatar']) && empty($sql_ary['user_avatar']))
f2e824
					   || ( !empty($userdata['user_avatar']) && !empty($sql_ary['user_avatar']) && $ext_new !== $ext_old))
f2e824
					{
f2e824
						avatar_delete('user', $userdata);
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
f2e824
			$sql = 'UPDATE ' . USERS_TABLE . '
f2e824
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
f2e824
				WHERE user_id = ' . (($custom_userdata === false) ? $user->data['user_id'] : $custom_userdata['user_id']);
f2e824
			$db->sql_query($sql);
f2e824
f2e824
		}
f2e824
	}
f2e824
f2e824
	return (sizeof($error)) ? false : true;
f2e824
}
f2e824
f2e824
//
f2e824
// Usergroup functions
f2e824
//
f2e824
f2e824
/**
f2e824
* Add or edit a group. If we're editing a group we only update user
f2e824
* parameters such as rank, etc. if they are changed
f2e824
*/
f2e824
function group_create(&$group_id, $type, $name, $desc, $group_attributes, $allow_desc_bbcode = false, $allow_desc_urls = false, $allow_desc_smilies = false)
f2e824
{
f2e824
	global $phpbb_root_path, $config, $db, $user, $file_upload;
f2e824
f2e824
	$error = array();
f2e824
	$attribute_ary = array(
f2e824
		'group_colour'			=> 'string',
f2e824
		'group_rank'			=> 'int',
f2e824
		'group_avatar'			=> 'string',
f2e824
		'group_avatar_type'		=> 'int',
f2e824
		'group_avatar_width'	=> 'int',
f2e824
		'group_avatar_height'	=> 'int',
f2e824
f2e824
		'group_receive_pm'		=> 'int',
f2e824
		'group_legend'			=> 'int',
f2e824
		'group_message_limit'	=> 'int',
f2e824
		'group_max_recipients'	=> 'int',
f2e824
f2e824
		'group_founder_manage'	=> 'int',
f2e824
	);
f2e824
f2e824
	// Those are group-only attributes
f2e824
	$group_only_ary = array('group_receive_pm', 'group_legend', 'group_message_limit', 'group_max_recipients', 'group_founder_manage');
f2e824
f2e824
	// Check data. Limit group name length.
f2e824
	if (!utf8_strlen($name) || utf8_strlen($name) > 60)
f2e824
	{
f2e824
		$error[] = (!utf8_strlen($name)) ? $user->lang['GROUP_ERR_USERNAME'] : $user->lang['GROUP_ERR_USER_LONG'];
f2e824
	}
f2e824
f2e824
	$err = group_validate_groupname($group_id, $name);
f2e824
	if (!empty($err))
f2e824
	{
f2e824
		$error[] = $user->lang[$err];
f2e824
	}
f2e824
f2e824
	if (!in_array($type, array(GROUP_OPEN, GROUP_CLOSED, GROUP_HIDDEN, GROUP_SPECIAL, GROUP_FREE)))
f2e824
	{
f2e824
		$error[] = $user->lang['GROUP_ERR_TYPE'];
f2e824
	}
f2e824
f2e824
	if (!sizeof($error))
f2e824
	{
f2e824
		$user_ary = array();
f2e824
		$sql_ary = array(
f2e824
			'group_name'			=> (string) $name,
f2e824
			'group_desc'			=> (string) $desc,
f2e824
			'group_desc_uid'		=> '',
f2e824
			'group_desc_bitfield'	=> '',
f2e824
			'group_type'			=> (int) $type,
f2e824
		);
f2e824
f2e824
		// Parse description
f2e824
		if ($desc)
f2e824
		{
f2e824
			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);
f2e824
		}
f2e824
f2e824
		if (sizeof($group_attributes))
f2e824
		{
f2e824
			foreach ($attribute_ary as $attribute => $_type)
f2e824
			{
f2e824
				if (isset($group_attributes[$attribute]))
f2e824
				{
f2e824
					settype($group_attributes[$attribute], $_type);
f2e824
					$sql_ary[$attribute] = $group_attributes[$attribute];
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Setting the log message before we set the group id (if group gets added)
f2e824
		$log = ($group_id) ? 'LOG_GROUP_UPDATED' : 'LOG_GROUP_CREATED';
f2e824
f2e824
		$query = '';
f2e824
f2e824
		if ($group_id)
f2e824
		{
f2e824
			$sql = 'SELECT user_id
f2e824
				FROM ' . USERS_TABLE . '
f2e824
				WHERE group_id = ' . $group_id;
f2e824
			$result = $db->sql_query($sql);
f2e824
f2e824
			while ($row = $db->sql_fetchrow($result))
f2e824
			{
f2e824
				$user_ary[] = $row['user_id'];
f2e824
			}
f2e824
			$db->sql_freeresult($result);
f2e824
f2e824
			if (isset($sql_ary['group_avatar']) && !$sql_ary['group_avatar'])
f2e824
			{
f2e824
				remove_default_avatar($group_id, $user_ary);
f2e824
			}
f2e824
			if (isset($sql_ary['group_rank']) && !$sql_ary['group_rank'])
f2e824
			{
f2e824
				remove_default_rank($group_id, $user_ary);
f2e824
			}
f2e824
f2e824
			$sql = 'UPDATE ' . GROUPS_TABLE . '
f2e824
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
f2e824
				WHERE group_id = $group_id";
f2e824
			$db->sql_query($sql);
f2e824
f2e824
			// Since we may update the name too, we need to do this on other tables too...
f2e824
			$sql = 'UPDATE ' . MODERATOR_CACHE_TABLE . "
f2e824
				SET group_name = '" . $db->sql_escape($sql_ary['group_name']) . "'
f2e824
				WHERE group_id = $group_id";
f2e824
			$db->sql_query($sql);
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$sql = 'INSERT INTO ' . GROUPS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
f2e824
			$db->sql_query($sql);
f2e824
		}
f2e824
f2e824
		if (!$group_id)
f2e824
		{
f2e824
			$group_id = $db->sql_nextid();
f2e824
			if (isset($sql_ary['group_avatar_type']) && $sql_ary['group_avatar_type'] == AVATAR_UPLOAD)
f2e824
			{
f2e824
				group_correct_avatar($group_id, $sql_ary['group_avatar']);
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Set user attributes
f2e824
		$sql_ary = array();
f2e824
		if (sizeof($group_attributes))
f2e824
		{
f2e824
			foreach ($attribute_ary as $attribute => $_type)
f2e824
			{
f2e824
				if (isset($group_attributes[$attribute]) && !in_array($attribute, $group_only_ary))
f2e824
				{
f2e824
					// If we are about to set an avatar, we will not overwrite user avatars if no group avatar is set...
f2e824
					if (strpos($attribute, 'group_avatar') === 0 && !$group_attributes[$attribute])
f2e824
					{
f2e824
						continue;
f2e824
					}
f2e824
f2e824
					$sql_ary[$attribute] = $group_attributes[$attribute];
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		if (sizeof($sql_ary) && sizeof($user_ary))
f2e824
		{
f2e824
			group_set_user_default($group_id, $user_ary, $sql_ary);
f2e824
		}
f2e824
f2e824
		$name = ($type == GROUP_SPECIAL) ? $user->lang['G_' . $name] : $name;
f2e824
		add_log('admin', $log, $name);
f2e824
f2e824
		group_update_listings($group_id);
f2e824
	}
f2e824
f2e824
	return (sizeof($error)) ? $error : false;
f2e824
}
f2e824
f2e824
f2e824
/**
f2e824
* Changes a group avatar's filename to conform to the naming scheme
f2e824
*/
f2e824
function group_correct_avatar($group_id, $old_entry)
f2e824
{
f2e824
	global $config, $db, $phpbb_root_path;
f2e824
f2e824
	$group_id		= (int)$group_id;
f2e824
	$ext 			= substr(strrchr($old_entry, '.'), 1);
f2e824
	$old_filename 	= get_avatar_filename($old_entry);
f2e824
	$new_filename 	= $config['avatar_salt'] . "_g$group_id.$ext";
f2e824
	$new_entry 		= 'g' . $group_id . '_' . substr(time(), -5) . ".$ext";
f2e824
f2e824
	$avatar_path = $phpbb_root_path . $config['avatar_path'];
f2e824
	if (@rename($avatar_path . '/'. $old_filename, $avatar_path . '/' . $new_filename))
f2e824
	{
f2e824
		$sql = 'UPDATE ' . GROUPS_TABLE . '
f2e824
			SET group_avatar = \'' . $db->sql_escape($new_entry) . "'
f2e824
			WHERE group_id = $group_id";
f2e824
		$db->sql_query($sql);
f2e824
	}
f2e824
}
f2e824
f2e824
f2e824
/**
f2e824
* Remove avatar also for users not having the group as default
f2e824
*/
f2e824
function avatar_remove_db($avatar_name)
f2e824
{
f2e824
	global $config, $db;
f2e824
f2e824
	$sql = 'UPDATE ' . USERS_TABLE . "
f2e824
		SET user_avatar = '',
f2e824
		user_avatar_type = 0
f2e824
		WHERE user_avatar = '" . $db->sql_escape($avatar_name) . '\'';
f2e824
	$db->sql_query($sql);
f2e824
}
f2e824
f2e824
f2e824
/**
f2e824
* Group Delete
f2e824
*/
f2e824
function group_delete($group_id, $group_name = false)
f2e824
{
f2e824
	global $db, $phpbb_root_path, $phpEx;
f2e824
f2e824
	if (!$group_name)
f2e824
	{
f2e824
		$group_name = get_group_name($group_id);
f2e824
	}
f2e824
f2e824
	$start = 0;
f2e824
f2e824
	do
f2e824
	{
f2e824
		$user_id_ary = $username_ary = array();
f2e824
f2e824
		// Batch query for group members, call group_user_del
f2e824
		$sql = 'SELECT u.user_id, u.username
f2e824
			FROM ' . USER_GROUP_TABLE . ' ug, ' . USERS_TABLE . " u
f2e824
			WHERE ug.group_id = $group_id
f2e824
				AND u.user_id = ug.user_id";
f2e824
		$result = $db->sql_query_limit($sql, 200, $start);
f2e824
f2e824
		if ($row = $db->sql_fetchrow($result))
f2e824
		{
f2e824
			do
f2e824
			{
f2e824
				$user_id_ary[] = $row['user_id'];
f2e824
				$username_ary[] = $row['username'];
f2e824
f2e824
				$start++;
f2e824
			}
f2e824
			while ($row = $db->sql_fetchrow($result));
f2e824
f2e824
			group_user_del($group_id, $user_id_ary, $username_ary, $group_name);
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$start = 0;
f2e824
		}
f2e824
		$db->sql_freeresult($result);
f2e824
	}
f2e824
	while ($start);
f2e824
f2e824
	// Delete group
f2e824
	$sql = 'DELETE FROM ' . GROUPS_TABLE . "
f2e824
		WHERE group_id = $group_id";
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	// Delete auth entries from the groups table
f2e824
	$sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . "
f2e824
		WHERE group_id = $group_id";
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	// Re-cache moderators
f2e824
	if (!function_exists('cache_moderators'))
f2e824
	{
f2e824
		include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
f2e824
	}
f2e824
f2e824
	cache_moderators();
f2e824
f2e824
	add_log('admin', 'LOG_GROUP_DELETE', $group_name);
f2e824
f2e824
	// Return false - no error
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Add user(s) to group
f2e824
*
f2e824
* @return mixed false if no errors occurred, else the user lang string for the relevant error, for example 'NO_USER'
f2e824
*/
f2e824
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)
f2e824
{
f2e824
	global $db, $auth;
f2e824
f2e824
	// We need both username and user_id info
f2e824
	$result = user_get_id_name($user_id_ary, $username_ary);
f2e824
f2e824
	if (!sizeof($user_id_ary) || $result !== false)
f2e824
	{
f2e824
		return 'NO_USER';
f2e824
	}
f2e824
f2e824
	// Remove users who are already members of this group
f2e824
	$sql = 'SELECT user_id, group_leader
f2e824
		FROM ' . USER_GROUP_TABLE . '
f2e824
		WHERE ' . $db->sql_in_set('user_id', $user_id_ary) . "
f2e824
			AND group_id = $group_id";
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	$add_id_ary = $update_id_ary = array();
f2e824
	while ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$add_id_ary[] = (int) $row['user_id'];
f2e824
f2e824
		if ($leader && !$row['group_leader'])
f2e824
		{
f2e824
			$update_id_ary[] = (int) $row['user_id'];
f2e824
		}
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	// Do all the users exist in this group?
f2e824
	$add_id_ary = array_diff($user_id_ary, $add_id_ary);
f2e824
f2e824
	// If we have no users
f2e824
	if (!sizeof($add_id_ary) && !sizeof($update_id_ary))
f2e824
	{
f2e824
		return 'GROUP_USERS_EXIST';
f2e824
	}
f2e824
f2e824
	$db->sql_transaction('begin');
f2e824
f2e824
	// Insert the new users
f2e824
	if (sizeof($add_id_ary))
f2e824
	{
f2e824
		$sql_ary = array();
f2e824
f2e824
		foreach ($add_id_ary as $user_id)
f2e824
		{
f2e824
			$sql_ary[] = array(
f2e824
				'user_id'		=> (int) $user_id,
f2e824
				'group_id'		=> (int) $group_id,
f2e824
				'group_leader'	=> (int) $leader,
f2e824
				'user_pending'	=> (int) $pending,
f2e824
			);
f2e824
		}
f2e824
f2e824
		$db->sql_multi_insert(USER_GROUP_TABLE, $sql_ary);
f2e824
	}
f2e824
f2e824
	if (sizeof($update_id_ary))
f2e824
	{
f2e824
		$sql = 'UPDATE ' . USER_GROUP_TABLE . '
f2e824
			SET group_leader = 1
f2e824
			WHERE ' . $db->sql_in_set('user_id', $update_id_ary) . "
f2e824
				AND group_id = $group_id";
f2e824
		$db->sql_query($sql);
f2e824
	}
f2e824
f2e824
	if ($default)
f2e824
	{
f2e824
		group_set_user_default($group_id, $user_id_ary, $group_attributes);
f2e824
	}
f2e824
f2e824
	$db->sql_transaction('commit');
f2e824
f2e824
	// Clear permissions cache of relevant users
f2e824
	$auth->acl_clear_prefetch($user_id_ary);
f2e824
f2e824
	if (!$group_name)
f2e824
	{
f2e824
		$group_name = get_group_name($group_id);
f2e824
	}
f2e824
f2e824
	$log = ($leader) ? 'LOG_MODS_ADDED' : 'LOG_USERS_ADDED';
f2e824
f2e824
	add_log('admin', $log, $group_name, implode(', ', $username_ary));
f2e824
f2e824
	group_update_listings($group_id);
f2e824
f2e824
	// Return false - no error
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Remove a user/s from a given group. When we remove users we update their
f2e824
* default group_id. We do this by examining which "special" groups they belong
f2e824
* to. The selection is made based on a reasonable priority system
f2e824
*
f2e824
* @return false if no errors occurred, else the user lang string for the relevant error, for example 'NO_USER'
f2e824
*/
f2e824
function group_user_del($group_id, $user_id_ary = false, $username_ary = false, $group_name = false)
f2e824
{
f2e824
	global $db, $auth;
f2e824
f2e824
	$group_order = array('ADMINISTRATORS', 'GLOBAL_MODERATORS', 'REGISTERED_COPPA', 'REGISTERED', 'BOTS', 'GUESTS');
f2e824
f2e824
	// We need both username and user_id info
f2e824
	$result = user_get_id_name($user_id_ary, $username_ary);
f2e824
f2e824
	if (!sizeof($user_id_ary) || $result !== false)
f2e824
	{
f2e824
		return 'NO_USER';
f2e824
	}
f2e824
f2e824
	$sql = 'SELECT *
f2e824
		FROM ' . GROUPS_TABLE . '
f2e824
		WHERE ' . $db->sql_in_set('group_name', $group_order);
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	$group_order_id = $special_group_data = array();
f2e824
	while ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$group_order_id[$row['group_name']] = $row['group_id'];
f2e824
f2e824
		$special_group_data[$row['group_id']] = array(
f2e824
			'group_colour'			=> $row['group_colour'],
f2e824
			'group_rank'				=> $row['group_rank'],
f2e824
		);
f2e824
f2e824
		// Only set the group avatar if one is defined...
f2e824
		if ($row['group_avatar'])
f2e824
		{
f2e824
			$special_group_data[$row['group_id']] = array_merge($special_group_data[$row['group_id']], array(
f2e824
				'group_avatar'			=> $row['group_avatar'],
f2e824
				'group_avatar_type'		=> $row['group_avatar_type'],
f2e824
				'group_avatar_width'		=> $row['group_avatar_width'],
f2e824
				'group_avatar_height'	=> $row['group_avatar_height'])
f2e824
			);
f2e824
		}
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	// 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
f2e824
	$sql = 'SELECT user_id, group_id
f2e824
		FROM ' . USERS_TABLE . '
f2e824
		WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	$default_groups = array();
f2e824
	while ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$default_groups[$row['user_id']] = $row['group_id'];
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	// What special group memberships exist for these users?
f2e824
	$sql = 'SELECT g.group_id, g.group_name, ug.user_id
f2e824
		FROM ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g
f2e824
		WHERE ' . $db->sql_in_set('ug.user_id', $user_id_ary) . "
f2e824
			AND g.group_id = ug.group_id
f2e824
			AND g.group_id <> $group_id
f2e824
			AND g.group_type = " . GROUP_SPECIAL . '
f2e824
		ORDER BY ug.user_id, g.group_id';
f2e824
	$result = $db->sql_query($sql);
f2e824
f2e824
	$temp_ary = array();
f2e824
	while ($row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		if ($default_groups[$row['user_id']] == $group_id && (!isset($temp_ary[$row['user_id']]) || array_search($row['group_name'], $group_order) < $temp_ary[$row['user_id']]))
f2e824
		{
f2e824
			$temp_ary[$row['user_id']] = $row['group_id'];
f2e824
		}
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	$sql_where_ary = array();
f2e824
	foreach ($temp_ary as $uid => $gid)
f2e824
	{
f2e824
		$sql_where_ary[$gid][] = $uid;
f2e824
	}
f2e824
	unset($temp_ary);
f2e824
f2e824
	foreach ($special_group_data as $gid => $default_data_ary)
f2e824
	{
f2e824
		if (isset($sql_where_ary[$gid]) && sizeof($sql_where_ary[$gid]))
f2e824
		{
f2e824
			remove_default_rank($group_id, $sql_where_ary[$gid]);
f2e824
			remove_default_avatar($group_id, $sql_where_ary[$gid]);
f2e824
			group_set_user_default($gid, $sql_where_ary[$gid], $default_data_ary);
f2e824
		}
f2e824
	}
f2e824
	unset($special_group_data);
f2e824
f2e824
	$sql = 'DELETE FROM ' . USER_GROUP_TABLE . "
f2e824
		WHERE group_id = $group_id
f2e824
			AND " . $db->sql_in_set('user_id', $user_id_ary);
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	// Clear permissions cache of relevant users
f2e824
	$auth->acl_clear_prefetch($user_id_ary);
f2e824
f2e824
	if (!$group_name)
f2e824
	{
f2e824
		$group_name = get_group_name($group_id);
f2e824
	}
f2e824
f2e824
	$log = 'LOG_GROUP_REMOVE';
f2e824
f2e824
	add_log('admin', $log, $group_name, implode(', ', $username_ary));
f2e824
f2e824
	group_update_listings($group_id);
f2e824
f2e824
	// Return false - no error
f2e824
	return false;
f2e824
}
f2e824
f2e824
f2e824
/**
f2e824
* Removes the group avatar of the default group from the users in user_ids who have that group as default.
f2e824
*/
f2e824
function remove_default_avatar($group_id, $user_ids)
f2e824
{
f2e824
	global $db;
f2e824
f2e824
	if (!is_array($user_ids))
f2e824
	{
f2e824
		$user_ids = array($user_ids);
f2e824
	}
f2e824
	if (empty($user_ids))
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$user_ids = array_map('intval', $user_ids);
f2e824
f2e824
	$sql = 'SELECT *
f2e824
		FROM ' . GROUPS_TABLE . '
f2e824
		WHERE group_id = ' . (int)$group_id;
f2e824
	$result = $db->sql_query($sql);
f2e824
	if (!$row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$db->sql_freeresult($result);
f2e824
		return false;
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	$sql = 'UPDATE ' . USERS_TABLE . "
f2e824
		SET user_avatar = '',
f2e824
			user_avatar_type = 0,
f2e824
			user_avatar_width = 0,
f2e824
			user_avatar_height = 0
f2e824
		WHERE group_id = " . (int) $group_id . "
f2e824
		AND user_avatar = '" . $db->sql_escape($row['group_avatar']) . "'
f2e824
		AND " . $db->sql_in_set('user_id', $user_ids);
f2e824
f2e824
	$db->sql_query($sql);
f2e824
}
f2e824
f2e824
/**
f2e824
* Removes the group rank of the default group from the users in user_ids who have that group as default.
f2e824
*/
f2e824
function remove_default_rank($group_id, $user_ids)
f2e824
{
f2e824
	global $db;
f2e824
f2e824
	if (!is_array($user_ids))
f2e824
	{
f2e824
		$user_ids = array($user_ids);
f2e824
	}
f2e824
	if (empty($user_ids))
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$user_ids = array_map('intval', $user_ids);
f2e824
f2e824
	$sql = 'SELECT *
f2e824
		FROM ' . GROUPS_TABLE . '
f2e824
		WHERE group_id = ' . (int)$group_id;
f2e824
	$result = $db->sql_query($sql);
f2e824
	if (!$row = $db->sql_fetchrow($result))
f2e824
	{
f2e824
		$db->sql_freeresult($result);
f2e824
		return false;
f2e824
	}
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	$sql = 'UPDATE ' . USERS_TABLE . '
f2e824
		SET user_rank = 0
f2e824
		WHERE group_id = ' . (int)$group_id . '
f2e824
		AND user_rank <> 0
f2e824
		AND user_rank = ' . (int)$row['group_rank'] . '
f2e824
		AND ' . $db->sql_in_set('user_id', $user_ids);
f2e824
	$db->sql_query($sql);
f2e824
}
f2e824
f2e824
/**
f2e824
* This is used to promote (to leader), demote or set as default a member/s
f2e824
*/
f2e824
function group_user_attributes($action, $group_id, $user_id_ary = false, $username_ary = false, $group_name = false, $group_attributes = false)
f2e824
{
f2e824
	global $db, $auth, $phpbb_root_path, $phpEx, $config;
f2e824
f2e824
	// We need both username and user_id info
f2e824
	$result = user_get_id_name($user_id_ary, $username_ary);
f2e824
f2e824
	if (!sizeof($user_id_ary) || $result !== false)
f2e824
	{
f2e824
		return 'NO_USERS';
f2e824
	}
f2e824
f2e824
	if (!$group_name)
f2e824
	{
f2e824
		$group_name = get_group_name($group_id);
f2e824
	}
f2e824
f2e824
	switch ($action)
f2e824
	{
f2e824
		case 'demote':
f2e824
		case 'promote':
f2e824
f2e824
			$sql = 'SELECT user_id FROM ' . USER_GROUP_TABLE . "
f2e824
				WHERE group_id = $group_id
f2e824
					AND user_pending = 1
f2e824
					AND " . $db->sql_in_set('user_id', $user_id_ary);
f2e824
			$result = $db->sql_query_limit($sql, 1);
f2e824
			$not_empty = ($db->sql_fetchrow($result));
f2e824
			$db->sql_freeresult($result);
f2e824
			if ($not_empty)
f2e824
			{
f2e824
				return 'NO_VALID_USERS';
f2e824
			}
f2e824
f2e824
			$sql = 'UPDATE ' . USER_GROUP_TABLE . '
f2e824
				SET group_leader = ' . (($action == 'promote') ? 1 : 0) . "
f2e824
				WHERE group_id = $group_id
f2e824
					AND user_pending = 0
f2e824
					AND " . $db->sql_in_set('user_id', $user_id_ary);
f2e824
			$db->sql_query($sql);
f2e824
f2e824
			$log = ($action == 'promote') ? 'LOG_GROUP_PROMOTED' : 'LOG_GROUP_DEMOTED';
f2e824
		break;
f2e824
f2e824
		case 'approve':
f2e824
			// Make sure we only approve those which are pending ;)
f2e824
			$sql = 'SELECT u.user_id, u.user_email, u.username, u.username_clean, u.user_notify_type, u.user_jabber, u.user_lang
f2e824
				FROM ' . USERS_TABLE . ' u, ' . USER_GROUP_TABLE . ' ug
f2e824
				WHERE ug.group_id = ' . $group_id . '
f2e824
					AND ug.user_pending = 1
f2e824
					AND ug.user_id = u.user_id
f2e824
					AND ' . $db->sql_in_set('ug.user_id', $user_id_ary);
f2e824
			$result = $db->sql_query($sql);
f2e824
f2e824
			$user_id_ary = $email_users = array();
f2e824
			while ($row = $db->sql_fetchrow($result))
f2e824
			{
f2e824
				$user_id_ary[] = $row['user_id'];
f2e824
				$email_users[] = $row;
f2e824
			}
f2e824
			$db->sql_freeresult($result);
f2e824
f2e824
			if (!sizeof($user_id_ary))
f2e824
			{
f2e824
				return false;
f2e824
			}
f2e824
f2e824
			$sql = 'UPDATE ' . USER_GROUP_TABLE . "
f2e824
				SET user_pending = 0
f2e824
				WHERE group_id = $group_id
f2e824
					AND " . $db->sql_in_set('user_id', $user_id_ary);
f2e824
			$db->sql_query($sql);
f2e824
f2e824
			// Send approved email to users...
f2e824
			include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
f2e824
			$messenger = new messenger();
f2e824
f2e824
			foreach ($email_users as $row)
f2e824
			{
f2e824
				$messenger->template('group_approved', $row['user_lang']);
f2e824
f2e824
				$messenger->to($row['user_email'], $row['username']);
f2e824
				$messenger->im($row['user_jabber'], $row['username']);
f2e824
f2e824
				$messenger->assign_vars(array(
f2e824
					'USERNAME'		=> htmlspecialchars_decode($row['username']),
f2e824
					'GROUP_NAME'	=> htmlspecialchars_decode($group_name),
f2e824
					'U_GROUP'		=> generate_board_url() . "/ucp.$phpEx?i=groups&mode=membership")
f2e824
				);
f2e824
f2e824
				$messenger->send($row['user_notify_type']);
f2e824
			}
f2e824
f2e824
			$messenger->save_queue();
f2e824
f2e824
			$log = 'LOG_USERS_APPROVED';
f2e824
		break;
f2e824
f2e824
		case 'default':
f2e824
			$sql = 'SELECT user_id, group_id FROM ' . USERS_TABLE . '
f2e824
				WHERE ' . $db->sql_in_set('user_id', $user_id_ary, false, true);
f2e824
			$result = $db->sql_query($sql);
f2e824
f2e824
			$groups = array();
f2e824
			while ($row = $db->sql_fetchrow($result))
f2e824
			{
f2e824
				if (!isset($groups[$row['group_id']]))
f2e824
				{
f2e824
					$groups[$row['group_id']] = array();
f2e824
				}
f2e824
				$groups[$row['group_id']][] = $row['user_id'];
f2e824
			}
f2e824
			$db->sql_freeresult($result);
f2e824
f2e824
			foreach ($groups as $gid => $uids)
f2e824
			{
f2e824
				remove_default_rank($gid, $uids);
f2e824
				remove_default_avatar($gid, $uids);
f2e824
			}
f2e824
			group_set_user_default($group_id, $user_id_ary, $group_attributes);
f2e824
			$log = 'LOG_GROUP_DEFAULTS';
f2e824
		break;
f2e824
	}
f2e824
f2e824
	// Clear permissions cache of relevant users
f2e824
	$auth->acl_clear_prefetch($user_id_ary);
f2e824
f2e824
	add_log('admin', $log, $group_name, implode(', ', $username_ary));
f2e824
f2e824
	group_update_listings($group_id);
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* A small version of validate_username to check for a group name's existence. To be called directly.
f2e824
*/
f2e824
function group_validate_groupname($group_id, $group_name)
f2e824
{
f2e824
	global $config, $db;
f2e824
f2e824
	$group_name =  utf8_clean_string($group_name);
f2e824
f2e824
	if (!empty($group_id))
f2e824
	{
f2e824
		$sql = 'SELECT group_name
f2e824
			FROM ' . GROUPS_TABLE . '
f2e824
			WHERE group_id = ' . (int) $group_id;
f2e824
		$result = $db->sql_query($sql);
f2e824
		$row = $db->sql_fetchrow($result);
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		if (!$row)
f2e824
		{
f2e824
			return false;
f2e824
		}
f2e824
f2e824
		$allowed_groupname = utf8_clean_string($row['group_name']);
f2e824
f2e824
		if ($allowed_groupname == $group_name)
f2e824
		{
f2e824
			return false;
f2e824
		}
f2e824
	}
f2e824
f2e824
	$sql = 'SELECT group_name
f2e824
		FROM ' . GROUPS_TABLE . "
f2e824
		WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($group_name)) . "'";
f2e824
	$result = $db->sql_query($sql);
f2e824
	$row = $db->sql_fetchrow($result);
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if ($row)
f2e824
	{
f2e824
		return 'GROUP_NAME_TAKEN';
f2e824
	}
f2e824
f2e824
	return false;
f2e824
}
f2e824
f2e824
/**
f2e824
* Set users default group
f2e824
*
f2e824
* @access private
f2e824
*/
f2e824
function group_set_user_default($group_id, $user_id_ary, $group_attributes = false, $update_listing = false)
f2e824
{
f2e824
	global $db;
f2e824
f2e824
	if (empty($user_id_ary))
f2e824
	{
f2e824
		return;
f2e824
	}
f2e824
f2e824
	$attribute_ary = array(
f2e824
		'group_colour'			=> 'string',
f2e824
		'group_rank'			=> 'int',
f2e824
		'group_avatar'			=> 'string',
f2e824
		'group_avatar_type'		=> 'int',
f2e824
		'group_avatar_width'	=> 'int',
f2e824
		'group_avatar_height'	=> 'int',
f2e824
	);
f2e824
f2e824
	$sql_ary = array(
f2e824
		'group_id'		=> $group_id
f2e824
	);
f2e824
f2e824
	// Were group attributes passed to the function? If not we need to obtain them
f2e824
	if ($group_attributes === false)
f2e824
	{
f2e824
		$sql = 'SELECT ' . implode(', ', array_keys($attribute_ary)) . '
f2e824
			FROM ' . GROUPS_TABLE . "
f2e824
			WHERE group_id = $group_id";
f2e824
		$result = $db->sql_query($sql);
f2e824
		$group_attributes = $db->sql_fetchrow($result);
f2e824
		$db->sql_freeresult($result);
f2e824
	}
f2e824
f2e824
	foreach ($attribute_ary as $attribute => $type)
f2e824
	{
f2e824
		if (isset($group_attributes[$attribute]))
f2e824
		{
f2e824
			// 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
f2e824
			if ((strpos($attribute, 'group_avatar') === 0 || strpos($attribute, 'group_rank') === 0) && !$group_attributes[$attribute])
f2e824
			{
f2e824
				continue;
f2e824
			}
f2e824
f2e824
			settype($group_attributes[$attribute], $type);
f2e824
			$sql_ary[str_replace('group_', 'user_', $attribute)] = $group_attributes[$attribute];
f2e824
		}
f2e824
	}
f2e824
f2e824
	// Before we update the user attributes, we will make a list of those having now the group avatar assigned
f2e824
	if (isset($sql_ary['user_avatar']))
f2e824
	{
f2e824
		// Ok, get the original avatar data from users having an uploaded one (we need to remove these from the filesystem)
f2e824
		$sql = 'SELECT user_id, group_id, user_avatar
f2e824
			FROM ' . USERS_TABLE . '
f2e824
			WHERE ' . $db->sql_in_set('user_id', $user_id_ary) . '
f2e824
				AND user_avatar_type = ' . AVATAR_UPLOAD;
f2e824
		$result = $db->sql_query($sql);
f2e824
f2e824
		while ($row = $db->sql_fetchrow($result))
f2e824
		{
f2e824
			avatar_delete('user', $row);
f2e824
		}
f2e824
		$db->sql_freeresult($result);
f2e824
	}
f2e824
	else
f2e824
	{
f2e824
		unset($sql_ary['user_avatar_type']);
f2e824
		unset($sql_ary['user_avatar_height']);
f2e824
		unset($sql_ary['user_avatar_width']);
f2e824
	}
f2e824
f2e824
	$sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
f2e824
		WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
f2e824
	$db->sql_query($sql);
f2e824
f2e824
	if (isset($sql_ary['user_colour']))
f2e824
	{
f2e824
		// Update any cached colour information for these users
f2e824
		$sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
f2e824
			WHERE " . $db->sql_in_set('forum_last_poster_id', $user_id_ary);
f2e824
		$db->sql_query($sql);
f2e824
f2e824
		$sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_first_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
f2e824
			WHERE " . $db->sql_in_set('topic_poster', $user_id_ary);
f2e824
		$db->sql_query($sql);
f2e824
f2e824
		$sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
f2e824
			WHERE " . $db->sql_in_set('topic_last_poster_id', $user_id_ary);
f2e824
		$db->sql_query($sql);
f2e824
f2e824
		global $config;
f2e824
f2e824
		if (in_array($config['newest_user_id'], $user_id_ary))
f2e824
		{
f2e824
			set_config('newest_user_colour', $sql_ary['user_colour'], true);
f2e824
		}
f2e824
	}
f2e824
f2e824
	if ($update_listing)
f2e824
	{
f2e824
		group_update_listings($group_id);
f2e824
	}
f2e824
}
f2e824
f2e824
/**
f2e824
* Get group name
f2e824
*/
f2e824
function get_group_name($group_id)
f2e824
{
f2e824
	global $db, $user;
f2e824
f2e824
	$sql = 'SELECT group_name, group_type
f2e824
		FROM ' . GROUPS_TABLE . '
f2e824
		WHERE group_id = ' . (int) $group_id;
f2e824
	$result = $db->sql_query($sql);
f2e824
	$row = $db->sql_fetchrow($result);
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	if (!$row)
f2e824
	{
f2e824
		return '';
f2e824
	}
f2e824
f2e824
	return ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
f2e824
}
f2e824
f2e824
/**
f2e824
* Obtain either the members of a specified group, the groups the specified user is subscribed to
f2e824
* or checking if a specified user is in a specified group. This function does not return pending memberships.
f2e824
*
f2e824
* Note: Never use this more than once... first group your users/groups
f2e824
*/
f2e824
function group_memberships($group_id_ary = false, $user_id_ary = false, $return_bool = false)
f2e824
{
f2e824
	global $db;
f2e824
f2e824
	if (!$group_id_ary && !$user_id_ary)
f2e824
	{
f2e824
		return true;
f2e824
	}
f2e824
f2e824
	if ($user_id_ary)
f2e824
	{
f2e824
		$user_id_ary = (!is_array($user_id_ary)) ? array($user_id_ary) : $user_id_ary;
f2e824
	}
f2e824
f2e824
	if ($group_id_ary)
f2e824
	{
f2e824
		$group_id_ary = (!is_array($group_id_ary)) ? array($group_id_ary) : $group_id_ary;
f2e824
	}
f2e824
f2e824
	$sql = 'SELECT ug.*, u.username, u.username_clean, u.user_email
f2e824
		FROM ' . USER_GROUP_TABLE . ' ug, ' . USERS_TABLE . ' u
f2e824
		WHERE ug.user_id = u.user_id
f2e824
			AND ug.user_pending = 0 AND ';
f2e824
f2e824
	if ($group_id_ary)
f2e824
	{
f2e824
		$sql .= ' ' . $db->sql_in_set('ug.group_id', $group_id_ary);
f2e824
	}
f2e824
f2e824
	if ($user_id_ary)
f2e824
	{
f2e824
		$sql .= ($group_id_ary) ? ' AND ' : ' ';
f2e824
		$sql .= $db->sql_in_set('ug.user_id', $user_id_ary);
f2e824
	}
f2e824
f2e824
	$result = ($return_bool) ? $db->sql_query_limit($sql, 1) : $db->sql_query($sql);
f2e824
f2e824
	$row = $db->sql_fetchrow($result);
f2e824
f2e824
	if ($return_bool)
f2e824
	{
f2e824
		$db->sql_freeresult($result);
f2e824
		return ($row) ? true : false;
f2e824
	}
f2e824
f2e824
	if (!$row)
f2e824
	{
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$return = array();
f2e824
f2e824
	do
f2e824
	{
f2e824
		$return[] = $row;
f2e824
	}
f2e824
	while ($row = $db->sql_fetchrow($result));
f2e824
f2e824
	$db->sql_freeresult($result);
f2e824
f2e824
	return $return;
f2e824
}
f2e824
f2e824
/**
f2e824
* Re-cache moderators and foes if group has a_ or m_ permissions
f2e824
*/
f2e824
function group_update_listings($group_id)
f2e824
{
f2e824
	global $auth;
f2e824
f2e824
	$hold_ary = $auth->acl_group_raw_data($group_id, array('a_', 'm_'));
f2e824
f2e824
	if (!sizeof($hold_ary))
f2e824
	{
f2e824
		return;
f2e824
	}
f2e824
f2e824
	$mod_permissions = $admin_permissions = false;
f2e824
f2e824
	foreach ($hold_ary as $g_id => $forum_ary)
f2e824
	{
f2e824
		foreach ($forum_ary as $forum_id => $auth_ary)
f2e824
		{
f2e824
			foreach ($auth_ary as $auth_option => $setting)
f2e824
			{
f2e824
				if ($mod_permissions && $admin_permissions)
f2e824
				{
f2e824
					break 3;
f2e824
				}
f2e824
f2e824
				if ($setting != ACL_YES)
f2e824
				{
f2e824
					continue;
f2e824
				}
f2e824
f2e824
				if ($auth_option == 'm_')
f2e824
				{
f2e824
					$mod_permissions = true;
f2e824
				}
f2e824
f2e824
				if ($auth_option == 'a_')
f2e824
				{
f2e824
					$admin_permissions = true;
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
	}
f2e824
f2e824
	if ($mod_permissions)
f2e824
	{
f2e824
		if (!function_exists('cache_moderators'))
f2e824
		{
f2e824
			global $phpbb_root_path, $phpEx;
f2e824
			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
f2e824
		}
f2e824
		cache_moderators();
f2e824
	}
f2e824
f2e824
	if ($mod_permissions || $admin_permissions)
f2e824
	{
f2e824
		if (!function_exists('update_foes'))
f2e824
		{
f2e824
			global $phpbb_root_path, $phpEx;
f2e824
			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
f2e824
		}
f2e824
		update_foes(array($group_id));
f2e824
	}
f2e824
}
f2e824
f2e824
?>