Reports.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. @ini_set('memory_limit', '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. while ($row = $smcFunc['db_fetch_assoc']($request))
  528. {
  529. // If this is a new permission flush the last row.
  530. if ($row['permission'] != $lastPermission)
  531. {
  532. // Send the data!
  533. if ($lastPermission !== null)
  534. addData($curData);
  535. // Add the permission name in the left column.
  536. $curData = array('col' => isset($txt['group_perms_name_' . $row['permission']]) ? $txt['group_perms_name_' . $row['permission']] : $row['permission']);
  537. $lastPermission = $row['permission'];
  538. }
  539. // Good stuff - add the permission to the list!
  540. if ($row['add_deny'])
  541. $curData[$row['id_group']] = '<span style="color: darkgreen;">' . $txt['board_perms_allow'] . '</span>';
  542. else
  543. $curData[$row['id_group']] = '<span style="color: red;">' . $txt['board_perms_deny'] . '</span>';
  544. }
  545. $smcFunc['db_free_result']($request);
  546. // Flush the last data!
  547. addData($curData);
  548. }
  549. /**
  550. * Report for showing all the forum staff members - quite a feat!
  551. * functions ending with "Report" are responsible for generating data
  552. * for reporting.
  553. * they are all called from ReportsMain.
  554. * never access the context directly, but use the data handling
  555. * functions to do so.
  556. */
  557. function StaffReport()
  558. {
  559. global $sourcedir, $context, $txt, $smcFunc;
  560. require_once($sourcedir . '/Subs-Members.php');
  561. // Fetch all the board names.
  562. $request = $smcFunc['db_query']('', '
  563. SELECT id_board, name
  564. FROM {db_prefix}boards',
  565. array(
  566. )
  567. );
  568. $boards = array();
  569. while ($row = $smcFunc['db_fetch_assoc']($request))
  570. $boards[$row['id_board']] = $row['name'];
  571. $smcFunc['db_free_result']($request);
  572. // Get every moderator.
  573. $request = $smcFunc['db_query']('', '
  574. SELECT mods.id_board, mods.id_member
  575. FROM {db_prefix}moderators AS mods',
  576. array(
  577. )
  578. );
  579. $moderators = array();
  580. $local_mods = array();
  581. while ($row = $smcFunc['db_fetch_assoc']($request))
  582. {
  583. $moderators[$row['id_member']][] = $row['id_board'];
  584. $local_mods[$row['id_member']] = $row['id_member'];
  585. }
  586. $smcFunc['db_free_result']($request);
  587. // Get a list of global moderators (i.e. members with moderation powers).
  588. $global_mods = array_intersect(membersAllowedTo('moderate_board', 0), membersAllowedTo('approve_posts', 0), membersAllowedTo('remove_any', 0), membersAllowedTo('modify_any', 0));
  589. // How about anyone else who is special?
  590. $allStaff = array_merge(membersAllowedTo('admin_forum'), membersAllowedTo('manage_membergroups'), membersAllowedTo('manage_permissions'), $local_mods, $global_mods);
  591. // Make sure everyone is there once - no admin less important than any other!
  592. $allStaff = array_unique($allStaff);
  593. // This is a bit of a cop out - but we're protecting their forum, really!
  594. if (count($allStaff) > 300)
  595. fatal_lang_error('report_error_too_many_staff');
  596. // Get all the possible membergroups!
  597. $request = $smcFunc['db_query']('', '
  598. SELECT id_group, group_name, online_color
  599. FROM {db_prefix}membergroups',
  600. array(
  601. )
  602. );
  603. $groups = array(0 => $txt['full_member']);
  604. while ($row = $smcFunc['db_fetch_assoc']($request))
  605. $groups[$row['id_group']] = empty($row['online_color']) ? $row['group_name'] : '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>';
  606. $smcFunc['db_free_result']($request);
  607. // All the fields we'll show.
  608. $staffSettings = array(
  609. 'position' => $txt['report_staff_position'],
  610. 'moderates' => $txt['report_staff_moderates'],
  611. 'posts' => $txt['report_staff_posts'],
  612. 'last_login' => $txt['report_staff_last_login'],
  613. );
  614. // Do it in columns, it's just easier.
  615. setKeys('cols');
  616. // Get each member!
  617. $request = $smcFunc['db_query']('', '
  618. SELECT id_member, real_name, id_group, posts, last_login
  619. FROM {db_prefix}members
  620. WHERE id_member IN ({array_int:staff_list})
  621. ORDER BY real_name',
  622. array(
  623. 'staff_list' => $allStaff,
  624. )
  625. );
  626. while ($row = $smcFunc['db_fetch_assoc']($request))
  627. {
  628. // Each member gets their own table!.
  629. newTable($row['real_name'], '', 'left', 'auto', 'left', 200, 'center');
  630. // First off, add in the side key.
  631. addData($staffSettings);
  632. // Create the main data array.
  633. $staffData = array(
  634. 'position' => isset($groups[$row['id_group']]) ? $groups[$row['id_group']] : $groups[0],
  635. 'posts' => $row['posts'],
  636. 'last_login' => timeformat($row['last_login']),
  637. 'moderates' => array(),
  638. );
  639. // What do they moderate?
  640. if (in_array($row['id_member'], $global_mods))
  641. $staffData['moderates'] = '<em>' . $txt['report_staff_all_boards'] . '</em>';
  642. elseif (isset($moderators[$row['id_member']]))
  643. {
  644. // Get the names
  645. foreach ($moderators[$row['id_member']] as $board)
  646. if (isset($boards[$board]))
  647. $staffData['moderates'][] = $boards[$board];
  648. $staffData['moderates'] = implode(', ', $staffData['moderates']);
  649. }
  650. else
  651. $staffData['moderates'] = '<em>' . $txt['report_staff_no_boards'] . '</em>';
  652. // Next add the main data.
  653. addData($staffData);
  654. }
  655. $smcFunc['db_free_result']($request);
  656. }
  657. /**
  658. * This function creates a new table of data, most functions will only use it once.
  659. * The core of this file, it creates a new, but empty, table of data in
  660. * context, ready for filling using addData().
  661. * Fills the context variable current_table with the ID of the table created.
  662. * Keeps track of the current table count using context variable table_count.
  663. *
  664. * @param string $title = '' Title to be displayed with this data table.
  665. * @param string $default_value = '' Value to be displayed if a key is missing from a row.
  666. * @param string $shading = 'all' Should the left, top or both (all) parts of the table beshaded?
  667. * @param string $width_normal = 'auto' width of an unshaded column (auto means not defined).
  668. * @param string $align_normal = 'center' alignment of data in an unshaded column.
  669. * @param string $width_shaded = 'auto' width of a shaded column (auto means not defined).
  670. * @param string $align_shaded = 'auto' alignment of data in a shaded column.
  671. */
  672. function newTable($title = '', $default_value = '', $shading = 'all', $width_normal = 'auto', $align_normal = 'center', $width_shaded = 'auto', $align_shaded = 'auto')
  673. {
  674. global $context;
  675. // Set the table count if needed.
  676. if (empty($context['table_count']))
  677. $context['table_count'] = 0;
  678. // Create the table!
  679. $context['tables'][$context['table_count']] = array(
  680. 'title' => $title,
  681. 'default_value' => $default_value,
  682. 'shading' => array(
  683. 'left' => $shading == 'all' || $shading == 'left',
  684. 'top' => $shading == 'all' || $shading == 'top',
  685. ),
  686. 'width' => array(
  687. 'normal' => $width_normal,
  688. 'shaded' => $width_shaded,
  689. ),
  690. 'align' => array(
  691. 'normal' => $align_normal,
  692. 'shaded' => $align_shaded,
  693. ),
  694. 'data' => array(),
  695. );
  696. $context['current_table'] = $context['table_count'];
  697. // Increment the count...
  698. $context['table_count']++;
  699. }
  700. /**
  701. * Adds an array of data into an existing table.
  702. * if there are no existing tables, will create one with default
  703. * attributes.
  704. * if custom_table isn't specified, it will use the last table created,
  705. * if it is specified and doesn't exist the function will return false.
  706. * if a set of keys have been specified, the function will check each
  707. * required key is present in the incoming data. If this data is missing
  708. * the current tables default value will be used.
  709. * if any key in the incoming data begins with '#sep#', the function
  710. * will add a separator accross the table at this point.
  711. * once the incoming data has been sanitized, it is added to the table.
  712. *
  713. * @param array $inc_data
  714. * @param int $custom_table = null
  715. */
  716. function addData($inc_data, $custom_table = null)
  717. {
  718. global $context;
  719. // No tables? Create one even though we are probably already in a bad state!
  720. if (empty($context['table_count']))
  721. newTable();
  722. // Specific table?
  723. if ($custom_table !== null && !isset($context['tables'][$custom_table]))
  724. return false;
  725. elseif ($custom_table !== null)
  726. $table = $custom_table;
  727. else
  728. $table = $context['current_table'];
  729. // If we have keys, sanitise the data...
  730. if (!empty($context['keys']))
  731. {
  732. // Basically, check every key exists!
  733. foreach ($context['keys'] as $key => $dummy)
  734. {
  735. $data[$key] = array(
  736. 'v' => empty($inc_data[$key]) ? $context['tables'][$table]['default_value'] : $inc_data[$key],
  737. );
  738. // Special "hack" the adding separators when doing data by column.
  739. if (strpos($key, '#sep#') === 0)
  740. $data[$key]['separator'] = true;
  741. }
  742. }
  743. else
  744. {
  745. $data = $inc_data;
  746. foreach ($data as $key => $value)
  747. {
  748. $data[$key] = array(
  749. 'v' => $value,
  750. );
  751. if (strpos($key, '#sep#') === 0)
  752. $data[$key]['separator'] = true;
  753. }
  754. }
  755. // Is it by row?
  756. if (empty($context['key_method']) || $context['key_method'] == 'rows')
  757. {
  758. // Add the data!
  759. $context['tables'][$table]['data'][] = $data;
  760. }
  761. // Otherwise, tricky!
  762. else
  763. {
  764. foreach ($data as $key => $item)
  765. $context['tables'][$table]['data'][$key][] = $item;
  766. }
  767. }
  768. /**
  769. * Add a separator row, only really used when adding data by rows.
  770. *
  771. * @param string $title = ''
  772. * @param string $custom_table = null
  773. *
  774. * @return bool returns false if there are no tables
  775. */
  776. function addSeparator($title = '', $custom_table = null)
  777. {
  778. global $context;
  779. // No tables - return?
  780. if (empty($context['table_count']))
  781. return;
  782. // Specific table?
  783. if ($custom_table !== null && !isset($context['tables'][$table]))
  784. return false;
  785. elseif ($custom_table !== null)
  786. $table = $custom_table;
  787. else
  788. $table = $context['current_table'];
  789. // Plumb in the separator
  790. $context['tables'][$table]['data'][] = array(0 => array(
  791. 'separator' => true,
  792. 'v' => $title
  793. ));
  794. }
  795. /**
  796. * This does the necessary count of table data before displaying them.
  797. * is (unfortunately) required to create some useful variables for templates.
  798. * foreach data table created, it will count the number of rows and
  799. * columns in the table.
  800. * will also create a max_width variable for the table, to give an
  801. * estimate width for the whole table * * if it can.
  802. */
  803. function finishTables()
  804. {
  805. global $context;
  806. if (empty($context['tables']))
  807. return;
  808. // Loop through each table counting up some basic values, to help with the templating.
  809. foreach ($context['tables'] as $id => $table)
  810. {
  811. $context['tables'][$id]['id'] = $id;
  812. $context['tables'][$id]['row_count'] = count($table['data']);
  813. $curElement = current($table['data']);
  814. $context['tables'][$id]['column_count'] = count($curElement);
  815. // Work out the rough width - for templates like the print template. Without this we might get funny tables.
  816. if ($table['shading']['left'] && $table['width']['shaded'] != 'auto' && $table['width']['normal'] != 'auto')
  817. $context['tables'][$id]['max_width'] = $table['width']['shaded'] + ($context['tables'][$id]['column_count'] - 1) * $table['width']['normal'];
  818. elseif ($table['width']['normal'] != 'auto')
  819. $context['tables'][$id]['max_width'] = $context['tables'][$id]['column_count'] * $table['width']['normal'];
  820. else
  821. $context['tables'][$id]['max_width'] = 'auto';
  822. }
  823. }
  824. /**
  825. * Set the keys in use by the tables - these ensure entries MUST exist if the data isn't sent.
  826. *
  827. * sets the current set of "keys" expected in each data array passed to
  828. * addData. It also sets the way we are adding data to the data table.
  829. * method specifies whether the data passed to addData represents a new
  830. * column, or a new row.
  831. * keys is an array whose keys are the keys for data being passed to
  832. * addData().
  833. * if reverse is set to true, then the values of the variable "keys"
  834. * are used as oppossed to the keys(!
  835. *
  836. * @param string $method = 'rows' rows or cols
  837. * @param array $keys = array()
  838. * @param bool $reverse = false
  839. */
  840. function setKeys($method = 'rows', $keys = array(), $reverse = false)
  841. {
  842. global $context;
  843. // Do we want to use the keys of the keys as the keys? :P
  844. if ($reverse)
  845. $context['keys'] = array_flip($keys);
  846. else
  847. $context['keys'] = $keys;
  848. // Rows or columns?
  849. $context['key_method'] = $method == 'rows' ? 'rows' : 'cols';
  850. }
  851. ?>