###############################################################################
# CP_AccessReport.src                                                         #
###############################################################################
# UltraBoard 2000 by UltraScripts.com, Inc.                                   #
# Scripts written by Jacky Wing Wa Yung, WebMaster@UltraScripts.com           #
# Available from http://www.UltraBoard.org/                                   #
# =========================================================================== #
# COPYRIGHT NOTICE :                                                          #
#                                                                             #
# Copyright (c) 1999-2000 UltraScripts.com, Inc. All Rights Reserved.         #
#                                                                             #
# This program is being distributed as shareware. It may be used and modified #
# free for personal, government or non-profit. Any commercial use must be     #
# registered. However, modified versions cannot be sold or distributed. You   #
# cannot alter the copyright and "powered by" notices throughout the scripts. #
# These notices must be clearly visible to the end users.                     #
#                                                                             #
# WARRANTY DISCLAIMER:                                                        #
#                                                                             #
# 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.                                           #
###############################################################################

package Source;

sub AccessReport {
  my ($ERROR) = @_;

  my $Display;
  my $Max;
  my $Logs = [];
  opendir (DIR, "$SYSTEM::LOGS_PATH");
    while ( defined (my $FILE = readdir (DIR)) ) {
      next if ($FILE =~ /^\.\.?$/);
      if ($FILE =~ /^access\.(\d+)\.(\d+)\.(\d+)\.log$/) {
        my $Number = $1.$2.$3;
        ($Max = $Number and $Display = $FILE) if ($Number > $Max);
        push (
          @{ $Logs },
          { KEY => $FILE, VALUE => "$1-$2-$3 Report" }
        );
      }
    }
  closedir (DIR);

  unless ($Display) {
    &HTML::Error("UltraBoard Statistics / UltraBoard access report", "No logs available for you to view yet.");
  }

  $Display = $UB::QUERY{'FORM_REPORT'} if $UB::QUERY{'FORM_REPORT'};

  my %FORM;
  $FORM{'FORM_REPORT'} = &HTML::Select (
    NAME      => "FORM_REPORT",
    VALUE     => $Logs     ,
    SELECTED  => [$Display]
  );

  $FORM{'FORM_PER_PAGE'}   = &HTML::Select (
    NAME      => "FORM_PER_PAGE",
    VALUE     => [
      { KEY => "100" , VALUE => "100 lines per page" },
      { KEY => "200" , VALUE => "200 lines per page" },
      { KEY => "300" , VALUE => "300 lines per page" },
      { KEY => "400" , VALUE => "400 lines per page" },
      { KEY => "500" , VALUE => "500 lines per page" },
      { KEY => "600" , VALUE => "600 lines per page" },
      { KEY => "700" , VALUE => "700 lines per page" },
      { KEY => "800" , VALUE => "800 lines per page" },
      { KEY => "900" , VALUE => "900 lines per page" },
      { KEY => "1000", VALUE => "1000 lines per page" },
      { KEY => "1500", VALUE => "1500 lines per page" },
      { KEY => "2000", VALUE => "2000 lines per page" },
      { KEY => "1500", VALUE => "2500 lines per page" },
      { KEY => "2000", VALUE => "3000 lines per page" },
      { KEY => "1500", VALUE => "3500 lines per page" },
      { KEY => "2000", VALUE => "4000 lines per page" },
      { KEY => "1500", VALUE => "4500 lines per page" },
      { KEY => "2000", VALUE => "5000 lines per page" }
    ],
    SELECTED  => [$UB::QUERY{'FORM_PER_PAGE'} || 100]
  );

  &Standard::FileOpen(*LOG, "r", "$SYSTEM::LOGS_PATH/$Display");
    my @LOG = <LOG>;
    chomp @LOG;
  close (LOG);

  $Totallines = scalar (@LOG);

  $UB::QUERY{'FORM_PER_PAGE'} = 100 unless $UB::QUERY{'FORM_PER_PAGE'};
  my $TotalPages  = &Standard::ceil( $Totallines, $UB::QUERY{'FORM_PER_PAGE'});

  my $Page = 1;
     $Page = $UB::QUERY{'P'} if ( ($UB::QUERY{'P'}) && ($UB::QUERY{'P'} > 0) && ($UB::QUERY{'P'} <= $TotalPages));

  my $Start =  ($Page - 1) * $UB::QUERY{'FORM_PER_PAGE'};
  my $End   = (($Page - 1) * $UB::QUERY{'FORM_PER_PAGE'}) + $UB::QUERY{'FORM_PER_PAGE'};
     $End   = $Totallines if $End > $Totallines;
     $Start = $Totallines - $Start - 1;
     $End   = $Totallines - $End;

  @LOG = @LOG[$End..$Start];

  my $Pages = [];
  for (my $i = 1; $i <= $TotalPages; $i++) {
    my $Start =  ($i - 1) * $UB::QUERY{'FORM_PER_PAGE'};
    my $End   = (($i - 1) * $UB::QUERY{'FORM_PER_PAGE'}) + $UB::QUERY{'FORM_PER_PAGE'};
       $End   = $Totallines if $End > $Totallines;
       $Start = $Totallines - $Start;
       $End   = $Totallines - $End + 1;
    push (
      @{ $Pages },
      { VALUE => "P. $i (Line $End to Line $Start)", KEY => $i }
    );
  }

  $FORM{'FORM_PAGE'} = &HTML::Select (
    NAME      => "P",
    VALUE     => $Pages,
    SELECTED  => [$Page]
  );

  my $epoch = time;
  my ($lHours, $lDays) = (localtime ($epoch))[2, 3];
  my ($gHours, $gDays) = (gmtime    ($epoch))[2, 3];

  $lHours += $lDays * 24;
  $gHours += $gDays * 24;

  my $tHours = $lHours - $gHours;

  my $TZ = "GMT";
  if ($tHours < 1) {
    $TZ = abs ($tHours)." hours before GMT";
  } elsif ($tHours > 1) {
    $TZ = $tHours." hours after GMT";
  } elsif ($tHours == -1) {
    $TZ = "1 hour before GMT";
  } elsif ($tHours ==  1) {
    $TZ = "1 hour after GMT";
  }

  &Standard::PrintHTMLHeader();
  print &HTML::Header;
  print<<HTML;
<table width="100%" border="0" cellspacing="0" cellpadding="5">
  <tr bgcolor="Gray">
    <td><font color="White"><b>
      <a href="$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=body" target="UBBODY" class="address">Control Panel</a> / UltraBoard Statistics / UltraBoard access report
    </b></font></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td>
            <table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolordark="White" bordercolorlight="Gray">
              <tr>
                <td>
                  <table width="100%" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                      <td>
                      Local server time is $TZ.
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            <table width="100%" border="0" cellspacing="0" cellpadding="5">
              <tr><td nowrap><form method="post"></td></tr>
              <tr>
                <td align="right">
                <input type="hidden" name="action" value="AccessReport">
                <input type="hidden" name="P" value="1">
                $FORM{'FORM_REPORT'} $FORM{'FORM_PER_PAGE'} <input type="submit" value="List">
                </td>
              </tr>
              <tr><td nowrap></form></td></tr>
            </table>
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="White" bordercolorlight="Gray">
              <tr height=24>
                <td align="center" nowrap>&nbsp;$Totallines&nbsp;</td>
                <td nowrap>&nbsp;Username&nbsp;</td>
                <td nowrap>&nbsp;IP&nbsp;</td>
                <td width="100%" nowrap>&nbsp;Host&nbsp;</td>
                <td nowrap>&nbsp;Time&nbsp;</td>
                <td nowrap>&nbsp;Action&nbsp;</td>
                <td nowrap>&nbsp;CID&nbsp;</td>
                <td nowrap>&nbsp;BID&nbsp;</td>
                <td nowrap>&nbsp;TID&nbsp;</td>
                <td nowrap>&nbsp;PID&nbsp;</td>
              </tr>
HTML
  for (my $i = $#LOG; $i >= 0; $i--) {
    my $Line = $End + $i + 1;
    my ($Time, $RemoteHost, $IP, $Agent, $Referer, $Query) = split (/\|/, $LOG[$i]);
    my (@Query) = split (/&/, $Query);

    my (%Query);
    for (@Query) {
      my ($Key, $Value) = split (/=/);
      $Query{$Key} = $Value;
    }

    if ($Query{'U'} ne "") {
      $Query{'U'} = qq~<a href="$SYSTEM::UB_URL/UltraBoard.$SYSTEM::EXT?action=Profile&UID=$Query{'U'}" target="PROFILE">$Query{'U'}</a>~;
    } else {
      $Query{'U'} = "n/a";
    }

    $Query{'A'} = "n/a" unless $Query{'A'};
    $Query{'C'} = "n/a" unless $Query{'C'};
    $Query{'B'} = "n/a" unless $Query{'B'};
    $Query{'T'} = "n/a" unless $Query{'T'};
    $Query{'P'} = "n/a" unless $Query{'P'};

    $IP = "n/a" unless $IP;
    $RemoteHost = "n/a" if ((!$RemoteHost) || ($RemoteHost eq $IP));

    my ($sec, $min, $hour) = (localtime ($Time)) [0..2];

    $Time = sprintf ("%02d:%02d:%02d", $hour, $min, $sec);

    print<<HTML;
              <tr bgcolor="White">
                <td>&nbsp;$Line&nbsp;</td>
                <td>&nbsp;$Query{'U'}&nbsp;</td>
                <td>&nbsp;$IP&nbsp;</td>
                <td>&nbsp;$RemoteHost&nbsp;</td>
                <td nowrap>&nbsp;$Time&nbsp;</td>
                <td>&nbsp;$Query{'A'}&nbsp;</td>
                <td>&nbsp;$Query{'C'}&nbsp;</td>
                <td>&nbsp;$Query{'B'}&nbsp;</td>
                <td>&nbsp;$Query{'T'}&nbsp;</td>
                <td>&nbsp;$Query{'P'}&nbsp;</td>
              </tr>
HTML
  }
print<<HTML;
            </table>
            <table width="100%" border="0" cellspacing="0" cellpadding="5">
              <tr><td nowrap><form method="post"></td></tr>
              <tr>
                <td align="right">
                <input type="hidden" name="action" value="AccessReport">
                <input type="hidden" name="FORM_REPORT" value="$Display">
                <input type="hidden" name="FORM_PER_PAGE" value="$UB::QUERY{'FORM_PER_PAGE'}">
                $FORM{'FORM_PAGE'} <input type="submit" value="List">
                </td>
              </tr>
              <tr><td nowrap></form></td></tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML
}


###############################################################################
1; # end of CP_AccessReport.src file
###############################################################################