[ Index ] |
PHP Cross Reference of phpBB-3.3.9-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('PHP_SELF'), 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 if ($break) 586 { 587 return true; 588 } 589 590 if (empty($this->replyto)) 591 { 592 $this->replyto = $board_contact; 593 } 594 595 if (empty($this->from)) 596 { 597 $this->from = $board_contact; 598 } 599 600 $encode_eol = $config['smtp_delivery'] || PHP_VERSION_ID >= 80000 ? "\r\n" : PHP_EOL; 601 602 // Build to, cc and bcc strings 603 $to = $cc = $bcc = ''; 604 foreach ($this->addresses as $type => $address_ary) 605 { 606 if ($type == 'im') 607 { 608 continue; 609 } 610 611 foreach ($address_ary as $which_ary) 612 { 613 ${$type} .= ((${$type} != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']); 614 } 615 } 616 617 // Build header 618 $headers = $this->build_header($to, $cc, $bcc); 619 620 // Send message ... 621 if (!$use_queue) 622 { 623 $mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to; 624 $err_msg = ''; 625 626 if ($config['smtp_delivery']) 627 { 628 $result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers); 629 } 630 else 631 { 632 $result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $encode_eol, $err_msg); 633 } 634 635 if (!$result) 636 { 637 $this->error('EMAIL', $err_msg); 638 return false; 639 } 640 } 641 else 642 { 643 $this->queue->put('email', array( 644 'to' => $to, 645 'addresses' => $this->addresses, 646 'subject' => $this->subject, 647 'msg' => $this->msg, 648 'headers' => $headers) 649 ); 650 } 651 652 return true; 653 } 654 655 /** 656 * Send jabber message out 657 */ 658 function msg_jabber() 659 { 660 global $config, $user, $phpbb_root_path, $phpEx; 661 662 if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password'])) 663 { 664 return false; 665 } 666 667 if (empty($this->addresses['im'])) 668 { 669 // Send was successful. ;) 670 return true; 671 } 672 673 $use_queue = false; 674 if ($config['jab_package_size'] && $this->use_queue) 675 { 676 if (empty($this->queue)) 677 { 678 $this->queue = new queue(); 679 $this->queue->init('jabber', $config['jab_package_size']); 680 } 681 $use_queue = true; 682 } 683 684 $addresses = array(); 685 foreach ($this->addresses['im'] as $type => $uid_ary) 686 { 687 $addresses[] = $uid_ary['uid']; 688 } 689 $addresses = array_unique($addresses); 690 691 if (!$use_queue) 692 { 693 include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx); 694 $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']); 695 696 if (!$this->jabber->connect()) 697 { 698 $this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->jabber->get_log()); 699 return false; 700 } 701 702 if (!$this->jabber->login()) 703 { 704 $this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '<br />' . $this->jabber->get_log()); 705 return false; 706 } 707 708 foreach ($addresses as $address) 709 { 710 $this->jabber->send_message($address, $this->msg, $this->subject); 711 } 712 713 $this->jabber->disconnect(); 714 } 715 else 716 { 717 $this->queue->put('jabber', array( 718 'addresses' => $addresses, 719 'subject' => $this->subject, 720 'msg' => $this->msg) 721 ); 722 } 723 unset($addresses); 724 return true; 725 } 726 727 /** 728 * Setup template engine 729 */ 730 protected function setup_template() 731 { 732 global $phpbb_container, $phpbb_dispatcher; 733 734 if ($this->template instanceof \phpbb\template\template) 735 { 736 return; 737 } 738 739 $template_environment = new \phpbb\template\twig\environment( 740 $phpbb_container->get('config'), 741 $phpbb_container->get('filesystem'), 742 $phpbb_container->get('path_helper'), 743 $phpbb_container->getParameter('core.template.cache_path'), 744 $phpbb_container->get('ext.manager'), 745 new \phpbb\template\twig\loader( 746 $phpbb_container->get('filesystem') 747 ), 748 $phpbb_dispatcher, 749 array() 750 ); 751 $template_environment->setLexer($phpbb_container->get('template.twig.lexer')); 752 753 $this->template = new \phpbb\template\twig\twig( 754 $phpbb_container->get('path_helper'), 755 $phpbb_container->get('config'), 756 new \phpbb\template\context(), 757 $template_environment, 758 $phpbb_container->getParameter('core.template.cache_path'), 759 $phpbb_container->get('user'), 760 $phpbb_container->get('template.twig.extensions.collection'), 761 $phpbb_container->get('ext.manager') 762 ); 763 } 764 765 /** 766 * Set template paths to load 767 */ 768 protected function set_template_paths($path_name, $paths) 769 { 770 $this->setup_template(); 771 772 $this->template->set_custom_style($path_name, $paths); 773 } 774 } 775 776 /** 777 * handling email and jabber queue 778 */ 779 class queue 780 { 781 var $data = array(); 782 var $queue_data = array(); 783 var $package_size = 0; 784 var $cache_file = ''; 785 var $eol = "\n"; 786 787 /** 788 * @var \phpbb\filesystem\filesystem_interface 789 */ 790 protected $filesystem; 791 792 /** 793 * constructor 794 */ 795 function __construct() 796 { 797 global $phpEx, $phpbb_root_path, $phpbb_filesystem, $phpbb_container; 798 799 $this->data = array(); 800 $this->cache_file = $phpbb_container->getParameter('core.cache_dir') . "queue.$phpEx"; 801 $this->filesystem = $phpbb_filesystem; 802 } 803 804 /** 805 * Init a queue object 806 */ 807 function init($object, $package_size) 808 { 809 $this->data[$object] = array(); 810 $this->data[$object]['package_size'] = $package_size; 811 $this->data[$object]['data'] = array(); 812 } 813 814 /** 815 * Put object in queue 816 */ 817 function put($object, $scope) 818 { 819 $this->data[$object]['data'][] = $scope; 820 } 821 822 /** 823 * Process queue 824 * Using lock file 825 */ 826 function process() 827 { 828 global $config, $phpEx, $phpbb_root_path, $user, $phpbb_dispatcher; 829 830 $lock = new \phpbb\lock\flock($this->cache_file); 831 $lock->acquire(); 832 833 // avoid races, check file existence once 834 $have_cache_file = file_exists($this->cache_file); 835 if (!$have_cache_file || $config['last_queue_run'] > time() - $config['queue_interval']) 836 { 837 if (!$have_cache_file) 838 { 839 $config->set('last_queue_run', time(), false); 840 } 841 842 $lock->release(); 843 return; 844 } 845 846 $config->set('last_queue_run', time(), false); 847 848 include($this->cache_file); 849 850 foreach ($this->queue_data as $object => $data_ary) 851 { 852 @set_time_limit(0); 853 854 if (!isset($data_ary['package_size'])) 855 { 856 $data_ary['package_size'] = 0; 857 } 858 859 $package_size = $data_ary['package_size']; 860 $num_items = (!$package_size || count($data_ary['data']) < $package_size) ? count($data_ary['data']) : $package_size; 861 862 /* 863 * This code is commented out because it causes problems on some web hosts. 864 * The core problem is rather restrictive email sending limits. 865 * This code is nly useful if you have no such restrictions from the 866 * web host and the package size setting is wrong. 867 868 // If the amount of emails to be sent is way more than package_size than we need to increase it to prevent backlogs... 869 if (count($data_ary['data']) > $package_size * 2.5) 870 { 871 $num_items = count($data_ary['data']); 872 } 873 */ 874 875 switch ($object) 876 { 877 case 'email': 878 // Delete the email queued objects if mailing is disabled 879 if (!$config['email_enable']) 880 { 881 unset($this->queue_data['email']); 882 continue 2; 883 } 884 break; 885 886 case 'jabber': 887 if (!$config['jab_enable']) 888 { 889 unset($this->queue_data['jabber']); 890 continue 2; 891 } 892 893 include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx); 894 $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']); 895 896 if (!$this->jabber->connect()) 897 { 898 $messenger = new messenger(); 899 $messenger->error('JABBER', $user->lang['ERR_JAB_CONNECT']); 900 continue 2; 901 } 902 903 if (!$this->jabber->login()) 904 { 905 $messenger = new messenger(); 906 $messenger->error('JABBER', $user->lang['ERR_JAB_AUTH']); 907 continue 2; 908 } 909 910 break; 911 912 default: 913 $lock->release(); 914 return; 915 } 916 917 for ($i = 0; $i < $num_items; $i++) 918 { 919 // Make variables available... 920 extract(array_shift($this->queue_data[$object]['data'])); 921 922 switch ($object) 923 { 924 case 'email': 925 $break = false; 926 /** 927 * Event to send message via external transport 928 * 929 * @event core.notification_message_process 930 * @var bool break Flag indicating if the function return after hook 931 * @var array addresses The message recipients 932 * @var string subject The message subject 933 * @var string msg The message text 934 * @since 3.2.4-RC1 935 */ 936 $vars = array( 937 'break', 938 'addresses', 939 'subject', 940 'msg', 941 ); 942 extract($phpbb_dispatcher->trigger_event('core.notification_message_process', compact($vars))); 943 944 if (!$break) 945 { 946 $err_msg = ''; 947 $to = (!$to) ? 'undisclosed-recipients:;' : $to; 948 949 if ($config['smtp_delivery']) 950 { 951 $result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers); 952 } 953 else 954 { 955 $encode_eol = $config['smtp_delivery'] || PHP_VERSION_ID >= 80000 ? "\r\n" : PHP_EOL; 956 $result = phpbb_mail($to, $subject, $msg, $headers, $encode_eol, $err_msg); 957 } 958 959 if (!$result) 960 { 961 $messenger = new messenger(); 962 $messenger->error('EMAIL', $err_msg); 963 continue 2; 964 } 965 } 966 break; 967 968 case 'jabber': 969 foreach ($addresses as $address) 970 { 971 if ($this->jabber->send_message($address, $msg, $subject) === false) 972 { 973 $messenger = new messenger(); 974 $messenger->error('JABBER', $this->jabber->get_log()); 975 continue 3; 976 } 977 } 978 break; 979 } 980 } 981 982 // No more data for this object? Unset it 983 if (!count($this->queue_data[$object]['data'])) 984 { 985 unset($this->queue_data[$object]); 986 } 987 988 // Post-object processing 989 switch ($object) 990 { 991 case 'jabber': 992 // Hang about a couple of secs to ensure the messages are 993 // handled, then disconnect 994 $this->jabber->disconnect(); 995 break; 996 } 997 } 998 999 if (!count($this->queue_data)) 1000 { 1001 @unlink($this->cache_file); 1002 } 1003 else 1004 { 1005 if ($fp = @fopen($this->cache_file, 'wb')) 1006 { 1007 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>"); 1008 fclose($fp); 1009 1010 if (function_exists('opcache_invalidate')) 1011 { 1012 @opcache_invalidate($this->cache_file); 1013 } 1014 1015 try 1016 { 1017 $this->filesystem->phpbb_chmod($this->cache_file, \phpbb\filesystem\filesystem_interface::CHMOD_READ | \phpbb\filesystem\filesystem_interface::CHMOD_WRITE); 1018 } 1019 catch (\phpbb\filesystem\exception\filesystem_exception $e) 1020 { 1021 // Do nothing 1022 } 1023 } 1024 } 1025 1026 $lock->release(); 1027 } 1028 1029 /** 1030 * Save queue 1031 */ 1032 function save() 1033 { 1034 if (!count($this->data)) 1035 { 1036 return; 1037 } 1038 1039 $lock = new \phpbb\lock\flock($this->cache_file); 1040 $lock->acquire(); 1041 1042 if (file_exists($this->cache_file)) 1043 { 1044 include($this->cache_file); 1045 1046 foreach ($this->queue_data as $object => $data_ary) 1047 { 1048 if (isset($this->data[$object]) && count($this->data[$object])) 1049 { 1050 $this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']); 1051 } 1052 else 1053 { 1054 $this->data[$object]['data'] = $data_ary['data']; 1055 } 1056 } 1057 } 1058 1059 if ($fp = @fopen($this->cache_file, 'w')) 1060 { 1061 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>"); 1062 fclose($fp); 1063 1064 if (function_exists('opcache_invalidate')) 1065 { 1066 @opcache_invalidate($this->cache_file); 1067 } 1068 1069 try 1070 { 1071 $this->filesystem->phpbb_chmod($this->cache_file, \phpbb\filesystem\filesystem_interface::CHMOD_READ | \phpbb\filesystem\filesystem_interface::CHMOD_WRITE); 1072 } 1073 catch (\phpbb\filesystem\exception\filesystem_exception $e) 1074 { 1075 // Do nothing 1076 } 1077 1078 $this->data = array(); 1079 } 1080 1081 $lock->release(); 1082 } 1083 } 1084 1085 /** 1086 * Replacement or substitute for PHP's mail command 1087 */ 1088 function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false) 1089 { 1090 global $config, $user; 1091 1092 // Fix any bare linefeeds in the message to make it RFC821 Compliant. 1093 $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message); 1094 1095 if ($headers !== false) 1096 { 1097 if (!is_array($headers)) 1098 { 1099 // Make sure there are no bare linefeeds in the headers 1100 $headers = preg_replace('#(?<!\r)\n#si', "\n", $headers); 1101 $headers = explode("\n", $headers); 1102 } 1103 1104 // Ok this is rather confusing all things considered, 1105 // but we have to grab bcc and cc headers and treat them differently 1106 // Something we really didn't take into consideration originally 1107 $headers_used = array(); 1108 1109 foreach ($headers as $header) 1110 { 1111 if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0) 1112 { 1113 continue; 1114 } 1115 $headers_used[] = trim($header); 1116 } 1117 1118 $headers = chop(implode("\r\n", $headers_used)); 1119 } 1120 1121 if (trim($subject) == '') 1122 { 1123 $err_msg = (isset($user->lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified'; 1124 return false; 1125 } 1126 1127 if (trim($message) == '') 1128 { 1129 $err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank'; 1130 return false; 1131 } 1132 1133 $mail_rcpt = $mail_to = $mail_cc = array(); 1134 1135 // Build correct addresses for RCPT TO command and the client side display (TO, CC) 1136 if (isset($addresses['to']) && count($addresses['to'])) 1137 { 1138 foreach ($addresses['to'] as $which_ary) 1139 { 1140 $mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; 1141 $mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>'; 1142 } 1143 } 1144 1145 if (isset($addresses['bcc']) && count($addresses['bcc'])) 1146 { 1147 foreach ($addresses['bcc'] as $which_ary) 1148 { 1149 $mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>'; 1150 } 1151 } 1152 1153 if (isset($addresses['cc']) && count($addresses['cc'])) 1154 { 1155 foreach ($addresses['cc'] as $which_ary) 1156 { 1157 $mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; 1158 $mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>'; 1159 } 1160 } 1161 1162 $smtp = new smtp_class(); 1163 1164 $errno = 0; 1165 $errstr = ''; 1166 1167 $smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']); 1168 1169 // Ok we have error checked as much as we can to this point let's get on it already. 1170 if (!class_exists('\phpbb\error_collector')) 1171 { 1172 global $phpbb_root_path, $phpEx; 1173 include($phpbb_root_path . 'includes/error_collector.' . $phpEx); 1174 } 1175 $collector = new \phpbb\error_collector; 1176 $collector->install(); 1177 1178 $options = array(); 1179 $verify_peer = (bool) $config['smtp_verify_peer']; 1180 $verify_peer_name = (bool) $config['smtp_verify_peer_name']; 1181 $allow_self_signed = (bool) $config['smtp_allow_self_signed']; 1182 $remote_socket = $config['smtp_host'] . ':' . $config['smtp_port']; 1183 1184 // Set ssl context options, see http://php.net/manual/en/context.ssl.php 1185 $options['ssl'] = array('verify_peer' => $verify_peer, 'verify_peer_name' => $verify_peer_name, 'allow_self_signed' => $allow_self_signed); 1186 $socket_context = stream_context_create($options); 1187 1188 $smtp->socket = @stream_socket_client($remote_socket, $errno, $errstr, 20, STREAM_CLIENT_CONNECT, $socket_context); 1189 $collector->uninstall(); 1190 $error_contents = $collector->format_errors(); 1191 1192 if (!$smtp->socket) 1193 { 1194 if ($errstr) 1195 { 1196 $errstr = utf8_convert_message($errstr); 1197 } 1198 1199 $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"; 1200 $err_msg .= ($error_contents) ? '<br /><br />' . htmlspecialchars($error_contents, ENT_COMPAT) : ''; 1201 return false; 1202 } 1203 1204 // Wait for reply 1205 if ($err_msg = $smtp->server_parse('220', __LINE__)) 1206 { 1207 $smtp->close_session($err_msg); 1208 return false; 1209 } 1210 1211 // Let me in. This function handles the complete authentication process 1212 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'])) 1213 { 1214 $smtp->close_session($err_msg); 1215 return false; 1216 } 1217 1218 // From this point onward most server response codes should be 250 1219 // Specify who the mail is from.... 1220 $smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>'); 1221 if ($err_msg = $smtp->server_parse('250', __LINE__)) 1222 { 1223 $smtp->close_session($err_msg); 1224 return false; 1225 } 1226 1227 // Specify each user to send to and build to header. 1228 $to_header = implode(', ', $mail_to); 1229 $cc_header = implode(', ', $mail_cc); 1230 1231 // Now tell the MTA to send the Message to the following people... [TO, BCC, CC] 1232 $rcpt = false; 1233 foreach ($mail_rcpt as $type => $mail_to_addresses) 1234 { 1235 foreach ($mail_to_addresses as $mail_to_address) 1236 { 1237 // Add an additional bit of error checking to the To field. 1238 if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address)) 1239 { 1240 $smtp->server_send("RCPT TO:$mail_to_address"); 1241 if ($err_msg = $smtp->server_parse('250', __LINE__)) 1242 { 1243 // We continue... if users are not resolved we do not care 1244 if ($smtp->numeric_response_code != 550) 1245 { 1246 $smtp->close_session($err_msg); 1247 return false; 1248 } 1249 } 1250 else 1251 { 1252 $rcpt = true; 1253 } 1254 } 1255 } 1256 } 1257 1258 // 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. 1259 if (!$rcpt) 1260 { 1261 $user->session_begin(); 1262 $err_msg .= '<br /><br />'; 1263 $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?'; 1264 $smtp->close_session($err_msg); 1265 return false; 1266 } 1267 1268 // Ok now we tell the server we are ready to start sending data 1269 $smtp->server_send('DATA'); 1270 1271 // This is the last response code we look for until the end of the message. 1272 if ($err_msg = $smtp->server_parse('354', __LINE__)) 1273 { 1274 $smtp->close_session($err_msg); 1275 return false; 1276 } 1277 1278 // Send the Subject Line... 1279 $smtp->server_send("Subject: $subject"); 1280 1281 // Now the To Header. 1282 $to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header; 1283 $smtp->server_send("To: $to_header"); 1284 1285 // Now the CC Header. 1286 if ($cc_header != '') 1287 { 1288 $smtp->server_send("CC: $cc_header"); 1289 } 1290 1291 // Now any custom headers.... 1292 if ($headers !== false) 1293 { 1294 $smtp->server_send("$headers\r\n"); 1295 } 1296 1297 // Ok now we are ready for the message... 1298 $smtp->server_send($message); 1299 1300 // Ok the all the ingredients are mixed in let's cook this puppy... 1301 $smtp->server_send('.'); 1302 if ($err_msg = $smtp->server_parse('250', __LINE__)) 1303 { 1304 $smtp->close_session($err_msg); 1305 return false; 1306 } 1307 1308 // Now tell the server we are done and close the socket... 1309 $smtp->server_send('QUIT'); 1310 $smtp->close_session($err_msg); 1311 1312 return true; 1313 } 1314 1315 /** 1316 * SMTP Class 1317 * Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR) 1318 * See docs/AUTHORS for more details 1319 */ 1320 class smtp_class 1321 { 1322 var $server_response = ''; 1323 var $socket = 0; 1324 protected $socket_tls = false; 1325 var $responses = array(); 1326 var $commands = array(); 1327 var $numeric_response_code = 0; 1328 1329 var $backtrace = false; 1330 var $backtrace_log = array(); 1331 1332 function __construct() 1333 { 1334 // Always create a backtrace for admins to identify SMTP problems 1335 $this->backtrace = true; 1336 $this->backtrace_log = array(); 1337 } 1338 1339 /** 1340 * Add backtrace message for debugging 1341 */ 1342 function add_backtrace($message) 1343 { 1344 if ($this->backtrace) 1345 { 1346 $this->backtrace_log[] = utf8_htmlspecialchars($message, ENT_COMPAT); 1347 } 1348 } 1349 1350 /** 1351 * Send command to smtp server 1352 */ 1353 function server_send($command, $private_info = false) 1354 { 1355 fputs($this->socket, $command . "\r\n"); 1356 1357 (!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information'); 1358 1359 // We could put additional code here 1360 } 1361 1362 /** 1363 * We use the line to give the support people an indication at which command the error occurred 1364 */ 1365 function server_parse($response, $line) 1366 { 1367 global $user; 1368 1369 $this->server_response = ''; 1370 $this->responses = array(); 1371 $this->numeric_response_code = 0; 1372 1373 while (substr($this->server_response, 3, 1) != ' ') 1374 { 1375 if (!($this->server_response = fgets($this->socket, 256))) 1376 { 1377 return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes'; 1378 } 1379 $this->responses[] = substr(rtrim($this->server_response), 4); 1380 $this->numeric_response_code = (int) substr($this->server_response, 0, 3); 1381 1382 $this->add_backtrace("LINE: $line <- {$this->server_response}"); 1383 } 1384 1385 if (!(substr($this->server_response, 0, 3) == $response)) 1386 { 1387 $this->numeric_response_code = (int) substr($this->server_response, 0, 3); 1388 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"; 1389 } 1390 1391 return 0; 1392 } 1393 1394 /** 1395 * Close session 1396 */ 1397 function close_session(&$err_msg) 1398 { 1399 fclose($this->socket); 1400 1401 if ($this->backtrace) 1402 { 1403 $message = '<h1>Backtrace</h1><p>' . implode('<br />', $this->backtrace_log) . '</p>'; 1404 $err_msg .= $message; 1405 } 1406 } 1407 1408 /** 1409 * Log into server and get possible auth codes if neccessary 1410 */ 1411 function log_into_server($hostname, $username, $password, $default_auth_method) 1412 { 1413 global $user; 1414 1415 // Here we try to determine the *real* hostname (reverse DNS entry preferrably) 1416 $local_host = $user->host; 1417 1418 if (function_exists('php_uname')) 1419 { 1420 $local_host = php_uname('n'); 1421 1422 // Able to resolve name to IP 1423 if (($addr = @gethostbyname($local_host)) !== $local_host) 1424 { 1425 // Able to resolve IP back to name 1426 if (($name = @gethostbyaddr($addr)) !== $addr) 1427 { 1428 $local_host = $name; 1429 } 1430 } 1431 } 1432 1433 // If we are authenticating through pop-before-smtp, we 1434 // have to login ones before we get authenticated 1435 // NOTE: on some configurations the time between an update of the auth database takes so 1436 // long that the first email send does not work. This is not a biggie on a live board (only 1437 // the install mail will most likely fail) - but on a dynamic ip connection this might produce 1438 // severe problems and is not fixable! 1439 if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password) 1440 { 1441 global $config; 1442 1443 $errno = 0; 1444 $errstr = ''; 1445 1446 $this->server_send("QUIT"); 1447 fclose($this->socket); 1448 1449 $this->pop_before_smtp($hostname, $username, $password); 1450 $username = $password = $default_auth_method = ''; 1451 1452 // We need to close the previous session, else the server is not 1453 // able to get our ip for matching... 1454 if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10)) 1455 { 1456 if ($errstr) 1457 { 1458 $errstr = utf8_convert_message($errstr); 1459 } 1460 1461 $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"; 1462 return $err_msg; 1463 } 1464 1465 // Wait for reply 1466 if ($err_msg = $this->server_parse('220', __LINE__)) 1467 { 1468 $this->close_session($err_msg); 1469 return $err_msg; 1470 } 1471 } 1472 1473 $hello_result = $this->hello($local_host); 1474 if (!is_null($hello_result)) 1475 { 1476 return $hello_result; 1477 } 1478 1479 // SMTP STARTTLS (RFC 3207) 1480 if (!$this->socket_tls) 1481 { 1482 $this->socket_tls = $this->starttls(); 1483 1484 if ($this->socket_tls) 1485 { 1486 // Switched to TLS 1487 // RFC 3207: "The client MUST discard any knowledge obtained from the server, [...]" 1488 // So say hello again 1489 $hello_result = $this->hello($local_host); 1490 1491 if (!is_null($hello_result)) 1492 { 1493 return $hello_result; 1494 } 1495 } 1496 } 1497 1498 // If we are not authenticated yet, something might be wrong if no username and passwd passed 1499 if (!$username || !$password) 1500 { 1501 return false; 1502 } 1503 1504 if (!isset($this->commands['AUTH'])) 1505 { 1506 return (isset($user->lang['SMTP_NO_AUTH_SUPPORT'])) ? $user->lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication'; 1507 } 1508 1509 // Get best authentication method 1510 $available_methods = explode(' ', $this->commands['AUTH']); 1511 1512 // Define the auth ordering if the default auth method was not found 1513 $auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5'); 1514 $method = ''; 1515 1516 if (in_array($default_auth_method, $available_methods)) 1517 { 1518 $method = $default_auth_method; 1519 } 1520 else 1521 { 1522 foreach ($auth_methods as $_method) 1523 { 1524 if (in_array($_method, $available_methods)) 1525 { 1526 $method = $_method; 1527 break; 1528 } 1529 } 1530 } 1531 1532 if (!$method) 1533 { 1534 return (isset($user->lang['NO_SUPPORTED_AUTH_METHODS'])) ? $user->lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods'; 1535 } 1536 1537 $method = strtolower(str_replace('-', '_', $method)); 1538 return $this->$method($username, $password); 1539 } 1540 1541 /** 1542 * SMTP EHLO/HELO 1543 * 1544 * @return mixed Null if the authentication process is supposed to continue 1545 * False if already authenticated 1546 * Error message (string) otherwise 1547 */ 1548 protected function hello($hostname) 1549 { 1550 // Try EHLO first 1551 $this->server_send("EHLO $hostname"); 1552 if ($err_msg = $this->server_parse('250', __LINE__)) 1553 { 1554 // a 503 response code means that we're already authenticated 1555 if ($this->numeric_response_code == 503) 1556 { 1557 return false; 1558 } 1559 1560 // If EHLO fails, we try HELO 1561 $this->server_send("HELO $hostname"); 1562 if ($err_msg = $this->server_parse('250', __LINE__)) 1563 { 1564 return ($this->numeric_response_code == 503) ? false : $err_msg; 1565 } 1566 } 1567 1568 foreach ($this->responses as $response) 1569 { 1570 $response = explode(' ', $response); 1571 $response_code = $response[0]; 1572 unset($response[0]); 1573 $this->commands[$response_code] = implode(' ', $response); 1574 } 1575 } 1576 1577 /** 1578 * SMTP STARTTLS (RFC 3207) 1579 * 1580 * @return bool Returns true if TLS was started 1581 * Otherwise false 1582 */ 1583 protected function starttls() 1584 { 1585 global $config; 1586 1587 // allow SMTPS (what was used by phpBB 3.0) if hostname is prefixed with tls:// or ssl:// 1588 if (strpos($config['smtp_host'], 'tls://') === 0 || strpos($config['smtp_host'], 'ssl://') === 0) 1589 { 1590 return true; 1591 } 1592 1593 if (!function_exists('stream_socket_enable_crypto')) 1594 { 1595 return false; 1596 } 1597 1598 if (!isset($this->commands['STARTTLS'])) 1599 { 1600 return false; 1601 } 1602 1603 $this->server_send('STARTTLS'); 1604 1605 if ($err_msg = $this->server_parse('220', __LINE__)) 1606 { 1607 return false; 1608 } 1609 1610 $result = false; 1611 $stream_meta = stream_get_meta_data($this->socket); 1612 1613 if (socket_set_blocking($this->socket, 1)) 1614 { 1615 // https://secure.php.net/manual/en/function.stream-socket-enable-crypto.php#119122 1616 $crypto = (phpbb_version_compare(PHP_VERSION, '5.6.7', '<')) ? STREAM_CRYPTO_METHOD_TLS_CLIENT : STREAM_CRYPTO_METHOD_SSLv23_CLIENT; 1617 $result = stream_socket_enable_crypto($this->socket, true, $crypto); 1618 socket_set_blocking($this->socket, (int) $stream_meta['blocked']); 1619 } 1620 1621 return $result; 1622 } 1623 1624 /** 1625 * Pop before smtp authentication 1626 */ 1627 function pop_before_smtp($hostname, $username, $password) 1628 { 1629 global $user; 1630 1631 if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10)) 1632 { 1633 if ($errstr) 1634 { 1635 $errstr = utf8_convert_message($errstr); 1636 } 1637 1638 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"; 1639 } 1640 1641 $this->server_send("USER $username", true); 1642 if ($err_msg = $this->server_parse('+OK', __LINE__)) 1643 { 1644 return $err_msg; 1645 } 1646 1647 $this->server_send("PASS $password", true); 1648 if ($err_msg = $this->server_parse('+OK', __LINE__)) 1649 { 1650 return $err_msg; 1651 } 1652 1653 $this->server_send('QUIT'); 1654 fclose($this->socket); 1655 1656 return false; 1657 } 1658 1659 /** 1660 * Plain authentication method 1661 */ 1662 function plain($username, $password) 1663 { 1664 $this->server_send('AUTH PLAIN'); 1665 if ($err_msg = $this->server_parse('334', __LINE__)) 1666 { 1667 return ($this->numeric_response_code == 503) ? false : $err_msg; 1668 } 1669 1670 $base64_method_plain = base64_encode("\0" . $username . "\0" . $password); 1671 $this->server_send($base64_method_plain, true); 1672 if ($err_msg = $this->server_parse('235', __LINE__)) 1673 { 1674 return $err_msg; 1675 } 1676 1677 return false; 1678 } 1679 1680 /** 1681 * Login authentication method 1682 */ 1683 function login($username, $password) 1684 { 1685 $this->server_send('AUTH LOGIN'); 1686 if ($err_msg = $this->server_parse('334', __LINE__)) 1687 { 1688 return ($this->numeric_response_code == 503) ? false : $err_msg; 1689 } 1690 1691 $this->server_send(base64_encode($username), true); 1692 if ($err_msg = $this->server_parse('334', __LINE__)) 1693 { 1694 return $err_msg; 1695 } 1696 1697 $this->server_send(base64_encode($password), true); 1698 if ($err_msg = $this->server_parse('235', __LINE__)) 1699 { 1700 return $err_msg; 1701 } 1702 1703 return false; 1704 } 1705 1706 /** 1707 * cram_md5 authentication method 1708 */ 1709 function cram_md5($username, $password) 1710 { 1711 $this->server_send('AUTH CRAM-MD5'); 1712 if ($err_msg = $this->server_parse('334', __LINE__)) 1713 { 1714 return ($this->numeric_response_code == 503) ? false : $err_msg; 1715 } 1716 1717 $md5_challenge = base64_decode($this->responses[0]); 1718 $password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password); 1719 $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)))); 1720 1721 $base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest); 1722 1723 $this->server_send($base64_method_cram_md5, true); 1724 if ($err_msg = $this->server_parse('235', __LINE__)) 1725 { 1726 return $err_msg; 1727 } 1728 1729 return false; 1730 } 1731 1732 /** 1733 * digest_md5 authentication method 1734 * A real pain in the *** 1735 */ 1736 function digest_md5($username, $password) 1737 { 1738 global $config, $user; 1739 1740 $this->server_send('AUTH DIGEST-MD5'); 1741 if ($err_msg = $this->server_parse('334', __LINE__)) 1742 { 1743 return ($this->numeric_response_code == 503) ? false : $err_msg; 1744 } 1745 1746 $md5_challenge = base64_decode($this->responses[0]); 1747 1748 // Parse the md5 challenge - from AUTH_SASL (PEAR) 1749 $tokens = array(); 1750 while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $md5_challenge, $matches)) 1751 { 1752 // Ignore these as per rfc2831 1753 if ($matches[1] == 'opaque' || $matches[1] == 'domain') 1754 { 1755 $md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1); 1756 continue; 1757 } 1758 1759 // Allowed multiple "realm" and "auth-param" 1760 if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param')) 1761 { 1762 if (is_array($tokens[$matches[1]])) 1763 { 1764 $tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]); 1765 } 1766 else 1767 { 1768 $tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2])); 1769 } 1770 } 1771 else if (!empty($tokens[$matches[1]])) // Any other multiple instance = failure 1772 { 1773 $tokens = array(); 1774 break; 1775 } 1776 else 1777 { 1778 $tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]); 1779 } 1780 1781 // Remove the just parsed directive from the challenge 1782 $md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1); 1783 } 1784 1785 // Realm 1786 if (empty($tokens['realm'])) 1787 { 1788 $tokens['realm'] = (function_exists('php_uname')) ? php_uname('n') : $user->host; 1789 } 1790 1791 // Maxbuf 1792 if (empty($tokens['maxbuf'])) 1793 { 1794 $tokens['maxbuf'] = 65536; 1795 } 1796 1797 // Required: nonce, algorithm 1798 if (empty($tokens['nonce']) || empty($tokens['algorithm'])) 1799 { 1800 $tokens = array(); 1801 } 1802 $md5_challenge = $tokens; 1803 1804 if (!empty($md5_challenge)) 1805 { 1806 $str = ''; 1807 for ($i = 0; $i < 32; $i++) 1808 { 1809 $str .= chr(mt_rand(0, 255)); 1810 } 1811 $cnonce = base64_encode($str); 1812 1813 $digest_uri = 'smtp/' . $config['smtp_host']; 1814 1815 $auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce); 1816 $auth_2 = 'AUTHENTICATE:' . $digest_uri; 1817 $response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2))); 1818 1819 $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']); 1820 } 1821 else 1822 { 1823 return (isset($user->lang['INVALID_DIGEST_CHALLENGE'])) ? $user->lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge'; 1824 } 1825 1826 $base64_method_digest_md5 = base64_encode($input_string); 1827 $this->server_send($base64_method_digest_md5, true); 1828 if ($err_msg = $this->server_parse('334', __LINE__)) 1829 { 1830 return $err_msg; 1831 } 1832 1833 $this->server_send(' '); 1834 if ($err_msg = $this->server_parse('235', __LINE__)) 1835 { 1836 return $err_msg; 1837 } 1838 1839 return false; 1840 } 1841 } 1842 1843 /** 1844 * Encodes the given string for proper display in UTF-8 or US-ASCII. 1845 * 1846 * This version is based on iconv_mime_encode() implementation 1847 * from symfomy/polyfill-iconv 1848 * https://github.com/symfony/polyfill-iconv/blob/fd324208ec59a39ebe776e6e9ec5540ad4f40aaa/Iconv.php#L355 1849 * 1850 * @param string $str 1851 * @param string $eol Lines delimiter (optional to be backwards compatible) 1852 * 1853 * @return string 1854 */ 1855 function mail_encode($str, $eol = "\r\n") 1856 { 1857 // Check if string contains ASCII only characters 1858 $is_ascii = strlen($str) === utf8_strlen($str); 1859 1860 $scheme = $is_ascii ? 'Q' : 'B'; 1861 1862 // Define start delimiter, end delimiter 1863 // Use the Quoted-Printable encoding for ASCII strings to avoid unnecessary encoding in Base64 1864 $start = '=?' . ($is_ascii ? 'US-ASCII' : 'UTF-8') . '?' . $scheme . '?'; 1865 $end = '?='; 1866 1867 // Maximum encoded-word length is 75 as per RFC 2047 section 2. 1868 // $split_length *must* be a multiple of 4, but <= 75 - strlen($start . $eol . $end)!!! 1869 $split_length = 75 - strlen($start . $eol . $end); 1870 $split_length = $split_length - $split_length % 4; 1871 1872 $line_length = strlen($start) + strlen($end); 1873 $line_offset = strlen($start) + 1; 1874 $line_data = ''; 1875 1876 $is_quoted_printable = 'Q' === $scheme; 1877 1878 preg_match_all('/./us', $str, $chars); 1879 $chars = $chars[0] ?? []; 1880 1881 $str = []; 1882 foreach ($chars as $char) 1883 { 1884 $encoded_char = $is_quoted_printable 1885 ? $char = preg_replace_callback( 1886 '/[()<>@,;:\\\\".\[\]=_?\x20\x00-\x1F\x80-\xFF]/', 1887 function ($matches) 1888 { 1889 $hex = dechex(ord($matches[0])); 1890 $hex = strlen($hex) == 1 ? "0$hex" : $hex; 1891 return '=' . strtoupper($hex); 1892 }, 1893 $char 1894 ) 1895 : base64_encode($line_data . $char); 1896 1897 if (isset($encoded_char[$split_length - $line_length])) 1898 { 1899 if (!$is_quoted_printable) 1900 { 1901 $line_data = base64_encode($line_data); 1902 } 1903 $str[] = $start . $line_data . $end; 1904 $line_length = $line_offset; 1905 $line_data = ''; 1906 } 1907 1908 $line_data .= $char; 1909 $is_quoted_printable && $line_length += strlen($char); 1910 } 1911 1912 if ($line_data !== '') 1913 { 1914 if (!$is_quoted_printable) 1915 { 1916 $line_data = base64_encode($line_data); 1917 } 1918 $str[] = $start . $line_data . $end; 1919 } 1920 1921 return implode($eol . ' ', $str); 1922 } 1923 1924 /** 1925 * Wrapper for sending out emails with the PHP's mail function 1926 */ 1927 function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg) 1928 { 1929 global $config, $phpbb_root_path, $phpEx, $phpbb_dispatcher; 1930 1931 // Convert Numeric Character References to UTF-8 chars (ie. Emojis) 1932 $subject = utf8_decode_ncr($subject); 1933 $msg = utf8_decode_ncr($msg); 1934 1935 /** 1936 * We use the EOL character for the OS here because the PHP mail function does not correctly transform line endings. 1937 * On Windows SMTP is used (SMTP is \r\n), on UNIX a command is used... 1938 * Reference: http://bugs.php.net/bug.php?id=15841 1939 */ 1940 $headers = implode($eol, $headers); 1941 1942 if (!class_exists('\phpbb\error_collector')) 1943 { 1944 include($phpbb_root_path . 'includes/error_collector.' . $phpEx); 1945 } 1946 1947 $collector = new \phpbb\error_collector; 1948 $collector->install(); 1949 1950 /** 1951 * On some PHP Versions mail() *may* fail if there are newlines within the subject. 1952 * Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8. 1953 * Because PHP can't decide what is wanted we revert back to the non-RFC-compliant way of separating by one space 1954 * (Use '' as parameter to mail_encode() results in SPACE used) 1955 */ 1956 $additional_parameters = $config['email_force_sender'] ? '-f' . $config['board_email'] : ''; 1957 1958 /** 1959 * Modify data before sending out emails with PHP's mail function 1960 * 1961 * @event core.phpbb_mail_before 1962 * @var string to The message recipient 1963 * @var string subject The message subject 1964 * @var string msg The message text 1965 * @var string headers The email headers 1966 * @var string eol The endline character 1967 * @var string additional_parameters The additional parameters 1968 * @since 3.3.6-RC1 1969 */ 1970 $vars = [ 1971 'to', 1972 'subject', 1973 'msg', 1974 'headers', 1975 'eol', 1976 'additional_parameters', 1977 ]; 1978 extract($phpbb_dispatcher->trigger_event('core.phpbb_mail_before', compact($vars))); 1979 1980 $result = mail($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers, $additional_parameters); 1981 1982 /** 1983 * Execute code after sending out emails with PHP's mail function 1984 * 1985 * @event core.phpbb_mail_after 1986 * @var string to The message recipient 1987 * @var string subject The message subject 1988 * @var string msg The message text 1989 * @var string headers The email headers 1990 * @var string eol The endline character 1991 * @var string additional_parameters The additional parameters 1992 * @var bool result True if the email was sent, false otherwise 1993 * @since 3.3.6-RC1 1994 */ 1995 $vars = [ 1996 'to', 1997 'subject', 1998 'msg', 1999 'headers', 2000 'eol', 2001 'additional_parameters', 2002 'result', 2003 ]; 2004 extract($phpbb_dispatcher->trigger_event('core.phpbb_mail_after', compact($vars))); 2005 2006 $collector->uninstall(); 2007 $err_msg = $collector->format_errors(); 2008 2009 return $result; 2010 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Dec 7 15:09:22 2022 | Cross-referenced by PHPXref 0.7.1 |