<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$userToDeleteList = array (
                      0 =>
                      array (
                        'contactId' => 'DYZLss',
                      ),
                      1 =>
                      array (
                        'contactId' => 'Dwu4fk',
                      ),
                      2 =>
                      array (
                        'contactId' => '34Lz0U',
                      ),
                      3 =>
                      array (
                        'contactId' => '081Dlj',
                      ),
                      4 =>
                      array (
                        'contactId' => 'mu1mtD',
                      ),
                      5 =>
                      array (
                        'contactId' => 'g2V1V6',
                      ),
                      6 =>
                      array (
                        'contactId' => '9mRpgg',
                      ),
                      7 =>
                      array (
                        'contactId' => '3otuuB',
                      ),
                      8 =>
                      array (
                        'contactId' => '3otWft',
                      ),
                      9 =>
                      array (
                        'contactId' => 'tCJGRI',
                      ),
                      10 =>
                      array (
                        'contactId' => '085VqW',
                      ),
                      11 =>
                      array (
                        'contactId' => 'DHAs6S',
                      ),
                      12 =>
                      array (
                        'contactId' => 'e2N1Zp',
                      ),
                      13 =>
                      array (
                        'contactId' => 'DlzqSB',
                      ),
                      14 =>
                      array (
                        'contactId' => 'JPIOE2',
                      ),
                      15 =>
                      array (
                        'contactId' => '0B0PBS',
                      ),
                      16 =>
                      array (
                        'contactId' => 'tCJu0p',
                      ),
                      17 =>
                      array (
                        'contactId' => '2pANDS',
                      ),
                      18 =>
                      array (
                        'contactId' => 'DZsZnU',
                      ),
                      19 =>
                      array (
                        'contactId' => '2MM7x2',
                      ),
                      20 =>
                      array (
                        'contactId' => 'mHMXmp',
                      ),
                      21 =>
                      array (
                        'contactId' => '2jjfHz',
                      ),
                      22 =>
                      array (
                        'contactId' => 'DLTLFe',
                      ),
                      23 =>
                      array (
                        'contactId' => '2tTGVu',
                      ),
                      24 =>
                      array (
                        'contactId' => 'ezQyyu',
                      ),
                      25 =>
                      array (
                        'contactId' => 'JwlXvM',
                      ),
                      26 =>
                      array (
                        'contactId' => 'JrP3GS',
                      ),
                      27 =>
                      array (
                        'contactId' => '0cyXQj',
                      ),
                      28 =>
                      array (
                        'contactId' => '0p3rGD',
                      ),
                      29 =>
                      array (
                        'contactId' => '2rFUZ6',
                      ),
                      30 =>
                      array (
                        'contactId' => 'DVoTMT',
                      ),
                      31 =>
                      array (
                        'contactId' => 'Ju68QQ',
                      ),
                      32 =>
                      array (
                        'contactId' => '0liybz',
                      ),
                      33 =>
                      array (
                        'contactId' => 'JPkrVd',
                      ),
                      34 =>
                      array (
                        'contactId' => 'Dl6bMg',
                      ),
                      35 =>
                      array (
                        'contactId' => 'DOJSdY',
                      ),
                      36 =>
                      array (
                        'contactId' => 'tih37d',
                      ),
                      37 =>
                      array (
                        'contactId' => 'eJFpJi',
                      ),
                      38 =>
                      array (
                        'contactId' => 'e7vATf',
                      ),
                      39 =>
                      array (
                        'contactId' => '0TUMJp',
                      ),
                      40 =>
                      array (
                        'contactId' => 'JSuhL6',
                      ),
                      41 =>
                      array (
                        'contactId' => 'm5MQ0I',
                      ),
                      42 =>
                      array (
                        'contactId' => 'lA6sop',
                      ),
                      43 =>
                      array (
                        'contactId' => 'kzNuSB',
                      ),
                      44 =>
                      array (
                        'contactId' => '2EeJNd',
                      ),
                      45 =>
                      array (
                        'contactId' => 'DR896g',
                      ),
                      46 =>
                      array (
                        'contactId' => '2zvp6B',
                      ),
                      47 =>
                      array (
                        'contactId' => '2zAc8T',
                      ),
                      48 =>
                      array (
                        'contactId' => '2zAZre',
                      ),
                      49 =>
                      array (
                        'contactId' => 'mfWGtS',
                      ),
                      50 =>
                      array (
                        'contactId' => 'IPkCFu',
                      ),
                      51 =>
                      array (
                        'contactId' => 'CRZR1W',
                      ),
                      52 =>
                      array (
                        'contactId' => 'lfwINh',
                      ),
                      53 =>
                      array (
                        'contactId' => 'DWTPYA',
                      ),
                      54 =>
                      array (
                        'contactId' => 'JuaEMC',
                      ),
                      55 =>
                      array (
                        'contactId' => '3Kdt6T',
                      ),
                      56 =>
                      array (
                        'contactId' => '2a0Xwe',
                      ),
                      57 =>
                      array (
                        'contactId' => 'mFfpCT',
                      ),
                      58 =>
                      array (
                        'contactId' => '0DONne',
                      ),
                      59 =>
                      array (
                        'contactId' => '20UXTc',
                      ),
                      60 =>
                      array (
                        'contactId' => '2kP1zt',
                      ),
                      61 =>
                      array (
                        'contactId' => 'JFWd6Q',
                      ),
                      62 =>
                      array (
                        'contactId' => '2PLz6k',
                      ),
                      63 =>
                      array (
                        'contactId' => '2bSYqe',
                      ),
                      64 =>
                      array (
                        'contactId' => '0KQzaI',
                      ),
                      65 =>
                      array (
                        'contactId' => 'mhZQds',
                      ),
                      66 =>
                      array (
                        'contactId' => 'lsnRsk',
                      ),
                      67 =>
                      array (
                        'contactId' => '0O8nYk',
                      ),
                      68 =>
                      array (
                        'contactId' => '2qJqg8',
                      ),
                      69 =>
                      array (
                        'contactId' => 'D7qHo6',
                      ),
                      70 =>
                      array (
                        'contactId' => 'DO02Yc',
                      ),
                      71 =>
                      array (
                        'contactId' => 'mNJTRI',
                      ),
                      72 =>
                      array (
                        'contactId' => '2ZoPnh',
                      ),
                      73 =>
                      array (
                        'contactId' => '2yvOWD',
                      ),
                      74 =>
                      array (
                        'contactId' => 'JGn5y6',
                      ),
                      75 =>
                      array (
                        'contactId' => 'ttahVU',
                      ),
                      76 =>
                      array (
                        'contactId' => 'X0SRJI',
                      ),
                      77 =>
                      array (
                        'contactId' => '2nDnt8',
                      ),
                      78 =>
                      array (
                        'contactId' => 'mHVusj',
                      ),
                      79 =>
                      array (
                        'contactId' => 'gwgIfk',
                      ),
                      80 =>
                      array (
                        'contactId' => '0mO9ru',
                      ),
                      81 =>
                      array (
                        'contactId' => 'tiBQOM',
                      ),
                      82 =>
                      array (
                        'contactId' => 'txE0SY',
                      ),
                      83 =>
                      array (
                        'contactId' => 'm2Cybh',
                      ),
                      84 =>
                      array (
                        'contactId' => '3O39H2',
                      ),
                      85 =>
                      array (
                        'contactId' => 'JSOEVI',
                      ),
                      86 =>
                      array (
                        'contactId' => '08UaYs',
                      ),
                      87 =>
                      array (
                        'contactId' => '042fzS',
                      ),
                      88 =>
                      array (
                        'contactId' => '2XE3wu',
                      ),
                      89 =>
                      array (
                        'contactId' => '2B7HCc',
                      ),
                      90 =>
                      array (
                        'contactId' => 'J3J5y6',
                      ),
                      91 =>
                      array (
                        'contactId' => '3bMfsf',
                      ),
                      92 =>
                      array (
                        'contactId' => '9LLZme',
                      ),
                      93 =>
                      array (
                        'contactId' => 'mfMOKh',
                      ),
                      94 =>
                      array (
                        'contactId' => 'wdIJPU',
                      ),
                    );

// getting custom fields information

// Only allow ajax requests
/*if( !isset($_SERVER["HTTP_REFERER"]) || !isset($_SERVER["HTTP_X_REQUESTED_WITH"]) || strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) != "xmlhttprequest" ) {

    exit("N - not allowed");

}*/

// test token = aV4d8
/*
4Z4uT y
T8BU8 n
4kj4U n
6jHeY n
6y3Cg n
6saig n
*/

function callDELETECURL($path, $headers)
{
    $url = $path;
    $json = '';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $resp = curl_exec($ch);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($resp, 0, $header_size);
    $body = substr($resp, $header_size);
    curl_close($ch);
    return "response of curl: " . $resp . "<br>Header info: " . $header . "<br>Body info: " . $body;
    //return $resp;
}

function callGETCURL($url, $headers) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $resp = curl_exec($ch);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($resp, 0, $header_size);
    $body = substr($resp, $header_size);
    curl_close($ch);
    //return "response of curl: " . $resp . "<br>Header info: " . $header . "<br>Body info: " . $body;
    return $resp;
}

function callPOSTCURL($dataArray, $headers, $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataArray));
    $resp = curl_exec($ch);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($resp, 0, $header_size);
    $body = substr($resp, $header_size);
    curl_close($ch);
    return "response of curl: " . $resp . "<br>Header info: " . $header . "<br>Body info: " . $body;
}

function getResultFromNewAPI($token, $email, $headers) {

    $apiEndpoint = "https://api3.getresponse360.pl/v3";
    //$url = $apiEndpoint . "/campaigns/" . $token;
    //$url = $apiEndpoint . "/custom-fields";
    //$url = $apiEndpoint . "/contacts?query[campaignId]=".$token."&query[email]=zascia@ukr.net";
    $url = $apiEndpoint . "/campaigns/".$token."/contacts";
    $response = callGETCURL($url, $headers);
    return $response;
}



function deleteUserFromNewAPI($contactId) {
    $apiEndpoint = "https://api3.getresponse360.pl/v3/contacts/";
    $path = $apiEndpoint . $contactId;
    $headers = array();
    $headers[] = "x-auth-token: api-key 98e9893f951367413bd5204890473ba0";
    $headers[] = "X-Domain: cepamail.com";
    $headers[] = "Content-Type: application/json";

    $jsondata = callDELETECURL($path, $headers);
    echo "result of delete user $contactId:<br><pre>";
    print_r(json_decode($jsondata));
}

function deleteUserInsideTheList($userToDeleteList) {
    foreach($userToDeleteList as $userItem) {
        $contactId = $userItem['contactId'];
        deleteUserFromNewAPI($contactId);
    }
}

function saveToFile($email, $jsondata) {
    $date = new DateTime();
    $file = 'data.txt';
    $datas = "Email: " . $email . " Response: " . $jsondata;

    file_put_contents($file, "\n".$date->format('Y-m-d H:i:s')." || ".$datas, FILE_APPEND | LOCK_EX);
}

function getContactsNewAPI() {
    $email = "zascia@ukr.net";
    $token = "yiCLG";
    $headers = array();
    $headers[] = "x-auth-token: api-key 98e9893f951367413bd5204890473ba0";
    $headers[] = "X-Domain: cepamail.com";
    $headers[] = "Content-Type: application/json";

    $jsondata = getResultFromNewAPI($token, $email, $headers);

    echo "<pre>";
    print_r(json_decode($jsondata));
}


//saveToFile($email, $jsondata);

// GETRESPONSE DELIVERY
function createUpdateOldAPICallback() {
    $dataArray = array (
    	"url" => "https://zascia.in.ua/api/getresponse/callback/",
    	"actions" => array (
    	    "subscribe" => true,
    		"unsubscribe" => true
    	)
    );

    $headers = array ("X-Auth-Token: api-key 98e9893f951367413bd5204890473ba0", "Content-Type: application/json");
    $url = 'https://api.getresponse.com/v3/accounts/callbacks';

    $responseAfterSetupCallback = callPOSTCURL($dataArray, $headers, $url);

    echo "<pre>";
    print_r($responseAfterSetupCallback);
}

function createUpdateNewAPICallback() {
    $dataArray = array (
    	"url" => "https://zascia.in.ua/api/getresponse/callbacknew2old/",
    	"actions" => array (
    	    "subscribe" => false,
    		"unsubscribe" => false
    	)
    );

    $headers = array ("X-Auth-Token: api-key 98e9893f951367413bd5204890473ba0", "Content-Type: application/json", "X-Domain: cepamail.com");
    $url = 'https://api3.getresponse360.pl/v3/accounts/callbacks';

    $responseAfterSetupCallback = callPOSTCURL($dataArray, $headers, $url);

    echo "<pre>";
    print_r($responseAfterSetupCallback);
}

//deleteUserInsideTheList($userToDeleteList);
createUpdateNewAPICallback();

//echo "</pre>";

?>