timekeeper.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * TimeKeeper front page / entry point.
  4. *
  5. * Changes from the current repo version:
  6. * - mode=Generate PDF and mode=short had no access check, so anyone, logged
  7. * in or not, could read any student's full question report or score with
  8. * ?mode=Generate%20PDF&user=<name>&output=html&range=pastYear.
  9. * Now:
  10. * Generate PDF -> must be logged in, and either a Main Admin or the
  11. * student the report is about
  12. * short -> same, OR a valid ?key= matching the plugin pref
  13. * `stats_key` (for a wall display / client that can't log in)
  14. * - removed the dead commented-out code (it's all in git history)
  15. *
  16. * IMPORTANT: anything that fetches ?mode=short without logging in (a kiosk,
  17. * a script) will now get 403 until stats_key is set and the URL has &key=...
  18. */
  19. if (!defined('e107_INIT')) {
  20. require_once('../../class2.php');
  21. }
  22. if (!e107::isInstalled('timekeeper')) {
  23. e107::redirect();
  24. exit;
  25. }
  26. require_once(e_PLUGIN . 'timekeeper/PreHeaderFunctions.php');
  27. /**
  28. * May the current visitor see the report for $user?
  29. *
  30. * @param string $user the ?user= value being requested
  31. * @param bool $allowKey also accept the shared ?key= (pref `stats_key`)
  32. */
  33. function tk_may_view_reports($user, $allowKey) {
  34. if ($allowKey) {
  35. $secret = (string) e107::getPlugPref('timekeeper', 'stats_key', '');
  36. $given = filter_input(INPUT_GET, 'key');
  37. if ($secret !== '' && is_string($given) && hash_equals($secret, $given)) {
  38. return true;
  39. }
  40. }
  41. if (!USER) {
  42. return false;
  43. }
  44. return check_class('Main Admin') || strcasecmp(e107::getUser()->getLoginName(), $user) === 0;
  45. }
  46. $mode = filter_input(INPUT_POST, 'mode');
  47. if ($mode == "") {
  48. $mode = filter_input(INPUT_GET, 'mode');
  49. }
  50. if ($mode == 'Generate PDF' || $mode == 'short') {
  51. $reportUser = filter_input(INPUT_GET, 'user');
  52. if (!is_string($reportUser) || !tk_may_view_reports($reportUser, $mode == 'short')) {
  53. http_response_code(403);
  54. exit('Forbidden');
  55. }
  56. if ($mode == 'Generate PDF') {
  57. $pdf = new ExcelFunctions;
  58. $pdf->GeneratePdf();
  59. } else {
  60. $json = new Summary;
  61. $json->GenerateSummary();
  62. }
  63. }
  64. require_once(HEADERF); // render the header (everything before the main content area)
  65. require_once(e_PLUGIN . 'timekeeper/TK2.php');
  66. $tk2 = new TK2;
  67. $tk2->run();
  68. require_once(FOOTERF); // render the footer (everything after the main content area)
  69. exit;