2022-02-23 10:23:16 +01:00
< ? 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 ();
2022-03-28 00:40:34 +02:00
for ( $m = 1 ; $m <= 12 ; $m ++ )
{
$months [ $m ] = date ( 'F' , mktime ( 0 , 0 , 0 , $m , 1 ));
}
2022-02-23 10:23:16 +01:00
2022-03-04 22:30:16 +01:00
require_once $configDir . " /aliases.php " ;
2022-02-23 10:23:16 +01:00
require_once $configDir . " /dashboard_conf.php " ;
require_once " class/main.php " ;
2022-04-07 01:44:17 +02:00
require_once " apiserver/cmd_functions.php " ;
2022-03-28 00:40:34 +02:00
2022-02-23 10:23:16 +01:00
// 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 );
2022-03-04 22:30:16 +01:00
function htmlSend ( $socket , $text , $meta = " " )
2022-02-23 10:23:16 +01:00
{
$httpHeader = " HTTP/1.1 200 OK " . EOLR .
" Date: " . date ( " r " ) . EOLR .
" Connection: close " . EOLR .
" Content-Type: text/html; charset=UTF-8 " . EOLR . EOLR ;
2022-03-28 00:40:34 +02:00
$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> " ;
2022-02-23 10:23:16 +01:00
stream_socket_sendto ( $socket , $response );
}
2022-04-07 01:44:17 +02:00
function apiServer ( $read )
2022-02-23 10:23:16 +01:00
{
global $topics , $indexDevices , $devices ;
2022-04-23 02:00:52 +02:00
logger ( DEBUG , " Function apiServer " , __FILE__ . " : " . __LINE__ );
2022-02-23 10:23:16 +01:00
$array = array ();
$argList = array ();
2022-04-23 02:00:52 +02:00
2022-02-23 10:23:16 +01:00
//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 );
2022-03-04 22:30:16 +01:00
$page = $input ;
2022-02-23 10:23:16 +01:00
$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
{
2022-03-13 22:33:26 +01:00
$key = trim ( strchr ( $tmp , " = " , true ));
$argList [ $key ] = trim ( substr ( strchr ( $tmp , " = " ), 1 ));
if ( count ( $argTmp ) == 1 ) $argList [ " cmd " ] = $key ;
2022-02-23 10:23:16 +01:00
}
}
2022-03-04 22:30:16 +01:00
2022-03-13 22:33:26 +01:00
logger ( DEBUG , print_r ( $argList , true ), __FILE__ . " : " . __LINE__ );
2022-02-23 10:23:16 +01:00
if ( array_key_exists ( " cmd " , $argList ))
{
$command = strtolower ( $argList [ " cmd " ]);
logger ( DEBUG , _ ( " command is " ) . $command , __FILE__ . " : " . __LINE__ );
switch ( $command )
{
2022-12-05 12:23:43 +01:00
case " ? " :
case " help " :
htmlSend ( $spawn , help ( $argList ));
break ;
2022-02-23 10:23:16 +01:00
case " dashboard " :
2022-04-23 02:00:52 +02:00
apiDashboard ( $spawn , $argList [ " dashboard " ]);
2022-02-23 10:23:16 +01:00
break ;
case " browse " :
logger ( DEBUG , _ ( " Browsing " ), __FILE__ . " : " . __LINE__ );
2022-04-23 02:00:52 +02:00
apiBrowse ( $spawn , $argList );
2022-02-23 10:23:16 +01:00
break ;
case " get " :
logger ( DEBUG , _ ( " GET reached " ), __FILE__ . " : " . __LINE__ );
2022-04-23 02:00:52 +02:00
htmlSend ( $spawn , apiGet ( $argList ));
2022-02-23 10:23:16 +01:00
break ;
case " set " :
logger ( DEBUG , _ ( " SET reached " ), __FILE__ . " : " . __LINE__ );
2022-04-23 02:00:52 +02:00
htmlSend ( $spawn , apiSet ( $argList ));
2022-02-23 10:23:16 +01:00
break ;
case " dump " :
case " print " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
2022-04-23 02:00:52 +02:00
htmlSend ( $spawn , apiPrint ( $argList , $command ));
2022-02-23 10:23:16 +01:00
break ;
case " notify " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
2022-04-23 02:00:52 +02:00
htmlSend ( $spawn , apiNotify ( $argList ));
2022-02-23 10:23:16 +01:00
logger ( DEBUG , print_r ( $monitored , true ), __FILE__ . " : " . __LINE__ );
break ;
2022-12-05 12:23:43 +01:00
//case "type":
//logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
2022-04-23 02:00:52 +02:00
//htmlSend($spawn, apiDisplayByType($argList));
2022-06-13 19:23:22 +02:00
case " enable " :
2022-03-28 00:40:34 +02:00
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
2022-06-13 19:23:22 +02:00
htmlSend ( $spawn , apiHookActive ( $argList , true ));
break ;
case " disable " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , apiHookActive ( $argList , false ));
2022-04-23 02:00:52 +02:00
break ;
case " verbose " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , apiVerbose ( $argList ));
break ;
case " friendlyname " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , getFn ( $argList ));
break ;
case " property " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , PropertiesDashboard ( $argList ));
break ;
2022-09-01 18:48:20 +02:00
case " deletedevice " :
2022-06-21 08:57:58 +02:00
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , deleteDevice ( $argList ));
break ;
2022-09-01 18:48:20 +02:00
case " present " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , presence ( $argList ));
break ;
case " ispresent " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , whoIsPresent ( $argList ));
break ;
2022-07-10 19:33:19 +02:00
case " test " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , test ( $argList ));
break ;
2022-12-05 12:23:43 +01:00
case " u navailable " :
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , unavailable ( $argList ));
break ;
2022-02-23 10:23:16 +01:00
default :
2022-04-23 02:00:52 +02:00
if ( is_numeric ( array_key_first ( $argList )))
{
apiDashboard ( $spawn , $argList [ 0 ]);
} else
2022-04-07 01:44:17 +02:00
{
2022-08-12 10:41:55 +02:00
logger ( DEBUG , $command . _ ( " does not exists ... so default action " ), __FILE__ . " : " . __LINE__ );
2022-04-23 02:00:52 +02:00
if ( file_exists ( " php://temp/ " . $command ))
{
logger ( DEBUG , $command . _ ( " is a file " ), __FILE__ . " : " . __LINE__ );
htmlSend ( $spawn , file_get_contents ( " php://temp/ " . $command ), 'Content-Type: image/png' );
2022-08-12 10:41:55 +02:00
} else
{
htmlSend ( $spawn , " command does not exists " );
2022-04-23 02:00:52 +02:00
}
2022-04-07 01:44:17 +02:00
}
2022-03-04 22:30:16 +01:00
}
if ( array_key_exists ( " page " , $argList ))
{
htmlSend ( $spawn , '<meta http-equiv="refresh" content="1; URL=' . $argList [ " page " ] . '" />' );
return ;
2022-02-23 10:23:16 +01:00
}
} else
{
2022-04-23 02:00:52 +02:00
apiDashboard ( $spawn );
2022-02-23 10:23:16 +01:00
}
} else
{
2022-04-23 02:00:52 +02:00
apiDashboard ( $spawn );
2022-02-23 10:23:16 +01:00
}
}
}
}
?>