* i.e timezones, clients and getting your API Key from username and password
 * @author tobyb
 *
 */
if (!class_exists('CS_REST_Wrapper_Base')) {
    class CS_REST_Wrapper_Base
    {
        /*  *
	 * The protocol to use while accessing the api
	 * @var string http or https
	 * @access private
	 */
        var $_protocol;
<?php

require_once '../../class/serialisation.php';
require_once '../../class/log.php';
// Get a serialiser for the webhook data - We assume here that we're dealing with json
$serialiser = CS_REST_SERIALISATION_get_available(new CS_REST_Log(CS_REST_LOG_NONE));
// Read all the posted data from the input stream
$raw_post = file_get_contents("php://input");
// We can log the raw data straight to disk
$raw_log = fopen('raw_log.txt', 'a') or die('Can\'t open raw log');
fwrite($raw_log, date('H:i:s') . $raw_post . "\n\n\n");
fclose($raw_log);
// And deserialise the data
$deserialised_data = $serialiser->deserialise($raw_post);
$parsed_log = fopen('parsed_log.txt', 'a') or die('Can\'t open parsed log');
fwrite($parsed_log, date('H:i:s') . ' Got hook data for list: ' . $deserialised_data->ListID . "\n");
// And now just do something with the data
foreach ($deserialised_data->Events as $event) {
    fwrite($parsed_log, 'Got ' . $event->Type . ' event for: ' . $event->EmailAddress . "\n");
    fwrite($parsed_log, var_export($event, true));
}
fclose($parsed_log);
function cmdr_cm_sync()
{
    global $cmdr_fields_to_hide;
    if (!class_exists('CS_REST_SERIALISATION_get_available')) {
        require_once CMDR_PLUGIN_PATH . 'campaignmonitor-createsend-php/class/serialisation.php';
    }
    if (!class_exists('CS_REST_Log')) {
        require_once CMDR_PLUGIN_PATH . 'campaignmonitor-createsend-php/class/log.php';
    }
    // Get a serialiser for the webhook data - We assume here that we're dealing with json
    $serialiser = CS_REST_SERIALISATION_get_available(new CS_REST_Log(CS_REST_LOG_NONE));
    // Read all the posted data from the input stream
    $raw_post = file_get_contents("php://input");
    // And deserialise the data
    $deserialised_data = $serialiser->deserialise($raw_post);
    // List ID check
    $list_id = $deserialised_data->ListID;
    if (trim($list_id) == trim(get_option('cmdr_list_id'))) {
        $cmdr_user_fields = (array) unserialize(base64_decode(get_option('cmdr_user_fields')));
        remove_action('profile_update', 'cmdr_user_update', 10);
        remove_action('user_register', 'cmdr_user_insert');
        remove_filter('update_user_metadata', 'cmdr_user_meta_update', 1000);
        foreach ($deserialised_data->Events as $subscriber) {
            if (!empty($subscriber->OldEmailAddress)) {
                $user = get_user_by('email', $subscriber->OldEmailAddress);
            } else {
                $user = get_user_by('email', $subscriber->EmailAddress);
            }
            if ($user) {
                if ($user->user_email != $subscriber->EmailAddress) {
                    wp_update_user(array('ID' => $user->ID, 'user_email' => $subscriber->EmailAddress));
                }
                if ($user->first_name . ' ' . $user->last_name != $subscriber->Name) {
                    $n = explode(' ', $subscriber->Name);
                    $fn = array_shift($n);
                    $ln = implode(' ', $n);
                    if ($fn) {
                        update_user_meta($user->ID, 'first_name', $fn);
                    }
                    if ($ln) {
                        update_user_meta($user->ID, 'last_name', $ln);
                    }
                    foreach ($subscriber->CustomFields as $key => $field) {
                        if (in_array($field->Key, $cmdr_user_fields) && !in_array($field->Key, $cmdr_fields_to_hide)) {
                            update_user_meta($user->ID, $field->Key, $field->Value);
                        }
                    }
                }
            }
        }
    }
    echo 'ok';
    die;
}