Reports.php 32 KB

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