[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

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

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


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1