ManagePermissions.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404
  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 2011 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'),
  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']]) ? $_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. 'stars' => '',
  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. 'stars' => '',
  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, stars
  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['stars'] = explode('#', $row['stars']);
  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. 'stars' => !empty($row['stars'][0]) && !empty($row['stars'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['stars'][1] . '" alt="*" />', $row['stars'][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, {db_prefix}board_permissions
  882. WHERE add_deny = {int:denied}',
  883. array(
  884. 'denied' => 0,
  885. )
  886. );
  887. }
  888. // Make sure there are no postgroup based permissions left.
  889. if (empty($modSettings['permission_enable_postgroups']))
  890. {
  891. // Get a list of postgroups.
  892. $post_groups = array();
  893. $request = $smcFunc['db_query']('', '
  894. SELECT id_group
  895. FROM {db_prefix}membergroups
  896. WHERE min_posts != {int:min_posts}',
  897. array(
  898. 'min_posts' => -1,
  899. )
  900. );
  901. while ($row = $smcFunc['db_fetch_assoc']($request))
  902. $post_groups[] = $row['id_group'];
  903. $smcFunc['db_free_result']($request);
  904. // Remove'em.
  905. $smcFunc['db_query']('', '
  906. DELETE FROM {db_prefix}permissions, {db_prefix}board_permissions
  907. WHERE id_group IN ({array_int:post_group_list})',
  908. array(
  909. 'post_group_list' => $post_groups,
  910. )
  911. );
  912. $smcFunc['db_query']('', '
  913. UPDATE {db_prefix}membergroups
  914. SET id_parent = {int:not_inherited}
  915. WHERE id_parent IN ({array_int:post_group_list})',
  916. array(
  917. 'post_group_list' => $post_groups,
  918. 'not_inherited' => -2,
  919. )
  920. );
  921. }
  922. redirectexit('action=admin;area=permissions;sa=settings');
  923. }
  924. prepareDBSettingContext($config_vars);
  925. }
  926. /**
  927. * Set the permission level for a specific profile, group, or group for a profile.
  928. * @internal
  929. *
  930. * @param string $level
  931. * @param int $group
  932. * @param mixed $profile = null, int expected
  933. */
  934. function setPermissionLevel($level, $group, $profile = 'null')
  935. {
  936. global $smcFunc, $context;
  937. loadIllegalPermissions();
  938. loadIllegalGuestPermissions();
  939. // Levels by group... restrict, standard, moderator, maintenance.
  940. $groupLevels = array(
  941. 'board' => array('inherit' => array()),
  942. 'group' => array('inherit' => array())
  943. );
  944. // Levels by board... standard, publish, free.
  945. $boardLevels = array('inherit' => array());
  946. // Restrictive - ie. guests.
  947. $groupLevels['global']['restrict'] = array(
  948. 'search_posts',
  949. 'calendar_view',
  950. 'view_stats',
  951. 'who_view',
  952. 'profile_view_own',
  953. 'profile_identity_own',
  954. );
  955. $groupLevels['board']['restrict'] = array(
  956. 'poll_view',
  957. 'post_new',
  958. 'post_reply_own',
  959. 'post_reply_any',
  960. 'delete_own',
  961. 'modify_own',
  962. 'mark_any_notify',
  963. 'mark_notify',
  964. 'report_any',
  965. 'send_topic',
  966. );
  967. // Standard - ie. members. They can do anything Restrictive can.
  968. $groupLevels['global']['standard'] = array_merge($groupLevels['global']['restrict'], array(
  969. 'view_mlist',
  970. 'karma_edit',
  971. 'pm_read',
  972. 'pm_send',
  973. 'profile_view_any',
  974. 'profile_extra_own',
  975. 'profile_server_avatar',
  976. 'profile_upload_avatar',
  977. 'profile_remote_avatar',
  978. 'profile_remove_own',
  979. ));
  980. $groupLevels['board']['standard'] = array_merge($groupLevels['board']['restrict'], array(
  981. 'poll_vote',
  982. 'poll_edit_own',
  983. 'poll_post',
  984. 'poll_add_own',
  985. 'post_attachment',
  986. 'lock_own',
  987. 'remove_own',
  988. 'view_attachments',
  989. ));
  990. // Moderator - ie. moderators :P. They can do what standard can, and more.
  991. $groupLevels['global']['moderator'] = array_merge($groupLevels['global']['standard'], array(
  992. 'calendar_post',
  993. 'calendar_edit_own',
  994. 'access_mod_center',
  995. 'issue_warning',
  996. ));
  997. $groupLevels['board']['moderator'] = array_merge($groupLevels['board']['standard'], array(
  998. 'make_sticky',
  999. 'poll_edit_any',
  1000. 'delete_any',
  1001. 'modify_any',
  1002. 'lock_any',
  1003. 'remove_any',
  1004. 'move_any',
  1005. 'merge_any',
  1006. 'split_any',
  1007. 'poll_lock_any',
  1008. 'poll_remove_any',
  1009. 'poll_add_any',
  1010. 'approve_posts',
  1011. ));
  1012. // Maintenance - wannabe admins. They can do almost everything.
  1013. $groupLevels['global']['maintenance'] = array_merge($groupLevels['global']['moderator'], array(
  1014. 'manage_attachments',
  1015. 'manage_smileys',
  1016. 'manage_boards',
  1017. 'moderate_forum',
  1018. 'manage_membergroups',
  1019. 'manage_bans',
  1020. 'admin_forum',
  1021. 'manage_permissions',
  1022. 'edit_news',
  1023. 'calendar_edit_any',
  1024. 'profile_identity_any',
  1025. 'profile_extra_any',
  1026. 'profile_title_any',
  1027. ));
  1028. $groupLevels['board']['maintenance'] = array_merge($groupLevels['board']['moderator'], array(
  1029. ));
  1030. // Standard - nothing above the group permissions. (this SHOULD be empty.)
  1031. $boardLevels['standard'] = array(
  1032. );
  1033. // Locked - just that, you can't post here.
  1034. $boardLevels['locked'] = array(
  1035. 'poll_view',
  1036. 'mark_notify',
  1037. 'report_any',
  1038. 'send_topic',
  1039. 'view_attachments',
  1040. );
  1041. // Publisher - just a little more...
  1042. $boardLevels['publish'] = array_merge($boardLevels['locked'], array(
  1043. 'post_new',
  1044. 'post_reply_own',
  1045. 'post_reply_any',
  1046. 'delete_own',
  1047. 'modify_own',
  1048. 'mark_any_notify',
  1049. 'delete_replies',
  1050. 'modify_replies',
  1051. 'poll_vote',
  1052. 'poll_edit_own',
  1053. 'poll_post',
  1054. 'poll_add_own',
  1055. 'poll_remove_own',
  1056. 'post_attachment',
  1057. 'lock_own',
  1058. 'remove_own',
  1059. ));
  1060. // Free for All - Scary. Just scary.
  1061. $boardLevels['free'] = array_merge($boardLevels['publish'], array(
  1062. 'poll_lock_any',
  1063. 'poll_edit_any',
  1064. 'poll_add_any',
  1065. 'poll_remove_any',
  1066. 'make_sticky',
  1067. 'lock_any',
  1068. 'remove_any',
  1069. 'delete_any',
  1070. 'split_any',
  1071. 'merge_any',
  1072. 'modify_any',
  1073. 'approve_posts',
  1074. ));
  1075. // Make sure we're not granting someone too many permissions!
  1076. foreach ($groupLevels['global'][$level] as $k => $permission)
  1077. {
  1078. if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
  1079. unset($groupLevels['global'][$level][$k]);
  1080. if ($group == -1 && in_array($permission, $context['non_guest_permissions']))
  1081. unset($groupLevels['global'][$level][$k]);
  1082. }
  1083. if ($group == -1)
  1084. foreach ($groupLevels['board'][$level] as $k => $permission)
  1085. if (in_array($permission, $context['non_guest_permissions']))
  1086. unset($groupLevels['board'][$level][$k]);
  1087. // Reset all cached permissions.
  1088. updateSettings(array('settings_updated' => time()));
  1089. // Setting group permissions.
  1090. if ($profile === 'null' && $group !== 'null')
  1091. {
  1092. $group = (int) $group;
  1093. if (empty($groupLevels['global'][$level]))
  1094. return;
  1095. $smcFunc['db_query']('', '
  1096. DELETE FROM {db_prefix}permissions
  1097. WHERE id_group = {int:current_group}
  1098. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  1099. array(
  1100. 'current_group' => $group,
  1101. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  1102. )
  1103. );
  1104. $smcFunc['db_query']('', '
  1105. DELETE FROM {db_prefix}board_permissions
  1106. WHERE id_group = {int:current_group}
  1107. AND id_profile = {int:default_profile}',
  1108. array(
  1109. 'current_group' => $group,
  1110. 'default_profile' => 1,
  1111. )
  1112. );
  1113. $groupInserts = array();
  1114. foreach ($groupLevels['global'][$level] as $permission)
  1115. $groupInserts[] = array($group, $permission);
  1116. $smcFunc['db_insert']('insert',
  1117. '{db_prefix}permissions',
  1118. array('id_group' => 'int', 'permission' => 'string'),
  1119. $groupInserts,
  1120. array('id_group')
  1121. );
  1122. $boardInserts = array();
  1123. foreach ($groupLevels['board'][$level] as $permission)
  1124. $boardInserts[] = array(1, $group, $permission);
  1125. $smcFunc['db_insert']('insert',
  1126. '{db_prefix}board_permissions',
  1127. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1128. $boardInserts,
  1129. array('id_profile', 'id_group')
  1130. );
  1131. }
  1132. // Setting profile permissions for a specific group.
  1133. elseif ($profile !== 'null' && $group !== 'null' && ($profile == 1 || $profile > 4))
  1134. {
  1135. $group = (int) $group;
  1136. $profile = (int) $profile;
  1137. if (!empty($groupLevels['global'][$level]))
  1138. {
  1139. $smcFunc['db_query']('', '
  1140. DELETE FROM {db_prefix}board_permissions
  1141. WHERE id_group = {int:current_group}
  1142. AND id_profile = {int:current_profile}',
  1143. array(
  1144. 'current_group' => $group,
  1145. 'current_profile' => $profile,
  1146. )
  1147. );
  1148. }
  1149. if (!empty($groupLevels['board'][$level]))
  1150. {
  1151. $boardInserts = array();
  1152. foreach ($groupLevels['board'][$level] as $permission)
  1153. $boardInserts[] = array($profile, $group, $permission);
  1154. $smcFunc['db_insert']('insert',
  1155. '{db_prefix}board_permissions',
  1156. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1157. $boardInserts,
  1158. array('id_profile', 'id_group')
  1159. );
  1160. }
  1161. }
  1162. // Setting profile permissions for all groups.
  1163. elseif ($profile !== 'null' && $group === 'null' && ($profile == 1 || $profile > 4))
  1164. {
  1165. $profile = (int) $profile;
  1166. $smcFunc['db_query']('', '
  1167. DELETE FROM {db_prefix}board_permissions
  1168. WHERE id_profile = {int:current_profile}',
  1169. array(
  1170. 'current_profile' => $profile,
  1171. )
  1172. );
  1173. if (empty($boardLevels[$level]))
  1174. return;
  1175. // Get all the groups...
  1176. $query = $smcFunc['db_query']('', '
  1177. SELECT id_group
  1178. FROM {db_prefix}membergroups
  1179. WHERE id_group > {int:moderator_group}
  1180. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  1181. array(
  1182. 'moderator_group' => 3,
  1183. 'newbie_group' => 4,
  1184. )
  1185. );
  1186. while ($row = $smcFunc['db_fetch_row']($query))
  1187. {
  1188. $group = $row[0];
  1189. $boardInserts = array();
  1190. foreach ($boardLevels[$level] as $permission)
  1191. $boardInserts[] = array($profile, $group, $permission);
  1192. $smcFunc['db_insert']('insert',
  1193. '{db_prefix}board_permissions',
  1194. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1195. $boardInserts,
  1196. array('id_profile', 'id_group')
  1197. );
  1198. }
  1199. $smcFunc['db_free_result']($query);
  1200. // Add permissions for ungrouped members.
  1201. $boardInserts = array();
  1202. foreach ($boardLevels[$level] as $permission)
  1203. $boardInserts[] = array($profile, 0, $permission);
  1204. $smcFunc['db_insert']('insert',
  1205. '{db_prefix}board_permissions',
  1206. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1207. $boardInserts,
  1208. array('id_profile', 'id_group')
  1209. );
  1210. }
  1211. // $profile and $group are both null!
  1212. else
  1213. fatal_lang_error('no_access', false);
  1214. }
  1215. /**
  1216. * Load permissions into $context['permissions'].
  1217. * @internal
  1218. *
  1219. * @param string $loadType, options: 'classic' or 'simple'
  1220. */
  1221. function loadAllPermissions($loadType = 'classic')
  1222. {
  1223. global $context, $txt, $modSettings;
  1224. // List of all the groups dependant on the currently selected view - for the order so it looks pretty, yea?
  1225. // 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.
  1226. $permissionGroups = array(
  1227. 'membergroup' => array(
  1228. 'simple' => array(
  1229. 'view_basic_info',
  1230. 'use_pm_system',
  1231. 'post_calendar',
  1232. 'edit_profile',
  1233. 'delete_account',
  1234. 'use_avatar',
  1235. 'moderate_general',
  1236. 'administrate',
  1237. ),
  1238. 'classic' => array(
  1239. 'general',
  1240. 'pm',
  1241. 'calendar',
  1242. 'maintenance',
  1243. 'member_admin',
  1244. 'profile',
  1245. ),
  1246. ),
  1247. 'board' => array(
  1248. 'simple' => array(
  1249. 'make_posts',
  1250. 'make_unapproved_posts',
  1251. 'post_polls',
  1252. 'participate',
  1253. 'modify',
  1254. 'notification',
  1255. 'attach',
  1256. 'moderate',
  1257. ),
  1258. 'classic' => array(
  1259. 'general_board',
  1260. 'topic',
  1261. 'post',
  1262. 'poll',
  1263. 'notification',
  1264. 'attachment',
  1265. ),
  1266. ),
  1267. );
  1268. /* The format of this list is as follows:
  1269. 'membergroup' => array(
  1270. 'permissions_inside' => array(has_multiple_options, classic_view_group, simple_view_group(_own)*, simple_view_group_any*),
  1271. ),
  1272. 'board' => array(
  1273. 'permissions_inside' => array(has_multiple_options, classic_view_group, simple_view_group(_own)*, simple_view_group_any*),
  1274. );
  1275. */
  1276. $permissionList = array(
  1277. 'membergroup' => array(
  1278. 'view_stats' => array(false, 'general', 'view_basic_info'),
  1279. 'view_mlist' => array(false, 'general', 'view_basic_info'),
  1280. 'who_view' => array(false, 'general', 'view_basic_info'),
  1281. 'search_posts' => array(false, 'general', 'view_basic_info'),
  1282. 'karma_edit' => array(false, 'general', 'moderate_general'),
  1283. 'disable_censor' => array(false, 'general', 'disable_censor'),
  1284. 'pm_read' => array(false, 'pm', 'use_pm_system'),
  1285. 'pm_send' => array(false, 'pm', 'use_pm_system'),
  1286. 'calendar_view' => array(false, 'calendar', 'view_basic_info'),
  1287. 'calendar_post' => array(false, 'calendar', 'post_calendar'),
  1288. 'calendar_edit' => array(true, 'calendar', 'post_calendar', 'moderate_general'),
  1289. 'admin_forum' => array(false, 'maintenance', 'administrate'),
  1290. 'manage_boards' => array(false, 'maintenance', 'administrate'),
  1291. 'manage_attachments' => array(false, 'maintenance', 'administrate'),
  1292. 'manage_smileys' => array(false, 'maintenance', 'administrate'),
  1293. 'edit_news' => array(false, 'maintenance', 'administrate'),
  1294. 'access_mod_center' => array(false, 'maintenance', 'moderate_general'),
  1295. 'moderate_forum' => array(false, 'member_admin', 'moderate_general'),
  1296. 'manage_membergroups' => array(false, 'member_admin', 'administrate'),
  1297. 'manage_permissions' => array(false, 'member_admin', 'administrate'),
  1298. 'manage_bans' => array(false, 'member_admin', 'administrate'),
  1299. 'send_mail' => array(false, 'member_admin', 'administrate'),
  1300. 'issue_warning' => array(false, 'member_admin', 'moderate_general'),
  1301. 'profile_view' => array(true, 'profile', 'view_basic_info', 'view_basic_info'),
  1302. 'profile_identity' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1303. 'profile_extra' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1304. 'profile_title' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1305. 'profile_remove' => array(true, 'profile', 'delete_account', 'moderate_general'),
  1306. 'profile_server_avatar' => array(false, 'profile', 'use_avatar'),
  1307. 'profile_upload_avatar' => array(false, 'profile', 'use_avatar'),
  1308. 'profile_remote_avatar' => array(false, 'profile', 'use_avatar'),
  1309. ),
  1310. 'board' => array(
  1311. 'moderate_board' => array(false, 'general_board', 'moderate'),
  1312. 'approve_posts' => array(false, 'general_board', 'moderate'),
  1313. 'post_new' => array(false, 'topic', 'make_posts'),
  1314. 'post_unapproved_topics' => array(false, 'topic', 'make_unapproved_posts'),
  1315. 'post_unapproved_replies' => array(true, 'topic', 'make_unapproved_posts', 'make_unapproved_posts'),
  1316. 'post_reply' => array(true, 'topic', 'make_posts', 'make_posts'),
  1317. 'merge_any' => array(false, 'topic', 'moderate'),
  1318. 'split_any' => array(false, 'topic', 'moderate'),
  1319. 'send_topic' => array(false, 'topic', 'moderate'),
  1320. 'make_sticky' => array(false, 'topic', 'moderate'),
  1321. 'move' => array(true, 'topic', 'moderate', 'moderate'),
  1322. 'lock' => array(true, 'topic', 'moderate', 'moderate'),
  1323. 'remove' => array(true, 'topic', 'modify', 'moderate'),
  1324. 'modify_replies' => array(false, 'topic', 'moderate'),
  1325. 'delete_replies' => array(false, 'topic', 'moderate'),
  1326. 'announce_topic' => array(false, 'topic', 'moderate'),
  1327. 'delete' => array(true, 'post', 'modify', 'moderate'),
  1328. 'modify' => array(true, 'post', 'modify', 'moderate'),
  1329. 'report_any' => array(false, 'post', 'participate'),
  1330. 'poll_view' => array(false, 'poll', 'participate'),
  1331. 'poll_vote' => array(false, 'poll', 'participate'),
  1332. 'poll_post' => array(false, 'poll', 'post_polls'),
  1333. 'poll_add' => array(true, 'poll', 'post_polls', 'moderate'),
  1334. 'poll_edit' => array(true, 'poll', 'modify', 'moderate'),
  1335. 'poll_lock' => array(true, 'poll', 'moderate', 'moderate'),
  1336. 'poll_remove' => array(true, 'poll', 'modify', 'moderate'),
  1337. 'mark_any_notify' => array(false, 'notification', 'notification'),
  1338. 'mark_notify' => array(false, 'notification', 'notification'),
  1339. 'view_attachments' => array(false, 'attachment', 'participate'),
  1340. 'post_unapproved_attachments' => array(false, 'attachment', 'make_unapproved_posts'),
  1341. 'post_attachment' => array(false, 'attachment', 'attach'),
  1342. ),
  1343. );
  1344. // All permission groups that will be shown in the left column on classic view.
  1345. $leftPermissionGroups = array(
  1346. 'general',
  1347. 'calendar',
  1348. 'maintenance',
  1349. 'member_admin',
  1350. 'topic',
  1351. 'post',
  1352. );
  1353. // We need to know what permissions we can't give to guests.
  1354. loadIllegalGuestPermissions();
  1355. // Some permissions are hidden if features are off.
  1356. $hiddenPermissions = array();
  1357. $relabelPermissions = array(); // Permissions to apply a different label to.
  1358. $relabelGroups = array(); // As above but for groups.
  1359. if (!in_array('cd', $context['admin_features']))
  1360. {
  1361. $hiddenPermissions[] = 'calendar_view';
  1362. $hiddenPermissions[] = 'calendar_post';
  1363. $hiddenPermissions[] = 'calendar_edit';
  1364. }
  1365. if (!in_array('w', $context['admin_features']))
  1366. $hiddenPermissions[] = 'issue_warning';
  1367. // Post moderation?
  1368. if (!$modSettings['postmod_active'])
  1369. {
  1370. $hiddenPermissions[] = 'approve_posts';
  1371. $hiddenPermissions[] = 'post_unapproved_topics';
  1372. $hiddenPermissions[] = 'post_unapproved_replies';
  1373. $hiddenPermissions[] = 'post_unapproved_attachments';
  1374. }
  1375. // If we show them on classic view we change the name.
  1376. else
  1377. {
  1378. // Relabel the topics permissions
  1379. $relabelPermissions['post_new'] = 'auto_approve_topics';
  1380. // Relabel the reply permissions
  1381. $relabelPermissions['post_reply'] = 'auto_approve_replies';
  1382. // Relabel the attachment permissions
  1383. $relabelPermissions['post_attachment'] = 'auto_approve_attachments';
  1384. }
  1385. // Provide a practical way to modify permissions.
  1386. call_integration_hook('integrate_load_permissions', array(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions));
  1387. $context['permissions'] = array();
  1388. $context['hidden_permissions'] = array();
  1389. foreach ($permissionList as $permissionType => $permissionList)
  1390. {
  1391. $context['permissions'][$permissionType] = array(
  1392. 'id' => $permissionType,
  1393. 'view' => $loadType,
  1394. 'columns' => array()
  1395. );
  1396. foreach ($permissionList as $permission => $permissionArray)
  1397. {
  1398. // If this is a guest permission we don't do it if it's the guest group.
  1399. if (isset($context['group']['id']) && $context['group']['id'] == -1 && in_array($permission, $context['non_guest_permissions']))
  1400. continue;
  1401. // What groups will this permission be in?
  1402. $own_group = $permissionArray[($loadType == 'classic' ? 1 : 2)];
  1403. $any_group = $loadType == 'simple' && !empty($permissionArray[3]) ? $permissionArray[3] : ($loadType == 'simple' && $permissionArray[0] ? $permissionArray[2] : '');
  1404. // First, Do these groups actually exist - if not add them.
  1405. if (!isset($permissionGroups[$permissionType][$loadType][$own_group]))
  1406. $permissionGroups[$permissionType][$loadType][$own_group] = true;
  1407. if (!empty($any_group) && !isset($permissionGroups[$permissionType][$loadType][$any_group]))
  1408. $permissionGroups[$permissionType][$loadType][$any_group] = true;
  1409. // What column should this be located into?
  1410. $position = $loadType == 'classic' && !in_array($own_group, $leftPermissionGroups) ? 1 : 0;
  1411. // If the groups have not yet been created be sure to create them.
  1412. $bothGroups = array('own' => $own_group);
  1413. $bothGroups = array();
  1414. // For guests, just reset the array.
  1415. if (!isset($context['group']['id']) || !($context['group']['id'] == -1 && $any_group))
  1416. $bothGroups['own'] = $own_group;
  1417. if ($any_group)
  1418. {
  1419. $bothGroups['any'] = $any_group;
  1420. }
  1421. foreach ($bothGroups as $group)
  1422. if (!isset($context['permissions'][$permissionType]['columns'][$position][$group]))
  1423. $context['permissions'][$permissionType]['columns'][$position][$group] = array(
  1424. 'type' => $permissionType,
  1425. 'id' => $group,
  1426. 'name' => $loadType == 'simple' ? (isset($txt['permissiongroup_simple_' . $group]) ? $txt['permissiongroup_simple_' . $group] : '') : $txt['permissiongroup_' . $group],
  1427. 'icon' => isset($txt['permissionicon_' . $group]) ? $txt['permissionicon_' . $group] : $txt['permissionicon'],
  1428. 'help' => isset($txt['permissionhelp_' . $group]) ? $txt['permissionhelp_' . $group] : '',
  1429. 'hidden' => false,
  1430. 'permissions' => array()
  1431. );
  1432. // This is where we set up the permission dependant on the view.
  1433. if ($loadType == 'classic')
  1434. {
  1435. $context['permissions'][$permissionType]['columns'][$position][$own_group]['permissions'][$permission] = array(
  1436. 'id' => $permission,
  1437. 'name' => !isset($relabelPermissions[$permission]) ? $txt['permissionname_' . $permission] : $txt[$relabelPermissions[$permission]],
  1438. 'show_help' => isset($txt['permissionhelp_' . $permission]),
  1439. 'note' => isset($txt['permissionnote_' . $permission]) ? $txt['permissionnote_' . $permission] : '',
  1440. 'has_own_any' => $permissionArray[0],
  1441. 'own' => array(
  1442. 'id' => $permission . '_own',
  1443. 'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_own'] : ''
  1444. ),
  1445. 'any' => array(
  1446. 'id' => $permission . '_any',
  1447. 'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_any'] : ''
  1448. ),
  1449. 'hidden' => in_array($permission, $hiddenPermissions),
  1450. );
  1451. }
  1452. else
  1453. {
  1454. foreach ($bothGroups as $group_type => $group)
  1455. {
  1456. $context['permissions'][$permissionType]['columns'][$position][$group]['permissions'][$permission . ($permissionArray[0] ? '_' . $group_type : '')] = array(
  1457. 'id' => $permission . ($permissionArray[0] ? '_' . $group_type : ''),
  1458. 'name' => isset($txt['permissionname_simple_' . $permission . ($permissionArray[0] ? '_' . $group_type : '')]) ? $txt['permissionname_simple_' . $permission . ($permissionArray[0] ? '_' . $group_type : '')] : $txt['permissionname_' . $permission],
  1459. 'help_index' => isset($txt['permissionhelp_' . $permission]) ? 'permissionhelp_' . $permission : '',
  1460. 'hidden' => in_array($permission, $hiddenPermissions),
  1461. );
  1462. }
  1463. }
  1464. if (in_array($permission, $hiddenPermissions))
  1465. {
  1466. if ($permissionArray[0])
  1467. {
  1468. $context['hidden_permissions'][] = $permission . '_own';
  1469. $context['hidden_permissions'][] = $permission . '_any';
  1470. }
  1471. else
  1472. $context['hidden_permissions'][] = $permission;
  1473. }
  1474. }
  1475. ksort($context['permissions'][$permissionType]['columns']);
  1476. }
  1477. // Check we don't leave any empty groups - and mark hidden ones as such.
  1478. foreach ($context['permissions'][$permissionType]['columns'] as $column => $groups)
  1479. foreach ($groups as $id => $group)
  1480. {
  1481. if (empty($group['permissions']))
  1482. unset($context['permissions'][$permissionType]['columns'][$column][$id]);
  1483. else
  1484. {
  1485. $foundNonHidden = false;
  1486. foreach ($group['permissions'] as $permission)
  1487. if (empty($permission['hidden']))
  1488. $foundNonHidden = true;
  1489. if (!$foundNonHidden)
  1490. $context['permissions'][$permissionType]['columns'][$column][$id]['hidden'] = true;
  1491. }
  1492. }
  1493. }
  1494. //
  1495. /**
  1496. * Initialize a form with inline permissions settings.
  1497. * It loads a context variables for each permission.
  1498. * This function is used by several settings screens to set specific permissions.
  1499. * @internal
  1500. *
  1501. * @param array $permissions
  1502. * @param array $excluded_groups = array()
  1503. *
  1504. * @uses ManagePermissions language
  1505. * @uses ManagePermissions template.
  1506. */
  1507. function init_inline_permissions($permissions, $excluded_groups = array())
  1508. {
  1509. global $context, $txt, $modSettings, $smcFunc;
  1510. loadLanguage('ManagePermissions');
  1511. loadTemplate('ManagePermissions');
  1512. $context['can_change_permissions'] = allowedTo('manage_permissions');
  1513. // Nothing to initialize here.
  1514. if (!$context['can_change_permissions'])
  1515. return;
  1516. // Load the permission settings for guests
  1517. foreach ($permissions as $permission)
  1518. $context[$permission] = array(
  1519. -1 => array(
  1520. 'id' => -1,
  1521. 'name' => $txt['membergroups_guests'],
  1522. 'is_postgroup' => false,
  1523. 'status' => 'off',
  1524. ),
  1525. 0 => array(
  1526. 'id' => 0,
  1527. 'name' => $txt['membergroups_members'],
  1528. 'is_postgroup' => false,
  1529. 'status' => 'off',
  1530. ),
  1531. );
  1532. $request = $smcFunc['db_query']('', '
  1533. SELECT id_group, CASE WHEN add_deny = {int:denied} THEN {string:deny} ELSE {string:on} END AS status, permission
  1534. FROM {db_prefix}permissions
  1535. WHERE id_group IN (-1, 0)
  1536. AND permission IN ({array_string:permissions})',
  1537. array(
  1538. 'denied' => 0,
  1539. 'permissions' => $permissions,
  1540. 'deny' => 'deny',
  1541. 'on' => 'on',
  1542. )
  1543. );
  1544. while ($row = $smcFunc['db_fetch_assoc']($request))
  1545. $context[$row['permission']][$row['id_group']]['status'] = $row['status'];
  1546. $smcFunc['db_free_result']($request);
  1547. $request = $smcFunc['db_query']('', '
  1548. SELECT mg.id_group, mg.group_name, mg.min_posts, IFNULL(p.add_deny, -1) AS status, p.permission
  1549. FROM {db_prefix}membergroups AS mg
  1550. LEFT JOIN {db_prefix}permissions AS p ON (p.id_group = mg.id_group AND p.permission IN ({array_string:permissions}))
  1551. WHERE mg.id_group NOT IN (1, 3)
  1552. AND mg.id_parent = {int:not_inherited}' . (empty($modSettings['permission_enable_postgroups']) ? '
  1553. AND mg.min_posts = {int:min_posts}' : '') . '
  1554. ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
  1555. array(
  1556. 'not_inherited' => -2,
  1557. 'min_posts' => -1,
  1558. 'newbie_group' => 4,
  1559. 'permissions' => $permissions,
  1560. )
  1561. );
  1562. while ($row = $smcFunc['db_fetch_assoc']($request))
  1563. {
  1564. // Initialize each permission as being 'off' until proven otherwise.
  1565. foreach ($permissions as $permission)
  1566. if (!isset($context[$permission][$row['id_group']]))
  1567. $context[$permission][$row['id_group']] = array(
  1568. 'id' => $row['id_group'],
  1569. 'name' => $row['group_name'],
  1570. 'is_postgroup' => $row['min_posts'] != -1,
  1571. 'status' => 'off',
  1572. );
  1573. $context[$row['permission']][$row['id_group']]['status'] = empty($row['status']) ? 'deny' : ($row['status'] == 1 ? 'on' : 'off');
  1574. }
  1575. $smcFunc['db_free_result']($request);
  1576. // Some permissions cannot be given to certain groups. Remove the groups.
  1577. foreach ($excluded_groups as $group)
  1578. {
  1579. foreach ($permissions as $permission)
  1580. {
  1581. if (isset($context[$permission][$group]))
  1582. unset($context[$permission][$group]);
  1583. }
  1584. }
  1585. }
  1586. /**
  1587. * Show a collapsible box to set a specific permission.
  1588. * The function is called by templates to show a list of permissions settings.
  1589. * Calls the template function template_inline_permissions().
  1590. *
  1591. * @param string $permission
  1592. */
  1593. function theme_inline_permissions($permission)
  1594. {
  1595. global $context;
  1596. $context['current_permission'] = $permission;
  1597. $context['member_groups'] = $context[$permission];
  1598. template_inline_permissions();
  1599. }
  1600. /**
  1601. * Save the permissions of a form containing inline permissions.
  1602. * @internal
  1603. *
  1604. * @param array $permissions
  1605. */
  1606. function save_inline_permissions($permissions)
  1607. {
  1608. global $context, $smcFunc;
  1609. // No permissions? Not a great deal to do here.
  1610. if (!allowedTo('manage_permissions'))
  1611. return;
  1612. // Almighty session check, verify our ways.
  1613. checkSession();
  1614. validateToken('admin-mp');
  1615. // Check they can't do certain things.
  1616. loadIllegalPermissions();
  1617. $insertRows = array();
  1618. foreach ($permissions as $permission)
  1619. {
  1620. if (!isset($_POST[$permission]))
  1621. continue;
  1622. foreach ($_POST[$permission] as $id_group => $value)
  1623. {
  1624. if (in_array($value, array('on', 'deny')) && (empty($context['illegal_permissions']) || !in_array($permission, $context['illegal_permissions'])))
  1625. $insertRows[] = array((int) $id_group, $permission, $value == 'on' ? 1 : 0);
  1626. }
  1627. }
  1628. // Remove the old permissions...
  1629. $smcFunc['db_query']('', '
  1630. DELETE FROM {db_prefix}permissions
  1631. WHERE permission IN ({array_string:permissions})
  1632. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  1633. array(
  1634. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  1635. 'permissions' => $permissions,
  1636. )
  1637. );
  1638. // ...and replace them with new ones.
  1639. if (!empty($insertRows))
  1640. $smcFunc['db_insert']('insert',
  1641. '{db_prefix}permissions',
  1642. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1643. $insertRows,
  1644. array('id_group', 'permission')
  1645. );
  1646. // Do a full child update.
  1647. updateChildPermissions(array(), -1);
  1648. // Just in case we cached this.
  1649. updateSettings(array('settings_updated' => time()));
  1650. }
  1651. /**
  1652. * Load permissions profiles.
  1653. */
  1654. function loadPermissionProfiles()
  1655. {
  1656. global $context, $txt, $smcFunc;
  1657. $request = $smcFunc['db_query']('', '
  1658. SELECT id_profile, profile_name
  1659. FROM {db_prefix}permission_profiles
  1660. ORDER BY id_profile',
  1661. array(
  1662. )
  1663. );
  1664. $context['profiles'] = array();
  1665. while ($row = $smcFunc['db_fetch_assoc']($request))
  1666. {
  1667. // Format the label nicely.
  1668. if (isset($txt['permissions_profile_' . $row['profile_name']]))
  1669. $name = $txt['permissions_profile_' . $row['profile_name']];
  1670. else
  1671. $name = $row['profile_name'];
  1672. $context['profiles'][$row['id_profile']] = array(
  1673. 'id' => $row['id_profile'],
  1674. 'name' => $name,
  1675. 'can_modify' => $row['id_profile'] == 1 || $row['id_profile'] > 4,
  1676. 'unformatted_name' => $row['profile_name'],
  1677. );
  1678. }
  1679. $smcFunc['db_free_result']($request);
  1680. }
  1681. /**
  1682. * Add/Edit/Delete profiles.
  1683. */
  1684. function EditPermissionProfiles()
  1685. {
  1686. global $context, $txt, $smcFunc;
  1687. // Setup the template, first for fun.
  1688. $context['page_title'] = $txt['permissions_profile_edit'];
  1689. $context['sub_template'] = 'edit_profiles';
  1690. // If we're creating a new one do it first.
  1691. if (isset($_POST['create']) && trim($_POST['profile_name']) != '')
  1692. {
  1693. checkSession();
  1694. validateToken('admin-mpp');
  1695. $_POST['copy_from'] = (int) $_POST['copy_from'];
  1696. $_POST['profile_name'] = $smcFunc['htmlspecialchars']($_POST['profile_name']);
  1697. // Insert the profile itself.
  1698. $smcFunc['db_insert']('',
  1699. '{db_prefix}permission_profiles',
  1700. array(
  1701. 'profile_name' => 'string',
  1702. ),
  1703. array(
  1704. $_POST['profile_name'],
  1705. ),
  1706. array('id_profile')
  1707. );
  1708. $profile_id = $smcFunc['db_insert_id']('{db_prefix}permission_profiles', 'id_profile');
  1709. // Load the permissions from the one it's being copied from.
  1710. $request = $smcFunc['db_query']('', '
  1711. SELECT id_group, permission, add_deny
  1712. FROM {db_prefix}board_permissions
  1713. WHERE id_profile = {int:copy_from}',
  1714. array(
  1715. 'copy_from' => $_POST['copy_from'],
  1716. )
  1717. );
  1718. $inserts = array();
  1719. while ($row = $smcFunc['db_fetch_assoc']($request))
  1720. $inserts[] = array($profile_id, $row['id_group'], $row['permission'], $row['add_deny']);
  1721. $smcFunc['db_free_result']($request);
  1722. if (!empty($inserts))
  1723. $smcFunc['db_insert']('insert',
  1724. '{db_prefix}board_permissions',
  1725. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1726. $inserts,
  1727. array('id_profile', 'id_group', 'permission')
  1728. );
  1729. }
  1730. // Renaming?
  1731. elseif (isset($_POST['rename']))
  1732. {
  1733. checkSession();
  1734. validateToken('admin-mpp');
  1735. // Just showing the boxes?
  1736. if (!isset($_POST['rename_profile']))
  1737. $context['show_rename_boxes'] = true;
  1738. else
  1739. {
  1740. foreach ($_POST['rename_profile'] as $id => $value)
  1741. {
  1742. $value = $smcFunc['htmlspecialchars']($value);
  1743. if (trim($value) != '' && $id > 4)
  1744. $smcFunc['db_query']('', '
  1745. UPDATE {db_prefix}permission_profiles
  1746. SET profile_name = {string:profile_name}
  1747. WHERE id_profile = {int:current_profile}',
  1748. array(
  1749. 'current_profile' => (int) $id,
  1750. 'profile_name' => $value,
  1751. )
  1752. );
  1753. }
  1754. }
  1755. }
  1756. // Deleting?
  1757. elseif (isset($_POST['delete']) && !empty($_POST['delete_profile']))
  1758. {
  1759. checkSession('post');
  1760. validateToken('admin-mpp');
  1761. $profiles = array();
  1762. foreach ($_POST['delete_profile'] as $profile)
  1763. if ($profile > 4)
  1764. $profiles[] = (int) $profile;
  1765. // Verify it's not in use...
  1766. $request = $smcFunc['db_query']('', '
  1767. SELECT id_board
  1768. FROM {db_prefix}boards
  1769. WHERE id_profile IN ({array_int:profile_list})
  1770. LIMIT 1',
  1771. array(
  1772. 'profile_list' => $profiles,
  1773. )
  1774. );
  1775. if ($smcFunc['db_num_rows']($request) != 0)
  1776. fatal_lang_error('no_access', false);
  1777. $smcFunc['db_free_result']($request);
  1778. // Oh well, delete.
  1779. $smcFunc['db_query']('', '
  1780. DELETE FROM {db_prefix}permission_profiles
  1781. WHERE id_profile IN ({array_int:profile_list})',
  1782. array(
  1783. 'profile_list' => $profiles,
  1784. )
  1785. );
  1786. }
  1787. // Clearly, we'll need this!
  1788. loadPermissionProfiles();
  1789. // Work out what ones are in use.
  1790. $request = $smcFunc['db_query']('', '
  1791. SELECT id_profile, COUNT(id_board) AS board_count
  1792. FROM {db_prefix}boards
  1793. GROUP BY id_profile',
  1794. array(
  1795. )
  1796. );
  1797. while ($row = $smcFunc['db_fetch_assoc']($request))
  1798. if (isset($context['profiles'][$row['id_profile']]))
  1799. {
  1800. $context['profiles'][$row['id_profile']]['in_use'] = true;
  1801. $context['profiles'][$row['id_profile']]['boards'] = $row['board_count'];
  1802. $context['profiles'][$row['id_profile']]['boards_text'] = $row['board_count'] > 1 ? sprintf($txt['permissions_profile_used_by_many'], $row['board_count']) : $txt['permissions_profile_used_by_' . ($row['board_count'] ? 'one' : 'none')];
  1803. }
  1804. $smcFunc['db_free_result']($request);
  1805. // What can we do with these?
  1806. $context['can_edit_something'] = false;
  1807. foreach ($context['profiles'] as $id => $profile)
  1808. {
  1809. // Can't delete special ones.
  1810. $context['profiles'][$id]['can_edit'] = isset($txt['permissions_profile_' . $profile['unformatted_name']]) ? false : true;
  1811. if ($context['profiles'][$id]['can_edit'])
  1812. $context['can_edit_something'] = true;
  1813. // You can only delete it if you can edit it AND it's not in use.
  1814. $context['profiles'][$id]['can_delete'] = $context['profiles'][$id]['can_edit'] && empty($profile['in_use']) ? true : false;
  1815. }
  1816. createToken('admin-mpp');
  1817. }
  1818. /**
  1819. * This function updates the permissions of any groups based off this group.
  1820. *
  1821. * @param mixed $parents (array or int)
  1822. * @param mixed $profile = null, int expected
  1823. */
  1824. function updateChildPermissions($parents, $profile = null)
  1825. {
  1826. global $smcFunc;
  1827. // All the parent groups to sort out.
  1828. if (!is_array($parents))
  1829. $parents = array($parents);
  1830. // Find all the children of this group.
  1831. $request = $smcFunc['db_query']('', '
  1832. SELECT id_parent, id_group
  1833. FROM {db_prefix}membergroups
  1834. WHERE id_parent != {int:not_inherited}
  1835. ' . (empty($parents) ? '' : 'AND id_parent IN ({array_int:parent_list})'),
  1836. array(
  1837. 'parent_list' => $parents,
  1838. 'not_inherited' => -2,
  1839. )
  1840. );
  1841. $children = array();
  1842. $parents = array();
  1843. $child_groups = array();
  1844. while ($row = $smcFunc['db_fetch_assoc']($request))
  1845. {
  1846. $children[$row['id_parent']][] = $row['id_group'];
  1847. $child_groups[] = $row['id_group'];
  1848. $parents[] = $row['id_parent'];
  1849. }
  1850. $smcFunc['db_free_result']($request);
  1851. $parents = array_unique($parents);
  1852. // Not a sausage, or a child?
  1853. if (empty($children))
  1854. return false;
  1855. // First off, are we doing general permissions?
  1856. if ($profile < 1 || $profile === null)
  1857. {
  1858. // Fetch all the parent permissions.
  1859. $request = $smcFunc['db_query']('', '
  1860. SELECT id_group, permission, add_deny
  1861. FROM {db_prefix}permissions
  1862. WHERE id_group IN ({array_int:parent_list})',
  1863. array(
  1864. 'parent_list' => $parents,
  1865. )
  1866. );
  1867. $permissions = array();
  1868. while ($row = $smcFunc['db_fetch_assoc']($request))
  1869. foreach ($children[$row['id_group']] as $child)
  1870. $permissions[] = array($child, $row['permission'], $row['add_deny']);
  1871. $smcFunc['db_free_result']($request);
  1872. $smcFunc['db_query']('', '
  1873. DELETE FROM {db_prefix}permissions
  1874. WHERE id_group IN ({array_int:child_groups})',
  1875. array(
  1876. 'child_groups' => $child_groups,
  1877. )
  1878. );
  1879. // Finally insert.
  1880. if (!empty($permissions))
  1881. {
  1882. $smcFunc['db_insert']('insert',
  1883. '{db_prefix}permissions',
  1884. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1885. $permissions,
  1886. array('id_group', 'permission')
  1887. );
  1888. }
  1889. }
  1890. // Then, what about board profiles?
  1891. if ($profile != -1)
  1892. {
  1893. $profileQuery = $profile === null ? '' : ' AND id_profile = {int:current_profile}';
  1894. // Again, get all the parent permissions.
  1895. $request = $smcFunc['db_query']('', '
  1896. SELECT id_profile, id_group, permission, add_deny
  1897. FROM {db_prefix}board_permissions
  1898. WHERE id_group IN ({array_int:parent_groups})
  1899. ' . $profileQuery,
  1900. array(
  1901. 'parent_groups' => $parents,
  1902. 'current_profile' => $profile !== null && $profile ? $profile : 1,
  1903. )
  1904. );
  1905. $permissions = array();
  1906. while ($row = $smcFunc['db_fetch_assoc']($request))
  1907. foreach ($children[$row['id_group']] as $child)
  1908. $permissions[] = array($child, $row['id_profile'], $row['permission'], $row['add_deny']);
  1909. $smcFunc['db_free_result']($request);
  1910. $smcFunc['db_query']('', '
  1911. DELETE FROM {db_prefix}board_permissions
  1912. WHERE id_group IN ({array_int:child_groups})
  1913. ' . $profileQuery,
  1914. array(
  1915. 'child_groups' => $child_groups,
  1916. 'current_profile' => $profile !== null && $profile ? $profile : 1,
  1917. )
  1918. );
  1919. // Do the insert.
  1920. if (!empty($permissions))
  1921. {
  1922. $smcFunc['db_insert']('insert',
  1923. '{db_prefix}board_permissions',
  1924. array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1925. $permissions,
  1926. array('id_group', 'id_profile', 'permission')
  1927. );
  1928. }
  1929. }
  1930. }
  1931. /**
  1932. * Load permissions someone cannot grant.
  1933. */
  1934. function loadIllegalPermissions()
  1935. {
  1936. global $context;
  1937. $context['illegal_permissions'] = array();
  1938. if (!allowedTo('admin_forum'))
  1939. $context['illegal_permissions'][] = 'admin_forum';
  1940. if (!allowedTo('manage_membergroups'))
  1941. $context['illegal_permissions'][] = 'manage_membergroups';
  1942. if (!allowedTo('manage_permissions'))
  1943. $context['illegal_permissions'][] = 'manage_permissions';
  1944. call_integration_hook('integrate_load_illegal_permissions');
  1945. }
  1946. /**
  1947. * Loads the permissions that can not be given to guests.
  1948. * Stores the permissions in $context['non_guest_permissions'].
  1949. */
  1950. function loadIllegalGuestPermissions()
  1951. {
  1952. global $context;
  1953. $context['non_guest_permissions'] = array(
  1954. 'delete_replies',
  1955. 'karma_edit',
  1956. 'poll_add_own',
  1957. 'pm_read',
  1958. 'pm_send',
  1959. 'profile_identity',
  1960. 'profile_extra',
  1961. 'profile_title',
  1962. 'profile_remove',
  1963. 'profile_server_avatar',
  1964. 'profile_upload_avatar',
  1965. 'profile_remote_avatar',
  1966. 'profile_view_own',
  1967. 'mark_any_notify',
  1968. 'mark_notify',
  1969. 'admin_forum',
  1970. 'manage_boards',
  1971. 'manage_attachments',
  1972. 'manage_smileys',
  1973. 'edit_news',
  1974. 'access_mod_center',
  1975. 'moderate_forum',
  1976. 'issue_warning',
  1977. 'manage_membergroups',
  1978. 'manage_permissions',
  1979. 'manage_bans',
  1980. 'move_own',
  1981. 'modify_replies',
  1982. 'send_mail',
  1983. 'approve_posts',
  1984. );
  1985. call_integration_hook('integrate_load_illegal_guest_permissions');
  1986. }
  1987. /**
  1988. * Present a nice way of applying post moderation.
  1989. */
  1990. function ModifyPostModeration()
  1991. {
  1992. global $context, $txt, $smcFunc, $modSettings;
  1993. // Just in case.
  1994. checkSession('get');
  1995. $context['page_title'] = $txt['permissions_post_moderation'];
  1996. $context['sub_template'] = 'postmod_permissions';
  1997. $context['current_profile'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 1;
  1998. // Load all the permission profiles.
  1999. loadPermissionProfiles();
  2000. // Mappings, our key => array(can_do_moderated, can_do_all)
  2001. $mappings = array(
  2002. 'new_topic' => array('post_new', 'post_unapproved_topics'),
  2003. 'replies_own' => array('post_reply_own', 'post_unapproved_replies_own'),
  2004. 'replies_any' => array('post_reply_any', 'post_unapproved_replies_any'),
  2005. 'attachment' => array('post_attachment', 'post_unapproved_attachments'),
  2006. );
  2007. call_integration_hook('integrate_post_moderation_mapping', array(&$mappings));
  2008. // Start this with the guests/members.
  2009. $context['profile_groups'] = array(
  2010. -1 => array(
  2011. 'id' => -1,
  2012. 'name' => $txt['membergroups_guests'],
  2013. 'color' => '',
  2014. 'new_topic' => 'disallow',
  2015. 'replies_own' => 'disallow',
  2016. 'replies_any' => 'disallow',
  2017. 'attachment' => 'disallow',
  2018. 'children' => array(),
  2019. ),
  2020. 0 => array(
  2021. 'id' => 0,
  2022. 'name' => $txt['membergroups_members'],
  2023. 'color' => '',
  2024. 'new_topic' => 'disallow',
  2025. 'replies_own' => 'disallow',
  2026. 'replies_any' => 'disallow',
  2027. 'attachment' => 'disallow',
  2028. 'children' => array(),
  2029. ),
  2030. );
  2031. // Load the groups.
  2032. $request = $smcFunc['db_query']('', '
  2033. SELECT id_group, group_name, online_color, id_parent
  2034. FROM {db_prefix}membergroups
  2035. WHERE id_group != {int:admin_group}
  2036. ' . (empty($modSettings['permission_enable_postgroups']) ? ' AND min_posts = {int:min_posts}' : '') . '
  2037. ORDER BY id_parent ASC',
  2038. array(
  2039. 'admin_group' => 1,
  2040. 'min_posts' => -1,
  2041. )
  2042. );
  2043. while ($row = $smcFunc['db_fetch_assoc']($request))
  2044. {
  2045. if ($row['id_parent'] == -2)
  2046. {
  2047. $context['profile_groups'][$row['id_group']] = array(
  2048. 'id' => $row['id_group'],
  2049. 'name' => $row['group_name'],
  2050. 'color' => $row['online_color'],
  2051. 'new_topic' => 'disallow',
  2052. 'replies_own' => 'disallow',
  2053. 'replies_any' => 'disallow',
  2054. 'attachment' => 'disallow',
  2055. 'children' => array(),
  2056. );
  2057. }
  2058. elseif (isset($context['profile_groups'][$row['id_parent']]))
  2059. $context['profile_groups'][$row['id_parent']]['children'][] = $row['group_name'];
  2060. }
  2061. $smcFunc['db_free_result']($request);
  2062. // What are the permissions we are querying?
  2063. $all_permissions = array();
  2064. foreach ($mappings as $perm_set)
  2065. $all_permissions = array_merge($all_permissions, $perm_set);
  2066. // If we're saving the changes then do just that - save them.
  2067. if (!empty($_POST['save_changes']) && ($context['current_profile'] == 1 || $context['current_profile'] > 4))
  2068. {
  2069. validateToken('admin-mppm');
  2070. // Start by deleting all the permissions relevant.
  2071. $smcFunc['db_query']('', '
  2072. DELETE FROM {db_prefix}board_permissions
  2073. WHERE id_profile = {int:current_profile}
  2074. AND permission IN ({array_string:permissions})
  2075. AND id_group IN ({array_int:profile_group_list})',
  2076. array(
  2077. 'profile_group_list' => array_keys($context['profile_groups']),
  2078. 'current_profile' => $context['current_profile'],
  2079. 'permissions' => $all_permissions,
  2080. )
  2081. );
  2082. // Do it group by group.
  2083. $new_permissions = array();
  2084. foreach ($context['profile_groups'] as $id => $group)
  2085. {
  2086. foreach ($mappings as $index => $data)
  2087. {
  2088. if (isset($_POST[$index][$group['id']]))
  2089. {
  2090. if ($_POST[$index][$group['id']] == 'allow')
  2091. {
  2092. // Give them both sets for fun.
  2093. $new_permissions[] = array($context['current_profile'], $group['id'], $data[0], 1);
  2094. $new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
  2095. }
  2096. elseif ($_POST[$index][$group['id']] == 'moderate')
  2097. $new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
  2098. }
  2099. }
  2100. }
  2101. // Insert new permissions.
  2102. if (!empty($new_permissions))
  2103. $smcFunc['db_insert']('',
  2104. '{db_prefix}board_permissions',
  2105. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  2106. $new_permissions,
  2107. array('id_profile', 'id_group', 'permission')
  2108. );
  2109. }
  2110. // Now get all the permissions!
  2111. $request = $smcFunc['db_query']('', '
  2112. SELECT id_group, permission, add_deny
  2113. FROM {db_prefix}board_permissions
  2114. WHERE id_profile = {int:current_profile}
  2115. AND permission IN ({array_string:permissions})
  2116. AND id_group IN ({array_int:profile_group_list})',
  2117. array(
  2118. 'profile_group_list' => array_keys($context['profile_groups']),
  2119. 'current_profile' => $context['current_profile'],
  2120. 'permissions' => $all_permissions,
  2121. )
  2122. );
  2123. while ($row = $smcFunc['db_fetch_assoc']($request))
  2124. {
  2125. foreach ($mappings as $key => $data)
  2126. {
  2127. foreach ($data as $index => $perm)
  2128. {
  2129. if ($perm == $row['permission'])
  2130. {
  2131. // Only bother if it's not denied.
  2132. if ($row['add_deny'])
  2133. {
  2134. // Full allowance?
  2135. if ($index == 0)
  2136. $context['profile_groups'][$row['id_group']][$key] = 'allow';
  2137. // Otherwise only bother with moderate if not on allow.
  2138. elseif ($context['profile_groups'][$row['id_group']][$key] != 'allow')
  2139. $context['profile_groups'][$row['id_group']][$key] = 'moderate';
  2140. }
  2141. }
  2142. }
  2143. }
  2144. }
  2145. $smcFunc['db_free_result']($request);
  2146. createToken('admin-mppm');
  2147. }
  2148. ?>