Groups.php 32 KB

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