Reports.php 33 KB

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