PageRenderTime 164ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/admin/ManageMembergroups.php

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