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());
     }
 }
 public function ajax_do_import()
 {
     define('MYMAIL_DO_BULKIMPORT', true);
     $safe_mode = @ini_get('safe_mode');
     $memory_limit = @ini_get('memory_limit');
     $max_execution_time = @ini_get('max_execution_time');
     if (!$safe_mode) {
         @set_time_limit(0);
         if (intval($max_execution_time) < 300) {
             @ini_set('max_execution_time', 300);
         }
         if (intval($memory_limit) < 256) {
             @ini_set('memory_limit', '256M');
         }
     }
     global $mymail_subscriber, $wpdb;
     $return['success'] = false;
     $this->ajax_nonce(json_encode($return));
     if (!current_user_can('mymail_import_subscribers')) {
         echo json_encode($return);
         exit;
     }
     $bulkdata = get_option('mymail_bulk_import');
     $bulkdata = wp_parse_args($_POST['options'], get_option('mymail_bulk_import'));
     //$bulkdata = wp_parse_args(get_option( 'mymail_bulk_import' ), $_POST['options']);
     $bulkdata['autoresponder'] = !!($bulkdata['autoresponder'] === 'true');
     $bulkdata['existing'] = esc_attr($bulkdata['existing']);
     $bulkdata['status'] = $bulkdata['status'] == 'subscribed' ? $bulkdata['status'] : 'unsubscribed';
     $allmails = $wpdb->get_results("SELECT post_title, ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'subscriber' ", OBJECT_K);
     parse_str($bulkdata['order']);
     parse_str($bulkdata['lists']);
     foreach ((array) $lists as $term) {
         if (!strlen(trim($term))) {
             continue;
         }
         if (!($term_info = term_exists($term, 'newsletter_lists'))) {
             if (is_int($term)) {
                 continue;
             }
             $term_info = wp_insert_term($term, 'newsletter_lists');
         }
         if (is_wp_error($term_info)) {
             echo json_encode($return);
             exit;
         }
     }
     $bulkdata['current'] = intval($_POST['id']);
     $list = unserialize(base64_decode(get_option('mymail_bulk_' . $bulkdata['current'])));
     if ($list) {
         foreach ($list as $line) {
             if (!trim($line)) {
                 $bulkdata['lines']--;
                 continue;
             }
             @set_time_limit(10);
             $data = explode($bulkdata['separator'], $line);
             $userdata = array();
             $userlists = $lists;
             for ($i = 0; $i < count($data); $i++) {
                 switch ($order[$i]) {
                     case 'email':
                         $email = trim($data[$i]);
                         break;
                     case '_ip':
                         if (!isset($userdata['_meta'])) {
                             $userdata['_meta'] = array();
                         }
                         $userdata['_meta']['ip'] = $userdata['_meta']['signupip'] = $userdata['_meta']['confirmip'] = trim($data[$i]);
                         break;
                     case '_signupdate':
                         if (!isset($userdata['_meta'])) {
                             $userdata['_meta'] = array();
                         }
                         $time = trim($data[$i]);
                         if (!is_numeric($time)) {
                             $time = strtotime($time);
                         }
                         $userdata['_meta']['confirmtime'] = $userdata['_meta']['signuptime'] = $time;
                         break;
                     case '-1':
                         break;
                     default:
                         $userdata[$order[$i]] = trim($data[$i]);
                 }
             }
             $email = trim(strtolower($email));
             if (!mymail_is_email($email)) {
                 $bulkdata['errormails'][$email] = __('invalid email address', 'mymail');
                 $bulkdata['errors']++;
                 continue;
             }
             if ($bulkdata['existing'] == 'skip') {
                 if (isset($allmails[$email])) {
                     $bulkdata['errormails'][$email] = sprintf(__('user already exists (ID: %d)', 'mymail'), $allmails[$email]->ID);
                     $bulkdata['errors']++;
                     continue;
                 }
             }
             $ID = isset($allmails[$email]) ? $allmails[$email]->ID : NULL;
             if ($ID && $bulkdata['existing'] == 'merge') {
                 $oldlists = wp_get_object_terms($ID, 'newsletter_lists', array('fields' => 'slugs', 'orderby' => 'none'));
                 $userlists = array_unique(array_merge($userlists, $oldlists));
                 $olduserdata = get_post_meta($ID, 'mymail-userdata', true);
                 $oldusermeta = isset($olduserdata['_meta']) ? $olduserdata['_meta'] : array();
                 $usermeta = wp_parse_args($userdata['_meta'], $oldusermeta);
                 $userdata = wp_parse_args($userdata, $olduserdata);
                 $userdata['_meta'] = $usermeta;
             }
             $result = $mymail_subscriber->rawinsert($ID, $email, $bulkdata['status'], $userdata, $userlists, $bulkdata['autoresponder']);
             if (is_wp_error($result)) {
                 $bulkdata['errormails'][$email] = $result->get_error_message();
                 $bulkdata['errors']++;
             } else {
                 $allmails[$email] = (object) array('post_title' => $email, 'ID' => $result);
                 $bulkdata['imported']++;
             }
         }
         delete_option('mymail_bulk_' . $bulkdata['current']);
     }
     $mymail_subscriber->trigger_update_post_term_count();
     $bulkdata['memoryusage'] = size_format(memory_get_peak_usage(true), 2);
     $return['html'] = '';
     if ($bulkdata['imported'] + $bulkdata['errors'] >= $bulkdata['lines']) {
         $return['html'] .= '<p>' . sprintf(__('%1$d of %2$d contacts imported', 'mymail'), $bulkdata['imported'], $bulkdata['lines']) . '<p>';
         if ($bulkdata['errors']) {
             $i = 0;
             $table = '<p>' . __('The following addresses were not imported', 'mymail') . ':</p>';
             $table .= '<table class="wp-list-table widefat fixed">';
             $table .= '<thead><tr><td width="5%">#</td><td>' . mymail_text('email') . '</td><td>' . __('Reason', 'mymail') . '</td></tr></thead><tbody>';
             foreach ($bulkdata['errormails'] as $email => $e) {
                 $table .= '<tr' . ($i % 2 ? '' : ' class="alternate"') . '><td>' . ++$i . '</td><td>' . $email . '</td><td>' . $e . '</td></tr></thead>';
             }
             $table .= '</tbody></table>';
             $return['html'] .= $table;
         }
         delete_option('mymail_bulk_import');
         mymail_clear_totals();
     } else {
         update_option('mymail_bulk_import', $bulkdata);
     }
     $return['data'] = $bulkdata;
     $return['success'] = true;
     echo json_encode($return);
     exit;
 }
 public function verify($options)
 {
     global $mymail;
     if (isset($_POST['mymail_generate_dkim_keys'])) {
         try {
             $res = openssl_pkey_new(array('private_key_bits' => isset($options['dkim_bitsize']) ? (int) $options['dkim_bitsize'] : 512));
             openssl_pkey_export($res, $dkim_private_key);
             $dkim_public_key = openssl_pkey_get_details($res);
             $dkim_public_key = $dkim_public_key["key"];
             $options['dkim_public_key'] = $dkim_public_key;
             $options['dkim_private_key'] = $dkim_private_key;
             add_settings_error('mymail_options', 'mymail_options', __('New DKIM keys have been created!', 'mymail'), 'updated');
         } catch (Exception $e) {
             add_settings_error('mymail_options', 'mymail_options', __('Not able to create new DKIM keys!', 'mymail'));
         }
     }
     if (!empty($_FILES['country_db_file']['name'])) {
         $file = $_FILES['country_db_file'];
         $dest = MYMAIL_UPLOAD_DIR . '/' . $file['name'];
         if (move_uploaded_file($file['tmp_name'], $dest)) {
             if (is_file($dest)) {
                 $options['countries_db'] = $dest;
                 add_settings_error('mymail_options', 'mymail_options', sprintf(__('File uploaded to %s', 'mymail'), '"' . $dest . '"'), 'updated');
             } else {
                 $options['countries_db'] = '';
             }
         } else {
             add_settings_error('mymail_options', 'mymail_options', __('unable to upload file', 'mymail'));
             $options['countries_db'] = '';
         }
     }
     if (!empty($_FILES['city_db_file']['name'])) {
         $file = $_FILES['city_db_file'];
         $dest = MYMAIL_UPLOAD_DIR . '/' . $file['name'];
         if (move_uploaded_file($file['tmp_name'], $dest)) {
             if (is_file($dest)) {
                 $options['cities_db'] = $dest;
                 add_settings_error('mymail_options', 'mymail_options', sprintf(__('File uploaded to %s', 'mymail'), '"' . $dest . '"'), 'updated');
             } else {
                 $options['cities_db'] = '';
             }
         } else {
             add_settings_error('mymail_options', 'mymail_options', __('unable to upload file', 'mymail'));
             $options['cities_db'] = '';
         }
     }
     $verify = array('from', 'reply_to', 'homepage', 'trackcountries', 'trackcities', 'vcard_content', 'custom_field', 'forms', 'form_css', 'send_period', 'bounce', 'cron_service', 'cron_secret', 'interval', 'roles', 'tweet_cache_time', 'deliverymethod', 'dkim_domain', 'dkim_selector', 'dkim_identity', 'dkim_passphrase', 'dkim_private_key', 'purchasecode');
     if (isset($_POST['mymail_import_settings']) && $_POST['mymail_import_settings']) {
         $settings = unserialize(base64_decode($_POST['mymail_import_settings']));
         $options = wp_parse_args($settings, $options);
     }
     foreach ($verify as $id) {
         if (!isset($options[$id])) {
             continue;
         }
         $value = $options[$id];
         $old = mymail_option($id);
         switch ($id) {
             case 'from':
             case 'reply_to':
             case 'bounce':
                 if ($value && !mymail_is_email($value)) {
                     add_settings_error('mymail_options', 'mymail_options', sprintf(__('%s is not a valid email address', 'mymail'), '"' . $value . '"'));
                     $value = $old;
                 }
                 break;
             case 'trackcountries':
                 if (!$options['countries_db'] || !is_file($options['countries_db'])) {
                     add_settings_error('mymail_options', 'mymail_options', __('No country database found! Please load it!', 'mymail'));
                     $value = false;
                 }
                 break;
             case 'trackcities':
                 if (!$options['cities_db'] || !is_file($options['cities_db'])) {
                     add_settings_error('mymail_options', 'mymail_options', __('No city database found! Please load it!', 'mymail'));
                     $value = false;
                 }
                 break;
             case 'homepage':
                 if ($old != $value) {
                     mymail_remove_notice('no-homepage');
                 }
                 break;
             case 'interval':
                 if ($old != $value) {
                 }
                 break;
             case 'cron_service':
                 if ($old != $value) {
                     if ($value == 'wp_cron') {
                         if (!wp_next_scheduled('mymail_cron_worker')) {
                             wp_schedule_event(floor(time() / 300) * 300, 'mymail_cron_interval', 'mymail_cron_worker');
                         }
                     } else {
                         wp_clear_scheduled_hook('mymail_cron_worker');
                     }
                 }
                 break;
             case 'cron_secret':
                 if ($old != $value) {
                     if ($value == '') {
                         $value = md5(uniqid());
                     }
                 }
                 break;
             case 'vcard_content':
                 $folder = MYMAIL_UPLOAD_DIR;
                 if (empty($options['vcard_content'])) {
                     $options['vcard'] = false;
                 }
                 if (!is_dir($folder)) {
                     wp_mkdir_p($folder);
                 }
                 $options['vcard_filename'] = sanitize_file_name($options['vcard_filename']);
                 $filename = $folder . '/' . $options['vcard_filename'];
                 if (!empty($options['vcard'])) {
                     file_put_contents($filename, $options['vcard_content']);
                 } else {
                     if (file_exists($filename)) {
                         @unlink($filename);
                     }
                 }
                 break;
             case 'custom_field':
                 if (serialize($old) != serialize($value)) {
                 }
                 break;
             case 'forms':
                 if (function_exists('add_settings_error')) {
                     foreach ($value as $form) {
                         if (!isset($form['lists']) || empty($form['lists'])) {
                             add_settings_error('mymail_options', 'mymail_options', sprintf(__('Form %s has no assigned lists', 'mymail'), '"' . $form['name'] . '"'));
                         }
                     }
                 }
                 if (serialize($old) != serialize($value)) {
                 }
                 break;
             case 'form_css':
                 if (isset($_POST['mymail_reset_form_css'])) {
                     require_once MYMAIL_DIR . '/includes/static.php';
                     $value = $mymail_form_css;
                     add_settings_error('mymail_options', 'mymail_options', __('Form CSS reseted!', 'mymail'), 'updated');
                 }
                 if ($old != $value) {
                     delete_transient('mymail_form_css');
                     $value = str_replace(array('MYMAIL_URI'), array(MYMAIL_URI), $value);
                     $options['form_css_hash'] = md5(MYMAIL_VERSION . $value);
                 }
                 break;
             case 'send_period':
                 if ($old != $value) {
                     if ($timestamp = get_option('_transient_timeout__mymail_send_period_timeout')) {
                         $new = time() + $value * 3600;
                         update_option('_transient_timeout__mymail_send_period_timeout', $new);
                     } else {
                         update_option('_transient__mymail_send_period_timeout', false);
                     }
                     mymail_remove_notice('dailylimit');
                 }
                 break;
             case 'roles':
                 if (serialize($old) != serialize($value)) {
                     require_once MYMAIL_DIR . '/includes/capability.php';
                     global $wp_roles;
                     if (!$wp_roles) {
                         break;
                     }
                     $newvalue = array();
                     //give admin all rights
                     $value['administrator'] = array();
                     //foreach role
                     foreach ($value as $role => $capabilities) {
                         if (!isset($newvalue[$role])) {
                             $newvalue[$role] = array();
                         }
                         foreach ($mymail_capabilities as $capability => $data) {
                             if (in_array($capability, $capabilities) || 'administrator' == $role) {
                                 $wp_roles->add_cap($role, $capability);
                                 $newvalue[$role][] = $capability;
                             } else {
                                 $wp_roles->remove_cap($role, $capability);
                             }
                         }
                     }
                     $value = $newvalue;
                 }
                 break;
             case 'tweet_cache_time':
                 $value = (int) $value;
                 if ($value < 10) {
                     $value = 10;
                     add_settings_error('mymail_options', 'mymail_options', sprintf(__('The caching time for tweets must be at least %d minutes', 'mymail'), '10'));
                 }
                 break;
             case 'deliverymethod':
                 if ($old != $value) {
                     if ($value == 'gmail') {
                         if ($options['send_limit'] != 500) {
                             $options['send_limit'] = 500;
                             $options['send_period'] = 24;
                             update_option('_transient__mymail_send_period_timeout', false);
                             add_settings_error('mymail_options', 'mymail_options', sprintf(__('Send limit has been adjusted to %d for Gmail', 'mymail'), 500));
                         }
                     }
                 }
                 break;
             case 'dkim_domain':
             case 'dkim_selector':
             case 'dkim_identity':
                 if ($old != $value) {
                     $value = trim($value);
                 }
                 break;
             case 'dkim_private_key':
                 if ($old != $value) {
                     global $wp_filesystem;
                     if (!mymail_require_filesystem('', '', false)) {
                         break;
                     }
                     $folder = MYMAIL_UPLOAD_DIR . '/dkim';
                     //create folder
                     if (!is_dir($folder)) {
                         wp_mkdir_p($folder);
                         $wp_filesystem->put_contents($folder . '/index.php', '<?php //silence is golden ?>', FS_CHMOD_FILE);
                     }
                     //remove old
                     if (isset($options['dkim_private_hash']) && is_file($folder . '/' . $options['dkim_private_hash'] . '.pem')) {
                         $wp_filesystem->delete($folder . '/' . $options['dkim_private_hash'] . '.pem');
                     }
                     $hash = md5($value);
                     if ($wp_filesystem->put_contents($folder . '/' . $hash . '.pem', $value, FS_CHMOD_FILE)) {
                         $options['dkim_private_hash'] = $hash;
                     }
                 }
                 break;
             case 'purchasecode':
                 if ($old != $value && $value) {
                     if (preg_match('#^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$#', $value)) {
                         $envato_plugins = get_option('envato_plugins');
                         if (isset($envato_plugins[MYMAIL_SLUG])) {
                             $envato_plugins[MYMAIL_SLUG]->last_update = 0;
                             update_option('envato_plugins', $envato_plugins);
                         }
                     } else {
                         add_settings_error('mymail_options', 'mymail_options', sprintf(__('The provided purchasecode %s is invalid', 'mymail'), '"' . $value . '"'));
                         $value = '';
                     }
                 }
                 break;
         }
         $options[$id] = $value;
     }
     $options = apply_filters('mymail_verify_options', $options);
     //clear everything thats cached
     mymail_clear_cache();
     return $options;
 }
 public function cronjob($cron_used = false)
 {
     if (defined('DOING_AJAX') || defined('DOING_AUTOSAVE') || defined('WP_INSTALLING')) {
         return false;
     }
     //define a constant with the time so we can take a look
     define('MYMAIL_DOING_CRON', time());
     if (!$cron_used) {
         if (mymail_option('cron_service') != 'wp_cron') {
             $this->remove_crons();
             return false;
         } else {
             sleep(2);
         }
     }
     $safe_mode = @ini_get('safe_mode');
     $memory_limit = @ini_get('memory_limit');
     $max_execution_time = @ini_get('max_execution_time');
     //lockfile exists
     if (file_exists(MYMAIL_UPLOAD_DIR . '/CRON_LOCK')) {
         $age = time() - filemtime(MYMAIL_UPLOAD_DIR . '/CRON_LOCK');
         if ($age < 10) {
             echo "Cron is currently running!";
             return false;
         } else {
             if ($age < ini_get('max_execution_time')) {
                 mymail_notice('<strong>' . sprintf(__('It seems your last cronjob hasn\'t been finished! Increase the %1$s, add %2$s to your wp-config.php or reduce the %3$s in the settings', 'mymail'), "'max_execution_time'", '<code>define("WP_MEMORY_LIMIT", "256M");</code>', '<a href="options-general.php?page=newsletter-settings&settings-updated=true#delivery">' . __('Number of mails sent', 'mymail') . '</a>') . '</strong>', 'error', false, 'cron_unfinished');
                 die;
             } else {
             }
         }
     } else {
         file_put_contents(MYMAIL_UPLOAD_DIR . '/CRON_LOCK', time());
     }
     $this->check_bounces();
     if (!$safe_mode) {
         @set_time_limit(0);
         if (intval($max_execution_time) < 300) {
             @ini_set('max_execution_time', 300);
             $max_execution_time = @ini_get('max_execution_time');
         }
         if (intval($memory_limit) < 256) {
             @ini_set('memory_limit', '256M');
             $memory_limit = @ini_get('memory_limit');
         }
     }
     @ignore_user_abort(true);
     register_shutdown_function(array($this, 'finish_cron'));
     global $wpdb, $mymail_campaignID, $mymail_campaigndata;
     $query = "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'newsletter' AND {$wpdb->posts}.post_status IN ('active', 'queued') GROUP BY {$wpdb->posts}.ID ORDER BY {$wpdb->posts}.post_modified ASC";
     $campaigns = $wpdb->get_results($query);
     $campaign_count = count($campaigns);
     $campaign_active_count = 0;
     for ($i = 0; $i < $campaign_count; $i++) {
         if ($campaigns[$i]->post_status == 'active') {
             $campaign_active_count++;
         }
     }
     $mymail_campaignID = $mymail_campaigndata = array();
     $max_memory_usage = 0;
     //how many newsletter sent at once
     $send_at_once = mymail_option('send_at_once');
     $send_per_camp = $campaign_active_count && mymail_option('split_campaigns') ? ceil($send_at_once / $campaign_active_count) : $send_at_once;
     $max_bounces = mymail_option('bounce_attempts');
     $sent_this_turn = 0;
     $send_delay = intval(mymail_option('send_delay', 0)) * 1000;
     $bounces_only = true;
     $senderrors = array();
     $quit_cronjob = !$campaign_count;
     $unsubscribe_homepage = get_page(mymail_option('homepage')) ? get_permalink(mymail_option('homepage')) : get_bloginfo('url');
     $unsubscribe_homepage = apply_filters('mymail_unsubscribe_link', $unsubscribe_homepage);
     if ($memory_limit) {
         $this->cron_log('memory limit', '<strong>' . intval($memory_limit) . ' MB</strong>');
     }
     $this->cron_log('safe_mode', '<strong>' . ($safe_mode ? 'enabled' : 'disabled') . '</strong>');
     $this->cron_log('max_execution_time', '<strong>' . $max_execution_time . ' seconds</strong>');
     $this->cron_log('campaigns found', '<strong>' . $campaign_active_count . '</strong>');
     $this->cron_log('send max at once', '<strong>' . $send_at_once . '</strong>');
     $this->cron_log();
     for ($i = 0; $i < $campaign_count; $i++) {
         $time_start = microtime(true);
         $break_on_error = false;
         //current newsletter
         $campaign = $campaigns[$i];
         $campaign_permalink = get_permalink($campaign->ID);
         //get data from the newsletter
         $data = get_post_meta($campaign->ID, 'mymail-data', true);
         //if mail is less then an hour in the future and
         if (current_time('timestamp') - $data['timestamp'] > -3600) {
             $quit_cronjob = false;
         }
         ///allready sent, not active or in the future go to next
         if (current_time('timestamp') - $data['timestamp'] < 0) {
             continue;
         }
         //change post status to active (silence)
         if ($campaign->post_status == 'queued') {
             $this->change_status($campaign, 'active', true);
         }
         //to many this turn => break;
         if ($sent_this_turn >= $send_at_once) {
             break;
         }
         //get more data from the newsletter
         $campaigncount = count($mymail_campaigndata);
         $mymail_campaignID[] = $campaign->ID;
         $mymail_campaigndata[$campaigncount] = get_post_meta($campaign->ID, 'mymail-campaign', true);
         $lists = wp_get_post_terms($campaign->ID, 'newsletter_lists', array("fields" => "ids"));
         //baselink with trailing slash required for links to work in Outlook 2007
         $baselink = add_query_arg('mymail', $campaign->ID, home_url('/'));
         $unsubscribelink = add_query_arg('unsubscribe', md5($campaign->ID . '_unsubscribe'), $unsubscribe_homepage);
         $errors = $mymail_campaigndata[$campaigncount]['errors'];
         $totalerrors = isset($mymail_campaigndata[$campaigncount]['totalerrors']) ? $mymail_campaigndata[$campaigncount]['totalerrors'] : count($mymail_campaigndata[$campaigncount]['errors']);
         require_once MYMAIL_DIR . '/classes/mail.class.php';
         $mail = mymail_mail::get_instance();
         //stop if send limit is reached
         if ($mail->sentlimitreached) {
             break;
         }
         $mail->from = $data['from'];
         $mail->from_name = $data['from_name'];
         $mail->bouncemail = mymail_option('bounce');
         $mail->reply_to = $data['reply_to'];
         $mail->embed_images = $data['embed_images'];
         require_once MYMAIL_DIR . '/classes/placeholder.class.php';
         $placeholder = new mymail_placeholder($campaign->post_content);
         $placeholder->add(array('preheader' => $data['preheader'], 'subject' => $data['subject'], 'webversion' => '<a href="{webversionlink}">' . mymail_text('webversion') . '</a>', 'webversionlink' => $campaign_permalink, 'unsub' => '<a href="{unsublink}">' . mymail_text('unsubscribelink') . '</a>', 'unsublink' => $unsubscribelink, 'forward' => '<a href="{forwardlink}">' . mymail_text('forward') . '</a>', 'email' => '<a href="mailto:{emailaddress}">{emailaddress}</a>'));
         $placeholder->share_service($campaign_permalink, $campaign->post_title);
         $mail->content = $placeholder->get_content(false);
         $mail->baselink = $baselink;
         $mail->prepare_content();
         //store the base content temporary;
         $basecontent = $mail->content;
         //get all links from the basecontent
         preg_match_all('#href=(\'|")?(https?[^\'"\\#]+)(\'|")?#', $basecontent, $links);
         $links = $links[2];
         $totalsend = 0;
         $term_taxonomy_ids = wp_list_pluck(get_terms('newsletter_lists', array('fields' => 'all', 'include' => $lists)), 'term_taxonomy_id');
         //no subscribers
         if (empty($term_taxonomy_ids) || empty($lists)) {
             continue;
         }
         //get all users from all lists only once ordered by ID
         $query = "SELECT {$wpdb->posts}.ID, {$wpdb->posts}.post_name AS hash, {$wpdb->posts}.post_title AS email, {$wpdb->posts}.post_status as status, {$wpdb->postmeta}.meta_value as meta FROM {$wpdb->posts} INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = 'mymail-campaigns') WHERE ( {$wpdb->term_relationships}.term_taxonomy_id IN (" . implode(',', $term_taxonomy_ids) . ") ) AND {$wpdb->posts}.post_type = 'subscriber' AND ({$wpdb->posts}.post_status IN ('subscribed', 'unsubscribed')) GROUP BY {$wpdb->posts}.ID ORDER BY {$wpdb->posts}.ID ASC";
         $result = mysql_query($query, $wpdb->dbh);
         $subscribers_count = mysql_num_rows($result);
         $this->cron_log('Campaign', '<strong>' . $campaign->post_title . '</strong>');
         $this->cron_log('Subscribers found', '<strong>' . $subscribers_count . '</strong>');
         $this->cron_log('send max for camp', '<strong>' . $send_per_camp . '</strong>');
         $this->cron_log();
         if ($error = mysql_error($wpdb->dbh)) {
             die($error);
         }
         $subscribercounter = $totalsend;
         $campaign_send_counter = 0;
         //foreach subscribers
         while ($subscriber = @mysql_fetch_object($result)) {
             if (!$safe_mode) {
                 @set_time_limit(0);
             }
             touch(MYMAIL_UPLOAD_DIR . '/CRON_LOCK');
             if (connection_aborted()) {
                 break;
             }
             $subscribercounter++;
             //to many send for this campaign;
             if ($campaign_send_counter >= $send_per_camp) {
                 break;
             }
             if ($break_on_error) {
                 break;
             }
             //to many this turn => break;
             if ($sent_this_turn >= $send_at_once) {
                 break;
             }
             //stop if send limit is reached
             if ($mail->sentlimitreached) {
                 break;
             }
             if (!isset($subscriber)) {
                 break;
             }
             $usercampaigndata = $subscriber->meta ? unserialize($subscriber->meta) : array();
             //check if campaign was sent
             if ($usercampaigndata[$campaign->ID]['sent'] || $subscribercounter <= 0) {
                 //campaign was sent
                 $totalsend++;
                 continue;
             } else {
                 //stop if user isn't subscribed
                 if ($subscriber->status != 'subscribed') {
                     continue;
                 }
                 //not sent
                 //user reaches bouncelimit
                 if ($usercampaigndata[$campaign->ID]['bounces'] >= $max_bounces) {
                     continue;
                 }
                 $time_mail_start = microtime(true);
                 $userdata = get_post_meta($subscriber->ID, 'mymail-userdata', true);
                 if (!is_array($userdata)) {
                     $userdata = array();
                 }
                 $mail->to = $subscriber->email;
                 $mail->hash = $subscriber->hash;
                 $mail->subject = $data['subject'];
                 //restore from the base
                 $placeholder->set_content($basecontent);
                 //unset meta property if set
                 if (isset($userdata['_meta'])) {
                     unset($userdata['_meta']);
                 }
                 $placeholder->add(array_merge(array('fullname' => trim($userdata['firstname'] . ' ' . $userdata['lastname']), 'forwardlink' => add_query_arg('forward', $subscriber->email, $campaign_permalink)), array('emailaddress' => $subscriber->email), $userdata));
                 //replace links
                 $placeholder->replace_links($baselink, $links, $subscriber->hash);
                 $mail->content = $placeholder->get_content();
                 //placeholders in subject
                 $placeholder->set_content($data['subject']);
                 $mail->subject = $placeholder->get_content();
                 //set headers for bouncing
                 $mail->add_header('X-MyMail', $subscriber->hash);
                 $mail->add_header('X-MyMail-Campaign', $campaign->ID);
                 $mail->add_header('List-Unsubscribe', add_query_arg(array('k' => $campaign->ID, 'unsubscribe' => $subscriber->hash), $unsubscribelink));
                 try {
                     $success = $mail->send();
                 } catch (Exception $e) {
                     $success = false;
                 }
                 //send mail
                 if ($success) {
                     //mark as send and increase total with 1
                     $usercampaigndata[$campaign->ID]['sent'] = true;
                     $usercampaigndata[$campaign->ID]['timestamp'] = current_time('timestamp');
                     $this->post_meta($subscriber->ID, 'mymail-campaigns', $usercampaigndata);
                     if (!isset($usercampaigndata[$campaign->ID]['bounces'])) {
                         $bounces_only = false;
                     }
                     //campaign was sent
                     $totalsend++;
                     $sent_this_turn++;
                     $campaign_send_counter++;
                     $this->cron_log('#' . $subscribercounter . ' <strong>' . $subscriber->email . '</strong> sent', 'try ' . ($usercampaigndata[$campaign->ID]['bounces'] + 1) . '.', microtime(true) - $time_mail_start);
                 } else {
                     $e_array = $mail->get_errors('array');
                     $errormsg = trim($e_array[count($e_array) - 1]);
                     if (!$errormsg) {
                         if (!$campaign->post_content) {
                             $errormsg = 'no content';
                         } else {
                             $errormsg = '';
                         }
                     }
                     $subscriber_errors = apply_filters('mymail_subscriber_errors', array('SMTP Error: The following recipients failed', 'The following From address failed', 'Invalid address:', 'SMTP Error: Data not accepted'));
                     $is_subscriber_error = !mymail_is_email($subscriber->email);
                     //check for subscriber error
                     foreach ($subscriber_errors as $subscriber_error) {
                         if (stripos($errormsg, $subscriber_error) !== false) {
                             $is_subscriber_error = true;
                             break;
                         }
                     }
                     //caused by the subscriber
                     if ($is_subscriber_error) {
                         $this->cron_log('#' . $subscribercounter . ' <strong>' . $subscriber->email . '</strong> <span style="color:#f33">not sent</span>', '<br><span style="color:#f33">' . $errormsg . '</span>', microtime(true) - $time_mail_start);
                         $usercampaigndata[$campaign->ID]['sent'] = false;
                         //change status
                         $this->change_status(get_post($subscriber->ID), 'error');
                         $errors[$subscriber->email] = $errormsg;
                         $totalerrors++;
                         do_action('mymail_subscriber_error', $campaign, $subscriber, $errormsg);
                         //campaign failure
                     } else {
                         if (!empty($e_array)) {
                             $senderrors[] = $mail->get_errors('br');
                             $break_on_error = true;
                         } elseif ($errormsg) {
                             $senderrors[] = $errormsg;
                             $break_on_error = true;
                         }
                         if ($break_on_error) {
                             $this->cron_log('Campaign paused cause of a sending error: <span style="color:#f33">' . $errormsg . '</span>');
                         }
                     }
                     $this->post_meta($subscriber->ID, 'mymail-campaigns', $usercampaigndata);
                 }
                 $max_memory_usage = max($max_memory_usage, memory_get_peak_usage(true));
                 $took_mail = (microtime(true) - $time_mail_start) * 1000;
                 //pause
                 if ($send_delay) {
                     usleep(max(1, round($send_delay - $took_mail)));
                 }
             }
         }
         $max_memory_usage = max($max_memory_usage, memory_get_peak_usage(true));
         mysql_free_result($result);
         $this->cron_log();
         $took = microtime(true) - $time_start;
         if ($max_memory_usage) {
             $this->cron_log('max. memory usage', '<strong>' . size_format($max_memory_usage, 2) . '</strong>');
         }
         $this->cron_log('sent this turn', $sent_this_turn);
         if ($sent_this_turn) {
             $this->cron_log('time', round($took, 2) . ' sec., (' . round($took / $sent_this_turn, 4) . '/mail)');
         }
         $this->cron_log();
         //close connection if smtp
         $mail->close();
         //load it again cause it may changed during sending
         wp_cache_delete($campaign->ID, 'post' . '_meta');
         $mymail_campaigndata[$campaigncount] = get_post_meta($campaign->ID, 'mymail-campaign', true);
         //count users and save to campaign
         $mymail_campaigndata[$campaigncount]['sent'] = $totalsend;
         $mymail_campaigndata[$campaigncount]['errors'] = $errors;
         $mymail_campaigndata[$campaigncount]['totalerrors'] = $totalerrors;
         if ($break_on_error) {
             $this->change_status($campaign, 'paused');
             mymail_notice(sprintf(__('Campaign %1$s has been paused cause of a sending error: %2$s', 'mymail'), '<a href="post.php?post=' . $campaign->ID . '&action=edit"><strong>' . $campaign->post_title . '</strong></a>', '<strong>' . implode('', $senderrors)) . '</strong>', 'error', false, 'camp_error_' . $campaign->ID);
             do_action('mymail_break_on_error', $campaign, $senderrors);
             //campaign is finished (or no mail was sent cause no subscriber)
         } else {
             //recalc totals
             $mymail_campaigndata[$campaigncount]['total'] = $this->get_totals_by_id($campaign->ID) + $mymail_campaigndata[$campaigncount]['unsubscribes'] + $mymail_campaigndata[$campaigncount]['hardbounces'] - $totalerrors;
             //stop with no subscribers
             if ($mymail_campaigndata[$campaigncount]['total'] == 0) {
                 continue;
             }
             //recalculate totals if sent is more or equal the once in the database
             /*
             				if($mymail_campaigndata[$campaigncount]['sent'] >= $subscribers_count){
             					//$mymail_campaigndata[$campaigncount]['total'] = $this->get_totals_by_id($campaign->ID, true, true)+$mymail_campaigndata[$campaigncount]['unsubscribes']-$totalerrors;
             				}
             */
             //obviously finished
             if ($mymail_campaigndata[$campaigncount]['sent'] + $mymail_campaigndata[$campaigncount]['hardbounces'] >= $mymail_campaigndata[$campaigncount]['total']) {
                 $mymail_campaigndata[$campaigncount]['timestamp'] = current_time('timestamp');
                 //change campaign to finished and replace the dyamic content
                 $placeholder->clear_placeholder();
                 $placeholder->set_content($campaign->post_content);
                 //$placeholder->add($placeholder->get_dynamic());
                 //remove the KSES filter which strips "unwanted" tags and attributes
                 remove_filter('content_save_pre', 'wp_filter_post_kses');
                 wp_update_post(array('ID' => $campaign->ID, 'post_content' => $placeholder->get_content(false)));
                 //change status silencly if only bounces where sent
                 $this->change_status($campaign, 'finished', $bounces_only);
                 //do third party plugins stuff
                 $this->thirdpartystuff();
             } else {
             }
             if ($sent_this_turn) {
                 do_action('mymail_cron_mails_sent', $campaign);
             }
         }
     }
     if ($cron_used && is_user_logged_in()) {
         $this->show_cron_log();
     }
     if ($quit_cronjob && !$cron_used) {
         $this->remove_crons();
     }
     do_action('mymail_cron_finished');
     return true;
 }
 public static function ValidateAddress($address)
 {
     return mymail_is_email($address);
 }
 public function send_confirmation($baselink, $email, $userdata = array(), $lists = array(), $confirm_array = array(), $template = 'notification.html')
 {
     $email = trim($email);
     if (!mymail_is_email($email)) {
         return false;
     }
     $userdata['email'] = $email;
     $hash = $this->hash($email);
     $confirms = get_option('mymail_confirms', array());
     $confirms[$hash] = wp_parse_args($confirm_array, array('timestamp' => time(), 'userdata' => $userdata, 'lists' => $lists, 'template' => $template, 'try' => 0, 'last' => time()));
     $link = htmlentities(add_query_arg(array('confirm' => '', 'k' => $hash), $baselink));
     require_once MYMAIL_DIR . '/classes/mail.class.php';
     $mail = mymail_mail::get_instance();
     $mail->to = $email;
     $mail->subject = mymail_text('subscription_subject');
     if (mymail_option('vcard')) {
         $mail->attachments[] = MYMAIL_UPLOAD_DIR . '/' . mymail_option('vcard_filename', 'vCard.vcf');
     }
     $text = mymail_text('subscription_text');
     if (strpos($text, '{link}') == -1) {
         $text .= "\n{link}";
     }
     if (isset($userdata['_meta'])) {
         unset($userdata['_meta']);
     }
     $userdata['emailaddress'] = $email;
     $userdata['email'] = '<a href="mailto:' . $email . '">' . $email . '</a>';
     $userdata['fullname'] = trim(@$userdata['firstname'] . ' ' . @$userdata['lastname']);
     $result = $mail->send_notification(nl2br($text), mymail_text('subscription_headline'), wp_parse_args(array('link' => '<a href="' . $link . '">' . mymail_text('subscription_link') . '</a>'), $userdata), true, $template);
     if ($result) {
         update_option('mymail_confirms', $confirms);
     }
     return $result;
 }