ManagePermissions.php 73 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 2013 Simple Machines and individual contributors
  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('No direct access...');
  16. /**
  17. * Dispaches to the right function based on the given subaction.
  18. * Checks the permissions, based on the sub-action.
  19. * Called by ?action=managepermissions.
  20. *
  21. * @uses ManagePermissions language file.
  22. */
  23. function ModifyPermissions()
  24. {
  25. global $txt, $scripturl, $context;
  26. loadLanguage('ManagePermissions+ManageMembers');
  27. loadTemplate('ManagePermissions');
  28. // Format: 'sub-action' => array('function_to_call', 'permission_needed'),
  29. $subActions = array(
  30. 'board' => array('PermissionByBoard', 'manage_permissions'),
  31. 'index' => array('PermissionIndex', 'manage_permissions'),
  32. 'modify' => array('ModifyMembergroup', 'manage_permissions'),
  33. 'modify2' => array('ModifyMembergroup2', 'manage_permissions'),
  34. 'quick' => array('SetQuickGroups', 'manage_permissions'),
  35. 'quickboard' => array('SetQuickBoards', 'manage_permissions'),
  36. 'postmod' => array('ModifyPostModeration', 'manage_permissions'),
  37. 'profiles' => array('EditPermissionProfiles', 'manage_permissions'),
  38. 'settings' => array('GeneralPermissionSettings', 'admin_forum'),
  39. );
  40. call_integration_hook('integrate_manage_permissions', array(&$subActions));
  41. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) && 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. 'help' => 'membergroup_guests',
  110. 'is_post_group' => false,
  111. 'color' => '',
  112. 'icons' => '',
  113. 'children' => array(),
  114. 'num_permissions' => array(
  115. 'allowed' => 0,
  116. // Can't deny guest permissions!
  117. 'denied' => '(' . $txt['permissions_none'] . ')'
  118. ),
  119. 'access' => false
  120. ),
  121. 0 => array(
  122. 'id' => 0,
  123. 'name' => $txt['membergroups_members'],
  124. 'num_members' => $num_members,
  125. 'allow_delete' => false,
  126. 'allow_modify' => true,
  127. 'can_search' => false,
  128. 'href' => $scripturl . '?action=moderate;area=viewgroups;sa=members;group=0',
  129. 'help' => 'membergroup_regular_members',
  130. 'is_post_group' => false,
  131. 'color' => '',
  132. 'icons' => '',
  133. 'children' => array(),
  134. 'num_permissions' => array(
  135. 'allowed' => 0,
  136. 'denied' => 0
  137. ),
  138. 'access' => false
  139. ),
  140. );
  141. $postGroups = array();
  142. $normalGroups = array();
  143. // Query the database defined membergroups.
  144. $query = $smcFunc['db_query']('', '
  145. SELECT id_group, id_parent, group_name, min_posts, online_color, icons
  146. FROM {db_prefix}membergroups' . (empty($modSettings['permission_enable_postgroups']) ? '
  147. WHERE min_posts = {int:min_posts}' : '') . '
  148. 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',
  149. array(
  150. 'min_posts' => -1,
  151. 'not_inherited' => -2,
  152. 'newbie_group' => 4,
  153. )
  154. );
  155. while ($row = $smcFunc['db_fetch_assoc']($query))
  156. {
  157. // If it's inherited, just add it as a child.
  158. if ($row['id_parent'] != -2)
  159. {
  160. if (isset($context['groups'][$row['id_parent']]))
  161. $context['groups'][$row['id_parent']]['children'][$row['id_group']] = $row['group_name'];
  162. continue;
  163. }
  164. $row['icons'] = explode('#', $row['icons']);
  165. $context['groups'][$row['id_group']] = array(
  166. 'id' => $row['id_group'],
  167. 'name' => $row['group_name'],
  168. 'num_members' => $row['id_group'] != 3 ? 0 : $txt['membergroups_guests_na'],
  169. 'allow_delete' => $row['id_group'] > 4,
  170. 'allow_modify' => $row['id_group'] > 1,
  171. 'can_search' => $row['id_group'] != 3,
  172. 'href' => $scripturl . '?action=moderate;area=viewgroups;sa=members;group=' . $row['id_group'],
  173. 'help' => $row['id_group'] == 1 ? 'membergroup_administrator' : ($row['id_group'] == 3 ? 'membergroup_moderator' : ''),
  174. 'is_post_group' => $row['min_posts'] != -1,
  175. 'color' => empty($row['online_color']) ? '' : $row['online_color'],
  176. 'icons' => !empty($row['icons'][0]) && !empty($row['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['icons'][1] . '" alt="*" />', $row['icons'][0]) : '',
  177. 'children' => array(),
  178. 'num_permissions' => array(
  179. 'allowed' => $row['id_group'] == 1 ? '(' . $txt['permissions_all'] . ')' : 0,
  180. 'denied' => $row['id_group'] == 1 ? '(' . $txt['permissions_none'] . ')' : 0
  181. ),
  182. 'access' => false,
  183. );
  184. if ($row['min_posts'] == -1)
  185. $normalGroups[$row['id_group']] = $row['id_group'];
  186. else
  187. $postGroups[$row['id_group']] = $row['id_group'];
  188. }
  189. $smcFunc['db_free_result']($query);
  190. // Get the number of members in this post group.
  191. if (!empty($postGroups))
  192. {
  193. $query = $smcFunc['db_query']('', '
  194. SELECT id_post_group AS id_group, COUNT(*) AS num_members
  195. FROM {db_prefix}members
  196. WHERE id_post_group IN ({array_int:post_group_list})
  197. GROUP BY id_post_group',
  198. array(
  199. 'post_group_list' => $postGroups,
  200. )
  201. );
  202. while ($row = $smcFunc['db_fetch_assoc']($query))
  203. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  204. $smcFunc['db_free_result']($query);
  205. }
  206. if (!empty($normalGroups))
  207. {
  208. // First, the easy one!
  209. $query = $smcFunc['db_query']('', '
  210. SELECT id_group, COUNT(*) AS num_members
  211. FROM {db_prefix}members
  212. WHERE id_group IN ({array_int:normal_group_list})
  213. GROUP BY id_group',
  214. array(
  215. 'normal_group_list' => $normalGroups,
  216. )
  217. );
  218. while ($row = $smcFunc['db_fetch_assoc']($query))
  219. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  220. $smcFunc['db_free_result']($query);
  221. // This one is slower, but it's okay... careful not to count twice!
  222. $query = $smcFunc['db_query']('', '
  223. SELECT mg.id_group, COUNT(*) AS num_members
  224. FROM {db_prefix}membergroups AS mg
  225. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_string}
  226. AND mem.id_group != mg.id_group
  227. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  228. WHERE mg.id_group IN ({array_int:normal_group_list})
  229. GROUP BY mg.id_group',
  230. array(
  231. 'normal_group_list' => $normalGroups,
  232. 'blank_string' => '',
  233. )
  234. );
  235. while ($row = $smcFunc['db_fetch_assoc']($query))
  236. $context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
  237. $smcFunc['db_free_result']($query);
  238. }
  239. foreach ($context['groups'] as $id => $data)
  240. {
  241. if ($data['href'] != '')
  242. $context['groups'][$id]['link'] = '<a href="' . $data['href'] . '">' . $data['num_members'] . '</a>';
  243. }
  244. if (empty($_REQUEST['pid']))
  245. {
  246. $request = $smcFunc['db_query']('', '
  247. SELECT id_group, COUNT(*) AS num_permissions, add_deny
  248. FROM {db_prefix}permissions
  249. ' . (empty($context['hidden_permissions']) ? '' : ' WHERE permission NOT IN ({array_string:hidden_permissions})') . '
  250. GROUP BY id_group, add_deny',
  251. array(
  252. 'hidden_permissions' => !empty($context['hidden_permissions']) ? $context['hidden_permissions'] : array(),
  253. )
  254. );
  255. while ($row = $smcFunc['db_fetch_assoc']($request))
  256. if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
  257. $context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] = $row['num_permissions'];
  258. $smcFunc['db_free_result']($request);
  259. // Get the "default" profile permissions too.
  260. $request = $smcFunc['db_query']('', '
  261. SELECT id_profile, id_group, COUNT(*) AS num_permissions, add_deny
  262. FROM {db_prefix}board_permissions
  263. WHERE id_profile = {int:default_profile}
  264. ' . (empty($context['hidden_permissions']) ? '' : ' AND permission NOT IN ({array_string:hidden_permissions})') . '
  265. GROUP BY id_profile, id_group, add_deny',
  266. array(
  267. 'default_profile' => 1,
  268. 'hidden_permissions' => !empty($context['hidden_permissions']) ? $context['hidden_permissions'] : array(),
  269. )
  270. );
  271. while ($row = $smcFunc['db_fetch_assoc']($request))
  272. {
  273. if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
  274. $context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] += $row['num_permissions'];
  275. }
  276. $smcFunc['db_free_result']($request);
  277. }
  278. else
  279. {
  280. $_REQUEST['pid'] = (int) $_REQUEST['pid'];
  281. if (!isset($context['profiles'][$_REQUEST['pid']]))
  282. fatal_lang_error('no_access', false);
  283. // Change the selected tab to better reflect that this really is a board profile.
  284. $context[$context['admin_menu_name']]['current_subsection'] = 'profiles';
  285. $request = $smcFunc['db_query']('', '
  286. SELECT id_profile, id_group, COUNT(*) AS num_permissions, add_deny
  287. FROM {db_prefix}board_permissions
  288. WHERE id_profile = {int:current_profile}
  289. GROUP BY id_profile, id_group, add_deny',
  290. array(
  291. 'current_profile' => $_REQUEST['pid'],
  292. )
  293. );
  294. while ($row = $smcFunc['db_fetch_assoc']($request))
  295. {
  296. if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
  297. $context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] += $row['num_permissions'];
  298. }
  299. $smcFunc['db_free_result']($request);
  300. $context['profile'] = array(
  301. 'id' => $_REQUEST['pid'],
  302. 'name' => $context['profiles'][$_REQUEST['pid']]['name'],
  303. );
  304. }
  305. // We can modify any permission set apart from the read only, reply only and no polls ones as they are redefined.
  306. $context['can_modify'] = empty($_REQUEST['pid']) || $_REQUEST['pid'] == 1 || $_REQUEST['pid'] > 4;
  307. // Load the proper template.
  308. $context['sub_template'] = 'permission_index';
  309. createToken('admin-mpq');
  310. }
  311. /**
  312. * Handle permissions by board... more or less. :P
  313. */
  314. function PermissionByBoard()
  315. {
  316. global $context, $modSettings, $txt, $smcFunc, $sourcedir, $cat_tree, $boardList, $boards;
  317. $context['page_title'] = $txt['permissions_boards'];
  318. $context['edit_all'] = isset($_GET['edit']);
  319. // Saving?
  320. if (!empty($_POST['save_changes']) && !empty($_POST['boardprofile']))
  321. {
  322. checkSession('request');
  323. validateToken('admin-mpb');
  324. $changes = array();
  325. foreach ($_POST['boardprofile'] as $board => $profile)
  326. {
  327. $changes[(int) $profile][] = (int) $board;
  328. }
  329. if (!empty($changes))
  330. {
  331. foreach ($changes as $profile => $boards)
  332. $smcFunc['db_query']('', '
  333. UPDATE {db_prefix}boards
  334. SET id_profile = {int:current_profile}
  335. WHERE id_board IN ({array_int:board_list})',
  336. array(
  337. 'board_list' => $boards,
  338. 'current_profile' => $profile,
  339. )
  340. );
  341. }
  342. $context['edit_all'] = false;
  343. }
  344. // Load all permission profiles.
  345. loadPermissionProfiles();
  346. // Get the board tree.
  347. require_once($sourcedir . '/Subs-Boards.php');
  348. getBoardTree();
  349. // Build the list of the boards.
  350. $context['categories'] = array();
  351. foreach ($cat_tree as $catid => $tree)
  352. {
  353. $context['categories'][$catid] = array(
  354. 'name' => &$tree['node']['name'],
  355. 'id' => &$tree['node']['id'],
  356. 'boards' => array()
  357. );
  358. foreach ($boardList[$catid] as $boardid)
  359. {
  360. if (!isset($context['profiles'][$boards[$boardid]['profile']]))
  361. $boards[$boardid]['profile'] = 1;
  362. $context['categories'][$catid]['boards'][$boardid] = array(
  363. 'id' => &$boards[$boardid]['id'],
  364. 'name' => &$boards[$boardid]['name'],
  365. 'description' => &$boards[$boardid]['description'],
  366. 'child_level' => &$boards[$boardid]['level'],
  367. 'profile' => &$boards[$boardid]['profile'],
  368. 'profile_name' => $context['profiles'][$boards[$boardid]['profile']]['name'],
  369. );
  370. }
  371. }
  372. $context['sub_template'] = 'by_board';
  373. createToken('admin-mpb');
  374. }
  375. /**
  376. * Handles permission modification actions from the upper part of the
  377. * permission manager index.
  378. */
  379. function SetQuickGroups()
  380. {
  381. global $context, $smcFunc;
  382. checkSession();
  383. validateToken('admin-mpq', 'quick');
  384. loadIllegalPermissions();
  385. loadIllegalGuestPermissions();
  386. // Make sure only one of the quick options was selected.
  387. 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'])))
  388. fatal_lang_error('permissions_only_one_option', false);
  389. if (empty($_POST['group']) || !is_array($_POST['group']))
  390. $_POST['group'] = array();
  391. // Only accept numeric values for selected membergroups.
  392. foreach ($_POST['group'] as $id => $group_id)
  393. $_POST['group'][$id] = (int) $group_id;
  394. $_POST['group'] = array_unique($_POST['group']);
  395. if (empty($_REQUEST['pid']))
  396. $_REQUEST['pid'] = 0;
  397. else
  398. $_REQUEST['pid'] = (int) $_REQUEST['pid'];
  399. // Fix up the old global to the new default!
  400. $bid = max(1, $_REQUEST['pid']);
  401. // No modifying the predefined profiles.
  402. if ($_REQUEST['pid'] > 1 && $_REQUEST['pid'] < 5)
  403. fatal_lang_error('no_access', false);
  404. // Clear out any cached authority.
  405. updateSettings(array('settings_updated' => time()));
  406. // No groups where selected.
  407. if (empty($_POST['group']))
  408. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  409. // Set a predefined permission profile.
  410. if (!empty($_POST['predefined']))
  411. {
  412. // Make sure it's a predefined permission set we expect.
  413. if (!in_array($_POST['predefined'], array('restrict', 'standard', 'moderator', 'maintenance')))
  414. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  415. foreach ($_POST['group'] as $group_id)
  416. {
  417. if (!empty($_REQUEST['pid']))
  418. setPermissionLevel($_POST['predefined'], $group_id, $_REQUEST['pid']);
  419. else
  420. setPermissionLevel($_POST['predefined'], $group_id);
  421. }
  422. }
  423. // Set a permission profile based on the permissions of a selected group.
  424. elseif ($_POST['copy_from'] != 'empty')
  425. {
  426. // Just checking the input.
  427. if (!is_numeric($_POST['copy_from']))
  428. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  429. // Make sure the group we're copying to is never included.
  430. $_POST['group'] = array_diff($_POST['group'], array($_POST['copy_from']));
  431. // No groups left? Too bad.
  432. if (empty($_POST['group']))
  433. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  434. if (empty($_REQUEST['pid']))
  435. {
  436. // Retrieve current permissions of group.
  437. $request = $smcFunc['db_query']('', '
  438. SELECT permission, add_deny
  439. FROM {db_prefix}permissions
  440. WHERE id_group = {int:copy_from}',
  441. array(
  442. 'copy_from' => $_POST['copy_from'],
  443. )
  444. );
  445. $target_perm = array();
  446. while ($row = $smcFunc['db_fetch_assoc']($request))
  447. $target_perm[$row['permission']] = $row['add_deny'];
  448. $smcFunc['db_free_result']($request);
  449. $inserts = array();
  450. foreach ($_POST['group'] as $group_id)
  451. foreach ($target_perm as $perm => $add_deny)
  452. {
  453. // No dodgy permissions please!
  454. if (!empty($context['illegal_permissions']) && in_array($perm, $context['illegal_permissions']))
  455. continue;
  456. if ($group_id == -1 && in_array($perm, $context['non_guest_permissions']))
  457. continue;
  458. if ($group_id != 1 && $group_id != 3)
  459. $inserts[] = array($perm, $group_id, $add_deny);
  460. }
  461. // Delete the previous permissions...
  462. $smcFunc['db_query']('', '
  463. DELETE FROM {db_prefix}permissions
  464. WHERE id_group IN ({array_int:group_list})
  465. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  466. array(
  467. 'group_list' => $_POST['group'],
  468. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  469. )
  470. );
  471. if (!empty($inserts))
  472. {
  473. // ..and insert the new ones.
  474. $smcFunc['db_insert']('',
  475. '{db_prefix}permissions',
  476. array(
  477. 'permission' => 'string', 'id_group' => 'int', 'add_deny' => 'int',
  478. ),
  479. $inserts,
  480. array('permission', 'id_group')
  481. );
  482. }
  483. }
  484. // Now do the same for the board permissions.
  485. $request = $smcFunc['db_query']('', '
  486. SELECT permission, add_deny
  487. FROM {db_prefix}board_permissions
  488. WHERE id_group = {int:copy_from}
  489. AND id_profile = {int:current_profile}',
  490. array(
  491. 'copy_from' => $_POST['copy_from'],
  492. 'current_profile' => $bid,
  493. )
  494. );
  495. $target_perm = array();
  496. while ($row = $smcFunc['db_fetch_assoc']($request))
  497. $target_perm[$row['permission']] = $row['add_deny'];
  498. $smcFunc['db_free_result']($request);
  499. $inserts = array();
  500. foreach ($_POST['group'] as $group_id)
  501. foreach ($target_perm as $perm => $add_deny)
  502. {
  503. // Are these for guests?
  504. if ($group_id == -1 && in_array($perm, $context['non_guest_permissions']))
  505. continue;
  506. $inserts[] = array($perm, $group_id, $bid, $add_deny);
  507. }
  508. // Delete the previous global board permissions...
  509. $smcFunc['db_query']('', '
  510. DELETE FROM {db_prefix}board_permissions
  511. WHERE id_group IN ({array_int:current_group_list})
  512. AND id_profile = {int:current_profile}',
  513. array(
  514. 'current_group_list' => $_POST['group'],
  515. 'current_profile' => $bid,
  516. )
  517. );
  518. // And insert the copied permissions.
  519. if (!empty($inserts))
  520. {
  521. // ..and insert the new ones.
  522. $smcFunc['db_insert']('',
  523. '{db_prefix}board_permissions',
  524. array('permission' => 'string', 'id_group' => 'int', 'id_profile' => 'int', 'add_deny' => 'int'),
  525. $inserts,
  526. array('permission', 'id_group', 'id_profile')
  527. );
  528. }
  529. // Update any children out there!
  530. updateChildPermissions($_POST['group'], $_REQUEST['pid']);
  531. }
  532. // Set or unset a certain permission for the selected groups.
  533. elseif (!empty($_POST['permissions']))
  534. {
  535. // Unpack two variables that were transported.
  536. list ($permissionType, $permission) = explode('/', $_POST['permissions']);
  537. // Check whether our input is within expected range.
  538. if (!in_array($_POST['add_remove'], array('add', 'clear', 'deny')) || !in_array($permissionType, array('membergroup', 'board')))
  539. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  540. if ($_POST['add_remove'] == 'clear')
  541. {
  542. if ($permissionType == 'membergroup')
  543. $smcFunc['db_query']('', '
  544. DELETE FROM {db_prefix}permissions
  545. WHERE id_group IN ({array_int:current_group_list})
  546. AND permission = {string:current_permission}
  547. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  548. array(
  549. 'current_group_list' => $_POST['group'],
  550. 'current_permission' => $permission,
  551. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  552. )
  553. );
  554. else
  555. $smcFunc['db_query']('', '
  556. DELETE FROM {db_prefix}board_permissions
  557. WHERE id_group IN ({array_int:current_group_list})
  558. AND id_profile = {int:current_profile}
  559. AND permission = {string:current_permission}',
  560. array(
  561. 'current_group_list' => $_POST['group'],
  562. 'current_profile' => $bid,
  563. 'current_permission' => $permission,
  564. )
  565. );
  566. }
  567. // Add a permission (either 'set' or 'deny').
  568. else
  569. {
  570. $add_deny = $_POST['add_remove'] == 'add' ? '1' : '0';
  571. $permChange = array();
  572. foreach ($_POST['group'] as $groupID)
  573. {
  574. if ($groupID == -1 && in_array($permission, $context['non_guest_permissions']))
  575. continue;
  576. if ($permissionType == 'membergroup' && $groupID != 1 && $groupID != 3 && (empty($context['illegal_permissions']) || !in_array($permission, $context['illegal_permissions'])))
  577. $permChange[] = array($permission, $groupID, $add_deny);
  578. elseif ($permissionType != 'membergroup')
  579. $permChange[] = array($permission, $groupID, $bid, $add_deny);
  580. }
  581. if (!empty($permChange))
  582. {
  583. if ($permissionType == 'membergroup')
  584. $smcFunc['db_insert']('replace',
  585. '{db_prefix}permissions',
  586. array('permission' => 'string', 'id_group' => 'int', 'add_deny' => 'int'),
  587. $permChange,
  588. array('permission', 'id_group')
  589. );
  590. // Board permissions go into the other table.
  591. else
  592. $smcFunc['db_insert']('replace',
  593. '{db_prefix}board_permissions',
  594. array('permission' => 'string', 'id_group' => 'int', 'id_profile' => 'int', 'add_deny' => 'int'),
  595. $permChange,
  596. array('permission', 'id_group', 'id_profile')
  597. );
  598. }
  599. }
  600. // Another child update!
  601. updateChildPermissions($_POST['group'], $_REQUEST['pid']);
  602. }
  603. redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
  604. }
  605. /**
  606. * Initializes the necessary to modify a membergroup's permissions.
  607. */
  608. function ModifyMembergroup()
  609. {
  610. global $context, $txt, $modSettings, $smcFunc, $sourcedir;
  611. if (!isset($_GET['group']))
  612. fatal_lang_error('no_access', false);
  613. $context['group']['id'] = (int) $_GET['group'];
  614. // It's not likely you'd end up here with this setting disabled.
  615. if ($_GET['group'] == 1)
  616. redirectexit('action=admin;area=permissions');
  617. loadAllPermissions();
  618. loadPermissionProfiles();
  619. if ($context['group']['id'] > 0)
  620. {
  621. $result = $smcFunc['db_query']('', '
  622. SELECT group_name, id_parent
  623. FROM {db_prefix}membergroups
  624. WHERE id_group = {int:current_group}
  625. LIMIT 1',
  626. array(
  627. 'current_group' => $context['group']['id'],
  628. )
  629. );
  630. list ($context['group']['name'], $parent) = $smcFunc['db_fetch_row']($result);
  631. $smcFunc['db_free_result']($result);
  632. // Cannot edit an inherited group!
  633. if ($parent != -2)
  634. fatal_lang_error('cannot_edit_permissions_inherited');
  635. }
  636. elseif ($context['group']['id'] == -1)
  637. $context['group']['name'] = $txt['membergroups_guests'];
  638. else
  639. $context['group']['name'] = $txt['membergroups_members'];
  640. $context['profile']['id'] = empty($_GET['pid']) ? 0 : (int) $_GET['pid'];
  641. // If this is a moderator and they are editing "no profile" then we only do boards.
  642. if ($context['group']['id'] == 3 && empty($context['profile']['id']))
  643. {
  644. // For sanity just check they have no general permissions.
  645. $smcFunc['db_query']('', '
  646. DELETE FROM {db_prefix}permissions
  647. WHERE id_group = {int:moderator_group}',
  648. array(
  649. 'moderator_group' => 3,
  650. )
  651. );
  652. $context['profile']['id'] = 1;
  653. }
  654. $context['permission_type'] = empty($context['profile']['id']) ? 'membergroup' : 'board';
  655. $context['profile']['can_modify'] = !$context['profile']['id'] || $context['profiles'][$context['profile']['id']]['can_modify'];
  656. // Set up things a little nicer for board related stuff...
  657. if ($context['permission_type'] == 'board')
  658. {
  659. $context['profile']['name'] = $context['profiles'][$context['profile']['id']]['name'];
  660. $context[$context['admin_menu_name']]['current_subsection'] = 'profiles';
  661. }
  662. // Fetch the current permissions.
  663. $permissions = array(
  664. 'membergroup' => array('allowed' => array(), 'denied' => array()),
  665. 'board' => array('allowed' => array(), 'denied' => array())
  666. );
  667. // General permissions?
  668. if ($context['permission_type'] == 'membergroup')
  669. {
  670. $result = $smcFunc['db_query']('', '
  671. SELECT permission, add_deny
  672. FROM {db_prefix}permissions
  673. WHERE id_group = {int:current_group}',
  674. array(
  675. 'current_group' => $_GET['group'],
  676. )
  677. );
  678. while ($row = $smcFunc['db_fetch_assoc']($result))
  679. $permissions['membergroup'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
  680. $smcFunc['db_free_result']($result);
  681. }
  682. // Fetch current board permissions...
  683. $result = $smcFunc['db_query']('', '
  684. SELECT permission, add_deny
  685. FROM {db_prefix}board_permissions
  686. WHERE id_group = {int:current_group}
  687. AND id_profile = {int:current_profile}',
  688. array(
  689. 'current_group' => $context['group']['id'],
  690. 'current_profile' => $context['permission_type'] == 'membergroup' ? 1 : $context['profile']['id'],
  691. )
  692. );
  693. while ($row = $smcFunc['db_fetch_assoc']($result))
  694. $permissions['board'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
  695. $smcFunc['db_free_result']($result);
  696. // Loop through each permission and set whether it's checked.
  697. foreach ($context['permissions'] as $permissionType => $tmp)
  698. {
  699. foreach ($tmp['columns'] as $position => $permissionGroups)
  700. {
  701. foreach ($permissionGroups as $permissionGroup => $permissionArray)
  702. {
  703. foreach ($permissionArray['permissions'] as $perm)
  704. {
  705. // Create a shortcut for the current permission.
  706. $curPerm = &$context['permissions'][$permissionType]['columns'][$position][$permissionGroup]['permissions'][$perm['id']];
  707. if ($perm['has_own_any'])
  708. {
  709. $curPerm['any']['select'] = in_array($perm['id'] . '_any', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_any', $permissions[$permissionType]['denied']) ? 'denied' : 'off');
  710. $curPerm['own']['select'] = in_array($perm['id'] . '_own', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_own', $permissions[$permissionType]['denied']) ? 'denied' : 'off');
  711. }
  712. else
  713. $curPerm['select'] = in_array($perm['id'], $permissions[$permissionType]['denied']) ? 'denied' : (in_array($perm['id'], $permissions[$permissionType]['allowed']) ? 'on' : 'off');
  714. }
  715. }
  716. }
  717. }
  718. $context['sub_template'] = 'modify_group';
  719. $context['page_title'] = $txt['permissions_modify_group'];
  720. createToken('admin-mp');
  721. }
  722. /**
  723. * This function actually saves modifications to a membergroup's board permissions.
  724. */
  725. function ModifyMembergroup2()
  726. {
  727. global $modSettings, $smcFunc, $context;
  728. checkSession();
  729. validateToken('admin-mp');
  730. loadIllegalPermissions();
  731. $_GET['group'] = (int) $_GET['group'];
  732. $_GET['pid'] = (int) $_GET['pid'];
  733. // Cannot modify predefined profiles.
  734. if ($_GET['pid'] > 1 && $_GET['pid'] < 5)
  735. fatal_lang_error('no_access', false);
  736. // Verify this isn't inherited.
  737. if ($_GET['group'] == -1 || $_GET['group'] == 0)
  738. $parent = -2;
  739. else
  740. {
  741. $result = $smcFunc['db_query']('', '
  742. SELECT id_parent
  743. FROM {db_prefix}membergroups
  744. WHERE id_group = {int:current_group}
  745. LIMIT 1',
  746. array(
  747. 'current_group' => $_GET['group'],
  748. )
  749. );
  750. list ($parent) = $smcFunc['db_fetch_row']($result);
  751. $smcFunc['db_free_result']($result);
  752. }
  753. if ($parent != -2)
  754. fatal_lang_error('cannot_edit_permissions_inherited');
  755. $givePerms = array('membergroup' => array(), 'board' => array());
  756. // Guest group, we need illegal, guest permissions.
  757. if ($_GET['group'] == -1)
  758. {
  759. loadIllegalGuestPermissions();
  760. $context['illegal_permissions'] = array_merge($context['illegal_permissions'], $context['non_guest_permissions']);
  761. }
  762. // Prepare all permissions that were set or denied for addition to the DB.
  763. if (isset($_POST['perm']) && is_array($_POST['perm']))
  764. {
  765. foreach ($_POST['perm'] as $perm_type => $perm_array)
  766. {
  767. if (is_array($perm_array))
  768. {
  769. foreach ($perm_array as $permission => $value)
  770. if ($value == 'on' || $value == 'deny')
  771. {
  772. // Don't allow people to escalate themselves!
  773. if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
  774. continue;
  775. $givePerms[$perm_type][] = array($_GET['group'], $permission, $value == 'deny' ? 0 : 1);
  776. }
  777. }
  778. }
  779. }
  780. // Insert the general permissions.
  781. if ($_GET['group'] != 3 && empty($_GET['pid']))
  782. {
  783. $smcFunc['db_query']('', '
  784. DELETE FROM {db_prefix}permissions
  785. WHERE id_group = {int:current_group}
  786. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  787. array(
  788. 'current_group' => $_GET['group'],
  789. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  790. )
  791. );
  792. if (!empty($givePerms['membergroup']))
  793. {
  794. $smcFunc['db_insert']('replace',
  795. '{db_prefix}permissions',
  796. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  797. $givePerms['membergroup'],
  798. array('id_group', 'permission')
  799. );
  800. }
  801. }
  802. // Insert the boardpermissions.
  803. $profileid = max(1, $_GET['pid']);
  804. $smcFunc['db_query']('', '
  805. DELETE FROM {db_prefix}board_permissions
  806. WHERE id_group = {int:current_group}
  807. AND id_profile = {int:current_profile}',
  808. array(
  809. 'current_group' => $_GET['group'],
  810. 'current_profile' => $profileid,
  811. )
  812. );
  813. if (!empty($givePerms['board']))
  814. {
  815. foreach ($givePerms['board'] as $k => $v)
  816. $givePerms['board'][$k][] = $profileid;
  817. $smcFunc['db_insert']('replace',
  818. '{db_prefix}board_permissions',
  819. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int', 'id_profile' => 'int'),
  820. $givePerms['board'],
  821. array('id_group', 'permission', 'id_profile')
  822. );
  823. }
  824. // Update any inherited permissions as required.
  825. updateChildPermissions($_GET['group'], $_GET['pid']);
  826. // Clear cached privs.
  827. updateSettings(array('settings_updated' => time()));
  828. redirectexit('action=admin;area=permissions;pid=' . $_GET['pid']);
  829. }
  830. /**
  831. * A screen to set some general settings for permissions.
  832. *
  833. * @param bool $return_config = false
  834. */
  835. function GeneralPermissionSettings($return_config = false)
  836. {
  837. global $context, $modSettings, $sourcedir, $txt, $scripturl, $smcFunc;
  838. // All the setting variables
  839. $config_vars = array(
  840. array('title', 'settings'),
  841. // Inline permissions.
  842. array('permissions', 'manage_permissions'),
  843. '',
  844. // A few useful settings
  845. array('check', 'permission_enable_deny', 0, $txt['permission_settings_enable_deny'], 'help' => 'permissions_deny'),
  846. array('check', 'permission_enable_postgroups', 0, $txt['permission_settings_enable_postgroups'], 'help' => 'permissions_postgroups'),
  847. );
  848. call_integration_hook('integrate_modify_permission_settings', array(&$config_vars));
  849. if ($return_config)
  850. return $config_vars;
  851. $context['page_title'] = $txt['permission_settings_title'];
  852. $context['sub_template'] = 'show_settings';
  853. // Needed for the inline permission functions, and the settings template.
  854. require_once($sourcedir . '/ManageServer.php');
  855. // Don't let guests have these permissions.
  856. $context['post_url'] = $scripturl . '?action=admin;area=permissions;save;sa=settings';
  857. $context['permissions_excluded'] = array(-1);
  858. // Saving the settings?
  859. if (isset($_GET['save']))
  860. {
  861. checkSession('post');
  862. call_integration_hook('integrate_save_permission_settings');
  863. saveDBSettings($config_vars);
  864. // Clear all deny permissions...if we want that.
  865. if (empty($modSettings['permission_enable_deny']))
  866. {
  867. $smcFunc['db_query']('', '
  868. DELETE FROM {db_prefix}permissions
  869. WHERE add_deny = {int:denied}',
  870. array(
  871. 'denied' => 0,
  872. )
  873. );
  874. $smcFunc['db_query']('', '
  875. DELETE FROM {db_prefix}board_permissions
  876. WHERE add_deny = {int:denied}',
  877. array(
  878. 'denied' => 0,
  879. )
  880. );
  881. }
  882. // Make sure there are no postgroup based permissions left.
  883. if (empty($modSettings['permission_enable_postgroups']))
  884. {
  885. // Get a list of postgroups.
  886. $post_groups = array();
  887. $request = $smcFunc['db_query']('', '
  888. SELECT id_group
  889. FROM {db_prefix}membergroups
  890. WHERE min_posts != {int:min_posts}',
  891. array(
  892. 'min_posts' => -1,
  893. )
  894. );
  895. while ($row = $smcFunc['db_fetch_assoc']($request))
  896. $post_groups[] = $row['id_group'];
  897. $smcFunc['db_free_result']($request);
  898. // Remove'em.
  899. $smcFunc['db_query']('', '
  900. DELETE FROM {db_prefix}permissions
  901. WHERE id_group IN ({array_int:post_group_list})',
  902. array(
  903. 'post_group_list' => $post_groups,
  904. )
  905. );
  906. $smcFunc['db_query']('', '
  907. DELETE FROM {db_prefix}board_permissions
  908. WHERE id_group IN ({array_int:post_group_list})',
  909. array(
  910. 'post_group_list' => $post_groups,
  911. )
  912. );
  913. $smcFunc['db_query']('', '
  914. UPDATE {db_prefix}membergroups
  915. SET id_parent = {int:not_inherited}
  916. WHERE id_parent IN ({array_int:post_group_list})',
  917. array(
  918. 'post_group_list' => $post_groups,
  919. 'not_inherited' => -2,
  920. )
  921. );
  922. }
  923. $_SESSION['adm-save'] = true;
  924. redirectexit('action=admin;area=permissions;sa=settings');
  925. }
  926. // We need this for the in-line permissions
  927. createToken('admin-mp');
  928. prepareDBSettingContext($config_vars);
  929. }
  930. /**
  931. * Set the permission level for a specific profile, group, or group for a profile.
  932. * @internal
  933. *
  934. * @param string $level
  935. * @param int $group
  936. * @param mixed $profile = null, int expected
  937. */
  938. function setPermissionLevel($level, $group, $profile = 'null')
  939. {
  940. global $smcFunc, $context;
  941. loadIllegalPermissions();
  942. loadIllegalGuestPermissions();
  943. // Levels by group... restrict, standard, moderator, maintenance.
  944. $groupLevels = array(
  945. 'board' => array('inherit' => array()),
  946. 'group' => array('inherit' => array())
  947. );
  948. // Levels by board... standard, publish, free.
  949. $boardLevels = array('inherit' => array());
  950. // Restrictive - ie. guests.
  951. $groupLevels['global']['restrict'] = array(
  952. 'search_posts',
  953. 'calendar_view',
  954. 'view_stats',
  955. 'who_view',
  956. 'profile_identity_own',
  957. );
  958. $groupLevels['board']['restrict'] = array(
  959. 'poll_view',
  960. 'post_new',
  961. 'post_reply_own',
  962. 'post_reply_any',
  963. 'delete_own',
  964. 'modify_own',
  965. 'report_any',
  966. 'send_topic',
  967. );
  968. // Standard - ie. members. They can do anything Restrictive can.
  969. $groupLevels['global']['standard'] = array_merge($groupLevels['global']['restrict'], array(
  970. 'view_mlist',
  971. 'karma_edit',
  972. 'pm_read',
  973. 'pm_send',
  974. 'send_email_to_members',
  975. 'profile_view',
  976. 'profile_extra_own',
  977. 'profile_signature_own',
  978. 'profile_forum_own',
  979. 'profile_other_own',
  980. 'profile_password_own',
  981. 'profile_server_avatar',
  982. 'profile_displayed_name',
  983. 'profile_upload_avatar',
  984. 'profile_remote_avatar',
  985. 'profile_remove_own',
  986. ));
  987. $groupLevels['board']['standard'] = array_merge($groupLevels['board']['restrict'], array(
  988. 'poll_vote',
  989. 'poll_edit_own',
  990. 'poll_post',
  991. 'poll_add_own',
  992. 'post_attachment',
  993. 'lock_own',
  994. 'remove_own',
  995. 'view_attachments',
  996. ));
  997. // Moderator - ie. moderators :P. They can do what standard can, and more.
  998. $groupLevels['global']['moderator'] = array_merge($groupLevels['global']['standard'], array(
  999. 'calendar_post',
  1000. 'calendar_edit_own',
  1001. 'access_mod_center',
  1002. 'issue_warning',
  1003. ));
  1004. $groupLevels['board']['moderator'] = array_merge($groupLevels['board']['standard'], array(
  1005. 'make_sticky',
  1006. 'poll_edit_any',
  1007. 'delete_any',
  1008. 'modify_any',
  1009. 'lock_any',
  1010. 'remove_any',
  1011. 'move_any',
  1012. 'merge_any',
  1013. 'split_any',
  1014. 'poll_lock_any',
  1015. 'poll_remove_any',
  1016. 'poll_add_any',
  1017. 'approve_posts',
  1018. ));
  1019. // Maintenance - wannabe admins. They can do almost everything.
  1020. $groupLevels['global']['maintenance'] = array_merge($groupLevels['global']['moderator'], array(
  1021. 'manage_attachments',
  1022. 'manage_smileys',
  1023. 'manage_boards',
  1024. 'moderate_forum',
  1025. 'manage_membergroups',
  1026. 'manage_bans',
  1027. 'admin_forum',
  1028. 'manage_permissions',
  1029. 'edit_news',
  1030. 'calendar_edit_any',
  1031. 'profile_identity_any',
  1032. 'profile_extra_any',
  1033. 'profile_signature_any',
  1034. 'profile_forum_own',
  1035. 'profile_other_any',
  1036. 'profile_displayed_name_any',
  1037. 'profile_password_any',
  1038. 'profile_title_any',
  1039. ));
  1040. $groupLevels['board']['maintenance'] = array_merge($groupLevels['board']['moderator'], array(
  1041. ));
  1042. // Standard - nothing above the group permissions. (this SHOULD be empty.)
  1043. $boardLevels['standard'] = array(
  1044. );
  1045. // Locked - just that, you can't post here.
  1046. $boardLevels['locked'] = array(
  1047. 'poll_view',
  1048. 'report_any',
  1049. 'send_topic',
  1050. 'view_attachments',
  1051. );
  1052. // Publisher - just a little more...
  1053. $boardLevels['publish'] = array_merge($boardLevels['locked'], array(
  1054. 'post_new',
  1055. 'post_reply_own',
  1056. 'post_reply_any',
  1057. 'delete_own',
  1058. 'modify_own',
  1059. 'delete_replies',
  1060. 'modify_replies',
  1061. 'poll_vote',
  1062. 'poll_edit_own',
  1063. 'poll_post',
  1064. 'poll_add_own',
  1065. 'poll_remove_own',
  1066. 'post_attachment',
  1067. 'lock_own',
  1068. 'remove_own',
  1069. ));
  1070. // Free for All - Scary. Just scary.
  1071. $boardLevels['free'] = array_merge($boardLevels['publish'], array(
  1072. 'poll_lock_any',
  1073. 'poll_edit_any',
  1074. 'poll_add_any',
  1075. 'poll_remove_any',
  1076. 'make_sticky',
  1077. 'lock_any',
  1078. 'remove_any',
  1079. 'delete_any',
  1080. 'split_any',
  1081. 'merge_any',
  1082. 'modify_any',
  1083. 'approve_posts',
  1084. ));
  1085. // Make sure we're not granting someone too many permissions!
  1086. foreach ($groupLevels['global'][$level] as $k => $permission)
  1087. {
  1088. if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
  1089. unset($groupLevels['global'][$level][$k]);
  1090. if ($group == -1 && in_array($permission, $context['non_guest_permissions']))
  1091. unset($groupLevels['global'][$level][$k]);
  1092. }
  1093. if ($group == -1)
  1094. foreach ($groupLevels['board'][$level] as $k => $permission)
  1095. if (in_array($permission, $context['non_guest_permissions']))
  1096. unset($groupLevels['board'][$level][$k]);
  1097. // Reset all cached permissions.
  1098. updateSettings(array('settings_updated' => time()));
  1099. // Setting group permissions.
  1100. if ($profile === 'null' && $group !== 'null')
  1101. {
  1102. $group = (int) $group;
  1103. if (empty($groupLevels['global'][$level]))
  1104. return;
  1105. $smcFunc['db_query']('', '
  1106. DELETE FROM {db_prefix}permissions
  1107. WHERE id_group = {int:current_group}
  1108. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  1109. array(
  1110. 'current_group' => $group,
  1111. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  1112. )
  1113. );
  1114. $smcFunc['db_query']('', '
  1115. DELETE FROM {db_prefix}board_permissions
  1116. WHERE id_group = {int:current_group}
  1117. AND id_profile = {int:default_profile}',
  1118. array(
  1119. 'current_group' => $group,
  1120. 'default_profile' => 1,
  1121. )
  1122. );
  1123. $groupInserts = array();
  1124. foreach ($groupLevels['global'][$level] as $permission)
  1125. $groupInserts[] = array($group, $permission);
  1126. $smcFunc['db_insert']('insert',
  1127. '{db_prefix}permissions',
  1128. array('id_group' => 'int', 'permission' => 'string'),
  1129. $groupInserts,
  1130. array('id_group')
  1131. );
  1132. $boardInserts = array();
  1133. foreach ($groupLevels['board'][$level] as $permission)
  1134. $boardInserts[] = array(1, $group, $permission);
  1135. $smcFunc['db_insert']('insert',
  1136. '{db_prefix}board_permissions',
  1137. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1138. $boardInserts,
  1139. array('id_profile', 'id_group')
  1140. );
  1141. }
  1142. // Setting profile permissions for a specific group.
  1143. elseif ($profile !== 'null' && $group !== 'null' && ($profile == 1 || $profile > 4))
  1144. {
  1145. $group = (int) $group;
  1146. $profile = (int) $profile;
  1147. if (!empty($groupLevels['global'][$level]))
  1148. {
  1149. $smcFunc['db_query']('', '
  1150. DELETE FROM {db_prefix}board_permissions
  1151. WHERE id_group = {int:current_group}
  1152. AND id_profile = {int:current_profile}',
  1153. array(
  1154. 'current_group' => $group,
  1155. 'current_profile' => $profile,
  1156. )
  1157. );
  1158. }
  1159. if (!empty($groupLevels['board'][$level]))
  1160. {
  1161. $boardInserts = array();
  1162. foreach ($groupLevels['board'][$level] as $permission)
  1163. $boardInserts[] = array($profile, $group, $permission);
  1164. $smcFunc['db_insert']('insert',
  1165. '{db_prefix}board_permissions',
  1166. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1167. $boardInserts,
  1168. array('id_profile', 'id_group')
  1169. );
  1170. }
  1171. }
  1172. // Setting profile permissions for all groups.
  1173. elseif ($profile !== 'null' && $group === 'null' && ($profile == 1 || $profile > 4))
  1174. {
  1175. $profile = (int) $profile;
  1176. $smcFunc['db_query']('', '
  1177. DELETE FROM {db_prefix}board_permissions
  1178. WHERE id_profile = {int:current_profile}',
  1179. array(
  1180. 'current_profile' => $profile,
  1181. )
  1182. );
  1183. if (empty($boardLevels[$level]))
  1184. return;
  1185. // Get all the groups...
  1186. $query = $smcFunc['db_query']('', '
  1187. SELECT id_group
  1188. FROM {db_prefix}membergroups
  1189. WHERE id_group > {int:moderator_group}
  1190. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  1191. array(
  1192. 'moderator_group' => 3,
  1193. 'newbie_group' => 4,
  1194. )
  1195. );
  1196. while ($row = $smcFunc['db_fetch_row']($query))
  1197. {
  1198. $group = $row[0];
  1199. $boardInserts = array();
  1200. foreach ($boardLevels[$level] as $permission)
  1201. $boardInserts[] = array($profile, $group, $permission);
  1202. $smcFunc['db_insert']('insert',
  1203. '{db_prefix}board_permissions',
  1204. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1205. $boardInserts,
  1206. array('id_profile', 'id_group')
  1207. );
  1208. }
  1209. $smcFunc['db_free_result']($query);
  1210. // Add permissions for ungrouped members.
  1211. $boardInserts = array();
  1212. foreach ($boardLevels[$level] as $permission)
  1213. $boardInserts[] = array($profile, 0, $permission);
  1214. $smcFunc['db_insert']('insert',
  1215. '{db_prefix}board_permissions',
  1216. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1217. $boardInserts,
  1218. array('id_profile', 'id_group')
  1219. );
  1220. }
  1221. // $profile and $group are both null!
  1222. else
  1223. fatal_lang_error('no_access', false);
  1224. }
  1225. /**
  1226. * Load permissions into $context['permissions'].
  1227. * @internal
  1228. */
  1229. function loadAllPermissions()
  1230. {
  1231. global $context, $txt, $modSettings;
  1232. // List of all the groups dependant on the currently selected view - for the order so it looks pretty, yea?
  1233. // 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.
  1234. $permissionGroups = array(
  1235. 'membergroup' => array(
  1236. 'general',
  1237. 'pm',
  1238. 'calendar',
  1239. 'maintenance',
  1240. 'member_admin',
  1241. 'profile',
  1242. ),
  1243. 'board' => array(
  1244. 'general_board',
  1245. 'topic',
  1246. 'post',
  1247. 'poll',
  1248. 'notification',
  1249. 'attachment',
  1250. ),
  1251. );
  1252. /* The format of this list is as follows:
  1253. 'membergroup' => array(
  1254. 'permissions_inside' => array(has_multiple_options, view_group),
  1255. ),
  1256. 'board' => array(
  1257. 'permissions_inside' => array(has_multiple_options, view_group),
  1258. );
  1259. */
  1260. $permissionList = array(
  1261. 'membergroup' => array(
  1262. 'view_stats' => array(false, 'general'),
  1263. 'view_mlist' => array(false, 'general'),
  1264. 'who_view' => array(false, 'general'),
  1265. 'search_posts' => array(false, 'general'),
  1266. 'karma_edit' => array(false, 'general'),
  1267. 'pm_read' => array(false, 'pm'),
  1268. 'pm_send' => array(false, 'pm'),
  1269. 'pm_draft' => array(false, 'pm'),
  1270. 'pm_autosave_draft' => array(false, 'pm'),
  1271. 'send_email_to_members' => array(false, 'pm'),
  1272. 'calendar_view' => array(false, 'calendar'),
  1273. 'calendar_post' => array(false, 'calendar'),
  1274. 'calendar_edit' => array(true, 'calendar'),
  1275. 'admin_forum' => array(false, 'maintenance'),
  1276. 'manage_boards' => array(false, 'maintenance'),
  1277. 'manage_attachments' => array(false, 'maintenance'),
  1278. 'manage_smileys' => array(false, 'maintenance'),
  1279. 'edit_news' => array(false, 'maintenance'),
  1280. 'access_mod_center' => array(false, 'maintenance'),
  1281. 'moderate_forum' => array(false, 'member_admin'),
  1282. 'manage_membergroups' => array(false, 'member_admin'),
  1283. 'manage_permissions' => array(false, 'member_admin'),
  1284. 'manage_bans' => array(false, 'member_admin'),
  1285. 'send_mail' => array(false, 'member_admin'),
  1286. 'issue_warning' => array(false, 'member_admin'),
  1287. 'profile_view' => array(false, 'profile'),
  1288. 'profile_forum' => array(true, 'profile'),
  1289. 'profile_extra' => array(true, 'profile'),
  1290. 'profile_signature' => array(true, 'profile'),
  1291. 'profile_other' => array(true, 'profile'),
  1292. 'profile_title' => array(true, 'profile'),
  1293. 'profile_blurb' => array(true, 'profile'),
  1294. 'profile_server_avatar' => array(false, 'profile'),
  1295. 'profile_upload_avatar' => array(false, 'profile'),
  1296. 'profile_remote_avatar' => array(false, 'profile'),
  1297. 'profile_identity' => array(true, 'profile_account'),
  1298. 'profile_displayed_name' => array(true, 'profile_account'),
  1299. 'profile_password' => array(true, 'profile_account'),
  1300. 'profile_remove' => array(true, 'profile_account'),
  1301. ),
  1302. 'board' => array(
  1303. 'moderate_board' => array(false, 'general_board'),
  1304. 'approve_posts' => array(false, 'general_board'),
  1305. 'post_new' => array(false, 'topic'),
  1306. 'post_draft' => array(false, 'topic'),
  1307. 'post_autosave_draft' => array(false, 'topic'),
  1308. 'post_unapproved_topics' => array(false, 'topic'),
  1309. 'post_unapproved_replies' => array(true, 'topic'),
  1310. 'post_reply' => array(true, 'topic'),
  1311. 'merge_any' => array(false, 'topic'),
  1312. 'split_any' => array(false, 'topic'),
  1313. 'send_topic' => array(false, 'topic'),
  1314. 'make_sticky' => array(false, 'topic'),
  1315. 'move' => array(true, 'topic', 'moderate'),
  1316. 'lock' => array(true, 'topic', 'moderate'),
  1317. 'remove' => array(true, 'topic', 'modify'),
  1318. 'modify_replies' => array(false, 'topic'),
  1319. 'delete_replies' => array(false, 'topic'),
  1320. 'announce_topic' => array(false, 'topic'),
  1321. 'delete' => array(true, 'post'),
  1322. 'modify' => array(true, 'post'),
  1323. 'report_any' => array(false, 'post'),
  1324. 'poll_view' => array(false, 'poll'),
  1325. 'poll_vote' => array(false, 'poll'),
  1326. 'poll_post' => array(false, 'poll'),
  1327. 'poll_add' => array(true, 'poll'),
  1328. 'poll_edit' => array(true, 'poll'),
  1329. 'poll_lock' => array(true, 'poll'),
  1330. 'poll_remove' => array(true, 'poll'),
  1331. 'view_attachments' => array(false, 'attachment'),
  1332. 'post_unapproved_attachments' => array(false, 'attachment'),
  1333. 'post_attachment' => array(false, 'attachment'),
  1334. ),
  1335. );
  1336. // All permission groups that will be shown in the left column on classic view.
  1337. $leftPermissionGroups = array(
  1338. 'general',
  1339. 'calendar',
  1340. 'maintenance',
  1341. 'member_admin',
  1342. 'topic',
  1343. 'post',
  1344. );
  1345. // We need to know what permissions we can't give to guests.
  1346. loadIllegalGuestPermissions();
  1347. // Some permissions are hidden if features are off.
  1348. $hiddenPermissions = array();
  1349. $relabelPermissions = array(); // Permissions to apply a different label to.
  1350. $relabelGroups = array(); // As above but for groups.
  1351. if (empty($modSettings['cal_enabled']))
  1352. {
  1353. $hiddenPermissions[] = 'calendar_view';
  1354. $hiddenPermissions[] = 'calendar_post';
  1355. $hiddenPermissions[] = 'calendar_edit';
  1356. }
  1357. if ($modSettings['warning_settings'][0] == 0)
  1358. $hiddenPermissions[] = 'issue_warning';
  1359. if (empty($modSettings['karmaMode']))
  1360. $hiddenPermissions[] = 'karma_edit';
  1361. // Post moderation?
  1362. if (!$modSettings['postmod_active'])
  1363. {
  1364. $hiddenPermissions[] = 'approve_posts';
  1365. $hiddenPermissions[] = 'post_unapproved_topics';
  1366. $hiddenPermissions[] = 'post_unapproved_replies';
  1367. $hiddenPermissions[] = 'post_unapproved_attachments';
  1368. }
  1369. // If we show them on classic view we change the name.
  1370. else
  1371. {
  1372. // Relabel the topics permissions
  1373. $relabelPermissions['post_new'] = 'auto_approve_topics';
  1374. // Relabel the reply permissions
  1375. $relabelPermissions['post_reply'] = 'auto_approve_replies';
  1376. // Relabel the attachment permissions
  1377. $relabelPermissions['post_attachment'] = 'auto_approve_attachments';
  1378. }
  1379. // Are attachments enabled?
  1380. if (empty($modSettings['attachmentEnable']))
  1381. {
  1382. $hiddenPermissions[] = 'manage_attachments';
  1383. $hiddenPermissions[] = 'view_attachments';
  1384. $hiddenPermissions[] = 'post_unapproved_attachments';
  1385. $hiddenPermissions[] = 'post_attachment';
  1386. }
  1387. // Provide a practical way to modify permissions.
  1388. call_integration_hook('integrate_load_permissions', array(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions));
  1389. $context['permissions'] = array();
  1390. $context['hidden_permissions'] = array();
  1391. foreach ($permissionList as $permissionType => $permissionList)
  1392. {
  1393. $context['permissions'][$permissionType] = array(
  1394. 'id' => $permissionType,
  1395. 'columns' => array()
  1396. );
  1397. foreach ($permissionList as $permission => $permissionArray)
  1398. {
  1399. // If this is a guest permission we don't do it if it's the guest group.
  1400. if (isset($context['group']['id']) && $context['group']['id'] == -1 && in_array($permission, $context['non_guest_permissions']))
  1401. continue;
  1402. // What groups will this permission be in?
  1403. $own_group = $permissionArray[1];
  1404. // First, Do these groups actually exist - if not add them.
  1405. if (!isset($permissionGroups[$permissionType][$own_group]))
  1406. $permissionGroups[$permissionType][$own_group] = true;
  1407. // What column should this be located into?
  1408. $position = !in_array($own_group, $leftPermissionGroups) ? 1 : 0;
  1409. // If the groups have not yet been created be sure to create them.
  1410. $bothGroups = array('own' => $own_group);
  1411. foreach ($bothGroups as $group)
  1412. if (!isset($context['permissions'][$permissionType]['columns'][$position][$group]))
  1413. $context['permissions'][$permissionType]['columns'][$position][$group] = array(
  1414. 'type' => $permissionType,
  1415. 'id' => $group,
  1416. 'name' => $txt['permissiongroup_' . $group],
  1417. 'icon' => isset($txt['permissionicon_' . $group]) ? $txt['permissionicon_' . $group] : $txt['permissionicon'],
  1418. 'help' => isset($txt['permissionhelp_' . $group]) ? $txt['permissionhelp_' . $group] : '',
  1419. 'hidden' => false,
  1420. 'permissions' => array()
  1421. );
  1422. $context['permissions'][$permissionType]['columns'][$position][$own_group]['permissions'][$permission] = array(
  1423. 'id' => $permission,
  1424. 'name' => !isset($relabelPermissions[$permission]) ? $txt['permissionname_' . $permission] : $txt[$relabelPermissions[$permission]],
  1425. 'show_help' => isset($txt['permissionhelp_' . $permission]),
  1426. 'note' => isset($txt['permissionnote_' . $permission]) ? $txt['permissionnote_' . $permission] : '',
  1427. 'has_own_any' => $permissionArray[0],
  1428. 'own' => array(
  1429. 'id' => $permission . '_own',
  1430. 'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_own'] : ''
  1431. ),
  1432. 'any' => array(
  1433. 'id' => $permission . '_any',
  1434. 'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_any'] : ''
  1435. ),
  1436. 'hidden' => in_array($permission, $hiddenPermissions),
  1437. );
  1438. if (in_array($permission, $hiddenPermissions))
  1439. {
  1440. if ($permissionArray[0])
  1441. {
  1442. $context['hidden_permissions'][] = $permission . '_own';
  1443. $context['hidden_permissions'][] = $permission . '_any';
  1444. }
  1445. else
  1446. $context['hidden_permissions'][] = $permission;
  1447. }
  1448. }
  1449. ksort($context['permissions'][$permissionType]['columns']);
  1450. // Check we don't leave any empty groups - and mark hidden ones as such.
  1451. foreach ($context['permissions'][$permissionType]['columns'] as $column => $groups)
  1452. foreach ($groups as $id => $group)
  1453. {
  1454. if (empty($group['permissions']))
  1455. unset($context['permissions'][$permissionType]['columns'][$column][$id]);
  1456. else
  1457. {
  1458. $foundNonHidden = false;
  1459. foreach ($group['permissions'] as $permission)
  1460. if (empty($permission['hidden']))
  1461. $foundNonHidden = true;
  1462. if (!$foundNonHidden)
  1463. $context['permissions'][$permissionType]['columns'][$column][$id]['hidden'] = true;
  1464. }
  1465. }
  1466. }
  1467. }
  1468. /**
  1469. * Initialize a form with inline permissions settings.
  1470. * It loads a context variables for each permission.
  1471. * This function is used by several settings screens to set specific permissions.
  1472. * @internal
  1473. *
  1474. * @param array $permissions
  1475. * @param array $excluded_groups = array()
  1476. *
  1477. * @uses ManagePermissions language
  1478. * @uses ManagePermissions template.
  1479. */
  1480. function init_inline_permissions($permissions, $excluded_groups = array())
  1481. {
  1482. global $context, $txt, $modSettings, $smcFunc;
  1483. loadLanguage('ManagePermissions');
  1484. loadTemplate('ManagePermissions');
  1485. $context['can_change_permissions'] = allowedTo('manage_permissions');
  1486. // Nothing to initialize here.
  1487. if (!$context['can_change_permissions'])
  1488. return;
  1489. // Load the permission settings for guests
  1490. foreach ($permissions as $permission)
  1491. $context[$permission] = array(
  1492. -1 => array(
  1493. 'id' => -1,
  1494. 'name' => $txt['membergroups_guests'],
  1495. 'is_postgroup' => false,
  1496. 'status' => 'off',
  1497. ),
  1498. 0 => array(
  1499. 'id' => 0,
  1500. 'name' => $txt['membergroups_members'],
  1501. 'is_postgroup' => false,
  1502. 'status' => 'off',
  1503. ),
  1504. );
  1505. $request = $smcFunc['db_query']('', '
  1506. SELECT id_group, CASE WHEN add_deny = {int:denied} THEN {string:deny} ELSE {string:on} END AS status, permission
  1507. FROM {db_prefix}permissions
  1508. WHERE id_group IN (-1, 0)
  1509. AND permission IN ({array_string:permissions})',
  1510. array(
  1511. 'denied' => 0,
  1512. 'permissions' => $permissions,
  1513. 'deny' => 'deny',
  1514. 'on' => 'on',
  1515. )
  1516. );
  1517. while ($row = $smcFunc['db_fetch_assoc']($request))
  1518. $context[$row['permission']][$row['id_group']]['status'] = $row['status'];
  1519. $smcFunc['db_free_result']($request);
  1520. $request = $smcFunc['db_query']('', '
  1521. SELECT mg.id_group, mg.group_name, mg.min_posts, IFNULL(p.add_deny, -1) AS status, p.permission
  1522. FROM {db_prefix}membergroups AS mg
  1523. LEFT JOIN {db_prefix}permissions AS p ON (p.id_group = mg.id_group AND p.permission IN ({array_string:permissions}))
  1524. WHERE mg.id_group NOT IN (1, 3)
  1525. AND mg.id_parent = {int:not_inherited}' . (empty($modSettings['permission_enable_postgroups']) ? '
  1526. AND mg.min_posts = {int:min_posts}' : '') . '
  1527. ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
  1528. array(
  1529. 'not_inherited' => -2,
  1530. 'min_posts' => -1,
  1531. 'newbie_group' => 4,
  1532. 'permissions' => $permissions,
  1533. )
  1534. );
  1535. while ($row = $smcFunc['db_fetch_assoc']($request))
  1536. {
  1537. // Initialize each permission as being 'off' until proven otherwise.
  1538. foreach ($permissions as $permission)
  1539. if (!isset($context[$permission][$row['id_group']]))
  1540. $context[$permission][$row['id_group']] = array(
  1541. 'id' => $row['id_group'],
  1542. 'name' => $row['group_name'],
  1543. 'is_postgroup' => $row['min_posts'] != -1,
  1544. 'status' => 'off',
  1545. );
  1546. $context[$row['permission']][$row['id_group']]['status'] = empty($row['status']) ? 'deny' : ($row['status'] == 1 ? 'on' : 'off');
  1547. }
  1548. $smcFunc['db_free_result']($request);
  1549. // Some permissions cannot be given to certain groups. Remove the groups.
  1550. foreach ($excluded_groups as $group)
  1551. {
  1552. foreach ($permissions as $permission)
  1553. {
  1554. if (isset($context[$permission][$group]))
  1555. unset($context[$permission][$group]);
  1556. }
  1557. }
  1558. }
  1559. /**
  1560. * Show a collapsible box to set a specific permission.
  1561. * The function is called by templates to show a list of permissions settings.
  1562. * Calls the template function template_inline_permissions().
  1563. *
  1564. * @param string $permission
  1565. */
  1566. function theme_inline_permissions($permission)
  1567. {
  1568. global $context;
  1569. $context['current_permission'] = $permission;
  1570. $context['member_groups'] = $context[$permission];
  1571. template_inline_permissions();
  1572. }
  1573. /**
  1574. * Save the permissions of a form containing inline permissions.
  1575. * @internal
  1576. *
  1577. * @param array $permissions
  1578. */
  1579. function save_inline_permissions($permissions)
  1580. {
  1581. global $context, $smcFunc;
  1582. // No permissions? Not a great deal to do here.
  1583. if (!allowedTo('manage_permissions'))
  1584. return;
  1585. // Almighty session check, verify our ways.
  1586. checkSession();
  1587. validateToken('admin-mp');
  1588. // Check they can't do certain things.
  1589. loadIllegalPermissions();
  1590. $insertRows = array();
  1591. foreach ($permissions as $permission)
  1592. {
  1593. if (!isset($_POST[$permission]))
  1594. continue;
  1595. foreach ($_POST[$permission] as $id_group => $value)
  1596. {
  1597. if (in_array($value, array('on', 'deny')) && (empty($context['illegal_permissions']) || !in_array($permission, $context['illegal_permissions'])))
  1598. $insertRows[] = array((int) $id_group, $permission, $value == 'on' ? 1 : 0);
  1599. }
  1600. }
  1601. // Remove the old permissions...
  1602. $smcFunc['db_query']('', '
  1603. DELETE FROM {db_prefix}permissions
  1604. WHERE permission IN ({array_string:permissions})
  1605. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  1606. array(
  1607. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  1608. 'permissions' => $permissions,
  1609. )
  1610. );
  1611. // ...and replace them with new ones.
  1612. if (!empty($insertRows))
  1613. $smcFunc['db_insert']('insert',
  1614. '{db_prefix}permissions',
  1615. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1616. $insertRows,
  1617. array('id_group', 'permission')
  1618. );
  1619. // Do a full child update.
  1620. updateChildPermissions(array(), -1);
  1621. // Just in case we cached this.
  1622. updateSettings(array('settings_updated' => time()));
  1623. }
  1624. /**
  1625. * Load permissions profiles.
  1626. */
  1627. function loadPermissionProfiles()
  1628. {
  1629. global $context, $txt, $smcFunc;
  1630. $request = $smcFunc['db_query']('', '
  1631. SELECT id_profile, profile_name
  1632. FROM {db_prefix}permission_profiles
  1633. ORDER BY id_profile',
  1634. array(
  1635. )
  1636. );
  1637. $context['profiles'] = array();
  1638. while ($row = $smcFunc['db_fetch_assoc']($request))
  1639. {
  1640. // Format the label nicely.
  1641. if (isset($txt['permissions_profile_' . $row['profile_name']]))
  1642. $name = $txt['permissions_profile_' . $row['profile_name']];
  1643. else
  1644. $name = $row['profile_name'];
  1645. $context['profiles'][$row['id_profile']] = array(
  1646. 'id' => $row['id_profile'],
  1647. 'name' => $name,
  1648. 'can_modify' => $row['id_profile'] == 1 || $row['id_profile'] > 4,
  1649. 'unformatted_name' => $row['profile_name'],
  1650. );
  1651. }
  1652. $smcFunc['db_free_result']($request);
  1653. }
  1654. /**
  1655. * Add/Edit/Delete profiles.
  1656. */
  1657. function EditPermissionProfiles()
  1658. {
  1659. global $context, $txt, $smcFunc;
  1660. // Setup the template, first for fun.
  1661. $context['page_title'] = $txt['permissions_profile_edit'];
  1662. $context['sub_template'] = 'edit_profiles';
  1663. // If we're creating a new one do it first.
  1664. if (isset($_POST['create']) && trim($_POST['profile_name']) != '')
  1665. {
  1666. checkSession();
  1667. validateToken('admin-mpp');
  1668. $_POST['copy_from'] = (int) $_POST['copy_from'];
  1669. $_POST['profile_name'] = $smcFunc['htmlspecialchars']($_POST['profile_name']);
  1670. // Insert the profile itself.
  1671. $smcFunc['db_insert']('',
  1672. '{db_prefix}permission_profiles',
  1673. array(
  1674. 'profile_name' => 'string',
  1675. ),
  1676. array(
  1677. $_POST['profile_name'],
  1678. ),
  1679. array('id_profile')
  1680. );
  1681. $profile_id = $smcFunc['db_insert_id']('{db_prefix}permission_profiles', 'id_profile');
  1682. // Load the permissions from the one it's being copied from.
  1683. $request = $smcFunc['db_query']('', '
  1684. SELECT id_group, permission, add_deny
  1685. FROM {db_prefix}board_permissions
  1686. WHERE id_profile = {int:copy_from}',
  1687. array(
  1688. 'copy_from' => $_POST['copy_from'],
  1689. )
  1690. );
  1691. $inserts = array();
  1692. while ($row = $smcFunc['db_fetch_assoc']($request))
  1693. $inserts[] = array($profile_id, $row['id_group'], $row['permission'], $row['add_deny']);
  1694. $smcFunc['db_free_result']($request);
  1695. if (!empty($inserts))
  1696. $smcFunc['db_insert']('insert',
  1697. '{db_prefix}board_permissions',
  1698. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1699. $inserts,
  1700. array('id_profile', 'id_group', 'permission')
  1701. );
  1702. }
  1703. // Renaming?
  1704. elseif (isset($_POST['rename']))
  1705. {
  1706. checkSession();
  1707. validateToken('admin-mpp');
  1708. // Just showing the boxes?
  1709. if (!isset($_POST['rename_profile']))
  1710. $context['show_rename_boxes'] = true;
  1711. else
  1712. {
  1713. foreach ($_POST['rename_profile'] as $id => $value)
  1714. {
  1715. $value = $smcFunc['htmlspecialchars']($value);
  1716. if (trim($value) != '' && $id > 4)
  1717. $smcFunc['db_query']('', '
  1718. UPDATE {db_prefix}permission_profiles
  1719. SET profile_name = {string:profile_name}
  1720. WHERE id_profile = {int:current_profile}',
  1721. array(
  1722. 'current_profile' => (int) $id,
  1723. 'profile_name' => $value,
  1724. )
  1725. );
  1726. }
  1727. }
  1728. }
  1729. // Deleting?
  1730. elseif (isset($_POST['delete']) && !empty($_POST['delete_profile']))
  1731. {
  1732. checkSession('post');
  1733. validateToken('admin-mpp');
  1734. $profiles = array();
  1735. foreach ($_POST['delete_profile'] as $profile)
  1736. if ($profile > 4)
  1737. $profiles[] = (int) $profile;
  1738. // Verify it's not in use...
  1739. $request = $smcFunc['db_query']('', '
  1740. SELECT id_board
  1741. FROM {db_prefix}boards
  1742. WHERE id_profile IN ({array_int:profile_list})
  1743. LIMIT 1',
  1744. array(
  1745. 'profile_list' => $profiles,
  1746. )
  1747. );
  1748. if ($smcFunc['db_num_rows']($request) != 0)
  1749. fatal_lang_error('no_access', false);
  1750. $smcFunc['db_free_result']($request);
  1751. // Oh well, delete.
  1752. $smcFunc['db_query']('', '
  1753. DELETE FROM {db_prefix}permission_profiles
  1754. WHERE id_profile IN ({array_int:profile_list})',
  1755. array(
  1756. 'profile_list' => $profiles,
  1757. )
  1758. );
  1759. }
  1760. // Clearly, we'll need this!
  1761. loadPermissionProfiles();
  1762. // Work out what ones are in use.
  1763. $request = $smcFunc['db_query']('', '
  1764. SELECT id_profile, COUNT(id_board) AS board_count
  1765. FROM {db_prefix}boards
  1766. GROUP BY id_profile',
  1767. array(
  1768. )
  1769. );
  1770. while ($row = $smcFunc['db_fetch_assoc']($request))
  1771. if (isset($context['profiles'][$row['id_profile']]))
  1772. {
  1773. $context['profiles'][$row['id_profile']]['in_use'] = true;
  1774. $context['profiles'][$row['id_profile']]['boards'] = $row['board_count'];
  1775. $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')];
  1776. }
  1777. $smcFunc['db_free_result']($request);
  1778. // What can we do with these?
  1779. $context['can_edit_something'] = false;
  1780. foreach ($context['profiles'] as $id => $profile)
  1781. {
  1782. // Can't delete special ones.
  1783. $context['profiles'][$id]['can_edit'] = isset($txt['permissions_profile_' . $profile['unformatted_name']]) ? false : true;
  1784. if ($context['profiles'][$id]['can_edit'])
  1785. $context['can_edit_something'] = true;
  1786. // You can only delete it if you can edit it AND it's not in use.
  1787. $context['profiles'][$id]['can_delete'] = $context['profiles'][$id]['can_edit'] && empty($profile['in_use']) ? true : false;
  1788. }
  1789. createToken('admin-mpp');
  1790. }
  1791. /**
  1792. * This function updates the permissions of any groups based off this group.
  1793. *
  1794. * @param mixed $parents (array or int)
  1795. * @param mixed $profile = null, int expected
  1796. */
  1797. function updateChildPermissions($parents, $profile = null)
  1798. {
  1799. global $smcFunc;
  1800. // All the parent groups to sort out.
  1801. if (!is_array($parents))
  1802. $parents = array($parents);
  1803. // Find all the children of this group.
  1804. $request = $smcFunc['db_query']('', '
  1805. SELECT id_parent, id_group
  1806. FROM {db_prefix}membergroups
  1807. WHERE id_parent != {int:not_inherited}
  1808. ' . (empty($parents) ? '' : 'AND id_parent IN ({array_int:parent_list})'),
  1809. array(
  1810. 'parent_list' => $parents,
  1811. 'not_inherited' => -2,
  1812. )
  1813. );
  1814. $children = array();
  1815. $parents = array();
  1816. $child_groups = array();
  1817. while ($row = $smcFunc['db_fetch_assoc']($request))
  1818. {
  1819. $children[$row['id_parent']][] = $row['id_group'];
  1820. $child_groups[] = $row['id_group'];
  1821. $parents[] = $row['id_parent'];
  1822. }
  1823. $smcFunc['db_free_result']($request);
  1824. $parents = array_unique($parents);
  1825. // Not a sausage, or a child?
  1826. if (empty($children))
  1827. return false;
  1828. // First off, are we doing general permissions?
  1829. if ($profile < 1 || $profile === null)
  1830. {
  1831. // Fetch all the parent permissions.
  1832. $request = $smcFunc['db_query']('', '
  1833. SELECT id_group, permission, add_deny
  1834. FROM {db_prefix}permissions
  1835. WHERE id_group IN ({array_int:parent_list})',
  1836. array(
  1837. 'parent_list' => $parents,
  1838. )
  1839. );
  1840. $permissions = array();
  1841. while ($row = $smcFunc['db_fetch_assoc']($request))
  1842. foreach ($children[$row['id_group']] as $child)
  1843. $permissions[] = array($child, $row['permission'], $row['add_deny']);
  1844. $smcFunc['db_free_result']($request);
  1845. $smcFunc['db_query']('', '
  1846. DELETE FROM {db_prefix}permissions
  1847. WHERE id_group IN ({array_int:child_groups})',
  1848. array(
  1849. 'child_groups' => $child_groups,
  1850. )
  1851. );
  1852. // Finally insert.
  1853. if (!empty($permissions))
  1854. {
  1855. $smcFunc['db_insert']('insert',
  1856. '{db_prefix}permissions',
  1857. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1858. $permissions,
  1859. array('id_group', 'permission')
  1860. );
  1861. }
  1862. }
  1863. // Then, what about board profiles?
  1864. if ($profile != -1)
  1865. {
  1866. $profileQuery = $profile === null ? '' : ' AND id_profile = {int:current_profile}';
  1867. // Again, get all the parent permissions.
  1868. $request = $smcFunc['db_query']('', '
  1869. SELECT id_profile, id_group, permission, add_deny
  1870. FROM {db_prefix}board_permissions
  1871. WHERE id_group IN ({array_int:parent_groups})
  1872. ' . $profileQuery,
  1873. array(
  1874. 'parent_groups' => $parents,
  1875. 'current_profile' => $profile !== null && $profile ? $profile : 1,
  1876. )
  1877. );
  1878. $permissions = array();
  1879. while ($row = $smcFunc['db_fetch_assoc']($request))
  1880. foreach ($children[$row['id_group']] as $child)
  1881. $permissions[] = array($child, $row['id_profile'], $row['permission'], $row['add_deny']);
  1882. $smcFunc['db_free_result']($request);
  1883. $smcFunc['db_query']('', '
  1884. DELETE FROM {db_prefix}board_permissions
  1885. WHERE id_group IN ({array_int:child_groups})
  1886. ' . $profileQuery,
  1887. array(
  1888. 'child_groups' => $child_groups,
  1889. 'current_profile' => $profile !== null && $profile ? $profile : 1,
  1890. )
  1891. );
  1892. // Do the insert.
  1893. if (!empty($permissions))
  1894. {
  1895. $smcFunc['db_insert']('insert',
  1896. '{db_prefix}board_permissions',
  1897. array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1898. $permissions,
  1899. array('id_group', 'id_profile', 'permission')
  1900. );
  1901. }
  1902. }
  1903. }
  1904. /**
  1905. * Load permissions someone cannot grant.
  1906. */
  1907. function loadIllegalPermissions()
  1908. {
  1909. global $context;
  1910. $context['illegal_permissions'] = array();
  1911. if (!allowedTo('admin_forum'))
  1912. $context['illegal_permissions'][] = 'admin_forum';
  1913. if (!allowedTo('manage_membergroups'))
  1914. $context['illegal_permissions'][] = 'manage_membergroups';
  1915. if (!allowedTo('manage_permissions'))
  1916. $context['illegal_permissions'][] = 'manage_permissions';
  1917. call_integration_hook('integrate_load_illegal_permissions');
  1918. }
  1919. /**
  1920. * Loads the permissions that can not be given to guests.
  1921. * Stores the permissions in $context['non_guest_permissions'].
  1922. */
  1923. function loadIllegalGuestPermissions()
  1924. {
  1925. global $context;
  1926. $context['non_guest_permissions'] = array(
  1927. 'access_mod_center',
  1928. 'admin_forum',
  1929. 'announce_topic',
  1930. 'approve_posts',
  1931. 'calendar_edit',
  1932. 'delete',
  1933. 'delete_replies',
  1934. 'edit_news',
  1935. 'issue_warning',
  1936. 'karma_edit',
  1937. 'lock',
  1938. 'make_sticky',
  1939. 'manage_attachments',
  1940. 'manage_bans',
  1941. 'manage_boards',
  1942. 'manage_membergroups',
  1943. 'manage_permissions',
  1944. 'manage_smileys',
  1945. 'merge_any',
  1946. 'moderate_board',
  1947. 'moderate_forum',
  1948. 'modify',
  1949. 'modify_replies',
  1950. 'move',
  1951. 'pm_autosave_draft',
  1952. 'pm_draft',
  1953. 'pm_read',
  1954. 'pm_send',
  1955. 'poll_add',
  1956. 'poll_edit',
  1957. 'poll_lock',
  1958. 'poll_remove',
  1959. 'post_autosave_draft',
  1960. 'post_draft',
  1961. 'profile_blurb',
  1962. 'profile_displayed_name',
  1963. 'profile_extra',
  1964. 'profile_forum',
  1965. 'profile_identity',
  1966. 'profile_other',
  1967. 'profile_password',
  1968. 'profile_remove',
  1969. 'profile_remote_avatar',
  1970. 'profile_server_avatar',
  1971. 'profile_signature',
  1972. 'profile_title',
  1973. 'profile_upload_avatar',
  1974. 'remove',
  1975. 'report_any',
  1976. 'send_email_to_members',
  1977. 'send_mail',
  1978. 'split_any',
  1979. );
  1980. call_integration_hook('integrate_load_illegal_guest_permissions');
  1981. }
  1982. /**
  1983. * Present a nice way of applying post moderation.
  1984. */
  1985. function ModifyPostModeration()
  1986. {
  1987. global $context, $txt, $smcFunc, $modSettings, $sourcedir;
  1988. // Just in case.
  1989. checkSession('get');
  1990. $context['page_title'] = $txt['permissions_post_moderation'];
  1991. $context['sub_template'] = 'postmod_permissions';
  1992. $context['current_profile'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 1;
  1993. // Load all the permission profiles.
  1994. loadPermissionProfiles();
  1995. // Mappings, our key => array(can_do_moderated, can_do_all)
  1996. $mappings = array(
  1997. 'new_topic' => array('post_new', 'post_unapproved_topics'),
  1998. 'replies_own' => array('post_reply_own', 'post_unapproved_replies_own'),
  1999. 'replies_any' => array('post_reply_any', 'post_unapproved_replies_any'),
  2000. 'attachment' => array('post_attachment', 'post_unapproved_attachments'),
  2001. );
  2002. call_integration_hook('integrate_post_moderation_mapping', array(&$mappings));
  2003. // Start this with the guests/members.
  2004. $context['profile_groups'] = array(
  2005. -1 => array(
  2006. 'id' => -1,
  2007. 'name' => $txt['membergroups_guests'],
  2008. 'color' => '',
  2009. 'new_topic' => 'disallow',
  2010. 'replies_own' => 'disallow',
  2011. 'replies_any' => 'disallow',
  2012. 'attachment' => 'disallow',
  2013. 'children' => array(),
  2014. ),
  2015. 0 => array(
  2016. 'id' => 0,
  2017. 'name' => $txt['membergroups_members'],
  2018. 'color' => '',
  2019. 'new_topic' => 'disallow',
  2020. 'replies_own' => 'disallow',
  2021. 'replies_any' => 'disallow',
  2022. 'attachment' => 'disallow',
  2023. 'children' => array(),
  2024. ),
  2025. );
  2026. // Load the groups.
  2027. $request = $smcFunc['db_query']('', '
  2028. SELECT id_group, group_name, online_color, id_parent
  2029. FROM {db_prefix}membergroups
  2030. WHERE id_group != {int:admin_group}
  2031. ' . (empty($modSettings['permission_enable_postgroups']) ? ' AND min_posts = {int:min_posts}' : '') . '
  2032. ORDER BY id_parent ASC',
  2033. array(
  2034. 'admin_group' => 1,
  2035. 'min_posts' => -1,
  2036. )
  2037. );
  2038. while ($row = $smcFunc['db_fetch_assoc']($request))
  2039. {
  2040. if ($row['id_parent'] == -2)
  2041. {
  2042. $context['profile_groups'][$row['id_group']] = array(
  2043. 'id' => $row['id_group'],
  2044. 'name' => $row['group_name'],
  2045. 'color' => $row['online_color'],
  2046. 'new_topic' => 'disallow',
  2047. 'replies_own' => 'disallow',
  2048. 'replies_any' => 'disallow',
  2049. 'attachment' => 'disallow',
  2050. 'children' => array(),
  2051. );
  2052. }
  2053. elseif (isset($context['profile_groups'][$row['id_parent']]))
  2054. $context['profile_groups'][$row['id_parent']]['children'][] = $row['group_name'];
  2055. }
  2056. $smcFunc['db_free_result']($request);
  2057. // What are the permissions we are querying?
  2058. $all_permissions = array();
  2059. foreach ($mappings as $perm_set)
  2060. $all_permissions = array_merge($all_permissions, $perm_set);
  2061. // If we're saving the changes then do just that - save them.
  2062. if (!empty($_POST['save_changes']) && ($context['current_profile'] == 1 || $context['current_profile'] > 4))
  2063. {
  2064. validateToken('admin-mppm');
  2065. // First, are we saving a new value for enabled post moderation?
  2066. $new_setting = !empty($_POST['postmod_active']);
  2067. if ($new_setting != $modSettings['postmod_active'])
  2068. {
  2069. if ($new_setting)
  2070. {
  2071. // Turning it on. This seems easy enough.
  2072. updateSettings(array('postmod_active' => 1));
  2073. }
  2074. else
  2075. {
  2076. // Turning it off. Not so straightforward. We have to turn off warnings to moderation level, and make everything approved.
  2077. updateSettings(array(
  2078. 'postmod_active' => 0,
  2079. 'warning_moderate' => 0,
  2080. ));
  2081. require_once($sourcedir . '/PostModeration.php');
  2082. approveAllData();
  2083. }
  2084. }
  2085. elseif ($modSettings['postmod_active'])
  2086. {
  2087. // We're not saving a new setting - and if it's still enabled we have more work to do.
  2088. // Start by deleting all the permissions relevant.
  2089. $smcFunc['db_query']('', '
  2090. DELETE FROM {db_prefix}board_permissions
  2091. WHERE id_profile = {int:current_profile}
  2092. AND permission IN ({array_string:permissions})
  2093. AND id_group IN ({array_int:profile_group_list})',
  2094. array(
  2095. 'profile_group_list' => array_keys($context['profile_groups']),
  2096. 'current_profile' => $context['current_profile'],
  2097. 'permissions' => $all_permissions,
  2098. )
  2099. );
  2100. // Do it group by group.
  2101. $new_permissions = array();
  2102. foreach ($context['profile_groups'] as $id => $group)
  2103. {
  2104. foreach ($mappings as $index => $data)
  2105. {
  2106. if (isset($_POST[$index][$group['id']]))
  2107. {
  2108. if ($_POST[$index][$group['id']] == 'allow')
  2109. {
  2110. // Give them both sets for fun.
  2111. $new_permissions[] = array($context['current_profile'], $group['id'], $data[0], 1);
  2112. $new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
  2113. }
  2114. elseif ($_POST[$index][$group['id']] == 'moderate')
  2115. $new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
  2116. }
  2117. }
  2118. }
  2119. // Insert new permissions.
  2120. if (!empty($new_permissions))
  2121. $smcFunc['db_insert']('',
  2122. '{db_prefix}board_permissions',
  2123. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  2124. $new_permissions,
  2125. array('id_profile', 'id_group', 'permission')
  2126. );
  2127. }
  2128. }
  2129. // Now get all the permissions!
  2130. $request = $smcFunc['db_query']('', '
  2131. SELECT id_group, permission, add_deny
  2132. FROM {db_prefix}board_permissions
  2133. WHERE id_profile = {int:current_profile}
  2134. AND permission IN ({array_string:permissions})
  2135. AND id_group IN ({array_int:profile_group_list})',
  2136. array(
  2137. 'profile_group_list' => array_keys($context['profile_groups']),
  2138. 'current_profile' => $context['current_profile'],
  2139. 'permissions' => $all_permissions,
  2140. )
  2141. );
  2142. while ($row = $smcFunc['db_fetch_assoc']($request))
  2143. {
  2144. foreach ($mappings as $key => $data)
  2145. {
  2146. foreach ($data as $index => $perm)
  2147. {
  2148. if ($perm == $row['permission'])
  2149. {
  2150. // Only bother if it's not denied.
  2151. if ($row['add_deny'])
  2152. {
  2153. // Full allowance?
  2154. if ($index == 0)
  2155. $context['profile_groups'][$row['id_group']][$key] = 'allow';
  2156. // Otherwise only bother with moderate if not on allow.
  2157. elseif ($context['profile_groups'][$row['id_group']][$key] != 'allow')
  2158. $context['profile_groups'][$row['id_group']][$key] = 'moderate';
  2159. }
  2160. }
  2161. }
  2162. }
  2163. }
  2164. $smcFunc['db_free_result']($request);
  2165. createToken('admin-mppm');
  2166. }
  2167. ?>