<?php

	// Perform Login
    $SHORTCODE_STATUS = PHPCUS_SHORTCODE::CUS_QUICK_LOGIN();
	
	// Check Array Input
	PHPCUS_PARAM::CUS_DOES_NOT_SUPPORT_ARRAY();
	
	// Check already authenticated
	PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();
	
	// Check access rights
	PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_ONLY, 'account_linksvr');
	
	if
	(
		PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
		(
			[
			        'PARAM_ACC_LINKSVR_ID',
			        'PARAM_REC_EODREP_ACC_GROUP_ID',
					'PARAM_REC_EODREP_ZREAD_DATE_FROM',
					'PARAM_REC_EODREP_ZREAD_DATE_TO'
			], True
		)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------	
		    $PARAM_ACC_LINKSVR_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ACC_LINKSVR_ID');
		    $PARAM_REC_EODREP_ACC_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EODREP_ACC_GROUP_ID');
			$PARAM_REC_EODREP_ZREAD_DATE_FROM = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EODREP_ZREAD_DATE_FROM');
			$PARAM_REC_EODREP_ZREAD_DATE_TO = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EODREP_ZREAD_DATE_TO');
			// ------------------------------------------------------------------
			
			
			// Get Self ID
			// ...........................................
			PHPCUS_SP::CUS_AUTHENTICATION_OVERRIDE('system_automation', '6UGlJNVxnQlBOCBoHJdvoYTg6HxKIGrY3862653932616634663463303736643732316230353234616335366436393137626539376633353837663763396262303265333265393534306234326664653562666361303464663234366234323166623633343939313236643238303461343239643537613666323736393835396635356466343138383137393565346532');
			$GET_MAPPED_ARRAY = PHPCUS_ACCOUNT_LINKSVR::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_IS_SELF"] = PHPCUS_SP::STATUS_ACTIVE;
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_STATUS"] = PHPCUS_SP::STATUS_ACTIVE;
			$GET_MAPPED_ARRAY["PARAM_LIMIT"] = 1;

			// If no self
			// ...........................................
			$DATASET = PHPCUS_ACCOUNT_LINKSVR::CUS_GET_MA($GET_MAPPED_ARRAY);
			if(empty($DATASET))
			{
			    PHPCUS_PARAM::CUS_JSON_ERROR('Server data is not prepared [self]');
			}
			$SELF_USERNAME = $DATASET[0]['ACC_LINKSVR_USERNAME']; 
			$SELF_PASSWORD = $DATASET[0]['ACC_LINKSVR_HASH_KEY'];
			
			
			// ~~~~~
			
			
			// Get Server URL
			// ...........................................
			PHPCUS_SP::CUS_AUTHENTICATION_OVERRIDE('system_automation', '6UGlJNVxnQlBOCBoHJdvoYTg6HxKIGrY3862653932616634663463303736643732316230353234616335366436393137626539376633353837663763396262303265333265393534306234326664653562666361303464663234366234323166623633343939313236643238303461343239643537613666323736393835396635356466343138383137393565346532');
			$GET_MAPPED_ARRAY = PHPCUS_ACCOUNT_LINKSVR::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_IS_SELF"] = PHPCUS_SP::STATUS_INACTIVE;
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_AUTH_LEVEL"] = 3;
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_STATUS"] = PHPCUS_SP::STATUS_ACTIVE;
			$GET_MAPPED_ARRAY["PARAM_LIMIT"] = 1;
					
			// If no server
			// ...........................................
			$DATASET = PHPCUS_ACCOUNT_LINKSVR::CUS_GET_MA($GET_MAPPED_ARRAY);
			if(empty($DATASET))
			{
			    PHPCUS_PARAM::CUS_JSON_ERROR('Server data is not prepared [server]');
			}
			$SERVER_URL = $DATASET[0]['ACC_LINKSVR_URL'] . 'Account/linksvr/svrport';
		
			
			// ~~~~~
			
			
			// Get Target URL (if exists)
			// ...........................................
			PHPCUS_SP::CUS_AUTHENTICATION_OVERRIDE('system_automation', '6UGlJNVxnQlBOCBoHJdvoYTg6HxKIGrY3862653932616634663463303736643732316230353234616335366436393137626539376633353837663763396262303265333265393534306234326664653562666361303464663234366234323166623633343939313236643238303461343239643537613666323736393835396635356466343138383137393565346532');
			$GET_MAPPED_ARRAY = PHPCUS_ACCOUNT_LINKSVR::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_ID"] = $PARAM_ACC_LINKSVR_ID;
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_IS_SELF"] = PHPCUS_SP::STATUS_INACTIVE;
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_AUTH_LEVEL"] = 1;
			$GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_STATUS"] = PHPCUS_SP::STATUS_ACTIVE;
			$GET_MAPPED_ARRAY["PARAM_LIMIT"] = 1;
			
			// If no Server
			// ...........................................
			$DATASET = PHPCUS_ACCOUNT_LINKSVR::CUS_GET_MA($GET_MAPPED_ARRAY);
			if(empty($DATASET))
			{
			    PHPCUS_PARAM::CUS_JSON_ERROR('Server data is not prepared [client]');
			}
			$CLIENT_USERNAME = $DATASET[0]['ACC_LINKSVR_USERNAME'];
			$CLIENT_PUBLIC_KEY = $DATASET[0]['ACC_LINKSVR_PUBLIC_KEY'];
			$CLIENT_URL = $DATASET[0]['ACC_LINKSVR_URL'];
			
			
			// ~~~~~
			
			
			// Populate Map Array
			// ...........................................
			$PARAM_DATA = '';
			$PARAM_DATA = $PARAM_DATA . 'PARAM_REC_EODREP_ACC_GROUP_ID=' . urlencode($PARAM_REC_EODREP_ACC_GROUP_ID) . '&';
			$PARAM_DATA = $PARAM_DATA . 'PARAM_REC_EODREP_ZREAD_DATE_FROM=' . urlencode($PARAM_REC_EODREP_ZREAD_DATE_FROM) . '&';
			$PARAM_DATA = $PARAM_DATA . 'PARAM_REC_EODREP_ZREAD_DATE_TO=' . urlencode($PARAM_REC_EODREP_ZREAD_DATE_TO) . '&';
			
			$HTTP_PARAM = '';
			$HTTP_PARAM = $HTTP_PARAM . 'PARAM_TABLE=' . urlencode('record_eod_zreading/estimate') . '&';
			$HTTP_PARAM = $HTTP_PARAM . 'PARAM_DATA=' . urlencode($PARAM_DATA) . '&';
			
			$HTTP_PARAM = $HTTP_PARAM . 'PARAM_ACC_LINKSVR_USERNAME_REQUESTER=' . urlencode($SELF_USERNAME) . '&';
			$HTTP_PARAM = $HTTP_PARAM . 'PARAM_ACC_LINKSVR_PASSWORD_REQUESTER=' . urlencode($SELF_PASSWORD) . '&';
			$HTTP_PARAM = $HTTP_PARAM . 'PARAM_ACC_LINKSVR_USERNAME_CLIENT=' . urlencode($CLIENT_USERNAME) . '&';
			$HTTP_PARAM = $HTTP_PARAM . 'PARAM_ACC_LINKSVR_PUBLIC_KEY_CLIENT=' . urlencode($CLIENT_PUBLIC_KEY) . '&';
			$HTTP_PARAM = $HTTP_PARAM . 'PARAM_ACC_LINKSVR_URL_CLIENT=' . urlencode($CLIENT_URL);
			
			// Send Request
			// ...........................................
			$RESPONSE_STRING = PHPRC_HTTP::REF_REQUEST($SERVER_URL, $HTTP_PARAM, 20);
			echo $RESPONSE_STRING;
			exit;
		}
		Catch(Exception $ERR)
		{		
			PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
			if(isset($_SERVER['HTTP_ORIGIN']))
			{
			    PHPVC_LOG::VAR_ERROR('HTTP_ORIGIN', $_SERVER['HTTP_ORIGIN']);
			}
			if(isset($_SERVER['HTTP_HOST']))
			{
			    PHPVC_LOG::VAR_ERROR('HTTP_HOST', $_SERVER['HTTP_HOST']);
			}		
			if(isset($_SERVER['HTTP_REFERER']))
			{
			    PHPVC_LOG::VAR_ERROR('HTTP_REFERER', $_SERVER['HTTP_REFERER']);
			}
			if(isset($_SERVER['REMOTE_ADDR']))
			{
			    PHPVC_LOG::VAR_ERROR('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
			}
			
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR->getMessage());
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}

?>