Groups.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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.0
  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;
  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. // Make sure we're dealing with integers only.
  406. foreach ($_REQUEST['rem'] as $key => $group)
  407. $_REQUEST['rem'][$key] = (int) $group;
  408. require_once($sourcedir . '/Subs-Membergroups.php');
  409. removeMembersFromGroups($_REQUEST['rem'], $_REQUEST['group'], true);
  410. }
  411. // Must be adding new members to the group...
  412. elseif (isset($_REQUEST['add']) && (!empty($_REQUEST['toAdd']) || !empty($_REQUEST['member_add'])) && $context['group']['assignable'])
  413. {
  414. checkSession();
  415. $member_query = array();
  416. $member_parameters = array();
  417. // Get all the members to be added... taking into account names can be quoted ;)
  418. $_REQUEST['toAdd'] = strtr($smcFunc['htmlspecialchars']($_REQUEST['toAdd'], ENT_QUOTES), array('&quot;' => '"'));
  419. preg_match_all('~"([^"]+)"~', $_REQUEST['toAdd'], $matches);
  420. $member_names = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_REQUEST['toAdd']))));
  421. foreach ($member_names as $index => $member_name)
  422. {
  423. $member_names[$index] = trim($smcFunc['strtolower']($member_names[$index]));
  424. if (strlen($member_names[$index]) == 0)
  425. unset($member_names[$index]);
  426. }
  427. // Any passed by ID?
  428. $member_ids = array();
  429. if (!empty($_REQUEST['member_add']))
  430. foreach ($_REQUEST['member_add'] as $id)
  431. if ($id > 0)
  432. $member_ids[] = (int) $id;
  433. // Construct the query pelements.
  434. if (!empty($member_ids))
  435. {
  436. $member_query[] = 'id_member IN ({array_int:member_ids})';
  437. $member_parameters['member_ids'] = $member_ids;
  438. }
  439. if (!empty($member_names))
  440. {
  441. $member_query[] = 'LOWER(member_name) IN ({array_string:member_names})';
  442. $member_query[] = 'LOWER(real_name) IN ({array_string:member_names})';
  443. $member_parameters['member_names'] = $member_names;
  444. }
  445. $members = array();
  446. if (!empty($member_query))
  447. {
  448. $request = $smcFunc['db_query']('', '
  449. SELECT id_member
  450. FROM {db_prefix}members
  451. WHERE (' . implode(' OR ', $member_query) . ')
  452. AND id_group != {int:id_group}
  453. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  454. array_merge($member_parameters, array(
  455. 'id_group' => $_REQUEST['group'],
  456. ))
  457. );
  458. while ($row = $smcFunc['db_fetch_assoc']($request))
  459. $members[] = $row['id_member'];
  460. $smcFunc['db_free_result']($request);
  461. }
  462. /**
  463. * @todo Add $_POST['additional'] to templates!
  464. */
  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. }
  550. /**
  551. * Show and manage all group requests.
  552. */
  553. function GroupRequests()
  554. {
  555. global $txt, $context, $scripturl, $user_info, $sourcedir, $smcFunc, $modSettings, $language;
  556. // Set up the template stuff...
  557. $context['page_title'] = $txt['mc_group_requests'];
  558. $context['sub_template'] = 'show_list';
  559. // Verify we can be here.
  560. if ($user_info['mod_cache']['gq'] == '0=1')
  561. isAllowedTo('manage_membergroups');
  562. // Normally, we act normally...
  563. $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'];
  564. $where_parameters = array();
  565. // We've submitted?
  566. if (isset($_POST[$context['session_var']]) && !empty($_POST['groupr']) && !empty($_POST['req_action']))
  567. {
  568. checkSession('post');
  569. // Clean the values.
  570. foreach ($_POST['groupr'] as $k => $request)
  571. $_POST['groupr'][$k] = (int) $request;
  572. // If we are giving a reason (And why shouldn't we?), then we don't actually do much.
  573. if ($_POST['req_action'] == 'reason')
  574. {
  575. // Different sub template...
  576. $context['sub_template'] = 'group_request_reason';
  577. // And a limitation. We don't care that the page number bit makes no sense, as we don't need it!
  578. $where .= ' AND lgr.id_request IN ({array_int:request_ids})';
  579. $where_parameters['request_ids'] = $_POST['groupr'];
  580. $context['group_requests'] = list_getGroupRequests(0, $modSettings['defaultMaxMessages'], 'lgr.id_request', $where, $where_parameters);
  581. // Let obExit etc sort things out.
  582. obExit();
  583. }
  584. // Otherwise we do something!
  585. else
  586. {
  587. // Get the details of all the members concerned...
  588. $request = $smcFunc['db_query']('', '
  589. SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address, mem.id_group AS primary_group,
  590. mem.additional_groups AS additional_groups, mem.lngfile, mem.member_name, mem.notify_types,
  591. mg.hidden, mg.group_name
  592. FROM {db_prefix}log_group_requests AS lgr
  593. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  594. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  595. WHERE ' . $where . '
  596. AND lgr.id_request IN ({array_int:request_list})
  597. ORDER BY mem.lngfile',
  598. array(
  599. 'request_list' => $_POST['groupr'],
  600. )
  601. );
  602. $email_details = array();
  603. $group_changes = array();
  604. while ($row = $smcFunc['db_fetch_assoc']($request))
  605. {
  606. $row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
  607. // If we are approving work out what their new group is.
  608. if ($_POST['req_action'] == 'approve')
  609. {
  610. // For people with more than one request at once.
  611. if (isset($group_changes[$row['id_member']]))
  612. {
  613. $row['additional_groups'] = $group_changes[$row['id_member']]['add'];
  614. $row['primary_group'] = $group_changes[$row['id_member']]['primary'];
  615. }
  616. else
  617. $row['additional_groups'] = explode(',', $row['additional_groups']);
  618. // Don't have it already?
  619. if ($row['primary_group'] == $row['id_group'] || in_array($row['id_group'], $row['additional_groups']))
  620. continue;
  621. // Should it become their primary?
  622. if ($row['primary_group'] == 0 && $row['hidden'] == 0)
  623. $row['primary_group'] = $row['id_group'];
  624. else
  625. $row['additional_groups'][] = $row['id_group'];
  626. // Add them to the group master list.
  627. $group_changes[$row['id_member']] = array(
  628. 'primary' => $row['primary_group'],
  629. 'add' => $row['additional_groups'],
  630. );
  631. }
  632. // Add required information to email them.
  633. if ($row['notify_types'] != 4)
  634. $email_details[] = array(
  635. 'rid' => $row['id_request'],
  636. 'member_id' => $row['id_member'],
  637. 'member_name' => $row['member_name'],
  638. 'group_id' => $row['id_group'],
  639. 'group_name' => $row['group_name'],
  640. 'email' => $row['email_address'],
  641. 'language' => $row['lngfile'],
  642. );
  643. }
  644. $smcFunc['db_free_result']($request);
  645. // Remove the evidence...
  646. $smcFunc['db_query']('', '
  647. DELETE FROM {db_prefix}log_group_requests
  648. WHERE id_request IN ({array_int:request_list})',
  649. array(
  650. 'request_list' => $_POST['groupr'],
  651. )
  652. );
  653. // Ensure everyone who is online gets their changes right away.
  654. updateSettings(array('settings_updated' => time()));
  655. if (!empty($email_details))
  656. {
  657. require_once($sourcedir . '/Subs-Post.php');
  658. // They are being approved?
  659. if ($_POST['req_action'] == 'approve')
  660. {
  661. // Make the group changes.
  662. foreach ($group_changes as $id => $groups)
  663. {
  664. // Sanity check!
  665. foreach ($groups['add'] as $key => $value)
  666. if ($value == 0 || trim($value) == '')
  667. unset($groups['add'][$key]);
  668. $smcFunc['db_query']('', '
  669. UPDATE {db_prefix}members
  670. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  671. WHERE id_member = {int:selected_member}',
  672. array(
  673. 'primary_group' => $groups['primary'],
  674. 'selected_member' => $id,
  675. 'additional_groups' => implode(',', $groups['add']),
  676. )
  677. );
  678. }
  679. $lastLng = $user_info['language'];
  680. foreach ($email_details as $email)
  681. {
  682. $replacements = array(
  683. 'USERNAME' => $email['member_name'],
  684. 'GROUPNAME' => $email['group_name'],
  685. );
  686. $emaildata = loadEmailTemplate('mc_group_approve', $replacements, $email['language']);
  687. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  688. }
  689. }
  690. // Otherwise, they are getting rejected (With or without a reason).
  691. else
  692. {
  693. // Same as for approving, kind of.
  694. $lastLng = $user_info['language'];
  695. foreach ($email_details as $email)
  696. {
  697. $custom_reason = isset($_POST['groupreason']) && isset($_POST['groupreason'][$email['rid']]) ? $_POST['groupreason'][$email['rid']] : '';
  698. $replacements = array(
  699. 'USERNAME' => $email['member_name'],
  700. 'GROUPNAME' => $email['group_name'],
  701. );
  702. if (!empty($custom_reason))
  703. $replacements['REASON'] = $custom_reason;
  704. $emaildata = loadEmailTemplate(empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason', $replacements, $email['language']);
  705. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  706. }
  707. }
  708. }
  709. // Restore the current language.
  710. loadLanguage('ModerationCenter');
  711. }
  712. }
  713. // We're going to want this for making our list.
  714. require_once($sourcedir . '/Subs-List.php');
  715. // This is all the information required for a group listing.
  716. $listOptions = array(
  717. 'id' => 'group_request_list',
  718. 'title' => $txt['mc_group_requests'],
  719. 'width' => '100%',
  720. 'items_per_page' => $modSettings['defaultMaxMessages'],
  721. 'no_items_label' => $txt['mc_groupr_none_found'],
  722. 'base_href' => $scripturl . '?action=groups;sa=requests',
  723. 'default_sort_col' => 'member',
  724. 'get_items' => array(
  725. 'function' => 'list_getGroupRequests',
  726. 'params' => array(
  727. $where,
  728. $where_parameters,
  729. ),
  730. ),
  731. 'get_count' => array(
  732. 'function' => 'list_getGroupRequestCount',
  733. 'params' => array(
  734. $where,
  735. $where_parameters,
  736. ),
  737. ),
  738. 'columns' => array(
  739. 'member' => array(
  740. 'header' => array(
  741. 'value' => $txt['mc_groupr_member'],
  742. ),
  743. 'data' => array(
  744. 'db' => 'member_link',
  745. ),
  746. 'sort' => array(
  747. 'default' => 'mem.member_name',
  748. 'reverse' => 'mem.member_name DESC',
  749. ),
  750. ),
  751. 'group' => array(
  752. 'header' => array(
  753. 'value' => $txt['mc_groupr_group'],
  754. ),
  755. 'data' => array(
  756. 'db' => 'group_link',
  757. ),
  758. 'sort' => array(
  759. 'default' => 'mg.group_name',
  760. 'reverse' => 'mg.group_name DESC',
  761. ),
  762. ),
  763. 'reason' => array(
  764. 'header' => array(
  765. 'value' => $txt['mc_groupr_reason'],
  766. ),
  767. 'data' => array(
  768. 'db' => 'reason',
  769. ),
  770. ),
  771. 'action' => array(
  772. 'header' => array(
  773. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  774. 'style' => 'width: 4%;',
  775. ),
  776. 'data' => array(
  777. 'sprintf' => array(
  778. 'format' => '<input type="checkbox" name="groupr[]" value="%1$d" class="input_check" />',
  779. 'params' => array(
  780. 'id' => false,
  781. ),
  782. ),
  783. 'style' => 'text-align: center;',
  784. ),
  785. ),
  786. ),
  787. 'form' => array(
  788. 'href' => $scripturl . '?action=groups;sa=requests',
  789. 'include_sort' => true,
  790. 'include_start' => true,
  791. 'hidden_fields' => array(
  792. $context['session_var'] => $context['session_id'],
  793. ),
  794. ),
  795. 'additional_rows' => array(
  796. array(
  797. 'position' => 'bottom_of_list',
  798. 'value' => '
  799. <select name="req_action" onchange="if (this.value != 0 &amp;&amp; (this.value == \'reason\' || confirm(\'' . $txt['mc_groupr_warning'] . '\'))) this.form.submit();">
  800. <option value="0">' . $txt['with_selected'] . ':</option>
  801. <option value="0">---------------------</option>
  802. <option value="approve">' . $txt['mc_groupr_approve'] . '</option>
  803. <option value="reject">' . $txt['mc_groupr_reject'] . '</option>
  804. <option value="reason">' . $txt['mc_groupr_reject_w_reason'] . '</option>
  805. </select>
  806. <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" />',
  807. 'align' => 'right',
  808. ),
  809. ),
  810. );
  811. // Create the request list.
  812. createList($listOptions);
  813. $context['default_list'] = 'group_request_list';
  814. }
  815. /**
  816. * Callback function for createList().
  817. *
  818. * @param $where
  819. * @param $where_parameters
  820. * @return int, the count of group requests
  821. */
  822. function list_getGroupRequestCount($where, $where_parameters)
  823. {
  824. global $smcFunc;
  825. $request = $smcFunc['db_query']('', '
  826. SELECT COUNT(*)
  827. FROM {db_prefix}log_group_requests AS lgr
  828. WHERE ' . $where,
  829. array_merge($where_parameters, array(
  830. ))
  831. );
  832. list ($totalRequests) = $smcFunc['db_fetch_row']($request);
  833. $smcFunc['db_free_result']($request);
  834. return $totalRequests;
  835. }
  836. /**
  837. * Callback function for createList()
  838. *
  839. * @param int $start
  840. * @param int $items_per_page
  841. * @param string $sort
  842. * @param string $where
  843. * @param string $where_parameters
  844. * @return array, an array of group requests
  845. * Each group request has:
  846. * 'id'
  847. * 'member_link'
  848. * 'group_link'
  849. * 'reason'
  850. * 'time_submitted'
  851. */
  852. function list_getGroupRequests($start, $items_per_page, $sort, $where, $where_parameters)
  853. {
  854. global $smcFunc, $txt, $scripturl;
  855. $request = $smcFunc['db_query']('', '
  856. SELECT lgr.id_request, lgr.id_member, lgr.id_group, lgr.time_applied, lgr.reason,
  857. mem.member_name, mg.group_name, mg.online_color, mem.real_name
  858. FROM {db_prefix}log_group_requests AS lgr
  859. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  860. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  861. WHERE ' . $where . '
  862. ORDER BY {raw:sort}
  863. LIMIT ' . $start . ', ' . $items_per_page,
  864. array_merge($where_parameters, array(
  865. 'sort' => $sort,
  866. ))
  867. );
  868. $group_requests = array();
  869. while ($row = $smcFunc['db_fetch_assoc']($request))
  870. {
  871. $group_requests[] = array(
  872. 'id' => $row['id_request'],
  873. 'member_link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  874. 'group_link' => '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>',
  875. 'reason' => censorText($row['reason']),
  876. 'time_submitted' => timeformat($row['time_applied']),
  877. );
  878. }
  879. $smcFunc['db_free_result']($request);
  880. return $group_requests;
  881. }
  882. ?>