PageRenderTime 32ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/message/message.php

http://github.com/phpbb/phpbb
PHP | 282 lines | 145 code | 26 blank | 111 comment | 6 complexity | 66b344cd579112e165edd3b0fd8e9930 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\message;
  14. /**
  15. * Class message
  16. * Holds all information for an email and sends it in the end
  17. */
  18. class message
  19. {
  20. /** @var string */
  21. protected $server_name;
  22. /** @var string */
  23. protected $subject = '';
  24. /** @var string */
  25. protected $body = '';
  26. /** @var string */
  27. protected $template = '';
  28. /** @var array */
  29. protected $template_vars = array();
  30. /** @var string */
  31. protected $sender_ip = '';
  32. /** @var string */
  33. protected $sender_name = '';
  34. /** @var string */
  35. protected $sender_address = '';
  36. /** @var string */
  37. protected $sender_lang = '';
  38. /** @var string */
  39. protected $sender_id = '';
  40. /** @var string */
  41. protected $sender_username = '';
  42. /** @var string */
  43. protected $sender_jabber = '';
  44. /** @var int */
  45. protected $sender_notify_type = NOTIFY_EMAIL;
  46. /** @var array */
  47. protected $recipients;
  48. /**
  49. * Construct
  50. *
  51. * @param string $server_name Used for AntiAbuse header
  52. */
  53. public function __construct($server_name)
  54. {
  55. $this->server_name = $server_name;
  56. }
  57. /**
  58. * Set the subject of the email
  59. *
  60. * @param string $subject
  61. * @return null
  62. */
  63. public function set_subject($subject)
  64. {
  65. $this->subject = $subject;
  66. }
  67. /**
  68. * Set the body of the email text
  69. *
  70. * @param string $body
  71. * @return null
  72. */
  73. public function set_body($body)
  74. {
  75. $this->body = $body;
  76. }
  77. /**
  78. * Set the name of the email template to use
  79. *
  80. * @param string $template
  81. * @return null
  82. */
  83. public function set_template($template)
  84. {
  85. $this->template = $template;
  86. }
  87. /**
  88. * Set the array with the "template" data for the email
  89. *
  90. * @param array $template_vars
  91. * @return null
  92. */
  93. public function set_template_vars($template_vars)
  94. {
  95. $this->template_vars = $template_vars;
  96. }
  97. /**
  98. * Add a recipient from \phpbb\user
  99. *
  100. * @param array $user
  101. * @return null
  102. */
  103. public function add_recipient_from_user_row(array $user)
  104. {
  105. $this->add_recipient(
  106. $user['username'],
  107. $user['user_email'],
  108. $user['user_lang'],
  109. $user['user_notify_type'],
  110. $user['username'],
  111. $user['user_jabber']
  112. );
  113. }
  114. /**
  115. * Add a recipient
  116. *
  117. * @param string $recipient_name Displayed sender name
  118. * @param string $recipient_address Email address
  119. * @param string $recipient_lang
  120. * @param int $recipient_notify_type Used notification methods (Jabber, Email, ...)
  121. * @param string $recipient_username User Name (used for AntiAbuse header)
  122. * @param string $recipient_jabber
  123. * @return null
  124. */
  125. public function add_recipient($recipient_name, $recipient_address, $recipient_lang, $recipient_notify_type = NOTIFY_EMAIL, $recipient_username = '', $recipient_jabber = '')
  126. {
  127. $this->recipients[] = array(
  128. 'name' => $recipient_name,
  129. 'address' => $recipient_address,
  130. 'lang' => $recipient_lang,
  131. 'username' => $recipient_username,
  132. 'jabber' => $recipient_jabber,
  133. 'notify_type' => $recipient_notify_type,
  134. 'to_name' => $recipient_name,
  135. );
  136. }
  137. /**
  138. * Set the senders data from \phpbb\user object
  139. *
  140. * @param \phpbb\user $user
  141. * @return null
  142. */
  143. public function set_sender_from_user($user)
  144. {
  145. $this->set_sender(
  146. $user->ip,
  147. $user->data['username'],
  148. $user->data['user_email'],
  149. $user->lang_name,
  150. $user->data['user_id'],
  151. $user->data['username'],
  152. $user->data['user_jabber']
  153. );
  154. $this->set_sender_notify_type($user->data['user_notify_type']);
  155. }
  156. /**
  157. * Set the senders data
  158. *
  159. * @param string $sender_ip
  160. * @param string $sender_name Displayed sender name
  161. * @param string $sender_address Email address
  162. * @param string $sender_lang
  163. * @param int $sender_id User ID
  164. * @param string $sender_username User Name (used for AntiAbuse header)
  165. * @param string $sender_jabber
  166. * @return null
  167. */
  168. public function set_sender($sender_ip, $sender_name, $sender_address, $sender_lang = '', $sender_id = 0, $sender_username = '', $sender_jabber = '')
  169. {
  170. $this->sender_ip = $sender_ip;
  171. $this->sender_name = $sender_name;
  172. $this->sender_address = $sender_address;
  173. $this->sender_lang = $sender_lang;
  174. $this->sender_id = $sender_id;
  175. $this->sender_username = $sender_username;
  176. $this->sender_jabber = $sender_jabber;
  177. }
  178. /**
  179. * Which notification type should be used? Jabber, Email, ...?
  180. *
  181. * @param int $sender_notify_type
  182. * @return null
  183. */
  184. public function set_sender_notify_type($sender_notify_type)
  185. {
  186. $this->sender_notify_type = $sender_notify_type;
  187. }
  188. /**
  189. * Ok, now the same email if CC specified, but without exposing the user's email address
  190. *
  191. * @return null
  192. */
  193. public function cc_sender()
  194. {
  195. if (!count($this->recipients))
  196. {
  197. trigger_error('No email recipients specified');
  198. }
  199. if (!$this->sender_address)
  200. {
  201. trigger_error('No email sender specified');
  202. }
  203. $this->recipients[] = array(
  204. 'lang' => $this->sender_lang,
  205. 'address' => $this->sender_address,
  206. 'name' => $this->sender_name,
  207. 'username' => $this->sender_username,
  208. 'jabber' => $this->sender_jabber,
  209. 'notify_type' => $this->sender_notify_type,
  210. 'to_name' => $this->recipients[0]['to_name'],
  211. );
  212. }
  213. /**
  214. * Send the email
  215. *
  216. * @param \messenger $messenger
  217. * @param string $contact
  218. * @return null
  219. */
  220. public function send(\messenger $messenger, $contact)
  221. {
  222. if (!count($this->recipients))
  223. {
  224. return;
  225. }
  226. foreach ($this->recipients as $recipient)
  227. {
  228. $messenger->template($this->template, $recipient['lang']);
  229. $messenger->replyto($this->sender_address);
  230. $messenger->to($recipient['address'], $recipient['name']);
  231. $messenger->im($recipient['jabber'], $recipient['username']);
  232. $messenger->headers('X-AntiAbuse: Board servername - ' . $this->server_name);
  233. $messenger->headers('X-AntiAbuse: User IP - ' . $this->sender_ip);
  234. if ($this->sender_id)
  235. {
  236. $messenger->headers('X-AntiAbuse: User_id - ' . $this->sender_id);
  237. }
  238. if ($this->sender_username)
  239. {
  240. $messenger->headers('X-AntiAbuse: Username - ' . $this->sender_username);
  241. }
  242. $messenger->subject(htmlspecialchars_decode($this->subject));
  243. $messenger->assign_vars(array(
  244. 'BOARD_CONTACT' => $contact,
  245. 'TO_USERNAME' => htmlspecialchars_decode($recipient['to_name']),
  246. 'FROM_USERNAME' => htmlspecialchars_decode($this->sender_name),
  247. 'MESSAGE' => htmlspecialchars_decode($this->body))
  248. );
  249. if (count($this->template_vars))
  250. {
  251. $messenger->assign_vars($this->template_vars);
  252. }
  253. $messenger->send($recipient['notify_type']);
  254. }
  255. }
  256. }