<protect pass=2>
<?
  # Supported variables:
  #   type    [access]  {transfer, access}
  #   period  [day]     {day, week, month, year, thismonth}
  #   site    []
  #   year    [this year]
  if (isset($_GET["type"]) && $_GET["type"] === 'transfer')
    $type = 'transfer';
  else
    $type = 'access';

  $periods = array('day','week','month','year','thismonth');
  if (isset($_GET["period"]) && in_array($_GET["period"], $periods, true) === true)
    $period = $_GET["period"];
  else
    $period = 'day';

  if (isset($_GET["site"]) && is_dir($_GET["site"]) && $_GET["site"][0] != '.')
    $site = $_GET["site"];
  else
    $site = '';

  $minyear = 2007;
  $maxyear = date("Y");
  if (isset($_GET["year"]) && is_numeric($_GET["year"]) && $_GET["year"] >= $minyear && $_GET["year"] <= $maxyear)
    $year = $_GET["year"];
  else
    unset ($year);

  $thispage = explode('/', $_SERVER['SCRIPT_NAME']);
  $thispage = $thispage[count($thispage)-1];
?>
</protect>

#include <infodrom.style>

<page func="Infodrom Oldenburg" title="Statistics">

<protect pass=2>
<?
  function emit_image($site, $type, $period, $link)
  {
    $fname = sprintf ("%s/%s-%s.png", $site, $type, $period);
    if (!is_file($fname)) return;
    $size = getimagesize($fname);

    echo ('<div style="padding-top: 1em"></div>');
    echo ('<div align="center">');
    if (!empty($link))
      printf ('<a href="%s">', $link);
    printf ('<img src="%s" %s border="0" />',
      $fname, $size);
    if (!empty($link))
      echo ('</a>');
    echo ('</div>');
  }

  $sites = array();
  $dir = opendir('.');
  while (($dent = readdir($dir)) !== false) {
    if (!is_dir($dent) || $dent[0] == '.') continue;
    if ($dent === 'total') continue;
    $sites[] = $dent;
  }
  closedir ($dir);
  asort ($sites);

  if (empty($site)) {
    foreach ($sites as $site) {
      $link = sprintf("%s?type=%s&site=%s", $thispage, $type, urlencode($site));
      emit_image ($site, $type, $period, $link);
    }
    $link = sprintf("%s?type=%s&site=%s", $thispage, $type, 'total');
    emit_image ('total', $type, $period, $link);
  } else {
    if (isset($year)) {
      for ($mon = 1; $mon <= 12; $mon++) {
        emit_image($site, $type, sprintf('%04d-%02d', $year, $mon), '');
      }
    } else {
      foreach ($periods as $period) {
        if ($period == 'thismonth') {
          $link = sprintf("%s?type=%s&period=thismonth&site=%s", $thispage, $type, urlencode($site));
          emit_image ($site, $type, $period, $link);
        } else
          emit_image ($site, $type, $period, '');
      }
    }
  }
?>
</protect>

<div style="padding-top: 1em"></div>

</page>

# Local variables:
# mode: html
# mode: auto-fill
# end:
