Reports.php 33 KB

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