<?php
/*
                               SiteSpeed v2.11
                        (c) 2003-2005 J.C. Kessels
                           http://www.numion.com/
*/



$FontTitle = '<font face="Verdana, Arial, Helvetica, sans-serif" size="+1"><b>';
$FontOther = '<font face="Arial, Helvetica, sans-serif" size="-1">';




function PageHeader($Jump) {
  global $FontOther;
  echo '<html>' . "\n";
  echo '<head>' . "\n";
  echo '<title>SiteSpeed statistics</title>' . "\n";
  echo '<meta name="Description" content="SiteSpeed statistics">' . "\n";
  echo '<meta name="Author" content="Jeroen Kessels, jeroen@numion.com">' . "\n";
  if ($Jump != '') echo '<meta http-equiv=refresh content="' . $Jump . '">' . "\n";
  echo '<link rel="SHORTCUT ICON" href="http://www.numion.com/favicon.ico">' . "\n";
  echo '</head>' . "\n";
  echo '<body bgcolor="#E7F3FF" text="#003374" link="#000000" vlink="#000000" alink="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">' . "\n";
?>
<map name="map1">
 <area shape=rect coords="137,50,224,78" href="http://www.numion.com/" alt="Goto Numion homepage">
 </map>
<center>

<table border=0 cellspacing=0 cellpadding=0 width="100%">
<tr>
  <td><img src="img/Top1.gif" width=75 height=81 border=0></td>
  <td width="100%" align=center background="img/Top2.gif"><img src="img/Top3.gif" width=384 height=81 border=0></td>
  <td><img src="img/Top4.gif" width=76 height=81 border=0></td>
  </tr>
</table>
</center>
</if>

<table border=0 cellspacing=0 cellpadding=0 width="100%">
<tr>
  <td background="img/Middle1.gif" valign=top><img src="img/filler.gif" width=75 height=1></td>
  <td width="100%">
<?php
  echo $FontOther . "\n";
  }




function PageLine() {
  echo '<p><center><img src="img/Bar.gif" width=462 height=25></center><p>' . "\n";
  }




function PageFooter() {
  echo '    </font>' . "\n";
  echo '    </td>' . "\n";
  echo '  <td background="img/Middle2.gif" valign=top>' .
    '<img src="img/filler.gif" width=76 height=1></td>' . "\n";
  echo '  </tr>' . "\n";
  echo '</table>' . "\n";

  echo '<table border=0 cellspacing=0 cellpadding=0 width="100%">' . "\n";
  echo '<tr>' . "\n";
  echo '  <td><img src="img/Bot1.gif" width=75 height=87 border=0></td>' . "\n";
  echo '  <td width="100%" align=center background="img/Bot2.gif">' .
    '<img src="img/Bot3.gif" width=367 height=87 border=0 usemap="#map1"></td>' . "\n";
  echo '  <td><img src="img/Bot4.gif" width=76 height=87 border=0></td>' . "\n";
  echo '  </tr>' . "\n";
  echo '</table>' . "\n";

  echo '</body>' . "\n";
  echo '</html>' . "\n";

  exit();
  }



$Name = $HTTP_GET_VARS['Name'];
if ((! isset($Name)) || ($Name == '')) $Name = $_GET['Name'];
if ((! isset($Name)) || ($Name == '')) $Name = 'default';



/* Functions used for writing to the include file. */
function WriteArray1($Fout,$S,$Name) {
  if (! isset($S)) return;
  $Out = '$' . $Name . ' = array(';
  for ($i = 0; $i <= 199; $i++) {
    if (isset($S[$i])) {
        $Out .= $S[$i];
      } else {
        $Out .= '0';
        }
    if ($i < 199) $Out .= ',';
    }
  $Out .= ');' . "\n";
  return(fwrite($Fout,$Out));
  }

function WriteArray2($Fout,$S,$Name,$Max) {
  if (! isset($S)) return;
  $Out = '';
  for ($U = 0; $U <= 1; $U++) {
    $Out .= '$' . $Name . '[' . $U . '] = array(' . ($Max - 200) . ' => ';
    for ($i = $Max - 200; $i <= $Max; $i++) {
      if (isset($S[$U][$i])) {
          $Out .= $S[$U][$i];
        } else {
          $Out .= '0';
          }
      if ($i < $Max) $Out .= ',';
      }
    $Out .= ');' . "\n";
    }
  return(fwrite($Fout,$Out));
  }

function WriteArray3($Fout,$S,$Name,$Max,$Count) {
  if (! isset($S)) return;
  $Out = '';
  for ($U = 0; $U <= 1; $U++) {
    for ($j = $Max - 7; $j <= $Max; $j++) {
      $Out .= '$' . $Name . '[' . $U . '][' . $j . '] = array(';
      for ($i = 0; $i < $Count; $i++) {
        if (isset($S[$U][$j][$i])) {
            $Out .= $S[$U][$j][$i];
          } else {
            $Out .= '0';
            }
        if ($i < $Count - 1) $Out .= ',';
        }
      $Out .= ');' . "\n";
      }
    }
  return(fwrite($Fout,$Out));
  }

function AnalyzeLogfile() {
  global $Name, $SLastM, $SLastU, $SLastT, $SLastI, $SLastC, $SLastCi,
    $SHourC, $SHourT, $SDayC, $SDayT, $SWeekC, $SWeekT,
    $SDowC, $SDowT, $SHodC, $SHodT, $SDist, $LogOffset, $LogDate,
    $FontTitle, $FontOther;

  $LogFile = 'Logs/' . base64_encode($Name) . '.log';
  $NewFile = 'Logs/' . base64_encode($Name) . '.new';
  $DatFile = 'Logs/' . base64_encode($Name) . '.dat';

  /* If the logfile has exactly the same size as the last time then
     no new measurements have been added, so leave. */
  $n1 = @filesize($LogFile);
  if ($n1 === FALSE) {
    PageHeader('');
    echo '<br>' . "\n";
    echo '<center>' . "\n";
    echo $FontTitle . 'SiteSpeed statistics</b></font><br>' . "\n";
    echo $FontOther . 'for page: <b>' . $Name . '</b></font><br>' . "\n";
    PageLine();
    echo 'Statistics are not yet available because no measurements have been recorded yet.<br><br><br>' . "\n";
    PageFooter();
    return;
    }
  if (($LogOffset > 0) && ($LogOffset == $n1)) return;

  /* Open the logfile and jump to the spot where we left the last time,
     unless the logfile is smaller than before (maybe it was deleted,
     or restored). */
  $Fin = @fopen($LogFile,"r");
  if ($Fin === FALSE) return;
  if (($LogOffset > 0) &&
      ($LogOffset < $n1) &&
      (fseek($Fin,$LogOffset,SEEK_SET) != 0)) return;

  /* If the new datafile exists then another process is already
     analyzing the logfile. Exit with a progress message. */
  if ((file_exists($NewFile) === TRUE) &&
      (time() - filemtime($NewFile) > 15)) unlink($NewFile);
  if (file_exists($NewFile) === TRUE) {
    @fclose($Fin);
    PageHeader('5; url=' . $_SERVER['PHP_SELF'] . '?Name=' . rawurlencode($Name));
    echo '<br><center>' . "\n";
    echo '<b>Analyzing logfile</b><p>' . "\n";
    echo number_format($LogOffset) . ' bytes processed<br>' . "\n";
    echo number_format(filesize($LogFile) - $LogOffset) . ' bytes to go<p>' . "\n";
    echo 'Please wait...<br><br><br>';
    PageFooter();
    return;
    }

  /* Create the new datafile. This is used to prevent two instances
     from running simultaneously, primitive, but it works. */
  $Fout = @fopen($NewFile,"w");
  if ($Fout === FALSE) return;

  /* Walk through all the measurements in the logfile. */
  $StartTime = time() + 5;
  $Line = TRUE;
  while ($StartTime > time()) {
    $Line = fgets($Fin,50000);
    if ($Line === FALSE) break;
    $a = explode(' ',rtrim($Line));
    if (($a[0] == '') || ($a[0] == 0)) continue;
    if ($a[0] <= $LogDate) continue;
    if (($a[1] < 0) || ($a[1] > 60000)) continue;
    $LogDate = $a[0];
    $U = '0';                                        /* Assume it's a Unique. */
    if ((isset($SLastM[$a[2]])) && ($SLastM[$a[2]] >= $a[0] - 14400)) {
      $U = '1';                                      /* No, it's a Reload. */
      }
    $SLastM[$a[2]] = $a[0];
    $SLastT[$SLastI] = $a[1];
    $SLastU[$SLastI] = $U;
    $SLastI++;
    if ($SLastI >= 200) $SLastI = 0;
    $i = floor($a[0] / 3600);
    if (isset($SHourC[$U][$i])) {
        $SHourC[$U][$i]++;
        $SHourT[$U][$i] += $a[1];
      } else {
        $SHourC[$U][$i] = 1;
        $SHourT[$U][$i] = $a[1];
        }
    $i = floor($a[0] / 86400);
    if (isset($SDayC[$U][$i])) {
        $SDayC[$U][$i]++;
        $SDayT[$U][$i] += $a[1];
      } else {
        $SDayC[$U][$i] = 1;
        $SDayT[$U][$i] = $a[1];
        }
    $i = floor($a[0] / 604800);
    if (isset($SWeekC[$U][$i])) {
        $SWeekC[$U][$i]++;
        $SWeekT[$U][$i] += $a[1];
      } else {
        $SWeekC[$U][$i] = 1;
        $SWeekT[$U][$i] = $a[1];
        }
    $i = floor($a[0] / 2419200);
    $j = date('G',$a[0]) + 24 * date('w',$a[0]);
    if (isset($SDowC[$U][$i][$j])) {
        $SDowC[$U][$i][$j]++;
        $SDowT[$U][$i][$j] += $a[1];
      } else {
        $SDowC[$U][$i][$j] = 1;
        $SDowT[$U][$i][$j] = $a[1];
        }
    $j = floor((date('i',$a[0]) + 60 * date('G',$a[0])) / 10);
    if (isset($SHodC[$U][$i][$j])) {
        $SHodC[$U][$i][$j]++;
        $SHodT[$U][$i][$j] += $a[1];
      } else {
        $SHodC[$U][$i][$j] = 1;
        $SHodT[$U][$i][$j] = $a[1];
        }
    $j = floor($a[1] / 125);
    if (isset($SDist[$U][$i][$j])) {
        $SDist[$U][$i][$j]++;
      } else {
        $SDist[$U][$i][$j] = 1;
        }
    }
  if ($Line === FALSE) {
      $LogOffset = filesize($LogFile);
      $Finished = TRUE;
    } else {
      $LogOffset = ftell($Fin);
      $Finished = FALSE;
      }
  @fclose($Fin);

  /* Write the data from memory to the temporary datafile. */
  $Out = '<?php' . "\n" .
    '/*' . "\n" .
    '   Statistics for URL: ' . $Name . "\n" .
    '*/' . "\n" .
    '$LogOffset = ' . $LogOffset . ';' . "\n" .
    '$LogDate = ' . $LogDate . ';' . "\n";
  $Result = fwrite($Fout,$Out);
  if (($Result !== FALSE) && (isset($SLastM))) {
    arsort($SLastM);
    reset($SLastM);
    $i = 1;
    $Out = '$SLastM = array(';
    foreach($SLastM as $a => $b) {
      if ($i > 1) $Out .= ',';
      $Out .= '"' . $a . '"=>' . $b;
      if ($i++ > 1000) break;
      }
    $Out .= ');' . "\n";
    if ($Result !== FALSE) $Result = fwrite($Fout,$Out);
    }
  if ($Result !== FALSE) $Result = WriteArray1($Fout,$SLastT,'SLastT');
  if ($Result !== FALSE) $Result = WriteArray1($Fout,$SLastU,'SLastU');
  if ($SLastI == '') {
      if ($Result !== FALSE) $Result = fwrite($Fout,'$SLastI = 0;' . "\n");
    } else {
      if ($Result !== FALSE) $Result = fwrite($Fout,'$SLastI = ' . $SLastI . ';' . "\n");
      }
  $Max = floor(time() / 3600);
  if ($Result !== FALSE) $Result = WriteArray2($Fout,$SHourC,'SHourC',$Max);
  if ($Result !== FALSE) $Result = WriteArray2($Fout,$SHourT,'SHourT',$Max);
  $Max = floor(time() / 86400);
  if ($Result !== FALSE) $Result = WriteArray2($Fout,$SDayC,'SDayC',$Max);
  if ($Result !== FALSE) $Result = WriteArray2($Fout,$SDayT,'SDayT',$Max);
  $Max = floor(time() / 604800);
  if ($Result !== FALSE) $Result = WriteArray2($Fout,$SWeekC,'SWeekC',$Max);
  if ($Result !== FALSE) $Result = WriteArray2($Fout,$SWeekT,'SWeekT',$Max);
  $Max = floor(time() / 2419200);
  if ($Result !== FALSE) $Result = WriteArray3($Fout,$SDowC,'SDowC',$Max,168);
  if ($Result !== FALSE) $Result = WriteArray3($Fout,$SDowT,'SDowT',$Max,168);
  if ($Result !== FALSE) $Result = WriteArray3($Fout,$SHodC,'SHodC',$Max,144);
  if ($Result !== FALSE) $Result = WriteArray3($Fout,$SHodT,'SHodT',$Max,144);
  if ($Result !== FALSE) $Result = WriteArray3($Fout,$SDist,'SDist',$Max,240);
  if ($Result !== FALSE) $Result = fwrite($Fout,'?>' . "\n");
  if ($Result !== FALSE) $Result = fflush($Fout);
  if ($Result !== FALSE) $Result = fclose($Fout);
  if ($Result !== FALSE) {
    if (file_exists($DatFile) == TRUE) unlink($DatFile);
    rename($NewFile,$DatFile);
    }

  if ($Finished === FALSE) {
    PageHeader('1; url=' . $_SERVER['PHP_SELF'] . '?Name=' . rawurlencode($Name));
    echo '<br><center>' . "\n";
    echo '<b>Analyzing logfile</b><p>' . "\n";
    echo number_format($LogOffset) . ' bytes processed<br>' . "\n";
    echo number_format(filesize($LogFile) - $LogOffset) . ' bytes to go<p>' . "\n";
    echo 'Please wait...<br><br><br>';
    PageFooter();
    }
  }




/* Encode a long into a string. Maximum integer: 49.787.136 */
function Encode83($Value) {
  $Trans = array(
    "!","#","$","%","(",")","*",",","-",".",
    "0","1","2","3","4","5","6","7","8","9",":",";","=",
    "@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
    "P","Q","R","S","T","U","V","W","X","Y","Z","[","]","^","_",
    "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
    "p","q","r","s","t","u","v","w","x","y","z","{","|","}","~");
  if (($Value > 47458321) || ($Value < 0)) return('!!!!');
  return($Trans[$Value / 571787] .
         $Trans[($Value % 571787) / 6889] .
         $Trans[($Value % 6889) / 83] .
         $Trans[$Value % 83]);
  }




function GenerateGraph($Select) {
  global $Name, $SLastM, $SLastU, $SLastT, $SLastI, $SLastC, $SLastCi,
    $SHourC, $SHourT, $SDayC, $SDayT, $SWeekC, $SWeekT,
    $SDowC, $SDowT, $SHodC, $SHodT, $SDist,
    $FontOther;

  $Out = '';

  /* Construct the graph. */
  $ScriptName = $_SERVER['PHP_SELF'];
  if (strrpos($ScriptName,'/') == FALSE) {
      $Codebase = '/';
    } else {
      $Codebase = substr($ScriptName,0,strrpos($ScriptName,'/')+1);
      }
  $Out .= '<applet code="Graph.class" archive="Graph.jar" codebase="' . $Codebase . '" width=469 height=275>';
  $Out .= '<param name="java_code" value="Graph.class">' . "\n";
  $Out .= '<param name="java_codebase" value="' . $Codebase . '">' . "\n";
  $Out .= '<param name="java_type" value="application/x-java-applet;jpi-version=1.4">' . "\n";
  $Out .= '<param name=COLOR value="B4D5FF">' . "\n";
  if ($Select == 'Last') {
    $Out .= '<param name=TYPE1 value="1">' . "\n";
    $Out .= '<param name=TYPE2 value="3">' . "\n";
    $Out .= '<param name=TITLE value="Last 200 measurements">' . "\n";
    $Legend = "The last 200 measurements.";
    $Out1 = '';
    $Out2 = '';
    for ($i = 0; $i < 3; $i++) {
      $TotalCounts[$i] = 0;
      $TotalTimes[$i] = 0;
      }
    $j = $SLastI + 1;
    for ($i = 0; $i < 200; $i++) {
      if ($j >= 200) $j = 0;
      $Value = 0;
      if (isset($SLastT[$j])) $Value = $SLastT[$j];
      if ((isset($SLastU[$j])) && ($SLastU[$j] == 1)) {
          $Out1 .= Encode83(0);
          $Out2 .= Encode83($Value);
          if ($Value > 0) {
            $TotalCounts[1] += 1;
            $TotalTimes[1] += $Value;
            }
        } else {
          $Out1 .= Encode83($Value);
          $Out2 .= Encode83(0);
          if ($Value > 0) {
            $TotalCounts[0] += 1;
            $TotalTimes[0] += $Value;
            }
          }
      if ($Value > 0) {
        $TotalCounts[2] += 1;
        $TotalTimes[2] += $Value;
        }
      $j++;
      }
    $Out .= '<param name="GRAPH1" value="' . $Out1 . '">' . "\n";
    $Out .= '<param name="GRAPH2" value="' . $Out2 . '">' . "\n";
    for ($i = 0; $i < 3; $i++) {
      $TotalAverage[$i] = 0;
      if ($TotalCounts[$i] > 0) {
        $TotalAverage[$i] = floor($TotalTimes[$i] / $TotalCounts[$i]);
        }
      }
    }
  if (($Select == 'Hour') || ($Select == 'Day') || ($Select == 'Week')) {
    if ($Select == 'Hour') {
      $Out .= '<param name=TYPE1 value="2">' . "\n";
      $Out .= '<param name=TYPE2 value="1">' . "\n";
      $Out .= '<param name=TITLE value="Speed per hour">' . "\n";
      $Legend = "Average download time (seconds) per hour for the last 200 hours (8 days).";
      $Now = floor(time() / 3600);
      $S1 = $SHourC;
      $S2 = $SHourT;
      }
    if ($Select == 'Day') {
      $Out .= '<param name=TYPE1 value="3">' . "\n";
      $Out .= '<param name=TYPE2 value="1">' . "\n";
      $Out .= '<param name=TITLE value="Speed per day">' . "\n";
      $Legend = "Average download time (seconds) per day for the last 200 days (6 months).";
      $Now = floor(time() / 86400);
      $S1 = $SDayC;
      $S2 = $SDayT;
      }
    if ($Select == 'Week') {
      $Out .= '<param name=TYPE1 value="4">' . "\n";
      $Out .= '<param name=TYPE2 value="1">' . "\n";
      $Out .= '<param name=TITLE value="Speed per week">' . "\n";
      $Legend = "Average download time (seconds) per week for the last 200 weeks (4 years).";
      $Now = floor(time() / 604800);
      $S1 = $SWeekC;
      $S2 = $SWeekT;
      }
    $Out1 = '';
    $Out2 = '';
    $Out3 = '';
    for ($i = 0; $i < 3; $i++) {
      $TotalCounts[$i] = 0;
      $TotalTimes[$i] = 0;
      }
    if (isset($S1)) for ($i = $Now - 199; $i <= $Now; $i++) {
      $Value1 = 0;
      $Value2 = 0;
      $Value3 = 0;
      if ((isset($S1[0][$i])) && ($S1[0][$i] > 0)) {
        $Value1 = floor($S2[0][$i] / $S1[0][$i]);
        }
      if ((isset($S1[1][$i])) && ($S1[1][$i] > 0)) {
        $Value2 = floor($S2[1][$i] / $S1[1][$i]);
        }
      if ((isset($S1[0][$i])) &&
          (isset($S1[1][$i])) &&
          ($S1[0][$i] + $S1[1][$i] > 0)) {
        $Value3 = floor(($S2[0][$i] + $S2[1][$i]) / ($S1[0][$i] + $S1[1][$i]));
        }
      $Out1 .= Encode83($Value1);
      $Out2 .= Encode83($Value2);
      $Out3 .= Encode83($Value3);
      if (isset($S1[0][$i])) {
        if (isset($S1[0][$i])) $TotalCounts[0] += $S1[0][$i];
        if (isset($S2[0][$i])) $TotalTimes[0] += $S2[0][$i];
        if (isset($S1[1][$i])) $TotalCounts[1] += $S1[1][$i];
        if (isset($S2[1][$i])) $TotalTimes[1] += $S2[1][$i];
        if ((isset($S1[0][$i])) && (isset($S1[1][$i]))) {
          $TotalCounts[2] += $S1[0][$i] + $S1[1][$i];
          }
        if ((isset($S2[0][$i])) && (isset($S2[1][$i]))) {
          $TotalTimes[2] += $S2[0][$i] + $S2[1][$i];
          }
        }
      }
    $Out .= '<param name="GRAPH1" value="' . $Out1 . '">' . "\n" .
      '<param name="GRAPH2" value="' . $Out2 . '">' . "\n" .
      '<param name="GRAPH3" value="' . $Out3 . '">' . "\n";
    for ($i = 0; $i < 3; $i++) {
      $TotalAverage[$i] = 0;
      if ($TotalCounts[$i] > 0) {
        $TotalAverage[$i] = floor($TotalTimes[$i] / $TotalCounts[$i]);
        }
      }
    }
  if ($Select == 'WeekDay') {
    $Out .= '<param name=TYPE1 value="5">' . "\n";
    $Out .= '<param name=TYPE2 value="1">' . "\n";
    $Out .= '<param name=TITLE value="Speed during a week">' . "\n";
    $Legend = "Average download time (seconds) per day of the week, averaged over the last 8 weeks.";
    $Out1 = '';
    $Out2 = '';
    $Out3 = '';
    for ($i = 0; $i < 3; $i++) {
      $TotalCounts[$i] = 0;
      $TotalTimes[$i] = 0;
      }
    $j = floor(time() / 2419200);
    for ($i = 0; $i < 168; $i++) {
      $Counts1 = 0;
      $Times1 = 0;
      $Counts2 = 0;
      $Times2 = 0;
      for ($k = $j - 10; $k <= $j; $k++) {
        if (isset($SDowC[0][$k][$i])) {
          $Counts1 += $SDowC[0][$k][$i];
          $Times1 += $SDowT[0][$k][$i];
          }
        if (isset($SDowC[1][$k][$i])) {
          $Counts2 += $SDowC[1][$k][$i];
          $Times2 += $SDowT[1][$k][$i];
          }
        }
      $Value1 = 0;
      if ($Counts1 > 0) $Value1 = floor($Times1 / $Counts1);
      $Value2 = 0;
      if ($Counts2 > 0) $Value2 = floor($Times2 / $Counts2);
      $Value3 = 0;
      if ($Counts1 + $Counts2 > 0) $Value3 = floor(($Times1 + $Times2) / ($Counts1 + $Counts2));
      $Out1 .= Encode83($Value1);
      if ($i == 0) $Value10 = $Value1;
      $Out2 .= Encode83($Value2);
      if ($i == 0) $Value20 = $Value2;
      $Out3 .= Encode83($Value3);
      if ($i == 0) $Value30 = $Value3;
      $TotalCounts[0] += $Counts1;
      $TotalTimes[0] += $Times1;
      $TotalCounts[1] += $Counts2;
      $TotalTimes[1] += $Times2;
      $TotalCounts[2] += $Counts1 + $Counts2;
      $TotalTimes[2] += $Times1 + $Times2;
      }
    $Out1 .= Encode83($Value10);
    $Out2 .= Encode83($Value20);
    $Out3 .= Encode83($Value30);
    $Out .= '<param name="GRAPH1" value="' . $Out1 . '">' . "\n" .
      '<param name="GRAPH2" value="' . $Out2 . '">' . "\n" .
      '<param name="GRAPH3" value="' . $Out3 . '">' . "\n";
    for ($k = 0; $k < 3; $k++) {
      $TotalAverage[$k] = 0;
      if ($TotalCounts[$k] > 0) {
        $TotalAverage[$k] = floor($TotalTimes[$k] / $TotalCounts[$k]);
        }
      }
    }
  if ($Select == 'DayHour') {
    $Out .= '<param name=TYPE1 value="6">' . "\n";
    $Out .= '<param name=TYPE2 value="1">' . "\n";
    $Out .= '<param name=TITLE value="Speed during a day">' . "\n";
    $Legend = "Average download time (seconds) per hour of the day, averaged over the last 8 weeks.";
    $Out1 = '';
    $Out2 = '';
    $Out3 = '';
    for ($i = 0; $i < 3; $i++) {
      $TotalCounts[$i] = 0;
      $TotalTimes[$i] = 0;
      }
    $j = floor(time() / 2419200);
    for ($i = 0; $i < 144; $i++) {
      $Counts1 = 0;
      $Times1 = 0;
      $Counts2 = 0;
      $Times2 = 0;
      for ($k = $j - 10; $k <= $j; $k++) {
        if (isset($SHodC[0][$k][$i])) {
          $Counts1 += $SHodC[0][$k][$i];
          $Times1 += $SHodT[0][$k][$i];
          }
        if (isset($SHodC[1][$k][$i])) {
          $Counts2 += $SHodC[1][$k][$i];
          $Times2 += $SHodT[1][$k][$i];
          }
        }
      $Value1 = 0;
      if ($Counts1 > 0) $Value1 = floor($Times1 / $Counts1);
      $Value2 = 0;
      if ($Counts2 > 0) $Value2 = floor($Times2 / $Counts2);
      $Value3 = 0;
      if ($Counts1 + $Counts2 > 0) $Value3 = floor(($Times1 + $Times2) / ($Counts1 + $Counts2));
      $Out1 .= Encode83($Value1);
      if ($i == 0) $Value10 = $Value1;
      $Out2 .= Encode83($Value2);
      if ($i == 0) $Value20 = $Value2;
      $Out3 .= Encode83($Value3);
      if ($i == 0) $Value30 = $Value3;
      $TotalCounts[0] += $Counts1;
      $TotalTimes[0] += $Times1;
      $TotalCounts[1] += $Counts2;
      $TotalTimes[1] += $Times2;
      $TotalCounts[2] += $Counts1 + $Counts2;
      $TotalTimes[2] += $Times1 + $Times2;
      }
    $Out1 .= Encode83($Value10);
    $Out2 .= Encode83($Value20);
    $Out3 .= Encode83($Value30);
    $Out .= '<param name="GRAPH1" value="' . $Out1 . '">' . "\n" .
      '<param name="GRAPH2" value="' . $Out2 . '">' . "\n" .
      '<param name="GRAPH3" value="' . $Out3 . '">' . "\n";
    for ($k = 0; $k < 3; $k++) {
      $TotalAverage[$k] = 0;
      if ($TotalCounts[$k] > 0) {
        $TotalAverage[$k] = floor($TotalTimes[$k] / $TotalCounts[$k]);
        }
      }
    }
  if ($Select == 'Distribution') {
    $Out .= '<param name=TYPE1 value="7">' . "\n";
    $Out .= '<param name=TYPE2 value="2">' . "\n";
    $Out .= '<param name=TITLE value="Speed distribution">' . "\n";
    $Legend = "Speed distribution during the last 8 weeks.";
    $Legend .= " The graph shows the number of measurements (vertical) and their time (horizontal, in seconds). For example: a measurement of 9.081 seconds will raise the graph vertically by 1 at horizontal position 9.";
    $Out1 = '';
    $Out2 = '';
    $Out3 = '';
    for ($i = 0; $i < 3; $i++) {
      $TotalCounts[$i] = 0;
      $TotalTimes[$i] = 0;
      $TopTime[$i] = 0;
      $TopCount[$i] = 0;
      $MeanTime[$i] = 0;
      $MeanCount[$i] = 0;
      }
    $j = floor(time() / 2419200);
    for ($i = 0; $i < 240; $i++) {
      $Value1 = 0;
      $Value2 = 0;
      for ($k = $j - 10; $k <= $j; $k++) {
        if (isset($SDist[0][$k][$i])) $Value1 += $SDist[0][$k][$i];
        if (isset($SDist[1][$k][$i])) $Value2 += $SDist[1][$k][$i];
        }
      $Out1 .= Encode83($Value1);
      $Out2 .= Encode83($Value2);
      $Out3 .= Encode83($Value1 + $Value2);
      $TotalCounts[0] += $Value1;
      $TotalTimes[0] += $Value1 * $i * 1000 / 8;
      if ($Value1 > $TopCount[0]) {
        $TopCount[0] = $Value1;
        $TopTime[0] = $i * 1000 / 8;
        }
      $TotalCounts[1] += $Value2;
      $TotalTimes[1] += $Value2 * $i * 1000 / 8;
      if ($Value2 > $TopCount[1]) {
        $TopCount[1] = $Value2;
        $TopTime[1] = $i * 1000 / 8;
        }
      $TotalCounts[2] += $Value1 + $Value2;
      $TotalTimes[2] += ($Value1 + $Value2) * $i * 1000 / 8;
      if ($Value1 + $Value2 > $TopCount[2]) {
        $TopCount[2] = $Value1 + $Value2;
        $TopTime[2] = $i * 1000 / 8;
        }
      }
    $Out .= '<param name="GRAPH1" value="' . $Out1 . '">' . "\n";
    $Out .= '<param name="GRAPH2" value="' . $Out2 . '">' . "\n";
    $Out .= '<param name="GRAPH3" value="' . $Out3 . '">' . "\n";

    for ($i = 0; $i < 3; $i++) {
      $TotalAverage[$i] = 0;
      if ($TotalCounts[$i] > 0) {
        $TotalAverage[$i] = floor($TotalTimes[$i] / $TotalCounts[$i]);
        }
      }
    for ($i = 0; $i < 240; $i++) {
      $Value1 = 0;
      $Value2 = 0;
      for ($k = $j - 10; $k <= $j; $k++) {
        if (isset($SDist[0][$k][$i])) $Value1 += $SDist[0][$k][$i];
        if (isset($SDist[1][$k][$i])) $Value2 += $SDist[1][$k][$i];
        }
      $MeanCount[0] += $Value1;
      $MeanCount[1] += $Value2;
      $MeanCount[2] += $Value1 + $Value2;
      if ($MeanCount[0] <= $TotalCounts[0] / 2) $MeanTime[0] = $i * 1000 / 8;
      if ($MeanCount[1] <= $TotalCounts[1] / 2) $MeanTime[1] = $i * 1000 / 8;
      if ($MeanCount[2] <= $TotalCounts[2] / 2) $MeanTime[2] = $i * 1000 / 8;
      }
    }

  $Out .= '<table bgcolor="#FFD900" width=469 height=275>' . "\n";
  $Out .= '<tr>' . "\n";
  $Out .= '<td align=center valign=center>' . "\n";
  $Out .= $FontOther . "\n";
  $Out .= 'Sorry!<br>' . "\n";
  $Out .= 'To see this graph you need Java.' . "\n";
  $Out .= 'Here are some tips to get Java working on your computer:' . "\n";
  $Out .= '<ul>' . "\n";
  $Out .= '<li>Java is not installed (you can check this by entering "jview"' . "\n";
  $Out .= 'on the Windows command prompt).' . "\n";
  $Out .= '  Install the latest Java version from Sun. It\'s a free update' . "\n";
  $Out .= '  which you can' . "\n";
  $Out .= '  <img border=0 width=24 height=10 src="img/here.gif" alt=" * ">' . "\n";
  $Out .= '  <a href="http://java.sun.com/webapps/getjava/BrowserRedirect?host=www.java.com:80">download from Sun\'s website.</a>' . "\n";
  $Out .= '<li>Java is installed but disabled. In Internet Explorer open' . "\n";
  $Out .= '  the internet options, "Advanced" tab, look for a heading called' . "\n";
  $Out .= '  "Microsoft VM", and check the "JIT compiler" item. Also see' . "\n";
  $Out .= '  the "Security" tab, custom-level button, a heading called' . "\n";
  $Out .= '  "Scripting", check the "Scripting of Java applets" item.' . "\n";
  $Out .= '<li>Settings are damaged. In Internet Explorer open the internet' . "\n";
  $Out .= '  options, "Programs" tab, click the "Reset Web Settings" button.' . "\n";
  $Out .= '<li>Browser is damaged. For Internet Explorer open the Control' . "\n";
  $Out .= '  Panel, "Add/Remove programs", select "Microsoft Internet' . "\n";
  $Out .= '  Explorer", "Change/remove" button, "Repair Internet Explorer"' . "\n";
  $Out .= '  option.' . "\n";
  $Out .= '<li>Some firewalls will block Java applets. You will have to ask' . "\n";
  $Out .= '  your system administrator.' . "\n";
  $Out .= '</ul>' . "\n";
  $Out .= '</font>' . "\n";
  $Out .= '</td>' . "\n";
  $Out .= '</tr>' . "\n";
  $Out .= '</table>' . "\n";
  $Out .= '</applet>';
  $Out .= '<p>';


  $Out .= $FontOther . $Legend . '</font><p>' . "\n";
  $Out .= '<table border=0>' . "\n";
  if ($Select == 'Distribution') {
    $Out .= '<tr>' . "\n";
    $Out .= '  <td>&nbsp;</td>' . "\n";
    $Out .= '  <td></td>' . "\n";
    $Out .= '  <td>' . $FontOther . 'Average</font></td>' . "\n";
    $Out .= '  <td>' . $FontOther . 'Middle</font></td>' . "\n";
    $Out .= '  <td>' . $FontOther . 'Most</font></td>' . "\n";
    $Out .= '  </tr>' . "\n";
    }
  $Out .= '<tr>' . "\n";
  $Out .= '  <td bgcolor="#FFD900">&nbsp;&nbsp;&nbsp;&nbsp;</font></td>' . "\n";
  $Out .= '  <td>' . $FontOther . 'Uniques</font></td>' . "\n";
  $Out .= '  <td>' . $FontOther . sprintf("%.03f",$TotalAverage[0] / 1000) . ' seconds</font></td>' . "\n";
  if ($Select == 'Distribution') {
    $Out .= '  <td>' . $FontOther . sprintf("%.03f",$MeanTime[0] / 1000) . ' seconds</font></td>' . "\n";
    $Out .= '  <td>' . $FontOther . sprintf("%.03f",$TopTime[0] / 1000) . ' seconds</font></td>' . "\n";
    }
  $Out .= '  </tr>' . "\n";
  if ($Select != 'Last') {
    $Out .= '<tr>' . "\n";
    $Out .= '  <td bgcolor="#00FF00">&nbsp;</font></td>' . "\n";
    $Out .= '  <td>' . $FontOther . 'Impressions</font></td>' . "\n";
    $Out .= '  <td>' . $FontOther . sprintf("%.03f",$TotalAverage[2] / 1000) . ' seconds</font></td>' . "\n";
    if ($Select == 'Distribution') {
      $Out .= '  <td>' . $FontOther . sprintf("%.03f",$MeanTime[2] / 1000) . ' seconds</font></td>' . "\n";
      $Out .= '  <td>' . $FontOther . sprintf("%.03f",$TopTime[2] / 1000) . ' seconds</font></td>' . "\n";
      }
    $Out .= '  </tr>' . "\n";
    }
  $Out .= '<tr>' . "\n";
  $Out .= '  <td bgcolor="#00D9FF">&nbsp;</font></td>' . "\n";
  $Out .= '  <td>' . $FontOther . 'Reloads</font></td>' . "\n";
  $Out .= '  <td>' . $FontOther . sprintf("%.03f",$TotalAverage[1] / 1000) . ' seconds</font></td>' . "\n";
  if ($Select == 'Distribution') {
    $Out .= '  <td>' . $FontOther . sprintf("%.03f",$MeanTime[1] / 1000) . ' seconds</font></td>' . "\n";
    $Out .= '  <td>' . $FontOther . sprintf("%.03f",$TopTime[1] / 1000) . ' seconds</font></td>' . "\n";
    }
  $Out .= '  </tr>' . "\n";
  if (($Select == 'WeekDay') || ($Select == 'DayHour')) {
    $Out .= '<tr>' . "\n";
    $Out .= '  <td bgcolor="#FFFFFF">&nbsp;</font></td>' . "\n";
    $Out .= '  <td>' . $FontOther . 'Now</font></td>' . "\n";
    $Out .= '  </tr>' . "\n";
    }
  $Out .= '</table>' . "\n";
  if ($Select == 'Distribution') {
    $Out .= $FontOther . "\n";
    $Out .= '<b>Middle:</b> 50% of the visitors are faster than this speed.<br>' . "\n";
    $Out .= '<b>Most:</b> the graph peaks at this speed.' . "\n";
    $Out .= '</font>' . "\n";
    }

  return($Out);
  }




/* Include the pre-processed data. The aggregated graphs are stored
   in arrays in a PHP include file which is automatically generated
   when the logfile is analysed, see below. */
$SLastI = 0;
$LogOffset = 0;
$LogDate = 0;
if (file_exists('Logs/' . base64_encode($Name) . '.dat') == TRUE) {
  include('Logs/' . base64_encode($Name) . '.dat');
  }



/* Process any pending measurements in the logfile. The measurements
   are aggregated into arrays in memory, the arrays directly map onto
   the graphs. For example, the by-hour graph shows the average of all
   the measurement in the last 168 hours, and is stored in an array
   exactly like that. The arrays are written into the ".dat" include
   file so that a later instance can pick it up. */
AnalyzeLogfile();



PageHeader('');
echo '<br>' . "\n";
echo '<center>' . "\n";
echo $FontTitle . 'SiteSpeed statistics</b></font><br>' . "\n";
echo $FontOther . 'for page: <b>' . $Name . '</b></font><br>' . "\n";
PageLine();
echo GenerateGraph('Hour');
PageLine();
echo GenerateGraph('Day');
PageLine();
echo GenerateGraph('Week');
PageLine();
echo GenerateGraph('Last');
PageLine();
echo GenerateGraph('WeekDay');
PageLine();
echo GenerateGraph('DayHour');
PageLine();
echo GenerateGraph('Distribution');
PageLine();
PageFooter();
?>