[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * This file is part of the phpBB Forum Software package. 5 * 6 * @copyright (c) phpBB Limited <https://www.phpbb.com> 7 * @license GNU General Public License, version 2 (GPL-2.0) 8 * 9 * For full copyright and license information, please see 10 * the docs/CREDITS.txt file. 11 * 12 */ 13 14 /** 15 * @ignore 16 */ 17 if (!defined('IN_PHPBB')) 18 { 19 exit; 20 } 21 22 /** 23 * Messenger 24 */ 25 class messenger 26 { 27 var $msg, $replyto, $from, $subject; 28 var $addresses = array(); 29 var $extra_headers = array(); 30 31 var $mail_priority = MAIL_NORMAL_PRIORITY; 32 var $use_queue = true; 33 34 /** @var \phpbb\template\template */ 35 protected $template; 36 37 /** 38 * Constructor 39 */ 40 function __construct($use_queue = true) 41 { 42 global $config; 43 44 $this->use_queue = (!$config['email_package_size']) ? false : $use_queue; 45 $this->subject = ''; 46 } 47 48 /** 49 * Resets all the data (address, template file, etc etc) to default 50 */ 51 function reset() 52 { 53 $this->addresses = $this->extra_headers = array(); 54 $this->msg = $this->replyto = $this->from = ''; 55 $this->mail_priority = MAIL_NORMAL_PRIORITY; 56 } 57 58 /** 59 * Set addresses for to/im as available 60 * 61 * @param array $user User row 62 */ 63 function set_addresses($user) 64 { 65 if (isset($user['user_email']) && $user['user_email']) 66 { 67 $this->to($user['user_email'], (isset($user['username']) ? $user['username'] : '')); 68 } 69 70 if (isset($user['user_jabber']) && $user['user_jabber']) 71 { 72 $this->im($user['user_jabber'], (isset($user['username']) ? $user['username'] : '')); 73 } 74 } 75 76 /** 77 * Sets an email address to send to 78 */ 79 function to($address, $realname = '') 80 { 81 global $config; 82 83 if (!trim($address)) 84 { 85 return; 86 } 87 88 $pos = isset($this->addresses['to']) ? count($this->addresses['to']) : 0; 89 90 $this->addresses['to'][$pos]['email'] = trim($address); 91 92 // If empty sendmail_path on windows, PHP changes the to line 93 if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\') 94 { 95 $this->addresses['to'][$pos]['name'] = ''; 96 } 97 else 98 { 99 $this->addresses['to'][$pos]['name'] = trim($realname); 100 } 101 } 102 103 /** 104 * Sets an cc address to send to 105 */ 106 function cc($address, $realname = '') 107 { 108 if (!trim($address)) 109 { 110 return; 111 } 112 113 $pos = isset($this->addresses['cc']) ? count($this->addresses['cc']) : 0; 114 $this->addresses['cc'][$pos]['email'] = trim($address); 115 $this->addresses['cc'][$pos]['name'] = trim($realname); 116 } 117 118 /** 119 * Sets an bcc address to send to 120 */ 121 function bcc($address, $realname = '') 122 { 123 if (!trim($address)) 124 { 125 return; 126 } 127 128 $pos = isset($this->addresses['bcc']) ? count($this->addresses['bcc']) : 0; 129 $this->addresses['bcc'][$pos]['email'] = trim($address); 130 $this->addresses['bcc'][$pos]['name'] = trim($realname); 131 } 132 133 /** 134 * Sets a im contact to send to 135 */ 136 function im($address, $realname = '') 137 { 138 // IM-Addresses could be empty 139 if (!trim($address)) 140 { 141 return; 142 } 143 144 $pos = isset($this->addresses['im']) ? count($this->addresses['im']) : 0; 145 $this->addresses['im'][$pos]['uid'] = trim($address); 146 $this->addresses['im'][$pos]['name'] = trim($realname); 147 } 148 149 /** 150 * Set the reply to address 151 */ 152 function replyto($address) 153 { 154 $this->replyto = trim($address); 155 } 156 157 /** 158 * Set the from address 159 */ 160 function from($address) 161 { 162 $this->from = trim($address); 163 } 164 165 /** 166 * set up subject for mail 167 */ 168 function subject($subject = '') 169 { 170 $this->subject = trim($subject); 171 } 172 173 /** 174 * set up extra mail headers 175 */ 176 function headers($headers) 177 { 178 $this->extra_headers[] = trim($headers); 179 } 180 181 /** 182 * Adds X-AntiAbuse headers 183 * 184 * @param \phpbb\config\config $config Config object 185 * @param \phpbb\user $user User object 186 * @return void 187 */ 188 function anti_abuse_headers($config, $user) 189 { 190 $this->headers('X-AntiAbuse: Board servername - ' . mail_encode($config['server_name'])); 191 $this->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']); 192 $this->headers('X-AntiAbuse: Username - ' . mail_encode($user->data['username'])); 193 $this->headers('X-AntiAbuse: User IP - ' . $user->ip); 194 } 195 196 /** 197 * Set the email priority 198 */ 199 function set_mail_priority($priority = MAIL_NORMAL_PRIORITY) 200 { 201 $this->mail_priority = $priority; 202 } 203 204 /** 205 * Set email template to use 206 */ 207 function template($template_file, $template_lang = '', $template_path = '', $template_dir_prefix = '') 208 { 209 global $config, $phpbb_root_path, $user; 210 211 $template_dir_prefix = (!$template_dir_prefix || $template_dir_prefix[0] === '/') ? $template_dir_prefix : '/' . $template_dir_prefix; 212 213 $this->setup_template(); 214 215 if (!trim($template_file)) 216 { 217 trigger_error('No template file for emailing set.', E_USER_ERROR); 218 } 219 220 if (!trim($template_lang)) 221 { 222 // fall back to board default language if the user's language is 223 // missing $template_file. If this does not exist either, 224 // $this->template->set_filenames will do a trigger_error 225 $template_lang = basename($config['default_lang']); 226 } 227 228 $ext_template_paths = array( 229 array( 230 'name' => $template_lang . '_email', 231 'ext_path' => 'language/' . $template_lang . '/email' . $template_dir_prefix, 232 ), 233 ); 234 235 if ($template_path) 236 { 237 $template_paths = array( 238 $template_path . $template_dir_prefix, 239 ); 240 } 241 else 242 { 243 $template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; 244 $template_path .= $template_lang . '/email'; 245 246 $template_paths = array( 247 $template_path . $template_dir_prefix, 248 ); 249 250 $board_language = basename($config['default_lang']); 251 252 // we can only specify default language fallback when the path is not a custom one for which we 253 // do not know the default language alternative 254 if ($template_lang !== $board_language) 255 { 256 $fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; 257 $fallback_template_path .= $board_language . '/email'; 258 259 $template_paths[] = $fallback_template_path . $template_dir_prefix; 260 261 $ext_template_paths[] = array( 262 'name' => $board_language . '_email', 263 'ext_path' => 'language/' . $board_language . '/email' . $template_dir_prefix, 264 ); 265 } 266 // If everything fails just fall back to en template 267 if ($template_lang !== 'en' && $board_language !== 'en') 268 { 269 $fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; 270 $fallback_template_path .= 'en/email'; 271 272 $template_paths[] = $fallback_template_path . $template_dir_prefix; 273 274 $ext_template_paths[] = array( 275 'name' => 'en_email', 276 'ext_path' => 'language/en/email' . $template_dir_prefix, 277 ); 278 } 279 } 280 281 $this->set_template_paths($ext_template_paths, $template_paths); 282 283 $this->template->set_filenames(array( 284 'body' => $template_file . '.txt', 285 )); 286 287 return true; 288 } 289 290 /** 291 * assign variables to email template 292 */ 293 function assign_vars($vars) 294 { 295 $this->setup_template(); 296 297 $this->template->assign_vars($vars); 298 } 299 300 function assign_block_vars($blockname, $vars) 301 { 302 $this->setup_template(); 303 304 $this->template->assign_block_vars($blockname, $vars); 305 } 306 307 /** 308 * Send the mail out to the recipients set previously in var $this->addresses 309 * 310 * @param int $method User notification method NOTIFY_EMAIL|NOTIFY_IM|NOTIFY_BOTH 311 * @param bool $break Flag indicating if the function only formats the subject 312 * and the message without sending it 313 * 314 * @return bool 315 */ 316 function send($method = NOTIFY_EMAIL, $break = false) 317 { 318 global $config, $user, $phpbb_dispatcher; 319 320 // We add some standard variables we always use, no need to specify them always 321 $this->assign_vars(array( 322 'U_BOARD' => generate_board_url(), 323 'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . html_entity_decode($config['board_email_sig'], ENT_COMPAT)), 324 'SITENAME' => html_entity_decode($config['sitename'], ENT_COMPAT), 325 )); 326 327 $subject = $this->subject; 328 $template = $this->template; 329 /** 330 * Event to modify the template before parsing 331 * 332 * @event core.modify_notification_template 333 * @var int method User notification method NOTIFY_EMAIL|NOTIFY_IM|NOTIFY_BOTH 334 * @var bool break Flag indicating if the function only formats the subject 335 * and the message without sending it 336 * @var string subject The message subject 337 * @var \phpbb\template\template template The (readonly) template object 338 * @since 3.2.4-RC1 339 */ 340 $vars = array('method', 'break', 'subject', 'template'); 341 extract($phpbb_dispatcher->trigger_event('core.modify_notification_template', compact($vars))); 342 343 // Parse message through template 344 $message = trim($this->template->assign_display('body')); 345 346 /** 347 * Event to modify notification message text after parsing 348 * 349 * @event core.modify_notification_message 350 * @var int method User notification method NOTIFY_EMAIL|NOTIFY_IM|NOTIFY_BOTH 351 * @var bool break Flag indicating if the function only formats the subject 352 * and the message without sending it 353 * @var string subject The message subject 354 * @var string message The message text 355 * @since 3.1.11-RC1 356 */ 357 $vars = array('method', 'break', 'subject', 'message'); 358 extract($phpbb_dispatcher->trigger_event('core.modify_notification_message', compact($vars))); 359 360 $this->subject = $subject; 361 $this->msg = $message; 362 unset($subject, $message, $template); 363 364 // Because we use \n for newlines in the body message we need to fix line encoding errors for those admins who uploaded email template files in the wrong encoding 365 $this->msg = str_replace("\r\n", "\n", $this->msg); 366 367 // We now try and pull a subject from the email body ... if it exists, 368 // do this here because the subject may contain a variable 369 $drop_header = ''; 370 $match = array(); 371 if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match)) 372 { 373 $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']); 374 $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#'); 375 } 376 else 377 { 378 $this->subject = (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']); 379 } 380 381 if (preg_match('#^(List-Unsubscribe:(.*?))$#m', $this->msg, $match)) 382 { 383 $this->extra_headers[] = $match[1]; 384 $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#'); 385 } 386 387 if ($drop_header) 388 { 389 $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg)); 390 } 391 392 if ($break) 393 { 394 return true; 395 } 396 397 switch ($method) 398 { 399 case NOTIFY_EMAIL: 400 $result = $this->msg_email(); 401 break; 402 403 case NOTIFY_IM: 404 $result = $this->msg_jabber(); 405 break; 406 407 case NOTIFY_BOTH: 408 $result = $this->msg_email(); 409 $this->msg_jabber(); 410 break; 411 } 412 413 $this->reset(); 414 return $result; 415 } 416 417 /** 418 * Add error message to log 419 */ 420 function error($type, $msg) 421 { 422 global $user, $config, $request, $phpbb_log; 423 424 // Session doesn't exist, create it 425 if (!isset($user->session_id) || $user->session_id === '') 426 { 427 $user->session_begin(); 428 } 429 430 $calling_page = html_entity_decode($request->server('REQUEST_URI'), ENT_COMPAT); 431 432 switch ($type) 433 { 434 case 'EMAIL': 435 $message = '<strong>EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/mail()') . '</strong>'; 436 break; 437 438 default: 439 $message = "<strong>$type</strong>"; 440 break; 441 } 442 443 $message .= '<br /><em>' . htmlspecialchars($calling_page, ENT_COMPAT) . '</em><br /><br />' . $msg . '<br />'; 444 $phpbb_log->add('critical', $user->data['user_id'], $user->ip, 'LOG_ERROR_' . $type, false, array($message)); 445 } 446 447 /** 448 * Save to queue 449 */ 450 function save_queue() 451 { 452 global $config; 453 454 if ($config['email_package_size'] && $this->use_queue && !empty($this->queue)) 455 { 456 $this->queue->save(); 457 return; 458 } 459 } 460 461 /** 462 * Generates a valid message id to be used in emails 463 * 464 * @return string message id 465 */ 466 function generate_message_id() 467 { 468 global $config, $request; 469 470 $domain = ($config['server_name']) ?: $request->server('SERVER_NAME', 'phpbb.generated'); 471 472 return md5(unique_id(time())) . '@' . $domain; 473 } 474 475 /** 476 * Return email header 477 */ 478 function build_header($to, $cc, $bcc) 479 { 480 global $config, $phpbb_dispatcher; 481 482 // We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility 483 $headers = array(); 484 485 $headers[] = 'From: ' . $this->from; 486 487 if ($cc) 488 { 489 $headers[] = 'Cc: ' . $cc; 490 } 491 492 if ($bcc) 493 { 494 $headers[] = 'Bcc: ' . $bcc; 495 } 496 497 $headers[] = 'Reply-To: ' . $this->replyto; 498 $headers[] = 'Return-Path: <' . $config['board_email'] . '>'; 499 $headers[] = 'Sender: <' . $config['board_email'] . '>'; 500 $headers[] = 'MIME-Version: 1.0'; 501 $headers[] = 'Message-ID: <' . $this->generate_message_id() . '>'; 502 $headers[] = 'Date: ' . date('r', time()); 503 $headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed 504 $headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit 505 506 $headers[] = 'X-Priority: ' . $this->mail_priority; 507 $headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High')); 508 $headers[] = 'X-Mailer: phpBB3'; 509 $headers[] = 'X-MimeOLE: phpBB3'; 510 $headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url()); 511 512 /** 513 * Event to modify email header entries 514 * 515 * @event core.modify_email_headers 516 * @var array headers Array containing email header entries 517 * @since 3.1.11-RC1 518 */ 519 $vars = array('headers'); 520 extract($phpbb_dispatcher->trigger_event('core.modify_email_headers', compact($vars))); 521 522 if (count($this->extra_headers)) 523 { 524 $headers = array_merge($headers, $this->extra_headers); 525 } 526 527 return $headers; 528 } 529 530 /** 531 * Send out emails 532 */ 533 function msg_email() 534 { 535 global $config, $phpbb_dispatcher; 536 537 if (empty($config['email_enable'])) 538 { 539 return false; 540 } 541 542 // Addresses to send to? 543 if (empty($this->addresses) || (empty($this->addresses['to']) && empty($this->addresses['cc']) && empty($this->addresses['bcc']))) 544 { 545 // Send was successful. ;) 546 return true; 547 } 548 549 $use_queue = false; 550 if ($config['email_package_size'] && $this->use_queue) 551 { 552 if (empty($this->queue)) 553 { 554 $this->queue = new queue(); 555 $this->queue->init('email', $config['email_package_size']); 556 } 557 $use_queue = true; 558 } 559 560 $contact_name = html_entity_decode($config['board_contact_name'], ENT_COMPAT); 561 $board_contact = (($contact_name !== '') ? '"' . mail_encode($contact_name) . '" ' : '') . '<' . $config['board_contact'] . '>'; 562 563 $break = false; 564 $addresses = $this->addresses; 565 $subject = $this->subject; 566 $msg = $this->msg; 567 /** 568 * Event to send message via external transport 569 * 570 * @event core.notification_message_email 571 * @var bool break Flag indicating if the function return after hook 572 * @var array addresses The message recipients 573 * @var string subject The message subject 574 * @var string msg The message text 575 * @since 3.2.4-RC1 576 */ 577 $vars = array( 578 'break', 579 'addresses', 580 'subject', 581 'msg', 582 ); 583 extract($phpbb_dispatcher->trigger_event('core.notification_message_email', compact($vars))); 584 585 $this->addresses = $addresses; 586 $this->subject = $subject; 587 $this->msg = $msg; 588 unset($addresses, $subject, $msg); 589 590 if ($break) 591 { 592 return true; 593 } 594 595 if (empty($this->replyto)) 596 { 597 $this->replyto = $board_contact; 598 } 599 600 if (empty($this->from)) 601 { 602 $this->from = $board_contact; 603 } 604 605 $encode_eol = $config['smtp_delivery'] || PHP_VERSION_ID >= 80000 ? "\r\n" : PHP_EOL; 606 607 // Build to, cc and bcc strings 608 $to = $cc = $bcc = ''; 609 foreach ($this->addresses as $type => $address_ary) 610 { 611 if ($type == 'im') 612 { 613 continue; 614 } 615 616 foreach ($address_ary as $which_ary) 617 { 618 ${$type} .= ((${$type} != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']); 619 } 620 } 621 622 // Build header 623 $headers = $this->build_header($to, $cc, $bcc); 624 625 // Send message ... 626 if (!$use_queue) 627 { 628 $mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to; 629 $err_msg = ''; 630 631 if ($config['smtp_delivery']) 632 { 633 $result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers); 634 } 635 else 636 { 637 $result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $encode_eol, $err_msg); 638 } 639 640 if (!$result) 641 { 642 $this->error('EMAIL', $err_msg); 643 return false; 644 } 645 } 646 else 647 { 648 $this->queue->put('email', array( 649 'to' => $to, 650 'addresses' => $this->addresses, 651 'subject' => $this->subject, 652 'msg' => $this->msg, 653 'headers' => $headers) 654 ); 655 } 656 657 return true; 658 } 659 660 /** 661 * Send jabber message out 662 */ 663 function msg_jabber() 664 { 665 global $config, $user, $phpbb_root_path, $phpEx; 666 667 if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password'])) 668 { 669 return false; 670 } 671 672 if (empty($this->addresses['im'])) 673 { 674 // Send was successful. ;) 675 return true; 676 } 677 678 $use_queue = false; 679 if ($config['jab_package_size'] && $this->use_queue) 680 { 681 if (empty($this->queue)) 682 { 683 $this->queue = new queue(); 684 $this->queue->init('jabber', $config['jab_package_size']); 685 } 686 $use_queue = true; 687 } 688 689 $addresses = array(); 690 foreach ($this->addresses['im'] as $type => $uid_ary) 691 { 692 $addresses[] = $uid_ary['uid']; 693 } 694 $addresses = array_unique($addresses); 695 696 if (!$use_queue) 697 { 698 include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx); 699 $this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], html_entity_decode($config['jab_password'], ENT_COMPAT), $config['jab_use_ssl'], $config['jab_verify_peer'], $config['jab_verify_peer_name'], $config['jab_allow_self_signed']); 700 701 if (!$this->jabber->connect()) 702 { 703 $this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->jabber->get_log()); 704 return false; 705 } 706 707 if (!$this->jabber->login()) 708 { 709 $this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '<br />' . $this->jabber->get_log()); 710 return false; 711 } 712 713 foreach ($addresses as $address) 714 { 715 $this->jabber->send_message($address, $this->msg, $this->subject); 716 } 717 718 $this->jabber->disconnect(); 719 } 720 else 721 { 722 $this->queue->put('jabber', array( 723 'addresses' => $addresses, 724 'subject' => $this->subject, 725 'msg' => $this->msg) 726 ); 727 } 728 unset($addresses); 729 return true; 730 } 731 732 /** 733 * Setup template engine 734 */ 735 protected function setup_template() 736 { 737 global $phpbb_container, $phpbb_dispatcher; 738 739 if ($this->template instanceof \phpbb\template\template) 740 { 741 return; 742 } 743 744 $template_environment = new \phpbb\template\twig\environment( 745 $phpbb_container->get('config'), 746 $phpbb_container->get('filesystem'), 747 $phpbb_container->get('path_helper'), 748 $phpbb_container->getParameter('core.template.cache_path'), 749 $phpbb_container->get('ext.manager'), 750 new \phpbb\template\twig\loader( 751 $phpbb_container->get('filesystem') 752 ), 753 $phpbb_dispatcher, 754 array() 755 ); 756 $template_environment->setLexer($phpbb_container->get('template.twig.lexer')); 757 758 $this->template = new \phpbb\template\twig\twig( 759 $phpbb_container->get('path_helper'), 760 $phpbb_container->get('config'), 761 new \phpbb\template\context(), 762 $template_environment, 763 $phpbb_container->getParameter('core.template.cache_path'), 764 $phpbb_container->get('user'), 765 $phpbb_container->get('template.twig.extensions.collection'), 766 $phpbb_container->get('ext.manager') 767 ); 768 } 769 770 /** 771 * Set template paths to load 772 */ 773 protected function set_template_paths($path_name, $paths) 774 { 775 $this->setup_template(); 776 777 $this->template->set_custom_style($path_name, $paths); 778 } 779 } 780 781 /** 782 * handling email and jabber queue 783 */ 784 class queue 785 { 786 var $data = array(); 787 var $queue_data = array(); 788 var $package_size = 0; 789 var $cache_file = ''; 790 var $eol = "\n"; 791 792 /** 793 * @var \phpbb\filesystem\filesystem_interface 794 */ 795 protected $filesystem; 796 797 /** 798 * constructor 799 */ 800 function __construct() 801 { 802 global $phpEx, $phpbb_root_path, $phpbb_filesystem, $phpbb_container; 803 804 $this->data = array(); 805 $this->cache_file = $phpbb_container->getParameter('core.cache_dir') . "queue.$phpEx"; 806 $this->filesystem = $phpbb_filesystem; 807 } 808 809 /** 810 * Init a queue object 811 */ 812 function init($object, $package_size) 813 { 814 $this->data[$object] = array(); 815 $this->data[$object]['package_size'] = $package_size; 816 $this->data[$object]['data'] = array(); 817 } 818 819 /** 820 * Put object in queue 821 */ 822 function put($object, $scope) 823 { 824 $this->data[$object]['data'][] = $scope; 825 } 826 827 /** 828 * Process queue 829 * Using lock file 830 */ 831 function process() 832 { 833 global $config, $phpEx, $phpbb_root_path, $user, $phpbb_dispatcher; 834 835 $lock = new \phpbb\lock\flock($this->cache_file); 836 $lock->acquire(); 837 838 // avoid races, check file existence once 839 $have_cache_file = file_exists($this->cache_file); 840 if (!$have_cache_file || $config['last_queue_run'] > time() - $config['queue_interval']) 841 { 842 if (!$have_cache_file) 843 { 844 $config->set('last_queue_run', time(), false); 845 } 846 847 $lock->release(); 848 return; 849 } 850 851 $config->set('last_queue_run', time(), false); 852 853 include($this->cache_file); 854 855 foreach ($this->queue_data as $object => $data_ary) 856 { 857 @set_time_limit(0); 858 859 if (!isset($data_ary['package_size'])) 860 { 861 $data_ary['package_size'] = 0; 862 } 863 864 $package_size = $data_ary['package_size']; 865 $num_items = (!$package_size || count($data_ary['data']) < $package_size) ? count($data_ary['data']) : $package_size; 866 867 /* 868 * This code is commented out because it causes problems on some web hosts. 869 * The core problem is rather restrictive email sending limits. 870 * This code is nly useful if you have no such restrictions from the 871 * web host and the package size setting is wrong. 872 873 // If the amount of emails to be sent is way more than package_size than we need to increase it to prevent backlogs... 874 if (count($data_ary['data']) > $package_size * 2.5) 875 { 876 $num_items = count($data_ary['data']); 877 } 878 */ 879 880 switch ($object) 881 { 882 case 'email': 883 // Delete the email queued objects if mailing is disabled 884 if (!$config['email_enable']) 885 { 886 unset($this->queue_data['email']); 887 continue 2; 888 } 889 break; 890 891 case 'jabber': 892 if (!$config['jab_enable']) 893 { 894 unset($this->queue_data['jabber']); 895 continue 2; 896 } 897 898 include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx); 899 $this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], html_entity_decode($config['jab_password'], ENT_COMPAT), $config['jab_use_ssl'], $config['jab_verify_peer'], $config['jab_verify_peer_name'], $config['jab_allow_self_signed']); 900 901 if (!$this->jabber->connect()) 902 { 903 $messenger = new messenger(); 904 $messenger->error('JABBER', $user->lang['ERR_JAB_CONNECT']); 905 continue 2; 906 } 907 908 if (!$this->jabber->login()) 909 { 910 $messenger = new messenger(); 911 $messenger->error('JABBER', $user->lang['ERR_JAB_AUTH']); 912 continue 2; 913 } 914 915 break; 916 917 default: 918 $lock->release(); 919 return; 920 } 921 922 for ($i = 0; $i < $num_items; $i++) 923 { 924 // Make variables available... 925 extract(array_shift($this->queue_data[$object]['data'])); 926 927 switch ($object) 928 { 929 case 'email': 930 $break = false; 931 /** 932 * Event to send message via external transport 933 * 934 * @event core.notification_message_process 935 * @var bool break Flag indicating if the function return after hook 936 * @var array addresses The message recipients 937 * @var string subject The message subject 938 * @var string msg The message text 939 * @since 3.2.4-RC1 940 */ 941 $vars = array( 942 'break', 943 'addresses', 944 'subject', 945 'msg', 946 ); 947 extract($phpbb_dispatcher->trigger_event('core.notification_message_process', compact($vars))); 948 949 if (!$break) 950 { 951 $err_msg = ''; 952 $to = (!$to) ? 'undisclosed-recipients:;' : $to; 953 954 if ($config['smtp_delivery']) 955 { 956 $result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers); 957 } 958 else 959 { 960 $encode_eol = $config['smtp_delivery'] || PHP_VERSION_ID >= 80000 ? "\r\n" : PHP_EOL; 961 $result = phpbb_mail($to, $subject, $msg, $headers, $encode_eol, $err_msg); 962 } 963 964 if (!$result) 965 { 966 $messenger = new messenger(); 967 $messenger->error('EMAIL', $err_msg); 968 continue 2; 969 } 970 } 971 break; 972 973 case 'jabber': 974 foreach ($addresses as $address) 975 { 976 if ($this->jabber->send_message($address, $msg, $subject) === false) 977 { 978 $messenger = new messenger(); 979 $messenger->error('JABBER', $this->jabber->get_log()); 980 continue 3; 981 } 982 } 983 break; 984 } 985 } 986 987 // No more data for this object? Unset it 988 if (!count($this->queue_data[$object]['data'])) 989 { 990 unset($this->queue_data[$object]); 991 } 992 993 // Post-object processing 994 switch ($object) 995 { 996 case 'jabber': 997 // Hang about a couple of secs to ensure the messages are 998 // handled, then disconnect 999 $this->jabber->disconnect(); 1000 break; 1001 } 1002 } 1003 1004 if (!count($this->queue_data)) 1005 { 1006 @unlink($this->cache_file); 1007 } 1008 else 1009 { 1010 if ($fp = @fopen($this->cache_file, 'wb')) 1011 { 1012 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>"); 1013 fclose($fp); 1014 1015 if (function_exists('opcache_invalidate')) 1016 { 1017 @opcache_invalidate($this->cache_file); 1018 } 1019 1020 try 1021 { 1022 $this->filesystem->phpbb_chmod($this->cache_file, \phpbb\filesystem\filesystem_interface::CHMOD_READ | \phpbb\filesystem\filesystem_interface::CHMOD_WRITE); 1023 } 1024 catch (\phpbb\filesystem\exception\filesystem_exception $e) 1025 { 1026 // Do nothing 1027 } 1028 } 1029 } 1030 1031 $lock->release(); 1032 } 1033 1034 /** 1035 * Save queue 1036 */ 1037 function save() 1038 { 1039 if (!count($this->data)) 1040 { 1041 return; 1042 } 1043 1044 $lock = new \phpbb\lock\flock($this->cache_file); 1045 $lock->acquire(); 1046 1047 if (file_exists($this->cache_file)) 1048 { 1049 include($this->cache_file); 1050 1051 foreach ($this->queue_data as $object => $data_ary) 1052 { 1053 if (isset($this->data[$object]) && count($this->data[$object])) 1054 { 1055 $this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']); 1056 } 1057 else 1058 { 1059 $this->data[$object]['data'] = $data_ary['data']; 1060 } 1061 } 1062 } 1063 1064 if ($fp = @fopen($this->cache_file, 'w')) 1065 { 1066 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>"); 1067 fclose($fp); 1068 1069 if (function_exists('opcache_invalidate')) 1070 { 1071 @opcache_invalidate($this->cache_file); 1072 } 1073 1074 try 1075 { 1076 $this->filesystem->phpbb_chmod($this->cache_file, \phpbb\filesystem\filesystem_interface::CHMOD_READ | \phpbb\filesystem\filesystem_interface::CHMOD_WRITE); 1077 } 1078 catch (\phpbb\filesystem\exception\filesystem_exception $e) 1079 { 1080 // Do nothing 1081 } 1082 1083 $this->data = array(); 1084 } 1085 1086 $lock->release(); 1087 } 1088 } 1089 1090 /** 1091 * Replacement or substitute for PHP's mail command 1092 */ 1093 function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false) 1094 { 1095 global $config, $user; 1096 1097 // Fix any bare linefeeds in the message to make it RFC821 Compliant. 1098 $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message); 1099 1100 if ($headers !== false) 1101 { 1102 if (!is_array($headers)) 1103 { 1104 // Make sure there are no bare linefeeds in the headers 1105 $headers = preg_replace('#(?<!\r)\n#si', "\n", $headers); 1106 $headers = explode("\n", $headers); 1107 } 1108 1109 // Ok this is rather confusing all things considered, 1110 // but we have to grab bcc and cc headers and treat them differently 1111 // Something we really didn't take into consideration originally 1112 $headers_used = array(); 1113 1114 foreach ($headers as $header) 1115 { 1116 if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0) 1117 { 1118 continue; 1119 } 1120 $headers_used[] = trim($header); 1121 } 1122 1123 $headers = chop(implode("\r\n", $headers_used)); 1124 } 1125 1126 if (trim($subject) == '') 1127 { 1128 $err_msg = (isset($user->lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified'; 1129 return false; 1130 } 1131 1132 if (trim($message) == '') 1133 { 1134 $err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank'; 1135 return false; 1136 } 1137 1138 $mail_rcpt = $mail_to = $mail_cc = array(); 1139 1140 // Build correct addresses for RCPT TO command and the client side display (TO, CC) 1141 if (isset($addresses['to']) && count($addresses['to'])) 1142 { 1143 foreach ($addresses['to'] as $which_ary) 1144 { 1145 $mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; 1146 $mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>'; 1147 } 1148 } 1149 1150 if (isset($addresses['bcc']) && count($addresses['bcc'])) 1151 { 1152 foreach ($addresses['bcc'] as $which_ary) 1153 { 1154 $mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>'; 1155 } 1156 } 1157 1158 if (isset($addresses['cc']) && count($addresses['cc'])) 1159 { 1160 foreach ($addresses['cc'] as $which_ary) 1161 { 1162 $mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; 1163 $mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>'; 1164 } 1165 } 1166 1167 $smtp = new smtp_class(); 1168 1169 $errno = 0; 1170 $errstr = ''; 1171 1172 $smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']); 1173 1174 // Ok we have error checked as much as we can to this point let's get on it already. 1175 if (!class_exists('\phpbb\error_collector')) 1176 { 1177 global $phpbb_root_path, $phpEx; 1178 include($phpbb_root_path . 'includes/error_collector.' . $phpEx); 1179 } 1180 $collector = new \phpbb\error_collector; 1181 $collector->install(); 1182 1183 $options = array(); 1184 $verify_peer = (bool) $config['smtp_verify_peer']; 1185 $verify_peer_name = (bool) $config['smtp_verify_peer_name']; 1186 $allow_self_signed = (bool) $config['smtp_allow_self_signed']; 1187 $remote_socket = $config['smtp_host'] . ':' . $config['smtp_port']; 1188 1189 // Set ssl context options, see http://php.net/manual/en/context.ssl.php 1190 $options['ssl'] = array('verify_peer' => $verify_peer, 'verify_peer_name' => $verify_peer_name, 'allow_self_signed' => $allow_self_signed); 1191 $socket_context = stream_context_create($options); 1192 1193 $smtp->socket = @stream_socket_client($remote_socket, $errno, $errstr, 20, STREAM_CLIENT_CONNECT, $socket_context); 1194 $collector->uninstall(); 1195 $error_contents = $collector->format_errors(); 1196 1197 if (!$smtp->socket) 1198 { 1199 if ($errstr) 1200 { 1201 $errstr = utf8_convert_message($errstr); 1202 } 1203 1204 $err_msg = (isset($user->lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($user->lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr"; 1205 $err_msg .= ($error_contents) ? '<br /><br />' . htmlspecialchars($error_contents, ENT_COMPAT) : ''; 1206 return false; 1207 } 1208 1209 // Wait for reply 1210 if ($err_msg = $smtp->server_parse('220', __LINE__)) 1211 { 1212 $smtp->close_session($err_msg); 1213 return false; 1214 } 1215 1216 // Let me in. This function handles the complete authentication process 1217 if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], html_entity_decode($config['smtp_password'], ENT_COMPAT), $config['smtp_auth_method'])) 1218 { 1219 $smtp->close_session($err_msg); 1220 return false; 1221 } 1222 1223 // From this point onward most server response codes should be 250 1224 // Specify who the mail is from.... 1225 $smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>'); 1226 if ($err_msg = $smtp->server_parse('250', __LINE__)) 1227 { 1228 $smtp->close_session($err_msg); 1229 return false; 1230 } 1231 1232 // Specify each user to send to and build to header. 1233 $to_header = implode(', ', $mail_to); 1234 $cc_header = implode(', ', $mail_cc); 1235 1236 // Now tell the MTA to send the Message to the following people... [TO, BCC, CC] 1237 $rcpt = false; 1238 foreach ($mail_rcpt as $type => $mail_to_addresses) 1239 { 1240 foreach ($mail_to_addresses as $mail_to_address) 1241 { 1242 // Add an additional bit of error checking to the To field. 1243 if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address)) 1244 { 1245 $smtp->server_send("RCPT TO:$mail_to_address"); 1246 if ($err_msg = $smtp->server_parse('250', __LINE__)) 1247 { 1248 // We continue... if users are not resolved we do not care 1249 if ($smtp->numeric_response_code != 550) 1250 { 1251 $smtp->close_session($err_msg); 1252 return false; 1253 } 1254 } 1255 else 1256 { 1257 $rcpt = true; 1258 } 1259 } 1260 } 1261 } 1262 1263 // We try to send messages even if a few people do not seem to have valid email addresses, but if no one has, we have to exit here. 1264 if (!$rcpt) 1265 { 1266 $user->session_begin(); 1267 $err_msg .= '<br /><br />'; 1268 $err_msg .= (isset($user->lang['INVALID_EMAIL_LOG'])) ? sprintf($user->lang['INVALID_EMAIL_LOG'], htmlspecialchars($mail_to_address, ENT_COMPAT)) : '<strong>' . htmlspecialchars($mail_to_address, ENT_COMPAT) . '</strong> possibly an invalid email address?'; 1269 $smtp->close_session($err_msg); 1270 return false; 1271 } 1272 1273 // Ok now we tell the server we are ready to start sending data 1274 $smtp->server_send('DATA'); 1275 1276 // This is the last response code we look for until the end of the message. 1277 if ($err_msg = $smtp->server_parse('354', __LINE__)) 1278 { 1279 $smtp->close_session($err_msg); 1280 return false; 1281 } 1282 1283 // Send the Subject Line... 1284 $smtp->server_send("Subject: $subject"); 1285 1286 // Now the To Header. 1287 $to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header; 1288 $smtp->server_send("To: $to_header"); 1289 1290 // Now the CC Header. 1291 if ($cc_header != '') 1292 { 1293 $smtp->server_send("CC: $cc_header"); 1294 } 1295 1296 // Now any custom headers.... 1297 if ($headers !== false) 1298 { 1299 $smtp->server_send("$headers\r\n"); 1300 } 1301 1302 // Ok now we are ready for the message... 1303 $smtp->server_send($message); 1304 1305 // Ok the all the ingredients are mixed in let's cook this puppy... 1306 $smtp->server_send('.'); 1307 if ($err_msg = $smtp->server_parse('250', __LINE__)) 1308 { 1309 $smtp->close_session($err_msg); 1310 return false; 1311 } 1312 1313 // Now tell the server we are done and close the socket... 1314 $smtp->server_send('QUIT'); 1315 $smtp->close_session($err_msg); 1316 1317 return true; 1318 } 1319 1320 /** 1321 * SMTP Class 1322 * Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR) 1323 * See docs/AUTHORS for more details 1324 */ 1325 class smtp_class 1326 { 1327 var $server_response = ''; 1328 var $socket = 0; 1329 protected $socket_tls = false; 1330 var $responses = array(); 1331 var $commands = array(); 1332 var $numeric_response_code = 0; 1333 1334 var $backtrace = false; 1335 var $backtrace_log = array(); 1336 1337 function __construct() 1338 { 1339 // Always create a backtrace for admins to identify SMTP problems 1340 $this->backtrace = true; 1341 $this->backtrace_log = array(); 1342 } 1343 1344 /** 1345 * Add backtrace message for debugging 1346 */ 1347 function add_backtrace($message) 1348 { 1349 if ($this->backtrace) 1350 { 1351 $this->backtrace_log[] = utf8_htmlspecialchars($message, ENT_COMPAT); 1352 } 1353 } 1354 1355 /** 1356 * Send command to smtp server 1357 */ 1358 function server_send($command, $private_info = false) 1359 { 1360 fputs($this->socket, $command . "\r\n"); 1361 1362 (!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information'); 1363 1364 // We could put additional code here 1365 } 1366 1367 /** 1368 * We use the line to give the support people an indication at which command the error occurred 1369 */ 1370 function server_parse($response, $line) 1371 { 1372 global $user; 1373 1374 $this->server_response = ''; 1375 $this->responses = array(); 1376 $this->numeric_response_code = 0; 1377 1378 while (substr($this->server_response, 3, 1) != ' ') 1379 { 1380 if (!($this->server_response = fgets($this->socket, 256))) 1381 { 1382 return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes'; 1383 } 1384 $this->responses[] = substr(rtrim($this->server_response), 4); 1385 $this->numeric_response_code = (int) substr($this->server_response, 0, 3); 1386 1387 $this->add_backtrace("LINE: $line <- {$this->server_response}"); 1388 } 1389 1390 if (!(substr($this->server_response, 0, 3) == $response)) 1391 { 1392 $this->numeric_response_code = (int) substr($this->server_response, 0, 3); 1393 return (isset($user->lang['EMAIL_SMTP_ERROR_RESPONSE'])) ? sprintf($user->lang['EMAIL_SMTP_ERROR_RESPONSE'], $line, $this->server_response) : "Ran into problems sending Mail at <strong>Line $line</strong>. Response: $this->server_response"; 1394 } 1395 1396 return 0; 1397 } 1398 1399 /** 1400 * Close session 1401 */ 1402 function close_session(&$err_msg) 1403 { 1404 fclose($this->socket); 1405 1406 if ($this->backtrace) 1407 { 1408 $message = '<h1>Backtrace</h1><p>' . implode('<br />', $this->backtrace_log) . '</p>'; 1409 $err_msg .= $message; 1410 } 1411 } 1412 1413 /** 1414 * Log into server and get possible auth codes if neccessary 1415 */ 1416 function log_into_server($hostname, $username, $password, $default_auth_method) 1417 { 1418 global $user; 1419 1420 // Here we try to determine the *real* hostname (reverse DNS entry preferrably) 1421 if (function_exists('php_uname') && !empty($local_host = php_uname('n'))) 1422 { 1423 // Able to resolve name to IP 1424 if (($addr = @gethostbyname($local_host)) !== $local_host) 1425 { 1426 // Able to resolve IP back to name 1427 if (!empty($name = @gethostbyaddr($addr)) && $name !== $addr) 1428 { 1429 $local_host = $name; 1430 } 1431 } 1432 } 1433 else 1434 { 1435 $local_host = $user->host; 1436 } 1437 1438 // If we are authenticating through pop-before-smtp, we 1439 // have to login ones before we get authenticated 1440 // NOTE: on some configurations the time between an update of the auth database takes so 1441 // long that the first email send does not work. This is not a biggie on a live board (only 1442 // the install mail will most likely fail) - but on a dynamic ip connection this might produce 1443 // severe problems and is not fixable! 1444 if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password) 1445 { 1446 global $config; 1447 1448 $errno = 0; 1449 $errstr = ''; 1450 1451 $this->server_send("QUIT"); 1452 fclose($this->socket); 1453 1454 $this->pop_before_smtp($hostname, $username, $password); 1455 $username = $password = $default_auth_method = ''; 1456 1457 // We need to close the previous session, else the server is not 1458 // able to get our ip for matching... 1459 if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10)) 1460 { 1461 if ($errstr) 1462 { 1463 $errstr = utf8_convert_message($errstr); 1464 } 1465 1466 $err_msg = (isset($user->lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($user->lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr"; 1467 return $err_msg; 1468 } 1469 1470 // Wait for reply 1471 if ($err_msg = $this->server_parse('220', __LINE__)) 1472 { 1473 $this->close_session($err_msg); 1474 return $err_msg; 1475 } 1476 } 1477 1478 $hello_result = $this->hello($local_host); 1479 if (!is_null($hello_result)) 1480 { 1481 return $hello_result; 1482 } 1483 1484 // SMTP STARTTLS (RFC 3207) 1485 if (!$this->socket_tls) 1486 { 1487 $this->socket_tls = $this->starttls(); 1488 1489 if ($this->socket_tls) 1490 { 1491 // Switched to TLS 1492 // RFC 3207: "The client MUST discard any knowledge obtained from the server, [...]" 1493 // So say hello again 1494 $hello_result = $this->hello($local_host); 1495 1496 if (!is_null($hello_result)) 1497 { 1498 return $hello_result; 1499 } 1500 } 1501 } 1502 1503 // If we are not authenticated yet, something might be wrong if no username and passwd passed 1504 if (!$username || !$password) 1505 { 1506 return false; 1507 } 1508 1509 if (!isset($this->commands['AUTH'])) 1510 { 1511 return (isset($user->lang['SMTP_NO_AUTH_SUPPORT'])) ? $user->lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication'; 1512 } 1513 1514 // Get best authentication method 1515 $available_methods = explode(' ', $this->commands['AUTH']); 1516 1517 // Define the auth ordering if the default auth method was not found 1518 $auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5'); 1519 $method = ''; 1520 1521 if (in_array($default_auth_method, $available_methods)) 1522 { 1523 $method = $default_auth_method; 1524 } 1525 else 1526 { 1527 foreach ($auth_methods as $_method) 1528 { 1529 if (in_array($_method, $available_methods)) 1530 { 1531 $method = $_method; 1532 break; 1533 } 1534 } 1535 } 1536 1537 if (!$method) 1538 { 1539 return (isset($user->lang['NO_SUPPORTED_AUTH_METHODS'])) ? $user->lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods'; 1540 } 1541 1542 $method = strtolower(str_replace('-', '_', $method)); 1543 return $this->$method($username, $password); 1544 } 1545 1546 /** 1547 * SMTP EHLO/HELO 1548 * 1549 * @return mixed Null if the authentication process is supposed to continue 1550 * False if already authenticated 1551 * Error message (string) otherwise 1552 */ 1553 protected function hello($hostname) 1554 { 1555 // Try EHLO first 1556 $this->server_send("EHLO $hostname"); 1557 if ($err_msg = $this->server_parse('250', __LINE__)) 1558 { 1559 // a 503 response code means that we're already authenticated 1560 if ($this->numeric_response_code == 503) 1561 { 1562 return false; 1563 } 1564 1565 // If EHLO fails, we try HELO 1566 $this->server_send("HELO $hostname"); 1567 if ($err_msg = $this->server_parse('250', __LINE__)) 1568 { 1569 return ($this->numeric_response_code == 503) ? false : $err_msg; 1570 } 1571 } 1572 1573 foreach ($this->responses as $response) 1574 { 1575 $response = explode(' ', $response); 1576 $response_code = $response[0]; 1577 unset($response[0]); 1578 $this->commands[$response_code] = implode(' ', $response); 1579 } 1580 } 1581 1582 /** 1583 * SMTP STARTTLS (RFC 3207) 1584 * 1585 * @return bool Returns true if TLS was started 1586 * Otherwise false 1587 */ 1588 protected function starttls() 1589 { 1590 global $config; 1591 1592 // allow SMTPS (what was used by phpBB 3.0) if hostname is prefixed with tls:// or ssl:// 1593 if (strpos($config['smtp_host'], 'tls://') === 0 || strpos($config['smtp_host'], 'ssl://') === 0) 1594 { 1595 return true; 1596 } 1597 1598 if (!function_exists('stream_socket_enable_crypto')) 1599 { 1600 return false; 1601 } 1602 1603 if (!isset($this->commands['STARTTLS'])) 1604 { 1605 return false; 1606 } 1607 1608 $this->server_send('STARTTLS'); 1609 1610 if ($err_msg = $this->server_parse('220', __LINE__)) 1611 { 1612 return false; 1613 } 1614 1615 $result = false; 1616 $stream_meta = stream_get_meta_data($this->socket); 1617 1618 if (socket_set_blocking($this->socket, 1)) 1619 { 1620 // https://secure.php.net/manual/en/function.stream-socket-enable-crypto.php#119122 1621 $crypto = (phpbb_version_compare(PHP_VERSION, '5.6.7', '<')) ? STREAM_CRYPTO_METHOD_TLS_CLIENT : STREAM_CRYPTO_METHOD_SSLv23_CLIENT; 1622 $result = stream_socket_enable_crypto($this->socket, true, $crypto); 1623 socket_set_blocking($this->socket, (int) $stream_meta['blocked']); 1624 } 1625 1626 return $result; 1627 } 1628 1629 /** 1630 * Pop before smtp authentication 1631 */ 1632 function pop_before_smtp($hostname, $username, $password) 1633 { 1634 global $user; 1635 1636 if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10)) 1637 { 1638 if ($errstr) 1639 { 1640 $errstr = utf8_convert_message($errstr); 1641 } 1642 1643 return (isset($user->lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($user->lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr"; 1644 } 1645 1646 $this->server_send("USER $username", true); 1647 if ($err_msg = $this->server_parse('+OK', __LINE__)) 1648 { 1649 return $err_msg; 1650 } 1651 1652 $this->server_send("PASS $password", true); 1653 if ($err_msg = $this->server_parse('+OK', __LINE__)) 1654 { 1655 return $err_msg; 1656 } 1657 1658 $this->server_send('QUIT'); 1659 fclose($this->socket); 1660 1661 return false; 1662 } 1663 1664 /** 1665 * Plain authentication method 1666 */ 1667 function plain($username, $password) 1668 { 1669 $this->server_send('AUTH PLAIN'); 1670 if ($err_msg = $this->server_parse('334', __LINE__)) 1671 { 1672 return ($this->numeric_response_code == 503) ? false : $err_msg; 1673 } 1674 1675 $base64_method_plain = base64_encode("\0" . $username . "\0" . $password); 1676 $this->server_send($base64_method_plain, true); 1677 if ($err_msg = $this->server_parse('235', __LINE__)) 1678 { 1679 return $err_msg; 1680 } 1681 1682 return false; 1683 } 1684 1685 /** 1686 * Login authentication method 1687 */ 1688 function login($username, $password) 1689 { 1690 $this->server_send('AUTH LOGIN'); 1691 if ($err_msg = $this->server_parse('334', __LINE__)) 1692 { 1693 return ($this->numeric_response_code == 503) ? false : $err_msg; 1694 } 1695 1696 $this->server_send(base64_encode($username), true); 1697 if ($err_msg = $this->server_parse('334', __LINE__)) 1698 { 1699 return $err_msg; 1700 } 1701 1702 $this->server_send(base64_encode($password), true); 1703 if ($err_msg = $this->server_parse('235', __LINE__)) 1704 { 1705 return $err_msg; 1706 } 1707 1708 return false; 1709 } 1710 1711 /** 1712 * cram_md5 authentication method 1713 */ 1714 function cram_md5($username, $password) 1715 { 1716 $this->server_send('AUTH CRAM-MD5'); 1717 if ($err_msg = $this->server_parse('334', __LINE__)) 1718 { 1719 return ($this->numeric_response_code == 503) ? false : $err_msg; 1720 } 1721 1722 $md5_challenge = base64_decode($this->responses[0]); 1723 $password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password); 1724 $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)))); 1725 1726 $base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest); 1727 1728 $this->server_send($base64_method_cram_md5, true); 1729 if ($err_msg = $this->server_parse('235', __LINE__)) 1730 { 1731 return $err_msg; 1732 } 1733 1734 return false; 1735 } 1736 1737 /** 1738 * digest_md5 authentication method 1739 * A real pain in the *** 1740 */ 1741 function digest_md5($username, $password) 1742 { 1743 global $config, $user; 1744 1745 $this->server_send('AUTH DIGEST-MD5'); 1746 if ($err_msg = $this->server_parse('334', __LINE__)) 1747 { 1748 return ($this->numeric_response_code == 503) ? false : $err_msg; 1749 } 1750 1751 $md5_challenge = base64_decode($this->responses[0]); 1752 1753 // Parse the md5 challenge - from AUTH_SASL (PEAR) 1754 $tokens = array(); 1755 while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $md5_challenge, $matches)) 1756 { 1757 // Ignore these as per rfc2831 1758 if ($matches[1] == 'opaque' || $matches[1] == 'domain') 1759 { 1760 $md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1); 1761 continue; 1762 } 1763 1764 // Allowed multiple "realm" and "auth-param" 1765 if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param')) 1766 { 1767 if (is_array($tokens[$matches[1]])) 1768 { 1769 $tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]); 1770 } 1771 else 1772 { 1773 $tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2])); 1774 } 1775 } 1776 else if (!empty($tokens[$matches[1]])) // Any other multiple instance = failure 1777 { 1778 $tokens = array(); 1779 break; 1780 } 1781 else 1782 { 1783 $tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]); 1784 } 1785 1786 // Remove the just parsed directive from the challenge 1787 $md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1); 1788 } 1789 1790 // Realm 1791 if (empty($tokens['realm'])) 1792 { 1793 $tokens['realm'] = (function_exists('php_uname')) ? php_uname('n') : $user->host; 1794 } 1795 1796 // Maxbuf 1797 if (empty($tokens['maxbuf'])) 1798 { 1799 $tokens['maxbuf'] = 65536; 1800 } 1801 1802 // Required: nonce, algorithm 1803 if (empty($tokens['nonce']) || empty($tokens['algorithm'])) 1804 { 1805 $tokens = array(); 1806 } 1807 $md5_challenge = $tokens; 1808 1809 if (!empty($md5_challenge)) 1810 { 1811 $str = ''; 1812 for ($i = 0; $i < 32; $i++) 1813 { 1814 $str .= chr(mt_rand(0, 255)); 1815 } 1816 $cnonce = base64_encode($str); 1817 1818 $digest_uri = 'smtp/' . $config['smtp_host']; 1819 1820 $auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce); 1821 $auth_2 = 'AUTHENTICATE:' . $digest_uri; 1822 $response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2))); 1823 1824 $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']); 1825 } 1826 else 1827 { 1828 return (isset($user->lang['INVALID_DIGEST_CHALLENGE'])) ? $user->lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge'; 1829 } 1830 1831 $base64_method_digest_md5 = base64_encode($input_string); 1832 $this->server_send($base64_method_digest_md5, true); 1833 if ($err_msg = $this->server_parse('334', __LINE__)) 1834 { 1835 return $err_msg; 1836 } 1837 1838 $this->server_send(' '); 1839 if ($err_msg = $this->server_parse('235', __LINE__)) 1840 { 1841 return $err_msg; 1842 } 1843 1844 return false; 1845 } 1846 } 1847 1848 /** 1849 * Encodes the given string for proper display in UTF-8 or US-ASCII. 1850 * 1851 * This version is based on iconv_mime_encode() implementation 1852 * from symfomy/polyfill-iconv 1853 * https://github.com/symfony/polyfill-iconv/blob/fd324208ec59a39ebe776e6e9ec5540ad4f40aaa/Iconv.php#L355 1854 * 1855 * @param string $str 1856 * @param string $eol Lines delimiter (optional to be backwards compatible) 1857 * 1858 * @return string 1859 */ 1860 function mail_encode($str, $eol = "\r\n") 1861 { 1862 // Check if string contains ASCII only characters 1863 $is_ascii = strlen($str) === utf8_strlen($str); 1864 1865 $scheme = $is_ascii ? 'Q' : 'B'; 1866 1867 // Define start delimiter, end delimiter 1868 // Use the Quoted-Printable encoding for ASCII strings to avoid unnecessary encoding in Base64 1869 $start = '=?' . ($is_ascii ? 'US-ASCII' : 'UTF-8') . '?' . $scheme . '?'; 1870 $end = '?='; 1871 1872 // Maximum encoded-word length is 75 as per RFC 2047 section 2. 1873 // $split_length *must* be a multiple of 4, but <= 75 - strlen($start . $eol . $end)!!! 1874 $split_length = 75 - strlen($start . $eol . $end); 1875 $split_length = $split_length - $split_length % 4; 1876 1877 $line_length = strlen($start) + strlen($end); 1878 $line_offset = strlen($start) + 1; 1879 $line_data = ''; 1880 1881 $is_quoted_printable = 'Q' === $scheme; 1882 1883 preg_match_all('/./us', $str, $chars); 1884 $chars = $chars[0] ?? []; 1885 1886 $str = []; 1887 foreach ($chars as $char) 1888 { 1889 $encoded_char = $is_quoted_printable 1890 ? $char = preg_replace_callback( 1891 '/[()<>@,;:\\\\".\[\]=_?\x20\x00-\x1F\x80-\xFF]/', 1892 function ($matches) 1893 { 1894 $hex = dechex(ord($matches[0])); 1895 $hex = strlen($hex) == 1 ? "0$hex" : $hex; 1896 return '=' . strtoupper($hex); 1897 }, 1898 $char 1899 ) 1900 : base64_encode($line_data . $char); 1901 1902 if (isset($encoded_char[$split_length - $line_length])) 1903 { 1904 if (!$is_quoted_printable) 1905 { 1906 $line_data = base64_encode($line_data); 1907 } 1908 $str[] = $start . $line_data . $end; 1909 $line_length = $line_offset; 1910 $line_data = ''; 1911 } 1912 1913 $line_data .= $char; 1914 $is_quoted_printable && $line_length += strlen($char); 1915 } 1916 1917 if ($line_data !== '') 1918 { 1919 if (!$is_quoted_printable) 1920 { 1921 $line_data = base64_encode($line_data); 1922 } 1923 $str[] = $start . $line_data . $end; 1924 } 1925 1926 return implode($eol . ' ', $str); 1927 } 1928 1929 /** 1930 * Wrapper for sending out emails with the PHP's mail function 1931 */ 1932 function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg) 1933 { 1934 global $config, $phpbb_root_path, $phpEx, $phpbb_dispatcher; 1935 1936 // Convert Numeric Character References to UTF-8 chars (ie. Emojis) 1937 $subject = utf8_decode_ncr($subject); 1938 $msg = utf8_decode_ncr($msg); 1939 1940 /** 1941 * We use the EOL character for the OS here because the PHP mail function does not correctly transform line endings. 1942 * On Windows SMTP is used (SMTP is \r\n), on UNIX a command is used... 1943 * Reference: http://bugs.php.net/bug.php?id=15841 1944 */ 1945 $headers = implode($eol, $headers); 1946 1947 if (!class_exists('\phpbb\error_collector')) 1948 { 1949 include($phpbb_root_path . 'includes/error_collector.' . $phpEx); 1950 } 1951 1952 $collector = new \phpbb\error_collector; 1953 $collector->install(); 1954 1955 /** 1956 * On some PHP Versions mail() *may* fail if there are newlines within the subject. 1957 * Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8. 1958 * Because PHP can't decide what is wanted we revert back to the non-RFC-compliant way of separating by one space 1959 * (Use '' as parameter to mail_encode() results in SPACE used) 1960 */ 1961 $additional_parameters = $config['email_force_sender'] ? '-f' . $config['board_email'] : ''; 1962 1963 /** 1964 * Modify data before sending out emails with PHP's mail function 1965 * 1966 * @event core.phpbb_mail_before 1967 * @var string to The message recipient 1968 * @var string subject The message subject 1969 * @var string msg The message text 1970 * @var string headers The email headers 1971 * @var string eol The endline character 1972 * @var string additional_parameters The additional parameters 1973 * @since 3.3.6-RC1 1974 */ 1975 $vars = [ 1976 'to', 1977 'subject', 1978 'msg', 1979 'headers', 1980 'eol', 1981 'additional_parameters', 1982 ]; 1983 extract($phpbb_dispatcher->trigger_event('core.phpbb_mail_before', compact($vars))); 1984 1985 $result = mail($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers, $additional_parameters); 1986 1987 /** 1988 * Execute code after sending out emails with PHP's mail function 1989 * 1990 * @event core.phpbb_mail_after 1991 * @var string to The message recipient 1992 * @var string subject The message subject 1993 * @var string msg The message text 1994 * @var string headers The email headers 1995 * @var string eol The endline character 1996 * @var string additional_parameters The additional parameters 1997 * @var bool result True if the email was sent, false otherwise 1998 * @since 3.3.6-RC1 1999 */ 2000 $vars = [ 2001 'to', 2002 'subject', 2003 'msg', 2004 'headers', 2005 'eol', 2006 'additional_parameters', 2007 'result', 2008 ]; 2009 extract($phpbb_dispatcher->trigger_event('core.phpbb_mail_after', compact($vars))); 2010 2011 $collector->uninstall(); 2012 $err_msg = $collector->format_errors(); 2013 2014 return $result; 2015 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |