ManageMembergroups.php 36 KB

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