includes/ucp/ucp_zebra.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/ucp/ucp_zebra.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,257 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package ucp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* ucp_zebra
    1.24 +* @package ucp
    1.25 +*/
    1.26 +class ucp_zebra
    1.27 +{
    1.28 +	var $u_action;
    1.29 +
    1.30 +	function main($id, $mode)
    1.31 +	{
    1.32 +		global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;
    1.33 +
    1.34 +		$submit	= (isset($_POST['submit']) || isset($_GET['add']) || isset($_GET['remove'])) ? true : false;
    1.35 +		$s_hidden_fields = '';
    1.36 +
    1.37 +		$l_mode = strtoupper($mode);
    1.38 +
    1.39 +		if ($submit)
    1.40 +		{
    1.41 +			$data = $error = array();
    1.42 +			$updated = false;
    1.43 +
    1.44 +			$var_ary = array(
    1.45 +				'usernames'	=> array(0),
    1.46 +				'add'		=> '',
    1.47 +			);
    1.48 +
    1.49 +			foreach ($var_ary as $var => $default)
    1.50 +			{
    1.51 +				$data[$var] = request_var($var, $default, true);
    1.52 +			}
    1.53 +
    1.54 +			if (!empty($data['add']) || sizeof($data['usernames']))
    1.55 +			{
    1.56 +				if (confirm_box(true))
    1.57 +				{
    1.58 +					// Remove users
    1.59 +					if (!empty($data['usernames']))
    1.60 +					{
    1.61 +						$sql = 'DELETE FROM ' . ZEBRA_TABLE . '
    1.62 +							WHERE user_id = ' . $user->data['user_id'] . '
    1.63 +								AND ' . $db->sql_in_set('zebra_id', $data['usernames']);
    1.64 +						$db->sql_query($sql);
    1.65 +
    1.66 +						$updated = true;
    1.67 +					}
    1.68 +
    1.69 +					// Add users
    1.70 +					if ($data['add'])
    1.71 +					{
    1.72 +						$data['add'] = array_map('trim', array_map('utf8_clean_string', explode("\n", $data['add'])));
    1.73 +
    1.74 +						// Do these name/s exist on a list already? If so, ignore ... we could be
    1.75 +						// 'nice' and automatically handle names added to one list present on
    1.76 +						// the other (by removing the existing one) ... but I have a feeling this
    1.77 +						// may lead to complaints
    1.78 +						$sql = 'SELECT z.*, u.username, u.username_clean
    1.79 +							FROM ' . ZEBRA_TABLE . ' z, ' . USERS_TABLE . ' u
    1.80 +							WHERE z.user_id = ' . $user->data['user_id'] . '
    1.81 +								AND u.user_id = z.zebra_id';
    1.82 +						$result = $db->sql_query($sql);
    1.83 +
    1.84 +						$friends = $foes = array();
    1.85 +						while ($row = $db->sql_fetchrow($result))
    1.86 +						{
    1.87 +							if ($row['friend'])
    1.88 +							{
    1.89 +								$friends[] = utf8_clean_string($row['username']);
    1.90 +							}
    1.91 +							else
    1.92 +							{
    1.93 +								$foes[] = utf8_clean_string($row['username']);
    1.94 +							}
    1.95 +						}
    1.96 +						$db->sql_freeresult($result);
    1.97 +
    1.98 +						// remove friends from the username array
    1.99 +						$n = sizeof($data['add']);
   1.100 +						$data['add'] = array_diff($data['add'], $friends);
   1.101 +
   1.102 +						if (sizeof($data['add']) < $n && $mode == 'foes')
   1.103 +						{
   1.104 +							$error[] = $user->lang['NOT_ADDED_FOES_FRIENDS'];
   1.105 +						}
   1.106 +
   1.107 +						// remove foes from the username array
   1.108 +						$n = sizeof($data['add']);
   1.109 +						$data['add'] = array_diff($data['add'], $foes);
   1.110 +
   1.111 +						if (sizeof($data['add']) < $n && $mode == 'friends')
   1.112 +						{
   1.113 +							$error[] = $user->lang['NOT_ADDED_FRIENDS_FOES'];
   1.114 +						}
   1.115 +
   1.116 +						// remove the user himself from the username array
   1.117 +						$n = sizeof($data['add']);
   1.118 +						$data['add'] = array_diff($data['add'], array(utf8_clean_string($user->data['username'])));
   1.119 +
   1.120 +						if (sizeof($data['add']) < $n)
   1.121 +						{
   1.122 +							$error[] = $user->lang['NOT_ADDED_' . $l_mode . '_SELF'];
   1.123 +						}
   1.124 +
   1.125 +						unset($friends, $foes, $n);
   1.126 +
   1.127 +						if (sizeof($data['add']))
   1.128 +						{
   1.129 +							$sql = 'SELECT user_id, user_type
   1.130 +								FROM ' . USERS_TABLE . '
   1.131 +								WHERE ' . $db->sql_in_set('username_clean', $data['add']) . '
   1.132 +									AND user_type <> ' . USER_INACTIVE;
   1.133 +							$result = $db->sql_query($sql);
   1.134 +
   1.135 +							$user_id_ary = array();
   1.136 +							while ($row = $db->sql_fetchrow($result))
   1.137 +							{
   1.138 +								if ($row['user_id'] != ANONYMOUS && $row['user_type'] != USER_IGNORE)
   1.139 +								{
   1.140 +									$user_id_ary[] = $row['user_id'];
   1.141 +								}
   1.142 +								else if ($row['user_id'] != ANONYMOUS)
   1.143 +								{
   1.144 +									$error[] = $user->lang['NOT_ADDED_' . $l_mode . '_BOTS'];
   1.145 +								}
   1.146 +								else
   1.147 +								{
   1.148 +									$error[] = $user->lang['NOT_ADDED_' . $l_mode . '_ANONYMOUS'];
   1.149 +								}
   1.150 +							}
   1.151 +							$db->sql_freeresult($result);
   1.152 +
   1.153 +							if (sizeof($user_id_ary))
   1.154 +							{
   1.155 +								// Remove users from foe list if they are admins or moderators
   1.156 +								if ($mode == 'foes')
   1.157 +								{
   1.158 +									$perms = array();
   1.159 +									foreach ($auth->acl_get_list($user_id_ary, array('a_', 'm_')) as $forum_id => $forum_ary)
   1.160 +									{
   1.161 +										foreach ($forum_ary as $auth_option => $user_ary)
   1.162 +										{
   1.163 +											$perms = array_merge($perms, $user_ary);
   1.164 +										}
   1.165 +									}
   1.166 +
   1.167 +									$perms = array_unique($perms);
   1.168 +
   1.169 +									if (sizeof($perms))
   1.170 +									{
   1.171 +										$error[] = $user->lang['NOT_ADDED_FOES_MOD_ADMIN'];
   1.172 +									}
   1.173 +
   1.174 +									// This may not be right ... it may yield true when perms equate to deny
   1.175 +									$user_id_ary = array_diff($user_id_ary, $perms);
   1.176 +									unset($perms);
   1.177 +								}
   1.178 +
   1.179 +								if (sizeof($user_id_ary))
   1.180 +								{
   1.181 +									$sql_mode = ($mode == 'friends') ? 'friend' : 'foe';
   1.182 +
   1.183 +									$sql_ary = array();
   1.184 +									foreach ($user_id_ary as $zebra_id)
   1.185 +									{
   1.186 +										$sql_ary[] = array(
   1.187 +											'user_id'		=> (int) $user->data['user_id'],
   1.188 +											'zebra_id'		=> (int) $zebra_id,
   1.189 +											$sql_mode		=> 1
   1.190 +										);
   1.191 +									}
   1.192 +
   1.193 +									$db->sql_multi_insert(ZEBRA_TABLE, $sql_ary);
   1.194 +
   1.195 +									$updated = true;
   1.196 +								}
   1.197 +								unset($user_id_ary);
   1.198 +							}
   1.199 +							else if (!sizeof($error))
   1.200 +							{
   1.201 +								$error[] = $user->lang['USER_NOT_FOUND_OR_INACTIVE'];
   1.202 +							}
   1.203 +						}
   1.204 +					}
   1.205 +
   1.206 +					if ($updated)
   1.207 +					{
   1.208 +						meta_refresh(3, $this->u_action);
   1.209 +						$message = $user->lang[$l_mode . '_UPDATED'] . '<br />' . implode('<br />', $error) . ((sizeof($error)) ? '<br />' : '') . '<br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
   1.210 +						trigger_error($message);
   1.211 +					}
   1.212 +					else
   1.213 +					{
   1.214 +						$template->assign_var('ERROR', implode('<br />', $error));
   1.215 +					}
   1.216 +				}
   1.217 +				else
   1.218 +				{
   1.219 +					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
   1.220 +						'mode'		=> $mode,
   1.221 +						'submit'	=> true,
   1.222 +						'usernames'	=> $data['usernames'],
   1.223 +						'add'		=> $data['add']))
   1.224 +					);
   1.225 +				}
   1.226 +			}
   1.227 +		}
   1.228 +
   1.229 +		$sql_and = ($mode == 'friends') ? 'z.friend = 1' : 'z.foe = 1';
   1.230 +		$sql = 'SELECT z.*, u.username, u.username_clean
   1.231 +			FROM ' . ZEBRA_TABLE . ' z, ' . USERS_TABLE . ' u
   1.232 +			WHERE z.user_id = ' . $user->data['user_id'] . "
   1.233 +				AND $sql_and
   1.234 +				AND u.user_id = z.zebra_id
   1.235 +			ORDER BY u.username_clean ASC";
   1.236 +		$result = $db->sql_query($sql);
   1.237 +
   1.238 +		$s_username_options = '';
   1.239 +		while ($row = $db->sql_fetchrow($result))
   1.240 +		{
   1.241 +			$s_username_options .= '<option value="' . $row['zebra_id'] . '">' . $row['username'] . '</option>';
   1.242 +		}
   1.243 +		$db->sql_freeresult($result);
   1.244 +
   1.245 +		$template->assign_vars(array(
   1.246 +			'L_TITLE'			=> $user->lang['UCP_ZEBRA_' . $l_mode],
   1.247 +
   1.248 +			'U_FIND_USERNAME'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=ucp&amp;field=add'),
   1.249 +
   1.250 +			'S_USERNAME_OPTIONS'	=> $s_username_options,
   1.251 +			'S_HIDDEN_FIELDS'		=> $s_hidden_fields,
   1.252 +			'S_UCP_ACTION'			=> $this->u_action)
   1.253 +		);
   1.254 +
   1.255 +		$this->tpl_name = 'ucp_zebra_' . $mode;
   1.256 +		$this->page_title = 'UCP_ZEBRA_' . $l_mode;
   1.257 +	}
   1.258 +}
   1.259 +
   1.260 +?>
   1.261 \ No newline at end of file