<?php

function sendAuthTicket($cn, $ticket){
	$uhash = "";
	if ( IsUserLogin() == 1 ){
		$userids = GetUserIDs();
		$user = db_getUserDataByID($cn, $userids[0]);
		$uhash = $user['user_hash'];
	}
	$uhashfake = '';
	if ( isset($_SESSION['user_uhashfake']) ){
		$uhashfake = $_SESSION['user_uhashfake'];
	}
	$pguid = db_getPortalGuid($cn);
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface']) : getSetting('portal','defaultLanguage','ru'));
	$content = ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru'));
	$xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><authticket uhashfake="'.$uhashfake.'"  ticket="'.$ticket.'" lang="'.$lang.'" content="'.$content.'" /></pkg>';
	return sendPostDataToServise($xml, false, 1);
}

function sendRepotStop($cn, $oguid, $rguid, $idbranch){
	$uhash = "";
	if ( IsUserLogin() == 1 ){
		$userids = GetUserIDs();
		$user = db_getUserDataByID($cn, $userids[0]);
		$uhash = $user['user_hash'];
	}
	$pguid = db_getPortalGuid($cn);
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface']) : getSetting('portal','defaultLanguage','ru'));
	$content = ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru'));
	$xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><requiredreportcancel uhash="'.$uhash.'" oguid="'.$oguid.'" rguid="'.$rguid.'" idbranch="'.$idbranch.'" lang="'.$lang.'" content="'.$content.'" /></pkg>';
	return sendPostDataToServise($xml);
}


function sendRepotCall($cn, $oguid, $rguid, $lang, $refresh="0", $timer="0", $idbranch){
	$uhash = "";
	if ( IsUserLogin() == 1){
		$userids = GetUserIDs();
		$user = db_getUserDataByID($cn, $userids[0]);
		$uhash = $user['user_hash'];
	}
	$pguid = db_getPortalGuid($cn);
	//62796
	$content = ( isset($lang) ? $lang : ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru')) );
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface']) : getSetting('portal','defaultLanguage','ru'));
    $xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><requiredreport uhash="'.$uhash.'" oguid="'.$oguid.'" rguid="'.$rguid.'" lang="'.$lang.'" content="'.$content.'" refresh="'.$refresh.'" timer="'.$timer.'" idbranch="'.$idbranch.'" /></pkg>' ;
	return sendPostDataToServise($xml);
}

function sendInputSave($cn, $inputarr, $datePeriod, $dateValue, $pf = 0 ){
	$uhash = "";
	if ( IsUserLogin() == 1){
		$userids = GetUserIDs();
		$user = db_getUserDataByID($cn, $userids[0]);
		$uhash = $user['user_hash'];
		$bsuhash = $_SESSION['user_bsuhash'];
	}
	$pguid = db_getPortalGuid($cn);
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface']) : getSetting('portal','defaultLanguage','ru'));
	$content = ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru'));
	$xml = '<?xml version="1.0" encoding="utf-8"?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><saveinputvalues uhash="'.$uhash.'" bsuhash="'.$bsuhash.'"  period="'.$datePeriod.'" date="'.$dateValue.'" pf="'.$pf.'" lang="'.$lang.'" content="'.$content.'">';
	foreach ($inputarr as $value){
	  $xml .= '<value oguid="'.$value['Value'].'" val="'.$value['Fact'].'" />';
	}
	$xml .= '</saveinputvalues></pkg>';
	return sendPostDataToServise($xml);
}

function sendInputCall($cn, $datePeriod, $dateValue, $orderColumn, $orderVal = "acs", $pf = 0, $lang = "ru-ru"){
	$uhash = "";
	if ( IsUserLogin() == 1){
		$userids = GetUserIDs();
		$user = db_getUserDataByID($cn, $userids[0]);
		$uhash = $user['user_hash'];
		$bsuhash = $_SESSION['user_bsuhash'];
	} else {
		$userids = null;
		$user = '';
		$uhash = '';
		$bsuhash = '';
	}

	$pguid = db_getPortalGuid($cn);
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface']) : getSetting('portal','defaultLanguage','ru'));
	$content = isset($lang) ? $lang : ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru'));
	$xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><getinputvalues uhash="'.$uhash.'" bsuhash="'.$bsuhash.'" period="'.$datePeriod.'" date="'.$dateValue.'" ordercolumn="'.$orderColumn.'"  orderval="'.$orderVal.'" pf="'.$pf.'" lang="'.$lang.'"  content="'.$content.'"/></pkg>';
	return sendPostDataToServise($xml);
}

function sendAuthCall($cn, $cookieshash, $ticket, $login="", $password=""){
	$pguid = db_getPortalGuid($cn);
	$lang = getLanguage();
	$xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'"><login cookieshash="'.$cookieshash.'" ticket="'.$ticket.'" login="'.$login.'" password="'.$password.'" lang="'.$lang.'" /></pkg>';
	return sendPostDataToServise($xml);
}

function sendSSPIAuthCall($cn, $ticket, $sspiuser=""){
	$pguid = db_getPortalGuid($cn);
	$lang = getLanguage();
	$xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'"><sspilogin ticket="'.$ticket.'" sspiuser="'.$sspiuser.'" lang="'.$lang.'" /></pkg>';
	return sendPostDataToServise($xml);
}

function sendLogout($cn, $ticket){
	$uhash = "";
	if ( IsUserLogin() == 1 ){
		$userids = GetUserIDs();
		$user = db_getUserDataByID($cn, $userids[0]);
		$uhash = $user['user_hash'];
	}
	//8535 begin
	$uhashfake = '';
	if ( isset($_SESSION['user_uhashfake']) ){
		$uhashfake = $_SESSION['user_uhashfake'];
	}
	//8535 end
	$pguid = db_getPortalGuid($cn);
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface']) : getSetting('portal','defaultLanguage','ru'));
	$content = ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru'));
	$xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><logout uhash="'.$uhash.'"  ticket="'.$ticket.'" uhashfake="'.$uhashfake.'" lang="'.$lang.'" /></pkg>';
	return sendPostDataToServise($xml);
}

function sendServiceShortLogCall($cn, $lang){
	$pguid = db_getPortalGuid($cn);
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface'], true /*71381*/) : getSetting('portal','defaultLanguage','ru'));
	$content = ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru'));
	$xml ='<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><getserviceshortlog/></pkg>';
	return sendPostDataToServise($xml, true);
}

function sendServiceConsoleDataCall($cn, $lang){
	$pguid = db_getPortalGuid($cn);
	$lang = ( isset($_SESSION['user_idlanguageinterface']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguageinterface'], true /*71381*/) : getSetting('portal','defaultLanguage','ru'));
	$content = ( isset($_SESSION['user_idlanguagecontent']) ? db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']) : getSetting('portal','defaultLanguage','ru'));
	$xml = '<?xml version="1.0" encoding="utf-8" ?><pkg type="portalrequest" v="1.0" portal="'.$pguid.'" lang="'.$lang.'" content="'.$content.'"><getservicetable/></pkg>';
	return sendPostDataToServise($xml, true);
}

//STU
function isServiceOnLine()
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, getSetting('service','ip','') );
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $html = curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if (200==$retcode) {
        return true;
    } else {
        return false;
    }
}

// $xml - ppost data, $retry - retry count, $delay - delay in seconds between attempts
function sendPostDataToServise($xml, $tackeoffsession = false, $retrycount = 10, $delay = 10)
{
	if ($tackeoffsession) {
		session_write_close();
	}

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,            getSetting('service','ip','') );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_POST,           1 );
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, getSetting('service','timeoutPost','30') );
	curl_setopt($ch, CURLOPT_TIMEOUT , 	     getSetting('service','timeoutPost','30') );
	curl_setopt($ch, CURLOPT_POSTFIELDS,     $xml );
	curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain'));

	$html = "";
	$count = 0;
	$end = false;
	do {
		$html = curl_exec ($ch);
	    $end = (curl_errno($ch) == 0);
	    $count++;

	    if($count >= $retrycount) {
	        break;
	        $end = true;
	    }

	    if (!$end) {
	    	sleep($delay);
	    }
	}
	while(!$end);

	return $html;
}
?>