Subs-Membergroups.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 contains functions regarding manipulation of and information
  15. about membergroups.
  16. bool deleteMembergroups(array groups)
  17. - delete one of more membergroups.
  18. - requires the manage_membergroups permission.
  19. - returns true on success or false on failure.
  20. - has protection against deletion of protected membergroups.
  21. - deletes the permissions linked to the membergroup.
  22. - takes members out of the deleted membergroups.
  23. bool removeMembersFromGroups(array members, array groups = null)
  24. - remove one or more members from one or more membergroups.
  25. - requires the manage_membergroups permission.
  26. - returns true on success or false on failure.
  27. - if groups is null, the specified members are stripped from all their
  28. membergroups.
  29. - function includes a protection against removing from implicit groups.
  30. - non-admins are not able to remove members from the admin group.
  31. bool addMembersToGroup(array members, group, type = 'auto')
  32. - add one or more members to a specified group.
  33. - requires the manage_membergroups permission.
  34. - returns true on success or false on failure.
  35. - the type parameter specifies whether the group is added as primary or
  36. as additional group.
  37. - function has protection against adding members to implicit groups.
  38. - non-admins are not able to add members to the admin group.
  39. bool listMembergroupMembers_Href(&array members, int membergroup, int limit = null)
  40. - get a list of all members that are part of a membergroup.
  41. - if limit is set to null, all members are returned.
  42. - returns a list of href-links in $members.
  43. - returns true if there are more than limit members.
  44. */
  45. // Delete one or more membergroups.
  46. function deleteMembergroups($groups)
  47. {
  48. global $sourcedir, $smcFunc, $modSettings;
  49. // Make sure it's an array.
  50. if (!is_array($groups))
  51. $groups = array((int) $groups);
  52. else
  53. {
  54. $groups = array_unique($groups);
  55. // Make sure all groups are integer.
  56. foreach ($groups as $key => $value)
  57. $groups[$key] = (int) $value;
  58. }
  59. // Some groups are protected (guests, administrators, moderators, newbies).
  60. $protected_groups = array(-1, 0, 1, 3, 4);
  61. // There maybe some others as well.
  62. if (!allowedTo('admin_forum'))
  63. {
  64. $request = $smcFunc['db_query']('', '
  65. SELECT id_group
  66. FROM {db_prefix}membergroups
  67. WHERE group_type = {int:is_protected}',
  68. array(
  69. 'is_protected' => 1,
  70. )
  71. );
  72. while ($row = $smcFunc['db_fetch_assoc']($request))
  73. $protected_groups[] = $row['id_group'];
  74. $smcFunc['db_free_result']($request);
  75. }
  76. // Make sure they don't delete protected groups!
  77. $groups = array_diff($groups, array_unique($protected_groups));
  78. if (empty($groups))
  79. return false;
  80. // Log the deletion.
  81. $request = $smcFunc['db_query']('', '
  82. SELECT group_name
  83. FROM {db_prefix}membergroups
  84. WHERE id_group IN ({array_int:group_list})',
  85. array(
  86. 'group_list' => $groups,
  87. )
  88. );
  89. while ($row = $smcFunc['db_fetch_assoc']($request))
  90. logAction('delete_group', array('group' => $row['group_name']), 'admin');
  91. $smcFunc['db_free_result']($request);
  92. // Remove the membergroups themselves.
  93. $smcFunc['db_query']('', '
  94. DELETE FROM {db_prefix}membergroups
  95. WHERE id_group IN ({array_int:group_list})',
  96. array(
  97. 'group_list' => $groups,
  98. )
  99. );
  100. // Remove the permissions of the membergroups.
  101. $smcFunc['db_query']('', '
  102. DELETE FROM {db_prefix}permissions
  103. WHERE id_group IN ({array_int:group_list})',
  104. array(
  105. 'group_list' => $groups,
  106. )
  107. );
  108. $smcFunc['db_query']('', '
  109. DELETE FROM {db_prefix}board_permissions
  110. WHERE id_group IN ({array_int:group_list})',
  111. array(
  112. 'group_list' => $groups,
  113. )
  114. );
  115. $smcFunc['db_query']('', '
  116. DELETE FROM {db_prefix}group_moderators
  117. WHERE id_group IN ({array_int:group_list})',
  118. array(
  119. 'group_list' => $groups,
  120. )
  121. );
  122. // Delete any outstanding requests.
  123. $smcFunc['db_query']('', '
  124. DELETE FROM {db_prefix}log_group_requests
  125. WHERE id_group IN ({array_int:group_list})',
  126. array(
  127. 'group_list' => $groups,
  128. )
  129. );
  130. // Update the primary groups of members.
  131. $smcFunc['db_query']('', '
  132. UPDATE {db_prefix}members
  133. SET id_group = {int:regular_group}
  134. WHERE id_group IN ({array_int:group_list})',
  135. array(
  136. 'group_list' => $groups,
  137. 'regular_group' => 0,
  138. )
  139. );
  140. // Update any inherited groups (Lose inheritance).
  141. $smcFunc['db_query']('', '
  142. UPDATE {db_prefix}membergroups
  143. SET id_parent = {int:uninherited}
  144. WHERE id_parent IN ({array_int:group_list})',
  145. array(
  146. 'group_list' => $groups,
  147. 'uninherited' => -2,
  148. )
  149. );
  150. // Update the additional groups of members.
  151. $request = $smcFunc['db_query']('', '
  152. SELECT id_member, additional_groups
  153. FROM {db_prefix}members
  154. WHERE FIND_IN_SET({raw:additional_groups_explode}, additional_groups) != 0',
  155. array(
  156. 'additional_groups_explode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
  157. )
  158. );
  159. $updates = array();
  160. while ($row = $smcFunc['db_fetch_assoc']($request))
  161. $updates[$row['additional_groups']][] = $row['id_member'];
  162. $smcFunc['db_free_result']($request);
  163. foreach ($updates as $additional_groups => $memberArray)
  164. updateMemberData($memberArray, array('additional_groups' => implode(',', array_diff(explode(',', $additional_groups), $groups))));
  165. // No boards can provide access to these membergroups anymore.
  166. $request = $smcFunc['db_query']('', '
  167. SELECT id_board, member_groups
  168. FROM {db_prefix}boards
  169. WHERE FIND_IN_SET({raw:member_groups_explode}, member_groups) != 0',
  170. array(
  171. 'member_groups_explode' => implode(', member_groups) != 0 OR FIND_IN_SET(', $groups),
  172. )
  173. );
  174. $updates = array();
  175. while ($row = $smcFunc['db_fetch_assoc']($request))
  176. $updates[$row['member_groups']][] = $row['id_board'];
  177. $smcFunc['db_free_result']($request);
  178. foreach ($updates as $member_groups => $boardArray)
  179. $smcFunc['db_query']('', '
  180. UPDATE {db_prefix}boards
  181. SET member_groups = {string:member_groups}
  182. WHERE id_board IN ({array_int:board_lists})',
  183. array(
  184. 'board_lists' => $boardArray,
  185. 'member_groups' => implode(',', array_diff(explode(',', $member_groups), $groups)),
  186. )
  187. );
  188. // Recalculate the post groups, as they likely changed.
  189. updateStats('postgroups');
  190. // Make a note of the fact that the cache may be wrong.
  191. $settings_update = array('settings_updated' => time());
  192. // Have we deleted the spider group?
  193. if (isset($modSettings['spider_group']) && in_array($modSettings['spider_group'], $groups))
  194. $settings_update['spider_group'] = 0;
  195. updateSettings($settings_update);
  196. // It was a success.
  197. return true;
  198. }
  199. // Remove one or more members from one or more membergroups.
  200. function removeMembersFromGroups($members, $groups = null, $permissionCheckDone = false)
  201. {
  202. global $smcFunc, $user_info, $modSettings;
  203. // You're getting nowhere without this permission, unless of course you are the group's moderator.
  204. if (!$permissionCheckDone)
  205. isAllowedTo('manage_membergroups');
  206. // Assume something will happen.
  207. updateSettings(array('settings_updated' => time()));
  208. // Cleaning the input.
  209. if (!is_array($members))
  210. $members = array((int) $members);
  211. else
  212. {
  213. $members = array_unique($members);
  214. // Cast the members to integer.
  215. foreach ($members as $key => $value)
  216. $members[$key] = (int) $value;
  217. }
  218. // Before we get started, let's check we won't leave the admin group empty!
  219. if ($groups === null || $groups == 1 || (is_array($groups) && in_array(1, $groups)))
  220. {
  221. $admins = array();
  222. listMembergroupMembers_Href($admins, 1);
  223. // Remove any admins if there are too many.
  224. $non_changing_admins = array_diff(array_keys($admins), $members);
  225. if (empty($non_changing_admins))
  226. $members = array_diff($members, array_keys($admins));
  227. }
  228. // Just in case.
  229. if (empty($members))
  230. return false;
  231. elseif ($groups === null)
  232. {
  233. // Wanna remove all groups from these members? That's easy.
  234. $smcFunc['db_query']('', '
  235. UPDATE {db_prefix}members
  236. SET
  237. id_group = {int:regular_member},
  238. additional_groups = {string:blank_string}
  239. WHERE id_member IN ({array_int:member_list})' . (allowedTo('admin_forum') ? '' : '
  240. AND id_group != {int:admin_group}
  241. AND FIND_IN_SET({int:admin_group}, additional_groups) = 0'),
  242. array(
  243. 'member_list' => $members,
  244. 'regular_member' => 0,
  245. 'admin_group' => 1,
  246. 'blank_string' => '',
  247. )
  248. );
  249. updateStats('postgroups', $members);
  250. // Log what just happened.
  251. foreach ($members as $member)
  252. logAction('removed_all_groups', array('member' => $member), 'admin');
  253. return true;
  254. }
  255. elseif (!is_array($groups))
  256. $groups = array((int) $groups);
  257. else
  258. {
  259. $groups = array_unique($groups);
  260. // Make sure all groups are integer.
  261. foreach ($groups as $key => $value)
  262. $groups[$key] = (int) $value;
  263. }
  264. // Fetch a list of groups members cannot be assigned to explicitely, and the group names of the ones we want.
  265. $implicitGroups = array(-1, 0, 3);
  266. $request = $smcFunc['db_query']('', '
  267. SELECT id_group, group_name, min_posts
  268. FROM {db_prefix}membergroups
  269. WHERE id_group IN ({array_int:group_list})',
  270. array(
  271. 'group_list' => $groups,
  272. )
  273. );
  274. $group_names = array();
  275. while ($row = $smcFunc['db_fetch_assoc']($request))
  276. {
  277. if ($row['min_posts'] != -1)
  278. $implicitGroups[] = $row['id_group'];
  279. else
  280. $group_names[$row['id_group']] = $row['group_name'];
  281. }
  282. $smcFunc['db_free_result']($request);
  283. // Now get rid of those groups.
  284. $groups = array_diff($groups, $implicitGroups);
  285. // Don't forget the protected groups.
  286. if (!allowedTo('admin_forum'))
  287. {
  288. $request = $smcFunc['db_query']('', '
  289. SELECT id_group
  290. FROM {db_prefix}membergroups
  291. WHERE group_type = {int:is_protected}',
  292. array(
  293. 'is_protected' => 1,
  294. )
  295. );
  296. $protected_groups = array(1);
  297. while ($row = $smcFunc['db_fetch_assoc']($request))
  298. $protected_groups[] = $row['id_group'];
  299. $smcFunc['db_free_result']($request);
  300. // If you're not an admin yourself, you can't touch protected groups!
  301. $groups = array_diff($groups, array_unique($protected_groups));
  302. }
  303. // Only continue if there are still groups and members left.
  304. if (empty($groups) || empty($members))
  305. return false;
  306. // First, reset those who have this as their primary group - this is the easy one.
  307. $log_inserts = array();
  308. $request = $smcFunc['db_query']('', '
  309. SELECT id_member, id_group
  310. FROM {db_prefix}members AS members
  311. WHERE id_group IN ({array_int:group_list})
  312. AND id_member IN ({array_int:member_list})',
  313. array(
  314. 'group_list' => $groups,
  315. 'member_list' => $members,
  316. )
  317. );
  318. while ($row = $smcFunc['db_fetch_assoc']($request))
  319. $log_inserts[] = array(
  320. time(), 3, $user_info['id'], $user_info['ip'], 'removed_from_group',
  321. 0, 0, 0, serialize(array('group' => $group_names[$row['id_group']], 'member' => $row['id_member'])),
  322. );
  323. $smcFunc['db_free_result']($request);
  324. $smcFunc['db_query']('', '
  325. UPDATE {db_prefix}members
  326. SET id_group = {int:regular_member}
  327. WHERE id_group IN ({array_int:group_list})
  328. AND id_member IN ({array_int:member_list})',
  329. array(
  330. 'group_list' => $groups,
  331. 'member_list' => $members,
  332. 'regular_member' => 0,
  333. )
  334. );
  335. // Those who have it as part of their additional group must be updated the long way... sadly.
  336. $request = $smcFunc['db_query']('', '
  337. SELECT id_member, additional_groups
  338. FROM {db_prefix}members
  339. WHERE (FIND_IN_SET({raw:additional_groups_implode}, additional_groups) != 0)
  340. AND id_member IN ({array_int:member_list})
  341. LIMIT ' . count($members),
  342. array(
  343. 'member_list' => $members,
  344. 'additional_groups_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
  345. )
  346. );
  347. $updates = array();
  348. while ($row = $smcFunc['db_fetch_assoc']($request))
  349. {
  350. // What log entries must we make for this one, eh?
  351. foreach (explode(',', $row['additional_groups']) as $group)
  352. if (in_array($group, $groups))
  353. $log_inserts[] = array(
  354. time(), 3, $user_info['id'], $user_info['ip'], 'removed_from_group',
  355. 0, 0, 0, serialize(array('group' => $group_names[$group], 'member' => $row['id_member'])),
  356. );
  357. $updates[$row['additional_groups']][] = $row['id_member'];
  358. }
  359. $smcFunc['db_free_result']($request);
  360. foreach ($updates as $additional_groups => $memberArray)
  361. $smcFunc['db_query']('', '
  362. UPDATE {db_prefix}members
  363. SET additional_groups = {string:additional_groups}
  364. WHERE id_member IN ({array_int:member_list})',
  365. array(
  366. 'member_list' => $memberArray,
  367. 'additional_groups' => implode(',', array_diff(explode(',', $additional_groups), $groups)),
  368. )
  369. );
  370. // Their post groups may have changed now...
  371. updateStats('postgroups', $members);
  372. // Do the log.
  373. if (!empty($log_inserts) && !empty($modSettings['modlog_enabled']))
  374. $smcFunc['db_insert']('',
  375. '{db_prefix}log_actions',
  376. array(
  377. 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
  378. 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
  379. ),
  380. $log_inserts,
  381. array('id_action')
  382. );
  383. // Mission successful.
  384. return true;
  385. }
  386. // Add one or more members to a membergroup.
  387. /* Supported types:
  388. - only_primary - Assigns a membergroup as primary membergroup, but only
  389. if a member has not yet a primary membergroup assigned,
  390. unless the member is already part of the membergroup.
  391. - only_additional - Assigns a membergroup to the additional membergroups,
  392. unless the member is already part of the membergroup.
  393. - force_primary - Assigns a membergroup as primary membergroup no matter
  394. what the previous primary membergroup was.
  395. - auto - Assigns a membergroup to the primary group if it's still
  396. available. If not, assign it to the additional group. */
  397. function addMembersToGroup($members, $group, $type = 'auto', $permissionCheckDone = false)
  398. {
  399. global $smcFunc, $user_info, $modSettings;
  400. // Show your licence, but only if it hasn't been done yet.
  401. if (!$permissionCheckDone)
  402. isAllowedTo('manage_membergroups');
  403. // Make sure we don't keep old stuff cached.
  404. updateSettings(array('settings_updated' => time()));
  405. if (!is_array($members))
  406. $members = array((int) $members);
  407. else
  408. {
  409. $members = array_unique($members);
  410. // Make sure all members are integer.
  411. foreach ($members as $key => $value)
  412. $members[$key] = (int) $value;
  413. }
  414. $group = (int) $group;
  415. // Some groups just don't like explicitly having members.
  416. $implicitGroups = array(-1, 0, 3);
  417. $request = $smcFunc['db_query']('', '
  418. SELECT id_group, group_name, min_posts
  419. FROM {db_prefix}membergroups
  420. WHERE id_group = {int:current_group}',
  421. array(
  422. 'current_group' => $group,
  423. )
  424. );
  425. $group_names = array();
  426. while ($row = $smcFunc['db_fetch_assoc']($request))
  427. {
  428. if ($row['min_posts'] != -1)
  429. $implicitGroups[] = $row['id_group'];
  430. else
  431. $group_names[$row['id_group']] = $row['group_name'];
  432. }
  433. $smcFunc['db_free_result']($request);
  434. // Sorry, you can't join an implicit group.
  435. if (in_array($group, $implicitGroups) || empty($members))
  436. return false;
  437. // Only admins can add admins...
  438. if (!allowedTo('admin_forum') && $group == 1)
  439. return false;
  440. // ... and assign protected groups!
  441. elseif (!allowedTo('admin_forum'))
  442. {
  443. $request = $smcFunc['db_query']('', '
  444. SELECT group_type
  445. FROM {db_prefix}membergroups
  446. WHERE id_group = {int:current_group}
  447. LIMIT {int:limit}',
  448. array(
  449. 'current_group' => $group,
  450. 'limit' => 1,
  451. )
  452. );
  453. list ($is_protected) = $smcFunc['db_fetch_row']($request);
  454. $smcFunc['db_free_result']($request);
  455. // Is it protected?
  456. if ($is_protected == 1)
  457. return false;
  458. }
  459. // Do the actual updates.
  460. if ($type == 'only_additional')
  461. $smcFunc['db_query']('', '
  462. UPDATE {db_prefix}members
  463. SET additional_groups = CASE WHEN additional_groups = {string:blank_string} THEN {string:id_group_string} ELSE CONCAT(additional_groups, {string:id_group_string_extend}) END
  464. WHERE id_member IN ({array_int:member_list})
  465. AND id_group != {int:id_group}
  466. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  467. array(
  468. 'member_list' => $members,
  469. 'id_group' => $group,
  470. 'id_group_string' => (string) $group,
  471. 'id_group_string_extend' => ',' . $group,
  472. 'blank_string' => '',
  473. )
  474. );
  475. elseif ($type == 'only_primary' || $type == 'force_primary')
  476. $smcFunc['db_query']('', '
  477. UPDATE {db_prefix}members
  478. SET id_group = {int:id_group}
  479. WHERE id_member IN ({array_int:member_list})' . ($type == 'force_primary' ? '' : '
  480. AND id_group = {int:regular_group}
  481. AND FIND_IN_SET({int:id_group}, additional_groups) = 0'),
  482. array(
  483. 'member_list' => $members,
  484. 'id_group' => $group,
  485. 'regular_group' => 0,
  486. )
  487. );
  488. elseif ($type == 'auto')
  489. $smcFunc['db_query']('', '
  490. UPDATE {db_prefix}members
  491. SET
  492. id_group = CASE WHEN id_group = {int:regular_group} THEN {int:id_group} ELSE id_group END,
  493. additional_groups = CASE WHEN id_group = {int:id_group} THEN additional_groups
  494. WHEN additional_groups = {string:blank_string} THEN {string:id_group_string}
  495. ELSE CONCAT(additional_groups, {string:id_group_string_extend}) END
  496. WHERE id_member IN ({array_int:member_list})
  497. AND id_group != {int:id_group}
  498. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  499. array(
  500. 'member_list' => $members,
  501. 'regular_group' => 0,
  502. 'id_group' => $group,
  503. 'blank_string' => '',
  504. 'id_group_string' => (string) $group,
  505. 'id_group_string_extend' => ',' . $group,
  506. )
  507. );
  508. // Ack!!? What happened?
  509. else
  510. trigger_error('addMembersToGroup(): Unknown type \'' . $type . '\'', E_USER_WARNING);
  511. // Update their postgroup statistics.
  512. updateStats('postgroups', $members);
  513. // Log the data.
  514. $log_inserts = array();
  515. foreach ($members as $member)
  516. $log_inserts[] = array(
  517. time(), 3, $user_info['id'], $user_info['ip'], 'added_to_group',
  518. 0, 0, 0, serialize(array('group' => $group_names[$group], 'member' => $member)),
  519. );
  520. if (!empty($log_inserts) && !empty($modSettings['modlog_enabled']))
  521. $smcFunc['db_insert']('',
  522. '{db_prefix}log_actions',
  523. array(
  524. 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
  525. 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
  526. ),
  527. $log_inserts,
  528. array('id_action')
  529. );
  530. return true;
  531. }
  532. function listMembergroupMembers_Href(&$members, $membergroup, $limit = null)
  533. {
  534. global $scripturl, $txt, $smcFunc;
  535. $request = $smcFunc['db_query']('', '
  536. SELECT id_member, real_name
  537. FROM {db_prefix}members
  538. WHERE id_group = {int:id_group} OR FIND_IN_SET({int:id_group}, additional_groups) != 0' . ($limit === null ? '' : '
  539. LIMIT ' . ($limit + 1)),
  540. array(
  541. 'id_group' => $membergroup,
  542. )
  543. );
  544. $members = array();
  545. while ($row = $smcFunc['db_fetch_assoc']($request))
  546. $members[$row['id_member']] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
  547. $smcFunc['db_free_result']($request);
  548. // If there are more than $limit members, add a 'more' link.
  549. if ($limit !== null && count($members) > $limit)
  550. {
  551. array_pop($members);
  552. return true;
  553. }
  554. else
  555. return false;
  556. }
  557. // Retrieve a list of (visible) membergroups used by the cache.
  558. function cache_getMembergroupList()
  559. {
  560. global $scripturl, $smcFunc;
  561. $request = $smcFunc['db_query']('', '
  562. SELECT id_group, group_name, online_color
  563. FROM {db_prefix}membergroups
  564. WHERE min_posts = {int:min_posts}
  565. AND hidden = {int:not_hidden}
  566. AND id_group != {int:mod_group}
  567. AND online_color != {string:blank_string}
  568. ORDER BY group_name',
  569. array(
  570. 'min_posts' => -1,
  571. 'not_hidden' => 0,
  572. 'mod_group' => 3,
  573. 'blank_string' => '',
  574. )
  575. );
  576. $groupCache = array();
  577. while ($row = $smcFunc['db_fetch_assoc']($request))
  578. $groupCache[] = '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_group'] . '" ' . ($row['online_color'] ? 'style="color: ' . $row['online_color'] . '"' : '') . '>' . $row['group_name'] . '</a>';
  579. $smcFunc['db_free_result']($request);
  580. return array(
  581. 'data' => $groupCache,
  582. 'expires' => time() + 3600,
  583. 'refresh_eval' => 'return $GLOBALS[\'modSettings\'][\'settings_updated\'] > ' . time() . ';',
  584. );
  585. }
  586. function list_getMembergroups($start, $items_per_page, $sort, $membergroup_type)
  587. {
  588. global $txt, $scripturl, $context, $settings, $smcFunc;
  589. $groups = array();
  590. // Get the basic group data.
  591. $request = $smcFunc['db_query']('substring_membergroups', '
  592. SELECT id_group, group_name, min_posts, online_color, stars, 0 AS num_members
  593. FROM {db_prefix}membergroups
  594. WHERE min_posts ' . ($membergroup_type === 'post_count' ? '!=' : '=') . ' -1' . (allowedTo('admin_forum') ? '' : '
  595. AND group_type != {int:is_protected}') . '
  596. ORDER BY {raw:sort}',
  597. array(
  598. 'is_protected' => 1,
  599. 'sort' => $sort,
  600. )
  601. );
  602. while ($row = $smcFunc['db_fetch_assoc']($request))
  603. $groups[$row['id_group']] = array(
  604. 'id_group' => $row['id_group'],
  605. 'group_name' => $row['group_name'],
  606. 'min_posts' => $row['min_posts'],
  607. 'online_color' => $row['online_color'],
  608. 'stars' => $row['stars'],
  609. 'num_members' => $row['num_members'],
  610. );
  611. $smcFunc['db_free_result']($request);
  612. // If we found any membergroups, get the amount of members in them.
  613. if (!empty($groups))
  614. {
  615. if ($membergroup_type === 'post_count')
  616. {
  617. $query = $smcFunc['db_query']('', '
  618. SELECT id_post_group AS id_group, COUNT(*) AS num_members
  619. FROM {db_prefix}members
  620. WHERE id_post_group IN ({array_int:group_list})
  621. GROUP BY id_post_group',
  622. array(
  623. 'group_list' => array_keys($groups),
  624. )
  625. );
  626. while ($row = $smcFunc['db_fetch_assoc']($query))
  627. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  628. $smcFunc['db_free_result']($query);
  629. }
  630. else
  631. {
  632. $query = $smcFunc['db_query']('', '
  633. SELECT id_group, COUNT(*) AS num_members
  634. FROM {db_prefix}members
  635. WHERE id_group IN ({array_int:group_list})
  636. GROUP BY id_group',
  637. array(
  638. 'group_list' => array_keys($groups),
  639. )
  640. );
  641. while ($row = $smcFunc['db_fetch_assoc']($query))
  642. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  643. $smcFunc['db_free_result']($query);
  644. $query = $smcFunc['db_query']('', '
  645. SELECT mg.id_group, COUNT(*) AS num_members
  646. FROM {db_prefix}membergroups AS mg
  647. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_string}
  648. AND mem.id_group != mg.id_group
  649. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  650. WHERE mg.id_group IN ({array_int:group_list})
  651. GROUP BY mg.id_group',
  652. array(
  653. 'group_list' => array_keys($groups),
  654. 'blank_string' => '',
  655. )
  656. );
  657. while ($row = $smcFunc['db_fetch_assoc']($query))
  658. $groups[$row['id_group']]['num_members'] += $row['num_members'];
  659. $smcFunc['db_free_result']($query);
  660. }
  661. }
  662. // Apply manual sorting if the 'number of members' column is selected.
  663. if (substr($sort, 0, 1) == '1' || strpos($sort, ', 1') !== false)
  664. {
  665. $sort_ascending = strpos($sort, 'DESC') === false;
  666. foreach ($groups as $group)
  667. $sort_array[] = $group['id_group'] != 3 ? (int) $group['num_members'] : -1;
  668. array_multisort($sort_array, $sort_ascending ? SORT_ASC : SORT_DESC, SORT_REGULAR, $groups);
  669. }
  670. return $groups;
  671. }
  672. ?>