6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
27 function main($id, $mode)
29 global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;
31 $submit = (isset($_POST['submit']) || isset($_GET['add']) || isset($_GET['remove'])) ? true : false;
32 $s_hidden_fields = '';
34 $l_mode = strtoupper($mode);
38 $data = $error = array();
42 'usernames' => array(0),
46 foreach ($var_ary as $var => $default)
48 $data[$var] = request_var($var, $default, true);
51 if (!empty($data['add']) || sizeof($data['usernames']))
53 if (confirm_box(true))
56 if (!empty($data['usernames']))
58 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
59 WHERE user_id = ' . $user->data['user_id'] . '
60 AND ' . $db->sql_in_set('zebra_id', $data['usernames']);
69 $data['add'] = array_map('trim', array_map('utf8_clean_string', explode("\n", $data['add'])));
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);
81 $friends = $foes = array();
82 while ($row = $db->sql_fetchrow($result))
86 $friends[] = utf8_clean_string($row['username']);
90 $foes[] = utf8_clean_string($row['username']);
93 $db->sql_freeresult($result);
95 // remove friends from the username array
96 $n = sizeof($data['add']);
97 $data['add'] = array_diff($data['add'], $friends);
99 if (sizeof($data['add']) < $n && $mode == 'foes')
101 $error[] = $user->lang['NOT_ADDED_FOES_FRIENDS'];
104 // remove foes from the username array
105 $n = sizeof($data['add']);
106 $data['add'] = array_diff($data['add'], $foes);
108 if (sizeof($data['add']) < $n && $mode == 'friends')
110 $error[] = $user->lang['NOT_ADDED_FRIENDS_FOES'];
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'])));
117 if (sizeof($data['add']) < $n)
119 $error[] = $user->lang['NOT_ADDED_' . $l_mode . '_SELF'];
122 unset($friends, $foes, $n);
124 if (sizeof($data['add']))
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);
132 $user_id_ary = array();
133 while ($row = $db->sql_fetchrow($result))
135 if ($row['user_id'] != ANONYMOUS && $row['user_type'] != USER_IGNORE)
137 $user_id_ary[] = $row['user_id'];
139 else if ($row['user_id'] != ANONYMOUS)
141 $error[] = $user->lang['NOT_ADDED_' . $l_mode . '_BOTS'];
145 $error[] = $user->lang['NOT_ADDED_' . $l_mode . '_ANONYMOUS'];
148 $db->sql_freeresult($result);
150 if (sizeof($user_id_ary))
152 // Remove users from foe list if they are admins or moderators
156 foreach ($auth->acl_get_list($user_id_ary, array('a_', 'm_')) as $forum_id => $forum_ary)
158 foreach ($forum_ary as $auth_option => $user_ary)
160 $perms = array_merge($perms, $user_ary);
164 $perms = array_unique($perms);
168 $error[] = $user->lang['NOT_ADDED_FOES_MOD_ADMIN'];
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);
176 if (sizeof($user_id_ary))
178 $sql_mode = ($mode == 'friends') ? 'friend' : 'foe';
181 foreach ($user_id_ary as $zebra_id)
184 'user_id' => (int) $user->data['user_id'],
185 'zebra_id' => (int) $zebra_id,
190 $db->sql_multi_insert(ZEBRA_TABLE, $sql_ary);
196 else if (!sizeof($error))
198 $error[] = $user->lang['USER_NOT_FOUND_OR_INACTIVE'];
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);
211 $template->assign_var('ERROR', implode('<br />', $error));
216 confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
219 'usernames' => $data['usernames'],
220 'add' => $data['add']))
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'] . "
231 AND u.user_id = z.zebra_id
232 ORDER BY u.username_clean ASC";
233 $result = $db->sql_query($sql);
235 $s_username_options = '';
236 while ($row = $db->sql_fetchrow($result))
238 $s_username_options .= '<option value="' . $row['zebra_id'] . '">' . $row['username'] . '</option>';
240 $db->sql_freeresult($result);
242 $template->assign_vars(array(
243 'L_TITLE' => $user->lang['UCP_ZEBRA_' . $l_mode],
245 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=ucp&field=add'),
247 'S_USERNAME_OPTIONS' => $s_username_options,
248 'S_HIDDEN_FIELDS' => $s_hidden_fields,
249 'S_UCP_ACTION' => $this->u_action)
252 $this->tpl_name = 'ucp_zebra_' . $mode;
253 $this->page_title = 'UCP_ZEBRA_' . $l_mode;