PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/include/email.php

https://bitbucket.org/gencer/fluxbb
PHP | 364 lines | 240 code | 68 blank | 56 comment | 41 complexity | 615bda34f9a4a024a0966b6a7b6161e3 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2012 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. // Make sure no one attempts to run this script "directly"
  8. if (!defined('PUN'))
  9. exit;
  10. // Define line breaks in mail headers; possible values can be PHP_EOL, "\r\n", "\n" or "\r"
  11. if (!defined('FORUM_EOL'))
  12. define('FORUM_EOL', PHP_EOL);
  13. require PUN_ROOT.'include/utf8/utils/ascii.php';
  14. //
  15. // Validate an email address
  16. //
  17. function is_valid_email($email)
  18. {
  19. if (strlen($email) > 80)
  20. return false;
  21. 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);
  22. }
  23. //
  24. // Check if $email is banned
  25. //
  26. function is_banned_email($email)
  27. {
  28. global $pun_bans;
  29. foreach ($pun_bans as $cur_ban)
  30. {
  31. if ($cur_ban['email'] != '' &&
  32. ($email == $cur_ban['email'] ||
  33. (strpos($cur_ban['email'], '@') === false && stristr($email, '@'.$cur_ban['email']))))
  34. return true;
  35. }
  36. return false;
  37. }
  38. //
  39. // Only encode with base64, if there is at least one unicode character in the string
  40. //
  41. function encode_mail_text($str)
  42. {
  43. if (utf8_is_ascii($str))
  44. return $str;
  45. return '=?UTF-8?B?'.base64_encode($str).'?=';
  46. }
  47. //
  48. // Make a post email safe
  49. //
  50. function bbcode2email($text, $wrap_length = 72)
  51. {
  52. static $base_url;
  53. if (!isset($base_url))
  54. $base_url = get_base_url();
  55. $text = pun_trim($text, "\t\n ");
  56. $shortcut_urls = array(
  57. 'topic' => '/viewtopic.php?id=$1',
  58. 'post' => '/viewtopic.php?pid=$1#p$1',
  59. 'forum' => '/viewforum.php?id=$1',
  60. 'user' => '/profile.php?id=$1',
  61. );
  62. // Split code blocks and text so BBcode in codeblocks won't be touched
  63. list($code, $text) = extract_blocks($text, '[code]', '[/code]');
  64. // Strip all bbcodes, except the quote, url, img, email, code and list items bbcodes
  65. $text = preg_replace(array(
  66. '%\[/?(?!(?:quote|url|topic|post|user|forum|img|email|code|list|\*))[a-z]+(?:=[^\]]+)?\]%i',
  67. '%\n\[/?list(?:=[^\]]+)?\]%i' // A separate regex for the list tags to get rid of some whitespace
  68. ), '', $text);
  69. // Match the deepest nested bbcode
  70. // An adapted example from Mastering Regular Expressions
  71. $match_quote_regex = '%
  72. \[(quote|\*|url|img|email|topic|post|user|forum)(?:=([^\]]+))?\]
  73. (
  74. (?>[^\[]*)
  75. (?>
  76. (?!\[/?\1(?:=[^\]]+)?\])
  77. \[
  78. [^\[]*
  79. )*
  80. )
  81. \[/\1\]
  82. %ix';
  83. $url_index = 1;
  84. $url_stack = array();
  85. while (preg_match($match_quote_regex, $text, $matches))
  86. {
  87. // Quotes
  88. if ($matches[1] == 'quote')
  89. {
  90. // Put '>' or '> ' at the start of a line
  91. $replacement = preg_replace(
  92. array('%^(?=\>)%m', '%^(?!\>)%m'),
  93. array('>', '> '),
  94. $matches[2]." said:\n".$matches[3]);
  95. }
  96. // List items
  97. elseif ($matches[1] == '*')
  98. {
  99. $replacement = ' * '.$matches[3];
  100. }
  101. // URLs and emails
  102. elseif (in_array($matches[1], array('url', 'email')))
  103. {
  104. if (!empty($matches[2]))
  105. {
  106. $replacement = '['.$matches[3].']['.$url_index.']';
  107. $url_stack[$url_index] = $matches[2];
  108. $url_index++;
  109. }
  110. else
  111. $replacement = '['.$matches[3].']';
  112. }
  113. // Images
  114. elseif ($matches[1] == 'img')
  115. {
  116. if (!empty($matches[2]))
  117. $replacement = '['.$matches[2].']['.$url_index.']';
  118. else
  119. $replacement = '['.basename($matches[3]).']['.$url_index.']';
  120. $url_stack[$url_index] = $matches[3];
  121. $url_index++;
  122. }
  123. // Topic, post, forum and user URLs
  124. elseif (in_array($matches[1], array('topic', 'post', 'forum', 'user')))
  125. {
  126. $url = isset($shortcut_urls[$matches[1]]) ? $base_url.$shortcut_urls[$matches[1]] : '';
  127. if (!empty($matches[2]))
  128. {
  129. $replacement = '['.$matches[3].']['.$url_index.']';
  130. $url_stack[$url_index] = str_replace('$1', $matches[2], $url);
  131. $url_index++;
  132. }
  133. else
  134. $replacement = '['.str_replace('$1', $matches[3], $url).']';
  135. }
  136. // Update the main text if there is a replacement
  137. if (!is_null($replacement))
  138. {
  139. $text = str_replace($matches[0], $replacement, $text);
  140. $replacement = null;
  141. }
  142. }
  143. // Put code blocks and text together
  144. if (isset($code))
  145. {
  146. $parts = explode("\1", $text);
  147. $text = '';
  148. foreach ($parts as $i => $part)
  149. {
  150. $text .= $part;
  151. if (isset($code[$i]))
  152. $text .= trim($code[$i], "\n\r");
  153. }
  154. }
  155. // Put URLs at the bottom
  156. if ($url_stack)
  157. {
  158. $text .= "\n\n";
  159. foreach ($url_stack as $i => $url)
  160. $text .= "\n".' ['.$i.']: '.$url;
  161. }
  162. // Wrap lines if $wrap_length is higher than -1
  163. if ($wrap_length > -1)
  164. {
  165. // Split all lines and wrap them individually
  166. $parts = explode("\n", $text);
  167. foreach ($parts as $k => $part)
  168. {
  169. preg_match('%^(>+ )?(.*)%', $part, $matches);
  170. $parts[$k] = wordwrap($matches[1].$matches[2], $wrap_length -
  171. strlen($matches[1]), "\n".$matches[1]);
  172. }
  173. return implode("\n", $parts);
  174. }
  175. else
  176. return $text;
  177. }
  178. //
  179. // Wrapper for PHP's mail()
  180. //
  181. function pun_mail($to, $subject, $message, $reply_to_email = '', $reply_to_name = '')
  182. {
  183. global $pun_config, $lang_common;
  184. // Use \r\n for SMTP servers, the system's line ending for local mailers
  185. $smtp = $pun_config['o_smtp_host'] != '';
  186. $EOL = $smtp ? "\r\n" : FORUM_EOL;
  187. // Default sender/return address
  188. $from_name = sprintf($lang_common['Mailer'], $pun_config['o_board_title']);
  189. $from_email = $pun_config['o_webmaster_email'];
  190. // Do a little spring cleaning
  191. $to = pun_trim(preg_replace('%[\n\r]+%s', '', $to));
  192. $subject = pun_trim(preg_replace('%[\n\r]+%s', '', $subject));
  193. $from_email = pun_trim(preg_replace('%[\n\r:]+%s', '', $from_email));
  194. $from_name = pun_trim(preg_replace('%[\n\r:]+%s', '', str_replace('"', '', $from_name)));
  195. $reply_to_email = pun_trim(preg_replace('%[\n\r:]+%s', '', $reply_to_email));
  196. $reply_to_name = pun_trim(preg_replace('%[\n\r:]+%s', '', str_replace('"', '', $reply_to_name)));
  197. // Set up some headers to take advantage of UTF-8
  198. $from = '"'.encode_mail_text($from_name).'" <'.$from_email.'>';
  199. $subject = encode_mail_text($subject);
  200. $headers = 'From: '.$from.$EOL.'Date: '.gmdate('r').$EOL.'MIME-Version: 1.0'.$EOL.'Content-transfer-encoding: 8bit'.$EOL.'Content-type: text/plain; charset=utf-8'.$EOL.'X-Mailer: FluxBB Mailer';
  201. // If we specified a reply-to email, we deal with it here
  202. if (!empty($reply_to_email))
  203. {
  204. $reply_to = '"'.encode_mail_text($reply_to_name).'" <'.$reply_to_email.'>';
  205. $headers .= $EOL.'Reply-To: '.$reply_to;
  206. }
  207. // Make sure all linebreaks are LF in message (and strip out any NULL bytes)
  208. $message = str_replace("\0", '', pun_linebreaks($message));
  209. $message = str_replace("\n", $EOL, $message);
  210. $mailer = $smtp ? 'smtp_mail' : 'mail';
  211. $mailer($to, $subject, $message, $headers);
  212. }
  213. //
  214. // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com)
  215. // They deserve all the credit for writing it. I made small modifications for it to suit PunBB and its coding standards
  216. //
  217. function server_parse($socket, $expected_response)
  218. {
  219. $server_response = '';
  220. while (substr($server_response, 3, 1) != ' ')
  221. {
  222. if (!($server_response = fgets($socket, 256)))
  223. error('Couldn\'t get mail server response codes. Please contact the forum administrator.', __FILE__, __LINE__);
  224. }
  225. if (!(substr($server_response, 0, 3) == $expected_response))
  226. error('Unable to send email. Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__);
  227. }
  228. //
  229. // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com)
  230. // They deserve all the credit for writing it. I made small modifications for it to suit PunBB and its coding standards.
  231. //
  232. function smtp_mail($to, $subject, $message, $headers = '')
  233. {
  234. global $pun_config;
  235. static $local_host;
  236. $recipients = explode(',', $to);
  237. // Sanitize the message
  238. $message = str_replace("\r\n.", "\r\n..", $message);
  239. $message = (substr($message, 0, 1) == '.' ? '.'.$message : $message);
  240. // Are we using port 25 or a custom port?
  241. if (strpos($pun_config['o_smtp_host'], ':') !== false)
  242. list($smtp_host, $smtp_port) = explode(':', $pun_config['o_smtp_host']);
  243. else
  244. {
  245. $smtp_host = $pun_config['o_smtp_host'];
  246. $smtp_port = 25;
  247. }
  248. if ($pun_config['o_smtp_ssl'] == '1')
  249. $smtp_host = 'ssl://'.$smtp_host;
  250. if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
  251. error('Could not connect to smtp host "'.$pun_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.')', __FILE__, __LINE__);
  252. server_parse($socket, '220');
  253. if (!isset($local_host))
  254. {
  255. // Here we try to determine the *real* hostname (reverse DNS entry preferably)
  256. $local_host = php_uname('n');
  257. // Able to resolve name to IP
  258. if (($local_addr = @gethostbyname($local_host)) !== $local_host)
  259. {
  260. // Able to resolve IP back to name
  261. if (($local_name = @gethostbyaddr($local_addr)) !== $local_addr)
  262. $local_host = $local_name;
  263. }
  264. }
  265. if ($pun_config['o_smtp_user'] != '' && $pun_config['o_smtp_pass'] != '')
  266. {
  267. fwrite($socket, 'EHLO '.$local_host."\r\n");
  268. server_parse($socket, '250');
  269. fwrite($socket, 'AUTH LOGIN'."\r\n");
  270. server_parse($socket, '334');
  271. fwrite($socket, base64_encode($pun_config['o_smtp_user'])."\r\n");
  272. server_parse($socket, '334');
  273. fwrite($socket, base64_encode($pun_config['o_smtp_pass'])."\r\n");
  274. server_parse($socket, '235');
  275. }
  276. else
  277. {
  278. fwrite($socket, 'HELO '.$local_host."\r\n");
  279. server_parse($socket, '250');
  280. }
  281. fwrite($socket, 'MAIL FROM: <'.$pun_config['o_webmaster_email'].'>'."\r\n");
  282. server_parse($socket, '250');
  283. foreach ($recipients as $email)
  284. {
  285. fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
  286. server_parse($socket, '250');
  287. }
  288. fwrite($socket, 'DATA'."\r\n");
  289. server_parse($socket, '354');
  290. fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
  291. fwrite($socket, '.'."\r\n");
  292. server_parse($socket, '250');
  293. fwrite($socket, 'QUIT'."\r\n");
  294. fclose($socket);
  295. return true;
  296. }