###############################################################################
# CP_HitsReport.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 HitsReport {
  my ($ERROR) = @_;

  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::FileOpen(*TOTAL_HITS, "rw", "$SYSTEM::LOGS_PATH/total_hits.stats");
    my $TOTAL_HITS = <TOTAL_HITS> || 0;
  close (TOTAL_HITS);

  &Standard::FileOpen(*TODAY_HITS, "rw", "$SYSTEM::LOGS_PATH/today_hits.stats");
    my $TODAY_HITS = <TODAY_HITS>;
  close (TODAY_HITS);

  &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 visitors 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. Total hits is $TOTAL_HITS. Today hits is $TODAY_HITS.
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;Last 28 days hits report
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="White" bordercolorlight="Gray">
              <tr height=24>
                <td width="25%" nowrap>&nbsp;Date&nbsp;</td>
                <td width="25%" nowrap>&nbsp;Hits&nbsp;</td>
                <td width="10" nowrap>&nbsp;</td>
                <td width="25%" nowrap>&nbsp;Date&nbsp;</td>
                <td width="25%" nowrap>&nbsp;Hits&nbsp;</td>
              </tr>
HTML
  &Standard::FileOpen(*OTHER_DAYS_HITS, "rw", "$SYSTEM::LOGS_PATH/other_days_hits.stats");
    my @OTHER_DAYS_HITS = <OTHER_DAYS_HITS>;
    chomp @OTHER_DAYS_HITS;
  close (OTHER_DAYS_HITS);

  my $Start = 0;
  my $End   = 27;
     $End   = $#OTHER_DAYS_HITS if $End > $#OTHER_DAYS_HITS;
     $Start = $#OTHER_DAYS_HITS;
     $End   = $#OTHER_DAYS_HITS - $End;

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

  my $Counter = 1;
  for (my $i = $#OTHER_DAYS_HITS; $i >= 0; $i--) {
    print qq~<tr>~ if ($Counter % 2 == 1);
    my ($Date, $Hits) = split (/\|\^\|/, $OTHER_DAYS_HITS[$i]);
    $Date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/g;
    print qq~<td width="10" nowrap>&nbsp;</td>~ if ($Counter % 2 == 0);
    print qq~<td width="25%" bgcolor="White">&nbsp;$Date&nbsp;</td><td width="25%" bgcolor="White">&nbsp;$Hits&nbsp;</td>~;
    print qq~</tr>~ if ($Counter % 2 == 0);
    $Counter++;
  }
print<<HTML;
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;Yearly hits report
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="White" bordercolorlight="Gray">
              <tr height=24>
                <td nowrap>&nbsp;Month&nbsp;</td>
                <td nowrap>&nbsp;Hits&nbsp;</td>
                <td nowrap>&nbsp;Percent&nbsp;</td>
                <td width="100%" nowrap>Graph</td>
              </tr>
HTML
  tie (my %YEAR, $AnyDBM_File::ISA[0], "$SYSTEM::LOGS_PATH/year_hits.stats", O_RDWR|O_CREAT, 0777);

  my %YearMonths = (
    0 => "January",
    1 => "February",
    2 => "March",
    3 => "April",
    4 => "May",
    5 => "June",
    6 => "July",
    7 => "Augest",
    8 => "September",
    9 => "October",
    10 => "November",
    11 => "December"
  );

  my $MaxHits = 1;
  my $TotalHits;
  foreach my $Hits (values %YEAR) {
    $MaxHits = $Hits if $Hits > $MaxHits;
    $TotalHits += $Hits;
  }
  $TotalHits ||= 1;

  foreach my $YearMonth (sort {$a <=> $b} keys %YearMonths) {
    my $Hits = $YEAR{ $YearMonth } || 0;
    my $Percent = int ((($Hits / $MaxHits) * 100));
    my $PercentOfTotal = sprintf ("%.02f", (($Hits / $TotalHits) * 100));
    my $Month = $YearMonths{ $YearMonth };
    print qq~<tr bgcolor="White">~;
    print qq~<td nowrap>&nbsp;$Month&nbsp;</td>~;
    print qq~<td align="center" nowrap>&nbsp;$Hits&nbsp;</td>~;
    print qq~<td align="center" nowrap>&nbsp;$PercentOfTotal%&nbsp;</td>~;
    print qq~<td width="100%"><img src="$SYSTEM::IMAGE_URL/graph.gif" width="$Percent%" height="100%" border="1"></td>~;
    print qq~</tr>~;
  }

  untie (%WEEK);
print<<HTML;
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;Weekly hits report
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="White" bordercolorlight="Gray">
              <tr height=24>
                <td nowrap>&nbsp;Day&nbsp;</td>
                <td nowrap>&nbsp;Hits&nbsp;</td>
                <td nowrap>&nbsp;Percent&nbsp;</td>
                <td width="100%" nowrap>Graph</td>
              </tr>
HTML
  tie (my %WEEK, $AnyDBM_File::ISA[0], "$SYSTEM::LOGS_PATH/week_hits.stats", O_RDWR|O_CREAT, 0777);

  my %Weekday = (
    0 => "Sunday",
    1 => "Monday",
    2 => "Tuesday",
    3 => "Wednesday",
    4 => "Thursday",
    5 => "Friday",
    6 => "Saturday"
  );

  my $MaxHits = 1;
  my $TotalHits;
  foreach my $Hits (values %WEEK) {
    $MaxHits = $Hits if $Hits > $MaxHits;
    $TotalHits += $Hits;
  }
  $TotalHits ||= 1;

  foreach my $Day (sort {$a <=> $b} keys %Weekday) {
    my $Hits = $WEEK{ $Day } || 0;
    my $Percent = int ((($Hits / $MaxHits) * 100));
    my $PercentOfTotal = sprintf ("%.02f", (($Hits / $TotalHits) * 100));
    my $Weekday = $Weekday{ $Day };
    print qq~<tr bgcolor="White">~;
    print qq~<td nowrap>&nbsp;$Weekday&nbsp;</td>~;
    print qq~<td align="center" nowrap>&nbsp;$Hits&nbsp;</td>~;
    print qq~<td align="center" nowrap>&nbsp;$PercentOfTotal%&nbsp;</td>~;
    print qq~<td width="100%"><img src="$SYSTEM::IMAGE_URL/graph.gif" width="$Percent%" height="100%" border="1"></td>~;
    print qq~</tr>~;
  }

  untie (%WEEK);
print<<HTML;
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;Daily hits report
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="White" bordercolorlight="Gray">
              <tr height=24>
                <td nowrap>&nbsp;Hour&nbsp;</td>
                <td nowrap>&nbsp;Hits&nbsp;</td>
                <td nowrap>&nbsp;Percent&nbsp;</td>
                <td width="100%" nowrap>Graph</td>
              </tr>
HTML
  tie (my %DAY, $AnyDBM_File::ISA[0], "$SYSTEM::LOGS_PATH/day_hits.stats", O_RDWR|O_CREAT, 0777);

  my $MaxHits = 1;
  my $TotalHits;
  foreach my $Hits (values %DAY) {
    $MaxHits = $Hits if $Hits > $MaxHits;
    $TotalHits += $Hits;
  }
  $TotalHits ||= 1;

  foreach my $DayHour (0..23) {
    my $Hits = $DAY{ $DayHour } || 0;
    my $Percent = int ((($Hits / $MaxHits) * 100));
    my $PercentOfTotal = sprintf ("%.02f", (($Hits / $TotalHits) * 100));
    my $Hour = sprintf ("%02d:00 - %02d:59", $DayHour, $DayHour);
    print qq~<tr bgcolor="White">~;
    print qq~<td nowrap>&nbsp;$Hour&nbsp;</td>~;
    print qq~<td align="center" nowrap>&nbsp;$Hits&nbsp;</td>~;
    print qq~<td align="center" nowrap>&nbsp;$PercentOfTotal%&nbsp;</td>~;
    print qq~<td width="100%"><img src="$SYSTEM::IMAGE_URL/graph.gif" width="$Percent%" height="100%" border="1"></td>~;
    print qq~</tr>~;
  }

  untie (%WEEK);
print<<HTML;
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML
}

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