PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/include/email.php

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