/**
  * Standard modular run function for CRON hooks. Searches for tasks to perform.
  */
 function run()
 {
     //if (!running_script('execute_temp')) return;
     $time_now = time();
     //$time_now=1335726076;
     $last_cron_time = intval(get_value('last_welcome_mail_time'));
     if ($last_cron_time == 0) {
         $last_cron_time = $time_now - 24 * 60 * 60 * 7;
     }
     set_value('last_welcome_mail_time', strval($time_now));
     //$last_cron_time=$time_now-60*60*1;
     require_code('mail');
     $GLOBALS['NO_DB_SCOPE_CHECK'] = true;
     $mails = $GLOBALS['SITE_DB']->query_select('f_welcome_emails', array('*'));
     $GLOBALS['NO_DB_SCOPE_CHECK'] = false;
     foreach ($mails as $mail) {
         $send_seconds_after_joining = $mail['w_send_time'] * 60 * 60;
         $newsletter_style = get_value('welcome_nw_choice') === '1' && !is_null($mail['w_newsletter']) || get_value('welcome_nw_choice') !== '1' && ($mail['w_newsletter'] == 1 || get_forum_type() != 'ocf');
         if ($newsletter_style) {
             if (addon_installed('newsletter')) {
                 // Think of it like this, m_join_time (members join time) must between $last_cron_time and $time_now, but offset back by $send_seconds_after_joining
                 $where = ' WHERE join_time>' . strval($last_cron_time - $send_seconds_after_joining) . ' AND join_time<=' . strval($time_now - $send_seconds_after_joining) . ' AND (the_level=3 OR the_level=4)';
                 if (get_value('welcome_nw_choice') === '1') {
                     $where .= ' AND newsletter_id=' . strval($mail['w_newsletter']);
                 }
                 $members = $GLOBALS['SITE_DB']->query('SELECT s.email AS m_email_address,the_password,n_forename,n_surname,n.id,join_time AS m_join_time FROM ' . get_table_prefix() . 'newsletter_subscribe s JOIN ' . get_table_prefix() . 'newsletter n ON n.email=s.email ' . $where . ' GROUP BY s.email');
             } else {
                 $members = array();
             }
         } else {
             // Think of it like this, m_join_time (members join time) must between $last_cron_time and $time_now, but offset back by $send_seconds_after_joining
             $where = ' WHERE m_join_time>' . strval($last_cron_time - $send_seconds_after_joining) . ' AND m_join_time<=' . strval($time_now - $send_seconds_after_joining);
             if (get_option('allow_email_from_staff_disable') == '1') {
                 $where .= ' AND m_allow_emails=1';
             }
             $query = 'SELECT m_email_address,m_username,id,m_join_time FROM ' . get_table_prefix() . 'f_members' . $where;
             $members = $GLOBALS['FORUM_DB']->query($query);
         }
         //var_dump($members);exit();
         foreach ($members as $member) {
             $subject = get_translated_text($mail['w_subject'], NULL, get_lang($member['id']));
             $text = get_translated_text($mail['w_text'], NULL, get_lang($member['id']));
             $_text = do_template('NEWSLETTER_DEFAULT', array('CONTENT' => $text, 'LANG' => get_site_default_lang()));
             for ($i = 0; $i < 100; $i++) {
                 if (strpos($text, '{{' . strval($i) . '}}') !== false) {
                     $text = str_replace('{{' . strval($i) . '}}', get_timezoned_date($time_now + $i * 60 * 60 * 24), $text);
                 }
             }
             if ($member['m_email_address'] != '') {
                 $message = $_text->evaluate(get_lang($member['id']));
                 if ($newsletter_style) {
                     $forename = $member['n_forename'];
                     $surname = $member['n_surname'];
                     $name = trim($forename . ' ' . $surname);
                     require_lang('newsletter');
                     if ($name == '') {
                         $name = do_lang('NEWSLETTER_SUBSCRIBER', get_site_name());
                     }
                 } else {
                     $forename = '';
                     $surname = '';
                     $name = $member['m_username'];
                 }
                 if (addon_installed('newsletter')) {
                     if ($newsletter_style) {
                         $sendid = 'n' . strval($member['id']);
                         $hash = best_hash($member['the_password'], 'xunsub');
                     } else {
                         $sendid = 'w' . strval('id');
                         $hash = '';
                     }
                     require_code('newsletter');
                     $message = newsletter_variable_substitution($message, $subject, $forename, $surname, $name, $member['m_email_address'], $sendid, $hash);
                 }
                 if ($is_html) {
                     require_code('tempcode_compiler');
                     $temp = template_to_tempcode($message);
                     $message = $temp->evaluate(get_lang($member['id']));
                 }
                 if (get_value('notification_safety_testing') === '1') {
                     $test = $GLOBALS['SITE_DB']->query_value_null_ok('logged_mail_messages', 'm_date_and_time', array('m_subject' => $subject, 'm_to_email' => serialize(array($member['m_email_address']))));
                     if (!is_null($test)) {
                         if ($test > $member['m_join_time']) {
                             fatal_exit(do_lang('INTERNAL_ERROR') . ' [' . $member['m_email_address'] . ']');
                         }
                         // otherwise they probably just resubscribed and hence bumped their time
                     }
                 }
                 mail_wrap($subject, $message, array($member['m_email_address']), $name, '', '', 3, NULL, false, NULL, true, $is_html);
             }
         }
     }
 }
Example #2
0
 /**
  * The UI to confirm sending of our newsletter.
  *
  * @return tempcode		The UI
  */
 function confirm_send()
 {
     $title = get_page_title('NEWSLETTER_SEND');
     $message = post_param('message');
     $subject = post_param('subject');
     $lang = choose_language($title);
     $template = post_param('template', 'MAIL');
     $in_full = post_param_integer('in_full', 0);
     $html_only = post_param_integer('html_only', 0);
     $from_email = post_param('from_email', '');
     $from_name = post_param('from_name', '');
     $extra_post_data = array();
     require_code('uploads');
     if (is_swf_upload(true) && array_key_exists('file', $_FILES) || array_key_exists('file', $_FILES) && is_uploaded_file($_FILES['file']['tmp_name'])) {
         $_csv_data = array();
         $myfile = fopen($_FILES['file']['tmp_name'], 'rt');
         $del = ',';
         $csv_test_line = fgetcsv($myfile, 4096, $del);
         if (count($csv_test_line) == 1 && strpos($csv_test_line[0], ';') !== false) {
             $del = ';';
         }
         rewind($myfile);
         while (($csv_line = fgetcsv($myfile, 4096, $del)) !== false) {
             $_csv_data[] = $csv_line;
         }
         fclose($myfile);
         $extra_post_data['csv_data'] = serialize($_csv_data);
     }
     if (post_param_integer('make_periodic', 0) == 1) {
         // We're making a periodic newsletter. Thus we need to pass this info
         // through to the next step
         $extra_post_data['make_periodic'] = '1';
         // Re-generate preview from latest chosen_categories
         $message = $this->generate_whats_new_comcode(post_param('chosen_categories', ''), $in_full, $lang, get_input_date('cutoff'));
     }
     $address = $GLOBALS['FORUM_DRIVER']->get_member_email_address(get_member());
     if ($address == '') {
         $address = get_option('staff_address');
     }
     $username = $GLOBALS['FORUM_DRIVER']->get_username(get_member());
     $message = newsletter_variable_substitution($message, $subject, '', '', do_lang('UNKNOWN'), $address, 'test', '');
     require_code('mail');
     require_code('tempcode_compiler');
     $in_html = false;
     if (strpos($message, '<html') !== false) {
         $_preview = template_to_tempcode($message);
         $in_html = true;
     } else {
         $comcode_version = comcode_to_tempcode($message, get_member(), true);
         $_preview = do_template('MAIL', array('TITLE' => $subject, 'CSS' => css_tempcode(true, true, $comcode_version->evaluate()), 'LANG' => get_site_default_lang(), 'LOGOURL' => get_logo_url(''), 'CONTENT' => $comcode_version), NULL, false, NULL, '.tpl', 'templates', $GLOBALS['FORUM_DRIVER']->get_theme(''));
         $in_html = $html_only == 1;
     }
     $text_preview = $html_only == 1 ? '' : comcode_to_clean_text(static_evaluate_tempcode(template_to_tempcode($message)));
     require_code('mail');
     $preview_subject = $subject;
     if (post_param_integer('make_periodic', 0) == 1) {
         $preview_subject .= ' - ' . get_timezoned_date(time(), false, false, false, true);
     }
     require_code('comcode_text');
     $preview = do_template('NEWSLETTER_CONFIRM_WRAP', array('_GUID' => '02bd5a782620141f8589e647e2c6d90b', 'TEXT_PREVIEW' => $text_preview, 'PREVIEW' => $_preview, 'SUBJECT' => $subject));
     mail_wrap($preview_subject, $html_only == 1 ? $_preview->evaluate() : $message, array($address), $username, $from_email, $from_name, 3, NULL, true, NULL, true, $in_html);
     breadcrumb_set_parents(array(array('_SELF:_SELF:misc', do_lang_tempcode('MANAGE_NEWSLETTER')), array('_SELF:_SELF:new', do_lang_tempcode('NEWSLETTER_SEND'))));
     breadcrumb_set_self(do_lang_tempcode('CONFIRM'));
     require_code('templates_confirm_screen');
     return form_confirm_screen($title, $preview, 'send', get_param('old_type', 'new'), $extra_post_data);
 }
Example #3
0
/**
 * Actually send out the newsletter in the background.
 */
function newsletter_shutdown_function()
{
    global $NEWSLETTER_SUBJECT, $NEWSLETTER_MESSAGE, $NEWSLETTER_HTML_ONLY, $NEWSLETTER_FROM_EMAIL, $NEWSLETTER_FROM_NAME, $NEWSLETTER_PRIORITY, $NEWSLETTER_SEND_DETAILS, $NEWSLETTER_LANGUAGE, $CSV_DATA, $NEWSLETTER_MAIL_TEMPLATE;
    //mail_wrap($NEWSLETTER_SUBJECT,$NEWSLETTER_MESSAGE,$NEWSLETTER_ADDRESSES,$NEWSLETTER_USERNAMES,$NEWSLETTER_FROM_EMAIL,$NEWSLETTER_FROM_NAME,3,NULL,true,NULL,true,$NEWSLETTER_HTML_ONLY==1);  Not so easy any more as message needs tailoring per subscriber
    disable_php_memory_limit();
    // As PHP can leak memory, or caches can fill, even if we do this carefully
    $last_cron = get_value('last_cron');
    $start = 0;
    do {
        list($addresses, $hashes, $usernames, $forenames, $surnames, $ids, ) = newsletter_who_send_to($NEWSLETTER_SEND_DETAILS, $NEWSLETTER_LANGUAGE, $start, 100, false, $CSV_DATA);
        // Send to all
        foreach ($addresses as $i => $email_address) {
            // Variable substitution in body
            $newsletter_message_substituted = newsletter_variable_substitution($NEWSLETTER_MESSAGE, $NEWSLETTER_SUBJECT, $forenames[$i], $surnames[$i], $usernames[$i], $email_address, $ids[$i], $hashes[$i]);
            $in_html = false;
            if (strpos($newsletter_message_substituted, '<html') === false) {
                if ($NEWSLETTER_HTML_ONLY == 1) {
                    $_m = comcode_to_tempcode($newsletter_message_substituted, get_member(), true);
                    $newsletter_message_substituted = $_m->evaluate($NEWSLETTER_LANGUAGE);
                    $in_html = true;
                }
            } else {
                require_code('tempcode_compiler');
                $_m = template_to_tempcode($newsletter_message_substituted);
                $newsletter_message_substituted = $_m->evaluate($NEWSLETTER_LANGUAGE);
                $in_html = true;
            }
            if (!is_null($last_cron)) {
                $GLOBALS['SITE_DB']->query_insert('newsletter_drip_send', array('d_inject_time' => time(), 'd_subject' => $NEWSLETTER_SUBJECT, 'd_message' => $newsletter_message_substituted, 'd_html_only' => $NEWSLETTER_HTML_ONLY, 'd_to_email' => $email_address, 'd_to_name' => $usernames[$i], 'd_from_email' => $NEWSLETTER_FROM_EMAIL, 'd_from_name' => $NEWSLETTER_FROM_NAME, 'd_priority' => $NEWSLETTER_PRIORITY, 'd_template' => $NEWSLETTER_MAIL_TEMPLATE));
            } else {
                mail_wrap($NEWSLETTER_SUBJECT, $newsletter_message_substituted, array($email_address), array($usernames[$i]), $NEWSLETTER_FROM_EMAIL, $NEWSLETTER_FROM_NAME, $NEWSLETTER_PRIORITY, NULL, true, NULL, true, $in_html, false, $NEWSLETTER_MAIL_TEMPLATE);
            }
        }
        $start += 100;
    } while (array_key_exists(0, $addresses));
}