Blame Extras/phpBB/3.0.4/includes/functions_user.php

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