| [ Index ] |
PHP Cross Reference of MyBB 1.6.5 |
[Summary view] [Print] [Text view]
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: class_mailhandler.php 5589 2011-09-13 15:08:11Z Tomm $ 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 if($this->parse_format == "html" || $this->parse_format == "both") 192 { 193 $this->set_html_headers($message, $message_text); 194 } 195 else 196 { 197 $this->message = $message; 198 $this->set_plain_headers(); 199 } 200 } 201 202 /** 203 * Sets and formats the email subject. 204 * 205 * @param string subject 206 */ 207 function set_subject($subject) 208 { 209 $this->orig_subject = $this->cleanup($subject); 210 $this->subject = $this->utf8_encode($this->orig_subject); 211 } 212 213 /** 214 * Sets and formats the recipient address. 215 * 216 * @param string to 217 */ 218 function set_to($to) 219 { 220 $to = $this->cleanup($to); 221 222 $this->to = $this->cleanup($to); 223 } 224 225 /** 226 * Sets the plain headers, text/plain 227 */ 228 function set_plain_headers() 229 { 230 $this->headers .= "Content-Type: text/plain; charset={$this->charset}{$this->delimiter}"; 231 } 232 233 /** 234 * Sets the alternative headers, text/html and text/plain. 235 * 236 * @param string message 237 */ 238 function set_html_headers($message, $message_text="") 239 { 240 if(!$message_text && $this->parse_format == 'both') 241 { 242 $message_text = strip_tags($message); 243 } 244 245 if($this->parse_format == 'both') 246 { 247 $mime_boundary = "=_NextPart".md5(TIME_NOW); 248 249 $this->headers .= "Content-Type: multipart/alternative; boundary=\"{$mime_boundary}\"{$this->delimiter}"; 250 $this->message = "This is a multi-part message in MIME format.{$this->delimiter}{$this->delimiter}"; 251 252 $this->message .= "--{$mime_boundary}{$this->delimiter}"; 253 $this->message .= "Content-Type: text/plain; charset=\"{$this->charset}\"{$this->delimiter}"; 254 $this->message .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}"; 255 $this->message .= $message_text."{$this->delimiter}{$this->delimiter}"; 256 257 $this->message .= "--{$mime_boundary}{$this->delimiter}"; 258 259 $this->message .= "Content-Type: text/html; charset=\"{$this->charset}\"{$this->delimiter}"; 260 $this->message .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}"; 261 $this->message .= $message."{$this->delimiter}{$this->delimiter}"; 262 263 $this->message .= "--{$mime_boundary}--{$this->delimiter}{$this->delimiter}"; 264 } 265 else 266 { 267 $this->headers .= "Content-Type: text/html; charset=\"{$this->charset}\"{$this->delimiter}"; 268 $this->headers .= "Content-Transfer-Encoding: 8bit{$this->delimiter}{$this->delimiter}"; 269 $this->message = $message."{$this->delimiter}{$this->delimiter}"; 270 } 271 } 272 273 /** 274 * Sets the common headers. 275 */ 276 function set_common_headers() 277 { 278 // Build mail headers 279 $this->headers .= "From: {$this->from}{$this->delimiter}"; 280 281 if($this->return_email) 282 { 283 $this->headers .= "Return-Path: {$this->return_email}{$this->delimiter}"; 284 $this->headers .= "Reply-To: {$this->return_email}{$this->delimiter}"; 285 } 286 287 if(isset($_SERVER['SERVER_NAME'])) 288 { 289 $http_host = $_SERVER['SERVER_NAME']; 290 } 291 else if(isset($_SERVER['HTTP_HOST'])) 292 { 293 $http_host = $_SERVER['HTTP_HOST']; 294 } 295 else 296 { 297 $http_host = "unknown.local"; 298 } 299 300 $msg_id = md5(uniqid(TIME_NOW)) . "@" . $http_host; 301 302 if($mybb->settings['mail_message_id']) 303 { 304 $this->headers .= "Message-ID: <{$msg_id}>{$this->delimiter}"; 305 } 306 $this->headers .= "Content-Transfer-Encoding: 8bit{$this->delimiter}"; 307 $this->headers .= "X-Priority: 3{$this->delimiter}"; 308 $this->headers .= "X-MSMail-Priority: Normal{$this->delimiter}"; 309 $this->headers .= "X-Mailer: MyBB{$this->delimiter}"; 310 $this->headers .= "MIME-Version: 1.0{$this->delimiter}"; 311 } 312 313 /** 314 * Log a fatal error message to the database. 315 * 316 * @param string The error message 317 * @param string Any additional information 318 */ 319 function fatal_error($error) 320 { 321 global $db; 322 323 $mail_error = array( 324 "subject" => $db->escape_string($this->orig_subject), 325 "message" => $db->escape_string($this->message), 326 "toaddress" => $db->escape_string($this->to), 327 "fromaddress" => $db->escape_string($this->from), 328 "dateline" => TIME_NOW, 329 "error" => $db->escape_string($error), 330 "smtperror" => $db->escape_string($this->data), 331 "smtpcode" => intval($this->code) 332 ); 333 $db->insert_query("mailerrors", $mail_error); 334 335 // 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? 336 } 337 338 /** 339 * Rids pesky characters from subjects, recipients, from addresses etc (prevents mail injection too) 340 * 341 * @param string The string being checked 342 * @return string The cleaned string 343 */ 344 function cleanup($string) 345 { 346 $string = str_replace(array("\r", "\n", "\r\n"), "", $string); 347 $string = trim($string); 348 return $string; 349 } 350 351 /** 352 * Encode a string based on the character set enabled. Used to encode subjects 353 * and recipients in email messages going out so that they show up correctly 354 * in email clients. 355 * 356 * @param string The string to be encoded. 357 * @return string The encoded string. 358 */ 359 function utf8_encode($string) 360 { 361 if(strtolower($this->charset) == 'utf-8' && preg_match('/[^\x20-\x7E]/', $string)) 362 { 363 $chunk_size = 47; // Derived from floor((75 - strlen("=?UTF-8?B??=")) * 0.75); 364 $len = strlen($string); 365 $output = ''; 366 $pos = 0; 367 368 while($pos < $len) 369 { 370 $newpos = min($pos + $chunk_size, $len); 371 372 while(ord($string[$newpos]) >= 0x80 && ord($string[$newpos]) < 0xC0) 373 { 374 // Reduce len until it's safe to split UTF-8. 375 $newpos--; 376 } 377 378 $chunk = substr($string, $pos, $newpos - $pos); 379 $pos = $newpos; 380 381 $output .= " =?UTF-8?B?".base64_encode($chunk)."?=\n"; 382 } 383 return trim($output); 384 } 385 return $string; 386 } 387 } 388 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Dec 11 14:16:27 2011 | Cross-referenced by PHPXref 0.7.1 |