[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

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


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