big-brother.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (C) 2005, 2009 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('big-brother.pl', 'Big Brother Extension');
  18. our ($q, $Now, %Action, $SurgeProtectionViews, $SurgeProtectionTime, @MyAdminCode, $RCName, $VisitorFile, $FS);
  19. our ($VisitorTime, @BigBrotherSecretParameters);
  20. my $US = "\x1f";
  21. $VisitorTime = 7200; # keep visitor data arround for 2 hours.
  22. # normal password parameter from wiki.pl
  23. # password parameters from login.pl
  24. @BigBrotherSecretParameters = qw(pwd pwd1 pwd2 oldpwd);
  25. push(@MyAdminCode, \&BigBrotherVisitors);
  26. sub BigBrotherVisitors {
  27. my ($id, $menuref, $restref) = @_;
  28. push(@$menuref, ScriptLink('action=visitors', Ts('Recent Visitors'), 'visitors'));
  29. }
  30. my %BigBrotherData;
  31. # replace the subs that DoSurgeProtection calls:
  32. # ReadRecentVisitors();
  33. # AddRecentVisitor($name);
  34. # WriteRecentVisitors();
  35. # if ($SurgeProtection and DelayRequired($name))
  36. sub AddRecentVisitor {
  37. my ($name) = shift;
  38. my $value = $BigBrotherData{$name};
  39. my %entries = $value ? %{$value} : ();
  40. # make sure we don't ignore hits in the same second
  41. my $ts = $Now;
  42. $ts++ while $entries{$ts};
  43. my $action = GetParam('action', 'browse');
  44. my $id = GetId(); # script/p/q -> q
  45. my %params = map { $_ => 1 } $q->param;
  46. for my $bad (@BigBrotherSecretParameters) {
  47. delete $params{$bad};
  48. }
  49. my $url = ScriptUrl(join(';', "action=$action;id=" . UrlEncode($id),
  50. map { $_ . '=' . UrlEncode(GetParam($_)) }
  51. keys %params));
  52. my $download = GetParam('action', 'browse') eq 'download'
  53. || GetParam('download', 0)
  54. || $q->path_info() =~ m/\/download\//;
  55. if ($download) {
  56. # do nothing
  57. } elsif ($id) {
  58. $entries{$ts} = $id . $US . $url;
  59. } elsif ($action eq 'rss' or $action eq 'rc') {
  60. $entries{$ts} = $RCName . $US . $url;
  61. } else {
  62. $entries{$ts} = T('some action') . $US . $url;
  63. }
  64. $BigBrotherData{$name} = \%entries;
  65. }
  66. sub DelayRequired {
  67. my $name = shift;
  68. return 0 unless $BigBrotherData{$name};
  69. my %entries = %{$BigBrotherData{$name}};
  70. my @times = sort keys %entries;
  71. return 0 if not $times[$SurgeProtectionViews - 1]; # all slots must be filled
  72. return 0 if ($Now - $times[0]) > $SurgeProtectionTime;
  73. return 1;
  74. }
  75. sub ReadRecentVisitors {
  76. my ($status, $data) = ReadFile($VisitorFile);
  77. %BigBrotherData = ();
  78. return unless $status;
  79. foreach (split(/\n/,$data)) {
  80. my ($name, %entries) = split /$FS/;
  81. $BigBrotherData{$name} = \%entries if $name and %entries;
  82. }
  83. }
  84. sub WriteRecentVisitors {
  85. my $data = '';
  86. my $limit = $Now - $VisitorTime; # don't save visits older than this
  87. foreach my $name (keys %BigBrotherData) {
  88. my %entries = %{$BigBrotherData{$name}};
  89. my @times = sort keys %entries;
  90. # strip entries older than the older visits
  91. while (@times and $times[0] < $limit) {
  92. splice(@times, 0, 1);
  93. }
  94. # if we still have more than the number of elements required for
  95. # surge protection, delete these as well
  96. @times = @times[-$SurgeProtectionViews .. -1] if @times > $SurgeProtectionViews;
  97. $data .= join($FS, $name, map { $_, $entries{$_}} @times) . "\n" if @times;
  98. }
  99. WriteStringToFile($VisitorFile, $data);
  100. }
  101. $Action{visitors} = \&DoBigBrother;
  102. sub DoBigBrother { # no caching of this page!
  103. print GetHeader('', T('Recent Visitors'), '', 1), $q->start_div({-class=>'content visitors'});
  104. ReadRecentVisitors();
  105. print '<p><ul>';
  106. my %latest = ();
  107. foreach (keys %BigBrotherData) {
  108. my %entries = %{$BigBrotherData{$_}};
  109. my @times = sort keys %entries;
  110. $latest{$_} = $times[-1];
  111. }
  112. foreach my $name (sort {$latest{$b} <=> $latest{$a}} keys %latest) {
  113. my $when = CalcTimeSince($Now - $latest{$name});
  114. my $error = ValidId($name);
  115. my $who = $name && !$error && $name !~ /\./ ? GetPageLink($name) : T('Anonymous');
  116. my %entries = %{$BigBrotherData{$name}};
  117. my %reverse = (); # reverse hash to filter out duplicate targets
  118. foreach my $key (keys %entries) {
  119. $reverse{$entries{$key}} = $key;
  120. }
  121. my $what = join(', ', map { my ($id, $url) = split(/$US/, $entries{$_});
  122. $q->a({-href=>$url}, $id); }
  123. sort values %reverse);
  124. print $q->li($who, T('was here'), $when, T('and read'), $what);
  125. }
  126. print '</ul>' . $q->end_div();
  127. PrintFooter();
  128. }