Files
overwatch/lib/modules/SystemServices/SystemServices.API.php

214 lines
4.9 KiB
PHP

<?php
/**
* SystemServices.API.php
*
* @package default
*/
class SystemServices {
/**
*
* @return unknown
*/
function loadInst() {
static $inst = null;
if ($inst === null) {
$inst = new self;
}
return $inst;
}
/**
*
* @return unknown
*/
function module_loaded() {
self::API_Register();
return true;
}
/**
*
*/
function API_Register() {
apiql::register("!SYSTEM/!INSTALL/!PACKAGE[json]", "SystemServices::InstallPackage");
apiql::register("!SYSTEM/!REMOVE/!PACKAGE[json]", "SystemServices::RemovePackage");
apiql::register("!SYSTEM/!CLEAN/!PACKAGES[json]", "SystemServices::CleanPackages");
apiql::register("!SYSTEM/!GET/!INFO[json]", "SystemServices::GetSystemInfo");
apiql::register("!SYSTEM/!TEST/!S2S[json]", "SystemServices::TestS2SEndpoint");
}
function TestS2SEndpoint($sql)
{
return "TEST!";
}
/**
*
* @return unknown
*/
function GetSystemInfo() {
$state['linfo'] = new \Linfo\Linfo;
$state['linfo_parser'] = $state['linfo']->getParser();
if(file_exists("/etc/os-release")) { $OSArray = parse_ini_file("/etc/os-release");
} else {
$OSArray = "Windows";
}
$OverwatchVer = constant("OVERWATCH_VERSION");
$KernelVer = $state['linfo_parser']->getKernel();
$KernelArch = $state['linfo_parser']->getCPUArchitecture();
$MemArray = $state['linfo_parser']->getRam();
$CPU = $state['linfo_parser']->getCPU();
$Sensors = $state['linfo_parser']->getTemps();
$Devices = $state['linfo_parser']->getDevs();
$Network = $state['linfo_parser']->getNet();
$Battery = $state['linfo_parser']->getBattery();
$SysInfo = array('OS'=>$OSArray,
'DaemonVersion'=>$OverwatchVer,
'Kernel'=>$KernelVer,
'CPUArch'=>$KernelArch,
'RAM'=>$MemArray,
'CPU'=>$CPU,
'Sensors'=>$Sensors,
'Devices'=>$Devices,
'Network'=>$Network,
'Battery'=>$Battery);
$ResponseArray = array("response_type"=>"success",
"response_content"=>$SysInfo,
"response_code"=>200);
return json_encode($ResponseArray);
}
function CleanPackages() {
$process = new React\ChildProcess\Process('exec apt-get clean');
$process->on('exit', function($exitCode, $termSignal) {
global $clients;
if ($exitCode > 0) {
$ResponseArray = array("response_type"=>"error",
"response_msg"=>"Package cache cleanup failed.",
"apt_exit_code"=>$exitCode,
"error_code"=>"APT_500C");
} else {
$ResponseArray = array("response_type"=>"success",
"response_msg"=>"Package cache cleanup succeeded.",
"apt_exit_code"=>$exitCode,
"response_code"=>"APT_200C");
}
foreach ($clients as $client) {
$client->write(json_encode($ResponseArray)."\r\n");
}
return json_encode($ResponseArray);
});
$process->start($loop);
}
function InstallPackage($sql) {
global $loop;
$Package = $sql['PACKAGE']['packages'];
//$Reinstall = $sql['REINSTALL'];
if (is_array($Package)) {
foreach ($Package as $value)
{
if(empty($PackageLine))
{
$PackageLine = $value;
} else {
$PackageLine .= " ".$value;
}
}
} else {
$PackageLine = $Package;
}
$process = new React\ChildProcess\Process('exec apt-get -y install '.$PackageLine);
$process->on('exit', function($exitCode, $termSignal) {
global $clients;
if($exitCode > 0)
{
$ResponseArray = array("response_type"=>"error",
"response_msg"=>"Package installation failed.",
"apt_exit_code"=>$exitCode,
"error_code"=>"APT_500I");
} else {
$ResponseArray = array("response_type"=>"success",
"response_msg"=>"Package installation succeeded.",
"apt_exit_code"=>$exitCode,
"response_code"=>"APT_200I");
}
foreach($clients as $client)
{
$client->write(json_encode($ResponseArray)."\r\n");
}
return json_encode($ResponseArray);
});
$process->start($loop);
}
function RemovePackage($sql)
{
global $loop;
$Package = $sql['PACKAGE']['packages'];
//$Reinstall = $sql['REINSTALL'];
if(is_array($Package))
{
foreach($Package as $value)
{
if(empty($PackageLine))
{
$PackageLine = $value;
} else {
$PackageLine .= " ".$value;
}
}
} else {
$PackageLine = $Package;
}
$process = new React\ChildProcess\Process('exec apt-get -y remove '.$PackageLine);
$process->on('exit', function($exitCode, $termSignal) {
global $clients;
if($exitCode > 0)
{
$ResponseArray = array("response_type"=>"error",
"response_msg"=>"Package removal failed.",
"apt_exit_code"=>$exitCode,
"error_code"=>"APT_500R");
} else {
$ResponseArray = array("response_type"=>"success",
"response_msg"=>"Package removal succeeded.",
"apt_exit_code"=>$exitCode,
"response_code"=>"APT_200R");
}
foreach($clients as $client)
{
$client->write(json_encode($ResponseArray)."\r\n");
}
return json_encode($ResponseArray);
});
$process->start($loop);
}
}