<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc');
}

if ( getSetting('portal','useRights', 0) == 1 && IsUserLogin() ){
	if (isset($_REQUEST['command']) && ($_REQUEST['command'] == 'add') && isset($_REQUEST['oguid'])){
		$cn = db_connect();
		if (isset($_REQUEST['idbranch'])) {
			$idbranch = $_REQUEST['idbranch'];
			$skbranch = db_getSKBranch($cn,$idbranch);	
		} else if (isset($_REQUEST['skbranch'])) {
			$skbranch = $_REQUEST['skbranch'];
			$idbranch = db_getIDBranch($cn, $skbranch );	
		} else {
			$skbranch = db_getMainSKBranch($cn);
			$idbranch = db_getMainIDBranch($cn);	
		}	
		$lng = ( isset($_REQUEST['lang']) ? 'lang='.$_REQUEST['lang'] : '');
		$lngid = db_getLanguageID($cn, getLanguage());
		$contlng = $lng;
		$contlngid = $lngid;
		if (getSetting('portal','useRights',0) == 1){
			$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
			$contlngid = $_SESSION['user_idlanguagecontent'];
		}
		$usr_arr = GetUserIDs();
		$rguid = null;
		if (isset($_REQUEST['rguid'])) {
			if (($_REQUEST['rguid']) == ''){
				$rguid = '00000000-0000-0000-0000-000000000000';
			} else {
				$rguid = $_REQUEST['rguid'] ;
			}
		} else {
			$trSt = db_getReportsToObject($cn, $_REQUEST['oguid'] , $usr_arr, $lngid, $contlngid, $skbranch );
			if (sizeof($trSt) > 0 ) {
				$rguid = $trSt[0]['r_guid'];
			} else {
				$rguid = '00000000-0000-0000-0000-000000000000';
			}
		}
		$urlparams = (isset($_REQUEST['urlparams']) ? "&".urldecode($_REQUEST['urlparams']) : "");
		if ( $rguid !== null ){
			db_addPersonalLink($cn, $usr_arr[0], $_REQUEST['oguid'], $rguid, $skbranch, $urlparams );
		}
		getResource('message.LinkAddedToPersonalPage'); 
		ob_end_flush();
		exit;
	} else {
		getResource('message.LinkAddedToPersonalPageError'); 
		ob_end_flush();
		exit;
	}
} else {
	echo"[{}]";
	ob_end_flush();
	exit;
}
?>