/**
  * Validate email based on user settings.
  *
  * First, verify email using `is_email()` WordPress function (required)
  *
  * Then, process email validation based on settings.
  *
  * @param  KWSContact $Contact Contact object
  *
  * @return WP_Error|boolean|void    If valid, return `true`, otherwise return a WP_Error object.
  */
 function validateEmail(KWSContact &$Contact)
 {
     if (!class_exists('DataValidation')) {
         include_once CTCT_DIR_PATH . 'lib/class.datavalidation.php';
     }
     if (!class_exists('SMTP_validateEmail')) {
         include_once CTCT_DIR_PATH . 'lib/mail/smtp_validateEmail.class.php';
     }
     $email = $Contact->get('email');
     $is_valid = array();
     // 1: Check if it's an email at all
     if (empty($email)) {
         do_action('ctct_activity', 'Empty email address', $email);
         $this->errors[] = new WP_Error('empty_email', __('Please enter your email address.', 'ctct'), 'email_address');
         return;
     } elseif (!is_email($email)) {
         do_action('ctct_activity', 'Invalid email address', $email);
         $this->errors[] = new WP_Error('not_email', __('Invalid email address.', 'ctct'), 'email_address');
         return;
     }
     $methods = (array) CTCT_Settings::get('spam_methods');
     // 2: Akismet validation
     if (in_array('akismet', $methods)) {
         $akismetCheck = $this->akismetCheck($Contact);
         if (is_wp_error($akismetCheck)) {
             $this->errors[] = $akismetCheck;
             return;
         }
     }
     // 3: WangGuard validation
     if (in_array('wangguard', $methods) && function_exists('wangguard_verify_email') && wangguard_server_connectivity_ok()) {
         global $wangguard_api_host;
         // If WangGuard isn't set up yet, set'er up!
         if (empty($wangguard_api_host)) {
             wangguard_init();
         }
         $return = wangguard_verify_email($email, wangguard_getRemoteIP(), wangguard_getRemoteProxyIP());
         if ($return == 'checked' || $return == 'not-checked') {
             do_action('ctct_activity', 'WangGuard validation passed.', $email, $return);
         } else {
             $this->errors[] = new WP_Error('wangguard', __('Email validation failed.', 'ctct'), $email, $return);
             return;
         }
     }
     // 4: DataValidation.com validation
     if (in_array('datavalidation', $methods) && class_exists('DataValidation')) {
         $Validate = new DataValidation(CTCT_Settings::get('datavalidation_api_key'));
         $validation = $Validate->validate($email);
         $process_inconclusive = apply_filters('ctct_process_inconclusive_emails', true);
         if (is_wp_error($validation)) {
             do_action('ctct_activity', 'DataValidation.com error', 'The email was not processed because of the error: ' . $validation->get_error_message());
             return;
         } elseif ($validation === false || $validation === NULL && !$process_inconclusive) {
             do_action('ctct_activity', 'DataValidation validation failed.', $email, $Validate);
             $message = isset($Validate->message) ? $Validate->message : __('Not a valid email.', 'ctct');
             $this->errors[] = new WP_Error('datavalidation', $message, $email, $Validate);
             return;
         }
         if ($validation === NULL) {
             do_action('ctct_activity', 'DataValidation validation inconclusive.', $email, $Validate);
         } elseif ($validation === true) {
             do_action('ctct_activity', 'DataValidation validation passed.', $email, $Validate);
         }
     }
     // 5: SMTP validation
     if (in_array('smtp', $methods) && class_exists('SMTP_validateEmail')) {
         try {
             $SMTP_Validator = new SMTP_validateEmail();
             // Timeout after 1 second
             $SMTP_Validator->max_conn_time = 1;
             $SMTP_Validator->max_read_time = 1;
             $SMTP_Validator->debug = 0;
             // Prevent PHP notices about timeouts
             ob_start();
             $results = $SMTP_Validator->validate(array($email), get_option('admin_email'));
             ob_clean();
             if (isset($results[$email])) {
                 // True = passed
                 if ($results[$email]) {
                     do_action('ctct_activity', 'SMTP validation passed.', $email, $results);
                     return true;
                 } else {
                     do_action('ctct_activity', 'SMTP validation failed.', $email, $results);
                     $this->errors[] = new WP_Error('smtp', __('Email validation failed.', 'ctct'), $email, $results);
                     return false;
                 }
             } else {
                 do_action('ctct_activity', 'SMTP validation did not work', 'Returned empty results. Maybe it timed out?');
                 return true;
             }
         } catch (Exception $e) {
             do_action('ctct_error', 'SMTP validation broke.', $e);
             return;
         }
     }
     return true;
 }
 function processAjax()
 {
     global $wpdb;
     // this is how you get access to the database
     // Remove the cache for this whole joint
     add_filter('ctct_cache', '__return_false');
     $id = (int) @$_REQUEST['id'];
     $component = esc_html(@$_REQUEST['component']);
     $field = esc_attr(@$_REQUEST['field']);
     $value = @$_REQUEST['value'];
     $value = is_array($value) ? $value : esc_attr($value);
     $parent = esc_attr(@$_REQUEST['parent']);
     $parent = !empty($parent) ? $parent . '_' : NULL;
     if (!isset($_REQUEST['_wpnonce']) || isset($_REQUEST['_wpnonce']) && !wp_verify_nonce($_REQUEST['_wpnonce'], 'ctct') && !defined('DOING_AJAX')) {
         $response['errors'] = __('You\'re not authorized to be here.', 'ctct');
     } elseif (empty($field)) {
         $response['errors'] = __('There is no field defined.', 'ctct');
     } elseif (!isset($_REQUEST['value'])) {
         $response['errors'] = __('There is no value defined.', 'ctct');
     } else {
         $KWSConstantContact = new KWSConstantContact();
         switch ($component) {
             case 'Contact':
                 try {
                     $KWSContact = new KWSContact($KWSConstantContact->getContact(CTCT_ACCESS_TOKEN, $id));
                     // Did anything change?
                     // Check unformattet, then formatted.
                     $nothingChanged = $value === $KWSContact->get($parent . $field) || $value === $KWSContact->get($parent . $field, true);
                     // Lists need to be handled slightly differently.
                     if ($parent . $field === 'lists') {
                         // Get the lists for the contact
                         $existingLists = $KWSContact->get($parent . $field, true);
                         $items = $value;
                         $value = array();
                         foreach ($items as $key => $item) {
                             $value[] = new KWSContactList(array('id' => $item['value']));
                             $compareLists[] = $item['value'];
                         }
                         // If nothing changed, the arrays should be the same
                         // and the diff should be empty
                         $diff = kws_array_diff($existingLists, $compareLists);
                         $nothingChanged = empty($diff);
                     }
                     if ($nothingChanged) {
                         $response['message'] = __('Nothing changed.', 'ctct');
                         $response['code'] = 204;
                     } else {
                         $updatable = $KWSContact->set($parent . $field, $value);
                         if (!$updatable) {
                             $response['message'] = __('This field is not updatable.', 'ctct');
                             $response['code'] = 400;
                         } else {
                             $fetch = $KWSConstantContact->updateContact(CTCT_ACCESS_TOKEN, $KWSContact);
                             $response['message'] = __('Successfully updated.', 'ctct');
                             $response['code'] = 200;
                             delete_transient('ctct_all_contacts');
                             /**
                              * Set this so that next time the user refreshes the contact page,
                              * CTCT_Admin_Contacts::single() will catch it and force refresh.
                              *
                              * @see CTCT_Admin_Contacts::single()
                              */
                             add_option('ctct_refresh_contact_' . $KWSContact->get('id'), 1);
                         }
                     }
                 } catch (Exception $e) {
                     $response['message'] = $e->getErrors();
                     $response['code'] = 400;
                 }
                 break;
             case 'ContactList':
                 try {
                     $KWSList = new KWSContactList($KWSConstantContact->getList(CTCT_ACCESS_TOKEN, $id));
                     if ($value === $KWSList->get($field)) {
                         $response['message'] = __('Nothing changed.', 'ctct');
                         $response['code'] = 204;
                     } else {
                         $updatable = $KWSList->set($field, $value);
                         if (!$updatable) {
                             $response['message'] = __('This field is not updatable.', 'ctct');
                             $response['code'] = 400;
                         } else {
                             $fetch = $KWSConstantContact->updateList(CTCT_ACCESS_TOKEN, $KWSList);
                             $response['message'] = __('Successfully updated.', 'ctct');
                             $response['code'] = 200;
                             delete_transient('ctct_all_lists');
                         }
                     }
                 } catch (Exception $e) {
                     $response['message'] = $e->getErrors();
                     $response['code'] = 400;
                 }
                 break;
             default:
                 $response['message'] = __('There is no component defined.', 'ctct');
                 $response['code'] = 400;
                 break;
         }
     }
     wp_die(json_encode($response));
 }
 protected function single()
 {
     $id = $this->id;
     if (empty($id)) {
         esc_html_e('You have not specified a Contact to view', 'ctct');
         return;
     }
     if ($refresh = get_option('ctct_refresh_contact_' . $id)) {
         delete_option('ctct_refresh_contact_' . $id);
         add_filter('ctct_cache', '__return_false');
     }
     $Contact = $this->cc->getContact(CTCT_ACCESS_TOKEN, $id);
     // The fetching of the contact failed.
     if (is_null($Contact->id)) {
         return;
     }
     $Contact = new KWSContact($Contact);
     $summary = $this->cc->getContactSummaryReport(CTCT_ACCESS_TOKEN, $Contact->get('id'));
     include CTCT_DIR_PATH . 'views/admin/view.contact-view.php';
 }
    /**
     * Hook into show_user_profile action to display our user subscription settings if necessary
     *
     * @global  $cc
     *
     * @param <type> $user
     *
     * @return <type>
     */
    function display($user)
    {
        $Contact = new KWSContact($this->cc->getContactByEmail($user->data->user_email));
        if ($Contact && current_user_can('edit_users') && !isset($_GET['debug-user-display'])) {
            echo sprintf(__('
				<p><img src="%s" width="225" height="33" alt="Constant Contact" class="block" /><a href="%s">Admin-Only: Edit this User\'s Details</a> %s</p>
			', 'ctct'), plugins_url('images/admin/logo-horizontal.png', CTCT_FILE), admin_url('admin.php?page=constant-contact-contacts&amp;edit=' . $Contact->id), constant_contact_tip(__('Users will not see this link or the Constant Contact logo.', 'ctct'), false));
        }
        if (!$this->subscribe_method) {
            return;
        }
        $register_page_method = CTCT_Settings::get('profile_page_form');
        // Prepare the description from the settings screen
        $signup_description = CTCT_Settings::get('signup_description');
        if ($signup_description) {
            $signup_description = wpautop($signup_description);
            $signup_description = "<div class='description'>{$signup_description}</div>";
        }
        ?>
		<h3><?php 
        echo CTCT_Settings::get('signup_title');
        ?>
</h3>
		<?php 
        echo $signup_description;
        ?>

		<p><?php 
        $lists = (array) $Contact->get('lists', true);
        echo KWSContactList::outputHTML('all', array('checked' => $lists));
        ?>
</p>
		<br/>
	<?php 
    }
 /**
  * Add a contact if it doesn't exist and update it if it does.
  * @param array|KWSContact $data Array of contact data or an existing KWSContact or Contact object.
  * @return  boolean|KWSContact Returns false if failed, otherwise returns a contact object.
  */
 function addUpdateContact($data)
 {
     $contact = new KWSContact($data);
     // check to see if a contact with the email addess already exists
     $existingContact = $this->getContactByEmail($contact->get('email'));
     // create a new contact if one does not exist
     if (empty($existingContact)) {
         $action = "Creating Contact";
         try {
             $returnContact = $this->addContact(CTCT_ACCESS_TOKEN, $contact, true);
             if (is_wp_error($returnContact)) {
                 $action .= ' Failed';
                 /** @var WP_Error $returnContact */
                 do_action('ctct_error', 'Creating Contact Exception', $returnContact->get_error_message());
             } else {
                 $action .= ' Succeeded';
             }
         } catch (Exception $e) {
             $returnContact = false;
             $action .= ' Failed';
             do_action('ctct_error', 'Creating Contact Exception', $e->getMessage());
         }
         // update the existing contact if address already existed
     } else {
         $action = "Updating Contact";
         try {
             if ($existingContact->get('status') === 'OPTOUT') {
                 $action .= ' Failed';
                 do_action('ctct_error', 'The contact has opted out; cannot add or update.', $existingContact);
                 $returnContact = new WP_Error('optout', __('You have opted out of our newsletters and cannot re-subscribe.'));
             } else {
                 // Update the contact details
                 $modifiedContact = $existingContact->update($data);
                 $returnContact = $this->updateContact(CTCT_ACCESS_TOKEN, $modifiedContact);
                 $action .= ' Succeeded';
                 unset($modifiedContact);
             }
         } catch (Exception $e) {
             $returnContact = false;
             $action .= ' Failed';
             do_action('ctct_error', 'Updating Contact Exception', $e);
         }
     }
     do_action('ctct_activity', $action, $returnContact);
     return $returnContact;
 }
    $Admin_Contacts = new CTCT_Admin_Contacts();
    $alt = empty($alt) ? 'class="alt"' : '';
    ?>
        <tr <?php 
    echo $alt;
    ?>
>
            <td class="email column-email">
                <a href="<?php 
    echo esc_url(add_query_arg(array('page' => $Admin_Contacts->getKey(), 'view' => $Contact->id), admin_url('admin.php')));
    ?>
" title="<?php 
    _e('View Contact', 'ctct');
    ?>
"><?php 
    echo $Contact->get('email_address');
    ?>
</a>
            </td>
            <td class="column-name">
                <?php 
    echo $Contact->get('name');
    ?>
            </td>
            <td class="column-status">
                <?php 
    echo $Contact->get('status');
    ?>
            </td>
            <td class="column-edit">
            	<div class="button-group">