ManageMembergroups.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. $postCountBasedGroup = isset($_POST['min_posts']) && (!isset($_POST['postgroup_based']) || !empty($_POST['postgroup_based']));
  299. $_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'];
  300. /**
  301. * @todo Check for members with same name too?
  302. */
  303. $request = $smcFunc['db_query']('', '
  304. SELECT MAX(id_group)
  305. FROM {db_prefix}membergroups',
  306. array(
  307. )
  308. );
  309. list ($id_group) = $smcFunc['db_fetch_row']($request);
  310. $smcFunc['db_free_result']($request);
  311. $id_group++;
  312. $smcFunc['db_insert']('',
  313. '{db_prefix}membergroups',
  314. array(
  315. 'id_group' => 'int', 'description' => 'string', 'group_name' => 'string-80', 'min_posts' => 'int',
  316. 'stars' => 'string', 'online_color' => 'string', 'group_type' => 'int',
  317. ),
  318. array(
  319. $id_group, '', $_POST['group_name'], ($postCountBasedGroup ? (int) $_POST['min_posts'] : '-1'),
  320. '1#star.gif', '', $_POST['group_type'],
  321. ),
  322. array('id_group')
  323. );
  324. // Update the post groups now, if this is a post group!
  325. if (isset($_POST['min_posts']))
  326. updateStats('postgroups');
  327. // You cannot set permissions for post groups if they are disabled.
  328. if ($postCountBasedGroup && empty($modSettings['permission_enable_postgroups']))
  329. $_POST['perm_type'] = '';
  330. if ($_POST['perm_type'] == 'predefined')
  331. {
  332. // Set default permission level.
  333. require_once($sourcedir . '/ManagePermissions.php');
  334. setPermissionLevel($_POST['level'], $id_group, 'null');
  335. }
  336. // Copy or inherit the permissions!
  337. elseif ($_POST['perm_type'] == 'copy' || $_POST['perm_type'] == 'inherit')
  338. {
  339. $copy_id = $_POST['perm_type'] == 'copy' ? (int) $_POST['copyperm'] : (int) $_POST['inheritperm'];
  340. // Are you a powerful admin?
  341. if (!allowedTo('admin_forum'))
  342. {
  343. $request = $smcFunc['db_query']('', '
  344. SELECT group_type
  345. FROM {db_prefix}membergroups
  346. WHERE id_group = {int:copy_from}
  347. LIMIT {int:limit}',
  348. array(
  349. 'copy_from' => $copy_id,
  350. 'limit' => 1,
  351. )
  352. );
  353. list ($copy_type) = $smcFunc['db_fetch_row']($request);
  354. $smcFunc['db_free_result']($request);
  355. // Protected groups are... well, protected!
  356. if ($copy_type == 1)
  357. fatal_lang_error('membergroup_does_not_exist');
  358. }
  359. // Don't allow copying of a real priviledged person!
  360. require_once($sourcedir . '/ManagePermissions.php');
  361. loadIllegalPermissions();
  362. $request = $smcFunc['db_query']('', '
  363. SELECT permission, add_deny
  364. FROM {db_prefix}permissions
  365. WHERE id_group = {int:copy_from}',
  366. array(
  367. 'copy_from' => $copy_id,
  368. )
  369. );
  370. $inserts = array();
  371. while ($row = $smcFunc['db_fetch_assoc']($request))
  372. {
  373. if (empty($context['illegal_permissions']) || !in_array($row['permission'], $context['illegal_permissions']))
  374. $inserts[] = array($id_group, $row['permission'], $row['add_deny']);
  375. }
  376. $smcFunc['db_free_result']($request);
  377. if (!empty($inserts))
  378. $smcFunc['db_insert']('insert',
  379. '{db_prefix}permissions',
  380. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  381. $inserts,
  382. array('id_group', 'permission')
  383. );
  384. $request = $smcFunc['db_query']('', '
  385. SELECT id_profile, permission, add_deny
  386. FROM {db_prefix}board_permissions
  387. WHERE id_group = {int:copy_from}',
  388. array(
  389. 'copy_from' => $copy_id,
  390. )
  391. );
  392. $inserts = array();
  393. while ($row = $smcFunc['db_fetch_assoc']($request))
  394. $inserts[] = array($id_group, $row['id_profile'], $row['permission'], $row['add_deny']);
  395. $smcFunc['db_free_result']($request);
  396. if (!empty($inserts))
  397. $smcFunc['db_insert']('insert',
  398. '{db_prefix}board_permissions',
  399. array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  400. $inserts,
  401. array('id_group', 'id_profile', 'permission')
  402. );
  403. // Also get some membergroup information if we're copying and not copying from guests...
  404. if ($copy_id > 0 && $_POST['perm_type'] == 'copy')
  405. {
  406. $request = $smcFunc['db_query']('', '
  407. SELECT online_color, max_messages, stars
  408. FROM {db_prefix}membergroups
  409. WHERE id_group = {int:copy_from}
  410. LIMIT 1',
  411. array(
  412. 'copy_from' => $copy_id,
  413. )
  414. );
  415. $group_info = $smcFunc['db_fetch_assoc']($request);
  416. $smcFunc['db_free_result']($request);
  417. // ...and update the new membergroup with it.
  418. $smcFunc['db_query']('', '
  419. UPDATE {db_prefix}membergroups
  420. SET
  421. online_color = {string:online_color},
  422. max_messages = {int:max_messages},
  423. stars = {string:stars}
  424. WHERE id_group = {int:current_group}',
  425. array(
  426. 'max_messages' => $group_info['max_messages'],
  427. 'current_group' => $id_group,
  428. 'online_color' => $group_info['online_color'],
  429. 'stars' => $group_info['stars'],
  430. )
  431. );
  432. }
  433. // If inheriting say so...
  434. elseif ($_POST['perm_type'] == 'inherit')
  435. {
  436. $smcFunc['db_query']('', '
  437. UPDATE {db_prefix}membergroups
  438. SET id_parent = {int:copy_from}
  439. WHERE id_group = {int:current_group}',
  440. array(
  441. 'copy_from' => $copy_id,
  442. 'current_group' => $id_group,
  443. )
  444. );
  445. }
  446. }
  447. // Make sure all boards selected are stored in a proper array.
  448. $_POST['boardaccess'] = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
  449. foreach ($_POST['boardaccess'] as $key => $value)
  450. $_POST['boardaccess'][$key] = (int) $value;
  451. // Only do this if they have special access requirements.
  452. if (!empty($_POST['boardaccess']))
  453. $smcFunc['db_query']('', '
  454. UPDATE {db_prefix}boards
  455. SET member_groups = CASE WHEN member_groups = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT(member_groups, {string:comma_group}) END
  456. WHERE id_board IN ({array_int:board_list})',
  457. array(
  458. 'board_list' => $_POST['boardaccess'],
  459. 'blank_string' => '',
  460. 'group_id_string' => (string) $id_group,
  461. 'comma_group' => ',' . $id_group,
  462. )
  463. );
  464. // If this is joinable then set it to show group membership in people's profiles.
  465. if (empty($modSettings['show_group_membership']) && $_POST['group_type'] > 1)
  466. updateSettings(array('show_group_membership' => 1));
  467. // Rebuild the group cache.
  468. updateSettings(array(
  469. 'settings_updated' => time(),
  470. ));
  471. // We did it.
  472. logAction('add_group', array('group' => $_POST['group_name']), 'admin');
  473. // Go change some more settings.
  474. redirectexit('action=admin;area=membergroups;sa=edit;group=' . $id_group);
  475. }
  476. // Just show the 'add membergroup' screen.
  477. $context['page_title'] = $txt['membergroups_new_group'];
  478. $context['sub_template'] = 'new_group';
  479. $context['post_group'] = isset($_REQUEST['postgroup']);
  480. $context['undefined_group'] = !isset($_REQUEST['postgroup']) && !isset($_REQUEST['generalgroup']);
  481. $context['allow_protected'] = allowedTo('admin_forum');
  482. $result = $smcFunc['db_query']('', '
  483. SELECT id_group, group_name
  484. FROM {db_prefix}membergroups
  485. WHERE (id_group > {int:moderator_group} OR id_group = {int:global_mod_group})' . (empty($modSettings['permission_enable_postgroups']) ? '
  486. AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
  487. AND group_type != {int:is_protected}') . '
  488. ORDER BY min_posts, id_group != {int:global_mod_group}, group_name',
  489. array(
  490. 'moderator_group' => 3,
  491. 'global_mod_group' => 2,
  492. 'min_posts' => -1,
  493. 'is_protected' => 1,
  494. )
  495. );
  496. $context['groups'] = array();
  497. while ($row = $smcFunc['db_fetch_assoc']($result))
  498. $context['groups'][] = array(
  499. 'id' => $row['id_group'],
  500. 'name' => $row['group_name']
  501. );
  502. $smcFunc['db_free_result']($result);
  503. $result = $smcFunc['db_query']('', '
  504. SELECT id_board, name, child_level
  505. FROM {db_prefix}boards
  506. ORDER BY board_order',
  507. array(
  508. )
  509. );
  510. $context['boards'] = array();
  511. while ($row = $smcFunc['db_fetch_assoc']($result))
  512. $context['boards'][] = array(
  513. 'id' => $row['id_board'],
  514. 'name' => $row['name'],
  515. 'child_level' => $row['child_level'],
  516. 'selected' => false
  517. );
  518. $smcFunc['db_free_result']($result);
  519. }
  520. /**
  521. * Deleting a membergroup by URL (not implemented).
  522. * Called by ?action=admin;area=membergroups;sa=delete;group=x;session_var=y.
  523. * Requires the manage_membergroups permission.
  524. * Redirects to ?action=admin;area=membergroups.
  525. *
  526. * @todo look at this
  527. */
  528. function DeleteMembergroup()
  529. {
  530. global $sourcedir;
  531. checkSession('get');
  532. require_once($sourcedir . '/Subs-Membergroups.php');
  533. deleteMembergroups((int) $_REQUEST['group']);
  534. // Go back to the membergroup index.
  535. redirectexit('action=admin;area=membergroups;');
  536. }
  537. /**
  538. * Editing a membergroup.
  539. * Screen to edit a specific membergroup.
  540. * Called by ?action=admin;area=membergroups;sa=edit;group=x.
  541. * It requires the manage_membergroups permission.
  542. * Also handles the delete button of the edit form.
  543. * Redirects to ?action=admin;area=membergroups.
  544. *
  545. * @uses the edit_group sub template of ManageMembergroups.
  546. */
  547. function EditMembergroup()
  548. {
  549. global $context, $txt, $sourcedir, $modSettings, $smcFunc;
  550. $_REQUEST['group'] = isset($_REQUEST['group']) && $_REQUEST['group'] > 0 ? (int) $_REQUEST['group'] : 0;
  551. // Make sure this group is editable.
  552. if (!empty($_REQUEST['group']))
  553. {
  554. $request = $smcFunc['db_query']('', '
  555. SELECT id_group
  556. FROM {db_prefix}membergroups
  557. WHERE id_group = {int:current_group}' . (allowedTo('admin_forum') ? '' : '
  558. AND group_type != {int:is_protected}') . '
  559. LIMIT {int:limit}',
  560. array(
  561. 'current_group' => $_REQUEST['group'],
  562. 'is_protected' => 1,
  563. 'limit' => 1,
  564. )
  565. );
  566. list ($_REQUEST['group']) = $smcFunc['db_fetch_row']($request);
  567. $smcFunc['db_free_result']($request);
  568. }
  569. // Now, do we have a valid id?
  570. if (empty($_REQUEST['group']))
  571. fatal_lang_error('membergroup_does_not_exist', false);
  572. // The delete this membergroup button was pressed.
  573. if (isset($_POST['delete']))
  574. {
  575. checkSession();
  576. require_once($sourcedir . '/Subs-Membergroups.php');
  577. deleteMembergroups($_REQUEST['group']);
  578. redirectexit('action=admin;area=membergroups;');
  579. }
  580. // A form was submitted with the new membergroup settings.
  581. elseif (isset($_POST['submit']))
  582. {
  583. // Validate the session.
  584. checkSession();
  585. // Can they really inherit from this group?
  586. if ($_POST['group_inherit'] != -2 && !allowedTo('admin_forum'))
  587. {
  588. $request = $smcFunc['db_query']('', '
  589. SELECT group_type
  590. FROM {db_prefix}membergroups
  591. WHERE id_group = {int:inherit_from}
  592. LIMIT {int:limit}',
  593. array(
  594. 'inherit_from' => $_POST['group_inherit'],
  595. 'limit' => 1,
  596. )
  597. );
  598. list ($inherit_type) = $smcFunc['db_fetch_row']($request);
  599. $smcFunc['db_free_result']($request);
  600. }
  601. // Set variables to their proper value.
  602. $_POST['max_messages'] = isset($_POST['max_messages']) ? (int) $_POST['max_messages'] : 0;
  603. $_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);
  604. $_POST['stars'] = (empty($_POST['star_count']) || $_POST['star_count'] < 0) ? '' : min((int) $_POST['star_count'], 99) . '#' . $_POST['star_image'];
  605. $_POST['group_desc'] = isset($_POST['group_desc']) && ($_REQUEST['group'] == 1 || (isset($_POST['group_type']) && $_POST['group_type'] != -1)) ? trim($_POST['group_desc']) : '';
  606. $_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'];
  607. $_POST['group_hidden'] = empty($_POST['group_hidden']) || $_POST['min_posts'] != -1 || $_REQUEST['group'] == 3 ? 0 : (int) $_POST['group_hidden'];
  608. $_POST['group_inherit'] = $_REQUEST['group'] > 1 && $_REQUEST['group'] != 3 && (empty($inherit_type) || $inherit_type != 1) ? (int) $_POST['group_inherit'] : -2;
  609. /**
  610. * @todo Don't set online_color for the Moderators group?
  611. */
  612. // Do the update of the membergroup settings.
  613. $smcFunc['db_query']('', '
  614. UPDATE {db_prefix}membergroups
  615. SET group_name = {string:group_name}, online_color = {string:online_color},
  616. max_messages = {int:max_messages}, min_posts = {int:min_posts}, stars = {string:stars},
  617. description = {string:group_desc}, group_type = {int:group_type}, hidden = {int:group_hidden},
  618. id_parent = {int:group_inherit}
  619. WHERE id_group = {int:current_group}',
  620. array(
  621. 'max_messages' => $_POST['max_messages'],
  622. 'min_posts' => $_POST['min_posts'],
  623. 'group_type' => $_POST['group_type'],
  624. 'group_hidden' => $_POST['group_hidden'],
  625. 'group_inherit' => $_POST['group_inherit'],
  626. 'current_group' => (int) $_REQUEST['group'],
  627. 'group_name' => $_POST['group_name'],
  628. 'online_color' => $_POST['online_color'],
  629. 'stars' => $_POST['stars'],
  630. 'group_desc' => $_POST['group_desc'],
  631. )
  632. );
  633. // Time to update the boards this membergroup has access to.
  634. if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
  635. {
  636. $_POST['boardaccess'] = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
  637. foreach ($_POST['boardaccess'] as $key => $value)
  638. $_POST['boardaccess'][$key] = (int) $value;
  639. // Find all board this group is in, but shouldn't be in.
  640. $request = $smcFunc['db_query']('', '
  641. SELECT id_board, member_groups
  642. FROM {db_prefix}boards
  643. WHERE FIND_IN_SET({string:current_group}, member_groups) != 0' . (empty($_POST['boardaccess']) ? '' : '
  644. AND id_board NOT IN ({array_int:board_access_list})'),
  645. array(
  646. 'current_group' => (int) $_REQUEST['group'],
  647. 'board_access_list' => $_POST['boardaccess'],
  648. )
  649. );
  650. while ($row = $smcFunc['db_fetch_assoc']($request))
  651. $smcFunc['db_query']('', '
  652. UPDATE {db_prefix}boards
  653. SET member_groups = {string:member_group_access}
  654. WHERE id_board = {int:current_board}',
  655. array(
  656. 'current_board' => $row['id_board'],
  657. 'member_group_access' => implode(',', array_diff(explode(',', $row['member_groups']), array($_REQUEST['group']))),
  658. )
  659. );
  660. $smcFunc['db_free_result']($request);
  661. // Add the membergroup to all boards that hadn't been set yet.
  662. if (!empty($_POST['boardaccess']))
  663. $smcFunc['db_query']('', '
  664. UPDATE {db_prefix}boards
  665. SET member_groups = CASE WHEN member_groups = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT(member_groups, {string:comma_group}) END
  666. WHERE id_board IN ({array_int:board_list})
  667. AND FIND_IN_SET({int:current_group}, member_groups) = 0',
  668. array(
  669. 'board_list' => $_POST['boardaccess'],
  670. 'blank_string' => '',
  671. 'current_group' => (int) $_REQUEST['group'],
  672. 'group_id_string' => (string) (int) $_REQUEST['group'],
  673. 'comma_group' => ',' . $_REQUEST['group'],
  674. )
  675. );
  676. }
  677. // Remove everyone from this group!
  678. if ($_POST['min_posts'] != -1)
  679. {
  680. $smcFunc['db_query']('', '
  681. UPDATE {db_prefix}members
  682. SET id_group = {int:regular_member}
  683. WHERE id_group = {int:current_group}',
  684. array(
  685. 'regular_member' => 0,
  686. 'current_group' => (int) $_REQUEST['group'],
  687. )
  688. );
  689. $request = $smcFunc['db_query']('', '
  690. SELECT id_member, additional_groups
  691. FROM {db_prefix}members
  692. WHERE FIND_IN_SET({string:current_group}, additional_groups) != 0',
  693. array(
  694. 'current_group' => (int) $_REQUEST['group'],
  695. )
  696. );
  697. $updates = array();
  698. while ($row = $smcFunc['db_fetch_assoc']($request))
  699. $updates[$row['additional_groups']][] = $row['id_member'];
  700. $smcFunc['db_free_result']($request);
  701. foreach ($updates as $additional_groups => $memberArray)
  702. updateMemberData($memberArray, array('additional_groups' => implode(',', array_diff(explode(',', $additional_groups), array((int) $_REQUEST['group'])))));
  703. }
  704. elseif ($_REQUEST['group'] != 3)
  705. {
  706. // Making it a hidden group? If so remove everyone with it as primary group (Actually, just make them additional).
  707. if ($_POST['group_hidden'] == 2)
  708. {
  709. $request = $smcFunc['db_query']('', '
  710. SELECT id_member, additional_groups
  711. FROM {db_prefix}members
  712. WHERE id_group = {int:current_group}
  713. AND FIND_IN_SET({int:current_group}, additional_groups) = 0',
  714. array(
  715. 'current_group' => (int) $_REQUEST['group'],
  716. )
  717. );
  718. $updates = array();
  719. while ($row = $smcFunc['db_fetch_assoc']($request))
  720. $updates[$row['additional_groups']][] = $row['id_member'];
  721. $smcFunc['db_free_result']($request);
  722. foreach ($updates as $additional_groups => $memberArray)
  723. updateMemberData($memberArray, array('additional_groups' => implode(',', array_merge(explode(',', $additional_groups), array((int) $_REQUEST['group'])))));
  724. $smcFunc['db_query']('', '
  725. UPDATE {db_prefix}members
  726. SET id_group = {int:regular_member}
  727. WHERE id_group = {int:current_group}',
  728. array(
  729. 'regular_member' => 0,
  730. 'current_group' => $_REQUEST['group'],
  731. )
  732. );
  733. }
  734. // Either way, let's check our "show group membership" setting is correct.
  735. $request = $smcFunc['db_query']('', '
  736. SELECT COUNT(*)
  737. FROM {db_prefix}membergroups
  738. WHERE group_type > {int:non_joinable}',
  739. array(
  740. 'non_joinable' => 1,
  741. )
  742. );
  743. list ($have_joinable) = $smcFunc['db_fetch_row']($request);
  744. $smcFunc['db_free_result']($request);
  745. // Do we need to update the setting?
  746. if ((empty($modSettings['show_group_membership']) && $have_joinable) || (!empty($modSettings['show_group_membership']) && !$have_joinable))
  747. updateSettings(array('show_group_membership' => $have_joinable ? 1 : 0));
  748. }
  749. // Do we need to set inherited permissions?
  750. if ($_POST['group_inherit'] != -2 && $_POST['group_inherit'] != $_POST['old_inherit'])
  751. {
  752. require_once($sourcedir . '/ManagePermissions.php');
  753. updateChildPermissions($_POST['group_inherit']);
  754. }
  755. // Finally, moderators!
  756. $moderator_string = isset($_POST['group_moderators']) ? trim($_POST['group_moderators']) : '';
  757. $smcFunc['db_query']('', '
  758. DELETE FROM {db_prefix}group_moderators
  759. WHERE id_group = {int:current_group}',
  760. array(
  761. 'current_group' => $_REQUEST['group'],
  762. )
  763. );
  764. if ((!empty($moderator_string) || !empty($_POST['moderator_list'])) && $_POST['min_posts'] == -1 && $_REQUEST['group'] != 3)
  765. {
  766. // Get all the usernames from the string
  767. if (!empty($moderator_string))
  768. {
  769. $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;' => '"'));
  770. preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
  771. $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
  772. for ($k = 0, $n = count($moderators); $k < $n; $k++)
  773. {
  774. $moderators[$k] = trim($moderators[$k]);
  775. if (strlen($moderators[$k]) == 0)
  776. unset($moderators[$k]);
  777. }
  778. // Find all the id_member's for the member_name's in the list.
  779. $group_moderators = array();
  780. if (!empty($moderators))
  781. {
  782. $request = $smcFunc['db_query']('', '
  783. SELECT id_member
  784. FROM {db_prefix}members
  785. WHERE member_name IN ({array_string:moderators}) OR real_name IN ({array_string:moderators})
  786. LIMIT ' . count($moderators),
  787. array(
  788. 'moderators' => $moderators,
  789. )
  790. );
  791. while ($row = $smcFunc['db_fetch_assoc']($request))
  792. $group_moderators[] = $row['id_member'];
  793. $smcFunc['db_free_result']($request);
  794. }
  795. }
  796. else
  797. {
  798. $moderators = array();
  799. foreach ($_POST['moderator_list'] as $moderator)
  800. $moderators[] = (int) $moderator;
  801. $group_moderators = array();
  802. if (!empty($moderators))
  803. {
  804. $request = $smcFunc['db_query']('', '
  805. SELECT id_member
  806. FROM {db_prefix}members
  807. WHERE id_member IN ({array_int:moderators})
  808. LIMIT {int:num_moderators}',
  809. array(
  810. 'moderators' => $moderators,
  811. 'num_moderators' => count($moderators),
  812. )
  813. );
  814. while ($row = $smcFunc['db_fetch_assoc']($request))
  815. $group_moderators[] = $row['id_member'];
  816. $smcFunc['db_free_result']($request);
  817. }
  818. }
  819. // Found some?
  820. if (!empty($group_moderators))
  821. {
  822. $mod_insert = array();
  823. foreach ($group_moderators as $moderator)
  824. $mod_insert[] = array($_REQUEST['group'], $moderator);
  825. $smcFunc['db_insert']('insert',
  826. '{db_prefix}group_moderators',
  827. array('id_group' => 'int', 'id_member' => 'int'),
  828. $mod_insert,
  829. array('id_group', 'id_member')
  830. );
  831. }
  832. }
  833. // There might have been some post group changes.
  834. updateStats('postgroups');
  835. // We've definitely changed some group stuff.
  836. updateSettings(array(
  837. 'settings_updated' => time(),
  838. ));
  839. // Log the edit.
  840. logAction('edited_group', array('group' => $_POST['group_name']), 'admin');
  841. redirectexit('action=admin;area=membergroups');
  842. }
  843. // Fetch the current group information.
  844. $request = $smcFunc['db_query']('', '
  845. SELECT group_name, description, min_posts, online_color, max_messages, stars, group_type, hidden, id_parent
  846. FROM {db_prefix}membergroups
  847. WHERE id_group = {int:current_group}
  848. LIMIT 1',
  849. array(
  850. 'current_group' => (int) $_REQUEST['group'],
  851. )
  852. );
  853. if ($smcFunc['db_num_rows']($request) == 0)
  854. fatal_lang_error('membergroup_does_not_exist', false);
  855. $row = $smcFunc['db_fetch_assoc']($request);
  856. $smcFunc['db_free_result']($request);
  857. $row['stars'] = explode('#', $row['stars']);
  858. $context['group'] = array(
  859. 'id' => $_REQUEST['group'],
  860. 'name' => $row['group_name'],
  861. 'description' => htmlspecialchars($row['description']),
  862. 'editable_name' => htmlspecialchars($row['group_name']),
  863. 'color' => $row['online_color'],
  864. 'min_posts' => $row['min_posts'],
  865. 'max_messages' => $row['max_messages'],
  866. 'star_count' => (int) $row['stars'][0],
  867. 'star_image' => isset($row['stars'][1]) ? $row['stars'][1] : '',
  868. 'is_post_group' => $row['min_posts'] != -1,
  869. 'type' => $row['min_posts'] != -1 ? 0 : $row['group_type'],
  870. 'hidden' => $row['min_posts'] == -1 ? $row['hidden'] : 0,
  871. 'inherited_from' => $row['id_parent'],
  872. 'allow_post_group' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
  873. 'allow_delete' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
  874. 'allow_protected' => allowedTo('admin_forum'),
  875. );
  876. // Get any moderators for this group
  877. $request = $smcFunc['db_query']('', '
  878. SELECT mem.id_member, mem.real_name
  879. FROM {db_prefix}group_moderators AS mods
  880. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
  881. WHERE mods.id_group = {int:current_group}',
  882. array(
  883. 'current_group' => $_REQUEST['group'],
  884. )
  885. );
  886. $context['group']['moderators'] = array();
  887. while ($row = $smcFunc['db_fetch_assoc']($request))
  888. $context['group']['moderators'][$row['id_member']] = $row['real_name'];
  889. $smcFunc['db_free_result']($request);
  890. $context['group']['moderator_list'] = empty($context['group']['moderators']) ? '' : '&quot;' . implode('&quot;, &quot;', $context['group']['moderators']) . '&quot;';
  891. if (!empty($context['group']['moderators']))
  892. list ($context['group']['last_moderator_id']) = array_slice(array_keys($context['group']['moderators']), -1);
  893. // Get a list of boards this membergroup is allowed to see.
  894. $context['boards'] = array();
  895. if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
  896. {
  897. $result = $smcFunc['db_query']('', '
  898. SELECT id_board, name, child_level, FIND_IN_SET({string:current_group}, member_groups) != 0 AS can_access
  899. FROM {db_prefix}boards
  900. ORDER BY board_order',
  901. array(
  902. 'current_group' => (int) $_REQUEST['group'],
  903. )
  904. );
  905. while ($row = $smcFunc['db_fetch_assoc']($result))
  906. $context['boards'][] = array(
  907. 'id' => $row['id_board'],
  908. 'name' => $row['name'],
  909. 'child_level' => $row['child_level'],
  910. 'selected' => !(empty($row['can_access']) || $row['can_access'] == 'f'),
  911. );
  912. $smcFunc['db_free_result']($result);
  913. }
  914. // Finally, get all the groups this could be inherited off.
  915. $request = $smcFunc['db_query']('', '
  916. SELECT id_group, group_name
  917. FROM {db_prefix}membergroups
  918. WHERE id_group != {int:current_group}' .
  919. (empty($modSettings['permission_enable_postgroups']) ? '
  920. AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
  921. AND group_type != {int:is_protected}') . '
  922. AND id_group NOT IN (1, 3)
  923. AND id_parent = {int:not_inherited}',
  924. array(
  925. 'current_group' => (int) $_REQUEST['group'],
  926. 'min_posts' => -1,
  927. 'not_inherited' => -2,
  928. 'is_protected' => 1,
  929. )
  930. );
  931. $context['inheritable_groups'] = array();
  932. while ($row = $smcFunc['db_fetch_assoc']($request))
  933. $context['inheritable_groups'][$row['id_group']] = $row['group_name'];
  934. $smcFunc['db_free_result']($request);
  935. $context['sub_template'] = 'edit_group';
  936. $context['page_title'] = $txt['membergroups_edit_group'];
  937. }
  938. /**
  939. * Set some general membergroup settings and permissions.
  940. * Called by ?action=admin;area=membergroups;sa=settings
  941. * Requires the admin_forum permission (and manage_permissions for changing permissions)
  942. * Redirects to itself.
  943. *
  944. * @uses membergroup_settings sub template of ManageMembergroups.
  945. */
  946. function ModifyMembergroupsettings()
  947. {
  948. global $context, $sourcedir, $scripturl, $modSettings, $txt;
  949. $context['sub_template'] = 'show_settings';
  950. $context['page_title'] = $txt['membergroups_settings'];
  951. // Needed for the settings functions.
  952. require_once($sourcedir . '/ManageServer.php');
  953. // Don't allow assignment of guests.
  954. $context['permissions_excluded'] = array(-1);
  955. // Only one thing here!
  956. $config_vars = array(
  957. array('permissions', 'manage_membergroups'),
  958. );
  959. if (isset($_REQUEST['save']))
  960. {
  961. checkSession();
  962. // Yeppers, saving this...
  963. saveDBSettings($config_vars);
  964. redirectexit('action=admin;area=membergroups;sa=settings');
  965. }
  966. // Some simple context.
  967. $context['post_url'] = $scripturl . '?action=admin;area=membergroups;save;sa=settings';
  968. $context['settings_title'] = $txt['membergroups_settings'];
  969. prepareDBSettingContext($config_vars);
  970. }
  971. ?>