"Fossies" - the Fresh Open Source Software Archive

Member "Upload/admin/modules/user/mass_mail.php" (8 Jun 2019, 53860 Bytes) of package /linux/www/mybb_1821.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mass_mail.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1820_vs_1821.

    1 <?php
    2 /**
    3  * MyBB 1.8
    4  * Copyright 2014 MyBB Group, All Rights Reserved
    5  *
    6  * Website: http://www.mybb.com
    7  * License: http://www.mybb.com/about/license
    8  *
    9  */
   10 
   11 // Disallow direct access to this file for security reasons
   12 if(!defined("IN_MYBB"))
   13 {
   14     die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
   15 }
   16 
   17 require_once MYBB_ROOT."/inc/functions_massmail.php";
   18 
   19 $page->add_breadcrumb_item($lang->mass_mail, "index.php?module=user-mass_mail");
   20 
   21 if($mybb->input['action'] == "send" || $mybb->input['action'] == "archive" || !$mybb->input['action'])
   22 {
   23     $sub_tabs['mail_queue'] = array(
   24         'title' => $lang->mass_mail_queue,
   25         'link' => 'index.php?module=user-mass_mail',
   26         'description' => $lang->mass_mail_queue_desc
   27     );
   28 
   29     $sub_tabs['send_mass_mail'] = array(
   30         'title' => $lang->create_mass_mail,
   31         'link' => 'index.php?module=user-mass_mail&action=send',
   32         'description' => $lang->create_mass_mail_desc
   33     );
   34 
   35     $sub_tabs['archive'] = array(
   36         'title' => $lang->mass_mail_archive,
   37         'link' => 'index.php?module=user-mass_mail&action=archive',
   38         'description' => $lang->mass_mail_archive_desc
   39     );
   40 }
   41 
   42 $plugins->run_hooks("admin_user_mass_email");
   43 
   44 if($mybb->input['action'] == "edit")
   45 {
   46     $page->add_breadcrumb_item($lang->edit_mass_mail);
   47 
   48     $query = $db->simple_select("massemails", "*", "mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
   49     $email = $db->fetch_array($query);
   50     if(!$email['mid'])
   51     {
   52         flash_message($lang->error_invalid_mid, 'error');
   53         admin_redirect("index.php?module=user-mass_mail");
   54     }
   55 
   56     $plugins->run_hooks("admin_user_mass_email_edit_start");
   57 
   58     if($email['conditions'] != '')
   59     {
   60         $email['conditions'] = my_unserialize($email['conditions']);
   61     }
   62 
   63     $sub_tabs['edit_mass_mail'] = array(
   64         'title' => $lang->edit_mass_mail,
   65         'link' => 'index.php?module=user-mass_mail&amp;action=edit&amp;mid='.$email['mid'],
   66         'description' => $lang->edit_mass_mail_desc
   67     );
   68 
   69     $replacement_fields = array(
   70         "{username}" => $lang->username,
   71         "{email}" => $lang->email_addr,
   72         "{bbname}" => $lang->board_name,
   73         "{bburl}" => $lang->board_url
   74     );
   75 
   76     $html_personalisation = $text_personalisation = "<script type=\"text/javascript\">\n<!--\ndocument.write('{$lang->personalize_message} ";
   77     foreach($replacement_fields as $value => $name)
   78     {
   79         $html_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \'htmlmessage\'); return false;\">{$name}</a>], ";
   80         $text_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \'message\'); return false;\">{$name}</a>], ";
   81     }
   82     $html_personalisation = substr($html_personalisation, 0, -2)."');\n// --></script>\n";
   83     $text_personalisation = substr($text_personalisation, 0, -2)."');\n// --></script>\n";
   84 
   85     $localized_time_offset = (float)$mybb->user['timezone']*3600 + $mybb->user['dst']*3600;
   86     
   87     // All done here
   88     if($mybb->request_method == "post")
   89     {
   90         // Sending this message now
   91         if($mybb->input['delivery_type'] == "now")
   92         {
   93             $delivery_date = TIME_NOW;
   94         }
   95         // Delivering in the future
   96         else
   97         {
   98             if(stristr($mybb->input['deliverytime_time'], "pm"))
   99             {
  100                 $mybb->input['deliveryhour'] += 12;
  101             }
  102 
  103             $exploded = explode(':', $mybb->input['endtime_time']);
  104             $mybb->input['deliveryhour'] = (int)$exploded[0];
  105 
  106             $exploded = explode(' ', $exploded[1]);
  107             $mybb->input['deliveryminute'] = (int)$exploded[0];
  108 
  109             $delivery_date = gmmktime($mybb->input['deliveryhour'], $mybb->input['deliveryminute'], 0, $mybb->input['endtime_month'], $mybb->input['endtime_day'], $mybb->input['endtime_year']) - $localized_time_offset;
  110             if($delivery_date <= TIME_NOW)
  111             {
  112                 $errors[] = $lang->error_only_in_future;
  113             }
  114         }
  115 
  116         // Need to perform the search to fetch the number of users we're emailing
  117         $member_query = build_mass_mail_query($mybb->input['conditions']);
  118         $query = $db->simple_select("users u", "COUNT(uid) AS num", $member_query);
  119         $num = $db->fetch_field($query, "num");
  120 
  121         if($num == 0)
  122         {
  123             $errors[] = $lang->error_no_users;
  124         }
  125 
  126         if(!trim($mybb->input['subject']))
  127         {
  128             $errors[] = $lang->error_missing_subject;
  129         }
  130 
  131         if($mybb->input['type'] == 1)
  132         {
  133             if(!$mybb->input['message'])
  134             {
  135                 $errors[] = $lang->error_missing_message;
  136             }
  137         }
  138         else
  139         {
  140             if($mybb->input['format'] == 2 && $mybb->input['automatic_text'] == 0 && !$mybb->input['message'])
  141             {
  142                 $errors[] = $lang->error_missing_plain_text;
  143             }
  144 
  145             if(($mybb->input['format'] == 1 || $mybb->input['format'] == 2) && !$mybb->input['htmlmessage'])
  146             {
  147                 $errors[] = $lang->error_missing_html;
  148             }
  149             else if($mybb->input['format'] == 0 && !$mybb->input['message'])
  150             {
  151                 $errors[] = $lang->error_missing_plain_text;
  152             }
  153         }
  154 
  155         if(!$errors)
  156         {
  157             // Sending via a PM
  158             if($mybb->input['type'] == 1)
  159             {
  160                 $mybb->input['format'] = 0;
  161                 $mybb->input['htmlmessage'] = '';
  162             }
  163             // Sending via email
  164             else
  165             {
  166                 // Do we need to generate a text based version?
  167                 if($mybb->input['format'] == 2 && $mybb->input['automatic_text'])
  168                 {
  169                     $mybb->input['message'] = create_text_message($mybb->input['htmlmessage']);
  170                 }
  171                 else if($mybb->input['format'] == 1)
  172                 {
  173                     $mybb->input['message'] = '';
  174                 }
  175                 else if($mybb->input['format'] == 0)
  176                 {
  177                     $mybb->input['htmlmessage'] = '';
  178                 }
  179             }
  180 
  181             // Mark as queued for delivery
  182             $updated_email = array(
  183                 "status" => 1,
  184                 "senddate" => $delivery_date,
  185                 "totalcount" => $num,
  186                 "conditions" => $db->escape_string(my_serialize($mybb->input['conditions'])),
  187                 "message" => $db->escape_string($mybb->input['message']),
  188                 "subject" => $db->escape_string($mybb->input['subject']),
  189                 "htmlmessage" => $db->escape_string($mybb->input['htmlmessage']),
  190                 "format" => $mybb->get_input('format', MyBB::INPUT_INT),
  191                 "type" => $mybb->get_input('type', MyBB::INPUT_INT),
  192                 "perpage" => $mybb->get_input('perpage', MyBB::INPUT_INT)
  193             );
  194 
  195             $plugins->run_hooks("admin_user_mass_email_edit_commit");
  196 
  197             $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
  198 
  199             flash_message($lang->success_mass_mail_saved, 'success');
  200             admin_redirect("index.php?module=user-mass_mail");
  201         }
  202     }
  203 
  204     $page->output_header($lang->edit_mass_mail);
  205 
  206     $page->output_nav_tabs($sub_tabs, 'edit_mass_mail');
  207 
  208     // If we have any error messages, show them
  209     if($errors)
  210     {
  211         $page->output_inline_error($errors);
  212         $input = $mybb->input;
  213     }
  214     else
  215     {
  216         $input = $email;
  217 
  218         if($email['senddate'] != 0)
  219         {
  220             if($email['senddate'] <= TIME_NOW)
  221             {
  222                 $input['delivery_type'] = "now";
  223                 $delivery_type_checked['now'] = " checked=\"checked\"";
  224             }
  225             else
  226             {
  227                 // correct date by timezone and dst
  228                 $offset = 
  229                 $input['delivery_type'] = "future";
  230                 $time = gmdate("d-n-Y", $email['senddate'] + $localized_time_offset);
  231                 $time = explode('-', $time);
  232                 $input['deliverymonth'] = (int)$time[1];
  233                 $input['deliveryday'] = (int)$time[0];
  234                 $input['deliveryyear'] = (int)$time[2];
  235                 $input['endtime_time'] = gmdate($mybb->settings['timeformat'], $email['senddate'] + $localized_time_offset);
  236                 $delivery_type_checked['future'] = " checked=\"checked\"";
  237             }
  238         }
  239         else
  240         {
  241             $input['delivery_type'] = "now";
  242             $delivery_type_checked['now'] = " checked=\"checked\"";
  243         }
  244     }
  245     
  246     if(!$input['endtime_time'])
  247     {
  248         $input['endtime_time'] = gmdate($mybb->settings['timeformat'], TIME_NOW + $localized_time_offset);
  249     }
  250 
  251     if(!$input['deliveryyear'])
  252     {
  253         $enddateyear = gmdate('Y', TIME_NOW + $localized_time_offset);
  254     }
  255     else
  256     {
  257         $enddateyear = (int)$input['deliveryyear'];
  258     }
  259 
  260     if(!$input['deliverymonth'])
  261     {
  262         $input['enddatemonth'] = gmdate('n', TIME_NOW + $localized_time_offset);
  263     }
  264     else
  265     {
  266         $input['enddatemonth'] = (int)$input['deliverymonth'];
  267     }
  268 
  269     if(!$input['deliveryday'])
  270     {
  271         $input['enddateday'] = gmdate('j', TIME_NOW + $localized_time_offset);
  272     }
  273     else
  274     {
  275         $input['enddateday'] = (int)$input['deliveryday'];
  276     }
  277 
  278     $form = new Form("index.php?module=user-mass_mail&amp;action=edit", "post");
  279     echo $form->generate_hidden_field("mid", $email['mid']);
  280 
  281     $mid_add = '';
  282     if($email['mid'])
  283     {
  284         $mid_add = "&amp;mid={$email['mid']}";
  285     }
  286 
  287     $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->message_settings}");
  288 
  289     $form_container->output_row("{$lang->subject}: <em>*</em>", $lang->subject_desc, $form->generate_text_box('subject', $input['subject'], array('id' => 'subject')), 'subject');
  290 
  291     if($input['type'] == 0)
  292     {
  293         $type_email_checked = true;
  294         $type_pm_checked = false;
  295     }
  296     else if($input['type'] == 1)
  297     {
  298         $type_email_checked = false;
  299         $type_pm_checked = true;
  300     }
  301 
  302     $type_options = array(
  303         $form->generate_radio_button("type", 0, $lang->send_via_email, array("id" => "type_email", "checked" => $type_email_checked)),
  304         $form->generate_radio_button("type", 1, $lang->send_via_pm, array("id" => "type_pm", "checked" => $type_pm_checked))
  305     );
  306     $form_container->output_row("{$lang->message_type}: <em>*</em>", "", implode("<br />", $type_options));
  307 
  308     $monthnames = array(
  309         "offset",
  310         $lang->january,
  311         $lang->february,
  312         $lang->march,
  313         $lang->april,
  314         $lang->may,
  315         $lang->june,
  316         $lang->july,
  317         $lang->august,
  318         $lang->september,
  319         $lang->october,
  320         $lang->november,
  321         $lang->december,
  322     );
  323 
  324     $enddatemonth = "";
  325     foreach($monthnames as $key => $month)
  326     {
  327         if($month == "offset")
  328         {
  329             continue;
  330         }
  331 
  332         if($key == $input['enddatemonth'])
  333         {
  334             $enddatemonth .= "<option value=\"{$key}\" selected=\"selected\">{$month}</option>\n";
  335         }
  336         else
  337         {
  338             $enddatemonth .= "<option value=\"{$key}\">{$month}</option>\n";
  339         }
  340     }
  341 
  342     $enddateday = "";
  343 
  344     // Construct option list for days
  345     for($i = 1; $i <= 31; ++$i)
  346     {
  347         if($i == $input['enddateday'])
  348         {
  349             $enddateday .= "<option value=\"{$i}\" selected=\"selected\">{$i}</option>\n";
  350         }
  351         else
  352         {
  353             $enddateday .= "<option value=\"{$i}\">{$i}</option>\n";
  354         }
  355     }
  356 
  357     $actions = "<script type=\"text/javascript\">
  358         function checkAction(id)
  359         {
  360             var checked = '';
  361 
  362             $('.'+id+'s_check').each(function(e, val)
  363             {
  364                 if($(this).prop('checked') == true)
  365                 {
  366                     checked = $(this).val();
  367                 }
  368             });
  369             $('.'+id+'s').each(function(e)
  370             {
  371                 $(this).hide();
  372             });
  373             if($('#'+id+'_'+checked))
  374             {
  375                 $('#'+id+'_'+checked).show();
  376             }
  377         }
  378     </script>
  379         <dl style=\"margin-top: 0; margin-bottom: 0; width: 100%;\">
  380         <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"now\" {$delivery_type_checked['now']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_immediately}</strong></label></dt>
  381 
  382         <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"future\" {$delivery_type_checked['future']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_specific}</strong></label></dt>
  383             <dd style=\"margin-top: 4px;\" id=\"delivery_type_future\" class=\"delivery_types\">
  384                 <table cellpadding=\"4\">
  385                     <tr>
  386                         <td><select name=\"endtime_day\">\n{$enddateday}</select>\n &nbsp; \n<select name=\"endtime_month\">\n{$enddatemonth}</select>\n &nbsp; \n<input type=\"text\" name=\"endtime_year\" value=\"{$enddateyear}\" class=\"text_input\" size=\"4\" maxlength=\"4\" />\n - {$lang->time} ".$form->generate_text_box('endtime_time', $input['endtime_time'], array('id' => 'endtime_time', 'style' => 'width: 60px;'))."</td>
  387                     </tr>
  388                 </table>
  389             </dd>
  390         </dl>
  391         <script type=\"text/javascript\">
  392         checkAction('delivery_type');
  393         </script>";
  394     $form_container->output_row("{$lang->delivery_date}: <em>*</em>", $lang->delivery_date_desc, $actions);
  395 
  396     $form_container->output_row("{$lang->per_page}: <em>*</em>", $lang->per_page_desc, $form->generate_numeric_field('perpage', $input['perpage'], array('id' => 'perpage', 'min' => 1)), 'perpage');
  397 
  398     $format_options = array(
  399         0 => $lang->plain_text_only,
  400         1 => $lang->html_only,
  401         2 => $lang->html_and_plain_text
  402     );
  403 
  404     $form_container->output_row("{$lang->message_format}: <em>*</em>", "", $form->generate_select_box('format', $format_options, $input['format'], array('id' => 'format')), 'format', null, array("id" => "format_container"));
  405 
  406     $form_container->end();
  407 
  408     if($input['format'] == 2)
  409     {
  410         if($input['automatic_text'] && !$email['mid'])
  411         {
  412             $automatic_text_check = true;
  413             $text_display = 'display: none';
  414             $automatic_display = 'display: none;';
  415         }
  416     }
  417     else if($input['format'] == 1 && $input['type'] != 1)
  418     {
  419         $text_display = 'display: none;';
  420     }
  421     else if($input['format'] == 0 || $input['type'] == 1)
  422     {
  423         $html_display = 'display: none';
  424     }
  425 
  426     echo "<div id=\"message_html\" style=\"{$html_display}\">";
  427     $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->define_html_message}");
  428     $form_container->output_row("{$lang->define_html_message_desc}:", $html_personalisation, $form->generate_text_area('htmlmessage', $input['htmlmessage'], array('id' => 'htmlmessage', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%'))."<div id=\"automatic_display\" style=\"{$automatic_display}\">".$form->generate_check_box('automatic_text', 1, $lang->auto_gen_plain_text, array('checked' => $automatic_text_check, "id" => "automatic_text"))."</div>");
  429     $form_container->end();
  430     echo "</div>";
  431 
  432     echo "<div id=\"message_text\" style=\"{$text_display}\">";
  433     $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->define_text_version}");
  434     $form_container->output_row("{$lang->define_text_version_desc}:", $text_personalisation, $form->generate_text_area('message', $input['message'], array('id' => 'message', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%')));
  435     $form_container->end();
  436     echo "</div>";
  437 
  438     echo "
  439     <script type=\"text/javascript\">
  440         function ToggleFormat()
  441         {
  442             var v = $('#format option:selected').val();
  443             if(v == 2)
  444             {
  445                 $('#automatic_display').show();
  446                 $('#message_html').show();
  447                 if($('#automatic_text').checked)
  448                 {
  449                     $('#message_text').hide();
  450                 }
  451                 else
  452                 {
  453                     $('#message_text').show();
  454                 }
  455             }
  456             else if(v == 1)
  457             {
  458                 $('#message_text').hide();
  459                 $('#message_html').show();
  460                 $('#automatic_display').hide();
  461             }
  462             else
  463             {
  464                 $('#message_text').show();
  465                 $('#message_html').hide();
  466             }
  467         }
  468         $(document).on('change', '#format', function() {
  469             ToggleFormat();
  470         });
  471 
  472         function ToggleType()
  473         {
  474             var v = $('#type_pm').prop('checked');
  475             if(v == true)
  476             {
  477                 $('#message_html').hide();
  478                 $('#message_text').show();
  479                 $('#format_container').hide();
  480             }
  481             else
  482             {
  483                 $('#message_html').show();
  484                 $('#format_container').show();
  485                 ToggleFormat();
  486             }
  487         }
  488         $('#type_pm').on('click', function() {
  489             ToggleType();
  490         });
  491         $('#type_email').on('click', function() {
  492             ToggleType();
  493         });
  494         ToggleType();
  495 
  496         function ToggleAutomatic()
  497         {
  498             var v = $('#automatic_text').prop('checked');
  499             if(v == true)
  500             {
  501                 $('#message_text').hide();
  502             }
  503             else
  504             {
  505                 $('#message_text').show();
  506             }
  507         }
  508 
  509         $('#automatic_text').on('click', function() {
  510             ToggleAutomatic();
  511         });
  512 
  513         function insertText(value, textarea)
  514         {
  515             textarea = document.getElementById(textarea);
  516             // Internet Explorer
  517             if(document.selection)
  518             {
  519                 textarea.trigger('focus');
  520                 var selection = document.selection.createRange();
  521                 selection.text = value;
  522             }
  523             // Firefox
  524             else if(textarea.selectionStart || textarea.selectionStart == '0')
  525             {
  526                 var start = textarea.selectionStart;
  527                 var end = textarea.selectionEnd;
  528                 textarea.value = textarea.value.substring(0, start) + value + textarea.value.substring(end, textarea.value.length);
  529             }
  530             else
  531             {
  532                 textarea.value += value;
  533             }
  534         }
  535 
  536     </script>";
  537 
  538     $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->define_the_recipients}");
  539 
  540     $form_container->output_row($lang->username_contains, "", $form->generate_text_box('conditions[username]', htmlspecialchars_uni($input['conditions']['username']), array('id' => 'username')), 'username');
  541     $form_container->output_row($lang->email_addr_contains, "", $form->generate_text_box('conditions[email]', $input['conditions']['email'], array('id' => 'email')), 'email');
  542 
  543     $query = $db->simple_select("usergroups", "gid, title", "gid != '1'", array('order_by' => 'title'));
  544 
  545     $options = array();
  546     while($usergroup = $db->fetch_array($query))
  547     {
  548         $options[$usergroup['gid']] = $usergroup['title'];
  549     }
  550 
  551     $form_container->output_row($lang->members_of, $lang->additional_user_groups_desc, $form->generate_select_box('conditions[usergroup][]', $options, $input['conditions']['usergroup'], array('id' => 'usergroups', 'multiple' => true, 'size' => 5)), 'usergroups');
  552 
  553     $greater_options = array(
  554         "greater_than" => $lang->greater_than,
  555         "is_exactly" => $lang->is_exactly,
  556         "less_than" => $lang->less_than
  557     );
  558     $form_container->output_row($lang->post_count_is, "", $form->generate_select_box('conditions[postnum_dir]', $greater_options, $input['conditions']['postnum_dir'], array('id' => 'postnum_dir'))." ".$form->generate_numeric_field('conditions[postnum]', $input['conditions']['postnum'], array('id' => 'postnum', 'min' => 0)), 'postnum');
  559 
  560     $more_options = array(
  561         "more_than" => $lang->more_than,
  562         "less_than" => $lang->less_than
  563     );
  564 
  565     $date_options = array(
  566         "hours" => $lang->hours,
  567         "days" => $lang->days,
  568         "weeks" => $lang->weeks,
  569         "months" => $lang->months,
  570         "years" => $lang->years
  571     );
  572     $form_container->output_row($lang->user_registered, "", $form->generate_select_box('conditions[regdate_dir]', $more_options, $input['conditions']['regdate_dir'], array('id' => 'regdate_dir'))." ".$form->generate_numeric_field('conditions[regdate]', $input['conditions']['regdate'], array('id' => 'regdate', 'min' => 0))." ".$form->generate_select_box('conditions[regdate_date]', $date_options, $input['conditions']['regdate_date'], array('id' => 'regdate_date'))." {$lang->ago}", 'regdate');
  573 
  574     $form_container->output_row($lang->user_last_active, "", $form->generate_select_box('conditions[lastactive_dir]', $more_options, $input['conditions']['lastactive_dir'], array('id' => 'lastactive_dir'))." ".$form->generate_numeric_field('conditions[lastactive]', $input['conditions']['lastactive'], array('id' => 'lastactive', 'min' => 0))." ".$form->generate_select_box('conditions[lastactive_date]', $date_options, $input['conditions']['lastactive_date'], array('id' => 'lastactive_date'))." {$lang->ago}", 'lastactive');
  575 
  576     $form_container->end();
  577 
  578     $buttons[] = $form->generate_submit_button($lang->save_mass_mail);
  579     $form->output_submit_wrapper($buttons);
  580 
  581     $form->end();
  582     $page->output_footer();
  583 }
  584 
  585 if($mybb->input['action'] == "send")
  586 {
  587     $page->add_breadcrumb_item($lang->send_mass_mail);
  588 
  589     if($mybb->input['step'])
  590     {
  591         $query = $db->simple_select("massemails", "*", "status=0 and mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
  592         $email = $db->fetch_array($query);
  593         if(!$email['mid'] && $mybb->input['step'] != 1)
  594         {
  595             flash_message($lang->error_invalid_mid, 'error');
  596             admin_redirect("index.php?module=user-mass_mail");
  597         }
  598     }
  599 
  600     $replacement_fields = array(
  601         "{username}" => $lang->username,
  602         "{email}" => $lang->email_addr,
  603         "{bbname}" => $lang->board_name,
  604         "{bburl}" => $lang->board_url
  605     );
  606 
  607     $html_personalisation = $text_personalisation = "<script type=\"text/javascript\">\n<!--\ndocument.write('{$lang->personalize_message} ";
  608     foreach($replacement_fields as $value => $name)
  609     {
  610         $html_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \'htmlmessage\'); return false;\">{$name}</a>], ";
  611         $text_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \'message\'); return false;\">{$name}</a>], ";
  612     }
  613     $html_personalisation = substr($html_personalisation, 0, -2)."');\n// --></script>\n";
  614     $text_personalisation = substr($text_personalisation, 0, -2)."');\n// --></script>\n";
  615 
  616     $plugins->run_hooks("admin_user_mass_email_send_start");
  617     
  618     $localized_time_offset = (float)$mybb->user['timezone']*3600 + $mybb->user['dst']*3600;
  619 
  620     if($mybb->input['step'] == 4)
  621     {
  622         // All done here
  623         if($mybb->request_method == "post")
  624         {
  625             // Sending this message now
  626             if($mybb->input['delivery_type'] == "now")
  627             {
  628                 $delivery_date = TIME_NOW;
  629             }
  630             // Delivering in the future
  631             else
  632             {
  633                 if(stristr($mybb->input['deliverytime_time'], "pm"))
  634                 {
  635                     $mybb->input['deliveryhour'] += 12;
  636                 }
  637 
  638                 $exploded = explode(':', $mybb->input['endtime_time']);
  639                 $mybb->input['deliveryhour'] = (int)$exploded[0];
  640 
  641                 $exploded = explode(' ', $exploded[1]);
  642                 $mybb->input['deliveryminute'] = (int)$exploded[0];
  643 
  644                 $delivery_date = gmmktime($mybb->input['deliveryhour'], $mybb->input['deliveryminute'], 0, $mybb->input['endtime_month'], $mybb->input['endtime_day'], $mybb->input['endtime_year'])- $localized_time_offset;
  645                 if($delivery_date <= TIME_NOW)
  646                 {
  647                     $errors[] = $lang->error_only_in_future;
  648                 }
  649             }
  650 
  651             if(!$errors)
  652             {
  653                 // Mark as queued for delivery
  654                 $updated_email = array(
  655                     "status" => 1,
  656                     "senddate" => $delivery_date
  657                 );
  658 
  659                 $plugins->run_hooks("admin_user_mass_email_send_finalize_commit");
  660 
  661                 $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
  662 
  663                 flash_message($lang->success_mass_mail_saved, 'success');
  664                 admin_redirect("index.php?module=user-mass_mail");
  665             }
  666         }
  667 
  668         // Show summary of the mass email we've just been creating and allow the user to specify the delivery date
  669         $page->output_header("{$lang->send_mass_mail}: {$lang->step_four}");
  670 
  671         $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
  672 
  673         // If we have any error messages, show them
  674         if($errors)
  675         {
  676             $page->output_inline_error($errors);
  677             $input = $mybb->input;
  678         }
  679         else
  680         {
  681             $input = array();
  682             if($email['senddate'] != 0)
  683             {
  684                 if($email['senddate'] <= TIME_NOW)
  685                 {
  686                     $input['delivery_type'] = "now";
  687                     $delivery_type_checked['now'] = " checked=\"checked\"";
  688                 }
  689                 else
  690                 {
  691                     $input['delivery_type'] = "future";
  692                     $time = gmdate("d-n-Y", $email['senddate'] + $localized_time_offset);
  693                     $time = explode('-', $time);
  694                     $input['deliverymonth'] = (int)$time[1];
  695                     $input['deliveryday'] = (int)$time[0];
  696                     $input['deliveryyear'] = (int)$time[2];
  697                     $input['endtime_time'] = gmdate($mybb->settings['timeformat'], $email['senddate'] + $localized_time_offset);
  698                     $delivery_type_checked['future'] = " checked=\"checked\"";
  699                 }
  700             }
  701             else
  702             {
  703                 $input['delivery_type'] = "now";
  704                 $delivery_type_checked['now'] = " checked=\"checked\"";
  705             }
  706         }
  707 
  708         $table = new Table;
  709         $table->construct_cell("<strong>{$lang->delivery_method}:</strong>", array('width' => '25%'));
  710         if($email['type'] == 1)
  711         {
  712             $delivery_type = $lang->private_message;
  713         }
  714         else if($email['type'] == 0)
  715         {
  716             $delivery_type = $lang->email;
  717         }
  718         $table->construct_cell($delivery_type);
  719         $table->construct_row();
  720 
  721         $table->construct_cell("<strong>{$lang->subject}:</strong>");
  722         $table->construct_cell(htmlspecialchars_uni($email['subject']));
  723         $table->construct_row();
  724 
  725         $table->construct_cell("<strong>{$lang->message}:</strong>");
  726         $format_preview = '';
  727         if($email['format'] == 0 || $email['format'] == 2)
  728         {
  729             $format_preview .= "{$lang->text_based} - <a href=\"#\" onclick=\"javascript:MyBB.popupWindow('index.php?module=user-mass_mail&amp;action=preview&amp;mid={$email['mid']}&amp;format=text', null, true);\">{$lang->preview}</a>";
  730         }
  731         if($email['format'] == 2)
  732         {
  733             $format_preview .= " {$lang->and} <br />";
  734         }
  735         if($email['format'] == 1 || $email['format'] == 2)
  736         {
  737             $format_preview.= "{$lang->html_based} - <a href=\"#\" onclick=\"javascript:MyBB.popupWindow('index.php?module=user-mass_mail&amp;action=preview&amp;mid={$email['mid']}', null, true);\">{$lang->preview}</a>";
  738         }
  739         $table->construct_cell($format_preview);
  740         $table->construct_row();
  741 
  742         // Recipient counts & details
  743         $table->construct_cell("<strong>{$lang->total_recipients}:</strong>");
  744         $table->construct_cell(my_number_format($email['totalcount'])." - <a href=\"index.php?module=user-mass_mail&amp;action=send&amp;step=3&amp;mid={$email['mid']}\">{$lang->change_recipient_conds}</a>");
  745         $table->construct_row();
  746 
  747         $table->output("{$lang->send_mass_mail}: {$lang->step_four} - {$lang->review_message}");
  748 
  749         if(!$input['endtime_time'])
  750         {
  751             $input['endtime_time'] = gmdate($mybb->settings['timeformat'], TIME_NOW + $localized_time_offset);
  752         }
  753 
  754         if(!$input['deliveryyear'])
  755         {
  756             $enddateyear = gmdate('Y', TIME_NOW + $localized_time_offset);
  757         }
  758         else
  759         {
  760             $enddateyear = (int)$input['deliveryyear'];
  761         }
  762 
  763         if(!$input['deliverymonth'])
  764         {
  765             $input['enddatemonth'] = gmdate('n', TIME_NOW + $localized_time_offset);
  766         }
  767         else
  768         {
  769             $input['enddatemonth'] = (int)$input['deliverymonth'];
  770         }
  771 
  772         if(!$input['deliveryday'])
  773         {
  774             $input['enddateday'] = gmdate('j', TIME_NOW + $localized_time_offset);
  775         }
  776         else
  777         {
  778             $input['enddateday'] = (int)$input['deliveryday'];
  779         }
  780 
  781         $monthnames = array(
  782             "offset",
  783             $lang->january,
  784             $lang->february,
  785             $lang->march,
  786             $lang->april,
  787             $lang->may,
  788             $lang->june,
  789             $lang->july,
  790             $lang->august,
  791             $lang->september,
  792             $lang->october,
  793             $lang->november,
  794             $lang->december,
  795         );
  796 
  797         $enddatemonth = "";
  798         foreach($monthnames as $key => $month)
  799         {
  800             if($month == "offset")
  801             {
  802                 continue;
  803             }
  804 
  805             if($key == $input['enddatemonth'])
  806             {
  807                 $enddatemonth .= "<option value=\"{$key}\" selected=\"selected\">{$month}</option>\n";
  808             }
  809             else
  810             {
  811                 $enddatemonth .= "<option value=\"{$key}\">{$month}</option>\n";
  812             }
  813         }
  814 
  815         $enddateday = "";
  816 
  817         // Construct option list for days
  818         for($i = 1; $i <= 31; ++$i)
  819         {
  820             if($i == $input['enddateday'])
  821             {
  822                 $enddateday .= "<option value=\"{$i}\" selected=\"selected\">{$i}</option>\n";
  823             }
  824             else
  825             {
  826                 $enddateday .= "<option value=\"{$i}\">{$i}</option>\n";
  827             }
  828         }
  829 
  830         $form = new Form("index.php?module=user-mass_mail&amp;action=send&amp;step=4&amp;mid={$email['mid']}", "post");
  831         $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_four} - {$lang->define_delivery_date}");
  832 
  833             $actions = "<script type=\"text/javascript\">
  834             function checkAction(id)
  835             {
  836                 var checked = '';
  837 
  838                 $('.'+id+'s_check').each(function(e, val)
  839                 {
  840                     if($(this).prop('checked') == true)
  841                     {
  842                         checked = $(this).val();
  843                     }
  844                 });
  845                 $('.'+id+'s').each(function(e)
  846                 {
  847                     $(this).hide();
  848                 });
  849                 if($('#'+id+'_'+checked))
  850                 {
  851                     $('#'+id+'_'+checked).show();
  852                 }
  853             }
  854         </script>
  855             <dl style=\"margin-top: 0; margin-bottom: 0; width: 100%;\">
  856             <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"now\" {$delivery_type_checked['now']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_immediately}</strong></label></dt>
  857 
  858             <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"future\" {$delivery_type_checked['future']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_specific}</strong></label></dt>
  859                 <dd style=\"margin-top: 4px;\" id=\"delivery_type_future\" class=\"delivery_types\">
  860                     <table cellpadding=\"4\">
  861                         <tr>
  862                             <td><select name=\"endtime_day\">\n{$enddateday}</select>\n &nbsp; \n<select name=\"endtime_month\">\n{$enddatemonth}</select>\n &nbsp; \n<input type=\"text\" name=\"endtime_year\" class=\"text_input\" value=\"{$enddateyear}\" size=\"4\" maxlength=\"4\" />\n - {$lang->time} ".$form->generate_text_box('endtime_time', $input['endtime_time'], array('id' => 'endtime_time', 'style' => 'width: 60px;'))."</td>
  863                         </tr>
  864                     </table>
  865                 </dd>
  866             </dl>
  867             <script type=\"text/javascript\">
  868             checkAction('delivery_type');
  869             </script>";
  870             $form_container->output_row("{$lang->delivery_date}: <em>*</em>", $lang->delivery_date_desc, $actions);
  871 
  872         $form_container->end();
  873 
  874         $buttons[] = $form->generate_submit_button($lang->schedule_for_delivery);
  875         $form->output_submit_wrapper($buttons);
  876 
  877         $form->end();
  878         $page->output_footer();
  879     }
  880     elseif($mybb->input['step'] == 3)
  881     {
  882         // Define the recipients/conditions
  883         if($mybb->request_method == "post")
  884         {
  885             // Need to perform the search to fetch the number of users we're emailing
  886             $member_query = build_mass_mail_query($mybb->input['conditions']);
  887             $query = $db->simple_select("users u", "COUNT(uid) AS num", $member_query);
  888             $num = $db->fetch_field($query, "num");
  889 
  890             if($num == 0)
  891             {
  892                 $errors[] = $lang->error_no_users;
  893             }
  894             // Got one or more results
  895             else
  896             {
  897                 $updated_email = array(
  898                     "totalcount" => $num,
  899                     "conditions" => $db->escape_string(my_serialize($mybb->input['conditions']))
  900                 );
  901 
  902                 $plugins->run_hooks("admin_user_mass_email_send_define_commit");
  903 
  904                 $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
  905 
  906                 // Take the user to the next step
  907                 admin_redirect("index.php?module=user-mass_mail&action=send&step=4&mid={$email['mid']}");
  908             }
  909         }
  910 
  911         $page->output_header("{$lang->send_mass_mail}: {$lang->step_three}");
  912 
  913         $form = new Form("index.php?module=user-mass_mail&amp;action=send&amp;step=3&amp;mid={$email['mid']}", "post");
  914         $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
  915 
  916         // If we have any error messages, show them
  917         if($errors)
  918         {
  919             $page->output_inline_error($errors);
  920             $input = $mybb->input;
  921         }
  922         else
  923         {
  924             if($email['conditions'] != '')
  925             {
  926                 $input = array(
  927                     "conditions" => my_unserialize($email['conditions'])
  928                 );
  929             }
  930             else
  931             {
  932                 $input = array();
  933             }
  934         }
  935 
  936         $options = array(
  937             'username', 'email', 'postnum_dir', 'postnum', 'regdate', 'regdate_date', 'regdate_dir', 'lastactive', 'lastactive_date', 'lastactive_dir'
  938         );
  939 
  940         foreach($options as $option)
  941         {
  942             if(!isset($input['conditions'][$option]))
  943             {
  944                 $input['conditions'][$option] = '';
  945             }
  946         }
  947         if(!isset($input['conditions']['usergroup']) || !is_array($input['conditions']['usergroup']))
  948         {
  949             $input['conditions']['usergroup'] = array();
  950         }
  951 
  952         $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_three} - {$lang->define_the_recipients}");
  953 
  954         $form_container->output_row($lang->username_contains, "", $form->generate_text_box('conditions[username]', htmlspecialchars_uni($input['conditions']['username']), array('id' => 'username')), 'username');
  955         $form_container->output_row($lang->email_addr_contains, "", $form->generate_text_box('conditions[email]', $input['conditions']['email'], array('id' => 'email')), 'email');
  956 
  957         $options = array();
  958         $query = $db->simple_select("usergroups", "gid, title", "gid != '1'", array('order_by' => 'title'));
  959         while($usergroup = $db->fetch_array($query))
  960         {
  961             $options[$usergroup['gid']] = $usergroup['title'];
  962         }
  963 
  964         $form_container->output_row($lang->members_of, $lang->additional_user_groups_desc, $form->generate_select_box('conditions[usergroup][]', $options, $input['conditions']['usergroup'], array('id' => 'usergroups', 'multiple' => true, 'size' => 5)), 'usergroups');
  965 
  966         $greater_options = array(
  967             "greater_than" => $lang->greater_than,
  968             "is_exactly" => $lang->is_exactly,
  969             "less_than" => $lang->less_than
  970         );
  971         $form_container->output_row($lang->post_count_is, "", $form->generate_select_box('conditions[postnum_dir]', $greater_options, $input['conditions']['postnum_dir'], array('id' => 'postnum_dir'))." ".$form->generate_numeric_field('conditions[postnum]', $input['conditions']['postnum'], array('id' => 'postnum', 'min' => 0)), 'postnum');
  972 
  973         $more_options = array(
  974             "more_than" => $lang->more_than,
  975             "less_than" => $lang->less_than
  976         );
  977 
  978         $date_options = array(
  979             "hours" => $lang->hours,
  980             "days" => $lang->days,
  981             "weeks" => $lang->weeks,
  982             "months" => $lang->months,
  983             "years" => $lang->years
  984         );
  985         $form_container->output_row($lang->user_registered, "", $form->generate_select_box('conditions[regdate_dir]', $more_options, $input['conditions']['regdate_dir'], array('id' => 'regdate_dir'))." ".$form->generate_numeric_field('conditions[regdate]', $input['conditions']['regdate'], array('id' => 'regdate', 'min' => 0))." ".$form->generate_select_box('conditions[regdate_date]', $date_options, $input['conditions']['regdate_date'], array('id' => 'regdate_date'))." {$lang->ago}", 'regdate');
  986 
  987         $form_container->output_row($lang->user_last_active, "", $form->generate_select_box('conditions[lastactive_dir]', $more_options, $input['conditions']['lastactive_dir'], array('id' => 'lastactive_dir'))." ".$form->generate_numeric_field('conditions[lastactive]', $input['conditions']['lastactive'], array('id' => 'lastactive', 'min' => 0))." ".$form->generate_select_box('conditions[lastactive_date]', $date_options, $input['conditions']['lastactive_date'], array('id' => 'lastactive_date'))." {$lang->ago}", 'lastactive');
  988 
  989         $form_container->end();
  990 
  991         $buttons[] = $form->generate_submit_button($lang->next_step);
  992         $form->output_submit_wrapper($buttons);
  993 
  994         $form->end();
  995         $page->output_footer();
  996     }
  997     // Reviewing the automatic text based version of the message.
  998     elseif($mybb->input['step'] == 2)
  999     {
 1000         // Update text based version
 1001         if($mybb->request_method == "post")
 1002         {
 1003             if(!trim($mybb->input['message']))
 1004             {
 1005                 $errors[] = $lang->error_missing_plain_text;
 1006             }
 1007             else
 1008             {
 1009                 $updated_email = array(
 1010                     "message" => $db->escape_string($mybb->input['message'])
 1011                 );
 1012 
 1013                 $plugins->run_hooks("admin_user_mass_email_send_review_commit");
 1014 
 1015                 $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
 1016 
 1017                 // Take the user to the next step
 1018                 admin_redirect("index.php?module=user-mass_mail&action=send&step=3&mid={$email['mid']}");
 1019             }
 1020         }
 1021 
 1022         $page->output_header("{$lang->send_mass_mail}: {$lang->step_two}");
 1023 
 1024         $form = new Form("index.php?module=user-mass_mail&amp;action=send&amp;step=2&amp;mid={$email['mid']}", "post");
 1025         $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
 1026 
 1027         // If we have any error messages, show them
 1028         if($errors)
 1029         {
 1030             $page->output_inline_error($errors);
 1031         }
 1032 
 1033         $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_two} - {$lang->review_text_version}");
 1034         $form_container->output_row("{$lang->review_text_version_desc}:", $text_personalisation, $form->generate_text_area('message', $email['message'], array('id' => 'message', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%')));
 1035         $form_container->end();
 1036 
 1037         $buttons[] = $form->generate_submit_button($lang->next_step);
 1038         $form->output_submit_wrapper($buttons);
 1039 
 1040         $form->end();
 1041         $page->output_footer();
 1042     }
 1043     elseif(!$mybb->input['step'] || $mybb->input['step'] == 1)
 1044     {
 1045         if($mybb->request_method == "post")
 1046         {
 1047             if(!trim($mybb->input['subject']))
 1048             {
 1049                 $errors[] = $lang->error_missing_subject;
 1050             }
 1051 
 1052             if($mybb->input['type'] == 1)
 1053             {
 1054                 if(!$mybb->input['message'])
 1055                 {
 1056                     $errors[] = $lang->error_missing_message;
 1057                 }
 1058             }
 1059             else
 1060             {
 1061                 if($mybb->input['format'] == 2 && $mybb->input['automatic_text'] == 0 && !$mybb->input['message'])
 1062                 {
 1063                     $errors[] = $lang->error_missing_plain_text;
 1064                 }
 1065 
 1066                 if(($mybb->input['format'] == 1 || $mybb->input['format'] == 2) && !$mybb->input['htmlmessage'])
 1067                 {
 1068                     $errors[] = $lang->error_missing_html;
 1069                 }
 1070                 else if($mybb->input['format'] == 0 && !$mybb->input['message'])
 1071                 {
 1072                     $errors[] = $lang->error_missing_plain_text;
 1073                 }
 1074             }
 1075 
 1076             // No errors, insert away
 1077             if(!$errors)
 1078             {
 1079                 if(!$new_email['mid'])
 1080                 {
 1081                     // Sending via a PM
 1082                     if($mybb->input['type'] == 1)
 1083                     {
 1084                         $mybb->input['format'] = 0;
 1085                         $mybb->input['htmlmessage'] = '';
 1086                     }
 1087                     // Sending via email
 1088                     else
 1089                     {
 1090                         // Do we need to generate a text based version?
 1091                         if($mybb->input['format'] == 2 && $mybb->input['automatic_text'])
 1092                         {
 1093                             $mybb->input['message'] = create_text_message($mybb->input['htmlmessage']);
 1094                         }
 1095                         else if($mybb->input['format'] == 1)
 1096                         {
 1097                             $mybb->input['message'] = '';
 1098                         }
 1099                         else if($mybb->input['format'] == 0)
 1100                         {
 1101                             $mybb->input['htmlmessage'] = '';
 1102                         }
 1103                     }
 1104 
 1105                     $new_email = array(
 1106                         "uid" => $mybb->user['uid'],
 1107                         "subject" => $db->escape_string($mybb->input['subject']),
 1108                         "message" => $db->escape_string($mybb->input['message']),
 1109                         "htmlmessage" => $db->escape_string($mybb->input['htmlmessage']),
 1110                         "format" => $mybb->get_input('format', MyBB::INPUT_INT),
 1111                         "type" => $mybb->get_input('type', MyBB::INPUT_INT),
 1112                         "dateline" => TIME_NOW,
 1113                         "senddate" => 0,
 1114                         "status" => 0,
 1115                         "sentcount" => 0,
 1116                         "totalcount" => 0,
 1117                         "conditions" => "",
 1118                         "perpage" => $mybb->get_input('perpage', MyBB::INPUT_INT)
 1119                     );
 1120 
 1121                     $mid = $db->insert_query("massemails", $new_email);
 1122 
 1123                     $plugins->run_hooks("admin_user_mass_email_send_insert_commit");
 1124                 }
 1125                 // Updating an existing one
 1126                 else
 1127                 {
 1128                     $updated_email = array(
 1129                         "subject" => $db->escape_string($mybb->input['subject']),
 1130                         "message" => $db->escape_string($mybb->input['message']),
 1131                         "htmlmessage" => $db->escape_string($mybb->input['htmlmessage']),
 1132                         "format" => $mybb->get_input('format', MyBB::INPUT_INT),
 1133                         "type" => $mybb->get_input('type', MyBB::INPUT_INT),
 1134                         "perpage" => $mybb->get_input('perpage', MyBB::INPUT_INT)
 1135                     );
 1136 
 1137                     $plugins->run_hooks("admin_user_mass_email_send_update_commit");
 1138 
 1139                     $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
 1140                     $mid = $email['mid'];
 1141                 }
 1142 
 1143                 if($mybb->input['format'] == 2 && $mybb->input['automatic_text'] == 1)
 1144                 {
 1145                     $next = 2;
 1146                 }
 1147                 else
 1148                 {
 1149                     $next = 3;
 1150                 }
 1151                 admin_redirect("index.php?module=user-mass_mail&action=send&step={$next}&mid={$mid}");
 1152             }
 1153         }
 1154 
 1155         $page->output_header("{$lang->send_mass_mail}: {$lang->step_one}");
 1156 
 1157         $mid_add = '';
 1158         if($email['mid'])
 1159         {
 1160             $mid_add = "&amp;mid={$email['mid']}";
 1161         }
 1162 
 1163         $form = new Form("index.php?module=user-mass_mail&amp;action=send{$mid_add}", "post");
 1164         $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
 1165 
 1166         // If we have any error messages, show them
 1167         if($errors)
 1168         {
 1169             $page->output_inline_error($errors);
 1170             $input = $mybb->input;
 1171         }
 1172         else if(!$email)
 1173         {
 1174             $input = array(
 1175                 "type" => 0,
 1176                 "format" => 2,
 1177                 "automatic_text" => 1,
 1178                 "perpage" => 50,
 1179             );
 1180         }
 1181         else
 1182         {
 1183             $input = $email;
 1184         }
 1185 
 1186         $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_one} - {$lang->message_settings}");
 1187 
 1188         $form_container->output_row("{$lang->subject}: <em>*</em>", $lang->subject_desc, $form->generate_text_box('subject', $input['subject'], array('id' => 'subject')), 'subject');
 1189 
 1190         if($mybb->input['type'] == 0)
 1191         {
 1192             $type_email_checked = true;
 1193             $type_pm_checked = false;
 1194         }
 1195         else if($mybb->input['type'] == 1)
 1196         {
 1197             $type_email_checked = false;
 1198             $type_pm_checked = true;
 1199         }
 1200 
 1201         $type_options = array(
 1202             $form->generate_radio_button("type", 0, $lang->send_via_email, array("id" => "type_email", "checked" => $type_email_checked)),
 1203             $form->generate_radio_button("type", 1, $lang->send_via_pm, array("id" => "type_pm", "checked" => $type_pm_checked))
 1204         );
 1205         $form_container->output_row("{$lang->message_type}:", "", implode("<br />", $type_options));
 1206 
 1207         $format_options = array(
 1208             0 => $lang->plain_text_only,
 1209             1 => $lang->html_only,
 1210             2 => $lang->html_and_plain_text
 1211         );
 1212 
 1213         $form_container->output_row("{$lang->message_format}:", "", $form->generate_select_box('format', $format_options, $input['format'], array('id' => 'format')), 'format', null, array("id" => "format_container"));
 1214 
 1215         $form_container->output_row("{$lang->per_page}: <em>*</em>", $lang->per_page_desc, $form->generate_numeric_field('perpage', $input['perpage'], array('id' => 'perpage', 'min' => 1)), 'perpage');
 1216 
 1217         $form_container->end();
 1218 
 1219         if($mybb->input['format'] == 2)
 1220         {
 1221             if($mybb->input['automatic_text'] && !$email['mid'])
 1222             {
 1223                 $automatic_text_check = true;
 1224                 $text_display = 'display: none';
 1225                 $automatic_display = 'display: none;';
 1226             }
 1227         }
 1228         else if($mybb->input['format'] == 1 && $mybb->input['type'] != 1)
 1229         {
 1230             $text_display = 'display: none;';
 1231         }
 1232         else if($mybb->input['format'] == 0 || $mybb->input['type'] == 1)
 1233         {
 1234             $html_display = 'display: none';
 1235         }
 1236 
 1237         echo "<div id=\"message_html\" style=\"{$html_display}\">";
 1238         $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_one} - {$lang->define_html_message}");
 1239         $form_container->output_row("{$lang->define_html_message_desc}:", $html_personalisation, $form->generate_text_area('htmlmessage', $input['htmlmessage'], array('id' => 'htmlmessage', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%'))."<div id=\"automatic_display\" style=\"{$automatic_display}\">".$form->generate_check_box('automatic_text', 1, $lang->auto_gen_plain_text, array('checked' => $automatic_text_check, "id" => "automatic_text"))."</div>");
 1240         $form_container->end();
 1241         echo "</div>";
 1242 
 1243         echo "<div id=\"message_text\" style=\"{$text_display}\">";
 1244         $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_one} - {$lang->define_text_version}");
 1245         $form_container->output_row("{$lang->define_text_version_desc}:", $text_personalisation, $form->generate_text_area('message', $input['message'], array('id' => 'message', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%')));
 1246         $form_container->end();
 1247         echo "</div>";
 1248 
 1249         echo "
 1250         <script type=\"text/javascript\">
 1251         function ToggleFormat()
 1252         {
 1253             var v = $('#format option:selected').val();
 1254             if(v == 2)
 1255             {
 1256                 $('#automatic_display').show();
 1257                 $('#message_html').show();
 1258                 if($('#automatic_text').checked)
 1259                 {
 1260                     $('#message_text').hide();
 1261                 }
 1262                 else
 1263                 {
 1264                     $('#message_text').show();
 1265                 }
 1266             }
 1267             else if(v == 1)
 1268             {
 1269                 $('#message_text').hide();
 1270                 $('#message_html').show();
 1271                 $('#automatic_display').hide();
 1272             }
 1273             else
 1274             {
 1275                 $('#message_text').show();
 1276                 $('#message_html').hide();
 1277             }
 1278         }
 1279         $(document).on('change', '#format', function() {
 1280             ToggleFormat();
 1281         });
 1282 
 1283         function ToggleType()
 1284         {
 1285             var v = $('#type_pm').prop('checked');
 1286             if(v == true)
 1287             {
 1288                 $('#message_html').hide();
 1289                 $('#message_text').show();
 1290                 $('#format_container').hide();
 1291             }
 1292             else
 1293             {
 1294                 $('#message_html').show();
 1295                 $('#format_container').show();
 1296                 ToggleFormat();
 1297             }
 1298         }
 1299         $('#type_pm').on('click', function() {
 1300             ToggleType();
 1301         });
 1302         $('#type_email').on('click', function() {
 1303             ToggleType();
 1304         });
 1305         ToggleType();
 1306 
 1307         function ToggleAutomatic()
 1308         {
 1309             var v = $('#automatic_text').prop('checked');
 1310             if(v == true)
 1311             {
 1312                 $('#message_text').hide();
 1313             }
 1314             else
 1315             {
 1316                 $('#message_text').show();
 1317             }
 1318         }
 1319 
 1320         $('#automatic_text').on('click', function() {
 1321             ToggleAutomatic();
 1322         });
 1323 
 1324         function insertText(value, textarea)
 1325         {
 1326             textarea = document.getElementById(textarea);
 1327             // Internet Explorer
 1328             if(document.selection)
 1329             {
 1330                 textarea.trigger('focus');
 1331                 var selection = document.selection.createRange();
 1332                 selection.text = value;
 1333             }
 1334             // Firefox
 1335             else if(textarea.selectionStart || textarea.selectionStart == '0')
 1336             {
 1337                 var start = textarea.selectionStart;
 1338                 var end = textarea.selectionEnd;
 1339                 textarea.value = textarea.value.substring(0, start) + value + textarea.value.substring(end, textarea.value.length);
 1340             }
 1341             else
 1342             {
 1343                 textarea.value += value;
 1344             }
 1345         }
 1346 
 1347         </script>";
 1348 
 1349         $buttons[] = $form->generate_submit_button($lang->next_step);
 1350         $form->output_submit_wrapper($buttons);
 1351 
 1352         $form->end();
 1353         $page->output_footer();
 1354     }
 1355 
 1356     $plugins->run_hooks("admin_user_mass_email_preview_end");
 1357 }
 1358 
 1359 if($mybb->input['action'] == "delete")
 1360 {
 1361     $query = $db->simple_select("massemails", "*", "mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
 1362     $mass_email = $db->fetch_array($query);
 1363 
 1364     if(!$mass_email['mid'])
 1365     {
 1366         flash_message($lang->error_delete_invalid_mid, 'error');
 1367         admin_redirect("index.php?module=user-mass_mail");
 1368     }
 1369 
 1370     // User clicked no
 1371     if($mybb->input['no'])
 1372     {
 1373         admin_redirect("index.php?module=user-mass_mail");
 1374     }
 1375 
 1376     $plugins->run_hooks("admin_user_mass_email_delete_start");
 1377 
 1378     if($mybb->request_method == "post")
 1379     {
 1380         $db->delete_query("massemails", "mid='{$mass_email['mid']}'");
 1381 
 1382         $plugins->run_hooks("admin_user_mass_email_delete_commit");
 1383 
 1384         // Log admin action
 1385         log_admin_action($mass_email['mid'], $mass_email['subject']);
 1386 
 1387         if($mybb->input['archive'] == 1)
 1388         {
 1389             flash_message($lang->success_mass_mail_deleted, 'success');
 1390             admin_redirect("index.php?module=user-mass_mail&action=archive");
 1391         }
 1392         else
 1393         {
 1394             flash_message($lang->success_mass_mail_deleted, 'success');
 1395             admin_redirect("index.php?module=user-mass_mail");
 1396         }
 1397     }
 1398     else
 1399     {
 1400         if($mybb->input['archive'] == 1)
 1401         {
 1402             $page->output_confirm_action("index.php?module=user-mass_mail&amp;action=delete&amp;mid={$mass_email['mid']}&amp;archive=1", $lang->mass_mail_deletion_confirmation);
 1403         }
 1404         else
 1405         {
 1406             $page->output_confirm_action("index.php?module=user-mass_mail&amp;action=delete&amp;mid={$mass_email['mid']}", $lang->mass_mail_deletion_confirmation);
 1407         }
 1408     }
 1409 }
 1410 
 1411 if($mybb->input['action'] == "preview")
 1412 {
 1413     $query = $db->simple_select("massemails", "*", "mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
 1414     $mass_email = $db->fetch_array($query);
 1415 
 1416     if(!$mass_email['mid'])
 1417     {
 1418         flash_message($lang->error_invalid_mid, 'error');
 1419         admin_redirect("index.php?module=user-mass_mail");
 1420     }
 1421 
 1422     $plugins->run_hooks("admin_user_mass_email_preview_start");
 1423 
 1424     echo '<div class="modal">
 1425     <div style="overflow-y: auto; max-height: 400px;">';
 1426     
 1427     $table = new Table();
 1428     
 1429     if($mybb->input['format'] == 'text' || !$mass_email['htmlmessage'])
 1430     {
 1431         // Show preview of the text version
 1432         $table->construct_cell(nl2br($mass_email['message']));
 1433     }
 1434     else
 1435     {
 1436         // Preview the HTML version
 1437         $table->construct_cell($mass_email['htmlmessage']);
 1438     }
 1439 
 1440     $plugins->run_hooks("admin_user_mass_email_preview_end");
 1441 
 1442     $table->construct_row();
 1443 
 1444     $table->output($lang->mass_mail_preview);
 1445 
 1446     echo '</div>
 1447 </div>';
 1448     exit;
 1449 }
 1450 
 1451 if($mybb->input['action'] == "resend")
 1452 {
 1453     // Copy and resend an email
 1454     $query = $db->simple_select("massemails", "*", "mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
 1455     $mass_email = $db->fetch_array($query);
 1456 
 1457     if(!$mass_email['mid'])
 1458     {
 1459         flash_message($lang->error_invalid_mid, 'error');
 1460         admin_redirect("index.php?module=user-mass_mail");
 1461     }
 1462 
 1463     $plugins->run_hooks("admin_user_mass_email_resend_start");
 1464 
 1465     // Need to perform the search to fetch the number of users we're emailing
 1466     $member_query = build_mass_mail_query(my_unserialize($mass_email['conditions']));
 1467     $query = $db->simple_select("users u", "COUNT(uid) AS num", $member_query);
 1468     $total_recipients = $db->fetch_field($query, "num");
 1469 
 1470     // Create the new email based off the old one.
 1471     $new_email = array(
 1472         "uid" => $mass_email['uid'],
 1473         "subject" => $db->escape_string($mass_email['subject']),
 1474         "message" => $db->escape_string($mass_email['message']),
 1475         "htmlmessage" => $db->escape_string($mass_email['htmlmessage']),
 1476         "type" => $db->escape_string($mass_email['type']),
 1477         "format" => $db->escape_string($mass_email['format']),
 1478         "dateline" => TIME_NOW,
 1479         "senddate" => '0',
 1480         "status" => 0,
 1481         "sentcount" => 0,
 1482         "totalcount" => $total_recipients,
 1483         "conditions" => $db->escape_string($mass_email['conditions']),
 1484         "perpage" => $mass_email['perpage']
 1485     );
 1486 
 1487     $mid = $db->insert_query("massemails", $new_email);
 1488 
 1489     $plugins->run_hooks("admin_user_mass_email_resend_end");
 1490 
 1491     // Redirect the user to the summary page so they can select when to deliver this message
 1492     flash_message($lang->success_mass_mail_resent, 'success');
 1493     admin_redirect("index.php?module=user-mass_mail&action=send&step=4&mid={$mid}");
 1494     exit;
 1495 }
 1496 
 1497 if($mybb->input['action'] == "cancel")
 1498 {
 1499     if(!verify_post_check($mybb->input['my_post_key']))
 1500     {
 1501         flash_message($lang->invalid_post_verify_key2, 'error');
 1502         admin_redirect("index.php?module=user-users");
 1503     }
 1504 
 1505     // Cancel the delivery of a mass-email.
 1506     $query = $db->simple_select("massemails", "*", "mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
 1507     $mass_email = $db->fetch_array($query);
 1508 
 1509     if(!$mass_email['mid'])
 1510     {
 1511         flash_message($lang->error_invalid_mid, 'error');
 1512         admin_redirect("index.php?module=user-mass_mail");
 1513     }
 1514 
 1515     $updated_email = array(
 1516         'status' => 4
 1517     );
 1518 
 1519     $plugins->run_hooks("admin_user_mass_email_cancel");
 1520 
 1521     $db->update_query("massemails", $updated_email, "mid='{$mass_email['mid']}'");
 1522 
 1523     flash_message($lang->success_mass_mail_canceled, 'success');
 1524     admin_redirect("index.php?module=user-mass_mail");
 1525     exit;
 1526 }
 1527 
 1528 if($mybb->input['action'] == "archive")
 1529 {
 1530     // View a list of archived email messages
 1531     $page->output_header($lang->mass_mail_archive);
 1532 
 1533     $plugins->run_hooks("admin_user_mass_email_archive_start");
 1534 
 1535     $page->output_nav_tabs($sub_tabs, 'archive');
 1536 
 1537     $table = new Table;
 1538     $table->construct_header($lang->subject);
 1539     $table->construct_header($lang->status, array('width' => '130', 'class' => 'align_center'));
 1540     $table->construct_header($lang->delivery_date, array('width' => '130', 'class' => 'align_center'));
 1541     $table->construct_header($lang->recipients, array('width' => '130', 'class' => 'align_center'));
 1542     $table->construct_header($lang->controls, array("class" => "align_center", "colspan" => 2, "width" => 200));
 1543 
 1544     $query = $db->simple_select("massemails", "*", "status NOT IN (0, 1, 2)", array('order_by' => 'senddate'));
 1545     while($email = $db->fetch_array($query))
 1546     {
 1547         $email['subject'] = htmlspecialchars_uni($email['subject']);
 1548         if($email['senddate'] < TIME_NOW)
 1549         {
 1550             $table->construct_cell("<strong>{$email['subject']}</strong>");
 1551         }
 1552         if($email['status'] == 3)
 1553         {
 1554             $status = $lang->delivered;
 1555         }
 1556         else if($email['status'] == 4)
 1557         {
 1558             $status = $lang->canceled;
 1559         }
 1560         $table->construct_cell($status, array("class" => "align_center"));
 1561 
 1562         $delivery_date = my_date($mybb->settings['dateformat'], $email['senddate']);
 1563 
 1564         $table->construct_cell($delivery_date, array("class" => "align_center"));
 1565         $table->construct_cell(my_number_format($email['totalcount']), array("class" => "align_center"));
 1566 
 1567         $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=resend&amp;mid={$email['mid']}\">{$lang->resend}</a>", array("width" => 100, "class" => "align_center"));
 1568         $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=delete&amp;mid={$email['mid']}&amp;my_post_key={$mybb->post_code}&amp;archive=1\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->mass_mail_deletion_confirmation}')\">{$lang->delete}</a>", array("width" => 100, "class" => "align_center"));
 1569 
 1570         $table->construct_row();
 1571     }
 1572 
 1573     if($table->num_rows() == 0)
 1574     {
 1575         $table->construct_cell($lang->no_archived_messages, array('colspan' => 6));
 1576         $table->construct_row();
 1577         $no_results = true;
 1578     }
 1579 
 1580     $plugins->run_hooks("admin_user_mass_email_archive_end");
 1581 
 1582     $table->output($lang->mass_mail_archive);
 1583 
 1584     $page->output_footer();
 1585 }
 1586 
 1587 if(!$mybb->input['action'])
 1588 {
 1589     $page->output_header($lang->mass_mail_queue);
 1590 
 1591     $plugins->run_hooks("admin_user_mass_email_start");
 1592 
 1593     $page->output_nav_tabs($sub_tabs, 'mail_queue');
 1594 
 1595     $table = new Table;
 1596     $table->construct_header($lang->subject);
 1597     $table->construct_header($lang->status, array('width' => '130', 'class' => 'align_center'));
 1598     $table->construct_header($lang->delivery_date, array('width' => '130', 'class' => 'align_center'));
 1599     $table->construct_header($lang->recipients, array('width' => '130', 'class' => 'align_center'));
 1600     $table->construct_header($lang->controls, array("class" => "align_center", "colspan" => 2, "width" => 200));
 1601 
 1602     $query = $db->simple_select("massemails", "*", "status IN (0, 1, 2)", array('order_by' => 'senddate'));
 1603     while($email = $db->fetch_array($query))
 1604     {
 1605         $email['subject'] = htmlspecialchars_uni($email['subject']);
 1606         if(TIME_NOW >= $email['senddate'] && $email['status'] > 1)
 1607         {
 1608             $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=edit&amp;mid={$email['mid']}\"><strong>{$email['subject']}</strong></a>");
 1609         }
 1610         else
 1611         {
 1612             $table->construct_cell("<strong>{$email['subject']}</strong>");
 1613         }
 1614         if($email['status'] == 0)
 1615         {
 1616             $status = $lang->draft;
 1617         }
 1618         else if($email['status'] == 1)
 1619         {
 1620             $status = $lang->queued;
 1621         }
 1622         else if($email['status'] == 2)
 1623         {
 1624             $progress = ceil($email['sentcount']/$email['totalcount']*100);
 1625             if($progress > 100)
 1626             {
 1627                 $progress = 100;
 1628             }
 1629             $status = "{$lang->delivering} ({$progress}%)";
 1630         }
 1631         $table->construct_cell($status, array("class" => "align_center"));
 1632 
 1633         if($email['status'] != 0)
 1634         {
 1635             $delivery_date = my_date($mybb->settings['dateformat'], $email['senddate']);
 1636         }
 1637         else
 1638         {
 1639             $delivery_date = $lang->na;
 1640         }
 1641 
 1642         $table->construct_cell($delivery_date, array("class" => "align_center"));
 1643         $table->construct_cell(my_number_format($email['totalcount']), array("class" => "align_center"));
 1644         if(TIME_NOW >= $email['senddate'] && $email['status'] > 1)
 1645         {
 1646             $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=cancel&amp;mid={$email['mid']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->mass_mail_cancel_confirmation}')\">{$lang->cancel}</a>", array("width" => 100, "colspan" => 2, "class" => "align_center"));
 1647         }
 1648         else
 1649         {
 1650             $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=edit&amp;mid={$email['mid']}\">{$lang->edit}</a>", array("width" => 100, "class" => "align_center"));
 1651             $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=delete&amp;mid={$email['mid']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->mass_mail_deletion_confirmation}')\">{$lang->delete}</a>", array("width" => 100, "class" => "align_center"));
 1652         }
 1653         $table->construct_row();
 1654     }
 1655 
 1656     if($table->num_rows() == 0)
 1657     {
 1658         $table->construct_cell($lang->no_unsent_messages, array('colspan' => 6));
 1659         $table->construct_row();
 1660         $no_results = true;
 1661     }
 1662 
 1663     $plugins->run_hooks("admin_user_mass_email_end");
 1664 
 1665     $table->output($lang->mass_mail_queue);
 1666 
 1667     $page->output_footer();
 1668 }