Groups.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. <?php
  2. /**
  3. * This file currently just shows group info, and allows certain priviledged members to add/remove members.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Entry point function, permission checks, admin bars, etc.
  18. * It allows moderators and users to access the group showing functions.
  19. * It handles permission checks, and puts the moderation bar on as required.
  20. */
  21. function Groups()
  22. {
  23. global $context, $txt, $scripturl, $sourcedir, $user_info;
  24. // The sub-actions that we can do. Format "Function Name, Mod Bar Index if appropriate".
  25. $subActions = array(
  26. 'index' => array('GroupList', 'view_groups'),
  27. 'members' => array('MembergroupMembers', 'view_groups'),
  28. 'requests' => array('GroupRequests', 'group_requests'),
  29. );
  30. // Default to sub action 'index' or 'settings' depending on permissions.
  31. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'index';
  32. // Get the template stuff up and running.
  33. loadLanguage('ManageMembers');
  34. loadLanguage('ModerationCenter');
  35. loadTemplate('ManageMembergroups');
  36. // If we can see the moderation center, and this has a mod bar entry, add the mod center bar.
  37. if (allowedTo('access_mod_center') || $user_info['mod_cache']['bq'] != '0=1' || $user_info['mod_cache']['gq'] != '0=1' || allowedTo('manage_membergroups'))
  38. {
  39. require_once($sourcedir . '/ModerationCenter.php');
  40. $_GET['area'] = $_REQUEST['sa'] == 'requests' ? 'groups' : 'viewgroups';
  41. ModerationMain(true);
  42. }
  43. // Otherwise add something to the link tree, for normal people.
  44. else
  45. {
  46. isAllowedTo('view_mlist');
  47. $context['linktree'][] = array(
  48. 'url' => $scripturl . '?action=groups',
  49. 'name' => $txt['groups'],
  50. );
  51. }
  52. // Call the actual function.
  53. $subActions[$_REQUEST['sa']][0]();
  54. }
  55. /**
  56. * This very simply lists the groups, nothing snazy.
  57. */
  58. function GroupList()
  59. {
  60. global $txt, $scripturl, $user_profile, $user_info, $context, $settings, $modSettings, $smcFunc, $sourcedir;
  61. // Yep, find the groups...
  62. $request = $smcFunc['db_query']('', '
  63. SELECT mg.id_group, mg.group_name, mg.description, mg.group_type, mg.online_color, mg.hidden,
  64. mg.stars, IFNULL(gm.id_member, 0) AS can_moderate
  65. FROM {db_prefix}membergroups AS mg
  66. LEFT JOIN {db_prefix}group_moderators AS gm ON (gm.id_group = mg.id_group AND gm.id_member = {int:current_member})
  67. WHERE mg.min_posts = {int:min_posts}
  68. AND mg.id_group != {int:mod_group}' . (allowedTo('admin_forum') ? '' : '
  69. AND mg.group_type != {int:is_protected}') . '
  70. ORDER BY group_name',
  71. array(
  72. 'current_member' => $user_info['id'],
  73. 'min_posts' => -1,
  74. 'mod_group' => 3,
  75. 'is_protected' => 1,
  76. )
  77. );
  78. // This is where we store our groups.
  79. $context['groups'] = array();
  80. $group_ids = array();
  81. $context['can_moderate'] = allowedTo('manage_membergroups');
  82. while ($row = $smcFunc['db_fetch_assoc']($request))
  83. {
  84. // We only list the groups they can see.
  85. if ($row['hidden'] && !$row['can_moderate'] && !allowedTo('manage_membergroups'))
  86. continue;
  87. $row['stars'] = explode('#', $row['stars']);
  88. $context['groups'][$row['id_group']] = array(
  89. 'id' => $row['id_group'],
  90. 'name' => $row['group_name'],
  91. 'desc' => $row['description'],
  92. 'color' => $row['online_color'],
  93. 'type' => $row['group_type'],
  94. 'num_members' => 0,
  95. 'stars' => !empty($row['stars'][0]) && !empty($row['stars'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['stars'][1] . '" alt="*" />', $row['stars'][0]) : '',
  96. );
  97. $context['can_moderate'] |= $row['can_moderate'];
  98. $group_ids[] = $row['id_group'];
  99. }
  100. $smcFunc['db_free_result']($request);
  101. // Count up the members separately...
  102. if (!empty($group_ids))
  103. {
  104. $query = $smcFunc['db_query']('', '
  105. SELECT id_group, COUNT(*) AS num_members
  106. FROM {db_prefix}members
  107. WHERE id_group IN ({array_int:group_list})
  108. GROUP BY id_group',
  109. array(
  110. 'group_list' => $group_ids,
  111. )
  112. );
  113. while ($row = $smcFunc['db_fetch_assoc']($query))
  114. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  115. $smcFunc['db_free_result']($query);
  116. // Only do additional groups if we can moderate...
  117. if ($context['can_moderate'])
  118. {
  119. $query = $smcFunc['db_query']('', '
  120. SELECT mg.id_group, COUNT(*) AS num_members
  121. FROM {db_prefix}membergroups AS mg
  122. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_screen}
  123. AND mem.id_group != mg.id_group
  124. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  125. WHERE mg.id_group IN ({array_int:group_list})
  126. GROUP BY mg.id_group',
  127. array(
  128. 'group_list' => $group_ids,
  129. 'blank_screen' => '',
  130. )
  131. );
  132. while ($row = $smcFunc['db_fetch_assoc']($query))
  133. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  134. $smcFunc['db_free_result']($query);
  135. }
  136. }
  137. $context['sub_template'] = 'group_index';
  138. $context['page_title'] = $txt['viewing_groups'];
  139. // Making a list is not hard with this beauty.
  140. require_once($sourcedir . '/Subs-List.php');
  141. // Use the standard templates for showing this.
  142. $listOptions = array(
  143. 'id' => 'group_lists',
  144. 'title' => $context['page_title'],
  145. 'get_items' => array(
  146. 'function' => 'list_getGroups',
  147. ),
  148. 'columns' => array(
  149. 'group' => array(
  150. 'header' => array(
  151. 'value' => $txt['name'],
  152. ),
  153. 'data' => array(
  154. 'function' => create_function('$group', '
  155. global $scripturl, $context;
  156. $output = \'<a href="\' . $scripturl . \'?action=\' . $context[\'current_action\'] . (isset($context[\'admin_area\']) ? \';area=\' . $context[\'admin_area\'] : \'\') . \';sa=members;group=\' . $group[\'id\'] . \'" \' . ($group[\'color\'] ? \'style="color: \' . $group[\'color\'] . \';"\' : \'\') . \'>\' . $group[\'name\'] . \'</a>\';
  157. if ($group[\'desc\'])
  158. $output .= \'<div class="smalltext">\' . $group[\'desc\'] . \'</div>\';
  159. return $output;
  160. '),
  161. 'style' => 'width: 50%;',
  162. ),
  163. ),
  164. 'stars' => array(
  165. 'header' => array(
  166. 'value' => $txt['membergroups_stars'],
  167. ),
  168. 'data' => array(
  169. 'db' => 'stars',
  170. ),
  171. ),
  172. 'moderators' => array(
  173. 'header' => array(
  174. 'value' => $txt['moderators'],
  175. ),
  176. 'data' => array(
  177. 'function' => create_function('$group', '
  178. global $txt;
  179. return empty($group[\'moderators\']) ? \'<em>\' . $txt[\'membergroups_new_copy_none\'] . \'</em>\' : implode(\', \', $group[\'moderators\']);
  180. '),
  181. ),
  182. ),
  183. 'members' => array(
  184. 'header' => array(
  185. 'value' => $txt['membergroups_members_top'],
  186. ),
  187. 'data' => array(
  188. 'comma_format' => true,
  189. 'db' => 'num_members',
  190. ),
  191. ),
  192. ),
  193. );
  194. // Create the request list.
  195. createList($listOptions);
  196. $context['sub_template'] = 'show_list';
  197. $context['default_list'] = 'group_lists';
  198. }
  199. /**
  200. * Get the group information for the list.
  201. * @param int $start
  202. * @param int $items_per_page
  203. * @param int $sort
  204. */
  205. function list_getGroups($start, $items_per_page, $sort)
  206. {
  207. global $smcFunc, $txt, $scripturl, $user_info, $settings, $context;
  208. // Yep, find the groups...
  209. $request = $smcFunc['db_query']('', '
  210. SELECT mg.id_group, mg.group_name, mg.description, mg.group_type, mg.online_color, mg.hidden,
  211. mg.stars, IFNULL(gm.id_member, 0) AS can_moderate
  212. FROM {db_prefix}membergroups AS mg
  213. LEFT JOIN {db_prefix}group_moderators AS gm ON (gm.id_group = mg.id_group AND gm.id_member = {int:current_member})
  214. WHERE mg.min_posts = {int:min_posts}
  215. AND mg.id_group != {int:mod_group}' . (allowedTo('admin_forum') ? '' : '
  216. AND mg.group_type != {int:is_protected}') . '
  217. ORDER BY group_name',
  218. array(
  219. 'current_member' => $user_info['id'],
  220. 'min_posts' => -1,
  221. 'mod_group' => 3,
  222. 'is_protected' => 1,
  223. )
  224. );
  225. // Start collecting the data.
  226. $groups = array();
  227. $group_ids = array();
  228. $context['can_moderate'] = allowedTo('manage_membergroups');
  229. while ($row = $smcFunc['db_fetch_assoc']($request))
  230. {
  231. // We only list the groups they can see.
  232. if ($row['hidden'] && !$row['can_moderate'] && !allowedTo('manage_membergroups'))
  233. continue;
  234. $row['stars'] = explode('#', $row['stars']);
  235. $groups[$row['id_group']] = array(
  236. 'id' => $row['id_group'],
  237. 'name' => $row['group_name'],
  238. 'desc' => $row['description'],
  239. 'color' => $row['online_color'],
  240. 'type' => $row['group_type'],
  241. 'num_members' => 0,
  242. 'moderators' => array(),
  243. 'stars' => !empty($row['stars'][0]) && !empty($row['stars'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['stars'][1] . '" alt="*" />', $row['stars'][0]) : '',
  244. );
  245. $context['can_moderate'] |= $row['can_moderate'];
  246. $group_ids[] = $row['id_group'];
  247. }
  248. $smcFunc['db_free_result']($request);
  249. // Count up the members separately...
  250. if (!empty($group_ids))
  251. {
  252. $query = $smcFunc['db_query']('', '
  253. SELECT id_group, COUNT(*) AS num_members
  254. FROM {db_prefix}members
  255. WHERE id_group IN ({array_int:group_list})
  256. GROUP BY id_group',
  257. array(
  258. 'group_list' => $group_ids,
  259. )
  260. );
  261. while ($row = $smcFunc['db_fetch_assoc']($query))
  262. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  263. $smcFunc['db_free_result']($query);
  264. // Only do additional groups if we can moderate...
  265. if ($context['can_moderate'])
  266. {
  267. $query = $smcFunc['db_query']('', '
  268. SELECT mg.id_group, COUNT(*) AS num_members
  269. FROM {db_prefix}membergroups AS mg
  270. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_screen}
  271. AND mem.id_group != mg.id_group
  272. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  273. WHERE mg.id_group IN ({array_int:group_list})
  274. GROUP BY mg.id_group',
  275. array(
  276. 'group_list' => $group_ids,
  277. 'blank_screen' => '',
  278. )
  279. );
  280. while ($row = $smcFunc['db_fetch_assoc']($query))
  281. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  282. $smcFunc['db_free_result']($query);
  283. }
  284. }
  285. // Get any group moderators.
  286. // Count up the members separately...
  287. if (!empty($group_ids))
  288. {
  289. $query = $smcFunc['db_query']('', '
  290. SELECT mods.id_group, mods.id_member, mem.member_name, mem.real_name
  291. FROM {db_prefix}group_moderators AS mods
  292. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
  293. WHERE mods.id_group IN ({array_int:group_list})',
  294. array(
  295. 'group_list' => $group_ids,
  296. )
  297. );
  298. while ($row = $smcFunc['db_fetch_assoc']($query))
  299. $groups[$row['id_group']]['moderators'][] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
  300. $smcFunc['db_free_result']($query);
  301. }
  302. return $groups;
  303. }
  304. /**
  305. * How many groups are there that are visible?
  306. *
  307. * @return int, the groups count.
  308. */
  309. function list_getGroupCount()
  310. {
  311. global $smcFunc;
  312. $request = $smcFunc['db_query']('', '
  313. SELECT COUNT(id_group) AS group_count
  314. FROM {db_prefix}membergroups
  315. WHERE mg.min_posts = {int:min_posts}
  316. AND mg.id_group != {int:mod_group}' . (allowedTo('admin_forum') ? '' : '
  317. AND mg.group_type != {int:is_protected}'),
  318. array(
  319. 'min_posts' => -1,
  320. 'mod_group' => 3,
  321. 'is_protected' => 1,
  322. )
  323. );
  324. list ($group_count) = $smcFunc['db_fetch_row']($request);
  325. $smcFunc['db_free_result']($request);
  326. return $group_count;
  327. }
  328. /**
  329. * Display members of a group, and allow adding of members to a group. Silly function name though ;)
  330. * It can be called from ManageMembergroups if it needs templating within the admin environment.
  331. * It shows a list of members that are part of a given membergroup.
  332. * It is called by ?action=moderate;area=viewgroups;sa=members;group=x
  333. * It requires the manage_membergroups permission.
  334. * It allows to add and remove members from the selected membergroup.
  335. * It allows sorting on several columns.
  336. * It redirects to itself.
  337. * @uses ManageMembergroups template, group_members sub template.
  338. */
  339. function MembergroupMembers()
  340. {
  341. global $txt, $scripturl, $context, $modSettings, $sourcedir, $user_info, $settings, $smcFunc;
  342. $_REQUEST['group'] = isset($_REQUEST['group']) ? (int) $_REQUEST['group'] : 0;
  343. // No browsing of guests, membergroup 0 or moderators.
  344. if (in_array($_REQUEST['group'], array(-1, 0, 3)))
  345. fatal_lang_error('membergroup_does_not_exist', false);
  346. // Load up the group details.
  347. $request = $smcFunc['db_query']('', '
  348. SELECT id_group AS id, group_name AS name, CASE WHEN min_posts = {int:min_posts} THEN 1 ELSE 0 END AS assignable, hidden, online_color,
  349. stars, description, CASE WHEN min_posts != {int:min_posts} THEN 1 ELSE 0 END AS is_post_group, group_type
  350. FROM {db_prefix}membergroups
  351. WHERE id_group = {int:id_group}
  352. LIMIT 1',
  353. array(
  354. 'min_posts' => -1,
  355. 'id_group' => $_REQUEST['group'],
  356. )
  357. );
  358. // Doesn't exist?
  359. if ($smcFunc['db_num_rows']($request) == 0)
  360. fatal_lang_error('membergroup_does_not_exist', false);
  361. $context['group'] = $smcFunc['db_fetch_assoc']($request);
  362. $smcFunc['db_free_result']($request);
  363. // Fix the stars.
  364. $context['group']['stars'] = explode('#', $context['group']['stars']);
  365. $context['group']['stars'] = !empty($context['group']['stars'][0]) && !empty($context['group']['stars'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $context['group']['stars'][1] . '" alt="*" />', $context['group']['stars'][0]) : '';
  366. $context['group']['can_moderate'] = allowedTo('manage_membergroups') && (allowedTo('admin_forum') || $context['group']['group_type'] != 1);
  367. $context['linktree'][] = array(
  368. 'url' => $scripturl . '?action=groups;sa=members;group=' . $context['group']['id'],
  369. 'name' => $context['group']['name'],
  370. );
  371. // Load all the group moderators, for fun.
  372. $request = $smcFunc['db_query']('', '
  373. SELECT mem.id_member, mem.real_name
  374. FROM {db_prefix}group_moderators AS mods
  375. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
  376. WHERE mods.id_group = {int:id_group}',
  377. array(
  378. 'id_group' => $_REQUEST['group'],
  379. )
  380. );
  381. $context['group']['moderators'] = array();
  382. while ($row = $smcFunc['db_fetch_assoc']($request))
  383. {
  384. $context['group']['moderators'][] = array(
  385. 'id' => $row['id_member'],
  386. 'name' => $row['real_name']
  387. );
  388. if ($user_info['id'] == $row['id_member'] && $context['group']['group_type'] != 1)
  389. $context['group']['can_moderate'] = true;
  390. }
  391. $smcFunc['db_free_result']($request);
  392. // If this group is hidden then it can only "exists" if the user can moderate it!
  393. if ($context['group']['hidden'] && !$context['group']['can_moderate'])
  394. fatal_lang_error('membergroup_does_not_exist', false);
  395. // You can only assign membership if you are the moderator and/or can manage groups!
  396. if (!$context['group']['can_moderate'])
  397. $context['group']['assignable'] = 0;
  398. // Non-admins cannot assign admins.
  399. elseif ($context['group']['id'] == 1 && !allowedTo('admin_forum'))
  400. $context['group']['assignable'] = 0;
  401. // Removing member from group?
  402. if (isset($_POST['remove']) && !empty($_REQUEST['rem']) && is_array($_REQUEST['rem']) && $context['group']['assignable'])
  403. {
  404. checkSession();
  405. validateToken('mod-mgm');
  406. // Make sure we're dealing with integers only.
  407. foreach ($_REQUEST['rem'] as $key => $group)
  408. $_REQUEST['rem'][$key] = (int) $group;
  409. require_once($sourcedir . '/Subs-Membergroups.php');
  410. removeMembersFromGroups($_REQUEST['rem'], $_REQUEST['group'], true);
  411. }
  412. // Must be adding new members to the group...
  413. elseif (isset($_REQUEST['add']) && (!empty($_REQUEST['toAdd']) || !empty($_REQUEST['member_add'])) && $context['group']['assignable'])
  414. {
  415. checkSession();
  416. validateToken('mod-mgm');
  417. $member_query = array();
  418. $member_parameters = array();
  419. // Get all the members to be added... taking into account names can be quoted ;)
  420. $_REQUEST['toAdd'] = strtr($smcFunc['htmlspecialchars']($_REQUEST['toAdd'], ENT_QUOTES), array('&quot;' => '"'));
  421. preg_match_all('~"([^"]+)"~', $_REQUEST['toAdd'], $matches);
  422. $member_names = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_REQUEST['toAdd']))));
  423. foreach ($member_names as $index => $member_name)
  424. {
  425. $member_names[$index] = trim($smcFunc['strtolower']($member_names[$index]));
  426. if (strlen($member_names[$index]) == 0)
  427. unset($member_names[$index]);
  428. }
  429. // Any passed by ID?
  430. $member_ids = array();
  431. if (!empty($_REQUEST['member_add']))
  432. foreach ($_REQUEST['member_add'] as $id)
  433. if ($id > 0)
  434. $member_ids[] = (int) $id;
  435. // Construct the query pelements.
  436. if (!empty($member_ids))
  437. {
  438. $member_query[] = 'id_member IN ({array_int:member_ids})';
  439. $member_parameters['member_ids'] = $member_ids;
  440. }
  441. if (!empty($member_names))
  442. {
  443. $member_query[] = 'LOWER(member_name) IN ({array_string:member_names})';
  444. $member_query[] = 'LOWER(real_name) IN ({array_string:member_names})';
  445. $member_parameters['member_names'] = $member_names;
  446. }
  447. $members = array();
  448. if (!empty($member_query))
  449. {
  450. $request = $smcFunc['db_query']('', '
  451. SELECT id_member
  452. FROM {db_prefix}members
  453. WHERE (' . implode(' OR ', $member_query) . ')
  454. AND id_group != {int:id_group}
  455. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  456. array_merge($member_parameters, array(
  457. 'id_group' => $_REQUEST['group'],
  458. ))
  459. );
  460. while ($row = $smcFunc['db_fetch_assoc']($request))
  461. $members[] = $row['id_member'];
  462. $smcFunc['db_free_result']($request);
  463. }
  464. /**
  465. * @todo Add $_POST['additional'] to templates!
  466. */
  467. // Do the updates...
  468. if (!empty($members))
  469. {
  470. require_once($sourcedir . '/Subs-Membergroups.php');
  471. addMembersToGroup($members, $_REQUEST['group'], isset($_POST['additional']) || $context['group']['hidden'] ? 'only_additional' : 'auto', true);
  472. }
  473. }
  474. // Sort out the sorting!
  475. $sort_methods = array(
  476. 'name' => 'real_name',
  477. 'email' => allowedTo('moderate_forum') ? 'email_address' : 'hide_email ' . (isset($_REQUEST['desc']) ? 'DESC' : 'ASC') . ', email_address',
  478. 'active' => 'last_login',
  479. 'registered' => 'date_registered',
  480. 'posts' => 'posts',
  481. );
  482. // They didn't pick one, default to by name..
  483. if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
  484. {
  485. $context['sort_by'] = 'name';
  486. $querySort = 'real_name';
  487. }
  488. // Otherwise default to ascending.
  489. else
  490. {
  491. $context['sort_by'] = $_REQUEST['sort'];
  492. $querySort = $sort_methods[$_REQUEST['sort']];
  493. }
  494. $context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up';
  495. // The where on the query is interesting. Non-moderators should only see people who are in this group as primary.
  496. if ($context['group']['can_moderate'])
  497. $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group} OR FIND_IN_SET({int:group}, additional_groups) != 0';
  498. else
  499. $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group}';
  500. // Count members of the group.
  501. $request = $smcFunc['db_query']('', '
  502. SELECT COUNT(*)
  503. FROM {db_prefix}members
  504. WHERE ' . $where,
  505. array(
  506. 'group' => $_REQUEST['group'],
  507. )
  508. );
  509. list ($context['total_members']) = $smcFunc['db_fetch_row']($request);
  510. $smcFunc['db_free_result']($request);
  511. $context['total_members'] = comma_format($context['total_members']);
  512. // Create the page index.
  513. $context['page_index'] = constructPageIndex($scripturl . '?action=' . ($context['group']['can_moderate'] ? 'moderate;area=viewgroups' : 'groups') . ';sa=members;group=' . $_REQUEST['group'] . ';sort=' . $context['sort_by'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['total_members'], $modSettings['defaultMaxMembers']);
  514. $context['start'] = $_REQUEST['start'];
  515. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  516. // Load up all members of this group.
  517. $request = $smcFunc['db_query']('', '
  518. SELECT id_member, member_name, real_name, email_address, member_ip, date_registered, last_login,
  519. hide_email, posts, is_activated, real_name
  520. FROM {db_prefix}members
  521. WHERE ' . $where . '
  522. ORDER BY ' . $querySort . ' ' . ($context['sort_direction'] == 'down' ? 'DESC' : 'ASC') . '
  523. LIMIT ' . $context['start'] . ', ' . $modSettings['defaultMaxMembers'],
  524. array(
  525. 'group' => $_REQUEST['group'],
  526. )
  527. );
  528. $context['members'] = array();
  529. while ($row = $smcFunc['db_fetch_assoc']($request))
  530. {
  531. $last_online = empty($row['last_login']) ? $txt['never'] : timeformat($row['last_login']);
  532. // Italicize the online note if they aren't activated.
  533. if ($row['is_activated'] % 10 != 1)
  534. $last_online = '<em title="' . $txt['not_activated'] . '">' . $last_online . '</em>';
  535. $context['members'][] = array(
  536. 'id' => $row['id_member'],
  537. 'name' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  538. 'email' => $row['email_address'],
  539. 'show_email' => showEmailAddress(!empty($row['hide_email']), $row['id_member']),
  540. 'ip' => '<a href="' . $scripturl . '?action=trackip;searchip=' . $row['member_ip'] . '">' . $row['member_ip'] . '</a>',
  541. 'registered' => timeformat($row['date_registered']),
  542. 'last_online' => $last_online,
  543. 'posts' => comma_format($row['posts']),
  544. 'is_activated' => $row['is_activated'] % 10 == 1,
  545. );
  546. }
  547. $smcFunc['db_free_result']($request);
  548. // Select the template.
  549. $context['sub_template'] = 'group_members';
  550. $context['page_title'] = $txt['membergroups_members_title'] . ': ' . $context['group']['name'];
  551. createToken('mod-mgm');
  552. }
  553. /**
  554. * Show and manage all group requests.
  555. */
  556. function GroupRequests()
  557. {
  558. global $txt, $context, $scripturl, $user_info, $sourcedir, $smcFunc, $modSettings, $language;
  559. // Set up the template stuff...
  560. $context['page_title'] = $txt['mc_group_requests'];
  561. $context['sub_template'] = 'show_list';
  562. // Verify we can be here.
  563. if ($user_info['mod_cache']['gq'] == '0=1')
  564. isAllowedTo('manage_membergroups');
  565. // Normally, we act normally...
  566. $where = $user_info['mod_cache']['gq'] == '1=1' || $user_info['mod_cache']['gq'] == '0=1' ? $user_info['mod_cache']['gq'] : 'lgr.' . $user_info['mod_cache']['gq'];
  567. $where_parameters = array();
  568. // We've submitted?
  569. if (isset($_POST[$context['session_var']]) && !empty($_POST['groupr']) && !empty($_POST['req_action']))
  570. {
  571. checkSession('post');
  572. validateToken('mod-gr');
  573. // Clean the values.
  574. foreach ($_POST['groupr'] as $k => $request)
  575. $_POST['groupr'][$k] = (int) $request;
  576. // If we are giving a reason (And why shouldn't we?), then we don't actually do much.
  577. if ($_POST['req_action'] == 'reason')
  578. {
  579. // Different sub template...
  580. $context['sub_template'] = 'group_request_reason';
  581. // And a limitation. We don't care that the page number bit makes no sense, as we don't need it!
  582. $where .= ' AND lgr.id_request IN ({array_int:request_ids})';
  583. $where_parameters['request_ids'] = $_POST['groupr'];
  584. $context['group_requests'] = list_getGroupRequests(0, $modSettings['defaultMaxMessages'], 'lgr.id_request', $where, $where_parameters);
  585. // Let obExit etc sort things out.
  586. obExit();
  587. }
  588. // Otherwise we do something!
  589. else
  590. {
  591. // Get the details of all the members concerned...
  592. $request = $smcFunc['db_query']('', '
  593. SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address, mem.id_group AS primary_group,
  594. mem.additional_groups AS additional_groups, mem.lngfile, mem.member_name, mem.notify_types,
  595. mg.hidden, mg.group_name
  596. FROM {db_prefix}log_group_requests AS lgr
  597. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  598. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  599. WHERE ' . $where . '
  600. AND lgr.id_request IN ({array_int:request_list})
  601. ORDER BY mem.lngfile',
  602. array(
  603. 'request_list' => $_POST['groupr'],
  604. )
  605. );
  606. $email_details = array();
  607. $group_changes = array();
  608. while ($row = $smcFunc['db_fetch_assoc']($request))
  609. {
  610. $row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
  611. // If we are approving work out what their new group is.
  612. if ($_POST['req_action'] == 'approve')
  613. {
  614. // For people with more than one request at once.
  615. if (isset($group_changes[$row['id_member']]))
  616. {
  617. $row['additional_groups'] = $group_changes[$row['id_member']]['add'];
  618. $row['primary_group'] = $group_changes[$row['id_member']]['primary'];
  619. }
  620. else
  621. $row['additional_groups'] = explode(',', $row['additional_groups']);
  622. // Don't have it already?
  623. if ($row['primary_group'] == $row['id_group'] || in_array($row['id_group'], $row['additional_groups']))
  624. continue;
  625. // Should it become their primary?
  626. if ($row['primary_group'] == 0 && $row['hidden'] == 0)
  627. $row['primary_group'] = $row['id_group'];
  628. else
  629. $row['additional_groups'][] = $row['id_group'];
  630. // Add them to the group master list.
  631. $group_changes[$row['id_member']] = array(
  632. 'primary' => $row['primary_group'],
  633. 'add' => $row['additional_groups'],
  634. );
  635. }
  636. // Add required information to email them.
  637. if ($row['notify_types'] != 4)
  638. $email_details[] = array(
  639. 'rid' => $row['id_request'],
  640. 'member_id' => $row['id_member'],
  641. 'member_name' => $row['member_name'],
  642. 'group_id' => $row['id_group'],
  643. 'group_name' => $row['group_name'],
  644. 'email' => $row['email_address'],
  645. 'language' => $row['lngfile'],
  646. );
  647. }
  648. $smcFunc['db_free_result']($request);
  649. // Remove the evidence...
  650. $smcFunc['db_query']('', '
  651. DELETE FROM {db_prefix}log_group_requests
  652. WHERE id_request IN ({array_int:request_list})',
  653. array(
  654. 'request_list' => $_POST['groupr'],
  655. )
  656. );
  657. // Ensure everyone who is online gets their changes right away.
  658. updateSettings(array('settings_updated' => time()));
  659. if (!empty($email_details))
  660. {
  661. require_once($sourcedir . '/Subs-Post.php');
  662. // They are being approved?
  663. if ($_POST['req_action'] == 'approve')
  664. {
  665. // Make the group changes.
  666. foreach ($group_changes as $id => $groups)
  667. {
  668. // Sanity check!
  669. foreach ($groups['add'] as $key => $value)
  670. if ($value == 0 || trim($value) == '')
  671. unset($groups['add'][$key]);
  672. $smcFunc['db_query']('', '
  673. UPDATE {db_prefix}members
  674. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  675. WHERE id_member = {int:selected_member}',
  676. array(
  677. 'primary_group' => $groups['primary'],
  678. 'selected_member' => $id,
  679. 'additional_groups' => implode(',', $groups['add']),
  680. )
  681. );
  682. }
  683. $lastLng = $user_info['language'];
  684. foreach ($email_details as $email)
  685. {
  686. $replacements = array(
  687. 'USERNAME' => $email['member_name'],
  688. 'GROUPNAME' => $email['group_name'],
  689. );
  690. $emaildata = loadEmailTemplate('mc_group_approve', $replacements, $email['language']);
  691. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  692. }
  693. }
  694. // Otherwise, they are getting rejected (With or without a reason).
  695. else
  696. {
  697. // Same as for approving, kind of.
  698. $lastLng = $user_info['language'];
  699. foreach ($email_details as $email)
  700. {
  701. $custom_reason = isset($_POST['groupreason']) && isset($_POST['groupreason'][$email['rid']]) ? $_POST['groupreason'][$email['rid']] : '';
  702. $replacements = array(
  703. 'USERNAME' => $email['member_name'],
  704. 'GROUPNAME' => $email['group_name'],
  705. );
  706. if (!empty($custom_reason))
  707. $replacements['REASON'] = $custom_reason;
  708. $emaildata = loadEmailTemplate(empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason', $replacements, $email['language']);
  709. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  710. }
  711. }
  712. }
  713. // Restore the current language.
  714. loadLanguage('ModerationCenter');
  715. }
  716. }
  717. // We're going to want this for making our list.
  718. require_once($sourcedir . '/Subs-List.php');
  719. // This is all the information required for a group listing.
  720. $listOptions = array(
  721. 'id' => 'group_request_list',
  722. 'title' => $txt['mc_group_requests'],
  723. 'width' => '100%',
  724. 'items_per_page' => $modSettings['defaultMaxMessages'],
  725. 'no_items_label' => $txt['mc_groupr_none_found'],
  726. 'base_href' => $scripturl . '?action=groups;sa=requests',
  727. 'default_sort_col' => 'member',
  728. 'get_items' => array(
  729. 'function' => 'list_getGroupRequests',
  730. 'params' => array(
  731. $where,
  732. $where_parameters,
  733. ),
  734. ),
  735. 'get_count' => array(
  736. 'function' => 'list_getGroupRequestCount',
  737. 'params' => array(
  738. $where,
  739. $where_parameters,
  740. ),
  741. ),
  742. 'columns' => array(
  743. 'member' => array(
  744. 'header' => array(
  745. 'value' => $txt['mc_groupr_member'],
  746. ),
  747. 'data' => array(
  748. 'db' => 'member_link',
  749. ),
  750. 'sort' => array(
  751. 'default' => 'mem.member_name',
  752. 'reverse' => 'mem.member_name DESC',
  753. ),
  754. ),
  755. 'group' => array(
  756. 'header' => array(
  757. 'value' => $txt['mc_groupr_group'],
  758. ),
  759. 'data' => array(
  760. 'db' => 'group_link',
  761. ),
  762. 'sort' => array(
  763. 'default' => 'mg.group_name',
  764. 'reverse' => 'mg.group_name DESC',
  765. ),
  766. ),
  767. 'reason' => array(
  768. 'header' => array(
  769. 'value' => $txt['mc_groupr_reason'],
  770. ),
  771. 'data' => array(
  772. 'db' => 'reason',
  773. ),
  774. ),
  775. 'action' => array(
  776. 'header' => array(
  777. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  778. 'style' => 'width: 4%;',
  779. ),
  780. 'data' => array(
  781. 'sprintf' => array(
  782. 'format' => '<input type="checkbox" name="groupr[]" value="%1$d" class="input_check" />',
  783. 'params' => array(
  784. 'id' => false,
  785. ),
  786. ),
  787. 'style' => 'text-align: center;',
  788. ),
  789. ),
  790. ),
  791. 'form' => array(
  792. 'href' => $scripturl . '?action=groups;sa=requests',
  793. 'include_sort' => true,
  794. 'include_start' => true,
  795. 'hidden_fields' => array(
  796. $context['session_var'] => $context['session_id'],
  797. ),
  798. 'token' => 'mod-gr',
  799. ),
  800. 'additional_rows' => array(
  801. array(
  802. 'position' => 'bottom_of_list',
  803. 'value' => '
  804. <select name="req_action" onchange="if (this.value != 0 &amp;&amp; (this.value == \'reason\' || confirm(\'' . $txt['mc_groupr_warning'] . '\'))) this.form.submit();">
  805. <option value="0">' . $txt['with_selected'] . ':</option>
  806. <option value="0">---------------------</option>
  807. <option value="approve">' . $txt['mc_groupr_approve'] . '</option>
  808. <option value="reject">' . $txt['mc_groupr_reject'] . '</option>
  809. <option value="reason">' . $txt['mc_groupr_reject_w_reason'] . '</option>
  810. </select>
  811. <input type="submit" name="go" value="' . $txt['go'] . '" onclick="var sel = document.getElementById(\'req_action\'); if (sel.value != 0 &amp;&amp; sel.value != \'reason\' &amp;&amp; !confirm(\'' . $txt['mc_groupr_warning'] . '\')) return false;" class="button_submit" />',
  812. 'align' => 'right',
  813. ),
  814. ),
  815. );
  816. // Create the request list.
  817. createToken('mod-gr');
  818. createList($listOptions);
  819. $context['default_list'] = 'group_request_list';
  820. }
  821. /**
  822. * Callback function for createList().
  823. *
  824. * @param $where
  825. * @param $where_parameters
  826. * @return int, the count of group requests
  827. */
  828. function list_getGroupRequestCount($where, $where_parameters)
  829. {
  830. global $smcFunc;
  831. $request = $smcFunc['db_query']('', '
  832. SELECT COUNT(*)
  833. FROM {db_prefix}log_group_requests AS lgr
  834. WHERE ' . $where,
  835. array_merge($where_parameters, array(
  836. ))
  837. );
  838. list ($totalRequests) = $smcFunc['db_fetch_row']($request);
  839. $smcFunc['db_free_result']($request);
  840. return $totalRequests;
  841. }
  842. /**
  843. * Callback function for createList()
  844. *
  845. * @param int $start
  846. * @param int $items_per_page
  847. * @param string $sort
  848. * @param string $where
  849. * @param string $where_parameters
  850. * @return array, an array of group requests
  851. * Each group request has:
  852. * 'id'
  853. * 'member_link'
  854. * 'group_link'
  855. * 'reason'
  856. * 'time_submitted'
  857. */
  858. function list_getGroupRequests($start, $items_per_page, $sort, $where, $where_parameters)
  859. {
  860. global $smcFunc, $txt, $scripturl;
  861. $request = $smcFunc['db_query']('', '
  862. SELECT lgr.id_request, lgr.id_member, lgr.id_group, lgr.time_applied, lgr.reason,
  863. mem.member_name, mg.group_name, mg.online_color, mem.real_name
  864. FROM {db_prefix}log_group_requests AS lgr
  865. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  866. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  867. WHERE ' . $where . '
  868. ORDER BY {raw:sort}
  869. LIMIT ' . $start . ', ' . $items_per_page,
  870. array_merge($where_parameters, array(
  871. 'sort' => $sort,
  872. ))
  873. );
  874. $group_requests = array();
  875. while ($row = $smcFunc['db_fetch_assoc']($request))
  876. {
  877. $group_requests[] = array(
  878. 'id' => $row['id_request'],
  879. 'member_link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  880. 'group_link' => '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>',
  881. 'reason' => censorText($row['reason']),
  882. 'time_submitted' => timeformat($row['time_applied']),
  883. );
  884. }
  885. $smcFunc['db_free_result']($request);
  886. return $group_requests;
  887. }
  888. ?>