<?php
// Check nonce
PHPCUS_PARAM::CUS_REQUIRES_NONCE();

// Check Array Input
PHPCUS_PARAM::CUS_DOES_NOT_SUPPORT_ARRAY();

// Check already authenticated
PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();

if
(
	PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
	(
			[
				'PARAM_TRANSACTION_ID',
				'PARAM_SHORTCODE',
				'PARAM_COMPANY_CODE',
					
				'PARAM_MOBILE_NUMBER',
				'PARAM_MESSAGE'
			], True
	)
)
{

	
	
	Try
	{
		// Read params
		// ------------------------------------------------------------------
		$PARAM_TRANSACTION_ID = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_TRANSACTION_ID');
		$PARAM_SHORTCODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_SHORTCODE');
		$PARAM_COMPANY_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_COMPANY_CODE');
		$PARAM_MOBILE_NUMBER = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_MOBILE_NUMBER');
		$PARAM_MESSAGE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_MESSAGE');
		$CLIENT_ID = '';
		$SECRET_KEY = '';
		
		// Check access rights for requested ShortCode
		// ------------------------------------------------------------------
		try 
		{
			// Retrieve ShortCode Data
			$DATA_SET = PHPCUS_MOBILE_SHORTCODE::CUS_GET
			(
					PHPRC_SQL_FLAGS::FLAG_BIGINT_NULL,
					PHPRC_SQL_FLAGS::FLAG_BIGINT_NULL,
					$PARAM_SHORTCODE
			);
			
			// Get Private Key / Client Key
			if(empty($DATA_SET) === false)
			{
				$CLIENT_ID = PHPRC_PDO_MYSQL_ARRAY::REF_FETCH
				(
					$DATA_SET,
					'MOBILE_CLIENT_ID'
				);
	
				$SECRET_KEY = PHPRC_PDO_MYSQL_ARRAY::REF_FETCH
				(
					$DATA_SET,
					'MOBILE_SECRET_KEY'
				);
			}		
		}
		catch(Exception $ERR)
		{
			PHPCUS_PARAM::CUS_JSON_ERROR('You don\'t have enough rights to perform this request');
		}
		
		
		// Handle Nulls
		// ------------------------------------------------------------------
		if
		(
			PHPVC_STR::VAR_IS_BLANK($PARAM_TRANSACTION_ID) ||
			$PARAM_TRANSACTION_ID == PHPRC_SQL_FLAGS::FLAG_NVARCHAR_NULL
		)
		{
			$PARAM_TRANSACTION_ID = null;
		}
		
		
		// Send Request
		// ------------------------------------------------------------------
		if
		(
			PHPRC_CHIKKA::REF_SEND_SMS
			(
				$PARAM_TRANSACTION_ID,
				$PARAM_MOBILE_NUMBER, 
				$PARAM_SHORTCODE,
				$PARAM_COMPANY_CODE,
				
				$PARAM_MESSAGE, 
				$CLIENT_ID, 
				$SECRET_KEY	
			)
		)
		{
			$ARR = array('UNIQUE_CODE' => PHPRC_CHIKKA::REF_PARAM_LAST_UNIQUE_KEY());
			PHPCUS_PARAM::CUS_JSON_SUCCESS($ARR);	
		}
		else
		{
			PHPCUS_PARAM::CUS_JSON_ERROR(PHPRC_CHIKKA::REF_PARAM_ERROR());
		}	
	}
	Catch(Exception $ERR)
	{
		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
		PHPCUS_PARAM::CUS_JSON_ERROR($ERR->getMessage());
	}
}
else
{
	PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
}

?>