<html>

<head>
  <title>SmartSwitch</title>
  <meta name="viewport" content="width=device-width, initial-scale=0.7">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet" type="text/css">
  <style type="text/css">
  body {
    font-family: 'Open Sans', sans-serif;
    font-size: 20px;
    text-align:center;
    margin: auto;
    width: 300px;
}
input {
    font-family: 'Open Sans', sans-serif;
    font-size: 16px;
    text-align:center;

}
h1 {
    font-size: 30px;
    font-family: 'Open Sans', sans-serif;
}

table {
    width: 300px;
    margin: 50px 0px 50px 0px;
}

.alert-box {
    color:#555;
    width: 260px;
    font-size: 12px;
    border-radius:10px;
    padding:10px 10px 10px 36px;
    margin-top:20px;
}
.alert-box span {
    font-weight:bold;
    text-transform:uppercase;
}
.error {
    background:#ffecec url('./error.png') no-repeat 10px 50%;
    border:1px solid #f5aca6;
}
.success {
    background:#e9ffd9 url('./success.png') no-repeat 10px 50%;
    border:1px solid #a6ca8a;
}
</style>
</head>

<body>
  <h1>SmartSwitch</h1>
  <table>
<?php
$email 
"info@hex.nl";
$hash "XXXXXX"// SHA-1, maak aan op http://www.sha1-online.com



$action $_GET['action'];
$smartswitch "".$_GET['smartswitch']."";
$apparaat $_GET['apparaat'];
$url 'https://cloud.homewizard.com/account/login';
$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_USERPWD"".$email.":".$hash."");
curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
$output curl_exec($ch);
$info curl_getinfo($ch);
curl_close($ch);
$output json_decode($outputtrue);
$token $output['session'];

$zurl 'https://plug.homewizard.com/plugs';
$headers = array(
    
"Accept: */*",
    
"Content-Type: application/json; charset=utf-8",
    
"X-Session-Token: " $token
);

$ch2 curl_init();
curl_setopt($ch2CURLOPT_HTTPHEADER$headers);
curl_setopt($ch2CURLOPT_URL$zurl);
curl_setopt($ch2CURLOPT_RETURNTRANSFERtrue);
curl_setopt($ch2CURLOPT_SSL_VERIFYPEERfalse);
curl_setopt($ch2CURLOPT_HTTPAUTHCURLAUTH_BASIC);
$zoutput curl_exec($ch2);
$zinfo curl_getinfo($ch2);
curl_close($ch2);
$zoutput json_decode($zoutputtrue);

foreach(
$zoutput as $sp)
{
    if(
$sp['name'] == $smartswitch)
    {
        
$plug $sp['id'];
        
$found1 "<br />\n\n<small>".$smartswitch." = ".$sp['id']."</small>\n<br />\n\n";
    }
}


foreach(
$zoutput['0']['devices'] as $sd)

{
    if(
$sd['name'] == $apparaat)
    {
        
$found2 "<small>".$apparaat." = ".$sd['id']."</small>\n\n<br />\n";
        
$device $sd['id'];
    }
    echo 
"<tr>\n<td>".$sd['name']."</td>\n<td><input type=\"button\" onclick=\"location.href='./?smartswitch=".$sp['name']."&apparaat=".$sd['name']."&action=On';\" value=\"on\" /></td>\n<td><input type=\"button\" onclick=\"location.href='./?smartswitch=".$sp['name']."&apparaat=".$sd['name']."&action=Off';\" value=\"off\" /></td>\n</tr>\n";
}
echo 
"</table>\n";

$curl "https://plug.homewizard.com/plugs/".$plug."/devices/".$device."/action";
$headers = array(
    
"Accept: */*",
    
"Content-Type: application/json; charset=utf-8",
    
"X-Session-Token: " $token
);
$data = array("action" => "".$_GET['action']."");
$data_string json_encode($data);
$ch3 curl_init();
curl_setopt($ch3CURLOPT_HTTPHEADER$headers);
curl_setopt($ch3CURLOPT_URL$curl);
curl_setopt($ch3CURLOPT_POST1);
curl_setopt($ch3CURLOPT_CUSTOMREQUEST"POST");
curl_setopt($ch3CURLOPT_POSTFIELDS$data_string);
curl_setopt($ch3CURLOPT_RETURNTRANSFERtrue);
curl_setopt($ch3CURLOPT_HTTPAUTHCURLAUTH_BASIC);
$coutput curl_exec($ch3);
$cinfo curl_getinfo($ch3);
curl_close($ch3);
$coutput json_decode($coutputtrue);

if (
$coutput['status'] == "Success")
{
    
$action strtolower($action);
    echo 
"\n<div class=\"alert-box success\"><span>success: </span>".$apparaat." turned <b>".$action."</b> successfully</div>";
} elseif (
$coutput['message'] == "Request method 'POST' not supported") {
    echo 
"";
} else {
    echo 
"\n<div class=\"alert-box error\"><span>error: </span>".$coutput['message']."</div>";
}

?>
<!-- SmartSwitch script door Ruben Bolink, https://hex.nl, info@hex.nl -->
</body>
</html>