ManagePermissions.php 74 KB

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