<?php

	// Perform Login
    $SHORTCODE_STATUS = PHPCUS_SHORTCODE::CUS_QUICK_LOGIN();
	
	// Check Array Input
	PHPCUS_PARAM::CUS_DOES_NOT_SUPPORT_ARRAY();
	
	if
	(
		PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
		(
			[
				'PARAM_TABLE',
				'PARAM_DATA',
			    
			    'PARAM_ACC_LINKSVR_USERNAME_REQUESTER',
			    'PARAM_ACC_LINKSVR_PASSWORD_REQUESTER',
			    'PARAM_ACC_LINKSVR_USERNAME_CLIENT',
			    'PARAM_ACC_LINKSVR_PUBLIC_KEY_CLIENT',
			    'PARAM_ACC_LINKSVR_URL_CLIENT'
			], True
		)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------	
		    $PARAM_TABLE = PHPVC_STR::VAR_LCASE(PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_TABLE'));
		    $PARAM_DATA = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_DATA');
		    
		    $PARAM_ACC_LINKSVR_USERNAME_REQUESTER = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_USERNAME_REQUESTER');
		    $PARAM_ACC_LINKSVR_PASSWORD_REQUESTER = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_PASSWORD_REQUESTER');
		    $PARAM_ACC_LINKSVR_USERNAME_CLIENT = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_USERNAME_CLIENT');
		    $PARAM_ACC_LINKSVR_PUBLIC_KEY_CLIENT = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_PUBLIC_KEY_CLIENT');
		    $PARAM_ACC_LINKSVR_URL_CLIENT = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_URL_CLIENT');	    
			// ------------------------------------------------------------------
			
			
		    // Verify Auth Username and Password
		    // _______________________________________________________________________
		    $DATASET = PHPCUS_ACCOUNT_LINKSVR_CUSTOM::CUS_LOGIN
		    (
		        $PARAM_ACC_LINKSVR_USERNAME_REQUESTER,
		        $PARAM_ACC_LINKSVR_PASSWORD_REQUESTER
		    );
		    if(empty($DATASET))
		    {
		        PHPCUS_PARAM::CUS_JSON_ERROR('Invalid username or password.');
		    }

		    
		    // Verify if Client Public Auth is correct
		    // _______________________________________________________________________
		    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_USERNAME"] = $PARAM_ACC_LINKSVR_USERNAME_CLIENT;
		    $GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_PUBLIC_KEY"] = $PARAM_ACC_LINKSVR_PUBLIC_KEY_CLIENT;
		    $GET_MAPPED_ARRAY["PARAM_ACC_LINKSVR_URL_CLIENT"] = $PARAM_ACC_LINKSVR_URL_CLIENT;
		    $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;
		    
		    $DATASET = PHPCUS_ACCOUNT_LINKSVR::CUS_GET_MA($GET_MAPPED_ARRAY);
		    if(empty($DATASET))
		    {
		        PHPCUS_PARAM::CUS_JSON_ERROR('You don\'t have enough rights to access this feature.');
		    }
		    $SELF_USERNAME = $DATASET[0]['ACC_LINKSVR_USERNAME'];
		    $SELF_PASSWORD = $DATASET[0]['ACC_LINKSVR_HASH_KEY'];
		    
		 
		    // Prepare URL 
		    // _______________________________________________________________________
	
		    // Group (Branches) : GET
		    // ...........................................
		    if($PARAM_TABLE == 'account_group/get')
		    {
		        $PARAM_ACC_SERVER_URL_CLIENT = $PARAM_ACC_LINKSVR_URL_CLIENT . '/Account/group/linksvr_port_get';
		    }
		    
            // Z-Reading : GET
            // ...........................................
            else if($PARAM_TABLE == 'record_eod_zreading/estimate')
            {
                $PARAM_ACC_SERVER_URL_CLIENT = $PARAM_ACC_LINKSVR_URL_CLIENT . '/Record/eod_zreading/linksvr_port_estimate';
            }
            
            // Loyalty : GET
            // ...........................................
            else if($PARAM_TABLE == 'record_loyalty/get')
            {
                
            }
            
            // Loyalty : SAVE
            // ...........................................
            else if($PARAM_TABLE == 'record_loyalty/save')
            {
                
            }
            
            // Invalid URL
            // ...........................................
            else
            {
                PHPCUS_PARAM::CUS_JSON_ERROR('Invalid PARAM_TABLE:' . $PARAM_TABLE);
            }
			      
            
			// Populate HTTP Param
			// _______________________________________________________________________
            $HTTP_PARAM = '';
            
            $PARAM_DATA = urldecode($PARAM_DATA);
            $HTTP_PARAM = $HTTP_PARAM . $PARAM_DATA;
            $HTTP_PARAM = $HTTP_PARAM . 'PARAM_ACC_LINKSVR_USERNAME=' . urlencode($SELF_USERNAME) . '&';
            $HTTP_PARAM = $HTTP_PARAM . 'PARAM_ACC_LINKSVR_PASSWORD=' . urlencode($SELF_PASSWORD);
            
            // Send Request
            // ...........................................
            $RESPONSE_STRING = PHPRC_HTTP::REF_REQUEST($PARAM_ACC_SERVER_URL_CLIENT, $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');
	}

?>