ManagePermissions.php 75 KB

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