Files
overwatch/lib/modules/WorkstationServices/WorkstationServices.API.php
2016-05-30 17:18:20 -04:00

160 lines
4.8 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("!DESKTOP/!REMOVE/!SHORTCUT[json]", "WorkstationServices::RemoveDesktopFile");
apiql::register("!DESKTOP/!CREATE/!SHORTCUT[json]", "WorkstationServices::CreateDesktopFile");
apiql::register("!DESKTOP/!SET/!ICON[json]", "WorkstationServices::SetLauncherIcon");
apiql::register("!STORAGE/!GET/!FREE/!SPACE[json]", "WorkstationServices::GetFreeSpace");
apiql::register("!STORAGE/!GET/!TOTAL/!SPACE[json]", "WorkstationServices::GetStorageCapacity");
apiql::register("!UPLOAD/!LOCAL/!RESOURCE[json]", "WorkstationServices::UploadResource");
apiql::register("!DESKTOP/!CHANGE/!WALLPAPER[json]", "WorkstationServices::SetWallpaper");
}
/**
*
* @param unknown $sql
*/
function SetWallpaper($sql) {
}
/**
*
* @param unknown $sql
* @return unknown
*/
function SetLauncherIcon($sql) {
$DesktopArray = $sql['ICON'];
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);
LogEcho("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) {
$resultArray = array("response_type"=>"success",
"response_msg"=>disk_free_space($sql['SPACE']['volume']),
"response_code"=>200);
return json_encode($resultArray);
}
function GetStorageCapacity($sql) {
$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.
$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) {
$DesktopArray = $sql['SHORTCUT'];
unlink("/home/phablet/.local/share/applications/".$DesktopArray['name'].".desktop");
}
}
?>