<?php

DEFINE ('PAPI_DEBUG', true);

header('Content-type: application/json; charset=utf-8');

date_default_timezone_set('Europe/Belgrade');

try {

  if (isset ($_POST['json'])) {
    $mJsonRequest = $_POST['json'];
  } else {
    $mJsonRequest = "";    
  }
  
  //$mJsonRequest = file_get_contents ('INFO_query.json');

  if (isset ($_POST['DebugLevel'])) {
    $mDebugLevel = (int)$_POST['DebugLevel'];
  } else {
    $mDebugLevel = 0;
  }
  
  if (isset ($_POST['DebugSections'])) {
    $mDebugSections = $_POST['DebugSections'];
  } else {
    $mDebugSections = "";
  }

  if (isset ($_POST['APIVersion'])) {
    $mAPIVersion = $_POST['APIVersion'];
  } else {
    $mAPIVersion = "";
  }

	$p6 = '';
	$p7 = '';
	$p8 = '';
  $p9 = '';
  $p10 = '';
  $p11 = '';
  $p12 = '';
  $p13 = '';
  $p14 = '';
  $p15 = '';
  
  for ($i=6; $i <= 15; $i++) {
    
    $mParamName = 'p' . $i;
    $mVarName = 'm' . $mParamName;
    
    if (isset ($_POST[$mParamName])) {
      ${$mVarName} = $_POST[$mParamName];
    }
    
  }

  $mEnvironment = GetEnvironment();
  
  $mOICF = new COM("oicf.TOICF", NULL, CP_UTF8);

  //$mResponse = $mOICF->Execute ($mJsonRequest, $mDebugLevel, $mDebugSections, $mEnvironment, $mAPIVersion);

 	//$mResponse = $mOICF->Execute ($mJsonRequest, $mDebugLevel, $mDebugSections, $mEnvironment, 'c:\\\\HandyStock\\');

  $mResponse = $mOICF->Execute2018 ($mJsonRequest, 
                                    $mDebugLevel, 
                                    $mDebugSections, 
                                    $mEnvironment, 
                                    $mAPIVersion,
                                    $p6, $p7, $p8, $p9, $p10, $p11, $p12, $p13, $p14, $p15);
                                



  $mResponseFileName = dirname(__FILE__) . '/resp-' . DATE ("Ymd-His") . '.json';
  
} catch ( exception $mError) {
  
  $mResponseObj['error_state'] = "oicf_interface_error";
  //$mResponseObj['error_message'] = print_r ($mError, TRUE);
  
  $reader = function & ($object, $property) {
    $value = & Closure::bind(function & () use ($property) { 
        return $this->$property; 
    }, $object, $object)->__invoke();

    return $value;
  };
  
  $mMessage = & $reader($mError, 'message');
  if (PAPI_DEBUG) {
    $mFile = & $reader($mError, 'file');
    $mLine = & $reader($mError, 'line');
    //$mTrace = & $reader($mError, 'trace');
    
  }
  
  $mResponseObj['error_message'] = $mMessage;
  if (PAPI_DEBUG) {
    $mResponseObj['error_message'] .= "";
    $mResponseObj['error_message'] .= "\r\nFile: $mFile";
    $mResponseObj['error_message'] .= "\r\nLine: $mLine";
    
  }
  
  $mResponse = json_encode ($mResponseObj);
  
//print_r ($mError);

}

echo $mResponse;


function GetEnvironment() {
  
  $mServerVarList = 'HTTP_HOST,HTTP_CONNECTION,HTTP_CACHE_CONTROL,HTTP_UPGRADE_INSECURE_REQUESTS,
                     HTTP_USER_AGENT,HTTP_ACCEPT,HTTP_ACCEPT_ENCODING,HTTP_ACCEPT_LANGUAGE,
                     HTTP_COOKIE,CONTENT_LENGTH,REQUEST_METHOD,REQUEST_FILENAME,SCRIPT_FILENAME,
                     SCRIPT_NAME,SCRIPT_URL,SCRIPT_URI,URL,DOCUMENT_ROOT,GATEWAY_INTERFACE,REQUEST_URI,
                     REMOTE_PORT,REMOTE_ADDR,LOCAL_ADDR,SERVER_ADDR,SERVER_NAME,SERVER_PORT,COOKIE,
                     HTTPS,SERVER_PROTOCOL,SERVER_SOFTWARE,FCGI_ROLE,REQUEST_TIME_FLOAT,REQUEST_TIME,
                     REDIRECT_STATUS';
  
  $mList = explode (',', $mServerVarList);
  
  $mResult = '';

  foreach ($mList as $mItem) {
    $mResult .= GetEnvironmentVar ($mItem);
  }

  echo $mResult;
  
  return $mResult;

}

function GetEnvironmentVar ($pName) {
  
  $mName = trim ($pName);
  if (isset ($_SERVER[$mName])) {
    $mResult = $mName . '=' . $_SERVER[$mName] . "\r\n";
  } else {
    $mResult = '';
  }  
}

?>