Reports.php 31 KB

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