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

// Dismiss Session
PHPVC_SESSION::VAR_STOP();

// Check array size
PHPCUS_PARAM::CUS_ARRAY_TOO_LARGE();

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

// Check access rights
// PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_EDIT, 'record_discon');

if
(
    PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
    (
        [
            'PARAM_REC_BROADCAST_PHONE',
            'PARAM_REC_BROADCAST_MESSAGE',
            'PARAM_REC_BROADCAST_REQUEST_DATE'
        ], True
        )
    )
{
    Try
    {
        $PARAM_REC_BROADCAST_PHONE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_BROADCAST_PHONE', true);
        $PARAM_REC_BROADCAST_MESSAGE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_BROADCAST_MESSAGE', true);
        $PARAM_REC_BROADCAST_REQUEST_DATE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_BROADCAST_REQUEST_DATE', true);
        $PARAM_ARRAY_COUNT = count($PARAM_REC_BROADCAST_PHONE);
        $ARRAY_CTR = 0;
        $HTTP_PARAM = '';
        
        while($ARRAY_CTR < $PARAM_ARRAY_COUNT)
        {
            if($ARRAY_CTR >= 1)
            {
                $HTTP_PARAM = $HTTP_PARAM . '&' .
                    'PARAM_REC_CLIENT_USERNAME[]=' . urlencode(PHPSET::SMS_GATEWAY_USERNAME) . '&' .
                    'PARAM_REC_CLIENT_PASSWORD[]=' . urlencode(PHPSET::SMS_GATEWAY_PASSWORD) . '&' .
                    'PARAM_REC_BROADCAST_PHONE[]=' . urlencode($PARAM_REC_BROADCAST_PHONE[$ARRAY_CTR]) . '&' .
                    'PARAM_REC_BROADCAST_MESSAGE[]=' . urlencode($PARAM_REC_BROADCAST_MESSAGE[$ARRAY_CTR] . ' ' . PHPSET::SMS_GATEWAY_USER) . '&' .
                    'PARAM_REC_BROADCAST_REQUEST_DATE[]=' . urlencode($PARAM_REC_BROADCAST_REQUEST_DATE[$ARRAY_CTR]) . '&' .
                    'PARAM_REC_BROADCAST_DEVICE[]=' . urlencode('PHP Server') . '&' .
                    'PARAM_REC_BROADCAST_STATUS[]=2';
            }
            else
            {
               $HTTP_PARAM =
                    'PARAM_REC_CLIENT_USERNAME[]=' . urlencode(PHPSET::SMS_GATEWAY_USERNAME) . '&' .
                    'PARAM_REC_CLIENT_PASSWORD[]=' . urlencode(PHPSET::SMS_GATEWAY_PASSWORD) . '&' .
                    'PARAM_REC_BROADCAST_PHONE[]=' . urlencode($PARAM_REC_BROADCAST_PHONE[$ARRAY_CTR]) . '&' .
                    'PARAM_REC_BROADCAST_MESSAGE[]=' . urlencode($PARAM_REC_BROADCAST_MESSAGE[$ARRAY_CTR] . ' ' . PHPSET::SMS_GATEWAY_USER) . '&' .
                    'PARAM_REC_BROADCAST_REQUEST_DATE[]=' . urlencode($PARAM_REC_BROADCAST_REQUEST_DATE[$ARRAY_CTR]) . '&' .
                    'PARAM_REC_BROADCAST_DEVICE[]=' . urlencode('PHP Server') . '&' .
                    'PARAM_REC_BROADCAST_STATUS[]=2';             
            }

            
            $ARRAY_CTR ++;
        }
        
        PHPRC_HTTP::REF_REQUEST(PHPSET::SMS_GATEWAY_URL, $HTTP_PARAM);
        PHPCUS_PARAM::CUS_JSON_SUCCESS('');   
    }
    Catch(Exception $ERR)
    {
        PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
    }
}
else
{
    PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
}

?>