[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/tasks/ -> massmail.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: massmail.php 5297 2010-12-28 22:01:14Z Tomm $
  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  require_once  MYBB_ROOT."inc/datahandlers/pm.php";
  20  
  21  function task_massmail($task)
  22  {
  23      global $db, $mybb, $lang;    
  24      
  25      $query = $db->simple_select("massemails", "*", "senddate <= '".TIME_NOW."' AND status IN (1,2)");
  26      while($mass_email = $db->fetch_array($query))
  27      {
  28          if($mass_email['status'] == 1)
  29          {
  30              $db->update_query("massemails", array('status' => 2), "mid='{$mass_email['mid']}'", 1);
  31          }
  32          
  33          $sentcount = 0;
  34          
  35          if(!$mass_email['perpage'])
  36          {
  37              $mass_email['perpage'] = 50;
  38          }
  39          
  40          if(strpos($mass_email['htmlmessage'], '<br />') === false && strpos($mass_email['htmlmessage'], '<br>') === false)
  41          {
  42              $mass_email['htmlmessage'] = nl2br($mass_email['htmlmessage']);
  43          }
  44          
  45          $mass_email['orig_message'] = $mass_email['message'];
  46          $mass_email['orig_htmlmessage'] = $mass_email['htmlmessage'];
  47          
  48          // Need to perform the search to fetch the number of users we're emailing
  49          $member_query = build_mass_mail_query(unserialize($mass_email['conditions']));
  50          
  51          $count_query = $db->simple_select("users u", "COUNT(uid) AS num", $member_query);
  52          $mass_email['totalcount'] = $db->fetch_field($count_query, "num"); 
  53          
  54          $query2 = $db->simple_select("users u", "u.uid, u.language, u.pmnotify, u.lastactive, u.username, u.email", $member_query, array('limit_start' => $mass_email['sentcount'], 'limit' => $mass_email['perpage'], 'order_by' => 'u.uid', 'order_dir' => 'asc'));
  55          while($user = $db->fetch_array($query2))
  56          {
  57              $replacement_fields = array(
  58                  "{uid}" => $user['uid'],                        
  59                  "{username}" => $user['username'],
  60                  "{email}" => $user['email'],
  61                  "{bbname}" => $mybb->settings['bbname'],
  62                  "{bburl}" => $mybb->settings['bburl'],
  63                  "[".$lang->massmail_username."]" => $user['username'],
  64                  "[".$lang->email_addr."]" => $user['email'],
  65                  "[".$lang->board_name."]" => $mybb->settings['bbname'],
  66                  "[".$lang->board_url."]" => $mybb->settings['bburl']
  67              );
  68              
  69              foreach($replacement_fields as $find => $replace)
  70              {
  71                  $mass_email['message'] = str_replace($find, $replace, $mass_email['message']);
  72                  $mass_email['htmlmessage'] = str_replace($find, $replace, $mass_email['htmlmessage']);                                                
  73              }
  74                  
  75              // Private Message
  76              if($mass_email['type'] == 1)
  77              {                
  78                  $pm_handler = new PMDataHandler();
  79                  $pm_handler->admin_override = true;
  80              
  81                  $pm = array(
  82                      "subject" => $mass_email['subject'],
  83                      "message" => $mass_email['message'],
  84                      "fromid" => $mass_email['uid'],
  85                      "options" => array("savecopy" => 0),
  86                  );
  87                  
  88                  $pm['to'] = explode(",", $user['username']);
  89                  $pm_handler->set_data($pm);
  90                  if(!$pm_handler->validate_pm())
  91                  {
  92                      $friendly_errors = implode('\n', $pm_handler->get_friendly_errors());
  93                      add_task_log($task, $lang->sprintf($lang->task_massmail_ran_errors, htmlspecialchars_uni($user['username']), $friendly_errors));
  94                      $friendly_errors = "";
  95                  }
  96                  else
  97                  {
  98                      $pm_handler->insert_pm();
  99                  }
 100              }
 101              // Normal Email
 102              else
 103              {
 104                  switch($mass_email['format'])
 105                  {
 106                      case 2:
 107                          $format = "both";
 108                          $text_message = $mass_email['message'];
 109                          $mass_email['message'] = $mass_email['htmlmessage'];
 110                          break;
 111                      case 1:
 112                          $format = "html";
 113                          $text_message = "";
 114                          $mass_email['message'] = $mass_email['htmlmessage'];
 115                          break;
 116                      default:
 117                          $format = "text";
 118                          $text_message = "";
 119                  }
 120                  my_mail($user['email'], $mass_email['subject'], $mass_email['message'], "", "", "", false, $format, $text_message);
 121              }
 122              ++$sentcount;
 123              
 124              $mass_email['message'] = $mass_email['orig_message'];
 125              $mass_email['htmlmessage'] = $mass_email['orig_htmlmessage'];
 126          }
 127          
 128          $update_array = array();
 129          
 130          $update_array['sentcount'] = $mass_email['sentcount'] + $sentcount;
 131          $update_array['totalcount'] = $mass_email['totalcount'];
 132          
 133          if($update_array['sentcount'] >= $mass_email['totalcount'])
 134          {
 135              $update_array['status'] = 3;
 136          }
 137          
 138          $db->update_query("massemails", $update_array, "mid='{$mass_email['mid']}'", 1);
 139      }
 140      
 141      add_task_log($task, $lang->task_massmail_ran);
 142  }
 143  ?>


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