Groups.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. // @todo Add $_POST['additional'] to templates!
  465. // Do the updates...
  466. if (!empty($members))
  467. {
  468. require_once($sourcedir . '/Subs-Membergroups.php');
  469. addMembersToGroup($members, $_REQUEST['group'], isset($_POST['additional']) || $context['group']['hidden'] ? 'only_additional' : 'auto', true);
  470. }
  471. }
  472. // Sort out the sorting!
  473. $sort_methods = array(
  474. 'name' => 'real_name',
  475. 'email' => allowedTo('moderate_forum') ? 'email_address' : 'hide_email ' . (isset($_REQUEST['desc']) ? 'DESC' : 'ASC') . ', email_address',
  476. 'active' => 'last_login',
  477. 'registered' => 'date_registered',
  478. 'posts' => 'posts',
  479. );
  480. // They didn't pick one, default to by name..
  481. if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
  482. {
  483. $context['sort_by'] = 'name';
  484. $querySort = 'real_name';
  485. }
  486. // Otherwise default to ascending.
  487. else
  488. {
  489. $context['sort_by'] = $_REQUEST['sort'];
  490. $querySort = $sort_methods[$_REQUEST['sort']];
  491. }
  492. $context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up';
  493. // The where on the query is interesting. Non-moderators should only see people who are in this group as primary.
  494. if ($context['group']['can_moderate'])
  495. $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group} OR FIND_IN_SET({int:group}, additional_groups) != 0';
  496. else
  497. $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group}';
  498. // Count members of the group.
  499. $request = $smcFunc['db_query']('', '
  500. SELECT COUNT(*)
  501. FROM {db_prefix}members
  502. WHERE ' . $where,
  503. array(
  504. 'group' => $_REQUEST['group'],
  505. )
  506. );
  507. list ($context['total_members']) = $smcFunc['db_fetch_row']($request);
  508. $smcFunc['db_free_result']($request);
  509. $context['total_members'] = comma_format($context['total_members']);
  510. // Create the page index.
  511. $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']);
  512. $context['start'] = $_REQUEST['start'];
  513. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  514. // Load up all members of this group.
  515. $request = $smcFunc['db_query']('', '
  516. SELECT id_member, member_name, real_name, email_address, member_ip, date_registered, last_login,
  517. hide_email, posts, is_activated, real_name
  518. FROM {db_prefix}members
  519. WHERE ' . $where . '
  520. ORDER BY ' . $querySort . ' ' . ($context['sort_direction'] == 'down' ? 'DESC' : 'ASC') . '
  521. LIMIT ' . $context['start'] . ', ' . $modSettings['defaultMaxMembers'],
  522. array(
  523. 'group' => $_REQUEST['group'],
  524. )
  525. );
  526. $context['members'] = array();
  527. while ($row = $smcFunc['db_fetch_assoc']($request))
  528. {
  529. $last_online = empty($row['last_login']) ? $txt['never'] : timeformat($row['last_login']);
  530. // Italicize the online note if they aren't activated.
  531. if ($row['is_activated'] % 10 != 1)
  532. $last_online = '<em title="' . $txt['not_activated'] . '">' . $last_online . '</em>';
  533. $context['members'][] = array(
  534. 'id' => $row['id_member'],
  535. 'name' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  536. 'email' => $row['email_address'],
  537. 'show_email' => showEmailAddress(!empty($row['hide_email']), $row['id_member']),
  538. 'ip' => '<a href="' . $scripturl . '?action=trackip;searchip=' . $row['member_ip'] . '">' . $row['member_ip'] . '</a>',
  539. 'registered' => timeformat($row['date_registered']),
  540. 'last_online' => $last_online,
  541. 'posts' => comma_format($row['posts']),
  542. 'is_activated' => $row['is_activated'] % 10 == 1,
  543. );
  544. }
  545. $smcFunc['db_free_result']($request);
  546. // Select the template.
  547. $context['sub_template'] = 'group_members';
  548. $context['page_title'] = $txt['membergroups_members_title'] . ': ' . $context['group']['name'];
  549. createToken('mod-mgm');
  550. }
  551. /**
  552. * Show and manage all group requests.
  553. */
  554. function GroupRequests()
  555. {
  556. global $txt, $context, $scripturl, $user_info, $sourcedir, $smcFunc, $modSettings, $language;
  557. // Set up the template stuff...
  558. $context['page_title'] = $txt['mc_group_requests'];
  559. $context['sub_template'] = 'show_list';
  560. // Verify we can be here.
  561. if ($user_info['mod_cache']['gq'] == '0=1')
  562. isAllowedTo('manage_membergroups');
  563. // Normally, we act normally...
  564. $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'];
  565. $where_parameters = array();
  566. // We've submitted?
  567. if (isset($_POST[$context['session_var']]) && !empty($_POST['groupr']) && !empty($_POST['req_action']))
  568. {
  569. checkSession('post');
  570. validateToken('mod-gr');
  571. // Clean the values.
  572. foreach ($_POST['groupr'] as $k => $request)
  573. $_POST['groupr'][$k] = (int) $request;
  574. // If we are giving a reason (And why shouldn't we?), then we don't actually do much.
  575. if ($_POST['req_action'] == 'reason')
  576. {
  577. // Different sub template...
  578. $context['sub_template'] = 'group_request_reason';
  579. // And a limitation. We don't care that the page number bit makes no sense, as we don't need it!
  580. $where .= ' AND lgr.id_request IN ({array_int:request_ids})';
  581. $where_parameters['request_ids'] = $_POST['groupr'];
  582. $context['group_requests'] = list_getGroupRequests(0, $modSettings['defaultMaxMessages'], 'lgr.id_request', $where, $where_parameters);
  583. // Let obExit etc sort things out.
  584. obExit();
  585. }
  586. // Otherwise we do something!
  587. else
  588. {
  589. // Get the details of all the members concerned...
  590. $request = $smcFunc['db_query']('', '
  591. SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address, mem.id_group AS primary_group,
  592. mem.additional_groups AS additional_groups, mem.lngfile, mem.member_name, mem.notify_types,
  593. mg.hidden, mg.group_name
  594. FROM {db_prefix}log_group_requests AS lgr
  595. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  596. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  597. WHERE ' . $where . '
  598. AND lgr.id_request IN ({array_int:request_list})
  599. ORDER BY mem.lngfile',
  600. array(
  601. 'request_list' => $_POST['groupr'],
  602. )
  603. );
  604. $email_details = array();
  605. $group_changes = array();
  606. while ($row = $smcFunc['db_fetch_assoc']($request))
  607. {
  608. $row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
  609. // If we are approving work out what their new group is.
  610. if ($_POST['req_action'] == 'approve')
  611. {
  612. // For people with more than one request at once.
  613. if (isset($group_changes[$row['id_member']]))
  614. {
  615. $row['additional_groups'] = $group_changes[$row['id_member']]['add'];
  616. $row['primary_group'] = $group_changes[$row['id_member']]['primary'];
  617. }
  618. else
  619. $row['additional_groups'] = explode(',', $row['additional_groups']);
  620. // Don't have it already?
  621. if ($row['primary_group'] == $row['id_group'] || in_array($row['id_group'], $row['additional_groups']))
  622. continue;
  623. // Should it become their primary?
  624. if ($row['primary_group'] == 0 && $row['hidden'] == 0)
  625. $row['primary_group'] = $row['id_group'];
  626. else
  627. $row['additional_groups'][] = $row['id_group'];
  628. // Add them to the group master list.
  629. $group_changes[$row['id_member']] = array(
  630. 'primary' => $row['primary_group'],
  631. 'add' => $row['additional_groups'],
  632. );
  633. }
  634. // Add required information to email them.
  635. if ($row['notify_types'] != 4)
  636. $email_details[] = array(
  637. 'rid' => $row['id_request'],
  638. 'member_id' => $row['id_member'],
  639. 'member_name' => $row['member_name'],
  640. 'group_id' => $row['id_group'],
  641. 'group_name' => $row['group_name'],
  642. 'email' => $row['email_address'],
  643. 'language' => $row['lngfile'],
  644. );
  645. }
  646. $smcFunc['db_free_result']($request);
  647. // Remove the evidence...
  648. $smcFunc['db_query']('', '
  649. DELETE FROM {db_prefix}log_group_requests
  650. WHERE id_request IN ({array_int:request_list})',
  651. array(
  652. 'request_list' => $_POST['groupr'],
  653. )
  654. );
  655. // Ensure everyone who is online gets their changes right away.
  656. updateSettings(array('settings_updated' => time()));
  657. if (!empty($email_details))
  658. {
  659. require_once($sourcedir . '/Subs-Post.php');
  660. // They are being approved?
  661. if ($_POST['req_action'] == 'approve')
  662. {
  663. // Make the group changes.
  664. foreach ($group_changes as $id => $groups)
  665. {
  666. // Sanity check!
  667. foreach ($groups['add'] as $key => $value)
  668. if ($value == 0 || trim($value) == '')
  669. unset($groups['add'][$key]);
  670. $smcFunc['db_query']('', '
  671. UPDATE {db_prefix}members
  672. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  673. WHERE id_member = {int:selected_member}',
  674. array(
  675. 'primary_group' => $groups['primary'],
  676. 'selected_member' => $id,
  677. 'additional_groups' => implode(',', $groups['add']),
  678. )
  679. );
  680. }
  681. $lastLng = $user_info['language'];
  682. foreach ($email_details as $email)
  683. {
  684. $replacements = array(
  685. 'USERNAME' => $email['member_name'],
  686. 'GROUPNAME' => $email['group_name'],
  687. );
  688. $emaildata = loadEmailTemplate('mc_group_approve', $replacements, $email['language']);
  689. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  690. }
  691. }
  692. // Otherwise, they are getting rejected (With or without a reason).
  693. else
  694. {
  695. // Same as for approving, kind of.
  696. $lastLng = $user_info['language'];
  697. foreach ($email_details as $email)
  698. {
  699. $custom_reason = isset($_POST['groupreason']) && isset($_POST['groupreason'][$email['rid']]) ? $_POST['groupreason'][$email['rid']] : '';
  700. $replacements = array(
  701. 'USERNAME' => $email['member_name'],
  702. 'GROUPNAME' => $email['group_name'],
  703. );
  704. if (!empty($custom_reason))
  705. $replacements['REASON'] = $custom_reason;
  706. $emaildata = loadEmailTemplate(empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason', $replacements, $email['language']);
  707. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  708. }
  709. }
  710. }
  711. // Restore the current language.
  712. loadLanguage('ModerationCenter');
  713. }
  714. }
  715. // We're going to want this for making our list.
  716. require_once($sourcedir . '/Subs-List.php');
  717. // This is all the information required for a group listing.
  718. $listOptions = array(
  719. 'id' => 'group_request_list',
  720. 'title' => $txt['mc_group_requests'],
  721. 'width' => '100%',
  722. 'items_per_page' => $modSettings['defaultMaxMessages'],
  723. 'no_items_label' => $txt['mc_groupr_none_found'],
  724. 'base_href' => $scripturl . '?action=groups;sa=requests',
  725. 'default_sort_col' => 'member',
  726. 'get_items' => array(
  727. 'function' => 'list_getGroupRequests',
  728. 'params' => array(
  729. $where,
  730. $where_parameters,
  731. ),
  732. ),
  733. 'get_count' => array(
  734. 'function' => 'list_getGroupRequestCount',
  735. 'params' => array(
  736. $where,
  737. $where_parameters,
  738. ),
  739. ),
  740. 'columns' => array(
  741. 'member' => array(
  742. 'header' => array(
  743. 'value' => $txt['mc_groupr_member'],
  744. ),
  745. 'data' => array(
  746. 'db' => 'member_link',
  747. ),
  748. 'sort' => array(
  749. 'default' => 'mem.member_name',
  750. 'reverse' => 'mem.member_name DESC',
  751. ),
  752. ),
  753. 'group' => array(
  754. 'header' => array(
  755. 'value' => $txt['mc_groupr_group'],
  756. ),
  757. 'data' => array(
  758. 'db' => 'group_link',
  759. ),
  760. 'sort' => array(
  761. 'default' => 'mg.group_name',
  762. 'reverse' => 'mg.group_name DESC',
  763. ),
  764. ),
  765. 'reason' => array(
  766. 'header' => array(
  767. 'value' => $txt['mc_groupr_reason'],
  768. ),
  769. 'data' => array(
  770. 'db' => 'reason',
  771. ),
  772. ),
  773. 'action' => array(
  774. 'header' => array(
  775. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  776. 'style' => 'width: 4%;',
  777. ),
  778. 'data' => array(
  779. 'sprintf' => array(
  780. 'format' => '<input type="checkbox" name="groupr[]" value="%1$d" class="input_check" />',
  781. 'params' => array(
  782. 'id' => false,
  783. ),
  784. ),
  785. 'style' => 'text-align: center;',
  786. ),
  787. ),
  788. ),
  789. 'form' => array(
  790. 'href' => $scripturl . '?action=groups;sa=requests',
  791. 'include_sort' => true,
  792. 'include_start' => true,
  793. 'hidden_fields' => array(
  794. $context['session_var'] => $context['session_id'],
  795. ),
  796. 'token' => 'mod-gr',
  797. ),
  798. 'additional_rows' => array(
  799. array(
  800. 'position' => 'bottom_of_list',
  801. 'value' => '
  802. <select name="req_action" onchange="if (this.value != 0 &amp;&amp; (this.value == \'reason\' || confirm(\'' . $txt['mc_groupr_warning'] . '\'))) this.form.submit();">
  803. <option value="0">' . $txt['with_selected'] . ':</option>
  804. <option value="0">---------------------</option>
  805. <option value="approve">' . $txt['mc_groupr_approve'] . '</option>
  806. <option value="reject">' . $txt['mc_groupr_reject'] . '</option>
  807. <option value="reason">' . $txt['mc_groupr_reject_w_reason'] . '</option>
  808. </select>
  809. <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" />',
  810. 'align' => 'right',
  811. ),
  812. ),
  813. );
  814. // Create the request list.
  815. createToken('mod-gr');
  816. createList($listOptions);
  817. $context['default_list'] = 'group_request_list';
  818. }
  819. /**
  820. * Callback function for createList().
  821. *
  822. * @param $where
  823. * @param $where_parameters
  824. * @return int, the count of group requests
  825. */
  826. function list_getGroupRequestCount($where, $where_parameters)
  827. {
  828. global $smcFunc;
  829. $request = $smcFunc['db_query']('', '
  830. SELECT COUNT(*)
  831. FROM {db_prefix}log_group_requests AS lgr
  832. WHERE ' . $where,
  833. array_merge($where_parameters, array(
  834. ))
  835. );
  836. list ($totalRequests) = $smcFunc['db_fetch_row']($request);
  837. $smcFunc['db_free_result']($request);
  838. return $totalRequests;
  839. }
  840. /**
  841. * Callback function for createList()
  842. *
  843. * @param int $start
  844. * @param int $items_per_page
  845. * @param string $sort
  846. * @param string $where
  847. * @param string $where_parameters
  848. * @return array, an array of group requests
  849. * Each group request has:
  850. * 'id'
  851. * 'member_link'
  852. * 'group_link'
  853. * 'reason'
  854. * 'time_submitted'
  855. */
  856. function list_getGroupRequests($start, $items_per_page, $sort, $where, $where_parameters)
  857. {
  858. global $smcFunc, $txt, $scripturl;
  859. $request = $smcFunc['db_query']('', '
  860. SELECT lgr.id_request, lgr.id_member, lgr.id_group, lgr.time_applied, lgr.reason,
  861. mem.member_name, mg.group_name, mg.online_color, mem.real_name
  862. FROM {db_prefix}log_group_requests AS lgr
  863. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  864. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  865. WHERE ' . $where . '
  866. ORDER BY {raw:sort}
  867. LIMIT ' . $start . ', ' . $items_per_page,
  868. array_merge($where_parameters, array(
  869. 'sort' => $sort,
  870. ))
  871. );
  872. $group_requests = array();
  873. while ($row = $smcFunc['db_fetch_assoc']($request))
  874. {
  875. $group_requests[] = array(
  876. 'id' => $row['id_request'],
  877. 'member_link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  878. 'group_link' => '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>',
  879. 'reason' => censorText($row['reason']),
  880. 'time_submitted' => timeformat($row['time_applied']),
  881. );
  882. }
  883. $smcFunc['db_free_result']($request);
  884. return $group_requests;
  885. }
  886. ?>