Reports.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. 'num_topics' => $txt['board_num_topics'],
  165. 'num_posts' => $txt['board_num_posts'],
  166. 'count_posts' => $txt['board_count_posts'],
  167. 'theme' => $txt['board_theme'],
  168. 'override_theme' => $txt['board_override_theme'],
  169. 'profile' => $txt['board_profile'],
  170. 'moderators' => $txt['board_moderators'],
  171. 'moderator_groups' => $txt['board_moderator_groups'],
  172. 'groups' => $txt['board_groups'],
  173. );
  174. if (!empty($modSettings['deny_boards_access']))
  175. $boardSettings['disallowed_groups'] = $txt['board_disallowed_groups'];
  176. // Do it in columns, it's just easier.
  177. setKeys('cols');
  178. // Go through each board!
  179. $request = $smcFunc['db_query']('order_by_board_order', '
  180. 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,
  181. c.name AS cat_name, IFNULL(par.name, {string:text_none}) AS parent_name, IFNULL(th.value, {string:text_none}) AS theme_name
  182. FROM {db_prefix}boards AS b
  183. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  184. LEFT JOIN {db_prefix}boards AS par ON (par.id_board = b.id_parent)
  185. LEFT JOIN {db_prefix}themes AS th ON (th.id_theme = b.id_theme AND th.variable = {string:name})',
  186. array(
  187. 'name' => 'name',
  188. 'text_none' => $txt['none'],
  189. )
  190. );
  191. $boards = array(0 => array('name' => $txt['global_boards']));
  192. while ($row = $smcFunc['db_fetch_assoc']($request))
  193. {
  194. // Each board has it's own table.
  195. newTable($row['name'], '', 'left', 'auto', 'left', 200, 'left');
  196. // First off, add in the side key.
  197. addData($boardSettings);
  198. // Format the profile name.
  199. $profile_name = $context['profiles'][$row['id_profile']]['name'];
  200. // Create the main data array.
  201. $boardData = array(
  202. 'category' => $row['cat_name'],
  203. 'parent' => $row['parent_name'],
  204. 'num_posts' => $row['num_posts'],
  205. 'num_topics' => $row['num_topics'],
  206. 'count_posts' => empty($row['count_posts']) ? $txt['yes'] : $txt['no'],
  207. 'theme' => $row['theme_name'],
  208. 'profile' => $profile_name,
  209. 'override_theme' => $row['override_theme'] ? $txt['yes'] : $txt['no'],
  210. 'moderators' => empty($moderators[$row['id_board']]) ? $txt['none'] : implode(', ', $moderators[$row['id_board']]),
  211. 'moderator_groups' => empty($moderator_groups[$row['id_board']]) ? $txt['none'] : implode(', ', $moderator_groups[$row['id_board']]),
  212. );
  213. // Work out the membergroups who can and cannot access it (but only if enabled).
  214. $allowedGroups = explode(',', $row['member_groups']);
  215. foreach ($allowedGroups as $key => $group)
  216. {
  217. if (isset($groups[$group]))
  218. $allowedGroups[$key] = $groups[$group];
  219. else
  220. unset($allowedGroups[$key]);
  221. }
  222. $boardData['groups'] = implode(', ', $allowedGroups);
  223. if (!empty($modSettings['deny_boards_access']))
  224. {
  225. $disallowedGroups = explode(',', $row['deny_member_groups']);
  226. foreach ($disallowedGroups as $key => $group)
  227. {
  228. if (isset($groups[$group]))
  229. $disallowedGroups[$key] = $groups[$group];
  230. else
  231. unset($disallowedGroups[$key]);
  232. }
  233. $boardData['disallowed_groups'] = implode(', ', $disallowedGroups);
  234. }
  235. // Next add the main data.
  236. addData($boardData);
  237. }
  238. $smcFunc['db_free_result']($request);
  239. }
  240. /**
  241. * Generate a report on the current permissions by board and membergroup.
  242. * functions ending with "Report" are responsible for generating data
  243. * for reporting.
  244. * they are all called from ReportsMain.
  245. * never access the context directly, but use the data handling
  246. * functions to do so.
  247. */
  248. function BoardPermissionsReport()
  249. {
  250. global $context, $txt, $modSettings, $smcFunc;
  251. // Get as much memory as possible as this can be big.
  252. setMemoryLimit('256M');
  253. if (isset($_REQUEST['boards']))
  254. {
  255. if (!is_array($_REQUEST['boards']))
  256. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  257. foreach ($_REQUEST['boards'] as $k => $dummy)
  258. $_REQUEST['boards'][$k] = (int) $dummy;
  259. $board_clause = 'id_board IN ({array_int:boards})';
  260. }
  261. else
  262. $board_clause = '1=1';
  263. if (isset($_REQUEST['groups']))
  264. {
  265. if (!is_array($_REQUEST['groups']))
  266. $_REQUEST['groups'] = explode(',', $_REQUEST['groups']);
  267. foreach ($_REQUEST['groups'] as $k => $dummy)
  268. $_REQUEST['groups'][$k] = (int) $dummy;
  269. $group_clause = 'id_group IN ({array_int:groups})';
  270. }
  271. else
  272. $group_clause = '1=1';
  273. // Fetch all the board names.
  274. $request = $smcFunc['db_query']('', '
  275. SELECT id_board, name, id_profile
  276. FROM {db_prefix}boards
  277. WHERE ' . $board_clause . '
  278. ORDER BY id_board',
  279. array(
  280. 'boards' => isset($_REQUEST['boards']) ? $_REQUEST['boards'] : array(),
  281. )
  282. );
  283. $profiles = array();
  284. while ($row = $smcFunc['db_fetch_assoc']($request))
  285. {
  286. $boards[$row['id_board']] = array(
  287. 'name' => $row['name'],
  288. 'profile' => $row['id_profile'],
  289. 'mod_groups' => array(),
  290. );
  291. $profiles[] = $row['id_profile'];
  292. }
  293. $smcFunc['db_free_result']($request);
  294. // Get the ids of any groups allowed to moderate this board
  295. // Limit it to any boards and/or groups we're looking at
  296. $request = $smcFunc['db_query']('', '
  297. SELECT id_board, id_group
  298. FROM {db_prefix}moderator_groups
  299. WHERE ' . $board_clause .' AND ' . $group_clause,
  300. array(
  301. )
  302. );
  303. while ($row = $smcFunc['db_fetch_assoc']($request))
  304. {
  305. $boards[$row['id_board']]['mod_groups'][] = $row['id_group'];
  306. }
  307. $smcFunc['db_free_result']($request);
  308. // Get all the possible membergroups, except admin!
  309. $request = $smcFunc['db_query']('', '
  310. SELECT id_group, group_name
  311. FROM {db_prefix}membergroups
  312. WHERE ' . $group_clause . '
  313. AND id_group != {int:admin_group}' . (empty($modSettings['permission_enable_postgroups']) ? '
  314. AND min_posts = {int:min_posts}' : '') . '
  315. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  316. array(
  317. 'admin_group' => 1,
  318. 'min_posts' => -1,
  319. 'newbie_group' => 4,
  320. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  321. )
  322. );
  323. if (!isset($_REQUEST['groups']) || in_array(-1, $_REQUEST['groups']) || in_array(0, $_REQUEST['groups']))
  324. $member_groups = array('col' => '', -1 => $txt['membergroups_guests'], 0 => $txt['membergroups_members']);
  325. else
  326. $member_groups = array('col' => '');
  327. while ($row = $smcFunc['db_fetch_assoc']($request))
  328. $member_groups[$row['id_group']] = $row['group_name'];
  329. $smcFunc['db_free_result']($request);
  330. // Make sure that every group is represented - plus in rows!
  331. setKeys('rows', $member_groups);
  332. // Cache every permission setting, to make sure we don't miss any allows.
  333. $permissions = array();
  334. $board_permissions = array();
  335. $request = $smcFunc['db_query']('', '
  336. SELECT id_profile, id_group, add_deny, permission
  337. FROM {db_prefix}board_permissions
  338. WHERE id_profile IN ({array_int:profile_list})
  339. AND ' . $group_clause . (empty($modSettings['permission_enable_deny']) ? '
  340. AND add_deny = {int:not_deny}' : '') . '
  341. ORDER BY id_profile, permission',
  342. array(
  343. 'profile_list' => $profiles,
  344. 'not_deny' => 1,
  345. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  346. )
  347. );
  348. while ($row = $smcFunc['db_fetch_assoc']($request))
  349. {
  350. foreach ($boards as $id => $board)
  351. if ($board['profile'] == $row['id_profile'])
  352. $board_permissions[$id][$row['id_group']][$row['permission']] = $row['add_deny'];
  353. // Make sure we get every permission.
  354. if (!isset($permissions[$row['permission']]))
  355. {
  356. // This will be reused on other boards.
  357. $permissions[$row['permission']] = array(
  358. 'title' => isset($txt['board_perms_name_' . $row['permission']]) ? $txt['board_perms_name_' . $row['permission']] : $row['permission'],
  359. );
  360. }
  361. }
  362. $smcFunc['db_free_result']($request);
  363. // Now cycle through the board permissions array... lots to do ;)
  364. foreach ($board_permissions as $board => $groups)
  365. {
  366. // Create the table for this board first.
  367. newTable($boards[$board]['name'], 'x', 'all', 100, 'center', 200, 'left');
  368. // Add the header row - shows all the membergroups.
  369. addData($member_groups);
  370. // Add the separator.
  371. addSeparator($txt['board_perms_permission']);
  372. // Here cycle through all the detected permissions.
  373. foreach ($permissions as $ID_PERM => $perm_info)
  374. {
  375. // Is this identical to the global?
  376. $identicalGlobal = $board == 0 ? false : true;
  377. // Default data for this row.
  378. $curData = array('col' => $perm_info['title']);
  379. // Now cycle each membergroup in this set of permissions.
  380. foreach ($member_groups as $id_group => $name)
  381. {
  382. // Don't overwrite the key column!
  383. if ($id_group === 'col')
  384. continue;
  385. $group_permissions = isset($groups[$id_group]) ? $groups[$id_group] : array();
  386. // Do we have any data for this group?
  387. if (isset($group_permissions[$ID_PERM]))
  388. {
  389. // Set the data for this group to be the local permission.
  390. $curData[$id_group] = $group_permissions[$ID_PERM];
  391. }
  392. // Is it inherited from Moderator?
  393. elseif (in_array($id_group, $boards[$board]['mod_groups']) && !empty($groups[3]) && isset($groups[3][$ID_PERM]))
  394. {
  395. $curData[$id_group] = $groups[3][$ID_PERM];
  396. }
  397. // Otherwise means it's set to disallow..
  398. else
  399. {
  400. $curData[$id_group] = 'x';
  401. }
  402. // Now actually make the data for the group look right.
  403. if (empty($curData[$id_group]))
  404. $curData[$id_group] = '<span style="color: red;">' . $txt['board_perms_deny'] . '</span>';
  405. elseif ($curData[$id_group] == 1)
  406. $curData[$id_group] = '<span style="color: darkgreen;">' . $txt['board_perms_allow'] . '</span>';
  407. else
  408. $curData[$id_group] = 'x';
  409. // Embolden those permissions different from global (makes it a lot easier!)
  410. if (@$board_permissions[0][$id_group][$ID_PERM] != @$group_permissions[$ID_PERM])
  411. $curData[$id_group] = '<strong>' . $curData[$id_group] . '</strong>';
  412. }
  413. // Now add the data for this permission.
  414. addData($curData);
  415. }
  416. }
  417. }
  418. /**
  419. * Show what the membergroups are made of.
  420. * functions ending with "Report" are responsible for generating data
  421. * for reporting.
  422. * they are all called from ReportsMain.
  423. * never access the context directly, but use the data handling
  424. * functions to do so.
  425. */
  426. function MemberGroupsReport()
  427. {
  428. global $context, $txt, $settings, $modSettings, $smcFunc;
  429. // Fetch all the board names.
  430. $request = $smcFunc['db_query']('', '
  431. SELECT id_board, name, member_groups, id_profile, deny_member_groups
  432. FROM {db_prefix}boards',
  433. array(
  434. )
  435. );
  436. while ($row = $smcFunc['db_fetch_assoc']($request))
  437. {
  438. if (trim($row['member_groups']) == '')
  439. $groups = array(1);
  440. else
  441. $groups = array_merge(array(1), explode(',', $row['member_groups']));
  442. if (trim($row['deny_member_groups']) == '')
  443. $denyGroups = array();
  444. else
  445. $denyGroups = explode(',', $row['deny_member_groups']);
  446. $boards[$row['id_board']] = array(
  447. 'id' => $row['id_board'],
  448. 'name' => $row['name'],
  449. 'profile' => $row['id_profile'],
  450. 'groups' => $groups,
  451. 'deny_groups' => $denyGroups,
  452. );
  453. }
  454. $smcFunc['db_free_result']($request);
  455. // Standard settings.
  456. $mgSettings = array(
  457. 'name' => '',
  458. '#sep#1' => $txt['member_group_settings'],
  459. 'color' => $txt['member_group_color'],
  460. 'min_posts' => $txt['member_group_min_posts'],
  461. 'max_messages' => $txt['member_group_max_messages'],
  462. 'icons' => $txt['member_group_icons'],
  463. '#sep#2' => $txt['member_group_access'],
  464. );
  465. // Add on the boards!
  466. foreach ($boards as $board)
  467. $mgSettings['board_' . $board['id']] = $board['name'];
  468. // Add all the membergroup settings, plus we'll be adding in columns!
  469. setKeys('cols', $mgSettings);
  470. // Only one table this time!
  471. newTable($txt['gr_type_member_groups'], '-', 'all', 100, 'center', 200, 'left');
  472. // Get the shaded column in.
  473. addData($mgSettings);
  474. // Now start cycling the membergroups!
  475. $request = $smcFunc['db_query']('', '
  476. SELECT mg.id_group, mg.group_name, mg.online_color, mg.min_posts, mg.max_messages, mg.icons,
  477. CASE WHEN bp.permission IS NOT NULL OR mg.id_group = {int:admin_group} THEN 1 ELSE 0 END AS can_moderate
  478. FROM {db_prefix}membergroups AS mg
  479. 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})
  480. ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
  481. array(
  482. 'admin_group' => 1,
  483. 'default_profile' => 1,
  484. 'newbie_group' => 4,
  485. 'moderate_board' => 'moderate_board',
  486. )
  487. );
  488. // Cache them so we get regular members too.
  489. $rows = array(
  490. array(
  491. 'id_group' => -1,
  492. 'group_name' => $txt['membergroups_guests'],
  493. 'online_color' => '',
  494. 'min_posts' => -1,
  495. 'max_messages' => null,
  496. 'icons' => ''
  497. ),
  498. array(
  499. 'id_group' => 0,
  500. 'group_name' => $txt['membergroups_members'],
  501. 'online_color' => '',
  502. 'min_posts' => -1,
  503. 'max_messages' => null,
  504. 'icons' => ''
  505. ),
  506. );
  507. while ($row = $smcFunc['db_fetch_assoc']($request))
  508. $rows[] = $row;
  509. $smcFunc['db_free_result']($request);
  510. foreach ($rows as $row)
  511. {
  512. $row['icons'] = explode('#', $row['icons']);
  513. $group = array(
  514. 'name' => $row['group_name'],
  515. 'color' => empty($row['online_color']) ? '-' : '<span style="color: ' . $row['online_color'] . ';">' . $row['online_color'] . '</span>',
  516. 'min_posts' => $row['min_posts'] == -1 ? 'N/A' : $row['min_posts'],
  517. 'max_messages' => $row['max_messages'],
  518. 'icons' => !empty($row['icons'][0]) && !empty($row['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['icons'][1] . '" alt="*" />', $row['icons'][0]) : '',
  519. );
  520. // Board permissions.
  521. foreach ($boards as $board)
  522. $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');
  523. addData($group);
  524. }
  525. }
  526. /**
  527. * Show the large variety of group permissions assigned to each membergroup.
  528. * functions ending with "Report" are responsible for generating data
  529. * for reporting.
  530. * they are all called from ReportsMain.
  531. * never access the context directly, but use the data handling
  532. * functions to do so.
  533. */
  534. function GroupPermissionsReport()
  535. {
  536. global $context, $txt, $modSettings, $smcFunc;
  537. if (isset($_REQUEST['groups']))
  538. {
  539. if (!is_array($_REQUEST['groups']))
  540. $_REQUEST['groups'] = explode(',', $_REQUEST['groups']);
  541. foreach ($_REQUEST['groups'] as $k => $dummy)
  542. $_REQUEST['groups'][$k] = (int) $dummy;
  543. $_REQUEST['groups'] = array_diff($_REQUEST['groups'], array(3));
  544. $clause = 'id_group IN ({array_int:groups})';
  545. }
  546. else
  547. $clause = 'id_group != {int:moderator_group}';
  548. // Get all the possible membergroups, except admin!
  549. $request = $smcFunc['db_query']('', '
  550. SELECT id_group, group_name
  551. FROM {db_prefix}membergroups
  552. WHERE ' . $clause . '
  553. AND id_group != {int:admin_group}' . (empty($modSettings['permission_enable_postgroups']) ? '
  554. AND min_posts = {int:min_posts}' : '') . '
  555. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  556. array(
  557. 'admin_group' => 1,
  558. 'min_posts' => -1,
  559. 'newbie_group' => 4,
  560. 'moderator_group' => 3,
  561. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  562. )
  563. );
  564. if (!isset($_REQUEST['groups']) || in_array(-1, $_REQUEST['groups']) || in_array(0, $_REQUEST['groups']))
  565. $groups = array('col' => '', -1 => $txt['membergroups_guests'], 0 => $txt['membergroups_members']);
  566. else
  567. $groups = array('col' => '');
  568. while ($row = $smcFunc['db_fetch_assoc']($request))
  569. $groups[$row['id_group']] = $row['group_name'];
  570. $smcFunc['db_free_result']($request);
  571. // Make sure that every group is represented!
  572. setKeys('rows', $groups);
  573. // Create the table first.
  574. newTable($txt['gr_type_group_perms'], '-', 'all', 100, 'center', 200, 'left');
  575. // Show all the groups
  576. addData($groups);
  577. // Add a separator
  578. addSeparator($txt['board_perms_permission']);
  579. // Now the big permission fetch!
  580. $request = $smcFunc['db_query']('', '
  581. SELECT id_group, add_deny, permission
  582. FROM {db_prefix}permissions
  583. WHERE ' . $clause . (empty($modSettings['permission_enable_deny']) ? '
  584. AND add_deny = {int:not_denied}' : '') . '
  585. ORDER BY permission',
  586. array(
  587. 'not_denied' => 1,
  588. 'moderator_group' => 3,
  589. 'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
  590. )
  591. );
  592. $lastPermission = null;
  593. $curData = array();
  594. while ($row = $smcFunc['db_fetch_assoc']($request))
  595. {
  596. // If this is a new permission flush the last row.
  597. if ($row['permission'] != $lastPermission)
  598. {
  599. // Send the data!
  600. if ($lastPermission !== null)
  601. addData($curData);
  602. // Add the permission name in the left column.
  603. $curData = array('col' => isset($txt['group_perms_name_' . $row['permission']]) ? $txt['group_perms_name_' . $row['permission']] : $row['permission']);
  604. $lastPermission = $row['permission'];
  605. }
  606. // Good stuff - add the permission to the list!
  607. if ($row['add_deny'])
  608. $curData[$row['id_group']] = '<span style="color: darkgreen;">' . $txt['board_perms_allow'] . '</span>';
  609. else
  610. $curData[$row['id_group']] = '<span style="color: red;">' . $txt['board_perms_deny'] . '</span>';
  611. }
  612. $smcFunc['db_free_result']($request);
  613. // Flush the last data!
  614. addData($curData);
  615. }
  616. /**
  617. * Report for showing all the forum staff members - quite a feat!
  618. * functions ending with "Report" are responsible for generating data
  619. * for reporting.
  620. * they are all called from ReportsMain.
  621. * never access the context directly, but use the data handling
  622. * functions to do so.
  623. */
  624. function StaffReport()
  625. {
  626. global $sourcedir, $context, $txt, $smcFunc;
  627. require_once($sourcedir . '/Subs-Members.php');
  628. // Fetch all the board names.
  629. $request = $smcFunc['db_query']('', '
  630. SELECT id_board, name
  631. FROM {db_prefix}boards',
  632. array(
  633. )
  634. );
  635. $boards = array();
  636. while ($row = $smcFunc['db_fetch_assoc']($request))
  637. $boards[$row['id_board']] = $row['name'];
  638. $smcFunc['db_free_result']($request);
  639. // Get every moderator.
  640. $request = $smcFunc['db_query']('', '
  641. SELECT mods.id_board, mods.id_member
  642. FROM {db_prefix}moderators AS mods',
  643. array(
  644. )
  645. );
  646. $moderators = array();
  647. $local_mods = array();
  648. while ($row = $smcFunc['db_fetch_assoc']($request))
  649. {
  650. $moderators[$row['id_member']][] = $row['id_board'];
  651. $local_mods[$row['id_member']] = $row['id_member'];
  652. }
  653. $smcFunc['db_free_result']($request);
  654. // Get any additional boards they can moderate through group-based board moderation
  655. $request = $smcFunc['db_query']('', '
  656. SELECT mem.id_member, modgs.id_board
  657. FROM {db_prefix}members AS mem
  658. 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)',
  659. array(
  660. )
  661. );
  662. // Add each board/member to the arrays, but only if they aren't already there
  663. while ($row = $smcFunc['db_fetch_assoc']($request))
  664. {
  665. // Either we don't have them as a moderator at all or at least not as a moderator of this board
  666. if (!array_key_exists($row['id_member'], $moderators) || !in_array($row['id_board'], $moderators[$row['id_member']]))
  667. $moderators[$row['id_member']][] = $row['id_board'];
  668. // We don't have them listed as a moderator yet
  669. if (!array_key_exists($row['id_member'], $local_mods))
  670. $local_mods[$row['id_member']] = $row['id_member'];
  671. }
  672. // Get a list of global moderators (i.e. members with moderation powers).
  673. $global_mods = array_intersect(membersAllowedTo('moderate_board', 0), membersAllowedTo('approve_posts', 0), membersAllowedTo('remove_any', 0), membersAllowedTo('modify_any', 0));
  674. // How about anyone else who is special?
  675. $allStaff = array_merge(membersAllowedTo('admin_forum'), membersAllowedTo('manage_membergroups'), membersAllowedTo('manage_permissions'), $local_mods, $global_mods);
  676. // Make sure everyone is there once - no admin less important than any other!
  677. $allStaff = array_unique($allStaff);
  678. // This is a bit of a cop out - but we're protecting their forum, really!
  679. if (count($allStaff) > 300)
  680. fatal_lang_error('report_error_too_many_staff');
  681. // Get all the possible membergroups!
  682. $request = $smcFunc['db_query']('', '
  683. SELECT id_group, group_name, online_color
  684. FROM {db_prefix}membergroups',
  685. array(
  686. )
  687. );
  688. $groups = array(0 => $txt['full_member']);
  689. while ($row = $smcFunc['db_fetch_assoc']($request))
  690. $groups[$row['id_group']] = empty($row['online_color']) ? $row['group_name'] : '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>';
  691. $smcFunc['db_free_result']($request);
  692. // All the fields we'll show.
  693. $staffSettings = array(
  694. 'position' => $txt['report_staff_position'],
  695. 'moderates' => $txt['report_staff_moderates'],
  696. 'posts' => $txt['report_staff_posts'],
  697. 'last_login' => $txt['report_staff_last_login'],
  698. );
  699. // Do it in columns, it's just easier.
  700. setKeys('cols');
  701. // Get each member!
  702. $request = $smcFunc['db_query']('', '
  703. SELECT id_member, real_name, id_group, posts, last_login
  704. FROM {db_prefix}members
  705. WHERE id_member IN ({array_int:staff_list})
  706. ORDER BY real_name',
  707. array(
  708. 'staff_list' => $allStaff,
  709. )
  710. );
  711. while ($row = $smcFunc['db_fetch_assoc']($request))
  712. {
  713. // Each member gets their own table!.
  714. newTable($row['real_name'], '', 'left', 'auto', 'left', 200, 'center');
  715. // First off, add in the side key.
  716. addData($staffSettings);
  717. // Create the main data array.
  718. $staffData = array(
  719. 'position' => isset($groups[$row['id_group']]) ? $groups[$row['id_group']] : $groups[0],
  720. 'posts' => $row['posts'],
  721. 'last_login' => timeformat($row['last_login']),
  722. 'moderates' => array(),
  723. );
  724. // What do they moderate?
  725. if (in_array($row['id_member'], $global_mods))
  726. $staffData['moderates'] = '<em>' . $txt['report_staff_all_boards'] . '</em>';
  727. elseif (isset($moderators[$row['id_member']]))
  728. {
  729. // Get the names
  730. foreach ($moderators[$row['id_member']] as $board)
  731. if (isset($boards[$board]))
  732. $staffData['moderates'][] = $boards[$board];
  733. $staffData['moderates'] = implode(', ', $staffData['moderates']);
  734. }
  735. else
  736. $staffData['moderates'] = '<em>' . $txt['report_staff_no_boards'] . '</em>';
  737. // Next add the main data.
  738. addData($staffData);
  739. }
  740. $smcFunc['db_free_result']($request);
  741. }
  742. /**
  743. * This function creates a new table of data, most functions will only use it once.
  744. * The core of this file, it creates a new, but empty, table of data in
  745. * context, ready for filling using addData().
  746. * Fills the context variable current_table with the ID of the table created.
  747. * Keeps track of the current table count using context variable table_count.
  748. *
  749. * @param string $title = '' Title to be displayed with this data table.
  750. * @param string $default_value = '' Value to be displayed if a key is missing from a row.
  751. * @param string $shading = 'all' Should the left, top or both (all) parts of the table beshaded?
  752. * @param string $width_normal = 'auto' width of an unshaded column (auto means not defined).
  753. * @param string $align_normal = 'center' alignment of data in an unshaded column.
  754. * @param string $width_shaded = 'auto' width of a shaded column (auto means not defined).
  755. * @param string $align_shaded = 'auto' alignment of data in a shaded column.
  756. */
  757. function newTable($title = '', $default_value = '', $shading = 'all', $width_normal = 'auto', $align_normal = 'center', $width_shaded = 'auto', $align_shaded = 'auto')
  758. {
  759. global $context;
  760. // Set the table count if needed.
  761. if (empty($context['table_count']))
  762. $context['table_count'] = 0;
  763. // Create the table!
  764. $context['tables'][$context['table_count']] = array(
  765. 'title' => $title,
  766. 'default_value' => $default_value,
  767. 'shading' => array(
  768. 'left' => $shading == 'all' || $shading == 'left',
  769. 'top' => $shading == 'all' || $shading == 'top',
  770. ),
  771. 'width' => array(
  772. 'normal' => $width_normal,
  773. 'shaded' => $width_shaded,
  774. ),
  775. 'align' => array(
  776. 'normal' => $align_normal,
  777. 'shaded' => $align_shaded,
  778. ),
  779. 'data' => array(),
  780. );
  781. $context['current_table'] = $context['table_count'];
  782. // Increment the count...
  783. $context['table_count']++;
  784. }
  785. /**
  786. * Adds an array of data into an existing table.
  787. * if there are no existing tables, will create one with default
  788. * attributes.
  789. * if custom_table isn't specified, it will use the last table created,
  790. * if it is specified and doesn't exist the function will return false.
  791. * if a set of keys have been specified, the function will check each
  792. * required key is present in the incoming data. If this data is missing
  793. * the current tables default value will be used.
  794. * if any key in the incoming data begins with '#sep#', the function
  795. * will add a separator accross the table at this point.
  796. * once the incoming data has been sanitized, it is added to the table.
  797. *
  798. * @param array $inc_data
  799. * @param int $custom_table = null
  800. */
  801. function addData($inc_data, $custom_table = null)
  802. {
  803. global $context;
  804. // No tables? Create one even though we are probably already in a bad state!
  805. if (empty($context['table_count']))
  806. newTable();
  807. // Specific table?
  808. if ($custom_table !== null && !isset($context['tables'][$custom_table]))
  809. return false;
  810. elseif ($custom_table !== null)
  811. $table = $custom_table;
  812. else
  813. $table = $context['current_table'];
  814. // If we have keys, sanitise the data...
  815. if (!empty($context['keys']))
  816. {
  817. // Basically, check every key exists!
  818. foreach ($context['keys'] as $key => $dummy)
  819. {
  820. $data[$key] = array(
  821. 'v' => empty($inc_data[$key]) ? $context['tables'][$table]['default_value'] : $inc_data[$key],
  822. );
  823. // Special "hack" the adding separators when doing data by column.
  824. if (substr($key, 0, 5) == '#sep#')
  825. $data[$key]['separator'] = true;
  826. }
  827. }
  828. else
  829. {
  830. $data = $inc_data;
  831. foreach ($data as $key => $value)
  832. {
  833. $data[$key] = array(
  834. 'v' => $value,
  835. );
  836. if (substr($key, 0, 5) == '#sep#')
  837. $data[$key]['separator'] = true;
  838. }
  839. }
  840. // Is it by row?
  841. if (empty($context['key_method']) || $context['key_method'] == 'rows')
  842. {
  843. // Add the data!
  844. $context['tables'][$table]['data'][] = $data;
  845. }
  846. // Otherwise, tricky!
  847. else
  848. {
  849. foreach ($data as $key => $item)
  850. $context['tables'][$table]['data'][$key][] = $item;
  851. }
  852. }
  853. /**
  854. * Add a separator row, only really used when adding data by rows.
  855. *
  856. * @param string $title = ''
  857. * @param string $custom_table = null
  858. *
  859. * @return boolean returns false if there are no tables
  860. */
  861. function addSeparator($title = '', $custom_table = null)
  862. {
  863. global $context;
  864. // No tables - return?
  865. if (empty($context['table_count']))
  866. return;
  867. // Specific table?
  868. if ($custom_table !== null && !isset($context['tables'][$table]))
  869. return false;
  870. elseif ($custom_table !== null)
  871. $table = $custom_table;
  872. else
  873. $table = $context['current_table'];
  874. // Plumb in the separator
  875. $context['tables'][$table]['data'][] = array(0 => array(
  876. 'separator' => true,
  877. 'v' => $title
  878. ));
  879. }
  880. /**
  881. * This does the necessary count of table data before displaying them.
  882. * is (unfortunately) required to create some useful variables for templates.
  883. * foreach data table created, it will count the number of rows and
  884. * columns in the table.
  885. * will also create a max_width variable for the table, to give an
  886. * estimate width for the whole table * * if it can.
  887. */
  888. function finishTables()
  889. {
  890. global $context;
  891. if (empty($context['tables']))
  892. return;
  893. // Loop through each table counting up some basic values, to help with the templating.
  894. foreach ($context['tables'] as $id => $table)
  895. {
  896. $context['tables'][$id]['id'] = $id;
  897. $context['tables'][$id]['row_count'] = count($table['data']);
  898. $curElement = current($table['data']);
  899. $context['tables'][$id]['column_count'] = count($curElement);
  900. // Work out the rough width - for templates like the print template. Without this we might get funny tables.
  901. if ($table['shading']['left'] && $table['width']['shaded'] != 'auto' && $table['width']['normal'] != 'auto')
  902. $context['tables'][$id]['max_width'] = $table['width']['shaded'] + ($context['tables'][$id]['column_count'] - 1) * $table['width']['normal'];
  903. elseif ($table['width']['normal'] != 'auto')
  904. $context['tables'][$id]['max_width'] = $context['tables'][$id]['column_count'] * $table['width']['normal'];
  905. else
  906. $context['tables'][$id]['max_width'] = 'auto';
  907. }
  908. }
  909. /**
  910. * Set the keys in use by the tables - these ensure entries MUST exist if the data isn't sent.
  911. *
  912. * sets the current set of "keys" expected in each data array passed to
  913. * addData. It also sets the way we are adding data to the data table.
  914. * method specifies whether the data passed to addData represents a new
  915. * column, or a new row.
  916. * keys is an array whose keys are the keys for data being passed to
  917. * addData().
  918. * if reverse is set to true, then the values of the variable "keys"
  919. * are used as oppossed to the keys(!
  920. *
  921. * @param string $method = 'rows' rows or cols
  922. * @param array $keys = array()
  923. * @param bool $reverse = false
  924. */
  925. function setKeys($method = 'rows', $keys = array(), $reverse = false)
  926. {
  927. global $context;
  928. // Do we want to use the keys of the keys as the keys? :P
  929. if ($reverse)
  930. $context['keys'] = array_flip($keys);
  931. else
  932. $context['keys'] = $keys;
  933. // Rows or columns?
  934. $context['key_method'] = $method == 'rows' ? 'rows' : 'cols';
  935. }
  936. ?>