Reports.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. <?php
  2. /**
  3. * This file is exclusively for generating reports to help assist forum
  4. * administrators keep track of their forum configuration and state. The
  5. * core report generation is done in two areas. Firstly, a report "generator"
  6. * will fill context with relevant data. Secondly, the choice of sub-template
  7. * will determine how this data is shown to the user
  8. *
  9. * Functions ending with "Report" are responsible for generating data for reporting.
  10. * They are all called from ReportsMain.
  11. * Never access the context directly, but use the data handling functions to do so.
  12. *
  13. * Simple Machines Forum (SMF)
  14. *
  15. * @package SMF
  16. * @author Simple Machines http://www.simplemachines.org
  17. * @copyright 2014 Simple Machines and individual contributors
  18. * @license http://www.simplemachines.org/about/smf/license.php BSD
  19. *
  20. * @version 2.1 Alpha 1
  21. */
  22. if (!defined('SMF'))
  23. die('No direct access...');
  24. /**
  25. * Handling function for generating reports.
  26. * Requires the admin_forum permission.
  27. * Loads the Reports template and language files.
  28. * Decides which type of report to generate, if this isn't passed
  29. * through the querystring it will set the report_type sub-template to
  30. * force the user to choose which type.
  31. * When generating a report chooses which sub_template to use.
  32. * Depends on the cal_enabled setting, and many of the other cal_
  33. * settings.
  34. * Will call the relevant report generation function.
  35. * If generating report will call finishTables before returning.
  36. * Accessed through ?action=admin;area=reports.
  37. */
  38. function ReportsMain()
  39. {
  40. global $txt, $context, $scripturl;
  41. // Only admins, only EVER admins!
  42. isAllowedTo('admin_forum');
  43. // Let's get our things running...
  44. loadTemplate('Reports');
  45. loadLanguage('Reports');
  46. $context['page_title'] = $txt['generate_reports'];
  47. // These are the types of reports which exist - and the functions to generate them.
  48. $context['report_types'] = array(
  49. 'boards' => 'BoardReport',
  50. 'board_perms' => 'BoardPermissionsReport',
  51. 'member_groups' => 'MemberGroupsReport',
  52. 'group_perms' => 'GroupPermissionsReport',
  53. 'staff' => 'StaffReport',
  54. );
  55. call_integration_hook('integrate_report_types');
  56. // Load up all the tabs...
  57. $context[$context['admin_menu_name']]['tab_data'] = array(
  58. 'title' => $txt['generate_reports'],
  59. 'help' => '',
  60. 'description' => $txt['generate_reports_desc'],
  61. );
  62. $is_first = 0;
  63. foreach ($context['report_types'] as $k => $temp)
  64. $context['report_types'][$k] = array(
  65. 'id' => $k,
  66. // @todo what is $type? It is never set!
  67. 'title' => isset($txt['gr_type_' . $k]) ? $txt['gr_type_' . $k] : $type['id'],
  68. 'description' => isset($txt['gr_type_desc_' . $k]) ? $txt['gr_type_desc_' . $k] : null,
  69. 'function' => $temp,
  70. 'is_first' => $is_first++ == 0,
  71. );
  72. // If they haven't choosen a report type which is valid, send them off to the report type chooser!
  73. if (empty($_REQUEST['rt']) || !isset($context['report_types'][$_REQUEST['rt']]))
  74. {
  75. $context['sub_template'] = 'report_type';
  76. return;
  77. }
  78. $context['report_type'] = $_REQUEST['rt'];
  79. // What are valid templates for showing reports?
  80. $reportTemplates = array(
  81. 'main' => array(
  82. 'layers' => null,
  83. ),
  84. 'print' => array(
  85. 'layers' => array('print'),
  86. ),
  87. );
  88. // Specific template? Use that instead of main!
  89. if (isset($_REQUEST['st']) && isset($reportTemplates[$_REQUEST['st']]))
  90. {
  91. $context['sub_template'] = $_REQUEST['st'];
  92. // Are we disabling the other layers - print friendly for example?
  93. if ($reportTemplates[$_REQUEST['st']]['layers'] !== null)
  94. $context['template_layers'] = $reportTemplates[$_REQUEST['st']]['layers'];
  95. }
  96. // Make the page title more descriptive.
  97. $context['page_title'] .= ' - ' . (isset($txt['gr_type_' . $context['report_type']]) ? $txt['gr_type_' . $context['report_type']] : $context['report_type']);
  98. // Build the reports button array.
  99. $context['report_buttons'] = array(
  100. 'generate_reports' => array('text' => 'generate_reports', 'image' => 'print.png', 'lang' => true, 'url' => $scripturl . '?action=admin;area=reports', 'active' => true),
  101. 'print' => array('text' => 'print', 'image' => 'print.png', 'lang' => true, 'url' => $scripturl . '?action=admin;area=reports;rt=' . $context['report_type']. ';st=print', 'custom' => 'target="_blank"'),
  102. );
  103. // Allow mods to add additional buttons here
  104. call_integration_hook('integrate_report_buttons');
  105. // Now generate the data.
  106. $context['report_types'][$context['report_type']]['function']();
  107. // Finish the tables before exiting - this is to help the templates a little more.
  108. finishTables();
  109. }
  110. /**
  111. * Standard report about what settings the boards have.
  112. * functions ending with "Report" are responsible for generating data
  113. * for reporting.
  114. * they are all called from ReportsMain.
  115. * never access the context directly, but use the data handling
  116. * functions to do so.
  117. */
  118. function BoardReport()
  119. {
  120. global $context, $txt, $sourcedir, $smcFunc, $modSettings;
  121. // Load the permission profiles.
  122. require_once($sourcedir . '/ManagePermissions.php');
  123. loadLanguage('ManagePermissions');
  124. loadPermissionProfiles();
  125. // Get every moderator.
  126. $request = $smcFunc['db_query']('', '
  127. SELECT mods.id_board, mods.id_member, mem.real_name
  128. FROM {db_prefix}moderators AS mods
  129. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)',
  130. array(
  131. )
  132. );
  133. $moderators = array();
  134. while ($row = $smcFunc['db_fetch_assoc']($request))
  135. $moderators[$row['id_board']][] = $row['real_name'];
  136. $smcFunc['db_free_result']($request);
  137. // Get every moderator gruop.
  138. $request = $smcFunc['db_query']('', '
  139. SELECT modgs.id_board, modgs.id_group, memg.group_name
  140. FROM {db_prefix}moderator_groups AS modgs
  141. INNER JOIN {db_prefix}membergroups AS memg ON (memg.id_group = modgs.id_group)',
  142. array(
  143. )
  144. );
  145. $moderator_groups = array();
  146. while ($row = $smcFunc['db_fetch_assoc']($request))
  147. $moderator_groups[$row['id_board']][] = $row['group_name'];
  148. $smcFunc['db_free_result']($request);
  149. // Get all the possible membergroups!
  150. $request = $smcFunc['db_query']('', '
  151. SELECT id_group, group_name, online_color
  152. FROM {db_prefix}membergroups',
  153. array(
  154. )
  155. );
  156. $groups = array(-1 => $txt['guest_title'], 0 => $txt['full_member']);
  157. while ($row = $smcFunc['db_fetch_assoc']($request))
  158. $groups[$row['id_group']] = empty($row['online_color']) ? $row['group_name'] : '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>';
  159. $smcFunc['db_free_result']($request);
  160. // All the fields we'll show.
  161. $boardSettings = array(
  162. 'category' => $txt['board_category'],
  163. 'parent' => $txt['board_parent'],
  164. 'redirect' => $txt['board_redirect'],
  165. 'num_topics' => $txt['board_num_topics'],
  166. 'num_posts' => $txt['board_num_posts'],
  167. 'count_posts' => $txt['board_count_posts'],
  168. 'theme' => $txt['board_theme'],
  169. 'override_theme' => $txt['board_override_theme'],
  170. 'profile' => $txt['board_profile'],
  171. 'moderators' => $txt['board_moderators'],
  172. 'moderator_groups' => $txt['board_moderator_groups'],
  173. 'groups' => $txt['board_groups'],
  174. );
  175. if (!empty($modSettings['deny_boards_access']))
  176. $boardSettings['disallowed_groups'] = $txt['board_disallowed_groups'];
  177. // Do it in columns, it's just easier.
  178. setKeys('cols');
  179. // Go through each board!
  180. $request = $smcFunc['db_query']('order_by_board_order', '
  181. SELECT b.id_board, b.name, b.num_posts, b.num_topics, b.count_posts, b.member_groups, b.override_theme, b.id_profile, b.deny_member_groups,
  182. b.redirect, c.name AS cat_name, IFNULL(par.name, {string:text_none}) AS parent_name, IFNULL(th.value, {string:text_none}) AS theme_name
  183. FROM {db_prefix}boards AS b
  184. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  185. LEFT JOIN {db_prefix}boards AS par ON (par.id_board = b.id_parent)
  186. LEFT JOIN {db_prefix}themes AS th ON (th.id_theme = b.id_theme AND th.variable = {string:name})',
  187. array(
  188. 'name' => 'name',
  189. 'text_none' => $txt['none'],
  190. )
  191. );
  192. $boards = array(0 => array('name' => $txt['global_boards']));
  193. while ($row = $smcFunc['db_fetch_assoc']($request))
  194. {
  195. // Each board has it's own table.
  196. newTable($row['name'], '', 'left', 'auto', 'left', 200, 'left');
  197. $this_boardSettings = $boardSettings;
  198. if (empty($row['redirect']))
  199. unset($this_boardSettings['redirect']);
  200. // First off, add in the side key.
  201. addData($this_boardSettings);
  202. // Format the profile name.
  203. $profile_name = $context['profiles'][$row['id_profile']]['name'];
  204. // Create the main data array.
  205. $boardData = array(
  206. 'category' => $row['cat_name'],
  207. 'parent' => $row['parent_name'],
  208. 'redirect' => $row['redirect'],
  209. 'num_posts' => $row['num_posts'],
  210. 'num_topics' => $row['num_topics'],
  211. 'count_posts' => empty($row['count_posts']) ? $txt['yes'] : $txt['no'],
  212. 'theme' => $row['theme_name'],
  213. 'profile' => $profile_name,
  214. 'override_theme' => $row['override_theme'] ? $txt['yes'] : $txt['no'],
  215. 'moderators' => empty($moderators[$row['id_board']]) ? $txt['none'] : implode(', ', $moderators[$row['id_board']]),
  216. 'moderator_groups' => empty($moderator_groups[$row['id_board']]) ? $txt['none'] : implode(', ', $moderator_groups[$row['id_board']]),
  217. );
  218. // Work out the membergroups who can and cannot access it (but only if enabled).
  219. $allowedGroups = explode(',', $row['member_groups']);
  220. foreach ($allowedGroups as $key => $group)
  221. {
  222. if (isset($groups[$group]))
  223. $allowedGroups[$key] = $groups[$group];
  224. else
  225. unset($allowedGroups[$key]);
  226. }
  227. $boardData['groups'] = implode(', ', $allowedGroups);
  228. if (!empty($modSettings['deny_boards_access']))
  229. {
  230. $disallowedGroups = explode(',', $row['deny_member_groups']);
  231. foreach ($disallowedGroups as $key => $group)
  232. {
  233. if (isset($groups[$group]))
  234. $disallowedGroups[$key] = $groups[$group];
  235. else
  236. unset($disallowedGroups[$key]);
  237. }
  238. $boardData['disallowed_groups'] = implode(', ', $disallowedGroups);
  239. }
  240. if (empty($row['redirect']))
  241. unset ($boardData['redirect']);
  242. // Next add the main data.
  243. addData($boardData);
  244. }
  245. $smcFunc['db_free_result']($request);
  246. }
  247. /**
  248. * Generate a report on the current permissions by board and membergroup.
  249. * functions ending with "Report" are responsible for generating data
  250. * for reporting.
  251. * they are all called from ReportsMain.
  252. * never access the context directly, but use the data handling
  253. * functions to do so.
  254. */
  255. function BoardPermissionsReport()
  256. {
  257. global $context, $txt, $modSettings, $smcFunc;
  258. // Get as much memory as possible as this can be big.
  259. setMemoryLimit('256M');
  260. if (isset($_REQUEST['boards']))
  261. {
  262. if (!is_array($_REQUEST['boards']))
  263. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  264. foreach ($_REQUEST['boards'] as $k => $dummy)
  265. $_REQUEST['boards'][$k] = (int) $dummy;
  266. $board_clause = 'id_board IN ({array_int:boards})';
  267. }
  268. else
  269. $board_clause = '1=1';
  270. if (isset($_REQUEST['groups']))
  271. {
  272. if (!is_array($_REQUEST['groups']))
  273. $_REQUEST['groups'] = explode(',', $_REQUEST['groups']);
  274. foreach ($_REQUEST['groups'] as $k => $dummy)
  275. $_REQUEST['groups'][$k] = (int) $dummy;
  276. $group_clause = 'id_group IN ({array_int:groups})';
  277. }
  278. else
  279. $group_clause = '1=1';
  280. // Fetch all the board names.
  281. $request = $smcFunc['db_query']('', '
  282. SELECT id_board, name, id_profile
  283. FROM {db_prefix}boards
  284. WHERE ' . $board_clause . '
  285. ORDER BY id_board',
  286. array(
  287. 'boards' => isset($_REQUEST['boards']) ? $_REQUEST['boards'] : array(),
  288. )
  289. );
  290. $profiles = array();
  291. while ($row = $smcFunc['db_fetch_assoc']($request))
  292. {
  293. $boards[$row['id_board']] = array(
  294. 'name' => $row['name'],
  295. 'profile' => $row['id_profile'],
  296. 'mod_groups' => array(),
  297. );
  298. $profiles[] = $row['id_profile'];
  299. }
  300. $smcFunc['db_free_result']($request);
  301. // Get the ids of any groups allowed to moderate this board
  302. // Limit it to any boards and/or groups we're looking at
  303. $request = $smcFunc['db_query']('', '
  304. SELECT id_board, id_group
  305. FROM {db_prefix}moderator_groups
  306. WHERE ' . $board_clause .' AND ' . $group_clause,
  307. array(
  308. )
  309. );
  310. while ($row = $smcFunc['db_fetch_assoc']($request))
  311. {
  312. $boards[$row['id_board']]['mod_groups'][] = $row['id_group'];
  313. }
  314. $smcFunc['db_free_result']($request);
  315. // Get all the possible membergroups, except admin!
  316. $request = $smcFunc['db_query']('', '
  317. SELECT id_group, group_name
  318. FROM {db_prefix}membergroups
  319. WHERE ' . $group_clause . '
  320. AND id_group != {int:admin_group}' . (empty($modSettings['permission_enable_postgroups']) ? '
  321. AND min_posts = {int:min_posts}' : '') . '
  322. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  323. array(
  324. 'admin_group' => 1,
  325. 'min_posts' => -1,
  326. 'newbie_group' => 4,
  327. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  328. )
  329. );
  330. if (!isset($_REQUEST['groups']) || in_array(-1, $_REQUEST['groups']) || in_array(0, $_REQUEST['groups']))
  331. $member_groups = array('col' => '', -1 => $txt['membergroups_guests'], 0 => $txt['membergroups_members']);
  332. else
  333. $member_groups = array('col' => '');
  334. while ($row = $smcFunc['db_fetch_assoc']($request))
  335. $member_groups[$row['id_group']] = $row['group_name'];
  336. $smcFunc['db_free_result']($request);
  337. // Make sure that every group is represented - plus in rows!
  338. setKeys('rows', $member_groups);
  339. // Certain permissions should not really be shown.
  340. $disabled_permissions = array();
  341. if (!$modSettings['postmod_active'])
  342. {
  343. $disabled_permissions[] = 'approve_posts';
  344. $disabled_permissions[] = 'post_unapproved_topics';
  345. $disabled_permissions[] = 'post_unapproved_replies_own';
  346. $disabled_permissions[] = 'post_unapproved_replies_any';
  347. $disabled_permissions[] = 'post_unapproved_attachments';
  348. }
  349. call_integration_hook('integrate_reports_boardperm', array(&$disabled_permissions));
  350. // Cache every permission setting, to make sure we don't miss any allows.
  351. $permissions = array();
  352. $board_permissions = array();
  353. $request = $smcFunc['db_query']('', '
  354. SELECT id_profile, id_group, add_deny, permission
  355. FROM {db_prefix}board_permissions
  356. WHERE id_profile IN ({array_int:profile_list})
  357. AND ' . $group_clause . (empty($modSettings['permission_enable_deny']) ? '
  358. AND add_deny = {int:not_deny}' : '') . '
  359. ORDER BY id_profile, permission',
  360. array(
  361. 'profile_list' => $profiles,
  362. 'not_deny' => 1,
  363. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  364. )
  365. );
  366. while ($row = $smcFunc['db_fetch_assoc']($request))
  367. {
  368. if (in_array($row['permission'], $disabled_permissions))
  369. continue;
  370. foreach ($boards as $id => $board)
  371. if ($board['profile'] == $row['id_profile'])
  372. $board_permissions[$id][$row['id_group']][$row['permission']] = $row['add_deny'];
  373. // Make sure we get every permission.
  374. if (!isset($permissions[$row['permission']]))
  375. {
  376. // This will be reused on other boards.
  377. $permissions[$row['permission']] = array(
  378. 'title' => isset($txt['board_perms_name_' . $row['permission']]) ? $txt['board_perms_name_' . $row['permission']] : $row['permission'],
  379. );
  380. }
  381. }
  382. $smcFunc['db_free_result']($request);
  383. // Now cycle through the board permissions array... lots to do ;)
  384. foreach ($board_permissions as $board => $groups)
  385. {
  386. // Create the table for this board first.
  387. newTable($boards[$board]['name'], 'x', 'all', 100, 'center', 200, 'left');
  388. // Add the header row - shows all the membergroups.
  389. addData($member_groups);
  390. // Add the separator.
  391. addSeparator($txt['board_perms_permission']);
  392. // Here cycle through all the detected permissions.
  393. foreach ($permissions as $ID_PERM => $perm_info)
  394. {
  395. // Is this identical to the global?
  396. $identicalGlobal = $board == 0 ? false : true;
  397. // Default data for this row.
  398. $curData = array('col' => $perm_info['title']);
  399. // Now cycle each membergroup in this set of permissions.
  400. foreach ($member_groups as $id_group => $name)
  401. {
  402. // Don't overwrite the key column!
  403. if ($id_group === 'col')
  404. continue;
  405. $group_permissions = isset($groups[$id_group]) ? $groups[$id_group] : array();
  406. // Do we have any data for this group?
  407. if (isset($group_permissions[$ID_PERM]))
  408. {
  409. // Set the data for this group to be the local permission.
  410. $curData[$id_group] = $group_permissions[$ID_PERM];
  411. }
  412. // Is it inherited from Moderator?
  413. elseif (in_array($id_group, $boards[$board]['mod_groups']) && !empty($groups[3]) && isset($groups[3][$ID_PERM]))
  414. {
  415. $curData[$id_group] = $groups[3][$ID_PERM];
  416. }
  417. // Otherwise means it's set to disallow..
  418. else
  419. {
  420. $curData[$id_group] = 'x';
  421. }
  422. // Now actually make the data for the group look right.
  423. if (empty($curData[$id_group]))
  424. $curData[$id_group] = '<span style="color: red;">' . $txt['board_perms_deny'] . '</span>';
  425. elseif ($curData[$id_group] == 1)
  426. $curData[$id_group] = '<span style="color: darkgreen;">' . $txt['board_perms_allow'] . '</span>';
  427. else
  428. $curData[$id_group] = 'x';
  429. // Embolden those permissions different from global (makes it a lot easier!)
  430. if (@$board_permissions[0][$id_group][$ID_PERM] != @$group_permissions[$ID_PERM])
  431. $curData[$id_group] = '<strong>' . $curData[$id_group] . '</strong>';
  432. }
  433. // Now add the data for this permission.
  434. addData($curData);
  435. }
  436. }
  437. }
  438. /**
  439. * Show what the membergroups are made of.
  440. * functions ending with "Report" are responsible for generating data
  441. * for reporting.
  442. * they are all called from ReportsMain.
  443. * never access the context directly, but use the data handling
  444. * functions to do so.
  445. */
  446. function MemberGroupsReport()
  447. {
  448. global $context, $txt, $settings, $modSettings, $smcFunc;
  449. // Fetch all the board names.
  450. $request = $smcFunc['db_query']('', '
  451. SELECT id_board, name, member_groups, id_profile, deny_member_groups
  452. FROM {db_prefix}boards',
  453. array(
  454. )
  455. );
  456. while ($row = $smcFunc['db_fetch_assoc']($request))
  457. {
  458. if (trim($row['member_groups']) == '')
  459. $groups = array(1);
  460. else
  461. $groups = array_merge(array(1), explode(',', $row['member_groups']));
  462. if (trim($row['deny_member_groups']) == '')
  463. $denyGroups = array();
  464. else
  465. $denyGroups = explode(',', $row['deny_member_groups']);
  466. $boards[$row['id_board']] = array(
  467. 'id' => $row['id_board'],
  468. 'name' => $row['name'],
  469. 'profile' => $row['id_profile'],
  470. 'groups' => $groups,
  471. 'deny_groups' => $denyGroups,
  472. );
  473. }
  474. $smcFunc['db_free_result']($request);
  475. // Standard settings.
  476. $mgSettings = array(
  477. 'name' => '',
  478. '#sep#1' => $txt['member_group_settings'],
  479. 'color' => $txt['member_group_color'],
  480. 'min_posts' => $txt['member_group_min_posts'],
  481. 'max_messages' => $txt['member_group_max_messages'],
  482. 'icons' => $txt['member_group_icons'],
  483. '#sep#2' => $txt['member_group_access'],
  484. );
  485. // Add on the boards!
  486. foreach ($boards as $board)
  487. $mgSettings['board_' . $board['id']] = $board['name'];
  488. // Add all the membergroup settings, plus we'll be adding in columns!
  489. setKeys('cols', $mgSettings);
  490. // Only one table this time!
  491. newTable($txt['gr_type_member_groups'], '-', 'all', 100, 'center', 200, 'left');
  492. // Get the shaded column in.
  493. addData($mgSettings);
  494. // Now start cycling the membergroups!
  495. $request = $smcFunc['db_query']('', '
  496. SELECT mg.id_group, mg.group_name, mg.online_color, mg.min_posts, mg.max_messages, mg.icons,
  497. CASE WHEN bp.permission IS NOT NULL OR mg.id_group = {int:admin_group} THEN 1 ELSE 0 END AS can_moderate
  498. FROM {db_prefix}membergroups AS mg
  499. LEFT JOIN {db_prefix}board_permissions AS bp ON (bp.id_group = mg.id_group AND bp.id_profile = {int:default_profile} AND bp.permission = {string:moderate_board})
  500. ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
  501. array(
  502. 'admin_group' => 1,
  503. 'default_profile' => 1,
  504. 'newbie_group' => 4,
  505. 'moderate_board' => 'moderate_board',
  506. )
  507. );
  508. // Cache them so we get regular members too.
  509. $rows = array(
  510. array(
  511. 'id_group' => -1,
  512. 'group_name' => $txt['membergroups_guests'],
  513. 'online_color' => '',
  514. 'min_posts' => -1,
  515. 'max_messages' => null,
  516. 'icons' => ''
  517. ),
  518. array(
  519. 'id_group' => 0,
  520. 'group_name' => $txt['membergroups_members'],
  521. 'online_color' => '',
  522. 'min_posts' => -1,
  523. 'max_messages' => null,
  524. 'icons' => ''
  525. ),
  526. );
  527. while ($row = $smcFunc['db_fetch_assoc']($request))
  528. $rows[] = $row;
  529. $smcFunc['db_free_result']($request);
  530. foreach ($rows as $row)
  531. {
  532. $row['icons'] = explode('#', $row['icons']);
  533. $group = array(
  534. 'name' => $row['group_name'],
  535. 'color' => empty($row['online_color']) ? '-' : '<span style="color: ' . $row['online_color'] . ';">' . $row['online_color'] . '</span>',
  536. 'min_posts' => $row['min_posts'] == -1 ? 'N/A' : $row['min_posts'],
  537. 'max_messages' => $row['max_messages'],
  538. 'icons' => !empty($row['icons'][0]) && !empty($row['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['icons'][1] . '" alt="*">', $row['icons'][0]) : '',
  539. );
  540. // Board permissions.
  541. foreach ($boards as $board)
  542. $group['board_' . $board['id']] = in_array($row['id_group'], $board['groups']) ? '<span class="success">' . $txt['board_perms_allow'] . '</span>' : (!empty($modSettings['deny_boards_access']) && in_array($row['id_group'], $board['deny_groups']) ? '<span class="alert">' . $txt['board_perms_deny'] . '</span>' : 'x');
  543. addData($group);
  544. }
  545. }
  546. /**
  547. * Show the large variety of group permissions assigned to each membergroup.
  548. * functions ending with "Report" are responsible for generating data
  549. * for reporting.
  550. * they are all called from ReportsMain.
  551. * never access the context directly, but use the data handling
  552. * functions to do so.
  553. */
  554. function GroupPermissionsReport()
  555. {
  556. global $context, $txt, $modSettings, $smcFunc;
  557. if (isset($_REQUEST['groups']))
  558. {
  559. if (!is_array($_REQUEST['groups']))
  560. $_REQUEST['groups'] = explode(',', $_REQUEST['groups']);
  561. foreach ($_REQUEST['groups'] as $k => $dummy)
  562. $_REQUEST['groups'][$k] = (int) $dummy;
  563. $_REQUEST['groups'] = array_diff($_REQUEST['groups'], array(3));
  564. $clause = 'id_group IN ({array_int:groups})';
  565. }
  566. else
  567. $clause = 'id_group != {int:moderator_group}';
  568. // Get all the possible membergroups, except admin!
  569. $request = $smcFunc['db_query']('', '
  570. SELECT id_group, group_name
  571. FROM {db_prefix}membergroups
  572. WHERE ' . $clause . '
  573. AND id_group != {int:admin_group}' . (empty($modSettings['permission_enable_postgroups']) ? '
  574. AND min_posts = {int:min_posts}' : '') . '
  575. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  576. array(
  577. 'admin_group' => 1,
  578. 'min_posts' => -1,
  579. 'newbie_group' => 4,
  580. 'moderator_group' => 3,
  581. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  582. )
  583. );
  584. if (!isset($_REQUEST['groups']) || in_array(-1, $_REQUEST['groups']) || in_array(0, $_REQUEST['groups']))
  585. $groups = array('col' => '', -1 => $txt['membergroups_guests'], 0 => $txt['membergroups_members']);
  586. else
  587. $groups = array('col' => '');
  588. while ($row = $smcFunc['db_fetch_assoc']($request))
  589. $groups[$row['id_group']] = $row['group_name'];
  590. $smcFunc['db_free_result']($request);
  591. // Make sure that every group is represented!
  592. setKeys('rows', $groups);
  593. // Create the table first.
  594. newTable($txt['gr_type_group_perms'], '-', 'all', 100, 'center', 200, 'left');
  595. // Show all the groups
  596. addData($groups);
  597. // Add a separator
  598. addSeparator($txt['board_perms_permission']);
  599. // Certain permissions should not really be shown.
  600. $disabled_permissions = array();
  601. if (empty($modSettings['cal_enabled']))
  602. {
  603. $disabled_permissions[] = 'calendar_view';
  604. $disabled_permissions[] = 'calendar_post';
  605. $disabled_permissions[] = 'calendar_edit_own';
  606. $disabled_permissions[] = 'calendar_edit_any';
  607. }
  608. if (empty($modSettings['warning_settings']) || $modSettings['warning_settings'][0] == 0)
  609. $disabled_permissions[] = 'issue_warning';
  610. if (empty($modSettings['karmaMode']))
  611. $disabled_permissions[] = 'karma_edit';
  612. call_integration_hook('integrate_reports_groupperm', array(&$disabled_permissions));
  613. // Now the big permission fetch!
  614. $request = $smcFunc['db_query']('', '
  615. SELECT id_group, add_deny, permission
  616. FROM {db_prefix}permissions
  617. WHERE ' . $clause . (empty($modSettings['permission_enable_deny']) ? '
  618. AND add_deny = {int:not_denied}' : '') . '
  619. ORDER BY permission',
  620. array(
  621. 'not_denied' => 1,
  622. 'moderator_group' => 3,
  623. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  624. )
  625. );
  626. $lastPermission = null;
  627. $curData = array();
  628. while ($row = $smcFunc['db_fetch_assoc']($request))
  629. {
  630. if (in_array($row['permission'], $disabled_permissions))
  631. continue;
  632. // If this is a new permission flush the last row.
  633. if ($row['permission'] != $lastPermission)
  634. {
  635. // Send the data!
  636. if ($lastPermission !== null)
  637. addData($curData);
  638. // Add the permission name in the left column.
  639. $curData = array('col' => isset($txt['group_perms_name_' . $row['permission']]) ? $txt['group_perms_name_' . $row['permission']] : $row['permission']);
  640. $lastPermission = $row['permission'];
  641. }
  642. // Good stuff - add the permission to the list!
  643. if ($row['add_deny'])
  644. $curData[$row['id_group']] = '<span style="color: darkgreen;">' . $txt['board_perms_allow'] . '</span>';
  645. else
  646. $curData[$row['id_group']] = '<span style="color: red;">' . $txt['board_perms_deny'] . '</span>';
  647. }
  648. $smcFunc['db_free_result']($request);
  649. // Flush the last data!
  650. addData($curData);
  651. }
  652. /**
  653. * Report for showing all the forum staff members - quite a feat!
  654. * functions ending with "Report" are responsible for generating data
  655. * for reporting.
  656. * they are all called from ReportsMain.
  657. * never access the context directly, but use the data handling
  658. * functions to do so.
  659. */
  660. function StaffReport()
  661. {
  662. global $sourcedir, $context, $txt, $smcFunc;
  663. require_once($sourcedir . '/Subs-Members.php');
  664. // Fetch all the board names.
  665. $request = $smcFunc['db_query']('', '
  666. SELECT id_board, name
  667. FROM {db_prefix}boards',
  668. array(
  669. )
  670. );
  671. $boards = array();
  672. while ($row = $smcFunc['db_fetch_assoc']($request))
  673. $boards[$row['id_board']] = $row['name'];
  674. $smcFunc['db_free_result']($request);
  675. // Get every moderator.
  676. $request = $smcFunc['db_query']('', '
  677. SELECT mods.id_board, mods.id_member
  678. FROM {db_prefix}moderators AS mods',
  679. array(
  680. )
  681. );
  682. $moderators = array();
  683. $local_mods = array();
  684. while ($row = $smcFunc['db_fetch_assoc']($request))
  685. {
  686. $moderators[$row['id_member']][] = $row['id_board'];
  687. $local_mods[$row['id_member']] = $row['id_member'];
  688. }
  689. $smcFunc['db_free_result']($request);
  690. // Get any additional boards they can moderate through group-based board moderation
  691. $request = $smcFunc['db_query']('', '
  692. SELECT mem.id_member, modgs.id_board
  693. FROM {db_prefix}members AS mem
  694. INNER JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_group = mem.id_group OR FIND_IN_SET(modgs.id_group, mem.additional_groups) != 0)',
  695. array(
  696. )
  697. );
  698. // Add each board/member to the arrays, but only if they aren't already there
  699. while ($row = $smcFunc['db_fetch_assoc']($request))
  700. {
  701. // Either we don't have them as a moderator at all or at least not as a moderator of this board
  702. if (!array_key_exists($row['id_member'], $moderators) || !in_array($row['id_board'], $moderators[$row['id_member']]))
  703. $moderators[$row['id_member']][] = $row['id_board'];
  704. // We don't have them listed as a moderator yet
  705. if (!array_key_exists($row['id_member'], $local_mods))
  706. $local_mods[$row['id_member']] = $row['id_member'];
  707. }
  708. // Get a list of global moderators (i.e. members with moderation powers).
  709. $global_mods = array_intersect(membersAllowedTo('moderate_board', 0), membersAllowedTo('approve_posts', 0), membersAllowedTo('remove_any', 0), membersAllowedTo('modify_any', 0));
  710. // How about anyone else who is special?
  711. $allStaff = array_merge(membersAllowedTo('admin_forum'), membersAllowedTo('manage_membergroups'), membersAllowedTo('manage_permissions'), $local_mods, $global_mods);
  712. // Make sure everyone is there once - no admin less important than any other!
  713. $allStaff = array_unique($allStaff);
  714. // This is a bit of a cop out - but we're protecting their forum, really!
  715. if (count($allStaff) > 300)
  716. fatal_lang_error('report_error_too_many_staff');
  717. // Get all the possible membergroups!
  718. $request = $smcFunc['db_query']('', '
  719. SELECT id_group, group_name, online_color
  720. FROM {db_prefix}membergroups',
  721. array(
  722. )
  723. );
  724. $groups = array(0 => $txt['full_member']);
  725. while ($row = $smcFunc['db_fetch_assoc']($request))
  726. $groups[$row['id_group']] = empty($row['online_color']) ? $row['group_name'] : '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>';
  727. $smcFunc['db_free_result']($request);
  728. // All the fields we'll show.
  729. $staffSettings = array(
  730. 'position' => $txt['report_staff_position'],
  731. 'moderates' => $txt['report_staff_moderates'],
  732. 'posts' => $txt['report_staff_posts'],
  733. 'last_login' => $txt['report_staff_last_login'],
  734. );
  735. // Do it in columns, it's just easier.
  736. setKeys('cols');
  737. // Get each member!
  738. $request = $smcFunc['db_query']('', '
  739. SELECT id_member, real_name, id_group, posts, last_login
  740. FROM {db_prefix}members
  741. WHERE id_member IN ({array_int:staff_list})
  742. ORDER BY real_name',
  743. array(
  744. 'staff_list' => $allStaff,
  745. )
  746. );
  747. while ($row = $smcFunc['db_fetch_assoc']($request))
  748. {
  749. // Each member gets their own table!.
  750. newTable($row['real_name'], '', 'left', 'auto', 'left', 200, 'center');
  751. // First off, add in the side key.
  752. addData($staffSettings);
  753. // Create the main data array.
  754. $staffData = array(
  755. 'position' => isset($groups[$row['id_group']]) ? $groups[$row['id_group']] : $groups[0],
  756. 'posts' => $row['posts'],
  757. 'last_login' => timeformat($row['last_login']),
  758. 'moderates' => array(),
  759. );
  760. // What do they moderate?
  761. if (in_array($row['id_member'], $global_mods))
  762. $staffData['moderates'] = '<em>' . $txt['report_staff_all_boards'] . '</em>';
  763. elseif (isset($moderators[$row['id_member']]))
  764. {
  765. // Get the names
  766. foreach ($moderators[$row['id_member']] as $board)
  767. if (isset($boards[$board]))
  768. $staffData['moderates'][] = $boards[$board];
  769. $staffData['moderates'] = implode(', ', $staffData['moderates']);
  770. }
  771. else
  772. $staffData['moderates'] = '<em>' . $txt['report_staff_no_boards'] . '</em>';
  773. // Next add the main data.
  774. addData($staffData);
  775. }
  776. $smcFunc['db_free_result']($request);
  777. }
  778. /**
  779. * This function creates a new table of data, most functions will only use it once.
  780. * The core of this file, it creates a new, but empty, table of data in
  781. * context, ready for filling using addData().
  782. * Fills the context variable current_table with the ID of the table created.
  783. * Keeps track of the current table count using context variable table_count.
  784. *
  785. * @param string $title = '' Title to be displayed with this data table.
  786. * @param string $default_value = '' Value to be displayed if a key is missing from a row.
  787. * @param string $shading = 'all' Should the left, top or both (all) parts of the table beshaded?
  788. * @param string $width_normal = 'auto' width of an unshaded column (auto means not defined).
  789. * @param string $align_normal = 'center' alignment of data in an unshaded column.
  790. * @param string $width_shaded = 'auto' width of a shaded column (auto means not defined).
  791. * @param string $align_shaded = 'auto' alignment of data in a shaded column.
  792. */
  793. function newTable($title = '', $default_value = '', $shading = 'all', $width_normal = 'auto', $align_normal = 'center', $width_shaded = 'auto', $align_shaded = 'auto')
  794. {
  795. global $context;
  796. // Set the table count if needed.
  797. if (empty($context['table_count']))
  798. $context['table_count'] = 0;
  799. // Create the table!
  800. $context['tables'][$context['table_count']] = array(
  801. 'title' => $title,
  802. 'default_value' => $default_value,
  803. 'shading' => array(
  804. 'left' => $shading == 'all' || $shading == 'left',
  805. 'top' => $shading == 'all' || $shading == 'top',
  806. ),
  807. 'width' => array(
  808. 'normal' => $width_normal,
  809. 'shaded' => $width_shaded,
  810. ),
  811. 'align' => array(
  812. 'normal' => $align_normal,
  813. 'shaded' => $align_shaded,
  814. ),
  815. 'data' => array(),
  816. );
  817. $context['current_table'] = $context['table_count'];
  818. // Increment the count...
  819. $context['table_count']++;
  820. }
  821. /**
  822. * Adds an array of data into an existing table.
  823. * if there are no existing tables, will create one with default
  824. * attributes.
  825. * if custom_table isn't specified, it will use the last table created,
  826. * if it is specified and doesn't exist the function will return false.
  827. * if a set of keys have been specified, the function will check each
  828. * required key is present in the incoming data. If this data is missing
  829. * the current tables default value will be used.
  830. * if any key in the incoming data begins with '#sep#', the function
  831. * will add a separator accross the table at this point.
  832. * once the incoming data has been sanitized, it is added to the table.
  833. *
  834. * @param array $inc_data
  835. * @param int $custom_table = null
  836. */
  837. function addData($inc_data, $custom_table = null)
  838. {
  839. global $context;
  840. // No tables? Create one even though we are probably already in a bad state!
  841. if (empty($context['table_count']))
  842. newTable();
  843. // Specific table?
  844. if ($custom_table !== null && !isset($context['tables'][$custom_table]))
  845. return false;
  846. elseif ($custom_table !== null)
  847. $table = $custom_table;
  848. else
  849. $table = $context['current_table'];
  850. // If we have keys, sanitise the data...
  851. if (!empty($context['keys']))
  852. {
  853. // Basically, check every key exists!
  854. foreach ($context['keys'] as $key => $dummy)
  855. {
  856. $data[$key] = array(
  857. 'v' => empty($inc_data[$key]) ? $context['tables'][$table]['default_value'] : $inc_data[$key],
  858. );
  859. // Special "hack" the adding separators when doing data by column.
  860. if (substr($key, 0, 5) == '#sep#')
  861. $data[$key]['separator'] = true;
  862. }
  863. }
  864. else
  865. {
  866. $data = $inc_data;
  867. foreach ($data as $key => $value)
  868. {
  869. $data[$key] = array(
  870. 'v' => $value,
  871. );
  872. if (substr($key, 0, 5) == '#sep#')
  873. $data[$key]['separator'] = true;
  874. }
  875. }
  876. // Is it by row?
  877. if (empty($context['key_method']) || $context['key_method'] == 'rows')
  878. {
  879. // Add the data!
  880. $context['tables'][$table]['data'][] = $data;
  881. }
  882. // Otherwise, tricky!
  883. else
  884. {
  885. foreach ($data as $key => $item)
  886. $context['tables'][$table]['data'][$key][] = $item;
  887. }
  888. }
  889. /**
  890. * Add a separator row, only really used when adding data by rows.
  891. *
  892. * @param string $title = ''
  893. * @param string $custom_table = null
  894. *
  895. * @return boolean returns false if there are no tables
  896. */
  897. function addSeparator($title = '', $custom_table = null)
  898. {
  899. global $context;
  900. // No tables - return?
  901. if (empty($context['table_count']))
  902. return;
  903. // Specific table?
  904. if ($custom_table !== null && !isset($context['tables'][$table]))
  905. return false;
  906. elseif ($custom_table !== null)
  907. $table = $custom_table;
  908. else
  909. $table = $context['current_table'];
  910. // Plumb in the separator
  911. $context['tables'][$table]['data'][] = array(0 => array(
  912. 'separator' => true,
  913. 'v' => $title
  914. ));
  915. }
  916. /**
  917. * This does the necessary count of table data before displaying them.
  918. * is (unfortunately) required to create some useful variables for templates.
  919. * foreach data table created, it will count the number of rows and
  920. * columns in the table.
  921. * will also create a max_width variable for the table, to give an
  922. * estimate width for the whole table * * if it can.
  923. */
  924. function finishTables()
  925. {
  926. global $context;
  927. if (empty($context['tables']))
  928. return;
  929. // Loop through each table counting up some basic values, to help with the templating.
  930. foreach ($context['tables'] as $id => $table)
  931. {
  932. $context['tables'][$id]['id'] = $id;
  933. $context['tables'][$id]['row_count'] = count($table['data']);
  934. $curElement = current($table['data']);
  935. $context['tables'][$id]['column_count'] = count($curElement);
  936. // Work out the rough width - for templates like the print template. Without this we might get funny tables.
  937. if ($table['shading']['left'] && $table['width']['shaded'] != 'auto' && $table['width']['normal'] != 'auto')
  938. $context['tables'][$id]['max_width'] = $table['width']['shaded'] + ($context['tables'][$id]['column_count'] - 1) * $table['width']['normal'];
  939. elseif ($table['width']['normal'] != 'auto')
  940. $context['tables'][$id]['max_width'] = $context['tables'][$id]['column_count'] * $table['width']['normal'];
  941. else
  942. $context['tables'][$id]['max_width'] = 'auto';
  943. }
  944. }
  945. /**
  946. * Set the keys in use by the tables - these ensure entries MUST exist if the data isn't sent.
  947. *
  948. * sets the current set of "keys" expected in each data array passed to
  949. * addData. It also sets the way we are adding data to the data table.
  950. * method specifies whether the data passed to addData represents a new
  951. * column, or a new row.
  952. * keys is an array whose keys are the keys for data being passed to
  953. * addData().
  954. * if reverse is set to true, then the values of the variable "keys"
  955. * are used as oppossed to the keys(!
  956. *
  957. * @param string $method = 'rows' rows or cols
  958. * @param array $keys = array()
  959. * @param bool $reverse = false
  960. */
  961. function setKeys($method = 'rows', $keys = array(), $reverse = false)
  962. {
  963. global $context;
  964. // Do we want to use the keys of the keys as the keys? :P
  965. if ($reverse)
  966. $context['keys'] = array_flip($keys);
  967. else
  968. $context['keys'] = $keys;
  969. // Rows or columns?
  970. $context['key_method'] = $method == 'rows' ? 'rows' : 'cols';
  971. }
  972. ?>