ManagePermissions.php 74 KB

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