Reports.php 33 KB

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