PageRenderTime 75ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/ucp/ucp_zebra.php

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