| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
<span class="code-comment">* |
|---|
| 4 |
* @package phpBB3 |
|---|
| 5 |
* @version $Id: functions_messenger.php,v 1.100 2007/10/05 14:30:10 acydburn Exp $ |
|---|
| 6 |
* @copyright (c) 2005 phpBB Group |
|---|
| 7 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|---|
| 8 |
* |
|---|
| 9 |
*/ |
|---|
| 10 |
|
|---|
| 11 |
/** |
|---|
| 12 |
* Messenger |
|---|
| 13 |
* @package phpBB3 |
|---|
| 14 |
*/ |
|---|
| 15 |
class messenger |
|---|
| 16 |
{</span> |
|---|
| 17 |
<span class="code-keyword"> var $vars, $msg, $extra_headers, $replyto, $from, $subject; |
|---|
| 18 |
var $addresses = array(); |
|---|
| 19 |
|
|---|
| 20 |
var $mail_priority = MAIL_NORMAL_PRIORITY; |
|---|
| 21 |
var $use_queue = true; |
|---|
| 22 |
|
|---|
| 23 |
var $tpl_obj = NULL; |
|---|
| 24 |
var $tpl_msg = array(); |
|---|
| 25 |
var $eol = "\n"; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* Constructor |
|---|
| 29 |
*/ |
|---|
| 30 |
function messenger($use_queue = true) |
|---|
| 31 |
{ |
|---|
| 32 |
global $config; |
|---|
| 33 |
|
|---|
| 34 |
$this->use_queue = (!$config['email_package_size']) ? false : $use_queue; |
|---|
| 35 |
$this->subject = ''; |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
$this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL; |
|---|
| 39 |
$this->eol = (!$this->eol) ? "\n" : $this->eol; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* Resets all the data (address, template file, etc etc) to default |
|---|
| 44 |
*/ |
|---|
| 45 |
function reset() |
|---|
| 46 |
{ |
|---|
| 47 |
$this->addresses = $this->extra_headers = array(); |
|---|
| 48 |
$this->vars = $this->msg = $this->replyto = $this->from = ''; |
|---|
| 49 |
$this->mail_priority = MAIL_NORMAL_PRIORITY; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
* Sets an email address to send to |
|---|
| 54 |
*/ |
|---|
| 55 |
function to($address, $realname = '') |
|---|
| 56 |
{ |
|---|
| 57 |
global $config; |
|---|
| 58 |
|
|---|
| 59 |
if (!trim($address)) |
|---|
| 60 |
{ |
|---|
| 61 |
return; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
$pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0; |
|---|
| 65 |
|
|---|
| 66 |
$this->addresses['to'][$pos]['email'] = trim($address); |
|---|
| 67 |
|
|---|
| 68 |
if ( trim($realname) ) { |
|---|
| 69 |
|
|---|
| 70 |
if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\') |
|---|
| 71 |
{ |
|---|
| 72 |
$this->addresses['to'][$pos]['name'] = ''; |
|---|
| 73 |
} |
|---|
| 74 |
else |
|---|
| 75 |
{ |
|---|
| 76 |
$this->addresses['to'][$pos]['name'] = trim($realname); |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
* Sets an cc address to send to |
|---|
| 83 |
*/ |
|---|
| 84 |
function cc($address, $realname = '') |
|---|
| 85 |
{ |
|---|
| 86 |
if (!trim($address)) |
|---|
| 87 |
{ |
|---|
| 88 |
return; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
$pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0; |
|---|
| 92 |
$this->addresses['cc'][$pos]['email'] = trim($address); |
|---|
| 93 |
if ( trim($realname) ) { |
|---|
| 94 |
$this->addresses['cc'][$pos]['name'] = trim($realname); |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
* Sets an bcc address to send to |
|---|
| 100 |
*/ |
|---|
| 101 |
function bcc($address, $realname = '') |
|---|
| 102 |
{ |
|---|
| 103 |
if (!trim($address)) |
|---|
| 104 |
{ |
|---|
| 105 |
return; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
$pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0; |
|---|
| 109 |
$this->addresses['bcc'][$pos]['email'] = trim($address); |
|---|
| 110 |
if ( trim($realname) ) { |
|---|
| 111 |
$this->addresses['bcc'][$pos]['name'] = trim($realname); |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
* Sets a im contact to send to |
|---|
| 117 |
*/ |
|---|
| 118 |
function im($address, $realname = '') |
|---|
| 119 |
{ |
|---|
| 120 |
|
|---|
| 121 |
if (!trim($address)) |
|---|
| 122 |
{ |
|---|
| 123 |
return; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
$pos = isset($this->addresses['im']) ? sizeof($this->addresses['im']) : 0; |
|---|
| 127 |
$this->addresses['im'][$pos]['uid'] = trim($address); |
|---|
| 128 |
if ( trim($realname) ) { |
|---|
| 129 |
$this->addresses['im'][$pos]['name'] = trim($realname); |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
* Set the reply to address |
|---|
| 135 |
*/ |
|---|
| 136 |
function replyto($address) |
|---|
| 137 |
{ |
|---|
| 138 |
$this->replyto = trim($address); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
* Set the from address |
|---|
| 143 |
*/ |
|---|
| 144 |
function from($address) |
|---|
| 145 |
{ |
|---|
| 146 |
$this->from = trim($address); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
* set up subject for mail |
|---|
| 151 |
*/ |
|---|
| 152 |
function subject($subject = '') |
|---|
| 153 |
{ |
|---|
| 154 |
$this->subject = trim($subject); |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
* set up msg for mail |
|---|
| 159 |
*/ |
|---|
| 160 |
function msg($msg = '') |
|---|
| 161 |
{ |
|---|
| 162 |
$this->msg = trim($msg); |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
* set up extra mail headers |
|---|
| 167 |
*/ |
|---|
| 168 |
function headers($headers) |
|---|
| 169 |
{ |
|---|
| 170 |
$this->extra_headers[] = trim($headers); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
* Set the email priority |
|---|
| 175 |
*/ |
|---|
| 176 |
function set_mail_priority($priority = MAIL_NORMAL_PRIORITY) |
|---|
| 177 |
{ |
|---|
| 178 |
$this->mail_priority = $priority; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
* Set email template to use |
|---|
| 183 |
*/ |
|---|
| 184 |
function template($template_file, $template_lang = '') |
|---|
| 185 |
{ |
|---|
| 186 |
global $config, $root_path; |
|---|
| 187 |
|
|---|
| 188 |
if (!trim($template_file)) |
|---|
| 189 |
{ |
|---|
| 190 |
trigger_error('No template file set', E_USER_ERROR); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
if (!trim($template_lang)) |
|---|
| 194 |
{ |
|---|
| 195 |
$template_lang = $config['default_lang']; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
if (empty($this->tpl_msg[$template_lang . $template_file])) |
|---|
| 199 |
{ |
|---|
| 200 |
$tpl_file = "{$root_path}languages/lang_$template_lang/email/$template_file.txt"; |
|---|
| 201 |
|
|---|
| 202 |
if (!file_exists($tpl_file)) |
|---|
| 203 |
{ |
|---|
| 204 |
trigger_error("Could not find email template file [ $tpl_file ]", E_USER_ERROR); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
if (($data = @file_get_contents($tpl_file)) === false) |
|---|
| 208 |
{ |
|---|
| 209 |
trigger_error("Failed opening template file [ $tpl_file ]", E_USER_ERROR); |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
$this->tpl_msg[$template_lang . $template_file] = $data; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
$this->msg = $this->tpl_msg[$template_lang . $template_file]; |
|---|
| 216 |
|
|---|
| 217 |
return true; |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
* assign variables to email template |
|---|
| 222 |
*/ |
|---|
| 223 |
function assign_vars($vars) |
|---|
| 224 |
{ |
|---|
| 225 |
$this->vars = (empty($this->vars)) ? $vars : $this->vars + $vars; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
* Send the mail out to the recipients set previously in var $this->addresses |
|---|
| 230 |
*/ |
|---|
| 231 |
function send($method = NOTIFY_EMAIL, $break = false) |
|---|
| 232 |
{ |
|---|
| 233 |
global $config, $lang; |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
$this->vars['U_BOARD'] = (!isset($this->vars['U_BOARD'])) ? generate_base_url() . '/' : $this->vars['U_BOARD']; |
|---|
| 237 |
$this->vars['EMAIL_SIG'] = (!isset($this->vars['EMAIL_SIG'])) ? str_replace('<br />', "\n", "-- \n" . htmlspecialchars($config['board_email_sig'])) : $this->vars['EMAIL_SIG']; |
|---|
| 238 |
$this->vars['SITENAME'] = (!isset($this->vars['SITENAME'])) ? htmlspecialchars($config['sitename']) : $this->vars['SITENAME']; |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
$this->msg = str_replace ("'", "\'", $this->msg); |
|---|
| 242 |
$this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . ((isset(\$this->vars['\\1'])) ? \$this->vars['\\1'] : '') . '", $this->msg); |
|---|
| 243 |
|
|---|
| 244 |
eval("\$this->msg = '$this->msg';"); |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
// do this here because the subject may contain a variable |
|---|
| 248 |
$drop_header = ''; |
|---|
| 249 |
$match = array(); |
|---|
| 250 |
if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match)) |
|---|
| 251 |
{ |
|---|
| 252 |
$this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $lang['no_subject']); |
|---|
| 253 |
$drop_header .= '[\r\n]*?' . preg_quote($match[1], '#'); |
|---|
| 254 |
} |
|---|
| 255 |
else |
|---|
| 256 |
{ |
|---|
| 257 |
$this->subject = (($this->subject != '') ? $this->subject : 'No subject'); |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
if ($drop_header) |
|---|
| 261 |
{ |
|---|
| 262 |
$this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg)); |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
if ($break) |
|---|
| 266 |
{ |
|---|
| 267 |
return true; |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
switch ($method) |
|---|
| 271 |
{ |
|---|
| 272 |
case NOTIFY_EMAIL: |
|---|
| 273 |
$result = $this->msg_email(); |
|---|
| 274 |
break; |
|---|
| 275 |
|
|---|
| 276 |
case NOTIFY_IM: |
|---|
| 277 |
$result = $this->msg_jabber(); |
|---|
| 278 |
break; |
|---|
| 279 |
|
|---|
| 280 |
case NOTIFY_BOTH: |
|---|
| 281 |
$result = $this->msg_email(); |
|---|
| 282 |
$this->msg_jabber(); |
|---|
| 283 |
break; |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
$this->reset(); |
|---|
| 287 |
return $result; |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
* Add error message to log |
|---|
| 292 |
*/ |
|---|
| 293 |
function error($type, $msg) |
|---|
| 294 |
{ |
|---|
| 295 |
global $userdata, $phpEx, $root_path, $config; |
|---|
| 296 |
|
|---|
| 297 |
$calling_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; |
|---|
| 298 |
|
|---|
| 299 |
$message = ''; |
|---|
| 300 |
switch ($type) |
|---|
| 301 |
{ |
|---|
| 302 |
case 'EMAIL': |
|---|
| 303 |
$message = '<strong>EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/mail()') . '</strong>'; |
|---|
| 304 |
break; |
|---|
| 305 |
|
|---|
| 306 |
default: |
|---|
| 307 |
$message = "<strong>$type</strong>"; |
|---|
| 308 |
break; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
$message .= '<br /><em>' . htmlspecialchars($calling_page) . '<em><br /><br />' . $msg . '<br />'; |
|---|
| 312 |
write_log($message, LOG_VIEW_SYSOP); |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
* Save to queue |
|---|
| 317 |
*/ |
|---|
| 318 |
function save_queue() |
|---|
| 319 |
{ |
|---|
| 320 |
global $config; |
|---|
| 321 |
|
|---|
| 322 |
if ($config['email_package_size'] && $this->use_queue && !empty($this->queue)) |
|---|
| 323 |
{ |
|---|
| 324 |
$this->queue->save(); |
|---|
| 325 |
return; |
|---|
| 326 |
} |
|---|
| 327 |
} |
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
* Return email header |
|---|
| 331 |
*/ |
|---|
| 332 |
function build_header($to, $cc, $bcc) |
|---|
| 333 |
{ |
|---|
| 334 |
global $config; |
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
$headers = array(); |
|---|
| 338 |
|
|---|
| 339 |
$headers[] = 'From: ' . $this->from; |
|---|
| 340 |
|
|---|
| 341 |
if ($cc) |
|---|
| 342 |
{ |
|---|
| 343 |
$headers[] = 'Cc: ' . $cc; |
|---|
| 344 |
} |
|---|
| 345 |
|
|---|
| 346 |
if ($bcc) |
|---|
| 347 |
{ |
|---|
| 348 |
$headers[] = 'Bcc: ' . $bcc; |
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
$headers[] = 'Reply-To: ' . $this->replyto; |
|---|
| 352 |
$headers[] = 'Return-Path: <' . $config['sitemail'] . '>'; |
|---|
| 353 |
$headers[] = 'Sender: <' . $config['sitemail'] . '>'; |
|---|
| 354 |
$headers[] = 'MIME-Version: 1.0'; |
|---|
| 355 |
$headers[] = 'Message-ID: <' . md5(uniqid(time())) . '@' . $config['server_name'] . '>'; |
|---|
| 356 |
$headers[] = 'Date: ' . date('r', time()); |
|---|
| 357 |
$headers[] = 'Content-Type: text/plain; charset=UTF-8'; |
|---|
| 358 |
$headers[] = 'Content-Transfer-Encoding: 8bit'; |
|---|
| 359 |
|
|---|
| 360 |
$headers[] = 'X-Priority: ' . $this->mail_priority; |
|---|
| 361 |
$headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High')); |
|---|
| 362 |
$headers[] = 'X-Mailer: TB Dev SZ edition'; |
|---|
| 363 |
$headers[] = 'X-MimeOLE: tb dev sz edition'; |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
if (sizeof($this->extra_headers)) |
|---|
| 367 |
{ |
|---|
| 368 |
$headers = array_merge($headers, $this->extra_headers); |
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
return $headers; |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
* Send out emails |
|---|
| 376 |
*/ |
|---|
| 377 |
function msg_email() |
|---|
| 378 |
{ |
|---|
| 379 |
global $config; |
|---|
| 380 |
|
|---|
| 381 |
if (empty($config['email_enable'])) |
|---|
| 382 |
{ |
|---|
| 383 |
return false; |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
if (empty($this->addresses) || (empty($this->addresses['to']) && empty($this->addresses['cc']) && empty($this->addresses['bcc']))) |
|---|
| 388 |
{ |
|---|
| 389 |
|
|---|
| 390 |
return true; |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
$use_queue = false; |
|---|
| 394 |
if ($config['email_package_size'] && $this->use_queue) |
|---|
| 395 |
{ |
|---|
| 396 |
if (empty($this->queue)) |
|---|
| 397 |
{ |
|---|
| 398 |
$this->queue = new queue(); |
|---|
| 399 |
$this->queue->init('email', $config['email_package_size']); |
|---|
| 400 |
} |
|---|
| 401 |
$use_queue = true; |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
if (empty($this->replyto)) |
|---|
| 405 |
{ |
|---|
| 406 |
$this->replyto = '<' . $config['sitemail'] . '>'; |
|---|
| 407 |
} |
|---|
| 408 |
|
|---|
| 409 |
if (empty($this->from)) |
|---|
| 410 |
{ |
|---|
| 411 |
$this->from = '<' . $config['sitemail'] . '>'; |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
$encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol; |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
$to = $cc = $bcc = ''; |
|---|
| 418 |
foreach ($this->addresses as $type => $address_ary) |
|---|
| 419 |
{ |
|---|
| 420 |
if ($type == 'im') |
|---|
| 421 |
{ |
|---|
| 422 |
continue; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
foreach ($address_ary as $which_ary) |
|---|
| 426 |
{ |
|---|
| 427 |
$$type .= (($$type != '') ? ', ' : '') . ((!empty($which_ary['name'])) ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']); |
|---|
| 428 |
} |
|---|
| 429 |
} |
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
$headers = $this->build_header($to, $cc, $bcc); |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
if (!$use_queue) |
|---|
| 436 |
{ |
|---|
| 437 |
$mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to; |
|---|
| 438 |
$err_msg = ''; |
|---|
| 439 |
|
|---|
| 440 |
if ($config['smtp_delivery']) |
|---|
| 441 |
{ |
|---|
| 442 |
$result = smtpmail($this->addresses, mail_encode($this->subject), $this->msg, $err_msg, $headers); |
|---|
| 443 |
} |
|---|
| 444 |
else |
|---|
| 445 |
{ |
|---|
| 446 |
$result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $this->eol, $err_msg); |
|---|
| 447 |
} |
|---|
| 448 |
|
|---|
| 449 |
if (!$result) |
|---|
| 450 |
{ |
|---|
| 451 |
$this->error('EMAIL', $err_msg); |
|---|
| 452 |
return false; |
|---|
| 453 |
} |
|---|
| 454 |
} |
|---|
| 455 |
else |
|---|
| 456 |
{ |
|---|
| 457 |
$this->queue->put('email', array( |
|---|
| 458 |
'to' => $to, |
|---|
| 459 |
'addresses' => $this->addresses, |
|---|
| 460 |
'subject' => $this->subject, |
|---|
| 461 |
'msg' => $this->msg, |
|---|
| 462 |
'headers' => $headers) |
|---|
| 463 |
); |
|---|
| 464 |
} |
|---|
| 465 |
|
|---|
| 466 |
return true; |
|---|
| 467 |
} |
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
* Send jabber message out |
|---|
| 471 |
*/ |
|---|
| 472 |
function msg_jabber() |
|---|
| 473 |
{ |
|---|
| 474 |
global $config, $db, $user, $root_path, $phpEx; |
|---|
| 475 |
|
|---|
| 476 |
if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password'])) |
|---|
| 477 |
{ |
|---|
| 478 |
return false; |
|---|
| 479 |
} |
|---|
| 480 |
|
|---|
| 481 |
if (empty($this->addresses['im'])) |
|---|
| 482 |
{ |
|---|
| 483 |
|
|---|
| 484 |
return true; |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
$use_queue = false; |
|---|
| 488 |
if ($config['jab_package_size'] && $this->use_queue) |
|---|
| 489 |
{ |
|---|
| 490 |
if (empty($this->queue)) |
|---|
| 491 |
{ |
|---|
| 492 |
$this->queue = new queue(); |
|---|
| 493 |
$this->queue->init('jabber', $config['jab_package_size']); |
|---|
| 494 |
} |
|---|
| 495 |
$use_queue = true; |
|---|
| 496 |
} |
|---|
| 497 |
|
|---|
| 498 |
$addresses = array(); |
|---|
| 499 |
foreach ($this->addresses['im'] as $type => $uid_ary) |
|---|
| 500 |
{ |
|---|
| 501 |
$addresses[] = $uid_ary['uid']; |
|---|
| 502 |
} |
|---|
| 503 |
$addresses = array_unique($addresses); |
|---|
| 504 |
|
|---|
| 505 |
if (!$use_queue) |
|---|
| 506 |
{ |
|---|
| 507 |
include_once($root_path . 'includes/functions_jabber.' . $phpEx); |
|---|
| 508 |
$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']); |
|---|
| 509 |
|
|---|
| 510 |
if (!$this->jabber->connect()) |
|---|
| 511 |
{ |
|---|
| 512 |
$this->error('JABBER', 'Could not connect to Jabber server<br />' . $this->jabber->get_log()); |
|---|
| 513 |
return false; |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
if (!$this->jabber->login()) |
|---|
| 517 |
{ |
|---|
| 518 |
$this->error('JABBER', 'Could not authorise on Jabber server<br />' . $this->jabber->get_log()); |
|---|
| 519 |
return false; |
|---|
| 520 |
} |
|---|
| 521 |
|
|---|
| 522 |
foreach ($addresses as $address) |
|---|
| 523 |
{ |
|---|
| 524 |
$this->jabber->send_message($address, $this->msg, $this->subject); |
|---|
| 525 |
} |
|---|
| 526 |
|
|---|
| 527 |
$this->jabber->disconnect(); |
|---|
| 528 |
} |
|---|
| 529 |
else |
|---|
| 530 |
{ |
|---|
| 531 |
$this->queue->put('jabber', array( |
|---|
| 532 |
'addresses' => $addresses, |
|---|
| 533 |
'subject' => $this->subject, |
|---|
| 534 |
'msg' => $this->msg) |
|---|
| 535 |
); |
|---|
| 536 |
} |
|---|
| 537 |
unset($addresses); |
|---|
| 538 |
return true; |
|---|
| 539 |
} |
|---|
| 540 |
} |
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
<span class="code-comment">* handling email and jabber queue |
|---|
| 544 |
* @package phpBB3 |
|---|
| 545 |
*/ |
|---|
| 546 |
class queue |
|---|
| 547 |
{</span> |
|---|
| 548 |
<span class="code-keyword"> var $data = array(); |
|---|
| 549 |
var $queue_data = array(); |
|---|
| 550 |
var $package_size = 0; |
|---|
| 551 |
var $cache_file = ''; |
|---|
| 552 |
var $eol = "\n"; |
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
* constructor |
|---|
| 556 |
*/ |
|---|
| 557 |
function queue( $cache_file = '' ) |
|---|
| 558 |
{ |
|---|
| 559 |
global $root_path; |
|---|
| 560 |
|
|---|
| 561 |
if ( $cache_file ) { |
|---|
| 562 |
$this->cache_file = $cache_file; |
|---|
| 563 |
} |
|---|
| 564 |
else { |
|---|
| 565 |
$this->cache_file = "{$root_path}cache/queue.php"; |
|---|
| 566 |
} |
|---|
| 567 |
|
|---|
| 568 |
$this->data = array(); |
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
$this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL; |
|---|
| 572 |
$this->eol = (!$this->eol) ? "\n" : $this->eol; |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
* Init a queue object |
|---|
| 577 |
*/ |
|---|
| 578 |
function init($object, $package_size) |
|---|
| 579 |
{ |
|---|
| 580 |
$this->data[$object] = array(); |
|---|
| 581 |
$this->data[$object]['package_size'] = $package_size; |
|---|
| 582 |
$this->data[$object]['data'] = array(); |
|---|
| 583 |
} |
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 |
* Put object in queue |
|---|
| 587 |
*/ |
|---|
| 588 |
function put($object, $scope) |
|---|
| 589 |
{ |
|---|
| 590 |
$this->data[$object]['data'][] = $scope; |
|---|
| 591 |
} |
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
* Process queue |
|---|
| 595 |
* Using lock file |
|---|
| 596 |
*/ |
|---|
| 597 |
function process() |
|---|
| 598 |
{ |
|---|
| 599 |
global $db, $config, $root_path; |
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 |
if ( ( file_exists($this->cache_file . '.lock') && !file_exists($this->cache_file) ) || ( file_exists($this->cache_file . '.lock') && filemtime($this->cache_file . '.lock') < time() - 2 * $config['queue_interval'] ) ) |
|---|
| 603 |
{ |
|---|
| 604 |
@unlink($this->cache_file . '.lock'); |
|---|
| 605 |
return; |
|---|
| 606 |
} |
|---|
| 607 |
|
|---|
| 608 |
if ( file_exists($this->cache_file . '.lock') && file_exists($this->cache_file) ) { |
|---|
| 609 |
return; |
|---|
| 610 |
} |
|---|
| 611 |
|
|---|
| 612 |
@set_time_limit(0); |
|---|
| 613 |
@ignore_user_abort(true); |
|---|
| 614 |
@ini_set('memory_limit', -1); |
|---|
| 615 |
|
|---|
| 616 |
$fp = @fopen($this->cache_file . '.lock', 'wb'); |
|---|
| 617 |
fclose($fp); |
|---|
| 618 |
@chmod($this->cache_file . '.lock', 0777); |
|---|
| 619 |
|
|---|
| 620 |
include($this->cache_file); |
|---|
| 621 |
|
|---|
| 622 |
foreach ($this->queue_data as $object => $data_ary) |
|---|
| 623 |
{ |
|---|
| 624 |
@set_time_limit(0); |
|---|
| 625 |
|
|---|
| 626 |
if (!isset($data_ary['package_size'])) |
|---|
| 627 |
{ |
|---|
| 628 |
$data_ary['package_size'] = 0; |
|---|
| 629 |
} |
|---|
| 630 |
|
|---|
| 631 |
$package_size = $data_ary['package_size']; |
|---|
| 632 |
$num_items = (!$package_size || sizeof($data_ary['data']) < $package_size) ? sizeof($data_ary['data']) : $package_size; |
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
/*if (sizeof($data_ary['data']) > $package_size * 2.5) |
|---|
| 636 |
{ |
|---|
| 637 |
$num_items = $package_size * 2.5; |
|---|
| 638 |
}*/ |
|---|
| 639 |
|
|---|
| 640 |
switch ($object) |
|---|
| 641 |
{ |
|---|
| 642 |
case 'email': |
|---|
| 643 |
|
|---|
| 644 |
if (!$config['email_enable']) |
|---|
| 645 |
{ |
|---|
| 646 |
unset($this->queue_data['email']); |
|---|
| 647 |
continue 2; |
|---|
| 648 |
} |
|---|
| 649 |
break; |
|---|
| 650 |
|
|---|
| 651 |
case 'jabber': |
|---|
| 652 |
if (!$config['jab_enable']) |
|---|
| 653 |
{ |
|---|
| 654 |
unset($this->queue_data['jabber']); |
|---|
| 655 |
continue 2; |
|---|
| 656 |
} |
|---|
| 657 |
|
|---|
| 658 |
include_once($root_path . 'includes/functions_jabber.php'); |
|---|
| 659 |
$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']); |
|---|
| 660 |
|
|---|
| 661 |
if (!$this->jabber->connect()) |
|---|
| 662 |
{ |
|---|
| 663 |
messenger::error('JABBER', 'Could not connect to Jabber server'); |
|---|
| 664 |
continue 2; |
|---|
| 665 |
} |
|---|
| 666 |
|
|---|
| 667 |
if (!$this->jabber->login()) |
|---|
| 668 |
{ |
|---|
| 669 |
messenger::error('JABBER', 'Could not authorise on Jabber server'); |
|---|
| 670 |
continue 2; |
|---|
| 671 |
} |
|---|
| 672 |
|
|---|
| 673 |
break; |
|---|
| 674 |
|
|---|
| 675 |
default: |
|---|
| 676 |
return; |
|---|
| 677 |
} |
|---|
| 678 |
|
|---|
| 679 |
for ($i = 0; $i < $num_items; $i++) |
|---|
| 680 |
{ |
|---|
| 681 |
|
|---|
| 682 |
extract(array_shift($this->queue_data[$object]['data'])); |
|---|
| 683 |
|
|---|
| 684 |
switch ($object) |
|---|
| 685 |
{ |
|---|
| 686 |
case 'email': |
|---|
| 687 |
$err_msg = ''; |
|---|
| 688 |
$to = (!$to) ? 'undisclosed-recipients:;' : $to; |
|---|
| 689 |
|
|---|
| 690 |
if ($config['smtp_delivery']) |
|---|
| 691 |
{ |
|---|
| 692 |
$result = smtpmail($addresses, mail_encode($subject), $msg, $err_msg, $headers); |
|---|
| 693 |
} |
|---|
| 694 |
else |
|---|
| 695 |
{ |
|---|
| 696 |
$result = phpbb_mail($to, $subject, $msg, $headers, $this->eol, $err_msg); |
|---|
| 697 |
} |
|---|
| 698 |
|
|---|
| 699 |
if (!$result) |
|---|
| 700 |
{ |
|---|
| 701 |
@unlink($this->cache_file . '.lock'); |
|---|
| 702 |
|
|---|
| 703 |
messenger::error('EMAIL', $err_msg); |
|---|
| 704 |
continue 2; |
|---|
| 705 |
} |
|---|
| 706 |
break; |
|---|
| 707 |
|
|---|
| 708 |
case 'jabber': |
|---|
| 709 |
foreach ($addresses as $address) |
|---|
| 710 |
{ |
|---|
| 711 |
if ($this->jabber->send_message($address, $msg, $subject) === false) |
|---|
| 712 |
{ |
|---|
| 713 |
messenger::error('JABBER', $this->jabber->get_log()); |
|---|
| 714 |
continue 3; |
|---|
| 715 |
} |
|---|
| 716 |
} |
|---|
| 717 |
break; |
|---|
| 718 |
} |
|---|
| 719 |
} |
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
if (!sizeof($this->queue_data[$object]['data'])) |
|---|
| 723 |
{ |
|---|
| 724 |
unset($this->queue_data[$object]); |
|---|
| 725 |
} |
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
switch ($object) |
|---|
| 729 |
{ |
|---|
| 730 |
case 'jabber': |
|---|
| 731 |
|
|---|
| 732 |
// handled, then disconnect |
|---|
| 733 |
$this->jabber->disconnect(); |
|---|
| 734 |
break; |
|---|
| 735 |
} |
|---|
| 736 |
} |
|---|
| 737 |
|
|---|
| 738 |
if (!sizeof($this->queue_data)) |
|---|
| 739 |
{ |
|---|
| 740 |
@unlink($this->cache_file); |
|---|
| 741 |
} |
|---|
| 742 |
else |
|---|
| 743 |
{ |
|---|
| 744 |
if ($fp = @fopen($this->cache_file, 'wb')) |
|---|
| 745 |
{ |
|---|
| 746 |
@flock($fp, LOCK_EX); |
|---|
| 747 |
fwrite($fp, "<?php\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>"); |
|---|
| 748 |
@flock($fp, LOCK_UN); |
|---|
| 749 |
fclose($fp); |
|---|
| 750 |
|
|---|
| 751 |
@chmod($this->cache_file, 0666); |
|---|
| 752 |
} |
|---|
| 753 |
} |
|---|
| 754 |
|
|---|
| 755 |
@unlink($this->cache_file . '.lock'); |
|---|
| 756 |
} |
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
* Save queue |
|---|
| 760 |
*/ |
|---|
| 761 |
function save() |
|---|
| 762 |
{ |
|---|
| 763 |
global $root_path; |
|---|
| 764 |
|
|---|
| 765 |
if (!sizeof($this->data)) |
|---|
| 766 |
{ |
|---|
| 767 |
return; |
|---|
| 768 |
} |
|---|
| 769 |
|
|---|
| 770 |
if ( sizeof($this->data['email']['data']) > 5 ) { |
|---|
| 771 |
$this->cache_file = "{$root_path}cache/queue_huge.php"; |
|---|
| 772 |
} |
|---|
| 773 |
|
|---|
| 774 |
@set_time_limit(0); |
|---|
| 775 |
@ignore_user_abort(true); |
|---|
| 776 |
@ini_set('memory_limit', -1); |
|---|
| 777 |
|
|---|
| 778 |
if (file_exists($this->cache_file)) |
|---|
| 779 |
{ |
|---|
| 780 |
include($this->cache_file); |
|---|
| 781 |
|
|---|
| 782 |
foreach ($this->queue_data as $object => $data_ary) |
|---|
| 783 |
{ |
|---|
| 784 |
if (isset($this->data[$object]) && sizeof($this->data[$object])) |
|---|
| 785 |
{ |
|---|
| 786 |
$this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']); |
|---|
| 787 |
} |
|---|
| 788 |
else |
|---|
| 789 |
{ |
|---|
| 790 |
$this->data[$object]['data'] = $data_ary['data']; |
|---|
| 791 |
} |
|---|
| 792 |
} |
|---|
| 793 |
} |
|---|
| 794 |
|
|---|
| 795 |
if ($fp = @fopen($this->cache_file, 'w')) |
|---|
| 796 |
{ |
|---|
| 797 |
@flock($fp, LOCK_EX); |
|---|
| 798 |
fwrite($fp, "<?php\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>"); |
|---|
| 799 |
@flock($fp, LOCK_UN); |
|---|
| 800 |
fclose($fp); |
|---|
| 801 |
|
|---|
| 802 |
@chmod($this->cache_file, 0666); |
|---|
| 803 |
} |
|---|
| 804 |
} |
|---|
| 805 |
} |
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
<span class="code-comment">* Replacement or substitute for PHP's mail command |
|---|
| 809 |
*/ |
|---|
| 810 |
function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false)</span> |
|---|
| 811 |
<span class="code-keyword">{ |
|---|
| 812 |
global $config; |
|---|
| 813 |
|
|---|
| 814 |
|
|---|
| 815 |
$message = preg_replace("#(?<!\r)\n#si", "\r\n", $message); |
|---|
| 816 |
|
|---|
| 817 |
if ($headers !== false) |
|---|
| 818 |
{ |
|---|
| 819 |
if (!is_array($headers)) |
|---|
| 820 |
{ |
|---|
| 821 |
|
|---|
| 822 |
$headers = preg_replace('#(?<!\r)\n#si', "\n", $headers); |
|---|
| 823 |
$headers = explode("\n", $headers); |
|---|
| 824 |
} |
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
// but we have to grab bcc and cc headers and treat them differently |
|---|
| 828 |
// Something we really didn't take into consideration originally |
|---|
| 829 |
$headers_used = array(); |
|---|
| 830 |
|
|---|
| 831 |
foreach ($headers as $header) |
|---|
| 832 |
{ |
|---|
| 833 |
if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0) |
|---|
| 834 |
{ |
|---|
| 835 |
continue; |
|---|
| 836 |
} |
|---|
| 837 |
$headers_used[] = trim($header); |
|---|
| 838 |
} |
|---|
| 839 |
|
|---|
| 840 |
$headers = chop(implode("\r\n", $headers_used)); |
|---|
| 841 |
} |
|---|
| 842 |
|
|---|
| 843 |
if (trim($subject) == '') |
|---|
| 844 |
{ |
|---|
| 845 |
$err_msg = (isset($lang['Empty_message'])) ? $lang['Empty_message'] : 'No email subject specified'; |
|---|
| 846 |
return false; |
|---|
| 847 |
} |
|---|
| 848 |
|
|---|
| 849 |
if (trim($message) == '') |
|---|
| 850 |
{ |
|---|
| 851 |
$err_msg = (isset($lang['Empty_subject'])) ? $lang['Empty_subject'] : 'Email message was blank'; |
|---|
| 852 |
return false; |
|---|
| 853 |
} |
|---|
| 854 |
|
|---|
| 855 |
$mail_rcpt = $mail_to = $mail_cc = array(); |
|---|
| 856 |
|
|---|
| 857 |
|
|---|
| 858 |
if (isset($addresses['to']) && sizeof($addresses['to'])) |
|---|
| 859 |
{ |
|---|
| 860 |
foreach ($addresses['to'] as $which_ary) |
|---|
| 861 |
{ |
|---|
| 862 |
$mail_to[] = (!empty($which_ary['name'])) ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; |
|---|
| 863 |
$mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>'; |
|---|
| 864 |
} |
|---|
| 865 |
} |
|---|
| 866 |
|
|---|
| 867 |
if (isset($addresses['bcc']) && sizeof($addresses['bcc'])) |
|---|
| 868 |
{ |
|---|
| 869 |
foreach ($addresses['bcc'] as $which_ary) |
|---|
| 870 |
{ |
|---|
| 871 |
$mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>'; |
|---|
| 872 |
} |
|---|
| 873 |
} |
|---|
| 874 |
|
|---|
| 875 |
if (isset($addresses['cc']) && sizeof($addresses['cc'])) |
|---|
| 876 |
{ |
|---|
| 877 |
foreach ($addresses['cc'] as $which_ary) |
|---|
| 878 |
{ |
|---|
| 879 |
$mail_cc[] = (!empty($which_ary['name'])) ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; |
|---|
| 880 |
$mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>'; |
|---|
| 881 |
} |
|---|
| 882 |
} |
|---|
| 883 |
|
|---|
| 884 |
$smtp = new smtp_class(); |
|---|
| 885 |
|
|---|
| 886 |
$errno = 0; |
|---|
| 887 |
$errstr = ''; |
|---|
| 888 |
|
|---|
| 889 |
$smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']); |
|---|
| 890 |
|
|---|
| 891 |
|
|---|
| 892 |
ob_start(); |
|---|
| 893 |
$smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20); |
|---|
| 894 |
$error_contents = ob_get_clean(); |
|---|
| 895 |
|
|---|
| 896 |
if (!$smtp->socket) |
|---|
| 897 |
{ |
|---|
| 898 |
|
|---|
| 899 |
{ |
|---|
| 900 |
$errstr = utf8_convert_message($errstr); |
|---|
| 901 |
}*/ |
|---|
| 902 |
|
|---|
| 903 |
$err_msg = (isset($lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr"; |
|---|
| 904 |
$err_msg .= ($error_contents) ? '<br /><br />' . htmlspecialchars($error_contents) : ''; |
|---|
| 905 |
return false; |
|---|
| 906 |
} |
|---|
| 907 |
|
|---|
| 908 |
|
|---|
| 909 |
if ($err_msg = $smtp->server_parse('220', __LINE__)) |
|---|
| 910 |
{ |
|---|
| 911 |
$smtp->close_session($err_msg); |
|---|
| 912 |
return false; |
|---|
| 913 |
} |
|---|
| 914 |
|
|---|
| 915 |
|
|---|
| 916 |
if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method'])) |
|---|
| 917 |
{ |
|---|
| 918 |
$smtp->close_session($err_msg); |
|---|
| 919 |
return false; |
|---|
| 920 |
} |
|---|
| 921 |
|
|---|
| 922 |
|
|---|
| 923 |
// Specify who the mail is from.... |
|---|
| 924 |
$smtp->server_send('MAIL FROM:<' . $config['sitemail'] . '>'); |
|---|
| 925 |
if ($err_msg = $smtp->server_parse('250', __LINE__)) |
|---|
| 926 |
{ |
|---|
| 927 |
$smtp->close_session($err_msg); |
|---|
| 928 |
return false; |
|---|
| 929 |
} |
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 |
$to_header = implode(', ', $mail_to); |
|---|
| 933 |
$cc_header = implode(', ', $mail_cc); |
|---|
| 934 |
|
|---|
| 935 |
|
|---|
| 936 |
$rcpt = false; |
|---|
| 937 |
foreach ($mail_rcpt as $type => $mail_to_addresses) |
|---|
| 938 |
{ |
|---|
| 939 |
foreach ($mail_to_addresses as $mail_to_address) |
|---|
| 940 |
{ |
|---|
| 941 |
|
|---|
| 942 |
if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address)) |
|---|
| 943 |
{ |
|---|
| 944 |
$smtp->server_send("RCPT TO:$mail_to_address"); |
|---|
| 945 |
if ($err_msg = $smtp->server_parse('250', __LINE__)) |
|---|
| 946 |
{ |
|---|
| 947 |
|
|---|
| 948 |
if ($smtp->numeric_response_code != 550) |
|---|
| 949 |
{ |
|---|
| 950 |
$smtp->close_session($err_msg); |
|---|
| 951 |
return false; |
|---|
| 952 |
} |
|---|
| 953 |
} |
|---|
| 954 |
else |
|---|
| 955 |
{ |
|---|
| 956 |
$rcpt = true; |
|---|
| 957 |
} |
|---|
| 958 |
} |
|---|
| 959 |
} |
|---|
| 960 |
} |
|---|
| 961 |
|
|---|
| 962 |
|
|---|
| 963 |
if (!$rcpt) |
|---|
| 964 |
{ |
|---|
| 965 |
$err_msg .= '<br /><br />'; |
|---|
| 966 |
$err_msg .= (isset($lang['INVALID_EMAIL_LOG'])) ? sprintf($lang['INVALID_EMAIL_LOG'], htmlspecialchars($mail_to_address)) : '<strong>' . htmlspecialchars($mail_to_address) . '</strong> possibly an invalid email address?'; |
|---|
| 967 |
$smtp->close_session($err_msg); |
|---|
| 968 |
return false; |
|---|
| 969 |
} |
|---|
| 970 |
|
|---|
| 971 |
|
|---|
| 972 |
$smtp->server_send('DATA'); |
|---|
| 973 |
|
|---|
| 974 |
|
|---|
| 975 |
if ($err_msg = $smtp->server_parse('354', __LINE__)) |
|---|
| 976 |
{ |
|---|
| 977 |
$smtp->close_session($err_msg); |
|---|
| 978 |
return false; |
|---|
| 979 |
} |
|---|
| 980 |
|
|---|
| 981 |
|
|---|
| 982 |
$smtp->server_send("Subject: $subject"); |
|---|
| 983 |
|
|---|
| 984 |
|
|---|
| 985 |
$to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header; |
|---|
| 986 |
$smtp->server_send("To: $to_header"); |
|---|
| 987 |
|
|---|
| 988 |
|
|---|
| 989 |
if ($cc_header != '') |
|---|
| 990 |
{ |
|---|
| 991 |
$smtp->server_send("CC: $cc_header"); |
|---|
| 992 |
} |
|---|
| 993 |
|
|---|
| 994 |
|
|---|
| 995 |
if ($headers !== false) |
|---|
| 996 |
{ |
|---|
| 997 |
$smtp->server_send("$headers\r\n"); |
|---|
| 998 |
} |
|---|
| 999 |
|
|---|
| 1000 |
|
|---|
| 1001 |
$smtp->server_send($message); |
|---|
| 1002 |
|
|---|
| 1003 |
|
|---|
| 1004 |
$smtp->server_send('.'); |
|---|
| 1005 |
if ($err_msg = $smtp->server_parse('250', __LINE__)) |
|---|
| 1006 |
{ |
|---|
| 1007 |
$smtp->close_session($err_msg); |
|---|
| 1008 |
return false; |
|---|
| 1009 |
} |
|---|
| 1010 |
|
|---|
| 1011 |
|
|---|
| 1012 |
$smtp->server_send('QUIT'); |
|---|
| 1013 |
$smtp->close_session($err_msg); |
|---|
| 1014 |
|
|---|
| 1015 |
return true; |
|---|
| 1016 |
} |
|---|
| 1017 |
|
|---|
| 1018 |
|
|---|
| 1019 |
<span class="code-comment">* SMTP Class |
|---|
| 1020 |
* Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR) |
|---|
| 1021 |
* See docs/AUTHORS for more details |
|---|
| 1022 |
* @package phpBB3 |
|---|
| 1023 |
*/ |
|---|
| 1024 |
class smtp_class |
|---|
| 1025 |
{</span> |
|---|
| 1026 |
<span class="code-keyword"> var $server_response = ''; |
|---|
| 1027 |
var $socket = 0; |
|---|
| 1028 |
var $responses = array(); |
|---|
| 1029 |
var $commands = array(); |
|---|
| 1030 |
var $numeric_response_code = 0; |
|---|
| 1031 |
|
|---|
| 1032 |
var $backtrace = false; |
|---|
| 1033 |
var $backtrace_log = array(); |
|---|
| 1034 |
|
|---|
| 1035 |
function smtp_class() |
|---|
| 1036 |
{ |
|---|
| 1037 |
|
|---|
| 1038 |
$this->backtrace = true; |
|---|
| 1039 |
$this->backtrace_log = array(); |
|---|
| 1040 |
} |
|---|
| 1041 |
|
|---|
| 1042 |
|
|---|
| 1043 |
* Add backtrace message for debugging |
|---|
| 1044 |
*/ |
|---|
| 1045 |
function add_backtrace($message) |
|---|
| 1046 |
{ |
|---|
| 1047 |
if ($this->backtrace) |
|---|
| 1048 |
{ |
|---|
| 1049 |
$this->backtrace_log[] = htmlspecialchars($message); |
|---|
| 1050 |
} |
|---|
| 1051 |
} |
|---|
| 1052 |
|
|---|
| 1053 |
|
|---|
| 1054 |
* Send command to smtp server |
|---|
| 1055 |
*/ |
|---|
| 1056 |
function server_send($command, $private_info = false) |
|---|
| 1057 |
{ |
|---|
| 1058 |
fputs($this->socket, $command . "\r\n"); |
|---|
| 1059 |
|
|---|
| 1060 |
(!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information'); |
|---|
| 1061 |
|
|---|
| 1062 |
|
|---|
| 1063 |
} |
|---|
| 1064 |
|
|---|
| 1065 |
|
|---|
| 1066 |
* We use the line to give the support people an indication at which command the error occurred |
|---|
| 1067 |
*/ |
|---|
| 1068 |
function server_parse($response, $line) |
|---|
| 1069 |
{ |
|---|
| 1070 |
global $user; |
|---|
| 1071 |
|
|---|
| 1072 |
$this->server_response = ''; |
|---|
| 1073 |
$this->responses = array(); |
|---|
| 1074 |
$this->numeric_response_code = 0; |
|---|
| 1075 |
|
|---|
| 1076 |
while (substr($this->server_response, 3, 1) != ' ') |
|---|
| 1077 |
{ |
|---|
| 1078 |
if (!($this->server_response = fgets($this->socket, 256))) |
|---|
| 1079 |
{ |
|---|
| 1080 |
return (isset($lang['NO_EMAIL_RESPONSE_CODE'])) ? $lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes'; |
|---|
| 1081 |
} |
|---|
| 1082 |
$this->responses[] = substr(rtrim($this->server_response), 4); |
|---|
| 1083 |
$this->numeric_response_code = (int) substr($this->server_response, 0, 3); |
|---|
| 1084 |
|
|---|
| 1085 |
$this->add_backtrace("LINE: $line <- {$this->server_response}"); |
|---|
| 1086 |
} |
|---|
| 1087 |
|
|---|
| 1088 |
if (!(substr($this->server_response, 0, 3) == $response)) |
|---|
| 1089 |
{ |
|---|
| 1090 |
$this->numeric_response_code = (int) substr($this->server_response, 0, 3); |
|---|
| 1091 |
return (isset($lang['EMAIL_SMTP_ERROR_RESPONSE'])) ? sprintf($lang['EMAIL_SMTP_ERROR_RESPONSE'], $line, $this->server_response) : "Ran into problems sending Mail at <strong>Line $line</strong>. Response: $this->server_response"; |
|---|
| 1092 |
} |
|---|
| 1093 |
|
|---|
| 1094 |
return 0; |
|---|
| 1095 |
} |
|---|
| 1096 |
|
|---|
| 1097 |
|
|---|
| 1098 |
* Close session |
|---|
| 1099 |
*/ |
|---|
| 1100 |
function close_session(&$err_msg) |
|---|
| 1101 |
{ |
|---|
| 1102 |
fclose($this->socket); |
|---|
| 1103 |
|
|---|
| 1104 |
if ($this->backtrace) |
|---|
| 1105 |
{ |
|---|
| 1106 |
$message = '<h1>Backtrace</h1><p>' . implode('<br />', $this->backtrace_log) . '</p>'; |
|---|
| 1107 |
$err_msg .= $message; |
|---|
| 1108 |
} |
|---|
| 1109 |
} |
|---|
| 1110 |
|
|---|
| 1111 |
|
|---|
| 1112 |
* Log into server and get possible auth codes if neccessary |
|---|
| 1113 |
*/ |
|---|
| 1114 |
function log_into_server($hostname, $username, $password, $default_auth_method) |
|---|
| 1115 |
{ |
|---|
| 1116 |
global $user; |
|---|
| 1117 |
|
|---|
| 1118 |
$err_msg = ''; |
|---|
| 1119 |
|
|---|
| 1120 |
|
|---|
| 1121 |
$local_host = $user->host; |
|---|
| 1122 |
|
|---|
| 1123 |
if (function_exists('php_uname')) |
|---|
| 1124 |
{ |
|---|
| 1125 |
$local_host = php_uname('n'); |
|---|
| 1126 |
|
|---|
| 1127 |
|
|---|
| 1128 |
if (($addr = @gethostbyname($local_host)) !== $local_host) |
|---|
| 1129 |
{ |
|---|
| 1130 |
|
|---|
| 1131 |
if (($name = @gethostbyaddr($addr)) !== $addr) |
|---|
| 1132 |
{ |
|---|
| 1133 |
$local_host = $name; |
|---|
| 1134 |
} |
|---|
| 1135 |
} |
|---|
| 1136 |
} |
|---|
| 1137 |
|
|---|
| 1138 |
|
|---|
| 1139 |
// have to login ones before we get authenticated |
|---|
| 1140 |
// NOTE: on some configurations the time between an update of the auth database takes so |
|---|
| 1141 |
// long that the first email send does not work. This is not a biggie on a live board (only |
|---|
| 1142 |
// the install mail will most likely fail) - but on a dynamic ip connection this might produce |
|---|
| 1143 |
// severe problems and is not fixable! |
|---|
| 1144 |
if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password) |
|---|
| 1145 |
{ |
|---|
| 1146 |
global $config; |
|---|
| 1147 |
|
|---|
| 1148 |
$errno = 0; |
|---|
| 1149 |
$errstr = ''; |
|---|
| 1150 |
|
|---|
| 1151 |
$this->server_send("QUIT"); |
|---|
| 1152 |
fclose($this->socket); |
|---|
| 1153 |
|
|---|
| 1154 |
$result = $this->pop_before_smtp($hostname, $username, $password); |
|---|
| 1155 |
$username = $password = $default_auth_method = ''; |
|---|
| 1156 |
|
|---|
| 1157 |
|
|---|
| 1158 |
// able to get our ip for matching... |
|---|
| 1159 |
if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10)) |
|---|
| 1160 |
{ |
|---|
| 1161 |
|
|---|
| 1162 |
//{ |
|---|
| 1163 |
// $errstr = utf8_convert_message($errstr); |
|---|
| 1164 |
//} |
|---|
| 1165 |
|
|---|
| 1166 |
$err_msg = (isset($lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr"; |
|---|
| 1167 |
return $err_msg; |
|---|
| 1168 |
} |
|---|
| 1169 |
|
|---|
| 1170 |
|
|---|
| 1171 |
if ($err_msg = $this->server_parse('220', __LINE__)) |
|---|
| 1172 |
{ |
|---|
| 1173 |
$this->close_session($err_msg); |
|---|
| 1174 |
return $err_msg; |
|---|
| 1175 |
} |
|---|
| 1176 |
} |
|---|
| 1177 |
|
|---|
| 1178 |
|
|---|
| 1179 |
$this->server_send("EHLO {$local_host}"); |
|---|
| 1180 |
if ($err_msg = $this->server_parse('250', __LINE__)) |
|---|
| 1181 |
{ |
|---|
| 1182 |
|
|---|
| 1183 |
if ($this->numeric_response_code == 503) |
|---|
| 1184 |
{ |
|---|
| 1185 |
return false; |
|---|
| 1186 |
} |
|---|
| 1187 |
|
|---|
| 1188 |
|
|---|
| 1189 |
$this->server_send("HELO {$local_host}"); |
|---|
| 1190 |
if ($err_msg = $this->server_parse('250', __LINE__)) |
|---|
| 1191 |
{ |
|---|
| 1192 |
return ($this->numeric_response_code == 503) ? false : $err_msg; |
|---|
| 1193 |
} |
|---|
| 1194 |
} |
|---|
| 1195 |
|
|---|
| 1196 |
foreach ($this->responses as $response) |
|---|
| 1197 |
{ |
|---|
| 1198 |
$response = explode(' ', $response); |
|---|
| 1199 |
$response_code = $response[0]; |
|---|
| 1200 |
unset($response[0]); |
|---|
| 1201 |
$this->commands[$response_code] = implode(' ', $response); |
|---|
| 1202 |
} |
|---|
| 1203 |
|
|---|
| 1204 |
|
|---|
| 1205 |
if (!$username || !$password) |
|---|
| 1206 |
{ |
|---|
| 1207 |
return false; |
|---|
| 1208 |
} |
|---|
| 1209 |
|
|---|
| 1210 |
if (!isset($this->commands['AUTH'])) |
|---|
| 1211 |
{ |
|---|
| 1212 |
return (isset($lang['SMTP_NO_AUTH_SUPPORT'])) ? $lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication'; |
|---|
| 1213 |
} |
|---|
| 1214 |
|
|---|
| 1215 |
|
|---|
| 1216 |
$available_methods = explode(' ', $this->commands['AUTH']); |
|---|
| 1217 |
|
|---|
| 1218 |
|
|---|
| 1219 |
$auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5'); |
|---|
| 1220 |
$method = ''; |
|---|
| 1221 |
|
|---|
| 1222 |
if (in_array($default_auth_method, $available_methods)) |
|---|
| 1223 |
{ |
|---|
| 1224 |
$method = $default_auth_method; |
|---|
| 1225 |
} |
|---|
| 1226 |
else |
|---|
| 1227 |
{ |
|---|
| 1228 |
foreach ($auth_methods as $_method) |
|---|
| 1229 |
{ |
|---|
| 1230 |
if (in_array($_method, $available_methods)) |
|---|
| 1231 |
{ |
|---|
| 1232 |
$method = $_method; |
|---|
| 1233 |
break; |
|---|
| 1234 |
} |
|---|
| 1235 |
} |
|---|
| 1236 |
} |
|---|
| 1237 |
|
|---|
| 1238 |
if (!$method) |
|---|
| 1239 |
{ |
|---|
| 1240 |
return (isset($lang['NO_SUPPORTED_AUTH_METHODS'])) ? $lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods'; |
|---|
| 1241 |
} |
|---|
| 1242 |
|
|---|
| 1243 |
$method = strtolower(str_replace('-', '_', $method)); |
|---|
| 1244 |
return $this->$method($username, $password); |
|---|
| 1245 |
} |
|---|
| 1246 |
|
|---|
| 1247 |
|
|---|
| 1248 |
* Pop before smtp authentication |
|---|
| 1249 |
*/ |
|---|
| 1250 |
function pop_before_smtp($hostname, $username, $password) |
|---|
| 1251 |
{ |
|---|
| 1252 |
global $user; |
|---|
| 1253 |
|
|---|
| 1254 |
if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10)) |
|---|
| 1255 |
{ |
|---|
| 1256 |
|
|---|
| 1257 |
//{ |
|---|
| 1258 |
// $errstr = utf8_convert_message($errstr); |
|---|
| 1259 |
//} |
|---|
| 1260 |
|
|---|
| 1261 |
return (isset($lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr"; |
|---|
| 1262 |
} |
|---|
| 1263 |
|
|---|
| 1264 |
$this->server_send("USER $username", true); |
|---|
| 1265 |
if ($err_msg = $this->server_parse('+OK', __LINE__)) |
|---|
| 1266 |
{ |
|---|
| 1267 |
return $err_msg; |
|---|
| 1268 |
} |
|---|
| 1269 |
|
|---|
| 1270 |
$this->server_send("PASS $password", true); |
|---|
| 1271 |
if ($err_msg = $this->server_parse('+OK', __LINE__)) |
|---|
| 1272 |
{ |
|---|
| 1273 |
return $err_msg; |
|---|
| 1274 |
} |
|---|
| 1275 |
|
|---|
| 1276 |
$this->server_send('QUIT'); |
|---|
| 1277 |
fclose($this->socket); |
|---|
| 1278 |
|
|---|
| 1279 |
return false; |
|---|
| 1280 |
} |
|---|
| 1281 |
|
|---|
| 1282 |
|
|---|
| 1283 |
* Plain authentication method |
|---|
| 1284 |
*/ |
|---|
| 1285 |
function plain($username, $password) |
|---|
| 1286 |
{ |
|---|
| 1287 |
$this->server_send('AUTH PLAIN'); |
|---|
| 1288 |
if ($err_msg = $this->server_parse('334', __LINE__)) |
|---|
| 1289 |
{ |
|---|
| 1290 |
return ($this->numeric_response_code == 503) ? false : $err_msg; |
|---|
| 1291 |
} |
|---|
| 1292 |
|
|---|
| 1293 |
$base64_method_plain = base64_encode("\0" . $username . "\0" . $password); |
|---|
| 1294 |
$this->server_send($base64_method_plain, true); |
|---|
| 1295 |
if ($err_msg = $this->server_parse('235', __LINE__)) |
|---|
| 1296 |
{ |
|---|
| 1297 |
return $err_msg; |
|---|
| 1298 |
} |
|---|
| 1299 |
|
|---|
| 1300 |
return false; |
|---|
| 1301 |
} |
|---|
| 1302 |
|
|---|
| 1303 |
|
|---|
| 1304 |
* Login authentication method |
|---|
| 1305 |
*/ |
|---|
| 1306 |
function login($username, $password) |
|---|
| 1307 |
{ |
|---|
| 1308 |
$this->server_send('AUTH LOGIN'); |
|---|
| 1309 |
if ($err_msg = $this->server_parse('334', __LINE__)) |
|---|
| 1310 |
{ |
|---|
| 1311 |
return ($this->numeric_response_code == 503) ? false : $err_msg; |
|---|
| 1312 |
} |
|---|
| 1313 |
|
|---|
| 1314 |
$this->server_send(base64_encode($username), true); |
|---|
| 1315 |
if ($err_msg = $this->server_parse('334', __LINE__)) |
|---|
| 1316 |
{ |
|---|
| 1317 |
return $err_msg; |
|---|
| 1318 |
} |
|---|
| 1319 |
|
|---|
| 1320 |
$this->server_send(base64_encode($password), true); |
|---|
| 1321 |
if ($err_msg = $this->server_parse('235', __LINE__)) |
|---|
| 1322 |
{ |
|---|
| 1323 |
return $err_msg; |
|---|
| 1324 |
} |
|---|
| 1325 |
|
|---|
| 1326 |
return false; |
|---|
| 1327 |
} |
|---|
| 1328 |
|
|---|
| 1329 |
|
|---|
| 1330 |
* cram_md5 authentication method |
|---|
| 1331 |
*/ |
|---|
| 1332 |
function cram_md5($username, $password) |
|---|
| 1333 |
{ |
|---|
| 1334 |
$this->server_send('AUTH CRAM-MD5'); |
|---|
| 1335 |
if ($err_msg = $this->server_parse('334', __LINE__)) |
|---|
| 1336 |
{ |
|---|
| 1337 |
return ($this->numeric_response_code == 503) ? false : $err_msg; |
|---|
| 1338 |
} |
|---|
| 1339 |
|
|---|
| 1340 |
$md5_challenge = base64_decode($this->responses[0]); |
|---|
| 1341 |
$password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password); |
|---|
| 1342 |
$md5_digest = md5((substr($password, 0, 64) ^ str_repeat(chr(0x5C), 64)) . (pack('H32', md5((substr($password, 0, 64) ^ str_repeat(chr(0x36), 64)) . $md5_challenge)))); |
|---|
| 1343 |
|
|---|
| 1344 |
$base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest); |
|---|
| 1345 |
|
|---|
| 1346 |
$this->server_send($base64_method_cram_md5, true); |
|---|
| 1347 |
if ($err_msg = $this->server_parse('235', __LINE__)) |
|---|
| 1348 |
{ |
|---|
| 1349 |
return $err_msg; |
|---|
| 1350 |
} |
|---|
| 1351 |
|
|---|
| 1352 |
return false; |
|---|
| 1353 |
} |
|---|
| 1354 |
|
|---|
| 1355 |
|
|---|
| 1356 |
* digest_md5 authentication method |
|---|
| 1357 |
* A real pain in the *** |
|---|
| 1358 |
*/ |
|---|
| 1359 |
function digest_md5($username, $password) |
|---|
| 1360 |
{ |
|---|
| 1361 |
global $config, $user; |
|---|
| 1362 |
|
|---|
| 1363 |
$this->server_send('AUTH DIGEST-MD5'); |
|---|
| 1364 |
if ($err_msg = $this->server_parse('334', __LINE__)) |
|---|
| 1365 |
{ |
|---|
| 1366 |
return ($this->numeric_response_code == 503) ? false : $err_msg; |
|---|
| 1367 |
} |
|---|
| 1368 |
|
|---|
| 1369 |
$md5_challenge = base64_decode($this->responses[0]); |
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
$tokens = array(); |
|---|
| 1373 |
while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $md5_challenge, $matches)) |
|---|
| 1374 |
{ |
|---|
| 1375 |
|
|---|
| 1376 |
if ($matches[1] == 'opaque' || $matches[1] == 'domain') |
|---|
| 1377 |
{ |
|---|
| 1378 |
$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1); |
|---|
| 1379 |
continue; |
|---|
| 1380 |
} |
|---|
| 1381 |
|
|---|
| 1382 |
|
|---|
| 1383 |
if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param')) |
|---|
| 1384 |
{ |
|---|
| 1385 |
if (is_array($tokens[$matches[1]])) |
|---|
| 1386 |
{ |
|---|
| 1387 |
$tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]); |
|---|
| 1388 |
} |
|---|
| 1389 |
else |
|---|
| 1390 |
{ |
|---|
| 1391 |
$tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2])); |
|---|
| 1392 |
} |
|---|
| 1393 |
} |
|---|
| 1394 |
else if (!empty($tokens[$matches[1]])) |
|---|
| 1395 |
{ |
|---|
| 1396 |
$tokens = array(); |
|---|
| 1397 |
break; |
|---|
| 1398 |
} |
|---|
| 1399 |
else |
|---|
| 1400 |
{ |
|---|
| 1401 |
$tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]); |
|---|
| 1402 |
} |
|---|
| 1403 |
|
|---|
| 1404 |
|
|---|
| 1405 |
$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1); |
|---|
| 1406 |
} |
|---|
| 1407 |
|
|---|
| 1408 |
|
|---|
| 1409 |
if (empty($tokens['realm'])) |
|---|
| 1410 |
{ |
|---|
| 1411 |
$tokens['realm'] = (function_exists('php_uname')) ? php_uname('n') : 'localhost'; |
|---|
| 1412 |
} |
|---|
| 1413 |
|
|---|
| 1414 |
|
|---|
| 1415 |
if (empty($tokens['maxbuf'])) |
|---|
| 1416 |
{ |
|---|
| 1417 |
$tokens['maxbuf'] = 65536; |
|---|
| 1418 |
} |
|---|
| 1419 |
|
|---|
| 1420 |
|
|---|
| 1421 |
if (empty($tokens['nonce']) || empty($tokens['algorithm'])) |
|---|
| 1422 |
{ |
|---|
| 1423 |
$tokens = array(); |
|---|
| 1424 |
} |
|---|
| 1425 |
$md5_challenge = $tokens; |
|---|
| 1426 |
|
|---|
| 1427 |
if (!empty($md5_challenge)) |
|---|
| 1428 |
{ |
|---|
| 1429 |
$str = ''; |
|---|
| 1430 |
for ($i = 0; $i < 32; $i++) |
|---|
| 1431 |
{ |
|---|
| 1432 |
$str .= chr(mt_rand(0, 255)); |
|---|
| 1433 |
} |
|---|
| 1434 |
$cnonce = base64_encode($str); |
|---|
| 1435 |
|
|---|
| 1436 |
$digest_uri = 'smtp/' . $config['smtp_host']; |
|---|
| 1437 |
|
|---|
| 1438 |
$auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce); |
|---|
| 1439 |
$auth_2 = 'AUTHENTICATE:' . $digest_uri; |
|---|
| 1440 |
$response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2))); |
|---|
| 1441 |
|
|---|
| 1442 |
$input_string = sprintf('username="%s",realm="%s",nonce="%s",cnonce="%s",nc="00000001",qop=auth,digest-uri="%s",response=%s,%d', $username, $md5_challenge['realm'], $md5_challenge['nonce'], $cnonce, $digest_uri, $response_value, $md5_challenge['maxbuf']); |
|---|
| 1443 |
} |
|---|
| 1444 |
else |
|---|
| 1445 |
{ |
|---|
| 1446 |
return (isset($lang['INVALID_DIGEST_CHALLENGE'])) ? $lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge'; |
|---|
| 1447 |
} |
|---|
| 1448 |
|
|---|
| 1449 |
$base64_method_digest_md5 = base64_encode($input_string); |
|---|
| 1450 |
$this->server_send($base64_method_digest_md5, true); |
|---|
| 1451 |
if ($err_msg = $this->server_parse('334', __LINE__)) |
|---|
| 1452 |
{ |
|---|
| 1453 |
return $err_msg; |
|---|
| 1454 |
} |
|---|
| 1455 |
|
|---|
| 1456 |
$this->server_send(' '); |
|---|
| 1457 |
if ($err_msg = $this->server_parse('235', __LINE__)) |
|---|
| 1458 |
{ |
|---|
| 1459 |
return $err_msg; |
|---|
| 1460 |
} |
|---|
| 1461 |
|
|---|
| 1462 |
return false; |
|---|
| 1463 |
} |
|---|
| 1464 |
} |
|---|
| 1465 |
|
|---|
| 1466 |
|
|---|
| 1467 |
<span class="code-comment">* Encodes the given string for proper display in UTF-8. |
|---|
| 1468 |
* |
|---|
| 1469 |
* This version is using base64 encoded data. The downside of this |
|---|
| 1470 |
* is if the mail client does not understand this encoding the user |
|---|
| 1471 |
* is basically doomed with an unreadable subject. |
|---|
| 1472 |
* |
|---|
| 1473 |
* Please note that this version fully supports RFC 2045 section 6.8. |
|---|
| 1474 |
* |
|---|
| 1475 |
* @param string $eol End of line we are using (optional to be backwards compatible) |
|---|
| 1476 |
*/ |
|---|
| 1477 |
function mail_encode($str, $eol = "\r\n")</span> |
|---|
| 1478 |
<span class="code-keyword">{ |
|---|
| 1479 |
|
|---|
| 1480 |
$start = "=?UTF-8?B?"; |
|---|
| 1481 |
$end = "?="; |
|---|
| 1482 |
$delimiter = "$eol "; |
|---|
| 1483 |
|
|---|
| 1484 |
|
|---|
| 1485 |
$split_length = 60; |
|---|
| 1486 |
$encoded_str = base64_encode($str); |
|---|
| 1487 |
|
|---|
| 1488 |
|
|---|
| 1489 |
if (strlen($encoded_str) <= $split_length) |
|---|
| 1490 |
{ |
|---|
| 1491 |
return $start . $encoded_str . $end; |
|---|
| 1492 |
} |
|---|
| 1493 |
|
|---|
| 1494 |
|
|---|
| 1495 |
if (strlen($str) === utf_strlen($str)) |
|---|
| 1496 |
{ |
|---|
| 1497 |
return $start . implode($end . $delimiter . $start, str_split($encoded_str, $split_length)) . $end; |
|---|
| 1498 |
} |
|---|
| 1499 |
|
|---|
| 1500 |
|
|---|
| 1501 |
$array = utf_str_split($str); |
|---|
| 1502 |
$str = ''; |
|---|
| 1503 |
|
|---|
| 1504 |
while (sizeof($array)) |
|---|
| 1505 |
{ |
|---|
| 1506 |
$text = ''; |
|---|
| 1507 |
|
|---|
| 1508 |
while (sizeof($array) && intval((strlen($text . $array[0]) + 2) / 3) << 2 <= $split_length) |
|---|
| 1509 |
{ |
|---|
| 1510 |
$text .= array_shift($array); |
|---|
| 1511 |
} |
|---|
| 1512 |
|
|---|
| 1513 |
$str .= $start . base64_encode($text) . $end . $delimiter; |
|---|
| 1514 |
} |
|---|
| 1515 |
|
|---|
| 1516 |
return substr($str, 0, -strlen($delimiter)); |
|---|
| 1517 |
} |
|---|
| 1518 |
|
|---|
| 1519 |
|
|---|
| 1520 |
<span class="code-comment">* Wrapper for sending out emails with the PHP's mail function |
|---|
| 1521 |
*/ |
|---|
| 1522 |
function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg)</span> |
|---|
| 1523 |
<span class="code-keyword">{ |
|---|
| 1524 |
global $config; |
|---|
| 1525 |
|
|---|
| 1526 |
|
|---|
| 1527 |
// Reference: http://bugs.php.net/bug.php?id=15841 |
|---|
| 1528 |
$headers = implode($eol, $headers); |
|---|
| 1529 |
|
|---|
| 1530 |
ob_start(); |
|---|
| 1531 |
|
|---|
| 1532 |
// Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8. |
|---|
| 1533 |
// Because PHP can't decide what is wanted we revert back to the non-RFC-compliant way of separating by one space (Use '' as parameter to mail_encode() results in SPACE used) |
|---|
| 1534 |
$result = $config['email_function_name']($to, mail_encode($subject, ''), $msg, $headers); |
|---|
| 1535 |
$err_msg = ob_get_clean(); |
|---|
| 1536 |
|
|---|
| 1537 |
return $result; |
|---|
| 1538 |
} |
|---|
| 1539 |
|
|---|
| 1540 |
?> |
|---|