private function __construct()
 {
     $this->deliverymethod = mymail_option('deliverymethod', 'simple');
     $this->dkim = mymail_option('dkim');
     require_once MYMAIL_DIR . '/classes/mail.helper.class.php';
     $this->mailer = new mymail_mail_helper(true);
     if ($this->deliverymethod == 'smtp') {
         $this->mailer->IsSMTP();
         $this->mailer->Host = mymail_option('smtp_host');
         $this->mailer->Port = mymail_option('smtp_port', 25);
         $this->mailer->Timeout = mymail_option('smtp_timeout', 10);
         $this->mailer->SMTPAuth = mymail_option('smtp_auth', false);
         if ($this->mailer->SMTPAuth) {
             $this->mailer->Username = mymail_option('smtp_user');
             $this->mailer->Password = mymail_option('smtp_pwd');
         }
         $this->mailer->SMTPSecure = mymail_option('smtp_secure', '');
         $this->mailer->SMTPKeepAlive = true;
         add_action('mymail_presend', array(&$this, 'pre_send'));
         add_action('mymail_dosend', array(&$this, 'do_send'));
     } else {
         if ($this->deliverymethod == 'gmail') {
             $this->mailer->IsSMTP();
             $this->mailer->Host = 'smtp.googlemail.com';
             $this->mailer->Port = 587;
             $this->mailer->SMTPAuth = true;
             $this->mailer->Username = mymail_option('gmail_user');
             $this->mailer->Password = mymail_option('gmail_pwd');
             $this->mailer->SMTPSecure = 'tls';
             $this->mailer->SMTPKeepAlive = true;
             add_action('mymail_presend', array(&$this, 'pre_send'));
             add_action('mymail_dosend', array(&$this, 'do_send'));
         } else {
             if ($this->deliverymethod == 'simple') {
                 $method = mymail_option('simplemethod', 'sendmail');
                 if ($method == 'sendmail') {
                     $this->mailer->Sendmail = mymail_option('sendmail_path');
                     if (empty($this->mailer->Sendmail)) {
                         $this->mailer->Sendmail = '/usr/sbin/sendmail';
                     }
                     $this->mailer->IsSendmail();
                 } else {
                     if ($method == 'qmail') {
                         $this->mailer->IsQmail();
                     } else {
                         $this->mailer->IsMail();
                     }
                 }
                 add_action('mymail_presend', array(&$this, 'pre_send'));
                 add_action('mymail_dosend', array(&$this, 'do_send'));
             } else {
                 do_action('mymail_initsend', $this);
             }
         }
     }
     if ($this->dkim) {
         $this->mailer->DKIM_selector = mymail_option('dkim_selector');
         $this->mailer->DKIM_domain = mymail_option('dkim_domain');
         $folder = MYMAIL_UPLOAD_DIR . '/dkim';
         $this->mailer->DKIM_private = $folder . '/' . mymail_option('dkim_private_hash') . '.pem';
         $this->mailer->DKIM_passphrase = mymail_option('dkim_passphrase');
         $this->mailer->DKIM_identity = mymail_option('dkim_identity');
     }
     $this->from = mymail_option('from');
     $this->from_name = mymail_option('from_name');
     $this->send_limit = mymail_option('send_limit');
     if (!get_transient('_mymail_send_period_timeout')) {
         set_transient('_mymail_send_period_timeout', true, mymail_option('send_period') * 3600);
     } else {
         $this->sent_within_period = get_transient('_mymail_send_period');
         if (!$this->sent_within_period) {
             $this->sent_within_period = 0;
         }
     }
     $this->sentlimitreached = $this->sent_within_period >= $this->send_limit;
     if ($this->sentlimitreached) {
         $msg = sprintf(__('Sent limit of %1$s reached! You have to wait %2$s before you can send more mails!', 'mymail'), '<strong>' . $this->send_limit . '</strong>', '<strong>' . human_time_diff(get_option('_transient_timeout__mymail_send_period_timeout')) . '</strong>');
         mymail_notice($msg, 'error', false, 'dailylimit');
     } else {
         mymail_remove_notice('dailylimit');
     }
 }
 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;
 }
 private function start_campaign($id)
 {
     if (!current_user_can('publish_newsletters')) {
         wp_die(__('You are not allowed to start campaigns.', 'mymail'));
     }
     $post = get_post($id);
     $post_meta = get_post_meta($id, 'mymail-data', true);
     if (!$this->get_totals_by_id($id)) {
         return false;
     }
     $post_meta['active'] = true;
     if (empty($post_meta['timestamp']) || $post->post_status == 'queued') {
         $currenttime = current_time('timestamp');
         $post_meta['timestamp'] = $currenttime;
         $post_meta['date'] = date('Y-m-d', $post_meta['timestamp']);
         $post_meta['time'] = date('H:i', $post_meta['timestamp']);
     }
     $this->post_meta($id, 'mymail-data', $post_meta, true);
     $success = $this->change_status($post, 'active');
     mymail_remove_notice('camp_error_' . $id);
     $this->add_cron();
     return $success;
 }