Reports.php 36 KB

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