[ Index ]

PHP Cross Reference of MyBB 1.8.30

title

Body

[close]

/admin/modules/user/ -> mass_mail.php (source)

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


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref