1
0
moha/webserver.php
2022-01-27 18:41:16 +01:00

102 lines
2.7 KiB
PHP

<?php
// server init: No Timeout
set_time_limit(0);
ob_implicit_flush();
$error_message = null;
$error_code = null;
$listenHost = "0.0.0.0";
$listenPort = 1025;
$socket = stream_socket_server("tcp://" . $listenHost . ":" . $listenPort, $error_code, $error_message) or logger(ERROR, _("Could not create socket") . EOL);
stream_set_blocking($socket, false);
$read = array( $socket );
function askWebServer($read)
{
$array = array();
$argList =array();
if ( stream_select( $read, $array, $array, 0 ))
{
logger(DEBUG,_("socket ready to read"));
$spawn = stream_socket_accept($read[0]);
if ($spawn !== false)
{
logger(DEBUG,_("socket accepted"));
$input = fgets($spawn, 4096);
logger(DEBUG, $input);
if (!empty($input))
{
$input = substr($input,5);
$input = explode(" ", $input); // suppress text
$argTmp = explode("&", $input[0]);
foreach($argTmp as $tmp)
{
logger(DEBUG, $tmp);
$array = explode("=", $tmp);
print_r($array);
if (isset($array[1])) $argList[$array[0]] = $array[1];
}
if(array_key_exists("cmd", $argList))
{
switch(strtolower($argList["cmd"]))
{
case "get":
logger(DEBUG, "get reached");
if(empty($argList["device"]))
{
$response = "<html><header><body>get passed</body></header><html>";
}
fwrite($spawn, $response);
break;
case "set":
logger(DEBUG, "set reached");
$response = "<html><header><body>set passed</body></header><html>";
fwrite($spawn, $response);
break;
case "print":
logger(DEBUG, "print reached");
$var = $GLOBALS[$argList["object"]];
if (isset($argList["topic"]))
{
$topic = $argList["topic"];
}
if (isset($argList["address"]))
{
$var = $var[$argList["address"]];
}elseif (isset($argList["fn"]))
{
if(!empty($topic))
{
$var = $var[$topic];
$path = explode("/", $argList["fn"]);
foreach($path as $tmp)
{
$var = $var[$tmp];
}
}else
{
logger(ERROR, _("topic is not defining: add &topic=zigbee2mqtt to the resquest"));
}
}
$error = error_get_last();
if($error !== null)
{
$response = "<html><header><body>" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "</body></header><html>";
}
$response = "<html><header><body>" . print_r($var, true) . "</body></header><html>";
fwrite($spawn, $response);
break;
default:
logger(DEBUG, "not understanding command");
fwrite($spawn, "not understanding command");
}
}
}
}
}
}
?>