1
0
moha/webserver.php

319 lines
9.7 KiB
PHP
Raw Normal View History

<?php
// server init: No Timeout
set_time_limit(0);
ob_implicit_flush();
2022-02-07 16:58:42 +01:00
$error_message = null;
$error_code = null;
$listenHost = "0.0.0.0";
$listenPort = 1025;
2022-02-07 16:58:42 +01:00
$Dashboards = array();
require_once $configDir . "/dashboard_conf.php";
require_once "class/main.php";
2022-02-07 16:58:42 +01:00
// opening listening server
$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 );
2022-02-07 16:58:42 +01:00
function htmlSend($socket, $text)
{
$httpHeader = "HTTP/1.1 200 OK" . EOLR .
"Date: " . date("r") . EOLR .
"Connection: close" . EOLR .
"Content-Type: text/html; charset=UTF-8" . EOLR . EOLR;
$response = $httpHeader . '<!doctype html>' . EOL . '<html lang="fr">' . EOL . '<head>' . EOL . '<meta charset="utf-8">' . EOL . '<title>Moha</title>' . EOL . '</head><body>' . $text . "</body></html>";
stream_socket_sendto($socket, $response);
}
function webListTopics()
{
global $topics;
logger(DEBUG, _("webListTopics function"));
$response = "";
foreach ($topics as $name => $topic)
{
$response .= '<a href="/browse&topic=' . $name . '">' . $name ."</a><br>";
2022-02-07 16:58:42 +01:00
}
return $response;
}
function webBrowse($socket, $argList)
{
global $topics, $devices;
logger(DEBUG, _("Generic response to choose device and property"), __FILE__ . ":" . __LINE__);
//$response = "<html><header></header><body>" . _("unknown command") . "</body></html>";
$response = "";
$flag = false;
if (array_key_exists("topic", $argList))
{
if (array_key_exists($argList["topic"], $topics))
{
logger(DEBUG, _("Topic exists") , __FILE__ . ":" . __LINE__);
$topicRef = '<a href="browse&amp;topic=' . $argList["topic"];
2022-02-07 16:58:42 +01:00
if (array_key_exists("fn", $argList))
{
logger(DEBUG, _("FriendlyName exists: ") . $argList["fn"] , __FILE__ . ":" . __LINE__);
$fn = "";
$fnArray = explode("/", $argList["fn"]);
$device = $devices[$argList["topic"]];
var_dump($fnArray);
2022-02-07 16:58:42 +01:00
foreach($fnArray as $value)
{
if ($flag) $fn .= "/";
$fn .= $value;
if (!array_key_exists($value, $device))
{
htmlSend($socket, sprintf(_('"%s" does not exists')));
return false;
}
$device = $device[$value];
$flag = true;
}
var_dump($device);
if (array_key_exists("device", $device)) // TODO
2022-02-07 16:58:42 +01:00
{
$device = $device["device"];
$fn .= "device/" . $fn;
}
if (is_a($device, "device"))
2022-02-07 16:58:42 +01:00
{
foreach($device as $key => $value)
{
if ($key != "devicarray_key_existse" and is_array($value))
2022-02-07 16:58:42 +01:00
{
//print_r($value);
2022-02-07 16:58:42 +01:00
if (array_key_exists("value", $value))
{
$response .= $key . ' = ' . bool2string($value["value"]) . "<br>";
}else
{
$response .= $key . ' = ' . bool2string($value) . "<br>";
2022-02-07 16:58:42 +01:00
}
}
}
}else
{
foreach($device as $key => $value)
{
$response .= $topicRef . "&amp;fn=" . $fn . "/" . $key . '">' . $key . "</a><br>";
2022-02-07 16:58:42 +01:00
}
}
logger(DEBUG, _("response: ") . EOL . $response , __FILE__ . ":" . __LINE__);
//htmlSend($socket, $response);
}else
{
foreach($devices[$argList["topic"]] as $key => $value)
{
print "key = " . print_r($key, true) . " value = " . print_r($value, true) . EOLR;
2022-02-07 16:58:42 +01:00
logger(DEBUG, _("devices de topic: ") . $key , __FILE__ . ":" . __LINE__);
$response .= $topicRef . "&amp;fn=" . $key . '">' . $key . "</a><br>";
2022-02-07 16:58:42 +01:00
logger(DEBUG, _("response: ") . $response , __FILE__ . ":" . __LINE__);
}
}
}else
{
$response = webListTopics();
}
}else
{
$response = webListTopics();
}
htmlSend($socket, $response);
}
function webDashboard($socket, $n = 0)
{
global $dashboards, $indexDevices;
logger(DEBUG, _("webDashboard function"));
$response = "";
if(array_key_exists($n, $dashboards))
{
foreach ($dashboards[$n] as $ieeeAddress => $property)
{
logger(DEBUG, _($indexDevices[$ieeeAddress]->friendlyName . " => " . bool2string($indexDevices[$ieeeAddress]->$property["value"])));
$response .= $indexDevices[$ieeeAddress]->friendlyName . " => " . bool2string($indexDevices[$ieeeAddress]->$property["value"]) . "<br>";
}
}else
{
$response = _("dashboard not found");
}
htmlSend($socket, $response);
}
function askWebServer($read)
{
2022-02-07 16:58:42 +01:00
global $topics, $indexDevices, $devices;
$array = array();
$argList =array();
if ( stream_select( $read, $array, $array, 0 ))
{
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("socket ready to read"), __FILE__ . ":" . __LINE__);
$spawn = stream_socket_accept($read[0]);
if ($spawn !== false)
{
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("socket accepted"), __FILE__ . ":" . __LINE__);
$input = fgets($spawn, 4096);
2022-01-28 23:05:58 +01:00
logger(DEBUG, $input, __FILE__ . ":" . __LINE__);
$input = substr($input,5);
$input = explode(" ", $input); // suppress text
if (!empty($input[0]))
{
2022-02-07 16:58:42 +01:00
$argTmp = explode("&", urldecode($input[0]));
foreach($argTmp as $tmp)
{
2022-02-07 16:58:42 +01:00
logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__);
if(strpos($tmp, "=") === false)
{
logger(DEBUG, _("no ="), __FILE__ . ":" . __LINE__);
$argList["cmd"] = trim($tmp);
2022-02-07 16:58:42 +01:00
}else
{
$argList[trim(strchr($tmp, "=", true))] = trim(substr(strchr($tmp, "="), 1));
}
}
logger(DEBUG, print_r($argList, true), __FILE__ . ":" . __LINE__);
2022-02-07 16:58:42 +01:00
/*if (array_key_exists("browse", $argList))
2022-02-07 16:58:42 +01:00
{
logger(DEBUG, _("Browsing"));
webBrowse($spawn, $argList);
return true;
}elseif(array_key_exists("dashboard", $argList))
{
webDashboard($spawn, $argList["dashboard"]);
return true;
}*/
if(array_key_exists("cmd", $argList))
{
2022-01-28 23:05:58 +01:00
$command = strtolower($argList["cmd"]);
logger(DEBUG, _("command is ") . $command, __FILE__ . ":" . __LINE__);
2022-01-28 23:05:58 +01:00
switch($command)
{
case "dashboard":
webDashboard($spawn, $argList["dashboard"]);
break;
case "browse":
logger(DEBUG, _("Browsing"), __FILE__ . ":" . __LINE__);
webBrowse($spawn, $argList);
//return true;
break;
case "get":
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("GET reached"), __FILE__ . ":" . __LINE__);
2022-02-02 21:18:44 +01:00
if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList))
{
2022-02-07 16:58:42 +01:00
$response = "GET: " . _("no parameters passed, need topic, fn and property");
2022-02-02 21:18:44 +01:00
}else
{
2022-02-07 16:58:42 +01:00
if (($device = getDevice($argList["topic"], $argList["fn"])) === false)
{
htmlSend($spawn, sprintf(_('"%s" or "%s" does not exists'), $argList["topic"], $argList["fn"]));
return false;
}
2022-02-02 21:18:44 +01:00
$property = $argList["property"];
2022-02-07 16:58:42 +01:00
$response = "GET: " . bool2string($device->$property["value"]);
}
2022-02-07 16:58:42 +01:00
htmlSend($spawn, $response);
break;
case "set":
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__);
2022-02-02 21:18:44 +01:00
if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("value", $argList))
{
2022-02-07 16:58:42 +01:00
$response = "SET: " . _("no parameters passed, need topic, fn, property and value") . "passed";
htmlSend($spawn, $response);
2022-02-02 21:18:44 +01:00
}else
{
2022-02-07 16:58:42 +01:00
$response = "setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"];
htmlSend($spawn, $response);
2022-02-02 21:18:44 +01:00
$payload = array($argList["property"] => $argList["value"]);
publish(Z2M . "/" . $argList["fn"], $payload);
}
break;
2022-01-28 23:05:58 +01:00
case "dump":
case "print":
2022-01-28 23:05:58 +01:00
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
if (array_key_exists($argList["object"], $GLOBALS))
{
$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 = getDevice($topic, $argList["fn"]);
}else
2022-01-28 23:05:58 +01:00
{
$str = _("topic is not defining: add &topic=\nThese topics are availables: ");
foreach($topics as $key => $value)
{
$str .= $key . EOL;
}
logger(ERROR, $str, __FILE__ . ":" . __LINE__);
2022-01-28 23:05:58 +01:00
}
}
$error = error_get_last();
if($error !== null)
{
$response = $error["message"] . " file: " . $error["file"] . " line: " . $error["line"];
}
if ($command === "print")
{
$response = "<pre>" . nl2br(print_r($var, true)) . "<pre>";
}elseif($command === "dump")
{
$response = "Dump" . EOL;
$response .= "<pre>" . nl2br(var_export($var, true)) . "<pre>";
}
}else
2022-01-28 23:05:58 +01:00
{
$response = _("Object do not exists");
2022-01-28 23:05:58 +01:00
}
2022-02-07 16:58:42 +01:00
htmlSend($spawn, $response);
break;
2022-01-30 00:21:50 +01:00
case "notify":
2022-02-02 21:18:44 +01:00
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
2022-01-30 00:21:50 +01:00
if (!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("condition", $argList) or !array_key_exists("value", $argList))
{
2022-02-07 16:58:42 +01:00
$response = _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value");
htmlSend($spawn, $response);
2022-01-30 00:21:50 +01:00
}else
{
2022-02-07 16:58:42 +01:00
$response = _("notify command have been set");
2022-01-30 00:21:50 +01:00
$monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]);
2022-02-07 16:58:42 +01:00
htmlSend($spawn, $response);
2022-01-30 00:21:50 +01:00
}
logger(DEBUG, print_r($monitored, true), __FILE__ . ":" . __LINE__);
2022-01-30 00:21:50 +01:00
break;
default:
2022-02-07 16:58:42 +01:00
webBrowse($spawn, $argList);
}
2022-02-07 16:58:42 +01:00
}else
{
webDashboard($spawn);
}
2022-02-07 16:58:42 +01:00
}else
{
webDashboard($spawn);
}
}
}
}
?>