<?php

###############################################################################
## Fancy Speed Test - Easily measure your upload and download speeds
## Home Page:   http://www.brandonchecketts.com/speedtest/
## Author:      Brandon Checketts
## File:        index.php
## Version:     1.1
## Date:        2006-02-06
## Purpose:     Display a welcome page, or redirect straight to the
##              download test if auto_start is enabled
###############################################################################

class ModuleSpeedTest extends Module
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'SpeedTest';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Testing of download and upload bandwidth';
  }

  function DoStart(): void
  {
    $this->System->Pages['speedtest'] = 'PageSpeedTest';
  }
}

class PageSpeedTest extends Page
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Title = 'Test rychlosti';
    $this->Description = 'Test rychlosti připojení';
    $this->ParentClass = 'PagePortal';
  }

  function Show(): string
  {
    if (count($this->System->PathItems) > 1)
    {
      if ($this->System->PathItems[1] == 'download.php') return $this->ShowDownload();
      //else if ($this->System->PathItems[1] == 'rss') return $this->ShowRSS();
      else return PAGE_NOT_FOUND;
    } else return $this->ShowMain();
  }

  function ShowDownload(): void
  {
    global $config;
    require_once(dirname(__FILE__)."/download.php");

  }

  function ShowMain(): string
  {
    global $config;

    require_once(dirname(__FILE__)."/common.php");
    ReadConfig(dirname(__FILE__)."/speedtest.cfg");

## Redirect immediately to download.php if auto_start = 1
if ($config->{'general'}->{'auto_start'}) {
    Header("Location: ".$config['general']['base_url']."/download.php");
    exit;
}

$Output = '<html>
<head>
<title>'.$config->{'general'}->{'page_title'}.' - Fancy Speed Test</title>
<meta http-equiv="Expires" CONTENT="Fri, Jan 1 1980 00:00:00 GMT" />
<meta http-equiv="Pragma" CONTENT="no-cache" />
<meta http-equiv="Cache-Control" CONTENT="no-cache" />
<link rel="stylesheet" href="style.css" />
</head>
<body>';

if (file_exists("header.html")) {
    ## Include "header.html" for a custom header, if the file exists
    include("header.html");
} else {
    ## Else just print a plain header
    print "<center>\n";
}
$Output .= '<div id="speedtest_content">';

if (file_exists("welcome.html")) {
    ## Include "welcome.html" for a custom welcome page, if the file exists
    include("welcome.html");
} else {
    ## Else print a standard welcome message

$Output .= '<center>
<div style="width: 400px;">
Welcome to our speedtest.  This test will measure your connection speed to and
from our server by downloading some data from our server, and then uploading it
back to the server.  The test should take approximately 30 seconds to complete.
</div>
</center>';
}

$Output .= '<center><h2><a class="start_test" title="Begin Speed Test" href="'.$config->{'general'}->{'base_url'}.'/download.php">Start Test</a></h2></center>
<div id="speedtest_credits">
    Powered by <a title="Brandon Checketts Fancy Source Speedtest" href="http://www.brandonchecketts.com/speedtest/" target="_new">Fancy Speed Test</a>
</div>
</div>';

if (file_exists("footer.html")) {
    ## Include "footer.html" for a custom footer, if the file exists
    include("footer.html");
} else {
    ## Else just print a plain footer
    print "</center>\n";
}
    return $Output;
  }
}

