public function handle_submission()
 {
     $double_opt_in = mymail_option('double_opt_in');
     $baselink = get_permalink(mymail_option('homepage'));
     if (!$baselink) {
         $baselink = site_url();
     }
     $referer = isset($_POST['_referer']) ? $_POST['_referer'] : $baselink;
     $forms = mymail_option('forms');
     $form_id = isset($forms[$_POST['formid']]) ? intval($_POST['formid']) : 0;
     $form = $forms[$form_id];
     $userdata = array();
     $customfields = mymail_option('custom_field');
     foreach ($form['order'] as $field) {
         $userdata[$field] = isset($_POST['userdata'][$field]) ? esc_attr($_POST['userdata'][$field]) : '';
         if ($field == 'email' && !mymail_is_email(trim($userdata[$field])) || !$userdata[$field] && in_array($field, $form['required'])) {
             $this->errors[$field] = mymail_text($field, isset($customfields[$field]['name']) ? $customfields[$field]['name'] : $field);
         }
     }
     $userdata['email'] = trim($userdata['email']);
     $this->values = $userdata;
     if (mymail_option('track_users')) {
         $userdata['_meta'] = array('ip' => mymail_get_ip(), 'signupip' => mymail_get_ip(), 'signuptime' => current_time('timestamp'));
     }
     $this->lists = isset($form['userschoice']) ? isset($_POST['lists']) ? (array) $_POST['lists'] : array() : $form['lists'];
     $this->errors = apply_filters('mymail_submit_errors', $this->errors);
     $this->lists = apply_filters('mymail_submit_lists', $this->lists);
     $userdata = apply_filters('mymail_submit_userdata', $userdata);
     if (empty($this->lists)) {
         $this->errors['lists'] = __('Select at least one list', 'mymail');
     }
     if ($this->valid()) {
         $email = $userdata['email'];
         if ($double_opt_in) {
             //send confirmation email
             global $mymail_subscriber;
             if ($e = $mymail_subscriber->send_confirmation($baselink, $email, $userdata, $this->lists, false, isset($form['template']) ? $form['template'] : 'notification.html')) {
                 $target = add_query_arg(array('confirm' => ''), $baselink);
             } else {
                 //error
             }
         } else {
             global $mymail_subscriber;
             unset($userdata['email']);
             //subscribe user
             if ($mymail_subscriber->insert($email, 'subscribed', $userdata, $this->lists)) {
                 $target = add_query_arg(array('subscribe' => ''), $baselink);
             } else {
                 //error
             }
         }
         //redirect if no ajax request oder extern
         if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || isset($_POST['_extern'])) {
             $target = !empty($form['redirect']) ? $form['redirect'] : add_query_arg(array('mymail_success' => $double_opt_in + 1, 'id' => $form_id, 'extern' => isset($_POST['_extern'])), $referer);
             wp_redirect(apply_filters('mymail_subscribe_target', $target, $form_id));
         } else {
             $return = array('html' => '<p>' . ($double_opt_in ? mymail_text('confirmation') : mymail_text('success')) . '</p>');
             if (!empty($form['redirect'])) {
                 $return = wp_parse_args(array('redirect' => $form['redirect']), $return);
             }
             return $return;
         }
         return $target;
     } else {
         //redirect if no ajax request oder extern
         if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || isset($_POST['_extern'])) {
             $save = array('values' => $this->values, 'errors' => $this->errors, 'lists' => $this->lists);
             $hash = md5(serialize($save));
             set_transient('mymail_error_' . $hash, $save);
             $target = add_query_arg(array('mymail_error' => $hash, 'id' => $form_id, 'extern' => isset($_POST['_extern'])), $referer);
             wp_redirect($target);
         }
         return array('error' => true, 'fields' => $this->errors, 'html' => $this->get_error_html());
     }
 }
		</tr>
		<?php 
if ($geoip && is_file(mymail_option('countries_db'))) {
    ?>
		<tr valign="top">
			<th scope="row"></th>
			<td>
			<p class="description"><?php 
    _e('If you don\'t find your country down below the geo database is missing or corrupt', 'mymail');
    ?>
</p>
			<strong><?php 
    _e('Your IP', 'mymail');
    ?>
:</strong> <?php 
    echo mymail_get_ip();
    ?>
<br>
			<strong><?php 
    _e('Your country', 'mymail');
    ?>
:</strong> <?php 
    echo mymail_ip2Country('', 'name');
    ?>
<br>&nbsp;&nbsp;<strong><?php 
    _e('Last update', 'mymail');
    ?>
: <?php 
    echo date(get_option('date_format') . ' ' . get_option('time_format'), get_option('mymail_countries') + get_option('gmt_offset') * 3600);
    ?>
 </strong><br>
 public function handleReferrer()
 {
     //user preg_replace to remove unwanted whitespaces
     $target = isset($_REQUEST['t']) ? preg_replace('/\\s+/', '', $_REQUEST['t']) : NULL;
     $hash = isset($_REQUEST['k']) ? preg_replace('/\\s+/', '', $_REQUEST['k']) : NULL;
     $count = isset($_REQUEST['c']) ? intval($_REQUEST['c']) : NULL;
     $campaign_id = intval($_REQUEST['mymail']);
     if (!$campaign_id) {
         return;
     }
     //get user information
     $user = new WP_query(array('post_type' => 'subscriber', 'name' => $hash, 'posts_per_page' => 1));
     //user exists
     if ($user->post) {
         wp_cache_delete($user->post->ID, 'post' . '_meta');
         $user_campaign_data = get_post_meta($user->post->ID, 'mymail-campaigns', true);
         //update latest IP address
         if (mymail_option('track_users')) {
             $user_data = get_post_meta($user->post->ID, 'mymail-userdata', true);
             $user_data['_meta']['ip'] = mymail_get_ip();
         }
         //user has recieved this campaign
         if (isset($user_campaign_data[$campaign_id])) {
             //register clicks for campaign
             wp_cache_delete($campaign_id, 'post' . '_meta');
             $campaign_data = get_post_meta($campaign_id, 'mymail-campaign', true);
             $is_autoresponder = 'autoresponder' == get_post_status($campaign_id);
             //if users is here it must be openend
             if (!isset($user_campaign_data[$campaign_id]['open'])) {
                 $campaign_data['opens']++;
                 $user_campaign_data[$campaign_id]['open'] = current_time('timestamp');
                 //save user country
                 $country = mymail_ip2Country();
                 if (!isset($campaign_data['countries'][$country])) {
                     $campaign_data['countries'][$country] = 0;
                 }
                 $campaign_data['countries'][$country]++;
                 if (mymail_option('trackcities')) {
                     //save user city
                     $city = mymail_ip2City();
                     if ($city == 'unknown') {
                         $city = (object) array('country_code' => '', 'city' => '');
                     }
                     if (empty($city->country_code)) {
                         $city->country_code = $country;
                     }
                     if (empty($city->country_code)) {
                         $city->country_code = 'unknown';
                     }
                     if (!empty($city->country_code)) {
                         if (!isset($campaign_data['cities'][$city->country_code])) {
                             $campaign_data['cities'][$city->country_code] = array();
                         }
                         if (empty($city->city)) {
                             $city->city = 'unknown';
                             $city->latitude = 0;
                             $city->longitude = 0;
                         }
                         if (!isset($campaign_data['cities'][$city->country_code][$city->city])) {
                             $campaign_data['cities'][$city->country_code][$city->city] = array('lat' => $city->latitude, 'lng' => $city->longitude, 'opens' => 0);
                         }
                         $campaign_data['cities'][$city->country_code][$city->city]['opens']++;
                     }
                 }
             }
             $redirect_to = false;
             //target => link clicked
             if ($target) {
                 //unsubscribe ?
                 if (strpos($target, 'unsubscribe=' . md5($campaign_id . '_unsubscribe'))) {
                     $unsubscribe = true;
                     //change target for tracking
                     $target = add_query_arg('unsubscribe', '', html_entity_decode($target));
                 }
                 $redirect_to = html_entity_decode($target);
                 $target = apply_filters('mymail_click_target', $target, $campaign_id);
                 //if users first click
                 if (!isset($user_campaign_data[$campaign_id]['clicks'])) {
                     //increase unique clicks
                     $campaign_data['totaluniqueclicks']++;
                     $user_campaign_data[$campaign_id]['firstclick'] = current_time('timestamp');
                     global $mymail_subscriber;
                     $mymail_subscriber->add_new('click');
                 }
                 //increase target clicks
                 if (!isset($campaign_data['clicks'][$target])) {
                     $campaign_data['clicks'][$target] = array();
                 }
                 if (!isset($campaign_data['clicks'][$target][$count])) {
                     $campaign_data['clicks'][$target][$count] = 0;
                 }
                 $campaign_data['clicks'][$target][$count]++;
                 //increase total clicks
                 $campaign_data['totalclicks']++;
                 //register user click
                 if (!isset($user_campaign_data[$campaign_id]['totalclicks'])) {
                     $user_campaign_data[$campaign_id]['totalclicks'] = 0;
                 }
                 $user_campaign_data[$campaign_id]['totalclicks']++;
                 //increase target clicks
                 if (!isset($user_campaign_data[$campaign_id]['clicks'][$target])) {
                     $user_campaign_data[$campaign_id]['clicks'][$target] = 0;
                     //increase unique clicks
                     if (!isset($user_campaign_data[$campaign_id]['totaluniqueclicks'])) {
                         $user_campaign_data[$campaign_id]['totaluniqueclicks'] = 0;
                     }
                     $user_campaign_data[$campaign_id]['totaluniqueclicks']++;
                 }
                 $user_campaign_data[$campaign_id]['clicks'][$target]++;
             }
             //save
             $this->post_meta($user->post->ID, 'mymail-campaigns', $user_campaign_data);
             if (isset($user_data)) {
                 $this->post_meta($user->post->ID, 'mymail-userdata', $user_data);
             }
             $this->post_meta($campaign_id, 'mymail-campaign', $campaign_data);
         }
     }
     //no target => tracking image
     if (!$redirect_to) {
         header('Content-type: image/gif');
         # The transparent, beacon image
         echo chr(71) . chr(73) . chr(70) . chr(56) . chr(57) . chr(97) . chr(1) . chr(0) . chr(1) . chr(0) . chr(128) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(33) . chr(249) . chr(4) . chr(1) . chr(0) . chr(0) . chr(0) . chr(0) . chr(44) . chr(0) . chr(0) . chr(0) . chr(0) . chr(1) . chr(0) . chr(1) . chr(0) . chr(0) . chr(2) . chr(2) . chr(68) . chr(1) . chr(0) . chr(59);
         die;
     } else {
         //redirect in any case
         isset($unsubscribe) && $unsubscribe ? wp_redirect(mymail_option('homepage') ? add_query_arg(array('k' => $campaign_id, 'unsubscribe' => $hash), get_permalink(mymail_option('homepage'))) : get_bloginfo('url')) : wp_redirect($redirect_to, 301);
         exit;
     }
 }
function mymail_ip2City($ip = '', $get = NULL)
{
    if (!mymail_option('trackcities')) {
        return 'unknown';
    }
    if (empty($ip)) {
        $ip = mymail_get_ip();
    }
    require_once MYMAIL_DIR . '/classes/libs/Ip2City.php';
    $i = new Ip2City();
    $code = $i->get($ip, $get);
    return $code ? $code : 'unknown';
}
 public function user_register($id)
 {
     //not from new user page
     if (!isset($_POST['_wpnonce_create-user']) || !wp_verify_nonce($_POST['_wpnonce_create-user'], 'create-user')) {
         return false;
     }
     $lists = mymail_option('register_other_lists');
     if (empty($lists)) {
         return false;
     }
     $user = get_userdata($id);
     $email = $user->data->user_email;
     $subscriber_exists = $this->get_by_mail($email);
     if ($subscriber_exists && !mymail_option('update_subscribers')) {
         return false;
     }
     $first_name = get_user_meta($id, 'first_name', true);
     $last_name = get_user_meta($id, 'last_name', true);
     if (!$first_name) {
         $first_name = $user->data->display_name;
     }
     $double_opt_in = mymail_option('double_opt_in');
     if ($double_opt_in && !$subscriber_exists && mymail_option('register_other_confirmation')) {
         $baselink = get_permalink(mymail_option('homepage'));
         if (!$baselink) {
             $baselink = site_url();
         }
         $res = $this->send_confirmation($baselink, $email, array('firstname' => $first_name, 'lastname' => $last_name, '_meta' => array('ip' => mymail_get_ip(), 'signupip' => mymail_get_ip(), 'signuptime' => current_time('timestamp'))), $lists);
     } else {
         $this->insert($user->user_email, 'subscribed', array('firstname' => $first_name, 'lastname' => $last_name, '_meta' => array('ip' => mymail_get_ip(), 'signupip' => mymail_get_ip(), 'signuptime' => current_time('timestamp'), 'confirmip' => mymail_get_ip(), 'confirmtime' => current_time('timestamp'))), $lists, true);
     }
 }