ManagePermissions.php 78 KB

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