<?php
ob_start();
session_start();
function get_client_socket()
	{
	if ((!isset($_SESSION['socket']))||empty($_SESSION['socket']))
		{
		$client = stream_socket_client("tcp://127.0.0.1:25000",$errno, $errstr, 30);
		if ($client)
			$_SESSION['socket']=$client;
		}
	else
		$client=$_SESSION['socket'];
	return $client;
	}
	
function get_value($var_name)
	{
	$client=get_client_socket();
	fwrite($client,"getv ".$var_name."\n");
	$reply = fread($client,1024);
	return $reply;
	}

function set_value($var_name,$var_value)
	{
	$client=get_client_socket();
	fwrite($client,"setv ".$var_name."=".$var_value."\n");
	$reply = fread($client,1024);
	return $reply;
	}
	
function valvapp_backup()
	{
	$client=get_client_socket();
	fwrite($client,"stop program\n");
	$reply = fread($client,1024);
	fwrite($client,"vabackup MAN\n");
	$reply = fread($client,1024);
	fwrite($client,"start program\n");
	$reply_restart = fread($client,1024);
	return $reply;
	}

function valvapp_list($dir_key)
	{
	$files = array();
	$dir = "tmp";
	if ($dir_key == "bak") $dir = "file_system/USR/BAK";
	if ($dir_key == "lib")
        {
        $client=get_client_socket();
        fwrite($client,"getv ident_Region\n");
        $reply = strtolower(fread($client,1024));
        $dir = "file_system/CFG/";
        if (($reply == "europe") || ($reply == "africa") || ($reply == "middle east"))
            $dir = $dir."EMEA";
        else
            $dir = $dir."North America";
        }
	$directory = opendir($dir);
	while($item = readdir($directory))
        {
        if (substr($item, (strlen($item) - 4)) == ".rdx")
            $files[] = $item;
        }
    sort($files);
	return $files;
	}

function valvapp_restore($dir_key, $va_name)
	{
	$dir = "tmp";
	if ($dir_key == "bak") $dir = "file_system/USR/BAK";
	if ($dir_key == "lib")
        {
        $client=get_client_socket();
        fwrite($client,"getv ident_Region\n");
        $reply = strtolower(fread($client,1024));
        $dir = "file_system/CFG/";
        if (($reply == "europe") || ($reply == "africa") || ($reply == "middle east"))
            $dir = $dir."EMEA";
        else
            $dir = $dir."North America";
        }
	$client=get_client_socket();
	fwrite($client,"stop program\n");
	$reply = fread($client,1024);
	fwrite($client,"vabackup AUTO\n");
	$reply = fread($client,1024);
	fwrite($client,"varestore ".$dir."/".$va_name."\n");
	$reply = fread($client,1024);
	return $reply;
	}

function valvapp_export()
	{
	$client=get_client_socket();
	fwrite($client,"stop program\n");
	$reply = fread($client,1024);
	fwrite($client,"vabackup export\n");
	$reply = fread($client,1024);
	fwrite($client,"start program\n");
	$reply_restart = fread($client,1024);
	return $reply;
	}

function logging_export($period)
	{
	$days = -1;
	if ($period == 0)
        $days = 1;
	if ($period == 1)
        $days = 7;
	if ($period == 2)
        $days = 31;
	if ($period == 3)
        $days = 0;
    if ($days < 0) return "ERROR";
	$client=get_client_socket();
	fwrite($client,"logexport ".$days."\n");
	$reply = fread($client,1024);
	return $reply;
	}

function logging_exportstatus()
	{
	$client=get_client_socket();
	fwrite($client,"logexport ?\n");
	$reply = fread($client,1024);
	return $reply;
	}

function update_engine($package_name)
	{
	$client=get_client_socket();
	fwrite($client,"updateengine ".$package_name."\n");
	$reply = fread($client,1024);
	return $reply;
	}

function update_enginestatus()
	{
	$client=get_client_socket();
	fwrite($client,"updateengine ?\n");
	$reply = fread($client,1024);
	return $reply;
	}

function export_diag()
	{
	$client=get_client_socket();
	fwrite($client,"diagexport\n");
	$reply = fread($client,1024);
	return $reply;
	}

function export_info()
	{
	$client=get_client_socket();
	fwrite($client,"infoexport\n");
	$reply = fread($client,1024);
	return $reply;
	}

function reboot()
	{
	$client=get_client_socket();
	fwrite($client,"reset\n");
	fclose($client);
	unset($_SESSION['socket']);
	}
?>

