<?php

$log    = "";
$crypto = new SecuredContent();

//CAMPAIGN SYNC
echo "\n\n***** CAMPAIGN SYNC *****\n";
$api_uri =  get_option("id_api_salesforce");
$api_rest=CampaignController::kREST_ACTION;

$api_param="?Brand=";
$api_brand= get_option("id_brand");
$url = $api_uri .$api_rest . $api_param . $api_brand;
echo "URL: ".$url;
$request_campaign = curl_init($url);
curl_setopt($request_campaign, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt($request_campaign, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request_campaign, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($request_campaign, CURLOPT_COOKIE, 'debug_logs=debug_logs,domain=.force.com');
$auth_header = json_encode($crypto->encode_content(urldecode($url)));
curl_setopt($request_campaign, CURLOPT_HTTPHEADER, array('CA-Authorization: ' . $auth_header));
$result = curl_exec($request_campaign);
curl_close($request_campaign);
echo $result;
$data = stripslashes($result);

if(!empty($data)){

    $data = substr($data,1);
    $data = substr($data,0,-1);
    $data = (OBJECT)json_decode($crypto->decode_content($data));

    if(!empty($data) && !empty($data->ResponseBody)){

        echo "\nCampaign Updates:";
        $campaignController = CampaignController::instance();
        $campaignController->cronJob($data);

    }else{
        echo "\nNo data";
    }

}else{
    echo "\n** FAILED **\n";
}

echo "\n\n***** END CAMPAIGN SYNC *****";
