PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Sources/ManagePermissions.php

https://github.com/smf-portal/SMF2.1
PHP | 2440 lines | 1898 code | 258 blank | 284 comment | 287 complexity | 719e58443e44648afa308711e94db4e5 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * ManagePermissions handles all possible permission stuff.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2012 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Dispaches to the right function based on the given subaction.
  18. * Checks the permissions, based on the sub-action.
  19. * Called by ?action=managepermissions.
  20. *
  21. * @uses ManagePermissions language file.
  22. */
  23. function ModifyPermissions()
  24. {
  25. global $txt, $scripturl, $context;
  26. loadLanguage('ManagePermissions+ManageMembers');
  27. loadTemplate('ManagePermissions');
  28. // Format: 'sub-action' => array('function_to_call', 'permission_needed'),
  29. $subActions = array(
  30. 'board' => array('PermissionByBoard', 'manage_permissions'),
  31. 'index' => array('PermissionIndex', 'manage_permissions'),
  32. 'modify' => array('ModifyMembergroup', 'manage_permissions'),
  33. 'modify2' => array('ModifyMembergroup2', 'manage_permissions'),
  34. 'quick' => array('SetQuickGroups', 'manage_permissions'),
  35. 'quickboard' => array('SetQuickBoards', 'manage_permissions'),
  36. 'postmod' => array('ModifyPostModeration', 'manage_permissions', 'disabled' => !in_array('pm', $context['admin_features'])),
  37. 'profiles' => array('EditPermissionProfiles', 'manage_permissions'),
  38. 'settings' => array('GeneralPermissionSettings', 'admin_forum'),
  39. );
  40. call_integration_hook('integrate_manage_permissions', array($subActions));
  41. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) && empty($subActions[$_REQUEST['sa']]['disabled']) ? $_REQUEST['sa'] : (allowedTo('manage_permissions') ? 'index' : 'settings');
  42. isAllowedTo($subActions[$_REQUEST['sa']][1]);
  43. // Create the tabs for the template.
  44. $context[$context['admin_menu_name']]['tab_data'] = array(
  45. 'title' => $txt['permissions_title'],
  46. 'help' => 'permissions',
  47. 'description' => '',
  48. 'tabs' => array(
  49. 'index' => array(
  50. 'description' => $txt['permissions_groups'],
  51. ),
  52. 'board' => array(
  53. 'description' => $txt['permission_by_board_desc'],
  54. ),
  55. 'profiles' => array(
  56. 'description' => $txt['permissions_profiles_desc'],
  57. ),
  58. 'postmod' => array(
  59. 'description' => $txt['permissions_post_moderation_desc'],
  60. ),
  61. 'settings' => array(
  62. 'description' => $txt['permission_settings_desc'],
  63. ),
  64. ),
  65. );
  66. $subActions[$_REQUEST['sa']][0]();
  67. }
  68. /**
  69. * Sets up the permissions by membergroup index page.
  70. * Called by ?action=managepermissions
  71. * Creates an array of all the groups with the number of members and permissions.
  72. *
  73. * @uses ManagePermissions language file.
  74. * @uses ManagePermissions template file.
  75. * @uses ManageBoards template, permission_index sub-template.
  76. */
  77. function PermissionIndex()
  78. {
  79. global $txt, $scripturl, $context, $settings, $modSettings, $smcFunc;
  80. $context['page_title'] = $txt['permissions_title'];
  81. // Load all the permissions. We'll need them in the template.
  82. loadAllPermissions();
  83. // Also load profiles, we may want to reset.
  84. loadPermissionProfiles();
  85. // Are we going to show the advanced options?
  86. $context['show_advanced_options'] = empty($context['admin_preferences']['app']);
  87. // Determine the number of ungrouped members.
  88. $request = $smcFunc['db_query']('', '
  89. SELECT COUNT(*)
  90. FROM {db_prefix}members
  91. WHERE id_group = {int:regular_group}',
  92. array(
  93. 'regular_group' => 0,
  94. )
  95. );
  96. list ($num_members) = $smcFunc['db_fetch_row']($request);
  97. $smcFunc['db_free_result']($request);
  98. // Fill the context variable with 'Guests' and 'Regular Members'.
  99. $context['groups'] = array(
  100. -1 => array(
  101. 'id' => -1,
  102. 'name' => $txt['membergroups_guests'],
  103. 'num_members' => $txt['membergroups_guests_na'],
  104. 'allow_delete' => false,
  105. 'allow_modify' => true,
  106. 'can_search' => false,
  107. 'href' => '',
  108. 'link' => '',
  109. 'is_post_group' => false,
  110. 'color' => '',
  111. 'icons' => '',
  112. 'children' => array(),
  113. 'num_permissions' => array(
  114. 'allowed' => 0,
  115. // Can't deny guest permissions!
  116. 'denied' => '(' . $txt['permissions_none'] . ')'
  117. ),
  118. 'access' => false
  119. ),
  120. 0 => array(
  121. 'id' => 0,
  122. 'name' => $txt['membergroups_members'],
  123. 'num_members' => $num_members,
  124. 'allow_delete' => false,
  125. 'allow_modify' => true,
  126. 'can_search' => false,
  127. 'href' => $scripturl . '?action=moderate;area=viewgroups;sa=members;group=0',
  128. 'is_post_group' => false,
  129. 'color' => '',
  130. 'icons' => '',
  131. 'children' => array(),
  132. 'num_permissions' => array(
  133. 'allowed' => 0,
  134. 'denied' => 0
  135. ),
  136. 'access' => false
  137. ),
  138. );
  139. $postGroups = array();
  140. $normalGroups = array();
  141. // Query the database defined membergroups.
  142. $query = $smcFunc['db_query']('', '
  143. SELECT id_group, id_parent, group_name, min_posts, online_color, icons
  144. FROM {db_prefix}membergroups' . (empty($modSettings['permission_enable_postgroups']) ? '
  145. WHERE min_posts = {int:min_posts}' : '') . '
  146. ORDER BY id_parent = {int:not_inherited} DESC, min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  147. array(
  148. 'min_posts' => -1,
  149. 'not_inherited' => -2,
  150. 'newbie_group' => 4,
  151. )
  152. );
  153. while ($row = $smcFunc['db_fetch_assoc']($query))
  154. {
  155. // If it's inherited, just add it as a child.
  156. if ($row['id_parent'] != -2)
  157. {
  158. if (isset($context['groups'][$row['id_parent']]))
  159. $context['groups'][$row['id_parent']]['children'][$row['id_group']] = $row['group_name'];
  160. continue;
  161. }
  162. $row['icons'] = explode('#', $row['icons']);
  163. $context['groups'][$row['id_group']] = array(
  164. 'id' => $row['id_group'],
  165. 'name' => $row['group_name'],
  166. 'num_members' => $row['id_group'] != 3 ? 0 : $txt['membergroups_guests_na'],
  167. 'allow_delete' => $row['id_group'] > 4,
  168. 'allow_modify' => $row['id_group'] > 1,
  169. 'can_search' => $row['id_group'] != 3,
  170. 'href' => $scripturl . '?action=moderate;area=viewgroups;sa=members;group=' . $row['id_group'],
  171. 'is_post_group' => $row['min_posts'] != -1,
  172. 'color' => empty($row['online_color']) ? '' : $row['online_color'],
  173. 'icons' => !empty($row['icons'][0]) && !empty($row['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['icons'][1] . '" alt="*" />', $row['icons'][0]) : '',
  174. 'children' => array(),
  175. 'num_permissions' => array(
  176. 'allowed' => $row['id_group'] == 1 ? '(' . $txt['permissions_all'] . ')' : 0,
  177. 'denied' => $row['id_group'] == 1 ? '(' . $txt['permissions_none'] . ')' : 0
  178. ),
  179. 'access' => false,
  180. );
  181. if ($row['min_posts'] == -1)
  182. $normalGroups[$row['id_group']] = $row['id_group'];
  183. else
  184. $postGroups[$row['id_group']] = $row['id_group'];
  185. }
  186. $smcFunc['db_free_result']($query);
  187. // Get the number of members in this post group.
  188. if (!empty($postGroups))
  189. {
  190. $query = $smcFunc['db_query']('', '
  191. SELECT id_post_group AS id_group, COUNT(*) AS num_members
  192. FROM {db_prefix}members
  193. WHERE id_post_group IN ({array_int:post_group_list})
  194. GROUP BY id_post_group',
  195. array(
  196. 'post_group_list' => $postGroups,
  197. )
  198. );
  199. while ($row = $smcFunc['db_fetch_assoc']($query))
  200. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  201. $smcFunc['db_free_result']($query);
  202. }
  203. if (!empty($normalGroups))
  204. {
  205. // First, the easy one!
  206. $query = $smcFunc['db_query']('', '
  207. SELECT id_group, COUNT(*) AS num_members
  208. FROM {db_prefix}members
  209. WHERE id_group IN ({array_int:normal_group_list})
  210. GROUP BY id_group',
  211. array(
  212. 'normal_group_list' => $normalGroups,
  213. )
  214. );
  215. while ($row = $smcFunc['db_fetch_assoc']($query))
  216. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  217. $smcFunc['db_free_result']($query);
  218. // This one is slower, but it's okay... careful not to count twice!
  219. $query = $smcFunc['db_query']('', '
  220. SELECT mg.id_group, COUNT(*) AS num_members
  221. FROM {db_prefix}membergroups AS mg
  222. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_string}
  223. AND mem.id_group != mg.id_group
  224. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  225. WHERE mg.id_group IN ({array_int:normal_group_list})
  226. GROUP BY mg.id_group',
  227. array(
  228. 'normal_group_list' => $normalGroups,
  229. 'blank_string' => '',
  230. )
  231. );
  232. while ($row = $smcFunc['db_fetch_assoc']($query))
  233. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  234. $smcFunc['db_free_result']($query);
  235. }
  236. foreach ($context['groups'] as $id => $data)
  237. {
  238. if ($data['href'] != '')
  239. $context['groups'][$id]['link'] = '<a href="' . $data['href'] . '">' . $data['num_members'] . '</a>';
  240. }
  241. if (empty($_REQUEST['pid']))
  242. {
  243. $request = $smcFunc['db_query']('', '
  244. SELECT id_group, COUNT(*) AS num_permissions, add_deny
  245. FROM {db_prefix}permissions
  246. ' . (empty($context['hidden_permissions']) ? '' : ' WHERE permission NOT IN ({array_string:hidden_permissions})') . '
  247. GROUP BY id_group, add_deny',
  248. array(
  249. 'hidden_permissions' => !empty($context['hidden_permissions']) ? $context['hidden_permissions'] : array(),
  250. )
  251. );
  252. while ($row = $smcFunc['db_fetch_assoc']($request))
  253. if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
  254. $context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] = $row['num_permissions'];
  255. $smcFunc['db_free_result']($request);
  256. // Get the "default" profile permissions too.
  257. $request = $smcFunc['db_query']('', '
  258. SELECT id_profile, id_group, COUNT(*) AS num_permissions, add_deny
  259. FROM {db_prefix}board_permissions
  260. WHERE id_profile = {int:default_profile}
  261. ' . (empty($context['hidden_permissions']) ? '' : ' AND permission NOT IN ({array_string:hidden_permissions})') . '
  262. GROUP BY id_profile, id_group, add_deny',
  263. array(
  264. 'default_profile' => 1,
  265. 'hidden_permissions' => !empty($context['hidden_permissions']) ? $context['hidden_permissions'] : array(),
  266. )
  267. );
  268. while ($row = $smcFunc['db_fetch_assoc']($request))
  269. {
  270. if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
  271. $context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] += $row['num_permissions'];
  272. }
  273. $smcFunc['db_free_result']($request);
  274. }
  275. else
  276. {
  277. $_REQUEST['pid'] = (int) $_REQUEST['pid'];
  278. if (!isset($context['profiles'][$_REQUEST['pid']]))
  279. fatal_lang_error('no_access', false);
  280. // Change the selected tab to better reflect that this really is a board profile.
  281. $context[$context['admin_menu_name']]['current_subsection'] = 'profiles';
  282. $request = $smcFunc['db_query']('', '
  283. SELECT id_profile, id_group, COUNT(*) AS num_permissions, add_deny
  284. FROM {db_prefix}board_permissions
  285. WHERE id_profile = {int:current_profile}
  286. GROUP BY id_profile, id_group, add_deny',
  287. array(
  288. 'current_profile' => $_REQUEST['pid'],
  289. )
  290. );
  291. while ($row = $smcFunc['db_fetch_assoc']($request))
  292. {
  293. if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
  294. $context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] += $row['num_permissions'];
  295. }
  296. $smcFunc['db_free_result']($request);
  297. $context['profile'] = array(
  298. 'id' => $_REQUEST['pid'],
  299. 'name' => $context['profiles'][$_REQUEST['pid']]['name'],
  300. );
  301. }
  302. // We can modify any permission set apart from the read only, reply only and no polls ones as they are redefined.
  303. $context['can_modify'] = empty($_REQUEST['pid']) || $_REQUEST['pid'] == 1 || $_REQUEST['pid'] > 4;
  304. // Load the proper template.
  305. $context['sub_template'] = 'permission_index';
  306. createToken('admin-mpq');
  307. }
  308. /**
  309. * Handle permissions by board... more or less. :P
  310. */
  311. function PermissionByBoard()
  312. {
  313. global $context, $modSettings, $txt, $smcFunc, $sourcedir, $cat_tree, $boardList, $boards;
  314. $context['page_title'] = $txt['permissions_boards'];
  315. $context['edit_all'] = isset($_GET['edit']);
  316. // Saving?
  317. if (!empty($_POST['save_changes']) && !empty($_POST['boardprofile']))
  318. {
  319. checkSession('request');
  320. validateToken('admin-mpb');
  321. $changes = array();
  322. foreach ($_POST['boardprofile'] as $board => $profile)
  323. {
  324. $changes[(int) $profile][] = (int) $board;
  325. }
  326. if (!empty($changes))
  327. {
  328. foreach ($changes as $profile => $boards)
  329. $smcFunc['db_query']('', '
  330. UPDATE {db_prefix}boards
  331. SET id_profile = {int:current_profile}
  332. WHERE id_board IN ({array_int:board_list})',
  333. array(
  334. 'board_list' => $boards,
  335. 'current_profile' => $profile,
  336. )
  337. );
  338. }
  339. $context['edit_all'] = false;
  340. }
  341. // Load all permission profiles.
  342. loadPermissionProfiles();
  343. // Get the board tree.
  344. require_once($sourcedir . '/Subs-Boards.php');
  345. getBoardTree();
  346. // Build the list of the boards.
  347. $context['categories'] = array();
  348. foreach ($cat_tree as $catid => $tree)
  349. {
  350. $context['categories'][$catid] = array(
  351. 'name' => &$tree['node']['name'],
  352. 'id' => &$tree['node']['id'],
  353. 'boards' => array()
  354. );
  355. foreach ($boardList[$catid] as $boardid)
  356. {
  357. if (!isset($context['profiles'][$boards[$boardid]['profile']]))
  358. $boards[$boardid]['profile'] = 1;
  359. $context['categories'][$catid]['boards'][$boardid] = array(
  360. 'id' => &$boards[$boardid]['id'],
  361. 'name' => &$boards[$boardid]['name'],
  362. 'description' => &$boards[$boardid]['description'],
  363. 'child_level' => &$boards[$boardid]['level'],
  364. 'profile' => &$boards[$boardid]['profile'],
  365. 'profile_name' => $context['profiles'][$boards[$boardid]['profile']]['name'],
  366. );
  367. }
  368. }
  369. $context['sub_template'] = 'by_board';
  370. createToken('admin-mpb');
  371. }
  372. /**
  373. * Handles permission modification actions from the upper part of the
  374. * permission manager index.
  375. */
  376. function SetQuickGroups()
  377. {
  378. global $context, $smcFunc;
  379. checkSession();
  380. validateToken('admin-mpq', 'quick');
  381. loadIllegalPermissions();
  382. loadIllegalGuestPermissions();
  383. // Make sure only one of the quick options was selected.
  384. if ((!empty($_POST['predefined']) && ((isset($_POST['copy_from']) && $_POST['copy_from'] != 'empty') || !empty($_POST['permissions']))) || (!empty($_POST['copy_from']) && $_POST['copy_from'] != 'empty' && !empty($_POST['permissions'])))
  385. fatal_lang_error('permissions_only_one_option', false);
  386. if (empty($_POST['group']) || !is_array($_POST['group']))
  387. $_POST['group'] = array();
  388. // Only accept numeric values for selected membergroups.
  389. foreach ($_POST['group'] as $id => $group_id)
  390. $_POST['group'][$id] = (int) $group_id;
  391. $_POST['group'] = array_unique($_POST['group']);
  392. if (empty($_REQUEST['pid']))
  393. $_REQUEST['pid'] = 0;
  394. else
  395. $_REQUEST['pid'] = (int) $_REQUEST['pid'];
  396. // Fix up the old global to the new default!
  397. $bid = max(1, $_REQUEST['pid']);
  398. // No modifying the predefined profiles.
  399. if ($_REQUEST['pid'] > 1 && $_REQUEST['pid'] < 5)
  400. fatal_lang_error('no_access', false);
  401. // Clear out any cached authority.
  402. updateSettings(array('settings_updated' => time()));
  403. // No groups where selected.
  404. if (empty($_POST['group']))
  405. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  406. // Set a predefined permission profile.
  407. if (!empty($_POST['predefined']))
  408. {
  409. // Make sure it's a predefined permission set we expect.
  410. if (!in_array($_POST['predefined'], array('restrict', 'standard', 'moderator', 'maintenance')))
  411. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  412. foreach ($_POST['group'] as $group_id)
  413. {
  414. if (!empty($_REQUEST['pid']))
  415. setPermissionLevel($_POST['predefined'], $group_id, $_REQUEST['pid']);
  416. else
  417. setPermissionLevel($_POST['predefined'], $group_id);
  418. }
  419. }
  420. // Set a permission profile based on the permissions of a selected group.
  421. elseif ($_POST['copy_from'] != 'empty')
  422. {
  423. // Just checking the input.
  424. if (!is_numeric($_POST['copy_from']))
  425. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  426. // Make sure the group we're copying to is never included.
  427. $_POST['group'] = array_diff($_POST['group'], array($_POST['copy_from']));
  428. // No groups left? Too bad.
  429. if (empty($_POST['group']))
  430. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  431. if (empty($_REQUEST['pid']))
  432. {
  433. // Retrieve current permissions of group.
  434. $request = $smcFunc['db_query']('', '
  435. SELECT permission, add_deny
  436. FROM {db_prefix}permissions
  437. WHERE id_group = {int:copy_from}',
  438. array(
  439. 'copy_from' => $_POST['copy_from'],
  440. )
  441. );
  442. $target_perm = array();
  443. while ($row = $smcFunc['db_fetch_assoc']($request))
  444. $target_perm[$row['permission']] = $row['add_deny'];
  445. $smcFunc['db_free_result']($request);
  446. $inserts = array();
  447. foreach ($_POST['group'] as $group_id)
  448. foreach ($target_perm as $perm => $add_deny)
  449. {
  450. // No dodgy permissions please!
  451. if (!empty($context['illegal_permissions']) && in_array($perm, $context['illegal_permissions']))
  452. continue;
  453. if ($group_id == -1 && in_array($perm, $context['non_guest_permissions']))
  454. continue;
  455. if ($group_id != 1 && $group_id != 3)
  456. $inserts[] = array($perm, $group_id, $add_deny);
  457. }
  458. // Delete the previous permissions...
  459. $smcFunc['db_query']('', '
  460. DELETE FROM {db_prefix}permissions
  461. WHERE id_group IN ({array_int:group_list})
  462. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  463. array(
  464. 'group_list' => $_POST['group'],
  465. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  466. )
  467. );
  468. if (!empty($inserts))
  469. {
  470. // ..and insert the new ones.
  471. $smcFunc['db_insert']('',
  472. '{db_prefix}permissions',
  473. array(
  474. 'permission' => 'string', 'id_group' => 'int', 'add_deny' => 'int',
  475. ),
  476. $inserts,
  477. array('permission', 'id_group')
  478. );
  479. }
  480. }
  481. // Now do the same for the board permissions.
  482. $request = $smcFunc['db_query']('', '
  483. SELECT permission, add_deny
  484. FROM {db_prefix}board_permissions
  485. WHERE id_group = {int:copy_from}
  486. AND id_profile = {int:current_profile}',
  487. array(
  488. 'copy_from' => $_POST['copy_from'],
  489. 'current_profile' => $bid,
  490. )
  491. );
  492. $target_perm = array();
  493. while ($row = $smcFunc['db_fetch_assoc']($request))
  494. $target_perm[$row['permission']] = $row['add_deny'];
  495. $smcFunc['db_free_result']($request);
  496. $inserts = array();
  497. foreach ($_POST['group'] as $group_id)
  498. foreach ($target_perm as $perm => $add_deny)
  499. {
  500. // Are these for guests?
  501. if ($group_id == -1 && in_array($perm, $context['non_guest_permissions']))
  502. continue;
  503. $inserts[] = array($perm, $group_id, $bid, $add_deny);
  504. }
  505. // Delete the previous global board permissions...
  506. $smcFunc['db_query']('', '
  507. DELETE FROM {db_prefix}board_permissions
  508. WHERE id_group IN ({array_int:current_group_list})
  509. AND id_profile = {int:current_profile}',
  510. array(
  511. 'current_group_list' => $_POST['group'],
  512. 'current_profile' => $bid,
  513. )
  514. );
  515. // And insert the copied permissions.
  516. if (!empty($inserts))
  517. {
  518. // ..and insert the new ones.
  519. $smcFunc['db_insert']('',
  520. '{db_prefix}board_permissions',
  521. array('permission' => 'string', 'id_group' => 'int', 'id_profile' => 'int', 'add_deny' => 'int'),
  522. $inserts,
  523. array('permission', 'id_group', 'id_profile')
  524. );
  525. }
  526. // Update any children out there!
  527. updateChildPermissions($_POST['group'], $_REQUEST['pid']);
  528. }
  529. // Set or unset a certain permission for the selected groups.
  530. elseif (!empty($_POST['permissions']))
  531. {
  532. // Unpack two variables that were transported.
  533. list ($permissionType, $permission) = explode('/', $_POST['permissions']);
  534. // Check whether our input is within expected range.
  535. if (!in_array($_POST['add_remove'], array('add', 'clear', 'deny')) || !in_array($permissionType, array('membergroup', 'board')))
  536. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  537. if ($_POST['add_remove'] == 'clear')
  538. {
  539. if ($permissionType == 'membergroup')
  540. $smcFunc['db_query']('', '
  541. DELETE FROM {db_prefix}permissions
  542. WHERE id_group IN ({array_int:current_group_list})
  543. AND permission = {string:current_permission}
  544. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  545. array(
  546. 'current_group_list' => $_POST['group'],
  547. 'current_permission' => $permission,
  548. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  549. )
  550. );
  551. else
  552. $smcFunc['db_query']('', '
  553. DELETE FROM {db_prefix}board_permissions
  554. WHERE id_group IN ({array_int:current_group_list})
  555. AND id_profile = {int:current_profile}
  556. AND permission = {string:current_permission}',
  557. array(
  558. 'current_group_list' => $_POST['group'],
  559. 'current_profile' => $bid,
  560. 'current_permission' => $permission,
  561. )
  562. );
  563. }
  564. // Add a permission (either 'set' or 'deny').
  565. else
  566. {
  567. $add_deny = $_POST['add_remove'] == 'add' ? '1' : '0';
  568. $permChange = array();
  569. foreach ($_POST['group'] as $groupID)
  570. {
  571. if ($groupID == -1 && in_array($permission, $context['non_guest_permissions']))
  572. continue;
  573. if ($permissionType == 'membergroup' && $groupID != 1 && $groupID != 3 && (empty($context['illegal_permissions']) || !in_array($permission, $context['illegal_permissions'])))
  574. $permChange[] = array($permission, $groupID, $add_deny);
  575. elseif ($permissionType != 'membergroup')
  576. $permChange[] = array($permission, $groupID, $bid, $add_deny);
  577. }
  578. if (!empty($permChange))
  579. {
  580. if ($permissionType == 'membergroup')
  581. $smcFunc['db_insert']('replace',
  582. '{db_prefix}permissions',
  583. array('permission' => 'string', 'id_group' => 'int', 'add_deny' => 'int'),
  584. $permChange,
  585. array('permission', 'id_group')
  586. );
  587. // Board permissions go into the other table.
  588. else
  589. $smcFunc['db_insert']('replace',
  590. '{db_prefix}board_permissions',
  591. array('permission' => 'string', 'id_group' => 'int', 'id_profile' => 'int', 'add_deny' => 'int'),
  592. $permChange,
  593. array('permission', 'id_group', 'id_profile')
  594. );
  595. }
  596. }
  597. // Another child update!
  598. updateChildPermissions($_POST['group'], $_REQUEST['pid']);
  599. }
  600. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  601. }
  602. /**
  603. * Initializes the necessary to modify a membergroup's permissions.
  604. */
  605. function ModifyMembergroup()
  606. {
  607. global $context, $txt, $modSettings, $smcFunc, $sourcedir;
  608. if (!isset($_GET['group']))
  609. fatal_lang_error('no_access', false);
  610. $context['group']['id'] = (int) $_GET['group'];
  611. // Are they toggling the view?
  612. if (isset($_GET['view']))
  613. {
  614. $context['admin_preferences']['pv'] = $_GET['view'] == 'classic' ? 'classic' : 'simple';
  615. // Update the users preferences.
  616. require_once($sourcedir . '/Subs-Admin.php');
  617. updateAdminPreferences();
  618. }
  619. $context['view_type'] = !empty($context['admin_preferences']['pv']) && $context['admin_preferences']['pv'] == 'classic' ? 'classic' : 'simple';
  620. // It's not likely you'd end up here with this setting disabled.
  621. if ($_GET['group'] == 1)
  622. redirectexit('action=admin;area=permissions');
  623. loadAllPermissions($context['view_type']);
  624. loadPermissionProfiles();
  625. if ($context['group']['id'] > 0)
  626. {
  627. $result = $smcFunc['db_query']('', '
  628. SELECT group_name, id_parent
  629. FROM {db_prefix}membergroups
  630. WHERE id_group = {int:current_group}
  631. LIMIT 1',
  632. array(
  633. 'current_group' => $context['group']['id'],
  634. )
  635. );
  636. list ($context['group']['name'], $parent) = $smcFunc['db_fetch_row']($result);
  637. $smcFunc['db_free_result']($result);
  638. // Cannot edit an inherited group!
  639. if ($parent != -2)
  640. fatal_lang_error('cannot_edit_permissions_inherited');
  641. }
  642. elseif ($context['group']['id'] == -1)
  643. $context['group']['name'] = $txt['membergroups_guests'];
  644. else
  645. $context['group']['name'] = $txt['membergroups_members'];
  646. $context['profile']['id'] = empty($_GET['pid']) ? 0 : (int) $_GET['pid'];
  647. // If this is a moderator and they are editing "no profile" then we only do boards.
  648. if ($context['group']['id'] == 3 && empty($context['profile']['id']))
  649. {
  650. // For sanity just check they have no general permissions.
  651. $smcFunc['db_query']('', '
  652. DELETE FROM {db_prefix}permissions
  653. WHERE id_group = {int:moderator_group}',
  654. array(
  655. 'moderator_group' => 3,
  656. )
  657. );
  658. $context['profile']['id'] = 1;
  659. }
  660. $context['permission_type'] = empty($context['profile']['id']) ? 'membergroup' : 'board';
  661. $context['profile']['can_modify'] = !$context['profile']['id'] || $context['profiles'][$context['profile']['id']]['can_modify'];
  662. // Set up things a little nicer for board related stuff...
  663. if ($context['permission_type'] == 'board')
  664. {
  665. $context['profile']['name'] = $context['profiles'][$context['profile']['id']]['name'];
  666. $context[$context['admin_menu_name']]['current_subsection'] = 'profiles';
  667. }
  668. // Fetch the current permissions.
  669. $permissions = array(
  670. 'membergroup' => array('allowed' => array(), 'denied' => array()),
  671. 'board' => array('allowed' => array(), 'denied' => array())
  672. );
  673. // General permissions?
  674. if ($context['permission_type'] == 'membergroup')
  675. {
  676. $result = $smcFunc['db_query']('', '
  677. SELECT permission, add_deny
  678. FROM {db_prefix}permissions
  679. WHERE id_group = {int:current_group}',
  680. array(
  681. 'current_group' => $_GET['group'],
  682. )
  683. );
  684. while ($row = $smcFunc['db_fetch_assoc']($result))
  685. $permissions['membergroup'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
  686. $smcFunc['db_free_result']($result);
  687. }
  688. // Fetch current board permissions...
  689. $result = $smcFunc['db_query']('', '
  690. SELECT permission, add_deny
  691. FROM {db_prefix}board_permissions
  692. WHERE id_group = {int:current_group}
  693. AND id_profile = {int:current_profile}',
  694. array(
  695. 'current_group' => $context['group']['id'],
  696. 'current_profile' => $context['permission_type'] == 'membergroup' ? 1 : $context['profile']['id'],
  697. )
  698. );
  699. while ($row = $smcFunc['db_fetch_assoc']($result))
  700. $permissions['board'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
  701. $smcFunc['db_free_result']($result);
  702. // Loop through each permission and set whether it's checked.
  703. foreach ($context['permissions'] as $permissionType => $tmp)
  704. {
  705. foreach ($tmp['columns'] as $position => $permissionGroups)
  706. {
  707. foreach ($permissionGroups as $permissionGroup => $permissionArray)
  708. {
  709. foreach ($permissionArray['permissions'] as $perm)
  710. {
  711. // Create a shortcut for the current permission.
  712. $curPerm = &$context['permissions'][$permissionType]['columns'][$position][$permissionGroup]['permissions'][$perm['id']];
  713. if ($tmp['view'] == 'classic')
  714. {
  715. if ($perm['has_own_any'])
  716. {
  717. $curPerm['any']['select'] = in_array($perm['id'] . '_any', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_any', $permissions[$permissionType]['denied']) ? 'denied' : 'off');
  718. $curPerm['own']['select'] = in_array($perm['id'] . '_own', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_own', $permissions[$permissionType]['denied']) ? 'denied' : 'off');
  719. }
  720. else
  721. $curPerm['select'] = in_array($perm['id'], $permissions[$permissionType]['denied']) ? 'denied' : (in_array($perm['id'], $permissions[$permissionType]['allowed']) ? 'on' : 'off');
  722. }
  723. else
  724. {
  725. $curPerm['select'] = in_array($perm['id'], $permissions[$permissionType]['denied']) ? 'denied' : (in_array($perm['id'], $permissions[$permissionType]['allowed']) ? 'on' : 'off');
  726. }
  727. }
  728. }
  729. }
  730. }
  731. $context['sub_template'] = 'modify_group';
  732. $context['page_title'] = $txt['permissions_modify_group'];
  733. createToken('admin-mp');
  734. }
  735. /**
  736. * This function actually saves modifications to a membergroup's board permissions.
  737. */
  738. function ModifyMembergroup2()
  739. {
  740. global $modSettings, $smcFunc, $context;
  741. checkSession();
  742. validateToken('admin-mp');
  743. loadIllegalPermissions();
  744. $_GET['group'] = (int) $_GET['group'];
  745. $_GET['pid'] = (int) $_GET['pid'];
  746. // Cannot modify predefined profiles.
  747. if ($_GET['pid'] > 1 && $_GET['pid'] < 5)
  748. fatal_lang_error('no_access', false);
  749. // Verify this isn't inherited.
  750. if ($_GET['group'] == -1 || $_GET['group'] == 0)
  751. $parent = -2;
  752. else
  753. {
  754. $result = $smcFunc['db_query']('', '
  755. SELECT id_parent
  756. FROM {db_prefix}membergroups
  757. WHERE id_group = {int:current_group}
  758. LIMIT 1',
  759. array(
  760. 'current_group' => $_GET['group'],
  761. )
  762. );
  763. list ($parent) = $smcFunc['db_fetch_row']($result);
  764. $smcFunc['db_free_result']($result);
  765. }
  766. if ($parent != -2)
  767. fatal_lang_error('cannot_edit_permissions_inherited');
  768. $givePerms = array('membergroup' => array(), 'board' => array());
  769. // Guest group, we need illegal, guest permissions.
  770. if ($_GET['group'] == -1)
  771. {
  772. loadIllegalGuestPermissions();
  773. $context['illegal_permissions'] = array_merge($context['illegal_permissions'], $context['non_guest_permissions']);
  774. }
  775. // Prepare all permissions that were set or denied for addition to the DB.
  776. if (isset($_POST['perm']) && is_array($_POST['perm']))
  777. {
  778. foreach ($_POST['perm'] as $perm_type => $perm_array)
  779. {
  780. if (is_array($perm_array))
  781. {
  782. foreach ($perm_array as $permission => $value)
  783. if ($value == 'on' || $value == 'deny')
  784. {
  785. // Don't allow people to escalate themselves!
  786. if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
  787. continue;
  788. $givePerms[$perm_type][] = array($_GET['group'], $permission, $value == 'deny' ? 0 : 1);
  789. }
  790. }
  791. }
  792. }
  793. // Insert the general permissions.
  794. if ($_GET['group'] != 3 && empty($_GET['pid']))
  795. {
  796. $smcFunc['db_query']('', '
  797. DELETE FROM {db_prefix}permissions
  798. WHERE id_group = {int:current_group}
  799. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  800. array(
  801. 'current_group' => $_GET['group'],
  802. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  803. )
  804. );
  805. if (!empty($givePerms['membergroup']))
  806. {
  807. $smcFunc['db_insert']('replace',
  808. '{db_prefix}permissions',
  809. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  810. $givePerms['membergroup'],
  811. array('id_group', 'permission')
  812. );
  813. }
  814. }
  815. // Insert the boardpermissions.
  816. $profileid = max(1, $_GET['pid']);
  817. $smcFunc['db_query']('', '
  818. DELETE FROM {db_prefix}board_permissions
  819. WHERE id_group = {int:current_group}
  820. AND id_profile = {int:current_profile}',
  821. array(
  822. 'current_group' => $_GET['group'],
  823. 'current_profile' => $profileid,
  824. )
  825. );
  826. if (!empty($givePerms['board']))
  827. {
  828. foreach ($givePerms['board'] as $k => $v)
  829. $givePerms['board'][$k][] = $profileid;
  830. $smcFunc['db_insert']('replace',
  831. '{db_prefix}board_permissions',
  832. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int', 'id_profile' => 'int'),
  833. $givePerms['board'],
  834. array('id_group', 'permission', 'id_profile')
  835. );
  836. }
  837. // Update any inherited permissions as required.
  838. updateChildPermissions($_GET['group'], $_GET['pid']);
  839. // Clear cached privs.
  840. updateSettings(array('settings_updated' => time()));
  841. redirectexit('action=admin;area=permissions;pid=' . $_GET['pid']);
  842. }
  843. /**
  844. * A screen to set some general settings for permissions.
  845. *
  846. * @param bool $return_config = false
  847. */
  848. function GeneralPermissionSettings($return_config = false)
  849. {
  850. global $context, $modSettings, $sourcedir, $txt, $scripturl, $smcFunc;
  851. // All the setting variables
  852. $config_vars = array(
  853. array('title', 'settings'),
  854. // Inline permissions.
  855. array('permissions', 'manage_permissions'),
  856. '',
  857. // A few useful settings
  858. array('check', 'permission_enable_deny', 0, $txt['permission_settings_enable_deny'], 'help' => 'permissions_deny'),
  859. array('check', 'permission_enable_postgroups', 0, $txt['permission_settings_enable_postgroups'], 'help' => 'permissions_postgroups'),
  860. );
  861. call_integration_hook('integrate_modify_permission_settings', array($config_vars));
  862. if ($return_config)
  863. return $config_vars;
  864. $context['page_title'] = $txt['permission_settings_title'];
  865. $context['sub_template'] = 'show_settings';
  866. // Needed for the inline permission functions, and the settings template.
  867. require_once($sourcedir . '/ManageServer.php');
  868. // Don't let guests have these permissions.
  869. $context['post_url'] = $scripturl . '?action=admin;area=permissions;save;sa=settings';
  870. $context['permissions_excluded'] = array(-1);
  871. // Saving the settings?
  872. if (isset($_GET['save']))
  873. {
  874. checkSession('post');
  875. call_integration_hook('integrate_save_permission_settings');
  876. saveDBSettings($config_vars);
  877. // Clear all deny permissions...if we want that.
  878. if (empty($modSettings['permission_enable_deny']))
  879. {
  880. $smcFunc['db_query']('', '
  881. DELETE FROM {db_prefix}permissions
  882. WHERE add_deny = {int:denied}',
  883. array(
  884. 'denied' => 0,
  885. )
  886. );
  887. $smcFunc['db_query']('', '
  888. DELETE FROM {db_prefix}board_permissions
  889. WHERE add_deny = {int:denied}',
  890. array(
  891. 'denied' => 0,
  892. )
  893. );
  894. }
  895. // Make sure there are no postgroup based permissions left.
  896. if (empty($modSettings['permission_enable_postgroups']))
  897. {
  898. // Get a list of postgroups.
  899. $post_groups = array();
  900. $request = $smcFunc['db_query']('', '
  901. SELECT id_group
  902. FROM {db_prefix}membergroups
  903. WHERE min_posts != {int:min_posts}',
  904. array(
  905. 'min_posts' => -1,
  906. )
  907. );
  908. while ($row = $smcFunc['db_fetch_assoc']($request))
  909. $post_groups[] = $row['id_group'];
  910. $smcFunc['db_free_result']($request);
  911. // Remove'em.
  912. $smcFunc['db_query']('', '
  913. DELETE FROM {db_prefix}permissions
  914. WHERE id_group IN ({array_int:post_group_list})',
  915. array(
  916. 'post_group_list' => $post_groups,
  917. )
  918. );
  919. $smcFunc['db_query']('', '
  920. DELETE FROM {db_prefix}board_permissions
  921. WHERE id_group IN ({array_int:post_group_list})',
  922. array(
  923. 'post_group_list' => $post_groups,
  924. )
  925. );
  926. $smcFunc['db_query']('', '
  927. UPDATE {db_prefix}membergroups
  928. SET id_parent = {int:not_inherited}
  929. WHERE id_parent IN ({array_int:post_group_list})',
  930. array(
  931. 'post_group_list' => $post_groups,
  932. 'not_inherited' => -2,
  933. )
  934. );
  935. }
  936. redirectexit('action=admin;area=permissions;sa=settings');
  937. }
  938. // We need this for the in-line permissions
  939. createToken('admin-mp');
  940. prepareDBSettingContext($config_vars);
  941. }
  942. /**
  943. * Set the permission level for a specific profile, group, or group for a profile.
  944. * @internal
  945. *
  946. * @param string $level
  947. * @param int $group
  948. * @param mixed $profile = null, int expected
  949. */
  950. function setPermissionLevel($level, $group, $profile = 'null')
  951. {
  952. global $smcFunc, $context;
  953. loadIllegalPermissions();
  954. loadIllegalGuestPermissions();
  955. // Levels by group... restrict, standard, moderator, maintenance.
  956. $groupLevels = array(
  957. 'board' => array('inherit' => array()),
  958. 'group' => array('inherit' => array())
  959. );
  960. // Levels by board... standard, publish, free.
  961. $boardLevels = array('inherit' => array());
  962. // Restrictive - ie. guests.
  963. $groupLevels['global']['restrict'] = array(
  964. 'search_posts',
  965. 'calendar_view',
  966. 'view_stats',
  967. 'who_view',
  968. 'profile_view_own',
  969. 'profile_identity_own',
  970. );
  971. $groupLevels['board']['restrict'] = array(
  972. 'poll_view',
  973. 'post_new',
  974. 'post_reply_own',
  975. 'post_reply_any',
  976. 'delete_own',
  977. 'modify_own',
  978. 'mark_any_notify',
  979. 'mark_notify',
  980. 'report_any',
  981. 'send_topic',
  982. );
  983. // Standard - ie. members. They can do anything Restrictive can.
  984. $groupLevels['global']['standard'] = array_merge($groupLevels['global']['restrict'], array(
  985. 'view_mlist',
  986. 'karma_edit',
  987. 'pm_read',
  988. 'pm_send',
  989. 'send_email_to_members',
  990. 'profile_view_any',
  991. 'profile_extra_own',
  992. 'profile_server_avatar',
  993. 'profile_upload_avatar',
  994. 'profile_remote_avatar',
  995. 'profile_remove_own',
  996. ));
  997. $groupLevels['board']['standard'] = array_merge($groupLevels['board']['restrict'], array(
  998. 'poll_vote',
  999. 'poll_edit_own',
  1000. 'poll_post',
  1001. 'poll_add_own',
  1002. 'post_attachment',
  1003. 'lock_own',
  1004. 'remove_own',
  1005. 'view_attachments',
  1006. ));
  1007. // Moderator - ie. moderators :P. They can do what standard can, and more.
  1008. $groupLevels['global']['moderator'] = array_merge($groupLevels['global']['standard'], array(
  1009. 'calendar_post',
  1010. 'calendar_edit_own',
  1011. 'access_mod_center',
  1012. 'issue_warning',
  1013. ));
  1014. $groupLevels['board']['moderator'] = array_merge($groupLevels['board']['standard'], array(
  1015. 'make_sticky',
  1016. 'poll_edit_any',
  1017. 'delete_any',
  1018. 'modify_any',
  1019. 'lock_any',
  1020. 'remove_any',
  1021. 'move_any',
  1022. 'merge_any',
  1023. 'split_any',
  1024. 'poll_lock_any',
  1025. 'poll_remove_any',
  1026. 'poll_add_any',
  1027. 'approve_posts',
  1028. ));
  1029. // Maintenance - wannabe admins. They can do almost everything.
  1030. $groupLevels['global']['maintenance'] = array_merge($groupLevels['global']['moderator'], array(
  1031. 'manage_attachments',
  1032. 'manage_smileys',
  1033. 'manage_boards',
  1034. 'moderate_forum',
  1035. 'manage_membergroups',
  1036. 'manage_bans',
  1037. 'admin_forum',
  1038. 'manage_permissions',
  1039. 'edit_news',
  1040. 'calendar_edit_any',
  1041. 'profile_identity_any',
  1042. 'profile_extra_any',
  1043. 'profile_title_any',
  1044. ));
  1045. $groupLevels['board']['maintenance'] = array_merge($groupLevels['board']['moderator'], array(
  1046. ));
  1047. // Standard - nothing above the group permissions. (this SHOULD be empty.)
  1048. $boardLevels['standard'] = array(
  1049. );
  1050. // Locked - just that, you can't post here.
  1051. $boardLevels['locked'] = array(
  1052. 'poll_view',
  1053. 'mark_notify',
  1054. 'report_any',
  1055. 'send_topic',
  1056. 'view_attachments',
  1057. );
  1058. // Publisher - just a little more...
  1059. $boardLevels['publish'] = array_merge($boardLevels['locked'], array(
  1060. 'post_new',
  1061. 'post_reply_own',
  1062. 'post_reply_any',
  1063. 'delete_own',
  1064. 'modify_own',
  1065. 'mark_any_notify',
  1066. 'delete_replies',
  1067. 'modify_replies',
  1068. 'poll_vote',
  1069. 'poll_edit_own',
  1070. 'poll_post',
  1071. 'poll_add_own',
  1072. 'poll_remove_own',
  1073. 'post_attachment',
  1074. 'lock_own',
  1075. 'remove_own',
  1076. ));
  1077. // Free for All - Scary. Just scary.
  1078. $boardLevels['free'] = array_merge($boardLevels['publish'], array(
  1079. 'poll_lock_any',
  1080. 'poll_edit_any',
  1081. 'poll_add_any',
  1082. 'poll_remove_any',
  1083. 'make_sticky',
  1084. 'lock_any',
  1085. 'remove_any',
  1086. 'delete_any',
  1087. 'split_any',
  1088. 'merge_any',
  1089. 'modify_any',
  1090. 'approve_posts',
  1091. ));
  1092. // Make sure we're not granting someone too many permissions!
  1093. foreach ($groupLevels['global'][$level] as $k => $permission)
  1094. {
  1095. if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
  1096. unset($groupLevels['global'][$level][$k]);
  1097. if ($group == -1 && in_array($permission, $context['non_guest_permissions']))
  1098. unset($groupLevels['global'][$level][$k]);
  1099. }
  1100. if ($group == -1)
  1101. foreach ($groupLevels['board'][$level] as $k => $permission)
  1102. if (in_array($permission, $context['non_guest_permissions']))
  1103. unset($groupLevels['board'][$level][$k]);
  1104. // Reset all cached permissions.
  1105. updateSettings(array('settings_updated' => time()));
  1106. // Setting group permissions.
  1107. if ($profile === 'null' && $group !== 'null')
  1108. {
  1109. $group = (int) $group;
  1110. if (empty($groupLevels['global'][$level]))
  1111. return;
  1112. $smcFunc['db_query']('', '
  1113. DELETE FROM {db_prefix}permissions
  1114. WHERE id_group = {int:current_group}
  1115. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  1116. array(
  1117. 'current_group' => $group,
  1118. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  1119. )
  1120. );
  1121. $smcFunc['db_query']('', '
  1122. DELETE FROM {db_prefix}board_permissions
  1123. WHERE id_group = {int:current_group}
  1124. AND id_profile = {int:default_profile}',
  1125. array(
  1126. 'current_group' => $group,
  1127. 'default_profile' => 1,
  1128. )
  1129. );
  1130. $groupInserts = array();
  1131. foreach ($groupLevels['global'][$level] as $permission)
  1132. $groupInserts[] = array($group, $permission);
  1133. $smcFunc['db_insert']('insert',
  1134. '{db_prefix}permissions',
  1135. array('id_group' => 'int', 'permission' => 'string'),
  1136. $groupInserts,
  1137. array('id_group')
  1138. );
  1139. $boardInserts = array();
  1140. foreach ($groupLevels['board'][$level] as $permission)
  1141. $boardInserts[] = array(1, $group, $permission);
  1142. $smcFunc['db_insert']('insert',
  1143. '{db_prefix}board_permissions',
  1144. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1145. $boardInserts,
  1146. array('id_profile', 'id_group')
  1147. );
  1148. }
  1149. // Setting profile permissions for a specific group.
  1150. elseif ($profile !== 'null' && $group !== 'null' && ($profile == 1 || $profile > 4))
  1151. {
  1152. $group = (int) $group;
  1153. $profile = (int) $profile;
  1154. if (!empty($groupLevels['global'][$level]))
  1155. {
  1156. $smcFunc['db_query']('', '
  1157. DELETE FROM {db_prefix}board_permissions
  1158. WHERE id_group = {int:current_group}
  1159. AND id_profile = {int:current_profile}',
  1160. array(
  1161. 'current_group' => $group,
  1162. 'current_profile' => $profile,
  1163. )
  1164. );
  1165. }
  1166. if (!empty($groupLevels['board'][$level]))
  1167. {
  1168. $boardInserts = array();
  1169. foreach ($groupLevels['board'][$level] as $permission)
  1170. $boardInserts[] = array($profile, $group, $permission);
  1171. $smcFunc['db_insert']('insert',
  1172. '{db_prefix}board_permissions',
  1173. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1174. $boardInserts,
  1175. array('id_profile', 'id_group')
  1176. );
  1177. }
  1178. }
  1179. // Setting profile permissions for all groups.
  1180. elseif ($profile !== 'null' && $group === 'null' && ($profile == 1 || $profile > 4))
  1181. {
  1182. $profile = (int) $profile;
  1183. $smcFunc['db_query']('', '
  1184. DELETE FROM {db_prefix}board_permissions
  1185. WHERE id_profile = {int:current_profile}',
  1186. array(
  1187. 'current_profile' => $profile,
  1188. )
  1189. );
  1190. if (empty($boardLevels[$level]))
  1191. return;
  1192. // Get all the groups...
  1193. $query = $smcFunc['db_query']('', '
  1194. SELECT id_group
  1195. FROM {db_prefix}membergroups
  1196. WHERE id_group > {int:moderator_group}
  1197. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  1198. array(
  1199. 'moderator_group' => 3,
  1200. 'newbie_group' => 4,
  1201. )
  1202. );
  1203. while ($row = $smcFunc['db_fetch_row']($query))
  1204. {
  1205. $group = $row[0];
  1206. $boardInserts = array();
  1207. foreach ($boardLevels[$level] as $permission)
  1208. $boardInserts[] = array($profile, $group, $permission);
  1209. $smcFunc['db_insert']('insert',
  1210. '{db_prefix}board_permissions',
  1211. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1212. $boardInserts,
  1213. array('id_profile', 'id_group')
  1214. );
  1215. }
  1216. $smcFunc['db_free_result']($query);
  1217. // Add permissions for ungrouped members.
  1218. $boardInserts = array();
  1219. foreach ($boardLevels[$level] as $permission)
  1220. $boardInserts[] = array($profile, 0, $permission);
  1221. $smcFunc['db_insert']('insert',
  1222. '{db_prefix}board_permissions',
  1223. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1224. $boardInserts,
  1225. array('id_profile', 'id_group')
  1226. );
  1227. }
  1228. // $profile and $group are both null!
  1229. else
  1230. fatal_lang_error('no_access', false);
  1231. }
  1232. /**
  1233. * Load permissions into $context['permissions'].
  1234. * @internal
  1235. *
  1236. * @param string $loadType options: 'classic' or 'simple'
  1237. */
  1238. function loadAllPermissions($loadType = 'classic')
  1239. {
  1240. global $context, $txt, $modSettings;
  1241. // List of all the groups dependant on the currently selected view - for the order so it looks pretty, yea?
  1242. // Note to Mod authors - you don't need to stick your permission group here if you don't mind SMF sticking it the last group of the page.
  1243. $permissionGroups = array(
  1244. 'membergroup' => array(
  1245. 'simple' => array(
  1246. 'view_basic_info',
  1247. 'disable_censor',
  1248. 'use_pm_system',
  1249. 'post_calendar',
  1250. 'edit_profile',
  1251. 'delete_account',
  1252. 'use_avatar',
  1253. 'moderate_general',
  1254. 'administrate',
  1255. ),
  1256. 'classic' => array(
  1257. 'general',
  1258. 'pm',
  1259. 'calendar',
  1260. 'maintenance',
  1261. 'member_admin',
  1262. 'profile',
  1263. ),
  1264. ),
  1265. 'board' => array(
  1266. 'simple' => array(
  1267. 'make_posts',
  1268. 'make_unapproved_posts',
  1269. 'post_polls',
  1270. 'participate',
  1271. 'modify',
  1272. 'notification',
  1273. 'attach',
  1274. 'moderate',
  1275. ),
  1276. 'classic' => array(
  1277. 'general_board',
  1278. 'topic',
  1279. 'post',
  1280. 'poll',
  1281. 'notification',
  1282. 'attachment',
  1283. ),
  1284. ),
  1285. );
  1286. /* The format of this list is as follows:
  1287. 'membergroup' => array(
  1288. 'permissions_inside' => array(has_multiple_options, classic_view_group, simple_view_group(_own)*, simple_view_group_any*),
  1289. ),
  1290. 'board' => array(
  1291. 'permissions_inside' => array(has_multiple_options, classic_view_group, simple_view_group(_own)*, simple_view_group_any*),
  1292. );
  1293. */
  1294. $permissionList = array(
  1295. 'membergroup' => array(
  1296. 'view_stats' => array(false, 'general', 'view_basic_info'),
  1297. 'view_mlist' => array(false, 'general', 'view_basic_info'),
  1298. 'who_view' => array(false, 'general', 'view_basic_info'),
  1299. 'search_posts' => array(false, 'general', 'view_basic_info'),
  1300. 'karma_edit' => array(false, 'general', 'moderate_general'),
  1301. 'disable_censor' => array(false, 'general', 'disable_censor'),
  1302. 'pm_read' => array(false, 'pm', 'use_pm_system'),
  1303. 'pm_send' => array(false, 'pm', 'use_pm_system'),
  1304. 'pm_draft' => array(false, 'pm', 'use_pm_system'),
  1305. 'pm_autosave_draft' => array(false, 'pm', 'use_pm_system'),
  1306. 'send_email_to_members' => array(false, 'pm', 'use_pm_system'),
  1307. 'calendar_view' => array(false, 'calendar', 'view_basic_info'),
  1308. 'calendar_post' => array(false, 'calendar', 'post_calendar'),
  1309. 'calendar_edit' => array(true, 'calendar', 'post_calendar', 'moderate_general'),
  1310. 'admin_forum' => array(false, 'maintenance', 'administrate'),
  1311. 'manage_boards' => array(false, 'maintenance', 'administrate'),
  1312. 'manage_attachments' => array(false, 'maintenance', 'administrate'),
  1313. 'manage_smileys' => array(false, 'maintenance', 'administrate'),
  1314. 'edit_news' => array(false, 'maintenance', 'administrate'),
  1315. 'access_mod_center' => array(false, 'maintenance', 'moderate_general'),
  1316. 'moderate_forum' => array(false, 'member_admin', 'moderate_general'),
  1317. 'manage_membergroups' => array(false, 'member_admin', 'administrate'),
  1318. 'manage_permissions' => array(false, 'member_admin', 'administrate'),
  1319. 'manage_bans' => array(false, 'member_admin', 'administrate'),
  1320. 'send_mail' => array(false, 'member_admin', 'administrate'),
  1321. 'issue_warning' => array(false, 'member_admin', 'moderate_general'),
  1322. 'profile_view' => array(true, 'profile', 'view_basic_info', 'view_basic_info'),
  1323. 'profile_identity' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1324. 'profile_extra' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1325. 'profile_title' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1326. 'profile_remove' => array(true, 'profile', 'delete_account', 'moderate_general'),
  1327. 'profile_server_avatar' => array(false, 'profile', 'use_avatar'),
  1328. 'profile_upload_avatar' => array(false, 'profile', 'use_avatar'),
  1329. 'profile_remote_avatar' => array(false, 'profile', 'use_avatar'),
  1330. ),
  1331. 'board' => array(
  1332. 'moderate_board' => array(false, 'general_board', 'moderate'),
  1333. 'approve_posts' => array(false, 'general_board', 'moderate'),
  1334. 'post_new' => array(false, 'topic', 'make_posts'),
  1335. 'post_draft' => array(false, 'topic', 'make_posts'),
  1336. 'post_autosave_draft' => array(false, 'topic', 'make_posts'),
  1337. 'post_unapproved_topics' => array(false, 'topic', 'make_unapproved_posts'),
  1338. 'post_unapproved_replies' => array(true, 'topic', 'make_unapproved_posts', 'make_unapproved_posts'),
  1339. 'post_reply' => array(true, 'topic', 'make_posts', 'make_posts'),
  1340. 'merge_any' => array(false, 'topic', 'moderate'),
  1341. 'split_any' => array(false, 'topic', 'moderate'),
  1342. 'send_topic' => array(false, 'topic', 'moderate'),
  1343. 'make_sticky' => array(false, 'topic', 'moderate'),
  1344. 'move' => array(true, 'topic', 'moderate', 'moderate'),
  1345. 'lock' => array(true, 'topic', 'moderate', 'moderate'),
  1346. 'remove' => array(true, 'topic', 'modify', 'moderate'),
  1347. 'modify_replies' => array(false, 'topic', 'moderate'),
  1348. 'delete_replies' => array(false, 'topic', 'moderate'),
  1349. 'announce_topic' => array(false, 'topic', 'moderate'),
  1350. 'delete' => array(true, 'post', 'modify', 'moderate'),
  1351. 'modify' => array(true, 'post', 'modify', 'moderate'),
  1352. 'report_any' => array(false, 'post', 'participate'),
  1353. 'poll_view' => array(false, 'poll', 'participate'),
  1354. 'poll_vote' => array(false, 'poll', 'participate'),
  1355. 'poll_post' => array(false, 'poll', 'post_polls'),
  1356. 'poll_add' => array(true, 'poll', 'post_polls', 'moderate'),
  1357. 'poll_edit' => array(true, 'poll', 'modify', 'moderate'),
  1358. 'poll_lock' => array(true, 'poll', 'moderate', 'moderate'),
  1359. 'poll_remove' => array(true, 'poll', 'modify', 'moderate'),
  1360. 'mark_any_notify' => array(false, 'notification', 'notification'),
  1361. 'mark_notify' => array(false, 'notification', 'notification'),
  1362. 'view_attachments' => array(false, 'attachment', 'participate'),
  1363. 'post_unapproved_attachments' => array(false, 'attachment', 'make_unapproved_posts'),
  1364. 'post_attachment' => array(false, 'attachment', 'attach'),
  1365. ),
  1366. );
  1367. // All permission groups that will be shown in the left column on classic view.
  1368. $leftPermissionGroups = array(
  1369. 'general',
  1370. 'calendar',
  1371. 'maintenance',
  1372. 'member_admin',
  1373. 'topic',
  1374. 'post',
  1375. );
  1376. // We need to know what permissions we can't give to guests.
  1377. loadIllegalGuestPermissions();
  1378. // Some permissions are hidden if features are off.
  1379. $hiddenPermissions = array();
  1380. $relabelPermissions = array(); // Permissions to apply a different label to.
  1381. $relabelGroups = array(); // As above but for groups.
  1382. if (!in_array('cd', $context['admin_features']))
  1383. {
  1384. $hiddenPermissions[] = 'calendar_view';
  1385. $hiddenPermissions[] = 'calendar_post';
  1386. $hiddenPermissions[] = 'calendar_edit';
  1387. }
  1388. if (!in_array('w', $context['admin_features']))
  1389. $hiddenPermissions[] = 'issue_warning';
  1390. if (!in_array('k', $cont

Large files files are truncated, but you can click here to view the full file