includes/ucp/ucp_zebra.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package ucp
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 if (!defined('IN_PHPBB'))
       
    15 {
       
    16 	exit;
       
    17 }
       
    18 
       
    19 /**
       
    20 * ucp_zebra
       
    21 * @package ucp
       
    22 */
       
    23 class ucp_zebra
       
    24 {
       
    25 	var $u_action;
       
    26 
       
    27 	function main($id, $mode)
       
    28 	{
       
    29 		global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;
       
    30 
       
    31 		$submit	= (isset($_POST['submit']) || isset($_GET['add']) || isset($_GET['remove'])) ? true : false;
       
    32 		$s_hidden_fields = '';
       
    33 
       
    34 		$l_mode = strtoupper($mode);
       
    35 
       
    36 		if ($submit)
       
    37 		{
       
    38 			$data = $error = array();
       
    39 			$updated = false;
       
    40 
       
    41 			$var_ary = array(
       
    42 				'usernames'	=> array(0),
       
    43 				'add'		=> '',
       
    44 			);
       
    45 
       
    46 			foreach ($var_ary as $var => $default)
       
    47 			{
       
    48 				$data[$var] = request_var($var, $default, true);
       
    49 			}
       
    50 
       
    51 			if (!empty($data['add']) || sizeof($data['usernames']))
       
    52 			{
       
    53 				if (confirm_box(true))
       
    54 				{
       
    55 					// Remove users
       
    56 					if (!empty($data['usernames']))
       
    57 					{
       
    58 						$sql = 'DELETE FROM ' . ZEBRA_TABLE . '
       
    59 							WHERE user_id = ' . $user->data['user_id'] . '
       
    60 								AND ' . $db->sql_in_set('zebra_id', $data['usernames']);
       
    61 						$db->sql_query($sql);
       
    62 
       
    63 						$updated = true;
       
    64 					}
       
    65 
       
    66 					// Add users
       
    67 					if ($data['add'])
       
    68 					{
       
    69 						$data['add'] = array_map('trim', array_map('utf8_clean_string', explode("\n", $data['add'])));
       
    70 
       
    71 						// Do these name/s exist on a list already? If so, ignore ... we could be
       
    72 						// 'nice' and automatically handle names added to one list present on
       
    73 						// the other (by removing the existing one) ... but I have a feeling this
       
    74 						// may lead to complaints
       
    75 						$sql = 'SELECT z.*, u.username, u.username_clean
       
    76 							FROM ' . ZEBRA_TABLE . ' z, ' . USERS_TABLE . ' u
       
    77 							WHERE z.user_id = ' . $user->data['user_id'] . '
       
    78 								AND u.user_id = z.zebra_id';
       
    79 						$result = $db->sql_query($sql);
       
    80 
       
    81 						$friends = $foes = array();
       
    82 						while ($row = $db->sql_fetchrow($result))
       
    83 						{
       
    84 							if ($row['friend'])
       
    85 							{
       
    86 								$friends[] = utf8_clean_string($row['username']);
       
    87 							}
       
    88 							else
       
    89 							{
       
    90 								$foes[] = utf8_clean_string($row['username']);
       
    91 							}
       
    92 						}
       
    93 						$db->sql_freeresult($result);
       
    94 
       
    95 						// remove friends from the username array
       
    96 						$n = sizeof($data['add']);
       
    97 						$data['add'] = array_diff($data['add'], $friends);
       
    98 
       
    99 						if (sizeof($data['add']) < $n && $mode == 'foes')
       
   100 						{
       
   101 							$error[] = $user->lang['NOT_ADDED_FOES_FRIENDS'];
       
   102 						}
       
   103 
       
   104 						// remove foes from the username array
       
   105 						$n = sizeof($data['add']);
       
   106 						$data['add'] = array_diff($data['add'], $foes);
       
   107 
       
   108 						if (sizeof($data['add']) < $n && $mode == 'friends')
       
   109 						{
       
   110 							$error[] = $user->lang['NOT_ADDED_FRIENDS_FOES'];
       
   111 						}
       
   112 
       
   113 						// remove the user himself from the username array
       
   114 						$n = sizeof($data['add']);
       
   115 						$data['add'] = array_diff($data['add'], array(utf8_clean_string($user->data['username'])));
       
   116 
       
   117 						if (sizeof($data['add']) < $n)
       
   118 						{
       
   119 							$error[] = $user->lang['NOT_ADDED_' . $l_mode . '_SELF'];
       
   120 						}
       
   121 
       
   122 						unset($friends, $foes, $n);
       
   123 
       
   124 						if (sizeof($data['add']))
       
   125 						{
       
   126 							$sql = 'SELECT user_id, user_type
       
   127 								FROM ' . USERS_TABLE . '
       
   128 								WHERE ' . $db->sql_in_set('username_clean', $data['add']) . '
       
   129 									AND user_type <> ' . USER_INACTIVE;
       
   130 							$result = $db->sql_query($sql);
       
   131 
       
   132 							$user_id_ary = array();
       
   133 							while ($row = $db->sql_fetchrow($result))
       
   134 							{
       
   135 								if ($row['user_id'] != ANONYMOUS && $row['user_type'] != USER_IGNORE)
       
   136 								{
       
   137 									$user_id_ary[] = $row['user_id'];
       
   138 								}
       
   139 								else if ($row['user_id'] != ANONYMOUS)
       
   140 								{
       
   141 									$error[] = $user->lang['NOT_ADDED_' . $l_mode . '_BOTS'];
       
   142 								}
       
   143 								else
       
   144 								{
       
   145 									$error[] = $user->lang['NOT_ADDED_' . $l_mode . '_ANONYMOUS'];
       
   146 								}
       
   147 							}
       
   148 							$db->sql_freeresult($result);
       
   149 
       
   150 							if (sizeof($user_id_ary))
       
   151 							{
       
   152 								// Remove users from foe list if they are admins or moderators
       
   153 								if ($mode == 'foes')
       
   154 								{
       
   155 									$perms = array();
       
   156 									foreach ($auth->acl_get_list($user_id_ary, array('a_', 'm_')) as $forum_id => $forum_ary)
       
   157 									{
       
   158 										foreach ($forum_ary as $auth_option => $user_ary)
       
   159 										{
       
   160 											$perms = array_merge($perms, $user_ary);
       
   161 										}
       
   162 									}
       
   163 
       
   164 									$perms = array_unique($perms);
       
   165 
       
   166 									if (sizeof($perms))
       
   167 									{
       
   168 										$error[] = $user->lang['NOT_ADDED_FOES_MOD_ADMIN'];
       
   169 									}
       
   170 
       
   171 									// This may not be right ... it may yield true when perms equate to deny
       
   172 									$user_id_ary = array_diff($user_id_ary, $perms);
       
   173 									unset($perms);
       
   174 								}
       
   175 
       
   176 								if (sizeof($user_id_ary))
       
   177 								{
       
   178 									$sql_mode = ($mode == 'friends') ? 'friend' : 'foe';
       
   179 
       
   180 									$sql_ary = array();
       
   181 									foreach ($user_id_ary as $zebra_id)
       
   182 									{
       
   183 										$sql_ary[] = array(
       
   184 											'user_id'		=> (int) $user->data['user_id'],
       
   185 											'zebra_id'		=> (int) $zebra_id,
       
   186 											$sql_mode		=> 1
       
   187 										);
       
   188 									}
       
   189 
       
   190 									$db->sql_multi_insert(ZEBRA_TABLE, $sql_ary);
       
   191 
       
   192 									$updated = true;
       
   193 								}
       
   194 								unset($user_id_ary);
       
   195 							}
       
   196 							else if (!sizeof($error))
       
   197 							{
       
   198 								$error[] = $user->lang['USER_NOT_FOUND_OR_INACTIVE'];
       
   199 							}
       
   200 						}
       
   201 					}
       
   202 
       
   203 					if ($updated)
       
   204 					{
       
   205 						meta_refresh(3, $this->u_action);
       
   206 						$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>');
       
   207 						trigger_error($message);
       
   208 					}
       
   209 					else
       
   210 					{
       
   211 						$template->assign_var('ERROR', implode('<br />', $error));
       
   212 					}
       
   213 				}
       
   214 				else
       
   215 				{
       
   216 					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
       
   217 						'mode'		=> $mode,
       
   218 						'submit'	=> true,
       
   219 						'usernames'	=> $data['usernames'],
       
   220 						'add'		=> $data['add']))
       
   221 					);
       
   222 				}
       
   223 			}
       
   224 		}
       
   225 
       
   226 		$sql_and = ($mode == 'friends') ? 'z.friend = 1' : 'z.foe = 1';
       
   227 		$sql = 'SELECT z.*, u.username, u.username_clean
       
   228 			FROM ' . ZEBRA_TABLE . ' z, ' . USERS_TABLE . ' u
       
   229 			WHERE z.user_id = ' . $user->data['user_id'] . "
       
   230 				AND $sql_and
       
   231 				AND u.user_id = z.zebra_id
       
   232 			ORDER BY u.username_clean ASC";
       
   233 		$result = $db->sql_query($sql);
       
   234 
       
   235 		$s_username_options = '';
       
   236 		while ($row = $db->sql_fetchrow($result))
       
   237 		{
       
   238 			$s_username_options .= '<option value="' . $row['zebra_id'] . '">' . $row['username'] . '</option>';
       
   239 		}
       
   240 		$db->sql_freeresult($result);
       
   241 
       
   242 		$template->assign_vars(array(
       
   243 			'L_TITLE'			=> $user->lang['UCP_ZEBRA_' . $l_mode],
       
   244 
       
   245 			'U_FIND_USERNAME'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=ucp&amp;field=add'),
       
   246 
       
   247 			'S_USERNAME_OPTIONS'	=> $s_username_options,
       
   248 			'S_HIDDEN_FIELDS'		=> $s_hidden_fields,
       
   249 			'S_UCP_ACTION'			=> $this->u_action)
       
   250 		);
       
   251 
       
   252 		$this->tpl_name = 'ucp_zebra_' . $mode;
       
   253 		$this->page_title = 'UCP_ZEBRA_' . $l_mode;
       
   254 	}
       
   255 }
       
   256 
       
   257 ?>