Reports.php 33 KB

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