PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/includes/acp/acp_email.php

https://github.com/naderman/phpbb-orchestra
PHP | 287 lines | 222 code | 41 blank | 24 comment | 34 complexity | 1c576fa9a8f556bd05e6ddc0a17d5eac MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package acp
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * @package acp
  19. */
  20. class acp_email
  21. {
  22. var $u_action;
  23. function main($id, $mode)
  24. {
  25. global $config, $db, $user, $auth, $template, $cache;
  26. global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
  27. $user->add_lang('acp/email');
  28. $this->tpl_name = 'acp_email';
  29. $this->page_title = 'ACP_MASS_EMAIL';
  30. $form_key = 'acp_email';
  31. add_form_key($form_key);
  32. // Set some vars
  33. $submit = (isset($_POST['submit'])) ? true : false;
  34. $error = array();
  35. $usernames = request_var('usernames', '', true);
  36. $group_id = request_var('g', 0);
  37. $subject = utf8_normalize_nfc(request_var('subject', '', true));
  38. $message = utf8_normalize_nfc(request_var('message', '', true));
  39. // Do the job ...
  40. if ($submit)
  41. {
  42. // Error checking needs to go here ... if no subject and/or no message then skip
  43. // over the send and return to the form
  44. $use_queue = (isset($_POST['send_immediately'])) ? false : true;
  45. $priority = request_var('mail_priority_flag', MAIL_NORMAL_PRIORITY);
  46. if (!check_form_key($form_key))
  47. {
  48. $error[] = $user->lang['FORM_INVALID'];
  49. }
  50. if (!$subject)
  51. {
  52. $error[] = $user->lang['NO_EMAIL_SUBJECT'];
  53. }
  54. if (!$message)
  55. {
  56. $error[] = $user->lang['NO_EMAIL_MESSAGE'];
  57. }
  58. if (!sizeof($error))
  59. {
  60. if ($usernames)
  61. {
  62. // If giving usernames the admin is able to email inactive users too...
  63. $sql = 'SELECT username, user_email, user_jabber, user_notify_type, user_lang
  64. FROM ' . USERS_TABLE . '
  65. WHERE ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', explode("\n", $usernames))) . '
  66. AND user_allow_massemail = 1
  67. ORDER BY user_lang, user_notify_type'; // , SUBSTRING(user_email FROM INSTR(user_email, '@'))
  68. }
  69. else
  70. {
  71. if ($group_id)
  72. {
  73. $sql_ary = array(
  74. 'SELECT' => 'u.user_email, u.username, u.username_clean, u.user_lang, u.user_jabber, u.user_notify_type',
  75. 'FROM' => array(
  76. USERS_TABLE => 'u',
  77. USER_GROUP_TABLE => 'ug',
  78. ),
  79. 'WHERE' => 'ug.group_id = ' . $group_id . '
  80. AND ug.user_pending = 0
  81. AND u.user_id = ug.user_id
  82. AND u.user_allow_massemail = 1
  83. AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
  84. 'ORDER_BY' => 'u.user_lang, u.user_notify_type',
  85. );
  86. }
  87. else
  88. {
  89. $sql_ary = array(
  90. 'SELECT' => 'u.username, u.username_clean, u.user_email, u.user_jabber, u.user_lang, u.user_notify_type',
  91. 'FROM' => array(
  92. USERS_TABLE => 'u',
  93. ),
  94. 'WHERE' => 'u.user_allow_massemail = 1
  95. AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
  96. 'ORDER_BY' => 'u.user_lang, u.user_notify_type',
  97. );
  98. }
  99. // Mail banned or not
  100. if (!isset($_REQUEST['mail_banned_flag']))
  101. {
  102. $sql_ary['WHERE'] .= ' AND (b.ban_id IS NULL
  103. OR b.ban_exclude = 1)';
  104. $sql_ary['LEFT_JOIN'] = array(
  105. array(
  106. 'FROM' => array(
  107. BANLIST_TABLE => 'b',
  108. ),
  109. 'ON' => 'u.user_id = b.ban_userid',
  110. ),
  111. );
  112. }
  113. $sql = $db->sql_build_query('SELECT', $sql_ary);
  114. }
  115. $result = $db->sql_query($sql);
  116. $row = $db->sql_fetchrow($result);
  117. if (!$row)
  118. {
  119. $db->sql_freeresult($result);
  120. trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
  121. }
  122. $i = $j = 0;
  123. // Send with BCC, no more than 50 recipients for one mail (to not exceed the limit)
  124. $max_chunk_size = 50;
  125. $email_list = array();
  126. $old_lang = $row['user_lang'];
  127. $old_notify_type = $row['user_notify_type'];
  128. do
  129. {
  130. if (($row['user_notify_type'] == NOTIFY_EMAIL && $row['user_email']) ||
  131. ($row['user_notify_type'] == NOTIFY_IM && $row['user_jabber']) ||
  132. ($row['user_notify_type'] == NOTIFY_BOTH && ($row['user_email'] || $row['user_jabber'])))
  133. {
  134. if ($i == $max_chunk_size || $row['user_lang'] != $old_lang || $row['user_notify_type'] != $old_notify_type)
  135. {
  136. $i = 0;
  137. if (sizeof($email_list))
  138. {
  139. $j++;
  140. }
  141. $old_lang = $row['user_lang'];
  142. $old_notify_type = $row['user_notify_type'];
  143. }
  144. $email_list[$j][$i]['lang'] = $row['user_lang'];
  145. $email_list[$j][$i]['method'] = $row['user_notify_type'];
  146. $email_list[$j][$i]['email'] = $row['user_email'];
  147. $email_list[$j][$i]['name'] = $row['username'];
  148. $email_list[$j][$i]['jabber'] = $row['user_jabber'];
  149. $i++;
  150. }
  151. }
  152. while ($row = $db->sql_fetchrow($result));
  153. $db->sql_freeresult($result);
  154. // Send the messages
  155. include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
  156. include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  157. $messenger = new messenger($use_queue);
  158. $errored = false;
  159. for ($i = 0, $size = sizeof($email_list); $i < $size; $i++)
  160. {
  161. $used_lang = $email_list[$i][0]['lang'];
  162. $used_method = $email_list[$i][0]['method'];
  163. for ($j = 0, $list_size = sizeof($email_list[$i]); $j < $list_size; $j++)
  164. {
  165. $email_row = $email_list[$i][$j];
  166. $messenger->{((sizeof($email_list[$i]) == 1) ? 'to' : 'bcc')}($email_row['email'], $email_row['name']);
  167. $messenger->im($email_row['jabber'], $email_row['name']);
  168. }
  169. $messenger->template('admin_send_email', $used_lang);
  170. $messenger->headers('X-AntiAbuse: Board servername - ' . $config['server_name']);
  171. $messenger->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
  172. $messenger->headers('X-AntiAbuse: Username - ' . $user->data['username']);
  173. $messenger->headers('X-AntiAbuse: User IP - ' . $user->ip);
  174. $messenger->subject(htmlspecialchars_decode($subject));
  175. $messenger->set_mail_priority($priority);
  176. $messenger->assign_vars(array(
  177. 'CONTACT_EMAIL' => $config['board_contact'],
  178. 'MESSAGE' => htmlspecialchars_decode($message))
  179. );
  180. if (!($messenger->send($used_method)))
  181. {
  182. $errored = true;
  183. }
  184. }
  185. unset($email_list);
  186. $messenger->save_queue();
  187. if ($usernames)
  188. {
  189. $usernames = explode("\n", $usernames);
  190. add_log('admin', 'LOG_MASS_EMAIL', implode(', ', utf8_normalize_nfc($usernames)));
  191. }
  192. else
  193. {
  194. if ($group_id)
  195. {
  196. $group_name = get_group_name($group_id);
  197. }
  198. else
  199. {
  200. // Not great but the logging routine doesn't cope well with localising on the fly
  201. $group_name = $user->lang['ALL_USERS'];
  202. }
  203. add_log('admin', 'LOG_MASS_EMAIL', $group_name);
  204. }
  205. if (!$errored)
  206. {
  207. $message = ($use_queue) ? $user->lang['EMAIL_SENT_QUEUE'] : $user->lang['EMAIL_SENT'];
  208. trigger_error($message . adm_back_link($this->u_action));
  209. }
  210. else
  211. {
  212. $message = sprintf($user->lang['EMAIL_SEND_ERROR'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=logs&amp;mode=critical') . '">', '</a>');
  213. trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
  214. }
  215. }
  216. }
  217. // Exclude bots and guests...
  218. $sql = 'SELECT group_id
  219. FROM ' . GROUPS_TABLE . "
  220. WHERE group_name IN ('BOTS', 'GUESTS')";
  221. $result = $db->sql_query($sql);
  222. $exclude = array();
  223. while ($row = $db->sql_fetchrow($result))
  224. {
  225. $exclude[] = $row['group_id'];
  226. }
  227. $db->sql_freeresult($result);
  228. $select_list = '<option value="0"' . ((!$group_id) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_USERS'] . '</option>';
  229. $select_list .= group_select_options($group_id, $exclude);
  230. $s_priority_options = '<option value="' . MAIL_LOW_PRIORITY . '">' . $user->lang['MAIL_LOW_PRIORITY'] . '</option>';
  231. $s_priority_options .= '<option value="' . MAIL_NORMAL_PRIORITY . '" selected="selected">' . $user->lang['MAIL_NORMAL_PRIORITY'] . '</option>';
  232. $s_priority_options .= '<option value="' . MAIL_HIGH_PRIORITY . '">' . $user->lang['MAIL_HIGH_PRIORITY'] . '</option>';
  233. $template->assign_vars(array(
  234. 'S_WARNING' => (sizeof($error)) ? true : false,
  235. 'WARNING_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
  236. 'U_ACTION' => $this->u_action,
  237. 'S_GROUP_OPTIONS' => $select_list,
  238. 'USERNAMES' => $usernames,
  239. 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=acp_email&amp;field=usernames'),
  240. 'SUBJECT' => $subject,
  241. 'MESSAGE' => $message,
  242. 'S_PRIORITY_OPTIONS' => $s_priority_options)
  243. );
  244. }
  245. }
  246. ?>