PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/include/email.php

https://bitbucket.org/gencer/punbb
PHP | 202 lines | 122 code | 47 blank | 33 comment | 32 complexity | 85ef03f15d58f598991fcac46df76f4d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Loads functions used in dealing with email addresses and email sending.
  4. *
  5. * @copyright (C) 2008-2012 PunBB, partially based on code (C) 2008-2009 FluxBB.org
  6. * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  7. * @package PunBB
  8. */
  9. // Make sure no one attempts to run this script "directly"
  10. if (!defined('FORUM'))
  11. exit;
  12. //
  13. // Validate an e-mail address
  14. //
  15. function is_valid_email($email)
  16. {
  17. $return = ($hook = get_hook('em_fn_is_valid_email_start')) ? eval($hook) : null;
  18. if ($return != null)
  19. return $return;
  20. if (strlen($email) > 80)
  21. return false;
  22. return preg_match('/^(([^<>()[\]\\.,;:\s@"\']+(\.[^<>()[\]\\.,;:\s@"\']+)*)|("[^"\']+"))@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])|(([a-zA-Z\d\-]+\.)+[a-zA-Z]{2,}))$/', $email);
  23. }
  24. //
  25. // Check if $email is banned
  26. //
  27. function is_banned_email($email)
  28. {
  29. global $forum_db, $forum_bans;
  30. $return = ($hook = get_hook('em_fn_is_banned_email_start')) ? eval($hook) : null;
  31. if ($return != null)
  32. return $return;
  33. foreach ($forum_bans as $cur_ban)
  34. {
  35. if ($cur_ban['email'] != '' &&
  36. ($email == $cur_ban['email'] ||
  37. (strpos($cur_ban['email'], '@') === false && stristr($email, '@'.$cur_ban['email']))))
  38. return true;
  39. }
  40. return false;
  41. }
  42. //
  43. // Wrapper for PHP's mail()
  44. //
  45. function forum_mail($to, $subject, $message, $reply_to_email = '', $reply_to_name = '')
  46. {
  47. global $forum_config, $lang_common;
  48. // Default sender address
  49. $from_name = sprintf($lang_common['Forum mailer'], $forum_config['o_board_title']);
  50. $from_email = $forum_config['o_webmaster_email'];
  51. ($hook = get_hook('em_fn_forum_mail_start')) ? eval($hook) : null;
  52. // Do a little spring cleaning
  53. $to = forum_trim(preg_replace('#[\n\r]+#s', '', $to));
  54. $subject = forum_trim(preg_replace('#[\n\r]+#s', '', $subject));
  55. $from_email = forum_trim(preg_replace('#[\n\r:]+#s', '', $from_email));
  56. $from_name = forum_trim(preg_replace('#[\n\r:]+#s', '', str_replace('"', '', $from_name)));
  57. $reply_to_email = forum_trim(preg_replace('#[\n\r:]+#s', '', $reply_to_email));
  58. $reply_to_name = forum_trim(preg_replace('#[\n\r:]+#s', '', str_replace('"', '', $reply_to_name)));
  59. // Set up some headers to take advantage of UTF-8
  60. $from = "=?UTF-8?B?".base64_encode($from_name)."?=".' <'.$from_email.'>';
  61. $subject = "=?UTF-8?B?".base64_encode($subject)."?=";
  62. $headers = 'From: '.$from."\r\n".'Date: '.gmdate('r')."\r\n".'MIME-Version: 1.0'."\r\n".'Content-transfer-encoding: 8bit'."\r\n".'Content-type: text/plain; charset=utf-8'."\r\n".'X-Mailer: PunBB Mailer';
  63. // If we specified a reply-to email, we deal with it here
  64. if (!empty($reply_to_email))
  65. {
  66. $reply_to = "=?UTF-8?B?".base64_encode($reply_to_name)."?=".' <'.$reply_to_email.'>';
  67. $headers .= "\r\n".'Reply-To: '.$reply_to;
  68. }
  69. // Make sure all linebreaks are CRLF in message (and strip out any NULL bytes)
  70. $message = str_replace(array("\n", "\0"), array("\r\n", ''), forum_linebreaks($message));
  71. ($hook = get_hook('em_fn_forum_mail_pre_send')) ? eval($hook) : null;
  72. if ($forum_config['o_smtp_host'] != '')
  73. smtp_mail($to, $subject, $message, $headers);
  74. else
  75. {
  76. // Change the linebreaks used in the headers according to OS
  77. if (strtoupper(substr(PHP_OS, 0, 3)) == 'MAC')
  78. $headers = str_replace("\r\n", "\r", $headers);
  79. else if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN')
  80. $headers = str_replace("\r\n", "\n", $headers);
  81. mail($to, $subject, $message, $headers);
  82. }
  83. }
  84. //
  85. // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
  86. // They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
  87. //
  88. function server_parse($socket, $expected_response)
  89. {
  90. $server_response = '';
  91. while (substr($server_response, 3, 1) != ' ')
  92. {
  93. if (!($server_response = fgets($socket, 256)))
  94. error('Couldn\'t get mail server response codes.<br />Please contact the forum administrator.', __FILE__, __LINE__);
  95. }
  96. if (!(substr($server_response, 0, 3) == $expected_response))
  97. error('Unable to send e-mail.<br />Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__);
  98. }
  99. //
  100. // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
  101. // They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
  102. //
  103. function smtp_mail($to, $subject, $message, $headers = '')
  104. {
  105. global $forum_config;
  106. $recipients = explode(',', $to);
  107. // Sanitize the message
  108. $message = str_replace("\r\n.", "\r\n..", $message);
  109. $message = (substr($message, 0, 1) == '.' ? '.'.$message : $message);
  110. // Are we using port 25 or a custom port?
  111. if (strpos($forum_config['o_smtp_host'], ':') !== false)
  112. list($smtp_host, $smtp_port) = explode(':', $forum_config['o_smtp_host']);
  113. else
  114. {
  115. $smtp_host = $forum_config['o_smtp_host'];
  116. $smtp_port = 25;
  117. }
  118. if ($forum_config['o_smtp_ssl'] == '1')
  119. $smtp_host = 'ssl://'.$smtp_host;
  120. if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
  121. error('Could not connect to smtp host "'.$forum_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.').', __FILE__, __LINE__);
  122. server_parse($socket, '220');
  123. if ($forum_config['o_smtp_user'] != '' && $forum_config['o_smtp_pass'] != '')
  124. {
  125. fwrite($socket, 'EHLO '.$smtp_host."\r\n");
  126. server_parse($socket, '250');
  127. fwrite($socket, 'AUTH LOGIN'."\r\n");
  128. server_parse($socket, '334');
  129. fwrite($socket, base64_encode($forum_config['o_smtp_user'])."\r\n");
  130. server_parse($socket, '334');
  131. fwrite($socket, base64_encode($forum_config['o_smtp_pass'])."\r\n");
  132. server_parse($socket, '235');
  133. }
  134. else
  135. {
  136. fwrite($socket, 'HELO '.$smtp_host."\r\n");
  137. server_parse($socket, '250');
  138. }
  139. fwrite($socket, 'MAIL FROM: <'.$forum_config['o_webmaster_email'].'>'."\r\n");
  140. server_parse($socket, '250');
  141. foreach ($recipients as $email)
  142. {
  143. fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
  144. server_parse($socket, '250');
  145. }
  146. fwrite($socket, 'DATA'."\r\n");
  147. server_parse($socket, '354');
  148. fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
  149. fwrite($socket, '.'."\r\n");
  150. server_parse($socket, '250');
  151. fwrite($socket, 'QUIT'."\r\n");
  152. fclose($socket);
  153. return true;
  154. }
  155. define('FORUM_EMAIL_FUNCTIONS_LOADED', 1);