Reports.php 31 KB

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