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

/modules/PNphpBB2/includes/emailer.php

https://gitlab.com/bulwye/reliquerunt
PHP | 389 lines | 262 code | 66 blank | 61 comment | 41 complexity | b4056f1fb1f95778f5e281fce3c28378 MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. emailer.php
  4. -------------------
  5. begin : Sunday Aug. 12, 2001
  6. copyright : (C) 2001 The phpBB Group
  7. email : support@phpbb.com
  8. $Id: emailer.php,v 1.2 2006/04/28 17:49:44 adrianc602 Exp $
  9. ***************************************************************************/
  10. /***************************************************************************
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. ***************************************************************************/
  18. //
  19. // The emailer class has support for attaching files, that isn't implemented
  20. // in the 2.0 release but we can probable find some way of using it in a future
  21. // release
  22. //
  23. class emailer
  24. {
  25. var $msg, $subject, $extra_headers;
  26. var $addresses, $reply_to, $from;
  27. var $use_smtp;
  28. var $tpl_msg = array();
  29. function emailer($use_smtp)
  30. {
  31. $this->reset();
  32. $this->use_smtp = $use_smtp;
  33. $this->reply_to = $this->from = '';
  34. }
  35. // Resets all the data (address, template file, etc etc to default
  36. function reset()
  37. {
  38. $this->addresses = array();
  39. $this->vars = $this->msg = $this->extra_headers = '';
  40. }
  41. // Sets an email address to send to
  42. function email_address($address)
  43. {
  44. $this->addresses['to'] = trim($address);
  45. }
  46. function cc($address)
  47. {
  48. $this->addresses['cc'][] = trim($address);
  49. }
  50. function bcc($address)
  51. {
  52. $this->addresses['bcc'][] = trim($address);
  53. }
  54. function replyto($address)
  55. {
  56. $this->reply_to = trim($address);
  57. }
  58. function from($address)
  59. {
  60. $this->from = trim($address);
  61. }
  62. // set up subject for mail
  63. function set_subject($subject = '')
  64. {
  65. $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
  66. }
  67. // set up extra mail headers
  68. function extra_headers($headers)
  69. {
  70. $this->extra_headers .= trim($headers) . "\n";
  71. }
  72. function use_template($template_file, $template_lang = '')
  73. {
  74. global $board_config, $phpbb_root_path;
  75. if (trim($template_file) == '')
  76. {
  77. message_die(GENERAL_ERROR, 'No template file set', '', __LINE__, __FILE__);
  78. }
  79. if (trim($template_lang) == '')
  80. {
  81. $template_lang = $board_config['default_lang'];
  82. }
  83. if (empty($this->tpl_msg[$template_lang . $template_file]))
  84. {
  85. $tpl_file = $phpbb_root_path . 'language/lang_' . $template_lang . '/email/' . $template_file . '.tpl';
  86. if (!@file_exists(@phpbb_realpath($tpl_file)))
  87. {
  88. $tpl_file = $phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/email/' . $template_file . '.tpl';
  89. if (!@file_exists(@phpbb_realpath($tpl_file)))
  90. {
  91. message_die(GENERAL_ERROR, 'Could not find email template file :: ' . $template_file, '', __LINE__, __FILE__);
  92. }
  93. }
  94. if (!($fd = @fopen($tpl_file, 'r')))
  95. {
  96. message_die(GENERAL_ERROR, 'Failed opening template file :: ' . $tpl_file, '', __LINE__, __FILE__);
  97. }
  98. $this->tpl_msg[$template_lang . $template_file] = fread($fd, filesize($tpl_file));
  99. fclose($fd);
  100. }
  101. $this->msg = $this->tpl_msg[$template_lang . $template_file];
  102. return true;
  103. }
  104. // assign variables
  105. function assign_vars($vars)
  106. {
  107. $this->vars = (empty($this->vars)) ? $vars : $this->vars . $vars;
  108. }
  109. // Send the mail out to the recipients set previously in var $this->address
  110. function send()
  111. {
  112. global $board_config, $lang, $phpEx, $phpbb_root_path, $db;
  113. // Escape all quotes, else the eval will fail.
  114. $this->msg = str_replace ("'", "\'", $this->msg);
  115. $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);
  116. // Set vars
  117. reset ($this->vars);
  118. while (list($key, $val) = each($this->vars))
  119. {
  120. $$key = $val;
  121. }
  122. eval("\$this->msg = '$this->msg';");
  123. // Clear vars
  124. reset ($this->vars);
  125. while (list($key, $val) = each($this->vars))
  126. {
  127. unset($$key);
  128. }
  129. // We now try and pull a subject from the email body ... if it exists,
  130. // do this here because the subject may contain a variable
  131. $drop_header = '';
  132. $match = array();
  133. if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
  134. {
  135. $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject');
  136. $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
  137. }
  138. else
  139. {
  140. $this->subject = (($this->subject != '') ? $this->subject : 'No Subject');
  141. }
  142. if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match))
  143. {
  144. $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim($lang['ENCODING']);
  145. $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
  146. }
  147. else
  148. {
  149. $this->encoding = trim($lang['ENCODING']);
  150. }
  151. if ($drop_header != '')
  152. {
  153. $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
  154. }
  155. $to = $this->addresses['to'];
  156. $cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : '';
  157. $bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : '';
  158. // Build header
  159. // Begin PNphpBB2 Module
  160. // $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . (($this->from != '') ? "From: $this->from\n" : "From: " . $board_config['board_email'] . "\n") . "Return-Path: " . $board_config['board_email'] . "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $board_config['server_name'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . date('r', time()) . "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By phpBB2\n" . $this->extra_headers . (($cc != '') ? "Cc: $cc\n" : '') . (($bcc != '') ? "Bcc: $bcc\n" : '');
  161. $extra_headers = ($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : "Reply-to: " . $board_config['sitename'] . " <" . $board_config['board_email'] . ">\n";
  162. $extra_headers .= ($this->from != '') ? "From: $this->from\n" : "From: " . $board_config['sitename'] . " <" . $board_config['board_email'] . ">\n";
  163. $extra_headers .= "Return-Path: " . $board_config['sitename'] . " <" . $board_config['board_email'] . ">\n";
  164. $extra_headers .= "Message-ID: <" . md5(uniqid(time())) . "@" . $board_config['server_name'] . ">\n";
  165. $extra_headers .= "Content-type: text/plain; charset=" . $this->encoding . "\n";
  166. $extra_headers .= "Content-transfer-encoding: 8bit\n";
  167. $extra_headers .= "Date: " . date('r', time()) . "\n";
  168. $extra_headers .= "X-Mailer: PNphpBB2 " . $board_config['version'] ."\n";
  169. $extra_headers .= ($cc != '') ? "Cc: $cc\n" : '';
  170. $extra_headers .= ($bcc != '') ? "Bcc: $bcc\n" : '';
  171. $this->extra_headers = $extra_headers;
  172. // End PNphpBB2 Module
  173. // Send message ... removed $this->encode() from subject for time being
  174. if ( $this->use_smtp )
  175. {
  176. if ( !defined('SMTP_INCLUDED') )
  177. {
  178. include($phpbb_root_path . 'includes/smtp.' . $phpEx);
  179. }
  180. $result = smtpmail($to, $this->subject, $this->msg, $this->extra_headers);
  181. }
  182. else
  183. {
  184. $empty_to_header = ($to == '') ? TRUE : FALSE;
  185. // Begin PNphpBB2 Module (Attempt to fix email notification problem by mihil)
  186. // $to = ($to == '') ? (($board_config['sendmail_fix']) ? ' ' : 'Undisclosed-recipients:;') : $to;
  187. $to = ($to == '') ? (($board_config['sendmail_fix'] && !$this->use_smtp) ? ' ' : '') : $to;
  188. // End PNphpBB2 Module (Attempt to fix email notification problem by mihil)
  189. $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
  190. if (!$result && !$board_config['sendmail_fix'] && $empty_to_header)
  191. {
  192. $to = ' ';
  193. $sql = "UPDATE " . CONFIG_TABLE . "
  194. SET config_value = '1'
  195. WHERE config_name = 'sendmail_fix'";
  196. if (!$db->sql_query($sql))
  197. {
  198. message_die(GENERAL_ERROR, 'Unable to update config table', '', __LINE__, __FILE__, $sql);
  199. }
  200. $board_config['sendmail_fix'] = 1;
  201. $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
  202. }
  203. }
  204. // Did it work?
  205. if (!$result)
  206. {
  207. message_die(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result, '', __LINE__, __FILE__);
  208. }
  209. return true;
  210. }
  211. // Encodes the given string for proper display for this encoding ... nabbed
  212. // from php.net and modified. There is an alternative encoding method which
  213. // may produce lesd output but it's questionable as to its worth in this
  214. // scenario IMO
  215. function encode($str)
  216. {
  217. if ($this->encoding == '')
  218. {
  219. return $str;
  220. }
  221. // define start delimimter, end delimiter and spacer
  222. $end = "?=";
  223. $start = "=?$this->encoding?B?";
  224. $spacer = "$end\r\n $start";
  225. // determine length of encoded text within chunks and ensure length is even
  226. $length = 75 - strlen($start) - strlen($end);
  227. $length = floor($length / 2) * 2;
  228. // encode the string and split it into chunks with spacers after each chunk
  229. $str = chunk_split(base64_encode($str), $length, $spacer);
  230. // remove trailing spacer and add start and end delimiters
  231. $str = preg_replace('#' . preg_quote($spacer, '#') . '$#', '', $str);
  232. return $start . $str . $end;
  233. }
  234. //
  235. // Attach files via MIME.
  236. //
  237. function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay)
  238. {
  239. global $lang;
  240. $mime_boundary = "--==================_846811060==_";
  241. $this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=\"" . $lang['ENCODING'] . "\"\n\n" . $this->msg;
  242. if ($mime_filename)
  243. {
  244. $filename = $mime_filename;
  245. $encoded = $this->encode_file($filename);
  246. }
  247. $fd = fopen($filename, "r");
  248. $contents = fread($fd, filesize($filename));
  249. $this->mimeOut = "--" . $mime_boundary . "\n";
  250. $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=\"$szFilenameToDisplay\"\n";
  251. $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
  252. $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=\"$szFilenameToDisplay\"\n\n";
  253. if ( $mimetype == "message/rfc822" )
  254. {
  255. $this->mimeOut .= "From: ".$szFromAddress."\n";
  256. $this->mimeOut .= "To: ".$this->emailAddress."\n";
  257. $this->mimeOut .= "Date: ".date("D, d M Y H:i:s") . " UT\n";
  258. $this->mimeOut .= "Reply-To:".$szFromAddress."\n";
  259. $this->mimeOut .= "Subject: ".$this->mailSubject."\n";
  260. $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
  261. $this->mimeOut .= "MIME-Version: 1.0\n";
  262. }
  263. $this->mimeOut .= $contents."\n";
  264. $this->mimeOut .= "--" . $mime_boundary . "--" . "\n";
  265. return $out;
  266. // added -- to notify email client attachment is done
  267. }
  268. function getMimeHeaders($filename, $mime_filename="")
  269. {
  270. $mime_boundary = "--==================_846811060==_";
  271. if ($mime_filename)
  272. {
  273. $filename = $mime_filename;
  274. }
  275. $out = "MIME-Version: 1.0\n";
  276. $out .= "Content-Type: multipart/mixed;\n\tboundary=\"$mime_boundary\"\n\n";
  277. $out .= "This message is in MIME format. Since your mail reader does not understand\n";
  278. $out .= "this format, some or all of this message may not be legible.";
  279. return $out;
  280. }
  281. //
  282. // Split string by RFC 2045 semantics (76 chars per line, end with \r\n).
  283. //
  284. function myChunkSplit($str)
  285. {
  286. $stmp = $str;
  287. $len = strlen($stmp);
  288. $out = "";
  289. while ($len > 0)
  290. {
  291. if ($len >= 76)
  292. {
  293. $out .= substr($stmp, 0, 76) . "\r\n";
  294. $stmp = substr($stmp, 76);
  295. $len = $len - 76;
  296. }
  297. else
  298. {
  299. $out .= $stmp . "\r\n";
  300. $stmp = "";
  301. $len = 0;
  302. }
  303. }
  304. return $out;
  305. }
  306. //
  307. // Split the specified file up into a string and return it
  308. //
  309. function encode_file($sourcefile)
  310. {
  311. if (is_readable(phpbb_realpath($sourcefile)))
  312. {
  313. $fd = fopen($sourcefile, "r");
  314. $contents = fread($fd, filesize($sourcefile));
  315. $encoded = $this->myChunkSplit(base64_encode($contents));
  316. fclose($fd);
  317. }
  318. return $encoded;
  319. }
  320. } // class emailer
  321. ?>