<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html><body>

<?php
include "config.php"; # DB and password config
$table = "nattsjo_control";


if (isset($_GET["pw"]) && ($_GET["pw"] == $PASSWORD)) { 

$DB =  mysql_connect($DBHOST, $DBUSER, $DBPASS);
mysql_select_db($DBNAME, $DB);

# Temp triggers

if (isset($_GET["indoor1_low"])) {
  $low = intval($_GET["indoor1_low"]);
  $sql1 = "UPDATE $table SET time = now(), temp = '$low' WHERE type = 'indoor1_low'";
  mysql_query($sql1) or die("MySQL error1!");
  # mysql_free_result($result);
}

if (isset($_GET["indoor1_high"])) {
  $high = intval($_GET["indoor1_high"]);
  $sql1 = "UPDATE $table SET time = now(), temp = '$high' WHERE type = 'indoor1_high'";
  mysql_query($sql1) or die("MySQL error2!");
  # mysql_free_result($result);
}

# Tellstick

if ( isset($_GET["update"]) &&  isset($_GET["hour"]) && isset($_GET["minute"])  ) {
  $table = "nattsjo_relays_cron";
  $id = intval($_GET["update"]);
  $hour = intval($_GET["hour"]);
  $minute = intval($_GET["minute"]);
  $sql1 = "UPDATE $table SET hour = $hour, minute = $minute WHERE id = $id";
  mysql_query($sql1) or die("MySQL error2!");
  # mysql_free_result($result);
}

if ( isset($_GET["add"]) &&  isset($_GET["hour"]) && isset($_GET["minute"])  ) {
  $table = "nattsjo_relays_cron";
  $new_id = intval($_GET["add"]);
  $hour = intval($_GET["hour"]);
  $minute = intval($_GET["minute"]);
  $sql1 = "INSERT INTO $table VALUES (null, $new_id, $hour, $minute)";
  mysql_query($sql1) or die("MySQL error2!");
  # mysql_free_result($result);
  # echo $sql1;
}

if ( isset($_GET["delete"]) ) {
  $table = "nattsjo_relays_cron";
  $id = intval($_GET["delete"]);
  $sql1 = "DELETE FROM $table WHERE id = $id LIMIT 1";
  mysql_query($sql1) or die("MySQL error2!");
  # mysql_free_result($result);
  // echo $sql1;
}


# Events

if (isset($_GET["id"]) and isset($_GET["method"])) {
  $id = intval($_GET["id"]);
  $method = intval($_GET["method"]);
  $add_query = "INSERT INTO nattsjo_events_log VALUES (null, now(), $id, $method)";
  $DB =  mysql_connect($DBHOST, $DBUSER, $DBPASS);
  mysql_select_db($DBNAME, $DB);
  mysql_query($add_query) or die("MySQL error!");
  # mysql_free_result($result);
}


#mysql_free_result($result);
mysql_close($DB);

} else {
  echo "Wrong password!\n\n";
}
?>


</BODY>
</HTML>