[ Index ]

PHP Cross Reference of MyBB 1.8.29

title

Body

[close]

/inc/ -> class_mailhandler.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  /**
  12   * Base mail handler class.
  13   */
  14  class MailHandler
  15  {
  16      /**
  17       * Which email it should send to.
  18       *
  19       * @var string
  20       */
  21      public $to;
  22  
  23      /**
  24       * 1/0 value weather it should show errors or not.
  25       *
  26       * @var integer
  27       */
  28      public $show_errors = 1;
  29  
  30      /**
  31       * Who it is from.
  32       *
  33       * @var string
  34       */
  35      public $from;
  36  
  37      /**
  38       * Full from string including name in format "name" <email>
  39       *
  40       * @var string
  41       */
  42      public $from_named;
  43  
  44      /**
  45       * Who the email should return to.
  46       *
  47       * @var string
  48       */
  49      public $return_email;
  50  
  51      /**
  52       * The subject of mail.
  53       *
  54       * @var string
  55       */
  56      public $subject;
  57  
  58      /**
  59       * The unaltered subject of mail.
  60       *
  61       * @var string
  62       */
  63      public $orig_subject;
  64  
  65      /**
  66       * The message of the mail.
  67       *
  68       * @var string
  69       */
  70      public $message;
  71  
  72      /**
  73       * The headers of the mail.
  74       *
  75       * @var string
  76       */
  77      public $headers;
  78  
  79      /**
  80       * The charset of the mail.
  81       *
  82       * @var string
  83       * @default utf-8
  84       */
  85      public $charset = "utf-8";
  86  
  87      /**
  88       * The currently used delimiter new lines.
  89       *
  90       * @var string
  91       */
  92      public $delimiter = "\r\n";
  93  
  94      /**
  95       * How it should parse the email (HTML or plain text?)
  96       *
  97       * @var string
  98       */
  99      public $parse_format = 'text';
 100  
 101      /**
 102       * The last received response from the SMTP server.
 103       *
 104       * @var string
 105       */
 106      public $data = '';
 107  
 108      /**
 109       * The last received response code from the SMTP server.
 110       *
 111       * @var string
 112       */
 113      public $code = 0;
 114  
 115      /**
 116       * Selects between AdminEmail and ReturnEmail, dependant on if ReturnEmail is filled.
 117       * 
 118       * @return string
 119       */
 120  	function get_from_email()
 121      {
 122          global $mybb;
 123          
 124          if(trim($mybb->settings['returnemail']))
 125          {
 126              $email = $mybb->settings['returnemail'];
 127          }
 128          else
 129          {
 130              $email = $mybb->settings['adminemail'];
 131          }
 132          
 133          return $email;
 134      }
 135  
 136      /**
 137       * Builds the whole mail.
 138       * To be used by the different email classes later.
 139       *
 140       * @param string $to to email.
 141       * @param string $subject subject of email.
 142       * @param string $message message of email.
 143       * @param string $from from email.
 144       * @param string $charset charset of email.
 145       * @param string $headers headers of email.
 146       * @param string $format format of the email (HTML, plain text, or both?).
 147       * @param string $message_text plain text version of the email.
 148       * @param string $return_email the return email address.
 149       */
 150  	function build_message($to, $subject, $message, $from="", $charset="", $headers="", $format="text", $message_text="", $return_email="")
 151      {
 152          global $parser, $lang, $mybb;
 153  
 154          $this->message = '';
 155          $this->headers = $headers;
 156          
 157          if($from)
 158          {
 159              $this->from = $from;
 160              $this->from_named = $this->from;
 161          }
 162          else
 163          {
 164              $this->from = $this->get_from_email();
 165              $this->from_named = '"'.$this->utf8_encode($mybb->settings['bbname']).'"';
 166              $this->from_named .= " <".$this->from.">";
 167          }
 168  
 169          if($return_email)
 170          {
 171              $this->return_email = $return_email;
 172          }
 173          else
 174          {
 175              $this->return_email = $this->get_from_email();
 176          }
 177  
 178          $this->set_to($to);
 179          $this->set_subject($subject);
 180  
 181          if($charset)
 182          {
 183              $this->set_charset($charset);
 184          }
 185  
 186          $this->parse_format = $format;
 187          $this->set_common_headers();
 188          $this->set_message($message, $message_text);
 189      }
 190  
 191      /**
 192       * Sets the charset.
 193       *
 194       * @param string $charset charset
 195       */
 196  	function set_charset($charset)
 197      {
 198          global $lang;
 199  
 200          if(empty($charset))
 201          {
 202              $this->charset = $lang->settings['charset'];
 203          }
 204          else
 205          {
 206              $this->charset = $charset;
 207          }
 208      }
 209  
 210      /**
 211       * Sets and formats the email message.
 212       *
 213       * @param string $message message
 214       * @param string $message_text
 215       */
 216  	function set_message($message, $message_text="")
 217      {
 218          $message = $this->cleanup_crlf($message);
 219  
 220          if($message_text)
 221          {
 222              $message_text = $this->cleanup_crlf($message_text);
 223          }
 224  
 225          if($this->parse_format == "html" || $this->parse_format == "both")
 226          {
 227              $this->set_html_headers($message, $message_text);
 228          }
 229          else
 230          {
 231              $this->message = $message;
 232              $this->set_plain_headers();
 233          }
 234      }
 235  
 236      /**
 237       * Sets and formats the email subject.
 238       *
 239       * @param string $subject
 240       */
 241  	function set_subject($subject)
 242      {
 243          $this->orig_subject = $this->cleanup($subject);
 244          $this->subject = $this->utf8_encode($this->orig_subject);
 245      }
 246  
 247      /**
 248       * Sets and formats the recipient address.
 249       *
 250       * @param string $to
 251       */
 252  	function set_to($to)
 253      {
 254          $to = $this->cleanup($to);
 255  
 256          $this->to = $this->cleanup($to);
 257      }
 258  
 259      /**
 260       * Sets the plain headers, text/plain
 261       */
 262  	function set_plain_headers()
 263      {
 264          $this->headers .= "Content-Type: text/plain; charset={$this->charset}{$this->delimiter}";
 265      }
 266  
 267      /**
 268       * Sets the alternative headers, text/html and text/plain.
 269       *
 270       * @param string $message
 271       * @param string $message_text
 272       */
 273  	function set_html_headers($message, $message_text="")
 274      {
 275          if(!$message_text && $this->parse_format == 'both')
 276          {
 277              $message_text = strip_tags($message);
 278          }
 279  
 280          if($this->parse_format == 'both')
 281          {
 282              $mime_boundary = "=_NextPart".md5(TIME_NOW);
 283  
 284              $this->headers .= "Content-Type: multipart/alternative; boundary=\"{$mime_boundary}\"{$this->delimiter}";
 285              $this->message = "This is a multi-part message in MIME format.{$this->delimiter}{$this->delimiter}";
 286  
 287              $this->message .= "--{$mime_boundary}{$this->delimiter}";
 288              $this->message .= "Content-Type: text/plain; charset=\"{$this->charset}\"{$this->delimiter}";
 289              $this->message .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}";
 290              $this->message .= $message_text."{$this->delimiter}{$this->delimiter}";
 291  
 292              $this->message .= "--{$mime_boundary}{$this->delimiter}";
 293  
 294              $this->message .= "Content-Type: text/html; charset=\"{$this->charset}\"{$this->delimiter}";
 295              $this->message .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}";
 296              $this->message .= $message."{$this->delimiter}{$this->delimiter}";
 297  
 298              $this->message .= "--{$mime_boundary}--{$this->delimiter}{$this->delimiter}";
 299          }
 300          else
 301          {
 302              $this->headers .= "Content-Type: text/html; charset=\"{$this->charset}\"{$this->delimiter}";
 303              $this->headers .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}";
 304              $this->message = $message."{$this->delimiter}{$this->delimiter}";
 305          }
 306      }
 307  
 308      /**
 309       * Sets the common headers.
 310       */
 311  	function set_common_headers()
 312      {
 313          global $mybb;
 314  
 315          // Build mail headers
 316          $this->headers .= "From: {$this->from_named}{$this->delimiter}";
 317  
 318          if($this->return_email)
 319          {
 320              $this->headers .= "Return-Path: {$this->return_email}{$this->delimiter}";
 321              $this->headers .= "Reply-To: {$this->return_email}{$this->delimiter}";
 322          }
 323  
 324          if(isset($_SERVER['SERVER_NAME']))
 325          {
 326              $http_host = $_SERVER['SERVER_NAME'];
 327          }
 328          else if(isset($_SERVER['HTTP_HOST']))
 329          {
 330              $http_host = $_SERVER['HTTP_HOST'];
 331          }
 332          else
 333          {
 334              $http_host = "unknown.local";
 335          }
 336  
 337          $msg_id = md5(uniqid(TIME_NOW, true)) . "@" . $http_host;
 338  
 339          if($mybb->settings['mail_message_id'])
 340          {
 341              $this->headers .= "Message-ID: <{$msg_id}>{$this->delimiter}";
 342          }
 343          $this->headers .= "Content-Transfer-Encoding: 8bit{$this->delimiter}";
 344          $this->headers .= "X-Priority: 3{$this->delimiter}";
 345          $this->headers .= "X-Mailer: MyBB{$this->delimiter}";
 346          $this->headers .= "MIME-Version: 1.0{$this->delimiter}";
 347      }
 348  
 349      /**
 350       * Log a fatal error message to the database.
 351       *
 352       * @param string $error The error message
 353       */
 354  	function fatal_error($error)
 355      {
 356          global $db;
 357  
 358          $mail_error = array(
 359              "subject" => $db->escape_string($this->orig_subject),
 360              "message" => $db->escape_string($this->message),
 361              "toaddress" => $db->escape_string($this->to),
 362              "fromaddress" => $db->escape_string($this->from),
 363              "dateline" => TIME_NOW,
 364              "error" => $db->escape_string($error),
 365              "smtperror" => $db->escape_string($this->data),
 366              "smtpcode" => (int)$this->code
 367          );
 368          $db->insert_query("mailerrors", $mail_error);
 369  
 370          // Another neat feature would be the ability to notify the site administrator via email - but wait, with email down, how do we do that? How about private message and hope the admin checks their PMs?
 371      }
 372  
 373      /**
 374       * Rids pesky characters from subjects, recipients, from addresses etc (prevents mail injection too)
 375       *
 376       * @param string $string The string being checked
 377       * @return string The cleaned string
 378       */
 379  	function cleanup($string)
 380      {
 381          $string = str_replace(array("\r", "\n", "\r\n"), "", $string);
 382          $string = trim($string);
 383          return $string;
 384      }
 385  
 386      /**
 387       * Converts message text to suit the correct delimiter
 388       * See dev.mybb.com/issues/1735 (Jorge Oliveira)
 389       *
 390       * @param string $text The text being converted
 391       * @return string The converted string
 392       */
 393  	function cleanup_crlf($text)
 394      {
 395          $text = str_replace("\r\n", "\n", $text);
 396          $text = str_replace("\r", "\n", $text);
 397          $text = str_replace("\n", "\r\n", $text);
 398  
 399          return $text;
 400      }
 401  
 402      /**
 403       * Encode a string based on the character set enabled. Used to encode subjects
 404       * and recipients in email messages going out so that they show up correctly
 405       * in email clients.
 406       *
 407       * @param string $string The string to be encoded.
 408       * @return string The encoded string.
 409       */
 410  	function utf8_encode($string)
 411      {
 412          if(strtolower($this->charset) == 'utf-8' && preg_match('/[^\x20-\x7E]/', $string))
 413          {
 414              $chunk_size = 47; // Derived from floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
 415              $len = strlen($string);
 416              $output = '';
 417              $pos = 0;
 418  
 419              while($pos < $len)
 420              {
 421                  $newpos = min($pos + $chunk_size, $len);
 422  
 423                  while(ord($string[$newpos]) >= 0x80 && ord($string[$newpos]) < 0xC0)
 424                  {
 425                      // Reduce len until it's safe to split UTF-8.
 426                      $newpos--;
 427                  }
 428  
 429                  $chunk = substr($string, $pos, $newpos - $pos);
 430                  $pos = $newpos;
 431  
 432                  $output .= " =?UTF-8?B?".base64_encode($chunk)."?=\n";
 433              }
 434              return trim($output);
 435          }
 436          return $string;
 437      }
 438  }


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