ManageMembergroups.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. <?php
  2. /**
  3. * This file is concerned with anything in the Manage Membergroups admin screen.
  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. * Main dispatcher, the entrance point for all 'Manage Membergroup' actions.
  18. * It forwards to a function based on the given subaction, default being subaction 'index', or, without manage_membergroup
  19. * permissions, then 'settings'.
  20. * Called by ?action=admin;area=membergroups.
  21. * Requires the manage_membergroups or the admin_forum permission.
  22. *
  23. * @uses ManageMembergroups template.
  24. * @uses ManageMembers language file.
  25. */
  26. function ModifyMembergroups()
  27. {
  28. global $context, $txt, $scripturl, $sourcedir;
  29. $subActions = array(
  30. 'add' => array('AddMembergroup', 'manage_membergroups'),
  31. 'delete' => array('DeleteMembergroup', 'manage_membergroups'),
  32. 'edit' => array('EditMembergroup', 'manage_membergroups'),
  33. 'index' => array('MembergroupIndex', 'manage_membergroups'),
  34. 'members' => array('MembergroupMembers', 'manage_membergroups', 'Groups.php'),
  35. 'settings' => array('ModifyMembergroupsettings', 'admin_forum'),
  36. );
  37. // Default to sub action 'index' or 'settings' depending on permissions.
  38. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (allowedTo('manage_membergroups') ? 'index' : 'settings');
  39. // Is it elsewhere?
  40. if (isset($subActions[$_REQUEST['sa']][2]))
  41. require_once($sourcedir . '/' . $subActions[$_REQUEST['sa']][2]);
  42. // Do the permission check, you might not be allowed her.
  43. isAllowedTo($subActions[$_REQUEST['sa']][1]);
  44. // Language and template stuff, the usual.
  45. loadLanguage('ManageMembers');
  46. loadTemplate('ManageMembergroups');
  47. // Setup the admin tabs.
  48. $context[$context['admin_menu_name']]['tab_data'] = array(
  49. 'title' => $txt['membergroups_title'],
  50. 'help' => 'membergroups',
  51. 'description' => $txt['membergroups_description'],
  52. );
  53. // Call the right function.
  54. $subActions[$_REQUEST['sa']][0]();
  55. }
  56. /**
  57. * Shows an overview of the current membergroups.
  58. * Called by ?action=admin;area=membergroups.
  59. * Requires the manage_membergroups permission.
  60. * Splits the membergroups in regular ones and post count based groups.
  61. * It also counts the number of members part of each membergroup.
  62. *
  63. * @uses ManageMembergroups template, main.
  64. */
  65. function MembergroupIndex()
  66. {
  67. global $txt, $scripturl, $context, $settings, $smcFunc, $sourcedir;
  68. $context['page_title'] = $txt['membergroups_title'];
  69. // The first list shows the regular membergroups.
  70. $listOptions = array(
  71. 'id' => 'regular_membergroups_list',
  72. 'title' => $txt['membergroups_regular'],
  73. 'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($_REQUEST['sort2']) ? ';sort2=' . urlencode($_REQUEST['sort2']) : ''),
  74. 'default_sort_col' => 'name',
  75. 'get_items' => array(
  76. 'file' => $sourcedir . '/Subs-Membergroups.php',
  77. 'function' => 'list_getMembergroups',
  78. 'params' => array(
  79. 'regular',
  80. ),
  81. ),
  82. 'columns' => array(
  83. 'name' => array(
  84. 'header' => array(
  85. 'value' => $txt['membergroups_name'],
  86. ),
  87. 'data' => array(
  88. 'function' => create_function('$rowData', '
  89. global $scripturl;
  90. // Since the moderator group has no explicit members, no link is needed.
  91. if ($rowData[\'id_group\'] == 3)
  92. $group_name = $rowData[\'group_name\'];
  93. else
  94. {
  95. $color_style = empty($rowData[\'online_color\']) ? \'\' : sprintf(\' style="color: %1$s;"\', $rowData[\'online_color\']);
  96. $group_name = sprintf(\'<a href="%1$s?action=admin;area=membergroups;sa=members;group=%2$d"%3$s>%4$s</a>\', $scripturl, $rowData[\'id_group\'], $color_style, $rowData[\'group_name\']);
  97. }
  98. // Add a help option for moderator and administrator.
  99. if ($rowData[\'id_group\'] == 1)
  100. $group_name .= sprintf(\' (<a href="%1$s?action=helpadmin;help=membergroup_administrator" onclick="return reqWin(this.href);">?</a>)\', $scripturl);
  101. elseif ($rowData[\'id_group\'] == 3)
  102. $group_name .= sprintf(\' (<a href="%1$s?action=helpadmin;help=membergroup_moderator" onclick="return reqWin(this.href);">?</a>)\', $scripturl);
  103. return $group_name;
  104. '),
  105. ),
  106. 'sort' => array(
  107. 'default' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, group_name',
  108. 'reverse' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, group_name DESC',
  109. ),
  110. ),
  111. 'stars' => array(
  112. 'header' => array(
  113. 'value' => $txt['membergroups_stars'],
  114. ),
  115. 'data' => array(
  116. 'function' => create_function('$rowData', '
  117. global $settings;
  118. $stars = explode(\'#\', $rowData[\'stars\']);
  119. // In case no stars are setup, return with nothing
  120. if (empty($stars[0]) || empty($stars[1]))
  121. return \'\';
  122. // Otherwise repeat the image a given number of times.
  123. else
  124. {
  125. $image = sprintf(\'<img src="%1$s/%2$s" alt="*" />\', $settings[\'images_url\'], $stars[1]);
  126. return str_repeat($image, $stars[0]);
  127. }
  128. '),
  129. ),
  130. 'sort' => array(
  131. 'default' => 'stars',
  132. 'reverse' => 'stars DESC',
  133. )
  134. ),
  135. 'members' => array(
  136. 'header' => array(
  137. 'value' => $txt['membergroups_members_top'],
  138. ),
  139. 'data' => array(
  140. 'function' => create_function('$rowData', '
  141. global $txt;
  142. // No explicit members for the moderator group.
  143. return $rowData[\'id_group\'] == 3 ? $txt[\'membergroups_guests_na\'] : $rowData[\'num_members\'];
  144. '),
  145. 'style' => 'text-align: center',
  146. ),
  147. 'sort' => array(
  148. 'default' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, 1',
  149. 'reverse' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, 1 DESC',
  150. ),
  151. ),
  152. 'modify' => array(
  153. 'header' => array(
  154. 'value' => $txt['modify'],
  155. ),
  156. 'data' => array(
  157. 'sprintf' => array(
  158. 'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
  159. 'params' => array(
  160. 'id_group' => false,
  161. ),
  162. ),
  163. 'style' => 'text-align: center',
  164. ),
  165. ),
  166. ),
  167. 'additional_rows' => array(
  168. array(
  169. 'position' => 'below_table_data',
  170. 'value' => '[<a href="' . $scripturl . '?action=admin;area=membergroups;sa=add;generalgroup">' . $txt['membergroups_add_group'] . '</a>]',
  171. ),
  172. ),
  173. );
  174. require_once($sourcedir . '/Subs-List.php');
  175. createList($listOptions);
  176. // The second list shows the post count based groups.
  177. $listOptions = array(
  178. 'id' => 'post_count_membergroups_list',
  179. 'title' => $txt['membergroups_post'],
  180. 'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($_REQUEST['sort']) ? ';sort=' . urlencode($_REQUEST['sort']) : ''),
  181. 'default_sort_col' => 'required_posts',
  182. 'request_vars' => array(
  183. 'sort' => 'sort2',
  184. 'desc' => 'desc2',
  185. ),
  186. 'get_items' => array(
  187. 'file' => $sourcedir . '/Subs-Membergroups.php',
  188. 'function' => 'list_getMembergroups',
  189. 'params' => array(
  190. 'post_count',
  191. ),
  192. ),
  193. 'columns' => array(
  194. 'name' => array(
  195. 'header' => array(
  196. 'value' => $txt['membergroups_name'],
  197. ),
  198. 'data' => array(
  199. 'function' => create_function('$rowData', '
  200. global $scripturl;
  201. $colorStyle = empty($rowData[\'online_color\']) ? \'\' : sprintf(\' style="color: %1$s;"\', $rowData[\'online_color\']);
  202. return sprintf(\'<a href="%1$s?action=moderate;area=viewgroups;sa=members;group=%2$d"%3$s>%4$s</a>\', $scripturl, $rowData[\'id_group\'], $colorStyle, $rowData[\'group_name\']);
  203. '),
  204. ),
  205. 'sort' => array(
  206. 'default' => 'group_name',
  207. 'reverse' => 'group_name DESC',
  208. ),
  209. ),
  210. 'stars' => array(
  211. 'header' => array(
  212. 'value' => $txt['membergroups_stars'],
  213. ),
  214. 'data' => array(
  215. 'function' => create_function('$rowData', '
  216. global $settings;
  217. $stars = explode(\'#\', $rowData[\'stars\']);
  218. if (empty($stars[0]) || empty($stars[1]))
  219. return \'\';
  220. else
  221. {
  222. $star_image = sprintf(\'<img src="%1$s/%2$s" alt="*" />\', $settings[\'images_url\'], $stars[1]);
  223. return str_repeat($star_image, $stars[0]);
  224. }
  225. '),
  226. ),
  227. 'sort' => array(
  228. 'default' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, stars',
  229. 'reverse' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, stars DESC',
  230. )
  231. ),
  232. 'members' => array(
  233. 'header' => array(
  234. 'value' => $txt['membergroups_members_top'],
  235. ),
  236. 'data' => array(
  237. 'db' => 'num_members',
  238. 'style' => 'text-align: center',
  239. ),
  240. 'sort' => array(
  241. 'default' => '1 DESC',
  242. 'reverse' => '1',
  243. ),
  244. ),
  245. 'required_posts' => array(
  246. 'header' => array(
  247. 'value' => $txt['membergroups_min_posts'],
  248. ),
  249. 'data' => array(
  250. 'db' => 'min_posts',
  251. 'style' => 'text-align: center',
  252. ),
  253. 'sort' => array(
  254. 'default' => 'min_posts',
  255. 'reverse' => 'min_posts DESC',
  256. ),
  257. ),
  258. 'modify' => array(
  259. 'header' => array(
  260. 'value' => $txt['modify'],
  261. ),
  262. 'data' => array(
  263. 'sprintf' => array(
  264. 'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
  265. 'params' => array(
  266. 'id_group' => false,
  267. ),
  268. ),
  269. 'style' => 'text-align: center',
  270. ),
  271. ),
  272. ),
  273. 'additional_rows' => array(
  274. array(
  275. 'position' => 'below_table_data',
  276. 'value' => '[<a href="' . $scripturl . '?action=admin;area=membergroups;sa=add;postgroup">' . $txt['membergroups_add_group'] . '</a>]',
  277. ),
  278. ),
  279. );
  280. createList($listOptions);
  281. }
  282. /**
  283. * This function handles adding a membergroup and setting some initial properties.
  284. * Called by ?action=admin;area=membergroups;sa=add.
  285. * It requires the manage_membergroups permission.
  286. * Allows to use a predefined permission profile or copy one from another group.
  287. * Redirects to action=admin;area=membergroups;sa=edit;group=x.
  288. *
  289. * @uses the new_group sub template of ManageMembergroups.
  290. */
  291. function AddMembergroup()
  292. {
  293. global $context, $txt, $sourcedir, $modSettings, $smcFunc;
  294. // A form was submitted, we can start adding.
  295. if (!empty($_POST['group_name']))
  296. {
  297. checkSession();
  298. validateToken('admin-mmg');
  299. $postCountBasedGroup = isset($_POST['min_posts']) && (!isset($_POST['postgroup_based']) || !empty($_POST['postgroup_based']));
  300. $_POST['group_type'] = !isset($_POST['group_type']) || $_POST['group_type'] < 0 || $_POST['group_type'] > 3 || ($_POST['group_type'] == 1 && !allowedTo('admin_forum')) ? 0 : (int) $_POST['group_type'];
  301. /**
  302. * @todo Check for members with same name too?
  303. */
  304. $request = $smcFunc['db_query']('', '
  305. SELECT MAX(id_group)
  306. FROM {db_prefix}membergroups',
  307. array(
  308. )
  309. );
  310. list ($id_group) = $smcFunc['db_fetch_row']($request);
  311. $smcFunc['db_free_result']($request);
  312. $id_group++;
  313. $smcFunc['db_insert']('',
  314. '{db_prefix}membergroups',
  315. array(
  316. 'id_group' => 'int', 'description' => 'string', 'group_name' => 'string-80', 'min_posts' => 'int',
  317. 'stars' => 'string', 'online_color' => 'string', 'group_type' => 'int',
  318. ),
  319. array(
  320. $id_group, '', $_POST['group_name'], ($postCountBasedGroup ? (int) $_POST['min_posts'] : '-1'),
  321. '1#star.gif', '', $_POST['group_type'],
  322. ),
  323. array('id_group')
  324. );
  325. // Update the post groups now, if this is a post group!
  326. if (isset($_POST['min_posts']))
  327. updateStats('postgroups');
  328. // You cannot set permissions for post groups if they are disabled.
  329. if ($postCountBasedGroup && empty($modSettings['permission_enable_postgroups']))
  330. $_POST['perm_type'] = '';
  331. if ($_POST['perm_type'] == 'predefined')
  332. {
  333. // Set default permission level.
  334. require_once($sourcedir . '/ManagePermissions.php');
  335. setPermissionLevel($_POST['level'], $id_group, 'null');
  336. }
  337. // Copy or inherit the permissions!
  338. elseif ($_POST['perm_type'] == 'copy' || $_POST['perm_type'] == 'inherit')
  339. {
  340. $copy_id = $_POST['perm_type'] == 'copy' ? (int) $_POST['copyperm'] : (int) $_POST['inheritperm'];
  341. // Are you a powerful admin?
  342. if (!allowedTo('admin_forum'))
  343. {
  344. $request = $smcFunc['db_query']('', '
  345. SELECT group_type
  346. FROM {db_prefix}membergroups
  347. WHERE id_group = {int:copy_from}
  348. LIMIT {int:limit}',
  349. array(
  350. 'copy_from' => $copy_id,
  351. 'limit' => 1,
  352. )
  353. );
  354. list ($copy_type) = $smcFunc['db_fetch_row']($request);
  355. $smcFunc['db_free_result']($request);
  356. // Protected groups are... well, protected!
  357. if ($copy_type == 1)
  358. fatal_lang_error('membergroup_does_not_exist');
  359. }
  360. // Don't allow copying of a real priviledged person!
  361. require_once($sourcedir . '/ManagePermissions.php');
  362. loadIllegalPermissions();
  363. $request = $smcFunc['db_query']('', '
  364. SELECT permission, add_deny
  365. FROM {db_prefix}permissions
  366. WHERE id_group = {int:copy_from}',
  367. array(
  368. 'copy_from' => $copy_id,
  369. )
  370. );
  371. $inserts = array();
  372. while ($row = $smcFunc['db_fetch_assoc']($request))
  373. {
  374. if (empty($context['illegal_permissions']) || !in_array($row['permission'], $context['illegal_permissions']))
  375. $inserts[] = array($id_group, $row['permission'], $row['add_deny']);
  376. }
  377. $smcFunc['db_free_result']($request);
  378. if (!empty($inserts))
  379. $smcFunc['db_insert']('insert',
  380. '{db_prefix}permissions',
  381. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  382. $inserts,
  383. array('id_group', 'permission')
  384. );
  385. $request = $smcFunc['db_query']('', '
  386. SELECT id_profile, permission, add_deny
  387. FROM {db_prefix}board_permissions
  388. WHERE id_group = {int:copy_from}',
  389. array(
  390. 'copy_from' => $copy_id,
  391. )
  392. );
  393. $inserts = array();
  394. while ($row = $smcFunc['db_fetch_assoc']($request))
  395. $inserts[] = array($id_group, $row['id_profile'], $row['permission'], $row['add_deny']);
  396. $smcFunc['db_free_result']($request);
  397. if (!empty($inserts))
  398. $smcFunc['db_insert']('insert',
  399. '{db_prefix}board_permissions',
  400. array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  401. $inserts,
  402. array('id_group', 'id_profile', 'permission')
  403. );
  404. // Also get some membergroup information if we're copying and not copying from guests...
  405. if ($copy_id > 0 && $_POST['perm_type'] == 'copy')
  406. {
  407. $request = $smcFunc['db_query']('', '
  408. SELECT online_color, max_messages, stars
  409. FROM {db_prefix}membergroups
  410. WHERE id_group = {int:copy_from}
  411. LIMIT 1',
  412. array(
  413. 'copy_from' => $copy_id,
  414. )
  415. );
  416. $group_info = $smcFunc['db_fetch_assoc']($request);
  417. $smcFunc['db_free_result']($request);
  418. // ...and update the new membergroup with it.
  419. $smcFunc['db_query']('', '
  420. UPDATE {db_prefix}membergroups
  421. SET
  422. online_color = {string:online_color},
  423. max_messages = {int:max_messages},
  424. stars = {string:stars}
  425. WHERE id_group = {int:current_group}',
  426. array(
  427. 'max_messages' => $group_info['max_messages'],
  428. 'current_group' => $id_group,
  429. 'online_color' => $group_info['online_color'],
  430. 'stars' => $group_info['stars'],
  431. )
  432. );
  433. }
  434. // If inheriting say so...
  435. elseif ($_POST['perm_type'] == 'inherit')
  436. {
  437. $smcFunc['db_query']('', '
  438. UPDATE {db_prefix}membergroups
  439. SET id_parent = {int:copy_from}
  440. WHERE id_group = {int:current_group}',
  441. array(
  442. 'copy_from' => $copy_id,
  443. 'current_group' => $id_group,
  444. )
  445. );
  446. }
  447. }
  448. // Make sure all boards selected are stored in a proper array.
  449. $_POST['boardaccess'] = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
  450. foreach ($_POST['boardaccess'] as $key => $value)
  451. $_POST['boardaccess'][$key] = (int) $value;
  452. // Only do this if they have special access requirements.
  453. if (!empty($_POST['boardaccess']))
  454. $smcFunc['db_query']('', '
  455. UPDATE {db_prefix}boards
  456. SET member_groups = CASE WHEN member_groups = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT(member_groups, {string:comma_group}) END
  457. WHERE id_board IN ({array_int:board_list})',
  458. array(
  459. 'board_list' => $_POST['boardaccess'],
  460. 'blank_string' => '',
  461. 'group_id_string' => (string) $id_group,
  462. 'comma_group' => ',' . $id_group,
  463. )
  464. );
  465. // If this is joinable then set it to show group membership in people's profiles.
  466. if (empty($modSettings['show_group_membership']) && $_POST['group_type'] > 1)
  467. updateSettings(array('show_group_membership' => 1));
  468. // Rebuild the group cache.
  469. updateSettings(array(
  470. 'settings_updated' => time(),
  471. ));
  472. // We did it.
  473. logAction('add_group', array('group' => $_POST['group_name']), 'admin');
  474. // Go change some more settings.
  475. redirectexit('action=admin;area=membergroups;sa=edit;group=' . $id_group);
  476. }
  477. // Just show the 'add membergroup' screen.
  478. $context['page_title'] = $txt['membergroups_new_group'];
  479. $context['sub_template'] = 'new_group';
  480. $context['post_group'] = isset($_REQUEST['postgroup']);
  481. $context['undefined_group'] = !isset($_REQUEST['postgroup']) && !isset($_REQUEST['generalgroup']);
  482. $context['allow_protected'] = allowedTo('admin_forum');
  483. $result = $smcFunc['db_query']('', '
  484. SELECT id_group, group_name
  485. FROM {db_prefix}membergroups
  486. WHERE (id_group > {int:moderator_group} OR id_group = {int:global_mod_group})' . (empty($modSettings['permission_enable_postgroups']) ? '
  487. AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
  488. AND group_type != {int:is_protected}') . '
  489. ORDER BY min_posts, id_group != {int:global_mod_group}, group_name',
  490. array(
  491. 'moderator_group' => 3,
  492. 'global_mod_group' => 2,
  493. 'min_posts' => -1,
  494. 'is_protected' => 1,
  495. )
  496. );
  497. $context['groups'] = array();
  498. while ($row = $smcFunc['db_fetch_assoc']($result))
  499. $context['groups'][] = array(
  500. 'id' => $row['id_group'],
  501. 'name' => $row['group_name']
  502. );
  503. $smcFunc['db_free_result']($result);
  504. $result = $smcFunc['db_query']('', '
  505. SELECT id_board, name, child_level
  506. FROM {db_prefix}boards
  507. ORDER BY board_order',
  508. array(
  509. )
  510. );
  511. $context['boards'] = array();
  512. while ($row = $smcFunc['db_fetch_assoc']($result))
  513. $context['boards'][] = array(
  514. 'id' => $row['id_board'],
  515. 'name' => $row['name'],
  516. 'child_level' => $row['child_level'],
  517. 'selected' => false
  518. );
  519. $smcFunc['db_free_result']($result);
  520. createToken('admin-mmg');
  521. }
  522. /**
  523. * Deleting a membergroup by URL (not implemented).
  524. * Called by ?action=admin;area=membergroups;sa=delete;group=x;session_var=y.
  525. * Requires the manage_membergroups permission.
  526. * Redirects to ?action=admin;area=membergroups.
  527. *
  528. * @todo look at this
  529. */
  530. function DeleteMembergroup()
  531. {
  532. global $sourcedir;
  533. checkSession('get');
  534. require_once($sourcedir . '/Subs-Membergroups.php');
  535. deleteMembergroups((int) $_REQUEST['group']);
  536. // Go back to the membergroup index.
  537. redirectexit('action=admin;area=membergroups;');
  538. }
  539. /**
  540. * Editing a membergroup.
  541. * Screen to edit a specific membergroup.
  542. * Called by ?action=admin;area=membergroups;sa=edit;group=x.
  543. * It requires the manage_membergroups permission.
  544. * Also handles the delete button of the edit form.
  545. * Redirects to ?action=admin;area=membergroups.
  546. *
  547. * @uses the edit_group sub template of ManageMembergroups.
  548. */
  549. function EditMembergroup()
  550. {
  551. global $context, $txt, $sourcedir, $modSettings, $smcFunc;
  552. $_REQUEST['group'] = isset($_REQUEST['group']) && $_REQUEST['group'] > 0 ? (int) $_REQUEST['group'] : 0;
  553. // Make sure this group is editable.
  554. if (!empty($_REQUEST['group']))
  555. {
  556. $request = $smcFunc['db_query']('', '
  557. SELECT id_group
  558. FROM {db_prefix}membergroups
  559. WHERE id_group = {int:current_group}' . (allowedTo('admin_forum') ? '' : '
  560. AND group_type != {int:is_protected}') . '
  561. LIMIT {int:limit}',
  562. array(
  563. 'current_group' => $_REQUEST['group'],
  564. 'is_protected' => 1,
  565. 'limit' => 1,
  566. )
  567. );
  568. list ($_REQUEST['group']) = $smcFunc['db_fetch_row']($request);
  569. $smcFunc['db_free_result']($request);
  570. }
  571. // Now, do we have a valid id?
  572. if (empty($_REQUEST['group']))
  573. fatal_lang_error('membergroup_does_not_exist', false);
  574. // The delete this membergroup button was pressed.
  575. if (isset($_POST['delete']))
  576. {
  577. checkSession();
  578. validateToken('admin-mmg');
  579. require_once($sourcedir . '/Subs-Membergroups.php');
  580. deleteMembergroups($_REQUEST['group']);
  581. redirectexit('action=admin;area=membergroups;');
  582. }
  583. // A form was submitted with the new membergroup settings.
  584. elseif (isset($_POST['submit']))
  585. {
  586. // Validate the session.
  587. checkSession();
  588. validateToken('admin-mmg');
  589. // Can they really inherit from this group?
  590. if ($_POST['group_inherit'] != -2 && !allowedTo('admin_forum'))
  591. {
  592. $request = $smcFunc['db_query']('', '
  593. SELECT group_type
  594. FROM {db_prefix}membergroups
  595. WHERE id_group = {int:inherit_from}
  596. LIMIT {int:limit}',
  597. array(
  598. 'inherit_from' => $_POST['group_inherit'],
  599. 'limit' => 1,
  600. )
  601. );
  602. list ($inherit_type) = $smcFunc['db_fetch_row']($request);
  603. $smcFunc['db_free_result']($request);
  604. }
  605. // Set variables to their proper value.
  606. $_POST['max_messages'] = isset($_POST['max_messages']) ? (int) $_POST['max_messages'] : 0;
  607. $_POST['min_posts'] = isset($_POST['min_posts']) && isset($_POST['group_type']) && $_POST['group_type'] == -1 && $_REQUEST['group'] > 3 ? abs($_POST['min_posts']) : ($_REQUEST['group'] == 4 ? 0 : -1);
  608. $_POST['stars'] = (empty($_POST['star_count']) || $_POST['star_count'] < 0) ? '' : min((int) $_POST['star_count'], 99) . '#' . $_POST['star_image'];
  609. $_POST['group_desc'] = isset($_POST['group_desc']) && ($_REQUEST['group'] == 1 || (isset($_POST['group_type']) && $_POST['group_type'] != -1)) ? trim($_POST['group_desc']) : '';
  610. $_POST['group_type'] = !isset($_POST['group_type']) || $_POST['group_type'] < 0 || $_POST['group_type'] > 3 || ($_POST['group_type'] == 1 && !allowedTo('admin_forum')) ? 0 : (int) $_POST['group_type'];
  611. $_POST['group_hidden'] = empty($_POST['group_hidden']) || $_POST['min_posts'] != -1 || $_REQUEST['group'] == 3 ? 0 : (int) $_POST['group_hidden'];
  612. $_POST['group_inherit'] = $_REQUEST['group'] > 1 && $_REQUEST['group'] != 3 && (empty($inherit_type) || $inherit_type != 1) ? (int) $_POST['group_inherit'] : -2;
  613. /**
  614. * @todo Don't set online_color for the Moderators group?
  615. */
  616. // Do the update of the membergroup settings.
  617. $smcFunc['db_query']('', '
  618. UPDATE {db_prefix}membergroups
  619. SET group_name = {string:group_name}, online_color = {string:online_color},
  620. max_messages = {int:max_messages}, min_posts = {int:min_posts}, stars = {string:stars},
  621. description = {string:group_desc}, group_type = {int:group_type}, hidden = {int:group_hidden},
  622. id_parent = {int:group_inherit}
  623. WHERE id_group = {int:current_group}',
  624. array(
  625. 'max_messages' => $_POST['max_messages'],
  626. 'min_posts' => $_POST['min_posts'],
  627. 'group_type' => $_POST['group_type'],
  628. 'group_hidden' => $_POST['group_hidden'],
  629. 'group_inherit' => $_POST['group_inherit'],
  630. 'current_group' => (int) $_REQUEST['group'],
  631. 'group_name' => $_POST['group_name'],
  632. 'online_color' => $_POST['online_color'],
  633. 'stars' => $_POST['stars'],
  634. 'group_desc' => $_POST['group_desc'],
  635. )
  636. );
  637. // Time to update the boards this membergroup has access to.
  638. if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
  639. {
  640. $_POST['boardaccess'] = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
  641. foreach ($_POST['boardaccess'] as $key => $value)
  642. $_POST['boardaccess'][$key] = (int) $value;
  643. // Find all board this group is in, but shouldn't be in.
  644. $request = $smcFunc['db_query']('', '
  645. SELECT id_board, member_groups
  646. FROM {db_prefix}boards
  647. WHERE FIND_IN_SET({string:current_group}, member_groups) != 0' . (empty($_POST['boardaccess']) ? '' : '
  648. AND id_board NOT IN ({array_int:board_access_list})'),
  649. array(
  650. 'current_group' => (int) $_REQUEST['group'],
  651. 'board_access_list' => $_POST['boardaccess'],
  652. )
  653. );
  654. while ($row = $smcFunc['db_fetch_assoc']($request))
  655. $smcFunc['db_query']('', '
  656. UPDATE {db_prefix}boards
  657. SET member_groups = {string:member_group_access}
  658. WHERE id_board = {int:current_board}',
  659. array(
  660. 'current_board' => $row['id_board'],
  661. 'member_group_access' => implode(',', array_diff(explode(',', $row['member_groups']), array($_REQUEST['group']))),
  662. )
  663. );
  664. $smcFunc['db_free_result']($request);
  665. // Add the membergroup to all boards that hadn't been set yet.
  666. if (!empty($_POST['boardaccess']))
  667. $smcFunc['db_query']('', '
  668. UPDATE {db_prefix}boards
  669. SET member_groups = CASE WHEN member_groups = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT(member_groups, {string:comma_group}) END
  670. WHERE id_board IN ({array_int:board_list})
  671. AND FIND_IN_SET({int:current_group}, member_groups) = 0',
  672. array(
  673. 'board_list' => $_POST['boardaccess'],
  674. 'blank_string' => '',
  675. 'current_group' => (int) $_REQUEST['group'],
  676. 'group_id_string' => (string) (int) $_REQUEST['group'],
  677. 'comma_group' => ',' . $_REQUEST['group'],
  678. )
  679. );
  680. }
  681. // Remove everyone from this group!
  682. if ($_POST['min_posts'] != -1)
  683. {
  684. $smcFunc['db_query']('', '
  685. UPDATE {db_prefix}members
  686. SET id_group = {int:regular_member}
  687. WHERE id_group = {int:current_group}',
  688. array(
  689. 'regular_member' => 0,
  690. 'current_group' => (int) $_REQUEST['group'],
  691. )
  692. );
  693. $request = $smcFunc['db_query']('', '
  694. SELECT id_member, additional_groups
  695. FROM {db_prefix}members
  696. WHERE FIND_IN_SET({string:current_group}, additional_groups) != 0',
  697. array(
  698. 'current_group' => (int) $_REQUEST['group'],
  699. )
  700. );
  701. $updates = array();
  702. while ($row = $smcFunc['db_fetch_assoc']($request))
  703. $updates[$row['additional_groups']][] = $row['id_member'];
  704. $smcFunc['db_free_result']($request);
  705. foreach ($updates as $additional_groups => $memberArray)
  706. updateMemberData($memberArray, array('additional_groups' => implode(',', array_diff(explode(',', $additional_groups), array((int) $_REQUEST['group'])))));
  707. }
  708. elseif ($_REQUEST['group'] != 3)
  709. {
  710. // Making it a hidden group? If so remove everyone with it as primary group (Actually, just make them additional).
  711. if ($_POST['group_hidden'] == 2)
  712. {
  713. $request = $smcFunc['db_query']('', '
  714. SELECT id_member, additional_groups
  715. FROM {db_prefix}members
  716. WHERE id_group = {int:current_group}
  717. AND FIND_IN_SET({int:current_group}, additional_groups) = 0',
  718. array(
  719. 'current_group' => (int) $_REQUEST['group'],
  720. )
  721. );
  722. $updates = array();
  723. while ($row = $smcFunc['db_fetch_assoc']($request))
  724. $updates[$row['additional_groups']][] = $row['id_member'];
  725. $smcFunc['db_free_result']($request);
  726. foreach ($updates as $additional_groups => $memberArray)
  727. updateMemberData($memberArray, array('additional_groups' => implode(',', array_merge(explode(',', $additional_groups), array((int) $_REQUEST['group'])))));
  728. $smcFunc['db_query']('', '
  729. UPDATE {db_prefix}members
  730. SET id_group = {int:regular_member}
  731. WHERE id_group = {int:current_group}',
  732. array(
  733. 'regular_member' => 0,
  734. 'current_group' => $_REQUEST['group'],
  735. )
  736. );
  737. }
  738. // Either way, let's check our "show group membership" setting is correct.
  739. $request = $smcFunc['db_query']('', '
  740. SELECT COUNT(*)
  741. FROM {db_prefix}membergroups
  742. WHERE group_type > {int:non_joinable}',
  743. array(
  744. 'non_joinable' => 1,
  745. )
  746. );
  747. list ($have_joinable) = $smcFunc['db_fetch_row']($request);
  748. $smcFunc['db_free_result']($request);
  749. // Do we need to update the setting?
  750. if ((empty($modSettings['show_group_membership']) && $have_joinable) || (!empty($modSettings['show_group_membership']) && !$have_joinable))
  751. updateSettings(array('show_group_membership' => $have_joinable ? 1 : 0));
  752. }
  753. // Do we need to set inherited permissions?
  754. if ($_POST['group_inherit'] != -2 && $_POST['group_inherit'] != $_POST['old_inherit'])
  755. {
  756. require_once($sourcedir . '/ManagePermissions.php');
  757. updateChildPermissions($_POST['group_inherit']);
  758. }
  759. // Finally, moderators!
  760. $moderator_string = isset($_POST['group_moderators']) ? trim($_POST['group_moderators']) : '';
  761. $smcFunc['db_query']('', '
  762. DELETE FROM {db_prefix}group_moderators
  763. WHERE id_group = {int:current_group}',
  764. array(
  765. 'current_group' => $_REQUEST['group'],
  766. )
  767. );
  768. if ((!empty($moderator_string) || !empty($_POST['moderator_list'])) && $_POST['min_posts'] == -1 && $_REQUEST['group'] != 3)
  769. {
  770. // Get all the usernames from the string
  771. if (!empty($moderator_string))
  772. {
  773. $moderator_string = strtr(preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', htmlspecialchars($moderator_string), ENT_QUOTES), array('&quot;' => '"'));
  774. preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
  775. $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
  776. for ($k = 0, $n = count($moderators); $k < $n; $k++)
  777. {
  778. $moderators[$k] = trim($moderators[$k]);
  779. if (strlen($moderators[$k]) == 0)
  780. unset($moderators[$k]);
  781. }
  782. // Find all the id_member's for the member_name's in the list.
  783. $group_moderators = array();
  784. if (!empty($moderators))
  785. {
  786. $request = $smcFunc['db_query']('', '
  787. SELECT id_member
  788. FROM {db_prefix}members
  789. WHERE member_name IN ({array_string:moderators}) OR real_name IN ({array_string:moderators})
  790. LIMIT ' . count($moderators),
  791. array(
  792. 'moderators' => $moderators,
  793. )
  794. );
  795. while ($row = $smcFunc['db_fetch_assoc']($request))
  796. $group_moderators[] = $row['id_member'];
  797. $smcFunc['db_free_result']($request);
  798. }
  799. }
  800. else
  801. {
  802. $moderators = array();
  803. foreach ($_POST['moderator_list'] as $moderator)
  804. $moderators[] = (int) $moderator;
  805. $group_moderators = array();
  806. if (!empty($moderators))
  807. {
  808. $request = $smcFunc['db_query']('', '
  809. SELECT id_member
  810. FROM {db_prefix}members
  811. WHERE id_member IN ({array_int:moderators})
  812. LIMIT {int:num_moderators}',
  813. array(
  814. 'moderators' => $moderators,
  815. 'num_moderators' => count($moderators),
  816. )
  817. );
  818. while ($row = $smcFunc['db_fetch_assoc']($request))
  819. $group_moderators[] = $row['id_member'];
  820. $smcFunc['db_free_result']($request);
  821. }
  822. }
  823. // Found some?
  824. if (!empty($group_moderators))
  825. {
  826. $mod_insert = array();
  827. foreach ($group_moderators as $moderator)
  828. $mod_insert[] = array($_REQUEST['group'], $moderator);
  829. $smcFunc['db_insert']('insert',
  830. '{db_prefix}group_moderators',
  831. array('id_group' => 'int', 'id_member' => 'int'),
  832. $mod_insert,
  833. array('id_group', 'id_member')
  834. );
  835. }
  836. }
  837. // There might have been some post group changes.
  838. updateStats('postgroups');
  839. // We've definitely changed some group stuff.
  840. updateSettings(array(
  841. 'settings_updated' => time(),
  842. ));
  843. // Log the edit.
  844. logAction('edited_group', array('group' => $_POST['group_name']), 'admin');
  845. redirectexit('action=admin;area=membergroups');
  846. }
  847. // Fetch the current group information.
  848. $request = $smcFunc['db_query']('', '
  849. SELECT group_name, description, min_posts, online_color, max_messages, stars, group_type, hidden, id_parent
  850. FROM {db_prefix}membergroups
  851. WHERE id_group = {int:current_group}
  852. LIMIT 1',
  853. array(
  854. 'current_group' => (int) $_REQUEST['group'],
  855. )
  856. );
  857. if ($smcFunc['db_num_rows']($request) == 0)
  858. fatal_lang_error('membergroup_does_not_exist', false);
  859. $row = $smcFunc['db_fetch_assoc']($request);
  860. $smcFunc['db_free_result']($request);
  861. $row['stars'] = explode('#', $row['stars']);
  862. $context['group'] = array(
  863. 'id' => $_REQUEST['group'],
  864. 'name' => $row['group_name'],
  865. 'description' => htmlspecialchars($row['description']),
  866. 'editable_name' => htmlspecialchars($row['group_name']),
  867. 'color' => $row['online_color'],
  868. 'min_posts' => $row['min_posts'],
  869. 'max_messages' => $row['max_messages'],
  870. 'star_count' => (int) $row['stars'][0],
  871. 'star_image' => isset($row['stars'][1]) ? $row['stars'][1] : '',
  872. 'is_post_group' => $row['min_posts'] != -1,
  873. 'type' => $row['min_posts'] != -1 ? 0 : $row['group_type'],
  874. 'hidden' => $row['min_posts'] == -1 ? $row['hidden'] : 0,
  875. 'inherited_from' => $row['id_parent'],
  876. 'allow_post_group' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
  877. 'allow_delete' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
  878. 'allow_protected' => allowedTo('admin_forum'),
  879. );
  880. // Get any moderators for this group
  881. $request = $smcFunc['db_query']('', '
  882. SELECT mem.id_member, mem.real_name
  883. FROM {db_prefix}group_moderators AS mods
  884. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
  885. WHERE mods.id_group = {int:current_group}',
  886. array(
  887. 'current_group' => $_REQUEST['group'],
  888. )
  889. );
  890. $context['group']['moderators'] = array();
  891. while ($row = $smcFunc['db_fetch_assoc']($request))
  892. $context['group']['moderators'][$row['id_member']] = $row['real_name'];
  893. $smcFunc['db_free_result']($request);
  894. $context['group']['moderator_list'] = empty($context['group']['moderators']) ? '' : '&quot;' . implode('&quot;, &quot;', $context['group']['moderators']) . '&quot;';
  895. if (!empty($context['group']['moderators']))
  896. list ($context['group']['last_moderator_id']) = array_slice(array_keys($context['group']['moderators']), -1);
  897. // Get a list of boards this membergroup is allowed to see.
  898. $context['boards'] = array();
  899. if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
  900. {
  901. $result = $smcFunc['db_query']('', '
  902. SELECT id_board, name, child_level, FIND_IN_SET({string:current_group}, member_groups) != 0 AS can_access
  903. FROM {db_prefix}boards
  904. ORDER BY board_order',
  905. array(
  906. 'current_group' => (int) $_REQUEST['group'],
  907. )
  908. );
  909. while ($row = $smcFunc['db_fetch_assoc']($result))
  910. $context['boards'][] = array(
  911. 'id' => $row['id_board'],
  912. 'name' => $row['name'],
  913. 'child_level' => $row['child_level'],
  914. 'selected' => !(empty($row['can_access']) || $row['can_access'] == 'f'),
  915. );
  916. $smcFunc['db_free_result']($result);
  917. }
  918. // Finally, get all the groups this could be inherited off.
  919. $request = $smcFunc['db_query']('', '
  920. SELECT id_group, group_name
  921. FROM {db_prefix}membergroups
  922. WHERE id_group != {int:current_group}' .
  923. (empty($modSettings['permission_enable_postgroups']) ? '
  924. AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
  925. AND group_type != {int:is_protected}') . '
  926. AND id_group NOT IN (1, 3)
  927. AND id_parent = {int:not_inherited}',
  928. array(
  929. 'current_group' => (int) $_REQUEST['group'],
  930. 'min_posts' => -1,
  931. 'not_inherited' => -2,
  932. 'is_protected' => 1,
  933. )
  934. );
  935. $context['inheritable_groups'] = array();
  936. while ($row = $smcFunc['db_fetch_assoc']($request))
  937. $context['inheritable_groups'][$row['id_group']] = $row['group_name'];
  938. $smcFunc['db_free_result']($request);
  939. $context['sub_template'] = 'edit_group';
  940. $context['page_title'] = $txt['membergroups_edit_group'];
  941. createToken('admin-mmg');
  942. }
  943. /**
  944. * Set some general membergroup settings and permissions.
  945. * Called by ?action=admin;area=membergroups;sa=settings
  946. * Requires the admin_forum permission (and manage_permissions for changing permissions)
  947. * Redirects to itself.
  948. *
  949. * @uses membergroup_settings sub template of ManageMembergroups.
  950. */
  951. function ModifyMembergroupsettings()
  952. {
  953. global $context, $sourcedir, $scripturl, $modSettings, $txt;
  954. $context['sub_template'] = 'show_settings';
  955. $context['page_title'] = $txt['membergroups_settings'];
  956. // Needed for the settings functions.
  957. require_once($sourcedir . '/ManageServer.php');
  958. // Don't allow assignment of guests.
  959. $context['permissions_excluded'] = array(-1);
  960. // Only one thing here!
  961. $config_vars = array(
  962. array('permissions', 'manage_membergroups'),
  963. );
  964. if (isset($_REQUEST['save']))
  965. {
  966. checkSession();
  967. // Yeppers, saving this...
  968. saveDBSettings($config_vars);
  969. redirectexit('action=admin;area=membergroups;sa=settings');
  970. }
  971. // Some simple context.
  972. $context['post_url'] = $scripturl . '?action=admin;area=membergroups;save;sa=settings';
  973. $context['settings_title'] = $txt['membergroups_settings'];
  974. prepareDBSettingContext($config_vars);
  975. }
  976. ?>