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

475 lines
17 KiB
PHP

<?php
/**
* WorkstationServices.API.php
*
* @package default
*/
include "IniFile.class.php";
class WorkstationServices {
/**
*
* @return unknown
*/
function loadInst() {
static $inst = null;
if ($inst === null) {
$inst = new self;
}
return $inst;
}
/**
*
*/
function API_Register() {
apiql::register("!STORAGE/!GET/!FREE/!SPACE[json]", "WorkstationServices::GetFreeSpace");
apiql::register("!STORAGE/!GET/!TOTAL/!SPACE[json]", "WorkstationServices::GetStorageCapacity");
apiql::register("!DESKTOP/!CHANGE/!WALLPAPER[json]", "WorkstationServices::SetWallpaper");
apiql::register("!WORKSTATION/!INSTALL/!PACKAGE[json]", "WorkstationServices::Libertine_InstallPackage");
apiql::register("!WORKSTATION/!REMOVE/!PACKAGE[json]", "WorkstationServices::Libertine_RemovePackage");
apiql::register("!WORKSTATION/!CREATE/!CONTAINER[json]", "WorkstationService::Libertine_CreateContainer");
apiql::register("!WORKSTATION/!DELETE/!CONTAINER[json]", "WorkstationService::Libertine_DeleteContainer");
apiql::register("!WORKSTATION/!CONF-APT/!REPOSITORY[json]", "WorkstationService::Libertine_ConfAptRepository");
apiql::register("!WORKSTATION/!EXEC/!CONTAINER[json]", "WorkstationSerivces::Libertine_ExecContainer");
apiql::register("!WORKSTATION/!GET/!CONTAINER/!STATUS[json]", "WorkstationServices::Libertine_GetContainers");
}
function Libertine_InstallPackage($sql) {
if(!file_exists("/userdata"))
{
// As Libertine is ONLY to be used on Ubuntu for Mobile devices, this command will error if it is used without a utouch device.
$ResponseArray['response_type'] = "error";
$ResponseArray['response_msg'] = "This machine is not an Ubuntu Touch workstation, Libertine commands are only available on Ubuntu Touch.";
$ResponseArray['response_code'] = 500;
return json_encode($ResponseArray);
}
$process = new React\ChildProcess\Process('exec libertine-container-manager install-package -p "'.$sql['PACKAGE']['package']);
$programRequestCode = sha1("INSTALL_LIBERTINE_".$sql['PACKAGE']['package']);
$ResponseArray = array();
$ResponseArray['response_process_marker'] = $programRequestCode;
$process->on('exit', function($exitCode, $termSignal) use ($ResponseArray) {
global $clients;
if($exitCode == 0)
{
$ResponseArray['response_type'] = "success";
$ResponseArray['response_code'] = "LCM_200E";
} else {
$ResponseArray['response_type'] = "failed";
$ResponseArray['response_code'] = "LCM_500E";
}
$ResponseArray['response_exit_code'] = $exitCode;
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
$process->stdout->on('data', function($output) use ($ResponseArray) {
global $clients;
$ResponseArray['response_type'] = "status";
$ResponseArray['response_msg'] = $output;
$ResponseArray['response_code'] = "LCM_STDO";
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
$process->stderr->on('data', function($output) use ($ResponseArray) {
global $clients;
$ResponseArray['response_type'] = "status";
$ResponseArray['response_msg'] = $output;
$ResponseArray['response_code'] = "LCM_STDE";
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
}
function Libertine_RemovePackage($sql) {
if(!file_exists("/userdata"))
{
// As Libertine is ONLY to be used on Ubuntu for Mobile devices, this command will error if it is used without a utouch device.
$ResponseArray['response_type'] = "error";
$ResponseArray['response_msg'] = "This machine is not an Ubuntu Touch workstation, Libertine commands are only available on Ubuntu Touch.";
$ResponseArray['response_code'] = 500;
return json_encode($ResponseArray);
}
$process = new React\ChildProcess\Process('exec libertine-container-manager remove-package -p "'.$sql['PACKAGE']);
$programRequestCode = sha1("REMOVE_LIBERTINE_".$sql['PACKAGE']);
$ResponseArray = array();
$ResponseArray['response_process_marker'] = $programRequestCode;
$process->on('exit', function($exitCode, $termSignal) use ($ResponseArray) {
global $clients;
if($exitCode == 0)
{
$ResponseArray['response_type'] = "success";
$ResponseArray['response_code'] = "LCM_200E";
} else {
$ResponseArray['response_type'] = "failed";
$ResponseArray['response_code'] = "LCM_500E";
}
$ResponseArray['response_exit_code'] = $exitCode;
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
$process->stdout->on('data', function($output) use ($ResponseArray) {
global $clients;
$ResponseArray['response_type'] = "status";
$ResponseArray['response_msg'] = $output;
$ResponseArray['response_code'] = "LCM_STDO";
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
$process->stderr->on('data', function($output) use ($ResponseArray) {
global $clients;
$ResponseArray['response_type'] = "status";
$ResponseArray['response_msg'] = $output;
$ResponseArray['response_code'] = "LCM_STDE";
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
}
function Libertine_CreateContainer($sql) {
if(!file_exists("/userdata"))
{
// As Libertine is ONLY to be used on Ubuntu for Mobile devices, this command will error if it is used without a utouch device.
$ResponseArray['response_type'] = "error";
$ResponseArray['response_msg'] = "This machine is not an Ubuntu Touch workstation, Libertine commands are only available on Ubuntu Touch.";
$ResponseArray['response_code'] = 500;
return json_encode($ResponseArray);
}
if(isset($sql['CONTAINER']['container-settings']['id'])) $ContainerID = 'applications';
if(isset($sql['CONTAINER']['container-settings']['name'])) $ContainerName = 'Applications';
}
function Libertine_ConfAptRepository($sql)
{
if(!file_exists("/userdata"))
{
// As Libertine is ONLY to be used on Ubuntu for Mobile devices, this command will error if it is used without a utouch device.
$ResponseArray['response_type'] = "error";
$ResponseArray['response_msg'] = "This machine is not an Ubuntu Touch workstation, Libertine commands are only available on Ubuntu Touch.";
$ResponseArray['response_code'] = 500;
return json_encode($ResponseArray);
}
if($sql['REPOSITORY']['operation'] == "add") $process = new React\ChildProcess\Process('exec sudo -u phablet libertine-container-manager configure -a '.$sql['REPOSITORY']['repository']);
if($sql['REPOSITORY']['operation'] == "del" OR $sql['REPOSITORY']['operation'] == "delete") $process = new React\ChildProcess\Process('exec sudo -u phablet libertine-container-manager configure -d '.$sql['REPOSITORY']['repository']);
if(!$process)
{
$ResponseArray = array("response_type"=>"error",
"response_msg"=>"Incorrect operation specified. Accepted options: add, del, delete",
"response_code"=>"LCM_500");
return json_encode($ResponseArray);
} else {
$process->on('exit', function($exitCode, $termSignal) {
global $clients;
if($exitCode > 0) {
$ResponseArray = array("response_type"=>"error",
"response_msg"=>"Repository management request failed.",
"lcm_exit_code"=>$exitCode,
"response_code"=>"LCM_500");
} else {
$ResponseArray = array("response_type"=>"success",
"response_msg"=>"Repository management request succeeded.",
"lcm_exit_code"=>$exitCode,
"response_code"=>"LCM_200");
}
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
$ResponseArray = array("response_type"=>"success",
"response_msg"=>"LCM spawned to process management request.",
"response_code"=>"LCM_200");
return json_encode($ResponseArray);
}
}
function Libertine_GetContainers($sql)
{
if(!file_exists("/userdata"))
{
// As Libertine is ONLY to be used on Ubuntu for Mobile devices, this command will error if it is used without a utouch device.
$ResponseArray['response_type'] = "error";
$ResponseArray['response_msg'] = "This machine is not an Ubuntu Touch workstation, Libertine commands are only available on Ubuntu Touch.";
$ResponseArray['response_code'] = 500;
return json_encode($ResponseArray);
}
if(file_exists("/home/phablet/.local/share/libertine/ContainersConfig.json"))
{
$ContainersConfig = file_get_contents("/home/phablet/.local/share/libertine/ContainersConfig.json");
$Containers = json_decode($ContainerConfig);
} else {
$ResponseArray = array("response_type"=>"error",
"response_msg"=>"Container Configuration wsa not foudn.",
"error_code"=>"LCM_500NC");
return json_encode($ResponseArray);
}
if(isset($sql['STATUS']['container-id']))
{
foreach ($Containers->containerList as $Container)
{
if($Container->id == $sql['STATUS']['container-id'])
{
$ContainerArray = array('id'=>$Container->id,
'name'=>$Contianer->name,
'distribution'=>$Container->distro,
'status'=>$Container->installStatus,
'applications'=>$Container->installedApps);
} else { continue; }
}
$ResponseArray = array("response_type"=>"success",
"response_data"=>$ContainerArray,
"response_code"=>"LCM_200S");
} else {
foreach ($Containers->containerLsit as $Container)
{
if($Container->id == $Containers->defaultContainer)
{
$ContainerArray = array('id'=>$Container->id,
'name'=>$Contianer->name,
'distribution'=>$Container->distro,
'status'=>$Container->installStatus,
'applications'=>$Container->installedApps);
} else { continue; }
}
$ResponseArray = array("resposne_type"=>"success",
"response_data"=>$ContainerArray,
"response_code"=>"LCM_200S");
}
return json_encode($ResponseArray);
}
function Libertine_ExecContainer($sql)
{
global $loop;
if(!file_exists("/userdata"))
{
// As Libertine is ONLY to be used on Ubuntu for Mobile devices, this command will error if it is used without a utouch device.
$ResponseArray['response_type'] = "error";
$ResponseArray['response_msg'] = "This machine is not an Ubuntu Touch workstation, Libertine commands are only available on Ubuntu Touch.";
$ResponseArray['response_code'] = 500;
return json_encode($ResponseArray);
}
$process = new React\ChildProcess\Process('exec libertine-container-manager exec -c "'.$sql['CONTAINER']['command']);
$programRequestCode = sha1($sql['CONTAINER']['command']);
$ResponseArray = array();
$ResponseArray['response_process_marker'] = $programRequestCode;
$process->on('exit', function($exitCode, $termSignal) use ($ResponseArray) {
global $clients;
if($exitCode == 0)
{
$ResponseArray['response_type'] = "success";
$ResponseArray['response_code'] = "LCM_200E";
} else {
$ResponseArray['response_type'] = "failed";
$ResponseArray['response_code'] = "LCM_500E";
}
$ResponseArray['response_exit_code'] = $exitCode;
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
$process->stdout->on('data', function($output) use ($ResponseArray) {
global $clients;
$ResponseArray['response_type'] = "status";
$ResponseArray['response_msg'] = $output;
$ResponseArray['response_code'] = "LCM_STDO";
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
$process->stderr->on('data', function($output) use ($ResponseArray) {
global $clients;
$ResponseArray['response_type'] = "status";
$ResponseArray['response_msg'] = $output;
$ResponseArray['response_code'] = "LCM_STDE";
foreach ($clients as $client)
{
$client->write(json_encode($ResponseArray));
}
});
}
/**
*
* @param unknown $sql
*/
function SetWallpaper($sql) {
}
/**
*
* @param unknown $sql
* @return unknown
*/
function SetLauncherIcon($sql) {
$DesktopArray = $sql['ICON'];
if(!file_exists("/etc"))
{
// We're not running a unix-based OS, error out.
$ResponseArray = array("response_type"=>"error", "response_msg"=>"Members of the Windows family are not yet supported by this module.", "response_code"=>500);
return $ResponseArray;
}
/** As we're going to be expanding Overwatch's reach to all systems, the below code will eventually need to be optimized."
* if (!file_exists("/home/phablet")) {
* $ErrorArray = array("response_type"=>"error", "error_msg"=>"This device is not an Ubuntu Touch workstation.", "error_code"=>505);
* return json_encode($ErrorArray);
* }
**/
if (!file_exists("/home/phablet/.local/share/applications/".$DesktopArray['name'].".desktop")) {
$ErrorArray = array("response_type"=>"error", "error_msg"=>"Desktop File does not Exist.", "error_code"=>404);
return json_encode($ErrorArray);
}
$desktop = IniFile::LoadFromFile("/home/phablet/.local/share/applications/".$DesktopArray['name'].".desktop");
$desktop->SetKey("Desktop Entry", "Icon",$DesktopArray['icon']);
$desktop->Save();
}
function UploadResource($sql) {
$FileArray = $sql['RESOURCE'];
$fileMD5 = $FileArray['MD5'];
$fileContents = base64_decode($FileArray['contents']);
$destination = $FileArray['dest'];
// This code is ready for use, but inactive as ReactPHP does not have Filesystem I/O yet.
//$filesystem = \React\Filesystem\Filesystem::create($loop);
//$filesystem->file($destination)->open('cwt')->then(function ($stream) {
// $stream->end($fileContents);
//});
file_put_contents($destination, $fileContents);
$MD5 = md5_file($destination);
\Overwatch\SDK\v1\Utility::Log("Uploaded MD5: ".$fileMD5.", Local MD5:".$MD5, "WorkstationServices");
if ($MD5 != $fileMD5) {
$ErrorArray = array("response_type"=>"error",
"error_msg"=>"MD5 Checksums do not match. File may be corrupt.",
"error_code"=>500);
return json_encode($ErrorArray);
} else {
$ResponseArray = array("response_type"=>"success",
"response_msg"=>"File uploaded successfully to ".$destination,
"response_code"=>200);
return json_encode($ResponseArray);
}
}
function module_loaded() {
self::API_Register();
return true;
}
function GetFreeSpace($sql) {
if($sql['SPACE']['volume'] == null)
{
$responseArray = array ("response_type"=>"error",
"response_msg"=>"You need to supply a volume.",
"response_code"=>500);
return json_encode($responseArray);
}
$resultArray = array("response_type"=>"success",
"response_msg"=>disk_free_space($sql['SPACE']['volume']),
"response_code"=>200);
return json_encode($resultArray);
}
function GetStorageCapacity($sql) {
if($sql['SPACE']['volume'] == null)
{
$responseArray = array ("response_type"=>"error",
"response_msg"=>"You need to supply a volume.",
"response_code"=>500);
return json_encode($responseArray);
}
$resultArray = array("response_type"=>"success",
"response_msg"=>disk_total_space($sql['SPACE']['volume']),
"response_code"=>200);
return json_encode($resultArray);
}
function CreateDesktopFile($sql) {
// Check for the existance of the requested .desktop file.
if(!file_exists("/etc"))
{
// We're not running a unix-based OS, error out.
$ResponseArray = array("response_type"=>"error", "response_msg"=>"Members of the Windows family are not yet supported by this module.", "response_code"=>500);
return $ResponseArray;
}
$DesktopArray = $sql['SHORTCUT'];
if (!file_exists("/usr/share/applications/".$DesktopArray['name'].".desktop")) {
$ErrorArray = array("response_type"=>"error","error_msg"=>"Desktop File does not Exist.", "error_code"=>404);
return json_encode($ErrorArray);
}
if (!file_exists("/home/phablet")) {
$ErrorArray = array("response_type"=>"error", "error_msg"=>"This device is not an Ubuntu Touch workstation.", "error_code"=>505);
return json_encode($ErrorArray);
}
copy("/usr/share/applications/".$DesktopArray['name'].".desktop", "/home/phablet/.local/share/applications/".$DesktopArray['name'].".desktop");
$desktop = IniFile::LoadFromFile("/home/phablet/.local/share/applications/".$DesktopArray['name'].".desktop");
$ExecLine = $desktop->GetKey("Desktop Entry","Exec");
$ExecLine = "/home/phablet/bin/wm-wrapper " . $ExecLine;
$desktop->SetKey("Desktop Entry", "Exec", $ExecLine);
$desktop->SetKey("Desktop Entry", "X-Ubuntu-Touch", "true");
$desktop->SetKey("Desktop Entry", "X-Ubuntu-XMir-Enable", "true");
$desktop->Save();
$ResultArray = array("response_type"=>"success", "response_msg"=>"Desktop file created successfully.", "response_code"=>200);
}
function RemoveDesktopFile($sql) {
if(!file_exists("/etc"))
{
// We're not running a unix-based OS, error out.
$ResponseArray = array("response_type"=>"error", "response_msg"=>"Members of the Windows family are not yet supported by this module.", "response_code"=>500);
return $ResponseArray;
}
$DesktopArray = $sql['SHORTCUT'];
unlink("/home/phablet/.local/share/applications/".$DesktopArray['name'].".desktop");
}
}
?>