1
0
moha/webserver/webserver.php
daniel Tartavel 0d35b1ff3f - lots of debugging
- beginning to add graphical stats
- beginning to add device by type
- added a dashboard "etage"
- begining to add notification to multiple recipients in freemobile hook
2022-03-28 00:40:34 +02:00

146 lines
4.2 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;
$Dashboards = array();
for ($m=1; $m<=12; $m++)
{
$months[$m] = date('F', mktime(0,0,0,$m, 1));
}
require_once $configDir . "/aliases.php";
require_once $configDir . "/dashboard_conf.php";
require_once "class/main.php";
require_once "webserver/cmd_functions.php";
//require_once "webserver/display_stats.php";
// opening listening server
$socket = stream_socket_server("tcp://" . $listenHost . ":" . $listenPort, $error_code, $error_message) or logger(ERROR, _("Could not create socket"), __FILE__ . ":" . __LINE__);
stream_set_blocking($socket, false);
$read = array( $socket );
function htmlSend($socket, $text, $meta="")
{
$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 . EOL
. '<meta charset="utf-8">' . EOL
. '<title>Moha</title>' . EOL
. '</head><body>' . EOL
. $text . "</body></html>";
stream_socket_sendto($socket, $response);
}
function askWebServer($read)
{
global $topics, $indexDevices, $devices;
$array = array();
$argList =array();
//logger(DEBUG, _("askWebserver function starting"), __FILE__ . ":" . __LINE__);
if ( stream_select( $read, $array, $array, 0 ))
{
logger(DEBUG, _("socket ready to read"), __FILE__ . ":" . __LINE__);
$spawn = stream_socket_accept($read[0]);
if ($spawn !== false)
{
logger(DEBUG, _("socket accepted"), __FILE__ . ":" . __LINE__);
$input = fgets($spawn, 4096);
logger(DEBUG, $input, __FILE__ . ":" . __LINE__);
$input = substr($input,5);
$page = $input;
$input = explode(" ", $input); // suppress text
if (!empty($input[0]))
{
$argTmp = explode("&", urldecode($input[0]));
foreach($argTmp as $tmp)
{
logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__);
if(strpos($tmp, "=") === false)
{
logger(DEBUG, _("no ="), __FILE__ . ":" . __LINE__);
$argList["cmd"] = trim($tmp);
}else
{
$key = trim(strchr($tmp, "=", true));
$argList[$key] = trim(substr(strchr($tmp, "="), 1));
if (count($argTmp) == 1) $argList["cmd"] = $key;
}
}
logger(DEBUG, print_r($argList, true), __FILE__ . ":" . __LINE__);
if(array_key_exists("cmd", $argList))
{
$command = strtolower($argList["cmd"]);
logger(DEBUG, _("command is ") . $command, __FILE__ . ":" . __LINE__);
switch($command)
{
case "dashboard":
webDashboard($spawn, $argList["dashboard"]);
break;
case "browse":
logger(DEBUG, _("Browsing"), __FILE__ . ":" . __LINE__);
webBrowse($spawn, $argList);
//return true;
break;
case "get":
logger(DEBUG, _("GET reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, webGet($argList));
break;
case "set":
logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, webSet($argList));
break;
case "dump":
case "print":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, webPrint($argList, $command));
break;
case "notify":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, webNotify($argList));
logger(DEBUG, print_r($monitored, true), __FILE__ . ":" . __LINE__);
break;
case "type":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
//htmlSend($spawn, webDisplayByType($argList));
case "stat":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, displayStats($argList));
default:
if (is_numeric(array_key_first($argList)))
{
webDashboard($spawn, $argList[0]);
}
}
if (array_key_exists("page", $argList))
{
htmlSend($spawn, '<meta http-equiv="refresh" content="1; URL=' . $argList["page"] . '" />');
return;
}
}else
{
webDashboard($spawn);
}
}else
{
webDashboard($spawn);
}
}
}
}
?>