#!/usr/bin/env php
<?php
/**
 * @version $Id:$
 * ------------------------------------------------------------------------------
 * description
 * ------------------------------------------------------------------------------
 * @author Andrey Starostin
 * @QA
 * @copyright videoNEXT Network Solutions, Inc, 2010
 * ------------------------------------------------------------------------------
 */

error_reporting(0);

// restore ENV variables
if (!isset($_SERVER["APL"]))
{
	$env = file("/opt/sarch/base/etc/env.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	foreach ($env as $line)
	{
		$matches = array();
		if (preg_match("/^(\w+)=(.*)$/", $line, $matches))
		{
			list(, $key, $value) = $matches;
			$_SERVER[$key] = $value;
		}
	}
}

require_once $_SERVER['APL'] . '/api/lib/php/bootstrap.php';

// read arguments
$arguments = array();
if ($argc > 1)
{
	for ($i = 1; $i < $argc; $i++)
	{
		$matches = array();
		if (preg_match("/^(\w+)=(.*)$/", $argv[$i], $matches))
		{
			list(, $key, $value) = $matches;
			$arguments[$key] = $value;
		}
	}
}

// do request form Admin user
$_SESSION[SESSION_USERID] = User::ADMIN;

$response = array();

if (isset($arguments['function']))
{
	$response = Proxy::call(array("API_GET", "API_POST"), $arguments["function"], $arguments);
} else {
	$response["error"] = "Please specify 'function' parameter";
	$response["code"] = 404;
}

echo json_encode($response) . "\n";
