/** * Set users default group * * @access private */ function group_set_user_default($group_id, $user_id_ary, $group_attributes = false, $update_listing = false) { global $phpbb_container, $db, $phpbb_dispatcher; if (empty($user_id_ary)) { return; } $attribute_ary = array('group_colour' => 'string', 'group_rank' => 'int', 'group_avatar' => 'string', 'group_avatar_type' => 'string', 'group_avatar_width' => 'int', 'group_avatar_height' => 'int'); $sql_ary = array('group_id' => $group_id); // Were group attributes passed to the function? If not we need to obtain them if ($group_attributes === false) { $sql = 'SELECT ' . implode(', ', array_keys($attribute_ary)) . ' FROM ' . GROUPS_TABLE . "\n\t\t\tWHERE group_id = {$group_id}"; $result = $db->sql_query($sql); $group_attributes = $db->sql_fetchrow($result); $db->sql_freeresult($result); } foreach ($attribute_ary as $attribute => $type) { if (isset($group_attributes[$attribute])) { // 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 if ((strpos($attribute, 'group_avatar') === 0 || strpos($attribute, 'group_rank') === 0) && !$group_attributes[$attribute]) { continue; } settype($group_attributes[$attribute], $type); $sql_ary[str_replace('group_', 'user_', $attribute)] = $group_attributes[$attribute]; } } $updated_sql_ary = $sql_ary; // Before we update the user attributes, we will update the rank for users that don't have a custom rank if (isset($sql_ary['user_rank'])) { $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array('user_rank' => $sql_ary['user_rank'])) . ' WHERE user_rank = 0 AND ' . $db->sql_in_set('user_id', $user_id_ary); $db->sql_query($sql); unset($sql_ary['user_rank']); } // Before we update the user attributes, we will update the avatar for users that don't have a custom avatar $avatar_options = array('user_avatar', 'user_avatar_type', 'user_avatar_height', 'user_avatar_width'); if (isset($sql_ary['user_avatar'])) { $avatar_sql_ary = array(); foreach ($avatar_options as $avatar_option) { if (isset($sql_ary[$avatar_option])) { $avatar_sql_ary[$avatar_option] = $sql_ary[$avatar_option]; } } $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $avatar_sql_ary) . "\n\t\t\tWHERE user_avatar = ''\n\t\t\t\tAND " . $db->sql_in_set('user_id', $user_id_ary); $db->sql_query($sql); } // Remove the avatar options, as we already updated them foreach ($avatar_options as $avatar_option) { unset($sql_ary[$avatar_option]); } if (!empty($sql_ary)) { $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE ' . $db->sql_in_set('user_id', $user_id_ary); $db->sql_query($sql); } if (isset($sql_ary['user_colour'])) { // Update any cached colour information for these users $sql = 'UPDATE ' . FORUMS_TABLE . "\n\t\t\tSET forum_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'\n\t\t\tWHERE " . $db->sql_in_set('forum_last_poster_id', $user_id_ary); $db->sql_query($sql); $sql = 'UPDATE ' . TOPICS_TABLE . "\n\t\t\tSET topic_first_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'\n\t\t\tWHERE " . $db->sql_in_set('topic_poster', $user_id_ary); $db->sql_query($sql); $sql = 'UPDATE ' . TOPICS_TABLE . "\n\t\t\tSET topic_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'\n\t\t\tWHERE " . $db->sql_in_set('topic_last_poster_id', $user_id_ary); $db->sql_query($sql); global $config; if (in_array($config['newest_user_id'], $user_id_ary)) { set_config('newest_user_colour', $sql_ary['user_colour'], true); } } // Make all values available for the event $sql_ary = $updated_sql_ary; /** * Event when the default group is set for an array of users * * @event core.user_set_default_group * @var int group_id ID of the group * @var array user_id_ary IDs of the users * @var array group_attributes Group attributes which were changed * @var array update_listing Update the list of moderators and foes * @var array sql_ary User attributes which were changed * @since 3.1.0-a1 */ $vars = array('group_id', 'user_id_ary', 'group_attributes', 'update_listing', 'sql_ary'); extract($phpbb_dispatcher->trigger_event('core.user_set_default_group', compact($vars))); if ($update_listing) { group_update_listings($group_id); } // Because some tables/caches use usercolour-specific data we need to purge this here. $phpbb_container->get('cache.driver')->destroy('sql', MODERATOR_CACHE_TABLE); }
/** * Set users default group * * @access private */ function group_set_user_default($group_id, $user_id_ary, $group_attributes = false, $update_listing = false) { global $db; if (empty($user_id_ary)) { return; } $attribute_ary = array('group_colour' => 'string', 'group_rank' => 'int', 'group_avatar' => 'string', 'group_avatar_type' => 'int', 'group_avatar_width' => 'int', 'group_avatar_height' => 'int'); $sql_ary = array('group_id' => $group_id); // Were group attributes passed to the function? If not we need to obtain them if ($group_attributes === false) { $sql = 'SELECT ' . implode(', ', array_keys($attribute_ary)) . ' FROM ' . GROUPS_TABLE . "\n\t\t\tWHERE group_id = {$group_id}"; $result = $db->sql_query($sql); $group_attributes = $db->sql_fetchrow($result); $db->sql_freeresult($result); } foreach ($attribute_ary as $attribute => $type) { if (isset($group_attributes[$attribute])) { // 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 if ((strpos($attribute, 'group_avatar') === 0 || strpos($attribute, 'group_rank') === 0) && !$group_attributes[$attribute]) { continue; } settype($group_attributes[$attribute], $type); $sql_ary[str_replace('group_', 'user_', $attribute)] = $group_attributes[$attribute]; } } // Before we update the user attributes, we will make a list of those having now the group avatar assigned if (isset($sql_ary['user_avatar'])) { // Ok, get the original avatar data from users having an uploaded one (we need to remove these from the filesystem) $sql = 'SELECT user_id, group_id, user_avatar FROM ' . USERS_TABLE . ' WHERE ' . $db->sql_in_set('user_id', $user_id_ary) . ' AND user_avatar_type = ' . AVATAR_UPLOAD; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { avatar_delete('user', $row); } $db->sql_freeresult($result); } else { unset($sql_ary['user_avatar_type']); unset($sql_ary['user_avatar_height']); unset($sql_ary['user_avatar_width']); } $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE ' . $db->sql_in_set('user_id', $user_id_ary); $db->sql_query($sql); if (isset($sql_ary['user_colour'])) { // Update any cached colour information for these users $sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'\n\t\t\tWHERE " . $db->sql_in_set('forum_last_poster_id', $user_id_ary); $db->sql_query($sql); $sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_first_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'\n\t\t\tWHERE " . $db->sql_in_set('topic_poster', $user_id_ary); $db->sql_query($sql); $sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'\n\t\t\tWHERE " . $db->sql_in_set('topic_last_poster_id', $user_id_ary); $db->sql_query($sql); global $config; if (in_array($config['newest_user_id'], $user_id_ary)) { set_config('newest_user_colour', $sql_ary['user_colour'], true); } } if ($update_listing) { group_update_listings($group_id); } }
/** * Run Tool * * Does the actual stuff we want the tool to do after submission */ function run_tool(&$error) { global $config, $user, $db, $cache; $user->add_lang('ucp'); if (!check_form_key('merge_users')) { $error[] = 'FORM_INVALID'; return; } $source_name = utf8_normalize_nfc(request_var('source_name', '', true)); $source_id = utf8_normalize_nfc(request_var('source_id', 0)); $target_name = utf8_normalize_nfc(request_var('target_name', '', true)); $target_id = utf8_normalize_nfc(request_var('target_id', 0)); $delete = request_var('delete', false); // Check that one source field and one target field is filled in. if (!$source_name && empty($source_id) || !$target_name && empty($target_id)) { if (!$source_name && !$source_id) { $error[] = 'NO_SOURCE_USER'; } if (!$target_name && !$target_id) { $error[] = 'NO_TARGET_USER'; } return; } // Check if both source fields or both target fields are filled in. if ($source_name && $source_id || $target_name && $target_id) { if ($source_name && $source_id) { $error[] = 'BOTH_SOURCE_USER'; } if ($target_name && $target_id) { $error[] = 'BOTH_TARGET_USER'; } return; } $sql = 'SELECT user_id, user_type FROM ' . USERS_TABLE . ' WHERE ' . ($source_name ? 'username_clean = \'' . $db->sql_escape(utf8_clean_string($source_name)) . '\'' : 'user_id = ' . (int) $source_id); $result = $db->sql_query($sql); $source = $db->sql_fetchrow($result); $db->sql_freeresult($result); $sql = 'SELECT user_id, user_type FROM ' . USERS_TABLE . ' WHERE ' . ($target_name ? 'username_clean = \'' . $db->sql_escape(utf8_clean_string($target_name)) . '\'' : 'user_id = ' . (int) $target_id); $result = $db->sql_query($sql); $target = $db->sql_fetchrow($result); $db->sql_freeresult($result); if (!$source || !$target) { if (!$source) { $error[] = 'NO_SOURCE_USER'; } if (!$target) { $error[] = 'NO_TARGET_USER'; } return; } if ($source['user_id'] == $target['user_id']) { $error[] = 'MERGE_USERS_SAME_USERS'; return; } if ($source['user_type'] != $target['user_type']) { if ($source['user_type'] == USER_FOUNDER || $target['user_type'] == USER_FOUNDER) { // Cannot merge a founder with a non founder or vice versa $error[] = 'MERGE_USERS_BOTH_FOUNDERS'; return; } else { if ($source['user_type'] == USER_IGNORE || $target['user_type'] == USER_IGNORE) { // Cannot merge a bot with a non bot or vice versa $error[] = 'MERGE_USERS_BOTH_IGNORE'; return; } } } $source = (int) $source['user_id']; $target = (int) $target['user_id']; // Needed for the merge include PHPBB_ROOT_PATH . 'includes/functions_user.' . PHP_EXT; $result = $this->merge($source, $target); if (is_string($result)) { $error[] = $result; return; } $db->sql_transaction('begin'); foreach ($result as $sql) { $db->sql_query($sql); } $db->sql_transaction('commit'); // Delete source user if ($delete) { user_delete('remove', $source, $post_username = false); } $sql = 'SELECT DISTINCT group_id FROM ' . USER_GROUP_TABLE . "\n\t\t\tWHERE user_id IN ({$source}, {$target})"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { group_update_listings((int) $row['group_id']); } $db->sql_freeresult($result); // Update data update_last_username(); $cache->destroy('sql', MODERATOR_CACHE_TABLE); trigger_error('MERGE_USERS_MERGED'); }