[ Index ] |
PHP Cross Reference of MyBB 1.8.39 |
[Summary view] [Print] [Text view]
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 * Returns the appropriate email address based on the type. 117 * 118 * @param string $type The type of email address to return. 119 * @return string The selected email address. 120 */ 121 function get_email($type='from') 122 { 123 global $mybb; 124 125 if($type === 'reply-to') 126 { 127 if(isset($mybb->settings['returnemail']) && trim($mybb->settings['returnemail'])) 128 { 129 return $mybb->settings['returnemail']; 130 } 131 } 132 133 // Fallback or 'from' case 134 return $mybb->settings['adminemail']; 135 } 136 137 /** 138 * Builds the whole mail. 139 * To be used by the different email classes later. 140 * 141 * @param string $to to email. 142 * @param string $subject subject of email. 143 * @param string $message message of email. 144 * @param string $from from email. 145 * @param string $charset charset of email. 146 * @param string $headers headers of email. 147 * @param string $format format of the email (HTML, plain text, or both?). 148 * @param string $message_text plain text version of the email. 149 * @param string $return_email the return email address. 150 */ 151 function build_message($to, $subject, $message, $from="", $charset="", $headers="", $format="text", $message_text="", $return_email="") 152 { 153 global $parser, $lang, $mybb; 154 155 $this->message = ''; 156 $this->headers = $headers; 157 158 if($from) 159 { 160 $this->from = $from; 161 $this->from_named = $this->from; 162 } 163 else 164 { 165 $this->from = $this->get_email('from'); 166 $this->from_named = '"'.$this->utf8_encode($mybb->settings['bbname']).'"'; 167 $this->from_named .= " <".$this->from.">"; 168 } 169 170 if($return_email) 171 { 172 $this->return_email = $return_email; 173 } 174 else 175 { 176 $this->return_email = $this->get_email('reply-to'); 177 } 178 179 $this->set_to($to); 180 $this->set_subject($subject); 181 182 if($charset) 183 { 184 $this->set_charset($charset); 185 } 186 187 $this->parse_format = $format; 188 $this->set_common_headers(); 189 $this->set_message($message, $message_text); 190 } 191 192 /** 193 * Sets the charset. 194 * 195 * @param string $charset charset 196 */ 197 function set_charset($charset) 198 { 199 global $lang; 200 201 if(empty($charset)) 202 { 203 $this->charset = $lang->settings['charset']; 204 } 205 else 206 { 207 $this->charset = $charset; 208 } 209 } 210 211 /** 212 * Sets and formats the email message. 213 * 214 * @param string $message message 215 * @param string $message_text 216 */ 217 function set_message($message, $message_text="") 218 { 219 $message = $this->cleanup_crlf($message); 220 221 if($message_text) 222 { 223 $message_text = $this->cleanup_crlf($message_text); 224 } 225 226 if($this->parse_format == "html" || $this->parse_format == "both") 227 { 228 $this->set_html_headers($message, $message_text); 229 } 230 else 231 { 232 $this->message = $message; 233 $this->set_plain_headers(); 234 } 235 } 236 237 /** 238 * Sets and formats the email subject. 239 * 240 * @param string $subject 241 */ 242 function set_subject($subject) 243 { 244 $this->orig_subject = $this->cleanup($subject); 245 $this->subject = $this->utf8_encode($this->orig_subject); 246 } 247 248 /** 249 * Sets and formats the recipient address. 250 * 251 * @param string $to 252 */ 253 function set_to($to) 254 { 255 $to = $this->cleanup($to); 256 257 $this->to = $this->cleanup($to); 258 } 259 260 /** 261 * Sets the plain headers, text/plain 262 */ 263 function set_plain_headers() 264 { 265 $this->headers .= "Content-Type: text/plain; charset={$this->charset}{$this->delimiter}"; 266 } 267 268 /** 269 * Sets the alternative headers, text/html and text/plain. 270 * 271 * @param string $message 272 * @param string $message_text 273 */ 274 function set_html_headers($message, $message_text="") 275 { 276 if(!$message_text && $this->parse_format == 'both') 277 { 278 $message_text = strip_tags($message); 279 } 280 281 if($this->parse_format == 'both') 282 { 283 $mime_boundary = "=_NextPart".md5(TIME_NOW); 284 285 $this->headers .= "Content-Type: multipart/alternative; boundary=\"{$mime_boundary}\"{$this->delimiter}"; 286 $this->message = "This is a multi-part message in MIME format.{$this->delimiter}{$this->delimiter}"; 287 288 $this->message .= "--{$mime_boundary}{$this->delimiter}"; 289 $this->message .= "Content-Type: text/plain; charset=\"{$this->charset}\"{$this->delimiter}"; 290 $this->message .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}"; 291 $this->message .= $message_text."{$this->delimiter}{$this->delimiter}"; 292 293 $this->message .= "--{$mime_boundary}{$this->delimiter}"; 294 295 $this->message .= "Content-Type: text/html; charset=\"{$this->charset}\"{$this->delimiter}"; 296 $this->message .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}"; 297 $this->message .= $message."{$this->delimiter}{$this->delimiter}"; 298 299 $this->message .= "--{$mime_boundary}--{$this->delimiter}{$this->delimiter}"; 300 } 301 else 302 { 303 $this->headers .= "Content-Type: text/html; charset=\"{$this->charset}\"{$this->delimiter}"; 304 $this->headers .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}"; 305 $this->message = $message."{$this->delimiter}{$this->delimiter}"; 306 } 307 } 308 309 /** 310 * Sets the common headers. 311 */ 312 function set_common_headers() 313 { 314 global $mybb; 315 316 // Build mail headers 317 $this->headers .= "From: {$this->from_named}{$this->delimiter}"; 318 319 if($this->return_email) 320 { 321 $this->headers .= "Return-Path: {$this->return_email}{$this->delimiter}"; 322 $this->headers .= "Reply-To: {$this->return_email}{$this->delimiter}"; 323 } 324 325 if(isset($_SERVER['SERVER_NAME'])) 326 { 327 $http_host = $_SERVER['SERVER_NAME']; 328 } 329 else if(isset($_SERVER['HTTP_HOST'])) 330 { 331 $http_host = $_SERVER['HTTP_HOST']; 332 } 333 else 334 { 335 $http_host = "unknown.local"; 336 } 337 338 $msg_id = md5(uniqid(TIME_NOW, true)) . "@" . $http_host; 339 340 if($mybb->settings['mail_message_id']) 341 { 342 $this->headers .= "Message-ID: <{$msg_id}>{$this->delimiter}"; 343 } 344 $this->headers .= "Content-Transfer-Encoding: 8bit{$this->delimiter}"; 345 $this->headers .= "X-Priority: 3{$this->delimiter}"; 346 $this->headers .= "X-Mailer: MyBB{$this->delimiter}"; 347 $this->headers .= "MIME-Version: 1.0{$this->delimiter}"; 348 } 349 350 /** 351 * Log a fatal error message to the database. 352 * 353 * @param string $error The error message 354 */ 355 function fatal_error($error) 356 { 357 global $db; 358 359 $mail_error = array( 360 "subject" => $db->escape_string($this->orig_subject), 361 "message" => $db->escape_string($this->message), 362 "toaddress" => $db->escape_string($this->to), 363 "fromaddress" => $db->escape_string($this->from), 364 "dateline" => TIME_NOW, 365 "error" => $db->escape_string($error), 366 "smtperror" => $db->escape_string($this->data), 367 "smtpcode" => (int)$this->code 368 ); 369 $db->insert_query("mailerrors", $mail_error); 370 371 // 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? 372 } 373 374 /** 375 * Rids pesky characters from subjects, recipients, from addresses etc (prevents mail injection too) 376 * 377 * @param string $string The string being checked 378 * @return string The cleaned string 379 */ 380 function cleanup($string) 381 { 382 $string = str_replace(array("\r", "\n", "\r\n"), "", $string); 383 $string = trim($string); 384 return $string; 385 } 386 387 /** 388 * Converts message text to suit the correct delimiter 389 * See dev.mybb.com/issues/1735 (Jorge Oliveira) 390 * 391 * @param string $text The text being converted 392 * @return string The converted string 393 */ 394 function cleanup_crlf($text) 395 { 396 $text = str_replace("\r\n", "\n", $text); 397 $text = str_replace("\r", "\n", $text); 398 $text = str_replace("\n", "\r\n", $text); 399 400 return $text; 401 } 402 403 /** 404 * Encode a string based on the character set enabled. Used to encode subjects 405 * and recipients in email messages going out so that they show up correctly 406 * in email clients. 407 * 408 * @param string $string The string to be encoded. 409 * @return string The encoded string. 410 */ 411 function utf8_encode($string) 412 { 413 if(strtolower($this->charset) == 'utf-8' && preg_match('/[^\x20-\x7E]/', $string)) 414 { 415 $chunk_size = 47; // Derived from floor((75 - strlen("=?UTF-8?B??=")) * 0.75); 416 $len = strlen($string); 417 $output = ''; 418 $pos = 0; 419 420 while($pos < $len) 421 { 422 $newpos = min($pos + $chunk_size, $len); 423 424 if($newpos != $len) 425 { 426 while(ord($string[$newpos]) >= 0x80 && ord($string[$newpos]) < 0xC0) 427 { 428 // Reduce len until it's safe to split UTF-8. 429 $newpos--; 430 } 431 } 432 433 $chunk = substr($string, $pos, $newpos - $pos); 434 $pos = $newpos; 435 436 $output .= " =?UTF-8?B?".base64_encode($chunk)."?=\n"; 437 } 438 return trim($output); 439 } 440 return $string; 441 } 442 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup | Cross-referenced by PHPXref |