Esempio n. 1
0
 /**
  * The actualiser to send a newsletter.
  *
  * @return tempcode		The UI
  */
 function send_message()
 {
     $title = get_page_title('NEWSLETTER_SEND');
     $lang = choose_language($title);
     if (is_object($lang)) {
         return $lang;
     }
     if (get_param('old_type', '') == 'whatsnew') {
         set_value('newsletter_whatsnew', strval(time()));
     }
     $message = post_param('message');
     $subject = post_param('subject');
     $csv_data = post_param('csv_data', '');
     // serialized PHP array
     $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', '');
     $priority = post_param_integer('priority', 3);
     $newsletters = $GLOBALS['SITE_DB']->query_select('newsletters', array('id'));
     $send_details = array();
     foreach ($newsletters as $newsletter) {
         $send_details[strval($newsletter['id'])] = post_param_integer(strval($newsletter['id']), 0);
     }
     if (get_forum_type() == 'ocf') {
         $groups = $GLOBALS['FORUM_DRIVER']->get_usergroup_list();
         foreach (array_keys($groups) as $id) {
             $send_details['g' . strval($id)] = post_param_integer('g' . strval($id), 0);
         }
         $send_details['-1'] = post_param_integer('-1', 0);
     }
     if (post_param_integer('make_periodic', 0) == 1) {
         // We're a periodic newsletter, so we don't actually want to be sent
         // out now. Rather, we store the newsletter settings so that it can be
         // regenerated as needed.
         // Next we store all of our settings in the newsletter_periodic table
         $when = post_param('periodic_when');
         $day = 1;
         if ($when == 'monthly') {
             $day = post_param_integer('periodic_monthly') % 29;
         } elseif ($when == 'biweekly') {
             $day = post_param_integer('periodic_weekday_biweekly', 5);
         } elseif ($when == 'weekly') {
             $day = post_param_integer('periodic_weekday_weekly', 5);
         }
         $map = array('np_message' => post_param('chosen_categories', ''), 'np_subject' => $subject, 'np_lang' => $lang, 'np_send_details' => serialize($send_details), 'np_html_only' => $html_only, 'np_from_email' => $from_email, 'np_from_name' => $from_name, 'np_priority' => $priority, 'np_csv_data' => $csv_data, 'np_frequency' => $when, 'np_day' => $day, 'np_in_full' => $in_full, 'np_template' => $template);
         require_lang('dates');
         $week_days = array(1 => do_lang('MONDAY'), 2 => do_lang('TUESDAY'), 3 => do_lang('WEDNESDAY'), 4 => do_lang('THURSDAY'), 5 => do_lang('FRIDAY'), 6 => do_lang('SATURDAY'), 7 => do_lang('SUNDAY'));
         if ($when == 'weekly') {
             $each = $week_days[$day];
         } elseif ($when == 'biweekly') {
             $each = $week_days[$day];
         } else {
             $suffix = gmdate('S', gmmktime(0, 0, 0, 1, $day, 1990));
             $each = strval($day) . $suffix;
         }
         $matches = array();
         if (preg_match('#^replace_existing\\_(\\d+)$#', post_param('periodic_choice', ''), $matches) != 0) {
             if (post_param('periodic_for') != 'future') {
                 $map['np_last_sent'] = 0;
             }
             $GLOBALS['SITE_DB']->query_update('newsletter_periodic', $map, array('id' => intval($matches[1])), '', 1);
             $message = do_lang('PERIODIC_SUCCESS_MESSAGE_EDIT', $when, $each);
         } else {
             $last_sent = post_param('periodic_for') == 'future' ? time() : 0;
             $map['np_last_sent'] = $last_sent;
             $GLOBALS['SITE_DB']->query_insert('newsletter_periodic', $map, true);
             $message = do_lang('PERIODIC_SUCCESS_MESSAGE_ADD', $when, $each);
         }
         $url = build_url(array('page' => 'admin_newsletter', 'type' => 'misc', 'redirected' => '1'), get_module_zone('admin_newsletter'));
         return redirect_screen(do_lang('SUCCESS'), $url, $message, false, 'inform');
     }
     if (addon_installed('calendar')) {
         $schedule = get_input_date('schedule');
         if (!is_null($schedule)) {
             require_code('calendar');
             require_code('calendar2');
             $send_details_string_exp = '';
             foreach ($send_details as $key => $val) {
                 $send_details_string_exp .= '"' . str_replace(chr(10), '\\n', addslashes($key)) . '"=>"' . str_replace(chr(10), '\\n', addslashes($val)) . '",';
             }
             $schedule_code = ':require_code(\'newsletter\'); actual_send_newsletter("' . php_addslashes($message) . '","' . php_addslashes($subject) . '","' . php_addslashes($lang) . '",array(' . $send_details_string_exp . '),' . strval($html_only) . ',"' . php_addslashes($from_email) . '","' . php_addslashes($from_name) . '",' . strval($priority) . ',"' . php_addslashes($template) . '");';
             $start_year = post_param_integer('schedule_year');
             $start_month = post_param_integer('schedule_month');
             $start_day = post_param_integer('schedule_day');
             $start_hour = post_param_integer('schedule_hour');
             $start_minute = post_param_integer('schedule_minute');
             $event_id = add_calendar_event(db_get_first_id(), '', NULL, 0, do_lang('NEWSLETTER_SEND', $subject), $schedule_code, 3, 0, $start_year, $start_month, $start_day, $start_hour, $start_minute);
             regenerate_event_reminder_jobs($event_id);
             return inform_screen($title, do_lang_tempcode('NEWSLETTER_DEFERRED', get_timezoned_date($schedule)));
         }
     }
     actual_send_newsletter($message, $subject, $lang, $send_details, $html_only, $from_email, $from_name, $priority, $csv_data, $template);
     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('DONE'));
     return inform_screen($title, do_lang_tempcode('SENDING_NEWSLETTER'));
 }
Esempio n. 2
0
 /**
  * Send a periodic newsletter.
  *
  * @param  array			Details of periodic newsletter
  * @return ?TIME			Time was sent (NULL: not sent)
  */
 function newsletter_periodic_handle($periodic_row)
 {
     // If we're here then we have a periodic newsletter along with details of
     // what we should put in it, who it should go to and when it should be
     // sent.
     // We check here to see if we're scheduled to be sent out
     $last_sent = $periodic_row['np_last_sent'];
     // At the moment we only support weekly or biweekly or monthly intervals. Thus we can
     // say for sure that if the last issue was sent in the past 4 days, we
     // don't need to run. This is useful because it stops the code sending out
     // multiple issues all day, and because we may as well extend it a few
     // days either side so that we bail out more quickly.
     if (abs(time() - $last_sent) < 60 * 60 * 24 * 4) {
         return NULL;
     }
     if ($periodic_row['np_frequency'] == 'monthly') {
         // Find out what day of the month it is
         $today = date('j');
         // Are we meant to be sending out an issue today?
         if (intval($today) != $periodic_row['np_day']) {
             return NULL;
         }
         // No, we're not
     } elseif ($periodic_row['np_frequency'] == 'weekly' || $periodic_row['np_frequency'] == 'biweekly') {
         // Find out what day of the week it is
         $weekdays = array('Error', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
         $send_day = $weekdays[$periodic_row['np_day']];
         $today = date('l');
         // Are we meant to be sending out an issue today?
         if ($today != $send_day) {
             return NULL;
         }
         // No, we're not
         if ($periodic_row['np_frequency'] == 'biweekly' && abs(time() - $last_sent) < 60 * 60 * 24 * 14) {
             return NULL;
         }
     } else {
         // If we don't know when to send it then we bail out
         return NULL;
     }
     // If we're here then we need to create and send out a newsletter.
     // We include everything since the last "What's New" newsletter,
     // irregardless of whether it was automatically or manually generated.
     $cutoff_time = $periodic_row['np_last_sent'];
     require_lang('newsletter');
     $lang = $periodic_row['np_lang'];
     $in_full = $periodic_row['np_in_full'] == 1;
     // We need to build the content, based on the chosen categories. This code
     // is lifted straight out of admin_newsletter.php
     // Generate Comcode for content selected, drawing on hooks
     $automatic = array();
     $i = 0;
     $contentarr = explode("\n", $periodic_row['np_message']);
     $_hooks = find_all_hooks('modules', 'admin_newsletter');
     foreach (array_keys($_hooks) as $hook) {
         require_code('hooks/modules/admin_newsletter/' . filter_naughty_harsh($hook));
         $object = object_factory('Hook_whats_news_' . filter_naughty_harsh($hook), true);
         if (is_null($object)) {
             continue;
         }
         //$filter=array_key_exists('whatsnew_'.$hook.'_filter',$_POST)?$_POST['whatsnew_'.$hook.'_filter']:NULL;
         $found_one_match = false;
         $last_find_id = mixed();
         $last_cat_id = mixed();
         $filter = '';
         foreach ($contentarr as $find_id => $line) {
             $matches = array();
             if (preg_match('#\\[' . str_replace('#', '\\#', preg_quote($hook)) . '/(.*)\\]#', $line, $matches) != 0) {
                 $found_one_match = true;
                 if (!is_null($last_find_id) && $find_id != $last_find_id + 1) {
                     $last_cat_id = intval($matches[1]);
                     $temp = $object->run(intval($cutoff_time), $lang, $filter, $in_full);
                     if (is_null($temp) || count($temp) == 0) {
                         continue;
                     }
                     if (!$temp[0]->is_empty()) {
                         $automatic[$last_find_id] = do_template('NEWSLETTER_AUTOMATE_SECTION_FCOMCODE', array('I' => strval($i + 1), 'TITLE' => $temp[1], 'CONTENT' => $temp[0]));
                         $i++;
                     }
                     $filter = $matches[1];
                 } else {
                     if ($filter != '') {
                         $filter .= ',';
                     }
                     $filter .= $matches[1];
                 }
                 $last_find_id = $find_id;
             }
         }
         //if (is_null($filter))
         if (!$found_one_match) {
             //$test=post_param_integer('whatsnew_'.$hook.'_include',0);
             //if ($test==0) continue;
             $found = false;
             foreach ($contentarr as $find_id => $line) {
                 if (strpos($line, '[' . $hook . ']') !== false) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 continue;
             }
             $temp = $object->run(intval($cutoff_time), $lang, $filter, $in_full);
             if (is_null($temp) || count($temp) == 0) {
                 continue;
             }
             if (!$temp[0]->is_empty()) {
                 $automatic[$find_id] = do_template('NEWSLETTER_AUTOMATE_SECTION_FCOMCODE', array('I' => strval($i + 1), 'TITLE' => $temp[1], 'CONTENT' => $temp[0]));
                 $i++;
             }
         } elseif ($filter != '') {
             $temp = $object->run(intval($cutoff_time), $lang, $filter, $in_full);
             if (is_null($temp) || count($temp) == 0) {
                 continue;
             }
             if (!$temp[0]->is_empty()) {
                 $automatic[$last_find_id] = do_template('NEWSLETTER_AUTOMATE_SECTION_FCOMCODE', array('I' => strval($i + 1), 'TITLE' => $temp[1], 'CONTENT' => $temp[0]));
                 $i++;
             }
         }
     }
     ksort($automatic);
     $_automatic = new ocp_tempcode();
     if (count($automatic) == 0) {
         return NULL;
     }
     // Nothing new
     foreach ($automatic as $tp) {
         $_automatic->attach($tp);
     }
     $completed = do_template('NEWSLETTER_AUTOMATED_FCOMCODE', array('CONTENT' => $_automatic));
     // Now we have the contents of our newsletter, we can send it to all of
     // those listed in the newsletter_periodic row
     $message = $completed->evaluate($lang);
     $subject = $periodic_row['np_subject'] . '-' . get_timezoned_date(time(), false, false, false, true);
     $time = time();
     require_code('newsletter');
     actual_send_newsletter($message, $subject, $lang, unserialize($periodic_row['np_send_details']), $periodic_row['np_html_only'], $periodic_row['np_from_email'], $periodic_row['np_from_name'], $periodic_row['np_priority'], $periodic_row['np_csv_data'], $periodic_row['np_template']);
     return $time;
 }