123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- # Copyright (C) 2005, 2009 Alex Schroeder <alex@gnu.org>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- use strict;
- use v5.10;
- AddModuleDescription('big-brother.pl', 'Big Brother Extension');
- our ($q, $Now, %Action, $SurgeProtectionViews, $SurgeProtectionTime, @MyAdminCode, $RCName, $VisitorFile, $FS);
- our ($VisitorTime, @BigBrotherSecretParameters);
- my $US = "\x1f";
- $VisitorTime = 7200; # keep visitor data arround for 2 hours.
- # normal password parameter from wiki.pl
- # password parameters from login.pl
- @BigBrotherSecretParameters = qw(pwd pwd1 pwd2 oldpwd);
- push(@MyAdminCode, \&BigBrotherVisitors);
- sub BigBrotherVisitors {
- my ($id, $menuref, $restref) = @_;
- push(@$menuref, ScriptLink('action=visitors', Ts('Recent Visitors'), 'visitors'));
- }
- my %BigBrotherData;
- # replace the subs that DoSurgeProtection calls:
- # ReadRecentVisitors();
- # AddRecentVisitor($name);
- # WriteRecentVisitors();
- # if ($SurgeProtection and DelayRequired($name))
- sub AddRecentVisitor {
- my ($name) = shift;
- my $value = $BigBrotherData{$name};
- my %entries = $value ? %{$value} : ();
- # make sure we don't ignore hits in the same second
- my $ts = $Now;
- $ts++ while $entries{$ts};
- my $action = GetParam('action', 'browse');
- my $id = GetId(); # script/p/q -> q
- my %params = map { $_ => 1 } $q->param;
- for my $bad (@BigBrotherSecretParameters) {
- delete $params{$bad};
- }
- my $url = ScriptUrl(join(';', "action=$action;id=" . UrlEncode($id),
- map { $_ . '=' . UrlEncode(GetParam($_)) }
- keys %params));
- my $download = GetParam('action', 'browse') eq 'download'
- || GetParam('download', 0)
- || $q->path_info() =~ m/\/download\//;
- if ($download) {
- # do nothing
- } elsif ($id) {
- $entries{$ts} = $id . $US . $url;
- } elsif ($action eq 'rss' or $action eq 'rc') {
- $entries{$ts} = $RCName . $US . $url;
- } else {
- $entries{$ts} = T('some action') . $US . $url;
- }
- $BigBrotherData{$name} = \%entries;
- }
- sub DelayRequired {
- my $name = shift;
- return 0 unless $BigBrotherData{$name};
- my %entries = %{$BigBrotherData{$name}};
- my @times = sort keys %entries;
- return 0 if not $times[$SurgeProtectionViews - 1]; # all slots must be filled
- return 0 if ($Now - $times[0]) > $SurgeProtectionTime;
- return 1;
- }
- sub ReadRecentVisitors {
- my ($status, $data) = ReadFile($VisitorFile);
- %BigBrotherData = ();
- return unless $status;
- foreach (split(/\n/,$data)) {
- my ($name, %entries) = split /$FS/;
- $BigBrotherData{$name} = \%entries if $name and %entries;
- }
- }
- sub WriteRecentVisitors {
- my $data = '';
- my $limit = $Now - $VisitorTime; # don't save visits older than this
- foreach my $name (keys %BigBrotherData) {
- my %entries = %{$BigBrotherData{$name}};
- my @times = sort keys %entries;
- # strip entries older than the older visits
- while (@times and $times[0] < $limit) {
- splice(@times, 0, 1);
- }
- # if we still have more than the number of elements required for
- # surge protection, delete these as well
- @times = @times[-$SurgeProtectionViews .. -1] if @times > $SurgeProtectionViews;
- $data .= join($FS, $name, map { $_, $entries{$_}} @times) . "\n" if @times;
- }
- WriteStringToFile($VisitorFile, $data);
- }
- $Action{visitors} = \&DoBigBrother;
- sub DoBigBrother { # no caching of this page!
- print GetHeader('', T('Recent Visitors'), '', 1), $q->start_div({-class=>'content visitors'});
- ReadRecentVisitors();
- print '<p><ul>';
- my %latest = ();
- foreach (keys %BigBrotherData) {
- my %entries = %{$BigBrotherData{$_}};
- my @times = sort keys %entries;
- $latest{$_} = $times[-1];
- }
- foreach my $name (sort {$latest{$b} <=> $latest{$a}} keys %latest) {
- my $when = CalcTimeSince($Now - $latest{$name});
- my $error = ValidId($name);
- my $who = $name && !$error && $name !~ /\./ ? GetPageLink($name) : T('Anonymous');
- my %entries = %{$BigBrotherData{$name}};
- my %reverse = (); # reverse hash to filter out duplicate targets
- foreach my $key (keys %entries) {
- $reverse{$entries{$key}} = $key;
- }
- my $what = join(', ', map { my ($id, $url) = split(/$US/, $entries{$_});
- $q->a({-href=>$url}, $id); }
- sort values %reverse);
- print $q->li($who, T('was here'), $when, T('and read'), $what);
- }
- print '</ul>' . $q->end_div();
- PrintFooter();
- }
|