ManagePermissions.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496
  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. // Are they toggling the view?
  615. if (isset($_GET['view']))
  616. {
  617. $context['admin_preferences']['pv'] = $_GET['view'] == 'classic' ? 'classic' : 'simple';
  618. // Update the users preferences.
  619. require_once($sourcedir . '/Subs-Admin.php');
  620. updateAdminPreferences();
  621. }
  622. $context['view_type'] = !empty($context['admin_preferences']['pv']) && $context['admin_preferences']['pv'] == 'classic' ? 'classic' : 'simple';
  623. // It's not likely you'd end up here with this setting disabled.
  624. if ($_GET['group'] == 1)
  625. redirectexit('action=admin;area=permissions');
  626. loadAllPermissions($context['view_type']);
  627. loadPermissionProfiles();
  628. if ($context['group']['id'] > 0)
  629. {
  630. $result = $smcFunc['db_query']('', '
  631. SELECT group_name, id_parent
  632. FROM {db_prefix}membergroups
  633. WHERE id_group = {int:current_group}
  634. LIMIT 1',
  635. array(
  636. 'current_group' => $context['group']['id'],
  637. )
  638. );
  639. list ($context['group']['name'], $parent) = $smcFunc['db_fetch_row']($result);
  640. $smcFunc['db_free_result']($result);
  641. // Cannot edit an inherited group!
  642. if ($parent != -2)
  643. fatal_lang_error('cannot_edit_permissions_inherited');
  644. }
  645. elseif ($context['group']['id'] == -1)
  646. $context['group']['name'] = $txt['membergroups_guests'];
  647. else
  648. $context['group']['name'] = $txt['membergroups_members'];
  649. $context['profile']['id'] = empty($_GET['pid']) ? 0 : (int) $_GET['pid'];
  650. // If this is a moderator and they are editing "no profile" then we only do boards.
  651. if ($context['group']['id'] == 3 && empty($context['profile']['id']))
  652. {
  653. // For sanity just check they have no general permissions.
  654. $smcFunc['db_query']('', '
  655. DELETE FROM {db_prefix}permissions
  656. WHERE id_group = {int:moderator_group}',
  657. array(
  658. 'moderator_group' => 3,
  659. )
  660. );
  661. $context['profile']['id'] = 1;
  662. }
  663. $context['permission_type'] = empty($context['profile']['id']) ? 'membergroup' : 'board';
  664. $context['profile']['can_modify'] = !$context['profile']['id'] || $context['profiles'][$context['profile']['id']]['can_modify'];
  665. // Set up things a little nicer for board related stuff...
  666. if ($context['permission_type'] == 'board')
  667. {
  668. $context['profile']['name'] = $context['profiles'][$context['profile']['id']]['name'];
  669. $context[$context['admin_menu_name']]['current_subsection'] = 'profiles';
  670. }
  671. // Fetch the current permissions.
  672. $permissions = array(
  673. 'membergroup' => array('allowed' => array(), 'denied' => array()),
  674. 'board' => array('allowed' => array(), 'denied' => array())
  675. );
  676. // General permissions?
  677. if ($context['permission_type'] == 'membergroup')
  678. {
  679. $result = $smcFunc['db_query']('', '
  680. SELECT permission, add_deny
  681. FROM {db_prefix}permissions
  682. WHERE id_group = {int:current_group}',
  683. array(
  684. 'current_group' => $_GET['group'],
  685. )
  686. );
  687. while ($row = $smcFunc['db_fetch_assoc']($result))
  688. $permissions['membergroup'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
  689. $smcFunc['db_free_result']($result);
  690. }
  691. // Fetch current board permissions...
  692. $result = $smcFunc['db_query']('', '
  693. SELECT permission, add_deny
  694. FROM {db_prefix}board_permissions
  695. WHERE id_group = {int:current_group}
  696. AND id_profile = {int:current_profile}',
  697. array(
  698. 'current_group' => $context['group']['id'],
  699. 'current_profile' => $context['permission_type'] == 'membergroup' ? 1 : $context['profile']['id'],
  700. )
  701. );
  702. while ($row = $smcFunc['db_fetch_assoc']($result))
  703. $permissions['board'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
  704. $smcFunc['db_free_result']($result);
  705. // Loop through each permission and set whether it's checked.
  706. foreach ($context['permissions'] as $permissionType => $tmp)
  707. {
  708. foreach ($tmp['columns'] as $position => $permissionGroups)
  709. {
  710. foreach ($permissionGroups as $permissionGroup => $permissionArray)
  711. {
  712. foreach ($permissionArray['permissions'] as $perm)
  713. {
  714. // Create a shortcut for the current permission.
  715. $curPerm = &$context['permissions'][$permissionType]['columns'][$position][$permissionGroup]['permissions'][$perm['id']];
  716. if ($tmp['view'] == 'classic')
  717. {
  718. if ($perm['has_own_any'])
  719. {
  720. $curPerm['any']['select'] = in_array($perm['id'] . '_any', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_any', $permissions[$permissionType]['denied']) ? 'denied' : 'off');
  721. $curPerm['own']['select'] = in_array($perm['id'] . '_own', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_own', $permissions[$permissionType]['denied']) ? 'denied' : 'off');
  722. }
  723. else
  724. $curPerm['select'] = in_array($perm['id'], $permissions[$permissionType]['denied']) ? 'denied' : (in_array($perm['id'], $permissions[$permissionType]['allowed']) ? 'on' : 'off');
  725. }
  726. else
  727. {
  728. $curPerm['select'] = in_array($perm['id'], $permissions[$permissionType]['denied']) ? 'denied' : (in_array($perm['id'], $permissions[$permissionType]['allowed']) ? 'on' : 'off');
  729. }
  730. }
  731. }
  732. }
  733. }
  734. $context['sub_template'] = 'modify_group';
  735. $context['page_title'] = $txt['permissions_modify_group'];
  736. createToken('admin-mp');
  737. }
  738. /**
  739. * This function actually saves modifications to a membergroup's board permissions.
  740. */
  741. function ModifyMembergroup2()
  742. {
  743. global $modSettings, $smcFunc, $context;
  744. checkSession();
  745. validateToken('admin-mp');
  746. loadIllegalPermissions();
  747. $_GET['group'] = (int) $_GET['group'];
  748. $_GET['pid'] = (int) $_GET['pid'];
  749. // Cannot modify predefined profiles.
  750. if ($_GET['pid'] > 1 && $_GET['pid'] < 5)
  751. fatal_lang_error('no_access', false);
  752. // Verify this isn't inherited.
  753. if ($_GET['group'] == -1 || $_GET['group'] == 0)
  754. $parent = -2;
  755. else
  756. {
  757. $result = $smcFunc['db_query']('', '
  758. SELECT id_parent
  759. FROM {db_prefix}membergroups
  760. WHERE id_group = {int:current_group}
  761. LIMIT 1',
  762. array(
  763. 'current_group' => $_GET['group'],
  764. )
  765. );
  766. list ($parent) = $smcFunc['db_fetch_row']($result);
  767. $smcFunc['db_free_result']($result);
  768. }
  769. if ($parent != -2)
  770. fatal_lang_error('cannot_edit_permissions_inherited');
  771. $givePerms = array('membergroup' => array(), 'board' => array());
  772. // Guest group, we need illegal, guest permissions.
  773. if ($_GET['group'] == -1)
  774. {
  775. loadIllegalGuestPermissions();
  776. $context['illegal_permissions'] = array_merge($context['illegal_permissions'], $context['non_guest_permissions']);
  777. }
  778. // Prepare all permissions that were set or denied for addition to the DB.
  779. if (isset($_POST['perm']) && is_array($_POST['perm']))
  780. {
  781. foreach ($_POST['perm'] as $perm_type => $perm_array)
  782. {
  783. if (is_array($perm_array))
  784. {
  785. foreach ($perm_array as $permission => $value)
  786. if ($value == 'on' || $value == 'deny')
  787. {
  788. // Don't allow people to escalate themselves!
  789. if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
  790. continue;
  791. $givePerms[$perm_type][] = array($_GET['group'], $permission, $value == 'deny' ? 0 : 1);
  792. }
  793. }
  794. }
  795. }
  796. // Insert the general permissions.
  797. if ($_GET['group'] != 3 && empty($_GET['pid']))
  798. {
  799. $smcFunc['db_query']('', '
  800. DELETE FROM {db_prefix}permissions
  801. WHERE id_group = {int:current_group}
  802. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  803. array(
  804. 'current_group' => $_GET['group'],
  805. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  806. )
  807. );
  808. if (!empty($givePerms['membergroup']))
  809. {
  810. $smcFunc['db_insert']('replace',
  811. '{db_prefix}permissions',
  812. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  813. $givePerms['membergroup'],
  814. array('id_group', 'permission')
  815. );
  816. }
  817. }
  818. // Insert the boardpermissions.
  819. $profileid = max(1, $_GET['pid']);
  820. $smcFunc['db_query']('', '
  821. DELETE FROM {db_prefix}board_permissions
  822. WHERE id_group = {int:current_group}
  823. AND id_profile = {int:current_profile}',
  824. array(
  825. 'current_group' => $_GET['group'],
  826. 'current_profile' => $profileid,
  827. )
  828. );
  829. if (!empty($givePerms['board']))
  830. {
  831. foreach ($givePerms['board'] as $k => $v)
  832. $givePerms['board'][$k][] = $profileid;
  833. $smcFunc['db_insert']('replace',
  834. '{db_prefix}board_permissions',
  835. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int', 'id_profile' => 'int'),
  836. $givePerms['board'],
  837. array('id_group', 'permission', 'id_profile')
  838. );
  839. }
  840. // Update any inherited permissions as required.
  841. updateChildPermissions($_GET['group'], $_GET['pid']);
  842. // Clear cached privs.
  843. updateSettings(array('settings_updated' => time()));
  844. redirectexit('action=admin;area=permissions;pid=' . $_GET['pid']);
  845. }
  846. /**
  847. * A screen to set some general settings for permissions.
  848. *
  849. * @param bool $return_config = false
  850. */
  851. function GeneralPermissionSettings($return_config = false)
  852. {
  853. global $context, $modSettings, $sourcedir, $txt, $scripturl, $smcFunc;
  854. // All the setting variables
  855. $config_vars = array(
  856. array('title', 'settings'),
  857. // Inline permissions.
  858. array('permissions', 'manage_permissions'),
  859. '',
  860. // A few useful settings
  861. array('check', 'permission_enable_deny', 0, $txt['permission_settings_enable_deny'], 'help' => 'permissions_deny'),
  862. array('check', 'permission_enable_postgroups', 0, $txt['permission_settings_enable_postgroups'], 'help' => 'permissions_postgroups'),
  863. );
  864. call_integration_hook('integrate_modify_permission_settings', array(&$config_vars));
  865. if ($return_config)
  866. return $config_vars;
  867. $context['page_title'] = $txt['permission_settings_title'];
  868. $context['sub_template'] = 'show_settings';
  869. // Needed for the inline permission functions, and the settings template.
  870. require_once($sourcedir . '/ManageServer.php');
  871. // Don't let guests have these permissions.
  872. $context['post_url'] = $scripturl . '?action=admin;area=permissions;save;sa=settings';
  873. $context['permissions_excluded'] = array(-1);
  874. // Saving the settings?
  875. if (isset($_GET['save']))
  876. {
  877. checkSession('post');
  878. call_integration_hook('integrate_save_permission_settings');
  879. saveDBSettings($config_vars);
  880. // Clear all deny permissions...if we want that.
  881. if (empty($modSettings['permission_enable_deny']))
  882. {
  883. $smcFunc['db_query']('', '
  884. DELETE FROM {db_prefix}permissions
  885. WHERE add_deny = {int:denied}',
  886. array(
  887. 'denied' => 0,
  888. )
  889. );
  890. $smcFunc['db_query']('', '
  891. DELETE FROM {db_prefix}board_permissions
  892. WHERE add_deny = {int:denied}',
  893. array(
  894. 'denied' => 0,
  895. )
  896. );
  897. }
  898. // Make sure there are no postgroup based permissions left.
  899. if (empty($modSettings['permission_enable_postgroups']))
  900. {
  901. // Get a list of postgroups.
  902. $post_groups = array();
  903. $request = $smcFunc['db_query']('', '
  904. SELECT id_group
  905. FROM {db_prefix}membergroups
  906. WHERE min_posts != {int:min_posts}',
  907. array(
  908. 'min_posts' => -1,
  909. )
  910. );
  911. while ($row = $smcFunc['db_fetch_assoc']($request))
  912. $post_groups[] = $row['id_group'];
  913. $smcFunc['db_free_result']($request);
  914. // Remove'em.
  915. $smcFunc['db_query']('', '
  916. DELETE FROM {db_prefix}permissions
  917. WHERE id_group IN ({array_int:post_group_list})',
  918. array(
  919. 'post_group_list' => $post_groups,
  920. )
  921. );
  922. $smcFunc['db_query']('', '
  923. DELETE FROM {db_prefix}board_permissions
  924. WHERE id_group IN ({array_int:post_group_list})',
  925. array(
  926. 'post_group_list' => $post_groups,
  927. )
  928. );
  929. $smcFunc['db_query']('', '
  930. UPDATE {db_prefix}membergroups
  931. SET id_parent = {int:not_inherited}
  932. WHERE id_parent IN ({array_int:post_group_list})',
  933. array(
  934. 'post_group_list' => $post_groups,
  935. 'not_inherited' => -2,
  936. )
  937. );
  938. }
  939. $_SESSION['adm-save'] = true;
  940. redirectexit('action=admin;area=permissions;sa=settings');
  941. }
  942. // We need this for the in-line permissions
  943. createToken('admin-mp');
  944. prepareDBSettingContext($config_vars);
  945. }
  946. /**
  947. * Set the permission level for a specific profile, group, or group for a profile.
  948. * @internal
  949. *
  950. * @param string $level
  951. * @param int $group
  952. * @param mixed $profile = null, int expected
  953. */
  954. function setPermissionLevel($level, $group, $profile = 'null')
  955. {
  956. global $smcFunc, $context;
  957. loadIllegalPermissions();
  958. loadIllegalGuestPermissions();
  959. // Levels by group... restrict, standard, moderator, maintenance.
  960. $groupLevels = array(
  961. 'board' => array('inherit' => array()),
  962. 'group' => array('inherit' => array())
  963. );
  964. // Levels by board... standard, publish, free.
  965. $boardLevels = array('inherit' => array());
  966. // Restrictive - ie. guests.
  967. $groupLevels['global']['restrict'] = array(
  968. 'search_posts',
  969. 'calendar_view',
  970. 'view_stats',
  971. 'who_view',
  972. 'profile_identity_own',
  973. );
  974. $groupLevels['board']['restrict'] = array(
  975. 'poll_view',
  976. 'post_new',
  977. 'post_reply_own',
  978. 'post_reply_any',
  979. 'delete_own',
  980. 'modify_own',
  981. 'report_any',
  982. 'send_topic',
  983. );
  984. // Standard - ie. members. They can do anything Restrictive can.
  985. $groupLevels['global']['standard'] = array_merge($groupLevels['global']['restrict'], array(
  986. 'view_mlist',
  987. 'karma_edit',
  988. 'pm_read',
  989. 'pm_send',
  990. 'send_email_to_members',
  991. 'profile_view',
  992. 'profile_extra_own',
  993. 'profile_signature_own',
  994. 'profile_forum_own',
  995. 'profile_other_own',
  996. 'profile_password_own',
  997. 'profile_server_avatar',
  998. 'profile_displayed_name',
  999. 'profile_upload_avatar',
  1000. 'profile_remote_avatar',
  1001. 'profile_remove_own',
  1002. ));
  1003. $groupLevels['board']['standard'] = array_merge($groupLevels['board']['restrict'], array(
  1004. 'poll_vote',
  1005. 'poll_edit_own',
  1006. 'poll_post',
  1007. 'poll_add_own',
  1008. 'post_attachment',
  1009. 'lock_own',
  1010. 'remove_own',
  1011. 'view_attachments',
  1012. ));
  1013. // Moderator - ie. moderators :P. They can do what standard can, and more.
  1014. $groupLevels['global']['moderator'] = array_merge($groupLevels['global']['standard'], array(
  1015. 'calendar_post',
  1016. 'calendar_edit_own',
  1017. 'access_mod_center',
  1018. 'issue_warning',
  1019. ));
  1020. $groupLevels['board']['moderator'] = array_merge($groupLevels['board']['standard'], array(
  1021. 'make_sticky',
  1022. 'poll_edit_any',
  1023. 'delete_any',
  1024. 'modify_any',
  1025. 'lock_any',
  1026. 'remove_any',
  1027. 'move_any',
  1028. 'merge_any',
  1029. 'split_any',
  1030. 'poll_lock_any',
  1031. 'poll_remove_any',
  1032. 'poll_add_any',
  1033. 'approve_posts',
  1034. ));
  1035. // Maintenance - wannabe admins. They can do almost everything.
  1036. $groupLevels['global']['maintenance'] = array_merge($groupLevels['global']['moderator'], array(
  1037. 'manage_attachments',
  1038. 'manage_smileys',
  1039. 'manage_boards',
  1040. 'moderate_forum',
  1041. 'manage_membergroups',
  1042. 'manage_bans',
  1043. 'admin_forum',
  1044. 'manage_permissions',
  1045. 'edit_news',
  1046. 'calendar_edit_any',
  1047. 'profile_identity_any',
  1048. 'profile_extra_any',
  1049. 'profile_signature_any',
  1050. 'profile_forum_own',
  1051. 'profile_other_any',
  1052. 'profile_displayed_name_any',
  1053. 'profile_password_any',
  1054. 'profile_title_any',
  1055. ));
  1056. $groupLevels['board']['maintenance'] = array_merge($groupLevels['board']['moderator'], array(
  1057. ));
  1058. // Standard - nothing above the group permissions. (this SHOULD be empty.)
  1059. $boardLevels['standard'] = array(
  1060. );
  1061. // Locked - just that, you can't post here.
  1062. $boardLevels['locked'] = array(
  1063. 'poll_view',
  1064. 'report_any',
  1065. 'send_topic',
  1066. 'view_attachments',
  1067. );
  1068. // Publisher - just a little more...
  1069. $boardLevels['publish'] = array_merge($boardLevels['locked'], array(
  1070. 'post_new',
  1071. 'post_reply_own',
  1072. 'post_reply_any',
  1073. 'delete_own',
  1074. 'modify_own',
  1075. 'delete_replies',
  1076. 'modify_replies',
  1077. 'poll_vote',
  1078. 'poll_edit_own',
  1079. 'poll_post',
  1080. 'poll_add_own',
  1081. 'poll_remove_own',
  1082. 'post_attachment',
  1083. 'lock_own',
  1084. 'remove_own',
  1085. ));
  1086. // Free for All - Scary. Just scary.
  1087. $boardLevels['free'] = array_merge($boardLevels['publish'], array(
  1088. 'poll_lock_any',
  1089. 'poll_edit_any',
  1090. 'poll_add_any',
  1091. 'poll_remove_any',
  1092. 'make_sticky',
  1093. 'lock_any',
  1094. 'remove_any',
  1095. 'delete_any',
  1096. 'split_any',
  1097. 'merge_any',
  1098. 'modify_any',
  1099. 'approve_posts',
  1100. ));
  1101. // Make sure we're not granting someone too many permissions!
  1102. foreach ($groupLevels['global'][$level] as $k => $permission)
  1103. {
  1104. if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
  1105. unset($groupLevels['global'][$level][$k]);
  1106. if ($group == -1 && in_array($permission, $context['non_guest_permissions']))
  1107. unset($groupLevels['global'][$level][$k]);
  1108. }
  1109. if ($group == -1)
  1110. foreach ($groupLevels['board'][$level] as $k => $permission)
  1111. if (in_array($permission, $context['non_guest_permissions']))
  1112. unset($groupLevels['board'][$level][$k]);
  1113. // Reset all cached permissions.
  1114. updateSettings(array('settings_updated' => time()));
  1115. // Setting group permissions.
  1116. if ($profile === 'null' && $group !== 'null')
  1117. {
  1118. $group = (int) $group;
  1119. if (empty($groupLevels['global'][$level]))
  1120. return;
  1121. $smcFunc['db_query']('', '
  1122. DELETE FROM {db_prefix}permissions
  1123. WHERE id_group = {int:current_group}
  1124. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  1125. array(
  1126. 'current_group' => $group,
  1127. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  1128. )
  1129. );
  1130. $smcFunc['db_query']('', '
  1131. DELETE FROM {db_prefix}board_permissions
  1132. WHERE id_group = {int:current_group}
  1133. AND id_profile = {int:default_profile}',
  1134. array(
  1135. 'current_group' => $group,
  1136. 'default_profile' => 1,
  1137. )
  1138. );
  1139. $groupInserts = array();
  1140. foreach ($groupLevels['global'][$level] as $permission)
  1141. $groupInserts[] = array($group, $permission);
  1142. $smcFunc['db_insert']('insert',
  1143. '{db_prefix}permissions',
  1144. array('id_group' => 'int', 'permission' => 'string'),
  1145. $groupInserts,
  1146. array('id_group')
  1147. );
  1148. $boardInserts = array();
  1149. foreach ($groupLevels['board'][$level] as $permission)
  1150. $boardInserts[] = array(1, $group, $permission);
  1151. $smcFunc['db_insert']('insert',
  1152. '{db_prefix}board_permissions',
  1153. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1154. $boardInserts,
  1155. array('id_profile', 'id_group')
  1156. );
  1157. }
  1158. // Setting profile permissions for a specific group.
  1159. elseif ($profile !== 'null' && $group !== 'null' && ($profile == 1 || $profile > 4))
  1160. {
  1161. $group = (int) $group;
  1162. $profile = (int) $profile;
  1163. if (!empty($groupLevels['global'][$level]))
  1164. {
  1165. $smcFunc['db_query']('', '
  1166. DELETE FROM {db_prefix}board_permissions
  1167. WHERE id_group = {int:current_group}
  1168. AND id_profile = {int:current_profile}',
  1169. array(
  1170. 'current_group' => $group,
  1171. 'current_profile' => $profile,
  1172. )
  1173. );
  1174. }
  1175. if (!empty($groupLevels['board'][$level]))
  1176. {
  1177. $boardInserts = array();
  1178. foreach ($groupLevels['board'][$level] as $permission)
  1179. $boardInserts[] = array($profile, $group, $permission);
  1180. $smcFunc['db_insert']('insert',
  1181. '{db_prefix}board_permissions',
  1182. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1183. $boardInserts,
  1184. array('id_profile', 'id_group')
  1185. );
  1186. }
  1187. }
  1188. // Setting profile permissions for all groups.
  1189. elseif ($profile !== 'null' && $group === 'null' && ($profile == 1 || $profile > 4))
  1190. {
  1191. $profile = (int) $profile;
  1192. $smcFunc['db_query']('', '
  1193. DELETE FROM {db_prefix}board_permissions
  1194. WHERE id_profile = {int:current_profile}',
  1195. array(
  1196. 'current_profile' => $profile,
  1197. )
  1198. );
  1199. if (empty($boardLevels[$level]))
  1200. return;
  1201. // Get all the groups...
  1202. $query = $smcFunc['db_query']('', '
  1203. SELECT id_group
  1204. FROM {db_prefix}membergroups
  1205. WHERE id_group > {int:moderator_group}
  1206. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  1207. array(
  1208. 'moderator_group' => 3,
  1209. 'newbie_group' => 4,
  1210. )
  1211. );
  1212. while ($row = $smcFunc['db_fetch_row']($query))
  1213. {
  1214. $group = $row[0];
  1215. $boardInserts = array();
  1216. foreach ($boardLevels[$level] as $permission)
  1217. $boardInserts[] = array($profile, $group, $permission);
  1218. $smcFunc['db_insert']('insert',
  1219. '{db_prefix}board_permissions',
  1220. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1221. $boardInserts,
  1222. array('id_profile', 'id_group')
  1223. );
  1224. }
  1225. $smcFunc['db_free_result']($query);
  1226. // Add permissions for ungrouped members.
  1227. $boardInserts = array();
  1228. foreach ($boardLevels[$level] as $permission)
  1229. $boardInserts[] = array($profile, 0, $permission);
  1230. $smcFunc['db_insert']('insert',
  1231. '{db_prefix}board_permissions',
  1232. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
  1233. $boardInserts,
  1234. array('id_profile', 'id_group')
  1235. );
  1236. }
  1237. // $profile and $group are both null!
  1238. else
  1239. fatal_lang_error('no_access', false);
  1240. }
  1241. /**
  1242. * Load permissions into $context['permissions'].
  1243. * @internal
  1244. *
  1245. * @param string $loadType options: 'classic' or 'simple'
  1246. */
  1247. function loadAllPermissions($loadType = 'classic')
  1248. {
  1249. global $context, $txt, $modSettings;
  1250. // List of all the groups dependant on the currently selected view - for the order so it looks pretty, yea?
  1251. // 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.
  1252. $permissionGroups = array(
  1253. 'membergroup' => array(
  1254. 'simple' => array(
  1255. 'view_basic_info',
  1256. 'use_pm_system',
  1257. 'post_calendar',
  1258. 'edit_profile',
  1259. 'delete_account',
  1260. 'use_avatar',
  1261. 'moderate_general',
  1262. 'administrate',
  1263. ),
  1264. 'classic' => array(
  1265. 'general',
  1266. 'pm',
  1267. 'calendar',
  1268. 'maintenance',
  1269. 'member_admin',
  1270. 'profile',
  1271. ),
  1272. ),
  1273. 'board' => array(
  1274. 'simple' => array(
  1275. 'make_posts',
  1276. 'make_unapproved_posts',
  1277. 'post_polls',
  1278. 'participate',
  1279. 'modify',
  1280. 'notification',
  1281. 'attach',
  1282. 'moderate',
  1283. ),
  1284. 'classic' => array(
  1285. 'general_board',
  1286. 'topic',
  1287. 'post',
  1288. 'poll',
  1289. 'notification',
  1290. 'attachment',
  1291. ),
  1292. ),
  1293. );
  1294. /* The format of this list is as follows:
  1295. 'membergroup' => array(
  1296. 'permissions_inside' => array(has_multiple_options, classic_view_group, simple_view_group(_own)*, simple_view_group_any*),
  1297. ),
  1298. 'board' => array(
  1299. 'permissions_inside' => array(has_multiple_options, classic_view_group, simple_view_group(_own)*, simple_view_group_any*),
  1300. );
  1301. */
  1302. $permissionList = array(
  1303. 'membergroup' => array(
  1304. 'view_stats' => array(false, 'general', 'view_basic_info'),
  1305. 'view_mlist' => array(false, 'general', 'view_basic_info'),
  1306. 'who_view' => array(false, 'general', 'view_basic_info'),
  1307. 'search_posts' => array(false, 'general', 'view_basic_info'),
  1308. 'karma_edit' => array(false, 'general', 'moderate_general'),
  1309. 'pm_read' => array(false, 'pm', 'use_pm_system'),
  1310. 'pm_send' => array(false, 'pm', 'use_pm_system'),
  1311. 'pm_draft' => array(false, 'pm', 'use_pm_system'),
  1312. 'pm_autosave_draft' => array(false, 'pm', 'use_pm_system'),
  1313. 'send_email_to_members' => array(false, 'pm', 'use_pm_system'),
  1314. 'calendar_view' => array(false, 'calendar', 'view_basic_info'),
  1315. 'calendar_post' => array(false, 'calendar', 'post_calendar'),
  1316. 'calendar_edit' => array(true, 'calendar', 'post_calendar', 'moderate_general'),
  1317. 'admin_forum' => array(false, 'maintenance', 'administrate'),
  1318. 'manage_boards' => array(false, 'maintenance', 'administrate'),
  1319. 'manage_attachments' => array(false, 'maintenance', 'administrate'),
  1320. 'manage_smileys' => array(false, 'maintenance', 'administrate'),
  1321. 'edit_news' => array(false, 'maintenance', 'administrate'),
  1322. 'access_mod_center' => array(false, 'maintenance', 'moderate_general'),
  1323. 'moderate_forum' => array(false, 'member_admin', 'moderate_general'),
  1324. 'manage_membergroups' => array(false, 'member_admin', 'administrate'),
  1325. 'manage_permissions' => array(false, 'member_admin', 'administrate'),
  1326. 'manage_bans' => array(false, 'member_admin', 'administrate'),
  1327. 'send_mail' => array(false, 'member_admin', 'administrate'),
  1328. 'issue_warning' => array(false, 'member_admin', 'moderate_general'),
  1329. 'profile_view' => array(false, 'profile', 'view_basic_info'),
  1330. 'profile_identity' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1331. 'profile_forum' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1332. 'profile_password' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1333. 'profile_extra' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1334. 'profile_signature' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1335. 'profile_other' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1336. 'profile_title' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1337. 'profile_displayed_name' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1338. 'profile_blurb' => array(true, 'profile', 'edit_profile', 'moderate_general'),
  1339. 'profile_remove' => array(true, 'profile', 'delete_account', 'moderate_general'),
  1340. 'profile_server_avatar' => array(false, 'profile', 'use_avatar'),
  1341. 'profile_upload_avatar' => array(false, 'profile', 'use_avatar'),
  1342. 'profile_remote_avatar' => array(false, 'profile', 'use_avatar'),
  1343. ),
  1344. 'board' => array(
  1345. 'moderate_board' => array(false, 'general_board', 'moderate'),
  1346. 'approve_posts' => array(false, 'general_board', 'moderate'),
  1347. 'post_new' => array(false, 'topic', 'make_posts'),
  1348. 'post_draft' => array(false, 'topic', 'make_posts'),
  1349. 'post_autosave_draft' => array(false, 'topic', 'make_posts'),
  1350. 'post_unapproved_topics' => array(false, 'topic', 'make_unapproved_posts'),
  1351. 'post_unapproved_replies' => array(true, 'topic', 'make_unapproved_posts', 'make_unapproved_posts'),
  1352. 'post_reply' => array(true, 'topic', 'make_posts', 'make_posts'),
  1353. 'merge_any' => array(false, 'topic', 'moderate'),
  1354. 'split_any' => array(false, 'topic', 'moderate'),
  1355. 'send_topic' => array(false, 'topic', 'moderate'),
  1356. 'make_sticky' => array(false, 'topic', 'moderate'),
  1357. 'move' => array(true, 'topic', 'moderate', 'moderate'),
  1358. 'lock' => array(true, 'topic', 'moderate', 'moderate'),
  1359. 'remove' => array(true, 'topic', 'modify', 'moderate'),
  1360. 'modify_replies' => array(false, 'topic', 'moderate'),
  1361. 'delete_replies' => array(false, 'topic', 'moderate'),
  1362. 'announce_topic' => array(false, 'topic', 'moderate'),
  1363. 'delete' => array(true, 'post', 'modify', 'moderate'),
  1364. 'modify' => array(true, 'post', 'modify', 'moderate'),
  1365. 'report_any' => array(false, 'post', 'participate'),
  1366. 'poll_view' => array(false, 'poll', 'participate'),
  1367. 'poll_vote' => array(false, 'poll', 'participate'),
  1368. 'poll_post' => array(false, 'poll', 'post_polls'),
  1369. 'poll_add' => array(true, 'poll', 'post_polls', 'moderate'),
  1370. 'poll_edit' => array(true, 'poll', 'modify', 'moderate'),
  1371. 'poll_lock' => array(true, 'poll', 'moderate', 'moderate'),
  1372. 'poll_remove' => array(true, 'poll', 'modify', 'moderate'),
  1373. 'view_attachments' => array(false, 'attachment', 'participate'),
  1374. 'post_unapproved_attachments' => array(false, 'attachment', 'make_unapproved_posts'),
  1375. 'post_attachment' => array(false, 'attachment', 'attach'),
  1376. ),
  1377. );
  1378. // All permission groups that will be shown in the left column on classic view.
  1379. $leftPermissionGroups = array(
  1380. 'general',
  1381. 'calendar',
  1382. 'maintenance',
  1383. 'member_admin',
  1384. 'topic',
  1385. 'post',
  1386. );
  1387. // We need to know what permissions we can't give to guests.
  1388. loadIllegalGuestPermissions();
  1389. // Some permissions are hidden if features are off.
  1390. $hiddenPermissions = array();
  1391. $relabelPermissions = array(); // Permissions to apply a different label to.
  1392. $relabelGroups = array(); // As above but for groups.
  1393. if (empty($modSettings['cal_enabled']))
  1394. {
  1395. $hiddenPermissions[] = 'calendar_view';
  1396. $hiddenPermissions[] = 'calendar_post';
  1397. $hiddenPermissions[] = 'calendar_edit';
  1398. }
  1399. if ($modSettings['warning_settings'][0] == 0)
  1400. $hiddenPermissions[] = 'issue_warning';
  1401. if (empty($modSettings['karmaMode']))
  1402. $hiddenPermissions[] = 'karma_edit';
  1403. // Post moderation?
  1404. if (!$modSettings['postmod_active'])
  1405. {
  1406. $hiddenPermissions[] = 'approve_posts';
  1407. $hiddenPermissions[] = 'post_unapproved_topics';
  1408. $hiddenPermissions[] = 'post_unapproved_replies';
  1409. $hiddenPermissions[] = 'post_unapproved_attachments';
  1410. }
  1411. // If we show them on classic view we change the name.
  1412. else
  1413. {
  1414. // Relabel the topics permissions
  1415. $relabelPermissions['post_new'] = 'auto_approve_topics';
  1416. // Relabel the reply permissions
  1417. $relabelPermissions['post_reply'] = 'auto_approve_replies';
  1418. // Relabel the attachment permissions
  1419. $relabelPermissions['post_attachment'] = 'auto_approve_attachments';
  1420. }
  1421. // Are attachments enabled?
  1422. if (empty($modSettings['attachmentEnable']))
  1423. {
  1424. $hiddenPermissions[] = 'manage_attachments';
  1425. $hiddenPermissions[] = 'view_attachments';
  1426. $hiddenPermissions[] = 'post_unapproved_attachments';
  1427. $hiddenPermissions[] = 'post_attachment';
  1428. }
  1429. // Provide a practical way to modify permissions.
  1430. call_integration_hook('integrate_load_permissions', array(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions));
  1431. $context['permissions'] = array();
  1432. $context['hidden_permissions'] = array();
  1433. foreach ($permissionList as $permissionType => $permissionList)
  1434. {
  1435. $context['permissions'][$permissionType] = array(
  1436. 'id' => $permissionType,
  1437. 'view' => $loadType,
  1438. 'columns' => array()
  1439. );
  1440. foreach ($permissionList as $permission => $permissionArray)
  1441. {
  1442. // If this is a guest permission we don't do it if it's the guest group.
  1443. if (isset($context['group']['id']) && $context['group']['id'] == -1 && in_array($permission, $context['non_guest_permissions']))
  1444. continue;
  1445. // What groups will this permission be in?
  1446. $own_group = $permissionArray[($loadType == 'classic' ? 1 : 2)];
  1447. $any_group = $loadType == 'simple' && !empty($permissionArray[3]) ? $permissionArray[3] : ($loadType == 'simple' && $permissionArray[0] ? $permissionArray[2] : '');
  1448. // First, Do these groups actually exist - if not add them.
  1449. if (!isset($permissionGroups[$permissionType][$loadType][$own_group]))
  1450. $permissionGroups[$permissionType][$loadType][$own_group] = true;
  1451. if (!empty($any_group) && !isset($permissionGroups[$permissionType][$loadType][$any_group]))
  1452. $permissionGroups[$permissionType][$loadType][$any_group] = true;
  1453. // What column should this be located into?
  1454. $position = $loadType == 'classic' && !in_array($own_group, $leftPermissionGroups) ? 1 : 0;
  1455. // If the groups have not yet been created be sure to create them.
  1456. $bothGroups = array('own' => $own_group);
  1457. $bothGroups = array();
  1458. // For guests, just reset the array.
  1459. if (!isset($context['group']['id']) || !($context['group']['id'] == -1 && $any_group))
  1460. $bothGroups['own'] = $own_group;
  1461. if ($any_group)
  1462. {
  1463. $bothGroups['any'] = $any_group;
  1464. }
  1465. foreach ($bothGroups as $group)
  1466. if (!isset($context['permissions'][$permissionType]['columns'][$position][$group]))
  1467. $context['permissions'][$permissionType]['columns'][$position][$group] = array(
  1468. 'type' => $permissionType,
  1469. 'id' => $group,
  1470. 'name' => $loadType == 'simple' ? (isset($txt['permissiongroup_simple_' . $group]) ? $txt['permissiongroup_simple_' . $group] : '') : $txt['permissiongroup_' . $group],
  1471. 'icon' => isset($txt['permissionicon_' . $group]) ? $txt['permissionicon_' . $group] : $txt['permissionicon'],
  1472. 'help' => isset($txt['permissionhelp_' . $group]) ? $txt['permissionhelp_' . $group] : '',
  1473. 'hidden' => false,
  1474. 'permissions' => array()
  1475. );
  1476. // This is where we set up the permission dependant on the view.
  1477. if ($loadType == 'classic')
  1478. {
  1479. $context['permissions'][$permissionType]['columns'][$position][$own_group]['permissions'][$permission] = array(
  1480. 'id' => $permission,
  1481. 'name' => !isset($relabelPermissions[$permission]) ? $txt['permissionname_' . $permission] : $txt[$relabelPermissions[$permission]],
  1482. 'show_help' => isset($txt['permissionhelp_' . $permission]),
  1483. 'note' => isset($txt['permissionnote_' . $permission]) ? $txt['permissionnote_' . $permission] : '',
  1484. 'has_own_any' => $permissionArray[0],
  1485. 'own' => array(
  1486. 'id' => $permission . '_own',
  1487. 'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_own'] : ''
  1488. ),
  1489. 'any' => array(
  1490. 'id' => $permission . '_any',
  1491. 'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_any'] : ''
  1492. ),
  1493. 'hidden' => in_array($permission, $hiddenPermissions),
  1494. );
  1495. }
  1496. else
  1497. {
  1498. foreach ($bothGroups as $group_type => $group)
  1499. {
  1500. $context['permissions'][$permissionType]['columns'][$position][$group]['permissions'][$permission . ($permissionArray[0] ? '_' . $group_type : '')] = array(
  1501. 'id' => $permission . ($permissionArray[0] ? '_' . $group_type : ''),
  1502. 'name' => isset($txt['permissionname_simple_' . $permission . ($permissionArray[0] ? '_' . $group_type : '')]) ? $txt['permissionname_simple_' . $permission . ($permissionArray[0] ? '_' . $group_type : '')] : $txt['permissionname_' . $permission],
  1503. 'help_index' => isset($txt['permissionhelp_' . $permission]) ? 'permissionhelp_' . $permission : '',
  1504. 'hidden' => in_array($permission, $hiddenPermissions),
  1505. );
  1506. }
  1507. }
  1508. if (in_array($permission, $hiddenPermissions))
  1509. {
  1510. if ($permissionArray[0])
  1511. {
  1512. $context['hidden_permissions'][] = $permission . '_own';
  1513. $context['hidden_permissions'][] = $permission . '_any';
  1514. }
  1515. else
  1516. $context['hidden_permissions'][] = $permission;
  1517. }
  1518. }
  1519. ksort($context['permissions'][$permissionType]['columns']);
  1520. // Check we don't leave any empty groups - and mark hidden ones as such.
  1521. foreach ($context['permissions'][$permissionType]['columns'] as $column => $groups)
  1522. foreach ($groups as $id => $group)
  1523. {
  1524. if (empty($group['permissions']))
  1525. unset($context['permissions'][$permissionType]['columns'][$column][$id]);
  1526. else
  1527. {
  1528. $foundNonHidden = false;
  1529. foreach ($group['permissions'] as $permission)
  1530. if (empty($permission['hidden']))
  1531. $foundNonHidden = true;
  1532. if (!$foundNonHidden)
  1533. $context['permissions'][$permissionType]['columns'][$column][$id]['hidden'] = true;
  1534. }
  1535. }
  1536. }
  1537. }
  1538. /**
  1539. * Initialize a form with inline permissions settings.
  1540. * It loads a context variables for each permission.
  1541. * This function is used by several settings screens to set specific permissions.
  1542. * @internal
  1543. *
  1544. * @param array $permissions
  1545. * @param array $excluded_groups = array()
  1546. *
  1547. * @uses ManagePermissions language
  1548. * @uses ManagePermissions template.
  1549. */
  1550. function init_inline_permissions($permissions, $excluded_groups = array())
  1551. {
  1552. global $context, $txt, $modSettings, $smcFunc;
  1553. loadLanguage('ManagePermissions');
  1554. loadTemplate('ManagePermissions');
  1555. $context['can_change_permissions'] = allowedTo('manage_permissions');
  1556. // Nothing to initialize here.
  1557. if (!$context['can_change_permissions'])
  1558. return;
  1559. // Load the permission settings for guests
  1560. foreach ($permissions as $permission)
  1561. $context[$permission] = array(
  1562. -1 => array(
  1563. 'id' => -1,
  1564. 'name' => $txt['membergroups_guests'],
  1565. 'is_postgroup' => false,
  1566. 'status' => 'off',
  1567. ),
  1568. 0 => array(
  1569. 'id' => 0,
  1570. 'name' => $txt['membergroups_members'],
  1571. 'is_postgroup' => false,
  1572. 'status' => 'off',
  1573. ),
  1574. );
  1575. $request = $smcFunc['db_query']('', '
  1576. SELECT id_group, CASE WHEN add_deny = {int:denied} THEN {string:deny} ELSE {string:on} END AS status, permission
  1577. FROM {db_prefix}permissions
  1578. WHERE id_group IN (-1, 0)
  1579. AND permission IN ({array_string:permissions})',
  1580. array(
  1581. 'denied' => 0,
  1582. 'permissions' => $permissions,
  1583. 'deny' => 'deny',
  1584. 'on' => 'on',
  1585. )
  1586. );
  1587. while ($row = $smcFunc['db_fetch_assoc']($request))
  1588. $context[$row['permission']][$row['id_group']]['status'] = $row['status'];
  1589. $smcFunc['db_free_result']($request);
  1590. $request = $smcFunc['db_query']('', '
  1591. SELECT mg.id_group, mg.group_name, mg.min_posts, IFNULL(p.add_deny, -1) AS status, p.permission
  1592. FROM {db_prefix}membergroups AS mg
  1593. LEFT JOIN {db_prefix}permissions AS p ON (p.id_group = mg.id_group AND p.permission IN ({array_string:permissions}))
  1594. WHERE mg.id_group NOT IN (1, 3)
  1595. AND mg.id_parent = {int:not_inherited}' . (empty($modSettings['permission_enable_postgroups']) ? '
  1596. AND mg.min_posts = {int:min_posts}' : '') . '
  1597. ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
  1598. array(
  1599. 'not_inherited' => -2,
  1600. 'min_posts' => -1,
  1601. 'newbie_group' => 4,
  1602. 'permissions' => $permissions,
  1603. )
  1604. );
  1605. while ($row = $smcFunc['db_fetch_assoc']($request))
  1606. {
  1607. // Initialize each permission as being 'off' until proven otherwise.
  1608. foreach ($permissions as $permission)
  1609. if (!isset($context[$permission][$row['id_group']]))
  1610. $context[$permission][$row['id_group']] = array(
  1611. 'id' => $row['id_group'],
  1612. 'name' => $row['group_name'],
  1613. 'is_postgroup' => $row['min_posts'] != -1,
  1614. 'status' => 'off',
  1615. );
  1616. $context[$row['permission']][$row['id_group']]['status'] = empty($row['status']) ? 'deny' : ($row['status'] == 1 ? 'on' : 'off');
  1617. }
  1618. $smcFunc['db_free_result']($request);
  1619. // Some permissions cannot be given to certain groups. Remove the groups.
  1620. foreach ($excluded_groups as $group)
  1621. {
  1622. foreach ($permissions as $permission)
  1623. {
  1624. if (isset($context[$permission][$group]))
  1625. unset($context[$permission][$group]);
  1626. }
  1627. }
  1628. }
  1629. /**
  1630. * Show a collapsible box to set a specific permission.
  1631. * The function is called by templates to show a list of permissions settings.
  1632. * Calls the template function template_inline_permissions().
  1633. *
  1634. * @param string $permission
  1635. */
  1636. function theme_inline_permissions($permission)
  1637. {
  1638. global $context;
  1639. $context['current_permission'] = $permission;
  1640. $context['member_groups'] = $context[$permission];
  1641. template_inline_permissions();
  1642. }
  1643. /**
  1644. * Save the permissions of a form containing inline permissions.
  1645. * @internal
  1646. *
  1647. * @param array $permissions
  1648. */
  1649. function save_inline_permissions($permissions)
  1650. {
  1651. global $context, $smcFunc;
  1652. // No permissions? Not a great deal to do here.
  1653. if (!allowedTo('manage_permissions'))
  1654. return;
  1655. // Almighty session check, verify our ways.
  1656. checkSession();
  1657. validateToken('admin-mp');
  1658. // Check they can't do certain things.
  1659. loadIllegalPermissions();
  1660. $insertRows = array();
  1661. foreach ($permissions as $permission)
  1662. {
  1663. if (!isset($_POST[$permission]))
  1664. continue;
  1665. foreach ($_POST[$permission] as $id_group => $value)
  1666. {
  1667. if (in_array($value, array('on', 'deny')) && (empty($context['illegal_permissions']) || !in_array($permission, $context['illegal_permissions'])))
  1668. $insertRows[] = array((int) $id_group, $permission, $value == 'on' ? 1 : 0);
  1669. }
  1670. }
  1671. // Remove the old permissions...
  1672. $smcFunc['db_query']('', '
  1673. DELETE FROM {db_prefix}permissions
  1674. WHERE permission IN ({array_string:permissions})
  1675. ' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
  1676. array(
  1677. 'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
  1678. 'permissions' => $permissions,
  1679. )
  1680. );
  1681. // ...and replace them with new ones.
  1682. if (!empty($insertRows))
  1683. $smcFunc['db_insert']('insert',
  1684. '{db_prefix}permissions',
  1685. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1686. $insertRows,
  1687. array('id_group', 'permission')
  1688. );
  1689. // Do a full child update.
  1690. updateChildPermissions(array(), -1);
  1691. // Just in case we cached this.
  1692. updateSettings(array('settings_updated' => time()));
  1693. }
  1694. /**
  1695. * Load permissions profiles.
  1696. */
  1697. function loadPermissionProfiles()
  1698. {
  1699. global $context, $txt, $smcFunc;
  1700. $request = $smcFunc['db_query']('', '
  1701. SELECT id_profile, profile_name
  1702. FROM {db_prefix}permission_profiles
  1703. ORDER BY id_profile',
  1704. array(
  1705. )
  1706. );
  1707. $context['profiles'] = array();
  1708. while ($row = $smcFunc['db_fetch_assoc']($request))
  1709. {
  1710. // Format the label nicely.
  1711. if (isset($txt['permissions_profile_' . $row['profile_name']]))
  1712. $name = $txt['permissions_profile_' . $row['profile_name']];
  1713. else
  1714. $name = $row['profile_name'];
  1715. $context['profiles'][$row['id_profile']] = array(
  1716. 'id' => $row['id_profile'],
  1717. 'name' => $name,
  1718. 'can_modify' => $row['id_profile'] == 1 || $row['id_profile'] > 4,
  1719. 'unformatted_name' => $row['profile_name'],
  1720. );
  1721. }
  1722. $smcFunc['db_free_result']($request);
  1723. }
  1724. /**
  1725. * Add/Edit/Delete profiles.
  1726. */
  1727. function EditPermissionProfiles()
  1728. {
  1729. global $context, $txt, $smcFunc;
  1730. // Setup the template, first for fun.
  1731. $context['page_title'] = $txt['permissions_profile_edit'];
  1732. $context['sub_template'] = 'edit_profiles';
  1733. // If we're creating a new one do it first.
  1734. if (isset($_POST['create']) && trim($_POST['profile_name']) != '')
  1735. {
  1736. checkSession();
  1737. validateToken('admin-mpp');
  1738. $_POST['copy_from'] = (int) $_POST['copy_from'];
  1739. $_POST['profile_name'] = $smcFunc['htmlspecialchars']($_POST['profile_name']);
  1740. // Insert the profile itself.
  1741. $smcFunc['db_insert']('',
  1742. '{db_prefix}permission_profiles',
  1743. array(
  1744. 'profile_name' => 'string',
  1745. ),
  1746. array(
  1747. $_POST['profile_name'],
  1748. ),
  1749. array('id_profile')
  1750. );
  1751. $profile_id = $smcFunc['db_insert_id']('{db_prefix}permission_profiles', 'id_profile');
  1752. // Load the permissions from the one it's being copied from.
  1753. $request = $smcFunc['db_query']('', '
  1754. SELECT id_group, permission, add_deny
  1755. FROM {db_prefix}board_permissions
  1756. WHERE id_profile = {int:copy_from}',
  1757. array(
  1758. 'copy_from' => $_POST['copy_from'],
  1759. )
  1760. );
  1761. $inserts = array();
  1762. while ($row = $smcFunc['db_fetch_assoc']($request))
  1763. $inserts[] = array($profile_id, $row['id_group'], $row['permission'], $row['add_deny']);
  1764. $smcFunc['db_free_result']($request);
  1765. if (!empty($inserts))
  1766. $smcFunc['db_insert']('insert',
  1767. '{db_prefix}board_permissions',
  1768. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1769. $inserts,
  1770. array('id_profile', 'id_group', 'permission')
  1771. );
  1772. }
  1773. // Renaming?
  1774. elseif (isset($_POST['rename']))
  1775. {
  1776. checkSession();
  1777. validateToken('admin-mpp');
  1778. // Just showing the boxes?
  1779. if (!isset($_POST['rename_profile']))
  1780. $context['show_rename_boxes'] = true;
  1781. else
  1782. {
  1783. foreach ($_POST['rename_profile'] as $id => $value)
  1784. {
  1785. $value = $smcFunc['htmlspecialchars']($value);
  1786. if (trim($value) != '' && $id > 4)
  1787. $smcFunc['db_query']('', '
  1788. UPDATE {db_prefix}permission_profiles
  1789. SET profile_name = {string:profile_name}
  1790. WHERE id_profile = {int:current_profile}',
  1791. array(
  1792. 'current_profile' => (int) $id,
  1793. 'profile_name' => $value,
  1794. )
  1795. );
  1796. }
  1797. }
  1798. }
  1799. // Deleting?
  1800. elseif (isset($_POST['delete']) && !empty($_POST['delete_profile']))
  1801. {
  1802. checkSession('post');
  1803. validateToken('admin-mpp');
  1804. $profiles = array();
  1805. foreach ($_POST['delete_profile'] as $profile)
  1806. if ($profile > 4)
  1807. $profiles[] = (int) $profile;
  1808. // Verify it's not in use...
  1809. $request = $smcFunc['db_query']('', '
  1810. SELECT id_board
  1811. FROM {db_prefix}boards
  1812. WHERE id_profile IN ({array_int:profile_list})
  1813. LIMIT 1',
  1814. array(
  1815. 'profile_list' => $profiles,
  1816. )
  1817. );
  1818. if ($smcFunc['db_num_rows']($request) != 0)
  1819. fatal_lang_error('no_access', false);
  1820. $smcFunc['db_free_result']($request);
  1821. // Oh well, delete.
  1822. $smcFunc['db_query']('', '
  1823. DELETE FROM {db_prefix}permission_profiles
  1824. WHERE id_profile IN ({array_int:profile_list})',
  1825. array(
  1826. 'profile_list' => $profiles,
  1827. )
  1828. );
  1829. }
  1830. // Clearly, we'll need this!
  1831. loadPermissionProfiles();
  1832. // Work out what ones are in use.
  1833. $request = $smcFunc['db_query']('', '
  1834. SELECT id_profile, COUNT(id_board) AS board_count
  1835. FROM {db_prefix}boards
  1836. GROUP BY id_profile',
  1837. array(
  1838. )
  1839. );
  1840. while ($row = $smcFunc['db_fetch_assoc']($request))
  1841. if (isset($context['profiles'][$row['id_profile']]))
  1842. {
  1843. $context['profiles'][$row['id_profile']]['in_use'] = true;
  1844. $context['profiles'][$row['id_profile']]['boards'] = $row['board_count'];
  1845. $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')];
  1846. }
  1847. $smcFunc['db_free_result']($request);
  1848. // What can we do with these?
  1849. $context['can_edit_something'] = false;
  1850. foreach ($context['profiles'] as $id => $profile)
  1851. {
  1852. // Can't delete special ones.
  1853. $context['profiles'][$id]['can_edit'] = isset($txt['permissions_profile_' . $profile['unformatted_name']]) ? false : true;
  1854. if ($context['profiles'][$id]['can_edit'])
  1855. $context['can_edit_something'] = true;
  1856. // You can only delete it if you can edit it AND it's not in use.
  1857. $context['profiles'][$id]['can_delete'] = $context['profiles'][$id]['can_edit'] && empty($profile['in_use']) ? true : false;
  1858. }
  1859. createToken('admin-mpp');
  1860. }
  1861. /**
  1862. * This function updates the permissions of any groups based off this group.
  1863. *
  1864. * @param mixed $parents (array or int)
  1865. * @param mixed $profile = null, int expected
  1866. */
  1867. function updateChildPermissions($parents, $profile = null)
  1868. {
  1869. global $smcFunc;
  1870. // All the parent groups to sort out.
  1871. if (!is_array($parents))
  1872. $parents = array($parents);
  1873. // Find all the children of this group.
  1874. $request = $smcFunc['db_query']('', '
  1875. SELECT id_parent, id_group
  1876. FROM {db_prefix}membergroups
  1877. WHERE id_parent != {int:not_inherited}
  1878. ' . (empty($parents) ? '' : 'AND id_parent IN ({array_int:parent_list})'),
  1879. array(
  1880. 'parent_list' => $parents,
  1881. 'not_inherited' => -2,
  1882. )
  1883. );
  1884. $children = array();
  1885. $parents = array();
  1886. $child_groups = array();
  1887. while ($row = $smcFunc['db_fetch_assoc']($request))
  1888. {
  1889. $children[$row['id_parent']][] = $row['id_group'];
  1890. $child_groups[] = $row['id_group'];
  1891. $parents[] = $row['id_parent'];
  1892. }
  1893. $smcFunc['db_free_result']($request);
  1894. $parents = array_unique($parents);
  1895. // Not a sausage, or a child?
  1896. if (empty($children))
  1897. return false;
  1898. // First off, are we doing general permissions?
  1899. if ($profile < 1 || $profile === null)
  1900. {
  1901. // Fetch all the parent permissions.
  1902. $request = $smcFunc['db_query']('', '
  1903. SELECT id_group, permission, add_deny
  1904. FROM {db_prefix}permissions
  1905. WHERE id_group IN ({array_int:parent_list})',
  1906. array(
  1907. 'parent_list' => $parents,
  1908. )
  1909. );
  1910. $permissions = array();
  1911. while ($row = $smcFunc['db_fetch_assoc']($request))
  1912. foreach ($children[$row['id_group']] as $child)
  1913. $permissions[] = array($child, $row['permission'], $row['add_deny']);
  1914. $smcFunc['db_free_result']($request);
  1915. $smcFunc['db_query']('', '
  1916. DELETE FROM {db_prefix}permissions
  1917. WHERE id_group IN ({array_int:child_groups})',
  1918. array(
  1919. 'child_groups' => $child_groups,
  1920. )
  1921. );
  1922. // Finally insert.
  1923. if (!empty($permissions))
  1924. {
  1925. $smcFunc['db_insert']('insert',
  1926. '{db_prefix}permissions',
  1927. array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1928. $permissions,
  1929. array('id_group', 'permission')
  1930. );
  1931. }
  1932. }
  1933. // Then, what about board profiles?
  1934. if ($profile != -1)
  1935. {
  1936. $profileQuery = $profile === null ? '' : ' AND id_profile = {int:current_profile}';
  1937. // Again, get all the parent permissions.
  1938. $request = $smcFunc['db_query']('', '
  1939. SELECT id_profile, id_group, permission, add_deny
  1940. FROM {db_prefix}board_permissions
  1941. WHERE id_group IN ({array_int:parent_groups})
  1942. ' . $profileQuery,
  1943. array(
  1944. 'parent_groups' => $parents,
  1945. 'current_profile' => $profile !== null && $profile ? $profile : 1,
  1946. )
  1947. );
  1948. $permissions = array();
  1949. while ($row = $smcFunc['db_fetch_assoc']($request))
  1950. foreach ($children[$row['id_group']] as $child)
  1951. $permissions[] = array($child, $row['id_profile'], $row['permission'], $row['add_deny']);
  1952. $smcFunc['db_free_result']($request);
  1953. $smcFunc['db_query']('', '
  1954. DELETE FROM {db_prefix}board_permissions
  1955. WHERE id_group IN ({array_int:child_groups})
  1956. ' . $profileQuery,
  1957. array(
  1958. 'child_groups' => $child_groups,
  1959. 'current_profile' => $profile !== null && $profile ? $profile : 1,
  1960. )
  1961. );
  1962. // Do the insert.
  1963. if (!empty($permissions))
  1964. {
  1965. $smcFunc['db_insert']('insert',
  1966. '{db_prefix}board_permissions',
  1967. array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  1968. $permissions,
  1969. array('id_group', 'id_profile', 'permission')
  1970. );
  1971. }
  1972. }
  1973. }
  1974. /**
  1975. * Load permissions someone cannot grant.
  1976. */
  1977. function loadIllegalPermissions()
  1978. {
  1979. global $context;
  1980. $context['illegal_permissions'] = array();
  1981. if (!allowedTo('admin_forum'))
  1982. $context['illegal_permissions'][] = 'admin_forum';
  1983. if (!allowedTo('manage_membergroups'))
  1984. $context['illegal_permissions'][] = 'manage_membergroups';
  1985. if (!allowedTo('manage_permissions'))
  1986. $context['illegal_permissions'][] = 'manage_permissions';
  1987. call_integration_hook('integrate_load_illegal_permissions');
  1988. }
  1989. /**
  1990. * Loads the permissions that can not be given to guests.
  1991. * Stores the permissions in $context['non_guest_permissions'].
  1992. */
  1993. function loadIllegalGuestPermissions()
  1994. {
  1995. global $context;
  1996. $context['non_guest_permissions'] = array(
  1997. 'access_mod_center',
  1998. 'admin_forum',
  1999. 'announce_topic',
  2000. 'approve_posts',
  2001. 'calendar_edit',
  2002. 'delete',
  2003. 'delete_replies',
  2004. 'edit_news',
  2005. 'issue_warning',
  2006. 'karma_edit',
  2007. 'lock',
  2008. 'make_sticky',
  2009. 'manage_attachments',
  2010. 'manage_bans',
  2011. 'manage_boards',
  2012. 'manage_membergroups',
  2013. 'manage_permissions',
  2014. 'manage_smileys',
  2015. 'merge_any',
  2016. 'moderate_board',
  2017. 'moderate_forum',
  2018. 'modify',
  2019. 'modify_replies',
  2020. 'move',
  2021. 'pm_autosave_draft',
  2022. 'pm_draft',
  2023. 'pm_read',
  2024. 'pm_send',
  2025. 'poll_add',
  2026. 'poll_edit',
  2027. 'poll_lock',
  2028. 'poll_remove',
  2029. 'post_autosave_draft',
  2030. 'post_draft',
  2031. 'profile_blurb',
  2032. 'profile_displayed_name',
  2033. 'profile_extra',
  2034. 'profile_forum',
  2035. 'profile_identity',
  2036. 'profile_other',
  2037. 'profile_password',
  2038. 'profile_remove',
  2039. 'profile_remote_avatar',
  2040. 'profile_server_avatar',
  2041. 'profile_signature',
  2042. 'profile_title',
  2043. 'profile_upload_avatar',
  2044. 'remove',
  2045. 'report_any',
  2046. 'send_email_to_members',
  2047. 'send_mail',
  2048. 'split_any',
  2049. );
  2050. call_integration_hook('integrate_load_illegal_guest_permissions');
  2051. }
  2052. /**
  2053. * Present a nice way of applying post moderation.
  2054. */
  2055. function ModifyPostModeration()
  2056. {
  2057. global $context, $txt, $smcFunc, $modSettings, $sourcedir;
  2058. // Just in case.
  2059. checkSession('get');
  2060. $context['page_title'] = $txt['permissions_post_moderation'];
  2061. $context['sub_template'] = 'postmod_permissions';
  2062. $context['current_profile'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 1;
  2063. // Load all the permission profiles.
  2064. loadPermissionProfiles();
  2065. // Mappings, our key => array(can_do_moderated, can_do_all)
  2066. $mappings = array(
  2067. 'new_topic' => array('post_new', 'post_unapproved_topics'),
  2068. 'replies_own' => array('post_reply_own', 'post_unapproved_replies_own'),
  2069. 'replies_any' => array('post_reply_any', 'post_unapproved_replies_any'),
  2070. 'attachment' => array('post_attachment', 'post_unapproved_attachments'),
  2071. );
  2072. call_integration_hook('integrate_post_moderation_mapping', array(&$mappings));
  2073. // Start this with the guests/members.
  2074. $context['profile_groups'] = array(
  2075. -1 => array(
  2076. 'id' => -1,
  2077. 'name' => $txt['membergroups_guests'],
  2078. 'color' => '',
  2079. 'new_topic' => 'disallow',
  2080. 'replies_own' => 'disallow',
  2081. 'replies_any' => 'disallow',
  2082. 'attachment' => 'disallow',
  2083. 'children' => array(),
  2084. ),
  2085. 0 => array(
  2086. 'id' => 0,
  2087. 'name' => $txt['membergroups_members'],
  2088. 'color' => '',
  2089. 'new_topic' => 'disallow',
  2090. 'replies_own' => 'disallow',
  2091. 'replies_any' => 'disallow',
  2092. 'attachment' => 'disallow',
  2093. 'children' => array(),
  2094. ),
  2095. );
  2096. // Load the groups.
  2097. $request = $smcFunc['db_query']('', '
  2098. SELECT id_group, group_name, online_color, id_parent
  2099. FROM {db_prefix}membergroups
  2100. WHERE id_group != {int:admin_group}
  2101. ' . (empty($modSettings['permission_enable_postgroups']) ? ' AND min_posts = {int:min_posts}' : '') . '
  2102. ORDER BY id_parent ASC',
  2103. array(
  2104. 'admin_group' => 1,
  2105. 'min_posts' => -1,
  2106. )
  2107. );
  2108. while ($row = $smcFunc['db_fetch_assoc']($request))
  2109. {
  2110. if ($row['id_parent'] == -2)
  2111. {
  2112. $context['profile_groups'][$row['id_group']] = array(
  2113. 'id' => $row['id_group'],
  2114. 'name' => $row['group_name'],
  2115. 'color' => $row['online_color'],
  2116. 'new_topic' => 'disallow',
  2117. 'replies_own' => 'disallow',
  2118. 'replies_any' => 'disallow',
  2119. 'attachment' => 'disallow',
  2120. 'children' => array(),
  2121. );
  2122. }
  2123. elseif (isset($context['profile_groups'][$row['id_parent']]))
  2124. $context['profile_groups'][$row['id_parent']]['children'][] = $row['group_name'];
  2125. }
  2126. $smcFunc['db_free_result']($request);
  2127. // What are the permissions we are querying?
  2128. $all_permissions = array();
  2129. foreach ($mappings as $perm_set)
  2130. $all_permissions = array_merge($all_permissions, $perm_set);
  2131. // If we're saving the changes then do just that - save them.
  2132. if (!empty($_POST['save_changes']) && ($context['current_profile'] == 1 || $context['current_profile'] > 4))
  2133. {
  2134. validateToken('admin-mppm');
  2135. // First, are we saving a new value for enabled post moderation?
  2136. $new_setting = !empty($_POST['postmod_active']);
  2137. if ($new_setting != $modSettings['postmod_active'])
  2138. {
  2139. if ($new_setting)
  2140. {
  2141. // Turning it on. This seems easy enough.
  2142. updateSettings(array('postmod_active' => 1));
  2143. }
  2144. else
  2145. {
  2146. // Turning it off. Not so straightforward. We have to turn off warnings to moderation level, and make everything approved.
  2147. updateSettings(array(
  2148. 'postmod_active' => 0,
  2149. 'warning_moderate' => 0,
  2150. ));
  2151. require_once($sourcedir . '/PostModeration.php');
  2152. approveAllData();
  2153. }
  2154. }
  2155. elseif ($modSettings['postmod_active'])
  2156. {
  2157. // We're not saving a new setting - and if it's still enabled we have more work to do.
  2158. // Start by deleting all the permissions relevant.
  2159. $smcFunc['db_query']('', '
  2160. DELETE FROM {db_prefix}board_permissions
  2161. WHERE id_profile = {int:current_profile}
  2162. AND permission IN ({array_string:permissions})
  2163. AND id_group IN ({array_int:profile_group_list})',
  2164. array(
  2165. 'profile_group_list' => array_keys($context['profile_groups']),
  2166. 'current_profile' => $context['current_profile'],
  2167. 'permissions' => $all_permissions,
  2168. )
  2169. );
  2170. // Do it group by group.
  2171. $new_permissions = array();
  2172. foreach ($context['profile_groups'] as $id => $group)
  2173. {
  2174. foreach ($mappings as $index => $data)
  2175. {
  2176. if (isset($_POST[$index][$group['id']]))
  2177. {
  2178. if ($_POST[$index][$group['id']] == 'allow')
  2179. {
  2180. // Give them both sets for fun.
  2181. $new_permissions[] = array($context['current_profile'], $group['id'], $data[0], 1);
  2182. $new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
  2183. }
  2184. elseif ($_POST[$index][$group['id']] == 'moderate')
  2185. $new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
  2186. }
  2187. }
  2188. }
  2189. // Insert new permissions.
  2190. if (!empty($new_permissions))
  2191. $smcFunc['db_insert']('',
  2192. '{db_prefix}board_permissions',
  2193. array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
  2194. $new_permissions,
  2195. array('id_profile', 'id_group', 'permission')
  2196. );
  2197. }
  2198. }
  2199. // Now get all the permissions!
  2200. $request = $smcFunc['db_query']('', '
  2201. SELECT id_group, permission, add_deny
  2202. FROM {db_prefix}board_permissions
  2203. WHERE id_profile = {int:current_profile}
  2204. AND permission IN ({array_string:permissions})
  2205. AND id_group IN ({array_int:profile_group_list})',
  2206. array(
  2207. 'profile_group_list' => array_keys($context['profile_groups']),
  2208. 'current_profile' => $context['current_profile'],
  2209. 'permissions' => $all_permissions,
  2210. )
  2211. );
  2212. while ($row = $smcFunc['db_fetch_assoc']($request))
  2213. {
  2214. foreach ($mappings as $key => $data)
  2215. {
  2216. foreach ($data as $index => $perm)
  2217. {
  2218. if ($perm == $row['permission'])
  2219. {
  2220. // Only bother if it's not denied.
  2221. if ($row['add_deny'])
  2222. {
  2223. // Full allowance?
  2224. if ($index == 0)
  2225. $context['profile_groups'][$row['id_group']][$key] = 'allow';
  2226. // Otherwise only bother with moderate if not on allow.
  2227. elseif ($context['profile_groups'][$row['id_group']][$key] != 'allow')
  2228. $context['profile_groups'][$row['id_group']][$key] = 'moderate';
  2229. }
  2230. }
  2231. }
  2232. }
  2233. }
  2234. $smcFunc['db_free_result']($request);
  2235. createToken('admin-mppm');
  2236. }
  2237. ?>