<?php
header("Content-Type: text/html");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header("Access-Control-Allow-Origin: *");

clearstatcache();

#phpinfo();

$serverbaseurl = "http://scherwel.dyndns.org:8000";
#$serverbaseurl = "http://localhost:80";
    
$MAX_SD_CARD_USAGE = 98;

// check responsetime for a webbserver
function pingDomain($domain)
{
    $starttime = microtime(true);
    
    // supress error messages with @
    $fp      = @fsockopen($domain, 80, $errno, $errstr, 5);
    #stream_set_timeout($fp, 5);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$fp)
    {
        $status = -1;  // Site is down
    }
    else
    {
        fclose($fp);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    
    return $status;
}

function getProxyImage($link, $file) # return image and end script
{
    # achtung: '&' in link muss mit '\' escaped sein
 
    unlink($file);
    $cmd = "wget -O $file $link";
    $res = exec($cmd);

    $fp = fopen($file, 'rb');
#echo("<h1>debug druckraumcam - fp=:$fp - cmd:'$cmd' - link:'$link' - file:'$file' - res='$res'</h1>");
#die(); 

    header('Content-type: image/jpeg;');

    while(!feof($fp))
    {
      print(fread($fp, 1024*32));
      flush();
      ob_flush();

      #usleep(2); # sleep 2ms
    }
    fclose($fp);
    
    die();
}

####

if (isset($_GET['fullscreen']))
{
  $res = shell_exec("/usr/bin/xte -x:0 'key F11'");
  echo("res: $res\n");
}

if (isset($_GET['clientinfo']))
{
  # http://192.168.2.109/test.php
  # http://scherwel.dyndns.org:8009/test.php
  
  $HTTPCLIENT = $_SERVER['HTTP_USER_AGENT'];
  $HTTPHOST = $_SERVER['HTTP_HOST'];
  $SERVER_ADDR = $_SERVER['SERVER_ADDR'];
  $SERVER_SIGNATURE = $_SERVER['SERVER_SIGNATURE'];
  $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];

  $freedisk = shell_exec("df -h");

  echo('<p>'
     . '<b>phpinfo:</b>'
     . '<hr>$_SERVER[\'HTTP_USER_AGENT\'] = ' . $HTTPCLIENT
     . '<hr>$_SERVER[\'HTTP_HOST\'] = ' . $HTTPHOST
     . '<hr>$_SERVER[\'SERVER_ADDR\'] = ' . $SERVER_ADDR
     . '<hr>$_SERVER[\'SERVER_SIGNATURE\'] = ' . $SERVER_SIGNATURE
     . '<hr>$_SERVER[\'REMOTE_ADDR\'] = ' . $REMOTE_ADDR
     . '<hr><b>df -h</b>'
     . '<pre>' . $freedisk . '</pre>'
     . '<hr></p>');
}

if (isset($_GET['zaehlerstand']))
{
  $device = "dev ???";
  $time = "time ???";
  $value = "value ???";
  $imgurl = "image ???";

  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
           
  if ($dyndns === "")
  {
    $server = "http://192.168.2.88/";
  }
  else
  {
    $server = "$serverbaseurl/";
  }

  if ($_GET['zaehlerstand'] == "wasseruhr")
  {
    $device = "Wasseruhr";
    $link = "$server/homeautomation/wasseruhr/wasseruhr.php";
    $imgurl = "$server/homeautomation/wasseruhrcam.php?getlastpic$dyndns&uid=" . time();
    $texturl = "$server/homeautomation/wasseruhrcam.php?getlastpictimespan$dyndns&uid=" . time();
    
    #$imgurl1 = "$server/homeautomation/wasseruhrcam-images/timeimg.jpg";
    $imgurl2 = "$server/homeautomation/wasseruhrcam-images/roi.jpg";
    $imgurl3 = "$server/homeautomation/wasseruhrcam-images/rotimg.jpg";
    $imgurl4 = "$server/homeautomation/wasseruhrcam-images/thresimg.jpg";

    $timespan = file_get_contents($texturl);
    
    echo("<center>"
       . "<h1>$device</h1>"
       . "<a href='$link'>detailierte Auswertung (n.y.i.)</a>"
       . "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "
       . "<a href='$imgurl'>Originalbild</a>"
       . "<br>"
       . "<h3>$timespan</h3>"   #"<img src='$imgurl1' width='650px' height='80px'>"
       . "<br>"
       . "<img src='$imgurl2' width='150px' height='175px'> &nbsp;&nbsp;&nbsp;"
       . "<img src='$imgurl3' width='200px' height='175px'> &nbsp;&nbsp;&nbsp;"
       . "<img src='$imgurl4' width='200px' height='175px'>"
       . "</center>");
  }
  else if ($_GET['zaehlerstand'] == "postcam")
  {
    $device = "Posteingang";
    $imgurl = "$server/homeautomation/postcam.php?getlastpic$dyndns&uid=" . time();

    echo("<center>"
       . "<h3>$device:</h3>"
       . "<hr><h3>$time   -   $value</h3>"
       . "<img src='$imgurl'>"
       . "</center>");
  }
  else if ($_GET['zaehlerstand'] == "aufzugmotorcam")
  {
    $url = "$server/homeautomation/aufzugmotorcam.php?getlastimgurls=5$dyndns&uid=" . time();
    $imgurls = file($url);
    #print_r($imgurls);
    
    $imglive = "http://192.168.2.96:81/stream?res=5&flash=1";
    $imgurl0 = str_replace("/media/md0/my_home/", $server, $imgurls[0]);
    $imgurl1 = str_replace("/media/md0/my_home/", $server, $imgurls[1]);
    $imgurl2 = str_replace("/media/md0/my_home/", $server, $imgurls[2]);
    $imgurl3 = str_replace("/media/md0/my_home/", $server, $imgurls[3]);
    $imgurl4 = str_replace("/media/md0/my_home/", $server, $imgurls[4]);

    $width  = 250; //2: 350
    $height = $width*3/4;
   
    $table = '<center><table>' 
           . ' <tr>'
           . '  <td>'
           . "<img src='$imglive' style='border: 10px solid #0f0;' width='" . $width ."px' height='" . $height . "px'>"
           . '  </td>'
           . '  <td>'
           . "<img src='$imgurl0' width='" . $width ."px' height='" . $height . "px'>"
           . '  </td>'
           . '  <td>'
           . "<img src='$imgurl1' width='" . $width ."px' height='" . $height . "px'>"
           . '  </td>'
           . ' </tr>'
           . ' <tr>'
           . '  <td>'
           . "<img src='$imgurl2' width='" . $width ."px' height='" . $height . "px'>"
           . '  </td>'
           . '  <td>'
           . "<img src='$imgurl3' width='" . $width ."px' height='" . $height . "px'>"
           . '  </td>'
           . '  <td>'
           . "<img src='$imgurl4' width='" . $width ."px' height='" . $height . "px'>"
           . '  </td>'
           . ' </tr>'
           . '</table></center>';
   
    echo("<center>"
       . "<h2>Aufzug Motor (die letzten Bilder)</h2>"
       . $table
       . "</center>");
  }
  else
  {
    $device = $_GET['zaehlerstand'];
    echo("<center>"
       . "<h3>Unbekanntes Device:</h3>"
       . "<h2>Unbekanntes Device: '$device'</h2>"
       . "</center>");
  }
}


if (isset($_GET['observation']))
{
  $save = 0;
  if (isset($_GET['save']))
    $save = $_GET['save'];
  $reset = 0;
  if (isset($_GET['reset']))
    $reset = $_GET['reset'];
    
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
  {
      $link1 = "http://192.168.2.88/homeautomation/observation/available_devices1.txt";
      $link2 = "http://192.168.2.88/homeautomation/observation/available_devices2.txt";
  }
  else
  {
      $link1 = "$serverbaseurl/homeautomation/observation/available_devices1.txt";
      $link2 = "$serverbaseurl/homeautomation/observation/available_devices2.txt";
  }
  
  $filecontent = file_get_contents($link1);
  $filecontent .= file_get_contents($link2);
  
/*
 unknown: a6:40:a0:7d:3c:d2
 ignored: a6:40:a0:7d:3c:d2
 name: liane
 offline: a6:40:a0:7d:3c:d2
 offline: iPhone
 name: iPad
*/
  
  $lines = explode("\n", $filecontent);

  $timestamp = trim($lines[0]);
  $unknown = array();
  $name = array();
  $offline = array();
  $ignored = array();
  
  for ($i = 1; $i<count($lines); $i++)
  {
     $line = $lines[$i];
    
     $arr = explode(": ", $line);
     #print_r($arr);
     if (count($arr) > 1)
     {
     $type = trim($arr[0]);
     $nameormac = trim($arr[1]);
     
     if ($type === "unknown")
     {
       if (strpos($nameormac, '(OFFLINE)') === false)
         array_push($unknown, $nameormac);
     }
     if ($type === "name")
      array_push($name, $nameormac);
     if ($type === "offline")
      array_push($offline, $nameormac);
     if ($type === "ignored")
      array_push($ignored, $nameormac);
    }
  }

  $content = "<center>";
  $content .= "<font size='5'>";
  $content .= "<table id='mactable' style='width:100%; font-size:18px; border:1px solid black;'>";
  $content .= "<caption>Aktive MAC-Adressen (" . $timestamp . "):</caption>";
  $content .= "<tr style='text-align:center; border:1px solid black;'>"
            . "<th style='text-align:center; border:1px solid black;'>Unbekannt</th>"
            . "<th style='text-align:center; border:1px solid black;'>Name</th>"
            . "<th style='text-align:center; border:1px solid black;'>Offline</th>"
            . "<th style='text-align:center; border:1px solid black;'>Ignoriert</th>"
            . "</tr>";
 
  $maxcount = max(count($unknown), count($name), count($offline), count($ignored));
  for ($i=0; $i<$maxcount; $i++)
  {
    $l1 = $l2 = $l3 = $l4 = "&nbsp;";

    if ($i < count($unknown)) $l1 = $unknown[$i];
    if ($i < count($name))    $l2 = $name[$i];
    if ($i < count($offline)) $l3 = $offline[$i];
    if ($i < count($ignored)) $l4 = $ignored[$i];
      
    $content .= "<tr style='text-align:center; border:1px solid black;'>"
              . "<td style='text-align:center; border:1px solid black;'>$l1</td>"
              . "<td style='text-align:center; border:1px solid black;'>$l2</td>"
              . "<td style='text-align:center; border:1px solid black;'>$l3</td>"
              . "<td style='text-align:center; border:1px solid black;'>$l4</td>"
              . "</tr>";
  }

  $content .= "</table>";
  if ($save === "true")
  {
    $list = "";
    for ($i=0; $i<count($unknown); $i++)
    {
      $list .= $unknown[$i] ."\n";
    }

    $list = str_replace("%20", '+', $list);
    $list = str_replace(' ', '+', $list);
    $list = str_replace("\n", '_', $list);
    $list = str_replace('#', ',', $list);
    $list = str_replace('(', 'QQQ', $list);
    $list = str_replace(')', 'RRR', $list);
    $list = str_replace(':', 'ZZZ', $list);

//echo();
//http://192.168.2.88/homeautomation/observation/logfile.php?info=2cZZZ3aZZZfdZZZ99ZZZ50ZZZ4d+QQQ1091RRR%20aaZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ1079RRR%20a6ZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ1079RRR%2078ZZZf8ZZZ82ZZZa5ZZZc1ZZZ1a+QQQOFFLINERRR
//http://192.168.2.88/homeautomation/observation/logfile.php?info=2cZZZ3aZZZfdZZZ99ZZZ50ZZZ4d+QQQ1901RRR aaZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ1889RRR a6ZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ1889RRR 78ZZZf8ZZZ82ZZZa5ZZZc1ZZZ1a+QQQOFFLINEERR
//http://192.168.2.88/homeautomation/observation/logfile.php?info=2cZZZ3aZZZfdZZZ99ZZZ50ZZZ4d+QQQ2851RRR aaZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ2839RRR a6ZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ2839RRR 78ZZZf8ZZZ82ZZZa5ZZZc1ZZZ1a+QQQOFFLINERRR ======2cZZZ3aZZZfdZZZ99ZZZ50ZZZ4d+QQQ2851RRR aaZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ2839RRR a6ZZZ40ZZZa0ZZZ7dZZZ3cZZZd2+QQQ2839RRR 78ZZZf8ZZZ82ZZZa5ZZZc1ZZZ1a+QQQOFFLINERRR 

    if ($dyndns === "")
      $logfile = "http://192.168.2.88/homeautomation/observation/logfile.php?info=$list";
    else
      $logfile = "$serverbaseurl/homeautomation/observation/logfile.php?info=$list";

    $res = file_get_contents($logfile);

    $content .= "<h3>$res</h3>";
  }

  if ($reset === "true")
  {
#todo: dazu müsste das esp32 request verarbeiten:
/*
   if ($dyndns === "")
      $logfile = "http://192.168.2.88/homeautomation/observation/logfile.php?info=$list";
    else
      $logfile = "$serverbaseurl/homeautomation/observation/logfile.php?info=$list";

    $res = file_get_contents($logfile);
*/

    $content .= "<h3>reset!</h3>";
  }

  $content .= "<a onclick='showObservation(true, false);'><font color='blue' font-size='10px'>Zustand speichern</font></a>";

#todo: dazu müsste das esp32 request verarbeiten:
#  $content .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a onclick='showObservation(true, true);'><font color='blue' font-size='10px'>Zur&uuml;cksetzen</font></a>";

  $content .= "</font></center>";

  echo("$content");
}

if (isset($_GET['link']))
{
  $link = urldecode($_GET['link']);

  $file = @file_get_contents($link);
  if ($file === false)
    echo("ERROR : " . $link);
  else
    echo("$file");
}

if (isset($_GET['serverstate']))
{
  $fullstate = 0;
  if (isset($_GET['full']))
    $fullstate = 1;

  $link1 = "http://192.168.2.88/homeautomation/raidstate.php";
  $link2 = "http://192.168.2.247/vdrinfo.php";

  $serverok = true;
  $raidused = "?";
  $syspartused = "?";
  $exthddused = "?";
  $vdrused = "?";

  $file1 = file_get_contents($link1);
  $lines1 = explode("\n", $file1);
  $cnt1 = count($lines1);
  #print_r($lines1);
  #print("cnt1:" . $cnt1 . "\n");
  
  $file2 = file_get_contents($link2);
  $lines2 = explode("\n", $file2);
  
  $info = "</pre><center><h2>Server Status</h2></center><pre>\n";
  for ($i=0; $i<$cnt1; $i++)
  {
    #print("> " . $lines1[$i] . "\n");
  
    if (strpos($lines1[$i], "blocks super") > 0)
    {
      if (strpos($lines1[$i], "[2/2] [UU]") < 0)
      {
        $serverok = false;
        $info .= trim($lines1[$i]) . "\n";
      }
    }
    
    if (strpos($lines1[$i], "Filesystem") > 0)
    {
      $info .= $lines1[$i] . "\n";
    }
    
    if (strpos($lines1[$i], 'md0') > 0)
    {
      $p = strpos($lines1[$i], "%");
      if ($p > 0)
      {
        $raidused = substr($lines1[$i], $p-3, 4);

        $raidfree = 100 - $raidused;
        if ($raidfree<2)
            $serverok = false;

        $info .= $lines1[$i] . "\n";
      }
    }
    
    if (strpos($lines1[$i], 'sdd1') > 0)
    {
      $p = strpos($lines1[$i], "%");
      if ($p > 0)
      {
        $exthddused = substr($lines1[$i], $p-3, 4);

        $exthddfree = 100 - $exthddused;
        if ($exthddfree<5)
            $serverok = false;

        $info .= $lines1[$i] . "\n";
      }
    }
    
    if (strpos($lines1[$i], 'sda1') > 0)
    {
      $p = strpos($lines1[$i], "%");
      if ($p > 0)
      {
        $syspartused = substr($lines1[$i], $p-3, 4);

        $syspartfree = 100 - $syspartused;
        if ($syspartfree<15)
            $serverok = false;

        $info .= $lines1[$i] . "\n";
      }
    }

    if (strpos($lines1[$i], 'USV Log') > 0)
    {
      for (; $i<count($lines1); $i++)
      {
        if (!$fullstate)
        {
          if (trim($lines1[$i]) == "")
          {
            break;
          }
        }
          
        $info .= $lines1[$i] . "\n";
      }
    }
  }
   
  $isfilm = false;   
  for ($i=0; $i<count($lines2); $i++)
  {
    if (strpos($lines2[$i], 'sda1') > 0)
    {
      $p = strpos($lines2[$i], "%");
      if ($p > 0)
      {
        $vdrused = substr($lines2[$i], $p-3, 4);

        $vdrfree = 100 - substr($lines2[$i], $p-3, 3);
        if ($vdrfree<5)
            $serverok = false;

        $info .= $lines2[$i] . "\n";
      }
    }
    
    if (strpos($lines2[$i], "Filesystem") > 0)
    {
      $info .= $lines2[$i] . "\n";
    }
    
    if (strpos($lines2[$i], "VDR Aufnahmen") > 0)
    {
      $isfilm = true;
    }

    if ($isfilm)
      $info .= "<td>" . $lines2[$i] . "</td>\n";
  }
 
  if ($fullstate)
  {
    echo($info); 
  }
  else
  { 
    $info = "Speicher: \n"; 
    $info .= ($serverok == true) ? "ok" : "error";
    $info .= "\n" . $raidused;
    $info .= "\n" . $vdrused;
    $info .= "\n" . $exthddused;
    $info .= "\n" . $syspartused;

    echo($info);
  }
}

if (isset($_GET['heizung']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
  $piconly = 1;  
  if (isset($_GET['piconly']))
    $piconly = $_GET['piconly'];

  if ($dyndns === "")
  {
    //$link = "http://192.168.2.88/homeautomation/heizung_klima/heizung_klima.php?bedienpanel=1&piconly=$piconly$dyndns";
    $link = "http://192.168.2.88/homeautomation/heizung.php?bedienpanel=1&piconly=$piconly$dyndns";
    //$link = "http://localhost/homeautomation/heizung_klima/heizung_klima.php?bedienpanel=1&piconly=$piconly$dyndns";
  }
  else
  {
    //$link = "$serverbaseurl/homeautomation/heizung_klima/heizung_klima.php?bedienpanel=1&piconly=$piconly$dyndns";
    $link = "$serverbaseurl/homeautomation/heizung.php?bedienpanel=1&piconly=$piconly$dyndns";
  }
  
  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['webcam']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
  $piconly = 1;  
  if (isset($_GET['piconly']))
    $piconly = $_GET['piconly'];
    
  if ($dyndns === "")
    $link = "http://192.168.2.133/livecam.php?piconly=$piconly$dyndns";
  else
    $link = "http://scherwel.dyndns.org:8033/livecam.php?piconly=$piconly$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['vnc']))
{
  if ($_GET['vnc'] === "close")
  {
    $link = "http://192.168.2.133/halt.php";
    $file = file_get_contents($link);
  }
  else
  {
    $file = "<h1>unknown cmd!</h1>";
  }

  echo("$file");
}

if (isset($_GET['mqtt']))
{
  if ($_GET['mqtt'] === "reset")
  {
    $link = "http://192.168.2.88/mqttreset.php";
    $file = file_get_contents($link);
  }
  else
  {
    $file = "<h1>unknown cmd!</h1>";
  }

  echo("$file");
}

if (isset($_GET['shutdown']))
{
  if ($_GET['shutdown'] === "true")
  {
    exec('sudo reboot' , $output);
    print_r($output);
  }
  else if ($_GET['shutdown'] === "false")
  {
    exec('sudo halt' , $output);
    print_r($output);
  }
  else
  {
    echo("unknown cmd!");
  }
}

if (isset($_GET['openelec']))
{
  if ($_GET['openelec'] === "movies")
  {
    $link = "http://192.168.2.111";
    $file = file_get_contents($link);
  }
  else
  {
    $file = "<h1>unknown cmd!</h1>";
  }

  echo("$file");
}

if (isset($_GET['networkstate']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
    $link = "http://192.168.2.88/homeautomation/netinfo.php?$dyndns";
  else
    $link = "$serverbaseurl/homeautomation/netinfo.php?$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['parking']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];

  $action = $_GET['parking'];

  if ($action == 0)
  {
    $linksource = "classify_parkplatz.php";
  }
  else if ($action == 1)
  {
    header("Content-Type: image/jpeg");
    $linksource = "classify.jpg";
  }
  else if ($action == 2)
  {
    header("Content-Type: image/jpeg");
    $linksource = "classify_inf.jpg";
  }
  else if ($action == 3)
  {
    header("Content-Type: image/jpeg");
    #$linksource = "classify_dl_inf.jpg";

    if ($dyndns === "")
      $link = "http://192.168.2.104:8088/getlastimage";
    else
      $link = "http://scherwel.dyndns.org:8188/getlastimage";

  #echo("INF: link $$link'");
  #die();

    $file = file_get_contents($link);

    echo("$file");

    http://192.168.2.104:8088/getlastimage
  }
  else if ($action == 4)
  {
    header("Content-Type: image/jpeg");
    $linksource = "classify_ml_inf.jpg";
  }
  else
  {
    echo("ERR: unknown action '$action' within cmd.php?parking");
  }
    
  if ($dyndns === "")
    $link = "http://192.168.2.104/$linksource?$dyndns";
  else
    $link = "http://scherwel.dyndns.org:8104/$linksource?$dyndns";

#echo("INF: link $$link'");
#die();

  #$file = file_get_contents($link);
  $file = file_get_contents($link, false, stream_context_create(array('http'=>array('timeout' => 120))));

  echo("$file");
}

if (isset($_GET['media']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
   
  $mediastate = ""; 
  if ($dyndns === "")
  {
    $info1 = pingDomain("192.168.2.103"); //media-raspi
    $info2 = pingDomain("192.168.2.29");  //tv-raspi
    
    $cnt = 0;
    if ($info1 >= 0)
    {
      $cnt++;
      $info1 = "an";
    }
    else
    {
      $info1 = "aus";
    }
      
    if ($info2 >= 0)
    {
      $cnt++;
      $info2 = "an";
    }
    else
    {
      $info2 = "aus";
    }

    $freedisk = shell_exec("df -h");
/*
Filesystem                    Size  Used Avail Use% Mounted on
/dev/root                      16G  8.2G  6.8G  55% /
devtmpfs                      480M     0  480M   0% /dev
*/
    $root = trim(explode("\n", $freedisk)[1]);
    $entries = explode(" ", $root);
    
    $usage = "?";
    for ($i=0; $i<count($entries); $i++)
    {
      if (strpos($entries[$i], "%") > 0)
      {
        $usage = substr($entries[$i], 0, strlen($entries[$i])-1);
        break;
      }
    }
    
    if ($usage < $MAX_SD_CARD_USAGE)   #ok
    {
      $info3 = "";
    }
    else                #sdcard damaged - too many writes?!
    {
      $cnt = 99;
      $info3 = "SD-Card > $usage%";  #"SD-Card defekt?!";
    }
    
    $mediastate .= "$cnt\nLE-Raspi: $info1\nTV-Raspi: $info2\n$info3";
  }
  else
  {
    $mediastate = "nicht über dyndns!";
  }

  echo("$mediastate");
}

if (isset($_GET['markise']))
{
  $cmd = "cmd=s";
  if (isset($_GET['cmd']))
    $cmd = $_GET['cmd'];

  $link = "http://192.168.2.99/?cmd=$cmd";
  
  $file = file_get_contents($link);
  echo($file);
}

if (isset($_GET['markisencam']))
{
  $link = "http://markisencam/capture?res=8"; #\&flash=1000";
  $file = "./tmp/markisencam.jpg";

  # achtung: '&' in link muss mit '\' escaped sein
  getProxyImage($link, $file); # return image and end script
}

if (isset($_GET['druckraumcam']))
{
  $link = "http://druckraumcam/capture?res=8\&flash=1000";
  $file = "./tmp/druckerraumcam.jpg";

  # achtung: '&' in link muss mit '\' escaped sein
  getProxyImage($link, $file); # return image and end script
}

if (isset($_GET['switchGadget']))
{
  $gadgetid = $_GET['gadgetid'];
  $onoff = $_GET['onoff'];

  if (strcmp($gadgetid, "dr-licht") == 0)
  {
    if ($onoff == '1')
      $url = "http://esp32aufzugraum/switchon?use_timeout=1&timeout=3";
    else
      $url = "http://esp32aufzugraum/switchoff?dummy=0";
      
    $url .= "&guid=" . time();
  }
  else
  {
    $result = "ERR: switchGadget() unknown: '$gadgetid'";
    return result;
  }


  $context = stream_context_create(array(
      'http' => array(
          'timeout' => 2    // Timeout in seconds
      )
  ));

  $result = file_get_contents($url, 0, $context);
  
  return $result;
}

if (isset($_GET['motorcam']))
{
  $link = "http://motorcam/capture?res=8\&flash=1000";
  $file = "./tmp/motorcam.jpg";
  
  # achtung: '&' in link muss mit '\' escaped sein
  getProxyImage($link, $file); # return image and end script
}

if (isset($_GET['postcam']))
{
  $link = "http://postcam/capture?res=8\&flash=1000";
  $file = "./tmp/postcam.jpg";
  
  # achtung: '&' in link muss mit '\' escaped sein
  getProxyImage($link, $file); # return image and end script
}

if (isset($_GET['pvanlage']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
  {
    #krtodo
    $link = "$serverbaseurl/homeautomation/pvanlage/pvanlage.php?bedienpanel=1$dyndns";
    #$link = "http://localhost/homeautomation/pvanlage/pvanlage.php?bedienpanel=1$dyndns";
  }
  else
    $link = "$serverbaseurl/homeautomation/pvanlage/pvanlage.php?bedienpanel=1$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['balkonkraftwerk']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
  {
    $link = "$serverbaseurl/homeautomation/balkonkraftwerk/balkonkraftwerk.php?bedienpanel=1$dyndns";
    #$link = "http://localhost/homeautomation/balkonkraftwerk/balkonkraftwerk.php?bedienpanel=1$dyndns";
  }
  else
    $link = "$serverbaseurl/homeautomation/balkonkraftwerk/balkonkraftwerk.php?bedienpanel=1$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['klimautostat']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
  {
    $link = "$serverbaseurl/homeautomation/auto_klima/auto_klima.php?bedienpanel=0$dyndns";
    #$link = "http://localhost/homeautomation/auto_klima/auto_klima.php?bedienpanel=0$dyndns";
  }
  else
    $link = "$serverbaseurl/homeautomation/auto_klima/auto_klima.php?bedienpanel=0$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['pvstatus']))
{
  $link = "http://scherwel.dyndns.org:8000/homeautomation/status_pv_bp.php";

  $content = file_get_contents($link);

  echo("$content");
}

if (isset($_GET['splitklimageraete']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
  {
    $link = "http://192.168.2.88/homeautomation/splitklima/splitklima.php?bedienpanel=1$dyndns";
    #$link = "http://localhost/homeautomation/splitklima/splitklima.php?bedienpanel=1$dyndns";
  }
  else
    $link = "$serverbaseurl/homeautomation/splitklima/splitklima.php?bedienpanel=1$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['temperaturen']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
  {
    #krtodo
    #$link = "http://192.168.2.88/homeautomation/heizung_klima/heizung_klima.php?bedienpanel=1$dyndns";
    $link = "http://localhost/homeautomation/heizung_klima/heizung_klima.php?bedienpanel=1$dyndns";
  }
  else
    $link = "$serverbaseurl/homeautomation/heizung_klima/heizung_klima.php?bedienpanel=1$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['stromzaehler']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
  {
    #krtodo
    $link = "$serverbaseurl/homeautomation/stromzaehler/stromzaehler.php?bedienpanel=1$dyndns";
  }
  else
    $link = "$serverbaseurl/homeautomation/stromzaehler/stromzaehler.php?bedienpanel=1$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['wetter']))
{
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
    
  if ($dyndns === "")
    $link = "http://192.168.2.88/homeautomation/wetter.php?$dyndns";
  else
    $link = "$serverbaseurl/homeautomation/wetter.php?$dyndns";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['pirstate']))
{
  $last = "";
  if (isset($_GET['lastpics']))
    $last = "&last=" . $_GET['lastpics'];
  $target = "&target=1";
  if (isset($_GET['target']))
    $target = "&target=" . $_GET['target'];
  $eventtime = "";
  if (isset($_GET['eventtime']))
    $eventtime = "&eventtime=" . $_GET['eventtime'];
  $dyndns = "";
  if (isset($_GET['dyndns']))
    $dyndns = "&dyndns=" . $_GET['dyndns'];
  $panelid = "";
  if (isset($_GET['panelid']))
    $panelid = "&panelid=" . $_GET['panelid'];
    
  if ($dyndns === "")
    $link = "http://192.168.2.102/pirstate.php?picsonly=1$last$target$eventtime$dyndns$panelid";
  else
    $link = "http://scherwel.dyndns.org:8002/pirstate.php?picsonly=1$last$target$eventtime$dyndns$panelid";

  $file = file_get_contents($link);

  echo("$file");
}

if (isset($_GET['drucker']))
{
  $printid = $_GET['printid'];
  
  $printerip = 'printerip???';
  if ($printid == 1)        # laserdrucker 
    $printerip = '192.168.2.48';
  else if ($printid == 2)   # 3d-drucker
    $printerip = '192.168.2.54';
  else if ($printid == -1)   # alle seite neu aufbauen
    $printerip = 'allprinters';
      
  if (isset($_GET['set']))
  {
    $state = shell_exec("wget -q -O - 'http://$printerip/relay?state=" . $_GET['set'] . "'");
    if ($state === false)
    {
      echo("Fehler\nSwitch Drucker\n");
      return;
    }
  }

  ###
  
  $cmd = "wget -q -O - \"http://192.168.2.48/report\"";
  $state = shell_exec($cmd);

  $search = '"relay":';
  $pos = strpos($state, $search);
  if ($pos === false)
  {
    echo("Fehler\nState Drucker: \"$cmd\" --> \"$state\"\n");
    return;
  }

  #

  $onoff1 = $state[$pos+strlen($search)];

  
  $cmd = "wget -q -O - \"http://192.168.2.54/report\"";
  $state = shell_exec($cmd);

  $search = '"relay":';
  $pos = strpos($state, $search);
  if ($pos === false)
  {
    echo("Fehler\nState Drucker: \"$cmd\" --> \"$state\"\n");
    return;
  }

  $onoff2 = $state[$pos+strlen($search)];

  ###
  
  $html = "<center><br><br>";
  $btnW = " 260px;";
  $btnH = " 120px;";
  $btnF = " 30px;";

  $html .= "<h2>Netzwerkdrucker</h2><br>";
  if ($onoff1 == 't')
  {
    $html .= "  <button style='background-color:lightgreen; width:$btnW  height:$btnH  font-size:$btnF' ";
    $html .= "   onclick=\"doDrucker('off', 1);\">Laserdrucker<br>ausschalten!";
    $html .= "  </button>";
  }
  else if ($onoff1 == 'f')
  {
    $html .= "  <button style='width:$btnW  height:$btnH  font-size:$btnF' ";
    $html .= "   onclick=\"doDrucker('on', 1);\">Laserdrucker<br>einschalten!";
    $html .= "  </button>";
  }
  else
    $html .= "<h1>illegal relay state: '$onoff1'</h1>";

  #
  
  $html .= "<h2>3D-Drucker</h2><br>";
  if ($onoff2 == 't')
  {
    $html .= "  <button style='background-color:lightgreen; width:$btnW  height:$btnH  font-size:$btnF' ";
    $html .= "   onclick=\"doDrucker('off', 2);\">Prusa MK3+<br>ausschalten!";
    $html .= "  </button>";
  }
  else if ($onoff2 == 'f')
  {
    $html .= "  <button style='width:$btnW  height:$btnH  font-size:$btnF' ";
    $html .= "   onclick=\"doDrucker('on', 2);\">Prusa MK3+<br>einschalten!";
    $html .= "  </button>";
  }
  else
    $html .= "<h1>illegal relay state: '$onoff2'</h1>";
    
  $html .= "</center>";

  echo("$html"); #"$state");
}

if (isset($_GET['alarmstate']))
{
  if (isset($_GET['reset']))
  {
    $alarm = "kein\nAlarm\n";
  
    $res = file_get_contents('http://192.168.2.188');
    if ($res === false)
      $alarm = "Fehler\nQuittierung\n";

    @file_put_contents('/tmp/alert.dat',  $alarm);
  }

  $file = @file_get_contents('/tmp/alert.dat');
  if (!isset($file))
  {
    $file = "keine\nAlarme\n";
  }
  else if (empty($file))
  {
    $file = "kein\nAlarm\n";
  }
  else if (empty(trim($file[0])) && empty(trim($file[1])))
  {
    $file = "kein\nAlarm\n";
  }
  
  echo("$file");
}

?>

