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