ManageNews.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /*
  15. void ManageNews()
  16. - the entrance point for all News and Newsletter screens.
  17. - called by ?action=admin;area=news.
  18. - does the permission checks.
  19. - calls the appropriate function based on the requested sub-action.
  20. void EditNews()
  21. - changes the current news items for the forum.
  22. - uses the ManageNews template and edit_news sub template.
  23. - called by ?action=admin;area=news.
  24. - requires the edit_news permission.
  25. - writes an entry into the moderation log.
  26. - uses the edit_news administration area.
  27. - can be accessed with ?action=admin;sa=editnews.
  28. void SelectMailingMembers()
  29. - allows a user to select the membergroups to send their mailing to.
  30. - uses the ManageNews template and email_members sub template.
  31. - called by ?action=admin;area=news;sa=mailingmembers.
  32. - requires the send_mail permission.
  33. - form is submitted to ?action=admin;area=news;mailingcompose.
  34. void ComposeMailing()
  35. - shows a form to edit a forum mailing and its recipients.
  36. - uses the ManageNews template and email_members_compose sub template.
  37. - called by ?action=admin;area=news;sa=mailingcompose.
  38. - requires the send_mail permission.
  39. - form is submitted to ?action=admin;area=news;sa=mailingsend.
  40. void SendMailing(bool clean_only = false)
  41. - handles the sending of the forum mailing in batches.
  42. - uses the ManageNews template and email_members_send sub template.
  43. - called by ?action=admin;area=news;sa=mailingsend
  44. - requires the send_mail permission.
  45. - redirects to itself when more batches need to be sent.
  46. - redirects to ?action=admin after everything has been sent.
  47. - if clean_only is set will only clean the variables, put them in context, then return.
  48. void NewsSettings()
  49. - set general news and newsletter settings and permissions.
  50. - uses the ManageNews template and news_settings sub template.
  51. - called by ?action=admin;area=news;sa=settings.
  52. - requires the forum_admin permission.
  53. */
  54. // The controller; doesn't do anything, just delegates.
  55. function ManageNews()
  56. {
  57. global $context, $txt, $scripturl;
  58. // First, let's do a quick permissions check for the best error message possible.
  59. isAllowedTo(array('edit_news', 'send_mail', 'admin_forum'));
  60. loadTemplate('ManageNews');
  61. // Format: 'sub-action' => array('function', 'permission')
  62. $subActions = array(
  63. 'editnews' => array('EditNews', 'edit_news'),
  64. 'mailingmembers' => array('SelectMailingMembers', 'send_mail'),
  65. 'mailingcompose' => array('ComposeMailing', 'send_mail'),
  66. 'mailingsend' => array('SendMailing', 'send_mail'),
  67. 'settings' => array('ModifyNewsSettings', 'admin_forum'),
  68. );
  69. // Default to sub action 'main' or 'settings' depending on permissions.
  70. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (allowedTo('edit_news') ? 'editnews' : (allowedTo('send_mail') ? 'mailingmembers' : 'settings'));
  71. // Have you got the proper permissions?
  72. isAllowedTo($subActions[$_REQUEST['sa']][1]);
  73. // Create the tabs for the template.
  74. $context[$context['admin_menu_name']]['tab_data'] = array(
  75. 'title' => $txt['news_title'],
  76. 'help' => 'edit_news',
  77. 'description' => $txt['admin_news_desc'],
  78. 'tabs' => array(
  79. 'editnews' => array(
  80. ),
  81. 'mailingmembers' => array(
  82. 'description' => $txt['news_mailing_desc'],
  83. ),
  84. 'settings' => array(
  85. 'description' => $txt['news_settings_desc'],
  86. ),
  87. ),
  88. );
  89. // Force the right area...
  90. if (substr($_REQUEST['sa'], 0, 7) == 'mailing')
  91. $context[$context['admin_menu_name']]['current_subsection'] = 'mailingmembers';
  92. $subActions[$_REQUEST['sa']][0]();
  93. }
  94. // Let the administrator(s) edit the news.
  95. function EditNews()
  96. {
  97. global $txt, $modSettings, $context, $sourcedir, $user_info;
  98. global $smcFunc;
  99. require_once($sourcedir . '/Subs-Post.php');
  100. // The 'remove selected' button was pressed.
  101. if (!empty($_POST['delete_selection']) && !empty($_POST['remove']))
  102. {
  103. checkSession();
  104. // Store the news temporarily in this array.
  105. $temp_news = explode("\n", $modSettings['news']);
  106. // Remove the items that were selected.
  107. foreach ($temp_news as $i => $news)
  108. if (in_array($i, $_POST['remove']))
  109. unset($temp_news[$i]);
  110. // Update the database.
  111. updateSettings(array('news' => implode("\n", $temp_news)));
  112. logAction('news');
  113. }
  114. // The 'Save' button was pressed.
  115. elseif (!empty($_POST['save_items']))
  116. {
  117. checkSession();
  118. foreach ($_POST['news'] as $i => $news)
  119. {
  120. if (trim($news) == '')
  121. unset($_POST['news'][$i]);
  122. else
  123. {
  124. $_POST['news'][$i] = $smcFunc['htmlspecialchars']($_POST['news'][$i], ENT_QUOTES);
  125. preparsecode($_POST['news'][$i]);
  126. }
  127. }
  128. // Send the new news to the database.
  129. updateSettings(array('news' => implode("\n", $_POST['news'])));
  130. // Log this into the moderation log.
  131. logAction('news');
  132. }
  133. // Ready the current news.
  134. foreach (explode("\n", $modSettings['news']) as $id => $line)
  135. $context['admin_current_news'][$id] = array(
  136. 'id' => $id,
  137. 'unparsed' => un_preparsecode($line),
  138. 'parsed' => preg_replace('~<([/]?)form[^>]*?[>]*>~i', '<em class="smalltext">&lt;$1form&gt;</em>', parse_bbc($line)),
  139. );
  140. $context['sub_template'] = 'edit_news';
  141. $context['page_title'] = $txt['admin_edit_news'];
  142. }
  143. function SelectMailingMembers()
  144. {
  145. global $txt, $context, $modSettings, $smcFunc;
  146. $context['page_title'] = $txt['admin_newsletters'];
  147. $context['sub_template'] = 'email_members';
  148. $context['groups'] = array();
  149. $postGroups = array();
  150. $normalGroups = array();
  151. // If we have post groups disabled then we need to give a "ungrouped members" option.
  152. if (empty($modSettings['permission_enable_postgroups']))
  153. {
  154. $context['groups'][0] = array(
  155. 'id' => 0,
  156. 'name' => $txt['membergroups_members'],
  157. 'member_count' => 0,
  158. );
  159. $normalGroups[0] = 0;
  160. }
  161. // Get all the extra groups as well as Administrator and Global Moderator.
  162. $request = $smcFunc['db_query']('', '
  163. SELECT mg.id_group, mg.group_name, mg.min_posts
  164. FROM {db_prefix}membergroups AS mg' . (empty($modSettings['permission_enable_postgroups']) ? '
  165. WHERE mg.min_posts = {int:min_posts}' : '') . '
  166. GROUP BY mg.id_group, mg.min_posts, mg.group_name
  167. ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
  168. array(
  169. 'min_posts' => -1,
  170. 'newbie_group' => 4,
  171. )
  172. );
  173. while ($row = $smcFunc['db_fetch_assoc']($request))
  174. {
  175. $context['groups'][$row['id_group']] = array(
  176. 'id' => $row['id_group'],
  177. 'name' => $row['group_name'],
  178. 'member_count' => 0,
  179. );
  180. if ($row['min_posts'] == -1)
  181. $normalGroups[$row['id_group']] = $row['id_group'];
  182. else
  183. $postGroups[$row['id_group']] = $row['id_group'];
  184. }
  185. $smcFunc['db_free_result']($request);
  186. // If we have post groups, let's count the number of members...
  187. if (!empty($postGroups))
  188. {
  189. $query = $smcFunc['db_query']('', '
  190. SELECT mem.id_post_group AS id_group, COUNT(*) AS member_count
  191. FROM {db_prefix}members AS mem
  192. WHERE mem.id_post_group IN ({array_int:post_group_list})
  193. GROUP BY mem.id_post_group',
  194. array(
  195. 'post_group_list' => $postGroups,
  196. )
  197. );
  198. while ($row = $smcFunc['db_fetch_assoc']($query))
  199. $context['groups'][$row['id_group']]['member_count'] += $row['member_count'];
  200. $smcFunc['db_free_result']($query);
  201. }
  202. if (!empty($normalGroups))
  203. {
  204. // Find people who are members of this group...
  205. $query = $smcFunc['db_query']('', '
  206. SELECT id_group, COUNT(*) AS member_count
  207. FROM {db_prefix}members
  208. WHERE id_group IN ({array_int:normal_group_list})
  209. GROUP BY id_group',
  210. array(
  211. 'normal_group_list' => $normalGroups,
  212. )
  213. );
  214. while ($row = $smcFunc['db_fetch_assoc']($query))
  215. $context['groups'][$row['id_group']]['member_count'] += $row['member_count'];
  216. $smcFunc['db_free_result']($query);
  217. // Also do those who have it as an additional membergroup - this ones more yucky...
  218. $query = $smcFunc['db_query']('', '
  219. SELECT mg.id_group, COUNT(*) AS member_count
  220. FROM {db_prefix}membergroups AS mg
  221. INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_string}
  222. AND mem.id_group != mg.id_group
  223. AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
  224. WHERE mg.id_group IN ({array_int:normal_group_list})
  225. GROUP BY mg.id_group',
  226. array(
  227. 'normal_group_list' => $normalGroups,
  228. 'blank_string' => '',
  229. )
  230. );
  231. while ($row = $smcFunc['db_fetch_assoc']($query))
  232. $context['groups'][$row['id_group']]['member_count'] += $row['member_count'];
  233. $smcFunc['db_free_result']($query);
  234. }
  235. // Any moderators?
  236. $request = $smcFunc['db_query']('', '
  237. SELECT COUNT(DISTINCT id_member) AS num_distinct_mods
  238. FROM {db_prefix}moderators
  239. LIMIT 1',
  240. array(
  241. )
  242. );
  243. list ($context['groups'][3]['member_count']) = $smcFunc['db_fetch_row']($request);
  244. $smcFunc['db_free_result']($request);
  245. $context['can_send_pm'] = allowedTo('pm_send');
  246. }
  247. // Email your members...
  248. function ComposeMailing()
  249. {
  250. global $txt, $sourcedir, $context, $smcFunc;
  251. // Start by finding any members!
  252. $toClean = array();
  253. if (!empty($_POST['members']))
  254. $toClean[] = 'members';
  255. if (!empty($_POST['exclude_members']))
  256. $toClean[] = 'exclude_members';
  257. if (!empty($toClean))
  258. {
  259. require_once($sourcedir . '/Subs-Auth.php');
  260. foreach ($toClean as $type)
  261. {
  262. // Remove the quotes.
  263. $_POST[$type] = strtr($_POST[$type], array('\\"' => '"'));
  264. preg_match_all('~"([^"]+)"~', $_POST[$type], $matches);
  265. $_POST[$type] = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST[$type]))));
  266. foreach ($_POST[$type] as $index => $member)
  267. if (strlen(trim($member)) > 0)
  268. $_POST[$type][$index] = $smcFunc['htmlspecialchars']($smcFunc['strtolower'](trim($member)));
  269. else
  270. unset($_POST[$type][$index]);
  271. // Find the members
  272. $_POST[$type] = implode(',', array_keys(findMembers($_POST[$type])));
  273. }
  274. }
  275. if (isset($_POST['member_list']) && is_array($_POST['member_list']))
  276. {
  277. $members = array();
  278. foreach ($_POST['member_list'] as $member_id)
  279. $members[] = (int) $member_id;
  280. $_POST['members'] = implode(',', $members);
  281. }
  282. if (isset($_POST['exclude_member_list']) && is_array($_POST['exclude_member_list']))
  283. {
  284. $members = array();
  285. foreach ($_POST['exclude_member_list'] as $member_id)
  286. $members[] = (int) $member_id;
  287. $_POST['exclude_members'] = implode(',', $members);
  288. }
  289. // Clean the other vars.
  290. SendMailing(true);
  291. // We need a couple strings from the email template file
  292. loadLanguage('EmailTemplates');
  293. // Get a list of all full banned users. Use their Username and email to find them. Only get the ones that can't login to turn off notification.
  294. $request = $smcFunc['db_query']('', '
  295. SELECT DISTINCT mem.id_member
  296. FROM {db_prefix}ban_groups AS bg
  297. INNER JOIN {db_prefix}ban_items AS bi ON (bg.id_ban_group = bi.id_ban_group)
  298. INNER JOIN {db_prefix}members AS mem ON (bi.id_member = mem.id_member)
  299. WHERE (bg.cannot_access = {int:cannot_access} OR bg.cannot_login = {int:cannot_login})
  300. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
  301. array(
  302. 'cannot_access' => 1,
  303. 'cannot_login' => 1,
  304. 'current_time' => time(),
  305. )
  306. );
  307. while ($row = $smcFunc['db_fetch_assoc']($request))
  308. $context['recipients']['exclude_members'][] = $row['id_member'];
  309. $smcFunc['db_free_result']($request);
  310. $request = $smcFunc['db_query']('', '
  311. SELECT DISTINCT bi.email_address
  312. FROM {db_prefix}ban_items AS bi
  313. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  314. WHERE (bg.cannot_access = {int:cannot_access} OR bg.cannot_login = {int:cannot_login})
  315. AND (COALESCE(bg.expire_time, 1=1) OR bg.expire_time > {int:current_time})
  316. AND bi.email_address != {string:blank_string}',
  317. array(
  318. 'cannot_access' => 1,
  319. 'cannot_login' => 1,
  320. 'current_time' => time(),
  321. 'blank_string' => '',
  322. )
  323. );
  324. $condition_array = array();
  325. $condition_array_params = array();
  326. $count = 0;
  327. while ($row = $smcFunc['db_fetch_assoc']($request))
  328. {
  329. $condition_array[] = '{string:email_' . $count . '}';
  330. $condition_array_params['email_' . $count++] = $row['email_address'];
  331. }
  332. if (!empty($condition_array))
  333. {
  334. $request = $smcFunc['db_query']('', '
  335. SELECT id_member
  336. FROM {db_prefix}members
  337. WHERE email_address IN(' . implode(', ', $condition_array) .')',
  338. $condition_array_params
  339. );
  340. while ($row = $smcFunc['db_fetch_assoc']($request))
  341. $context['recipients']['exclude_members'][] = $row['id_member'];
  342. }
  343. // Did they select moderators - if so add them as specific members...
  344. if ((!empty($context['recipients']['groups']) && in_array(3, $context['recipients']['groups'])) || (!empty($context['recipients']['exclude_groups']) && in_array(3, $context['recipients']['exclude_groups'])))
  345. {
  346. $request = $smcFunc['db_query']('', '
  347. SELECT DISTINCT mem.id_member AS identifier
  348. FROM {db_prefix}members AS mem
  349. INNER JOIN {db_prefix}moderators AS mods ON (mods.id_member = mem.id_member)
  350. WHERE mem.is_activated = {int:is_activated}',
  351. array(
  352. 'is_activated' => 1,
  353. )
  354. );
  355. while ($row = $smcFunc['db_fetch_assoc']($request))
  356. {
  357. if (in_array(3, $context['recipients']))
  358. $context['recipients']['exclude_members'][] = $row['identifier'];
  359. else
  360. $context['recipients']['members'][] = $row['identifier'];
  361. }
  362. $smcFunc['db_free_result']($request);
  363. }
  364. // For progress bar!
  365. $context['total_emails'] = count($context['recipients']['emails']);
  366. $request = $smcFunc['db_query']('', '
  367. SELECT MAX(id_member)
  368. FROM {db_prefix}members',
  369. array(
  370. )
  371. );
  372. list ($context['max_id_member']) = $smcFunc['db_fetch_row']($request);
  373. $smcFunc['db_free_result']($request);
  374. // Clean up the arrays.
  375. $context['recipients']['members'] = array_unique($context['recipients']['members']);
  376. $context['recipients']['exclude_members'] = array_unique($context['recipients']['exclude_members']);
  377. // Setup the template!
  378. $context['page_title'] = $txt['admin_newsletters'];
  379. $context['sub_template'] = 'email_members_compose';
  380. $context['default_subject'] = htmlspecialchars($context['forum_name'] . ': ' . $txt['subject']);
  381. $context['default_message'] = htmlspecialchars($txt['message'] . "\n\n" . $txt['regards_team'] . "\n\n" . '{$board_url}');
  382. }
  383. // Send out the mailing!
  384. function SendMailing($clean_only = false)
  385. {
  386. global $txt, $sourcedir, $context, $smcFunc;
  387. global $scripturl, $modSettings, $user_info;
  388. // How many to send at once? Quantity depends on whether we are queueing or not.
  389. $num_at_once = empty($modSettings['mail_queue']) ? 60 : 1000;
  390. // If by PM's I suggest we half the above number.
  391. if (!empty($_POST['send_pm']))
  392. $num_at_once /= 2;
  393. checkSession();
  394. // Where are we actually to?
  395. $context['start'] = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
  396. $context['email_force'] = !empty($_POST['email_force']) ? 1 : 0;
  397. $context['send_pm'] = !empty($_POST['send_pm']) ? 1 : 0;
  398. $context['total_emails'] = !empty($_POST['total_emails']) ? (int) $_POST['total_emails'] : 0;
  399. $context['max_id_member'] = !empty($_POST['max_id_member']) ? (int) $_POST['max_id_member'] : 0;
  400. $context['send_html'] = !empty($_POST['send_html']) ? '1' : '0';
  401. $context['parse_html'] = !empty($_POST['parse_html']) ? '1' : '0';
  402. // Create our main context.
  403. $context['recipients'] = array(
  404. 'groups' => array(),
  405. 'exclude_groups' => array(),
  406. 'members' => array(),
  407. 'exclude_members' => array(),
  408. 'emails' => array(),
  409. );
  410. // Have we any excluded members?
  411. if (!empty($_POST['exclude_members']))
  412. {
  413. $members = explode(',', $_POST['exclude_members']);
  414. foreach ($members as $member)
  415. if ($member >= $context['start'])
  416. $context['recipients']['exclude_members'][] = (int) $member;
  417. }
  418. // What about members we *must* do?
  419. if (!empty($_POST['members']))
  420. {
  421. $members = explode(',', $_POST['members']);
  422. foreach ($members as $member)
  423. if ($member >= $context['start'])
  424. $context['recipients']['members'][] = (int) $member;
  425. }
  426. // Cleaning groups is simple - although deal with both checkbox and commas.
  427. if (!empty($_POST['groups']))
  428. {
  429. if (is_array($_POST['groups']))
  430. {
  431. foreach ($_POST['groups'] as $group => $dummy)
  432. $context['recipients']['groups'][] = (int) $group;
  433. }
  434. else
  435. {
  436. $groups = explode(',', $_POST['groups']);
  437. foreach ($groups as $group)
  438. $context['recipients']['groups'][] = (int) $group;
  439. }
  440. }
  441. // Same for excluded groups
  442. if (!empty($_POST['exclude_groups']))
  443. {
  444. if (is_array($_POST['exclude_groups']))
  445. {
  446. foreach ($_POST['exclude_groups'] as $group => $dummy)
  447. $context['recipients']['exclude_groups'][] = (int) $group;
  448. }
  449. else
  450. {
  451. $groups = explode(',', $_POST['exclude_groups']);
  452. foreach ($groups as $group)
  453. $context['recipients']['exclude_groups'][] = (int) $group;
  454. }
  455. }
  456. // Finally - emails!
  457. if (!empty($_POST['emails']))
  458. {
  459. $addressed = array_unique(explode(';', strtr($_POST['emails'], array("\n" => ';', "\r" => ';', ',' => ';'))));
  460. foreach ($addressed as $curmem)
  461. {
  462. $curmem = trim($curmem);
  463. if ($curmem != '')
  464. $context['recipients']['emails'][$curmem] = $curmem;
  465. }
  466. }
  467. // If we're only cleaning drop out here.
  468. if ($clean_only)
  469. return;
  470. require_once($sourcedir . '/Subs-Post.php');
  471. // Save the message and its subject in $context
  472. $context['subject'] = htmlspecialchars($_POST['subject']);
  473. $context['message'] = htmlspecialchars($_POST['message']);
  474. // Prepare the message for sending it as HTML
  475. if (!$context['send_pm'] && !empty($_POST['send_html']))
  476. {
  477. // Prepare the message for HTML.
  478. if (!empty($_POST['parse_html']))
  479. $_POST['message'] = str_replace(array("\n", ' '), array('<br />' . "\n", '&nbsp; '), $_POST['message']);
  480. // This is here to prevent spam filters from tagging this as spam.
  481. if (preg_match('~\<html~i', $_POST['message']) == 0)
  482. {
  483. if (preg_match('~\<body~i', $_POST['message']) == 0)
  484. $_POST['message'] = '<html><head><title>' . $_POST['subject'] . '</title></head>' . "\n" . '<body>' . $_POST['message'] . '</body></html>';
  485. else
  486. $_POST['message'] = '<html>' . $_POST['message'] . '</html>';
  487. }
  488. }
  489. // Use the default time format.
  490. $user_info['time_format'] = $modSettings['time_format'];
  491. $variables = array(
  492. '{$board_url}',
  493. '{$current_time}',
  494. '{$latest_member.link}',
  495. '{$latest_member.id}',
  496. '{$latest_member.name}'
  497. );
  498. // We might need this in a bit
  499. $cleanLatestMember = empty($_POST['send_html']) || $context['send_pm'] ? un_htmlspecialchars($modSettings['latestRealName']) : $modSettings['latestRealName'];
  500. // Replace in all the standard things.
  501. $_POST['message'] = str_replace($variables,
  502. array(
  503. !empty($_POST['send_html']) ? '<a href="' . $scripturl . '">' . $scripturl . '</a>' : $scripturl,
  504. timeformat(forum_time(), false),
  505. !empty($_POST['send_html']) ? '<a href="' . $scripturl . '?action=profile;u=' . $modSettings['latestMember'] . '">' . $cleanLatestMember . '</a>' : ($context['send_pm'] ? '[url=' . $scripturl . '?action=profile;u=' . $modSettings['latestMember'] . ']' . $cleanLatestMember . '[/url]' : $cleanLatestMember),
  506. $modSettings['latestMember'],
  507. $cleanLatestMember
  508. ), $_POST['message']);
  509. $_POST['subject'] = str_replace($variables,
  510. array(
  511. $scripturl,
  512. timeformat(forum_time(), false),
  513. $modSettings['latestRealName'],
  514. $modSettings['latestMember'],
  515. $modSettings['latestRealName']
  516. ), $_POST['subject']);
  517. $from_member = array(
  518. '{$member.email}',
  519. '{$member.link}',
  520. '{$member.id}',
  521. '{$member.name}'
  522. );
  523. // If we still have emails, do them first!
  524. $i = 0;
  525. foreach ($context['recipients']['emails'] as $k => $email)
  526. {
  527. // Done as many as we can?
  528. if ($i >= $num_at_once)
  529. break;
  530. // Don't sent it twice!
  531. unset($context['recipients']['emails'][$k]);
  532. // Dammit - can't PM emails!
  533. if ($context['send_pm'])
  534. continue;
  535. $to_member = array(
  536. $email,
  537. !empty($_POST['send_html']) ? '<a href="mailto:' . $email . '">' . $email . '</a>' : $email,
  538. '??',
  539. $email
  540. );
  541. sendmail($email, str_replace($from_member, $to_member, $_POST['subject']), str_replace($from_member, $to_member, $_POST['message']), null, null, !empty($_POST['send_html']), 5);
  542. // Done another...
  543. $i++;
  544. }
  545. // Got some more to send this batch?
  546. $last_id_member = 0;
  547. if ($i < $num_at_once)
  548. {
  549. // Need to build quite a query!
  550. $sendQuery = '(';
  551. $sendParams = array();
  552. if (!empty($context['recipients']['groups']))
  553. {
  554. // Take the long route...
  555. $queryBuild = array();
  556. foreach ($context['recipients']['groups'] as $group)
  557. {
  558. $sendParams['group_' . $group] = $group;
  559. $queryBuild[] = 'mem.id_group = {int:group_' . $group . '}';
  560. if (!empty($group))
  561. {
  562. $queryBuild[] = 'FIND_IN_SET({int:group_' . $group . '}, mem.additional_groups) != 0';
  563. $queryBuild[] = 'mem.id_post_group = {int:group_' . $group . '}';
  564. }
  565. }
  566. if (!empty($queryBuild))
  567. $sendQuery .= implode(' OR ', $queryBuild);
  568. }
  569. if (!empty($context['recipients']['members']))
  570. {
  571. $sendQuery .= ($sendQuery == '(' ? '' : ' OR ') . 'mem.id_member IN ({array_int:members})';
  572. $sendParams['members'] = $context['recipients']['members'];
  573. }
  574. $sendQuery .= ')';
  575. // If we've not got a query then we must be done!
  576. if ($sendQuery == '()')
  577. redirectexit('action=admin');
  578. // Anything to exclude?
  579. if (!empty($context['recipients']['exclude_groups']) && in_array(0, $context['recipients']['exclude_groups']))
  580. $sendQuery .= ' AND mem.id_group != {int:regular_group}';
  581. if (!empty($context['recipients']['exclude_members']))
  582. {
  583. $sendQuery .= ' AND mem.id_member NOT IN ({array_int:exclude_members})';
  584. $sendParams['exclude_members'] = $context['recipients']['exclude_members'];
  585. }
  586. // Force them to have it?
  587. if (empty($context['email_force']))
  588. $sendQuery .= ' AND mem.notify_announcements = {int:notify_announcements}';
  589. // Get the smelly people - note we respect the id_member range as it gives us a quicker query.
  590. $result = $smcFunc['db_query']('', '
  591. SELECT mem.id_member, mem.email_address, mem.real_name, mem.id_group, mem.additional_groups, mem.id_post_group
  592. FROM {db_prefix}members AS mem
  593. WHERE mem.id_member > {int:min_id_member}
  594. AND mem.id_member < {int:max_id_member}
  595. AND ' . $sendQuery . '
  596. AND mem.is_activated = {int:is_activated}
  597. ORDER BY mem.id_member ASC
  598. LIMIT {int:atonce}',
  599. array_merge($sendParams, array(
  600. 'min_id_member' => $context['start'],
  601. 'max_id_member' => $context['start'] + $num_at_once - $i,
  602. 'atonce' => $num_at_once - $i,
  603. 'regular_group' => 0,
  604. 'notify_announcements' => 1,
  605. 'is_activated' => 1,
  606. ))
  607. );
  608. while ($row = $smcFunc['db_fetch_assoc']($result))
  609. {
  610. $last_id_member = $row['id_member'];
  611. // What groups are we looking at here?
  612. if (empty($row['additional_groups']))
  613. $groups = array($row['id_group'], $row['id_post_group']);
  614. else
  615. $groups = array_merge(
  616. array($row['id_group'], $row['id_post_group']),
  617. explode(',', $row['additional_groups'])
  618. );
  619. // Excluded groups?
  620. if (array_intersect($groups, $context['recipients']['exclude_groups']))
  621. continue;
  622. // We might need this
  623. $cleanMemberName = empty($_POST['send_html']) || $context['send_pm'] ? un_htmlspecialchars($row['real_name']) : $row['real_name'];
  624. // Replace the member-dependant variables
  625. $message = str_replace($from_member,
  626. array(
  627. $row['email_address'],
  628. !empty($_POST['send_html']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $cleanMemberName . '</a>' : ($context['send_pm'] ? '[url=' . $scripturl . '?action=profile;u=' . $row['id_member'] . ']' . $cleanMemberName . '[/url]' : $cleanMemberName),
  629. $row['id_member'],
  630. $cleanMemberName,
  631. ), $_POST['message']);
  632. $subject = str_replace($from_member,
  633. array(
  634. $row['email_address'],
  635. $row['real_name'],
  636. $row['id_member'],
  637. $row['real_name'],
  638. ), $_POST['subject']);
  639. // Send the actual email - or a PM!
  640. if (!$context['send_pm'])
  641. sendmail($row['email_address'], $subject, $message, null, null, !empty($_POST['send_html']), 5);
  642. else
  643. sendpm(array('to' => array($row['id_member']), 'bcc' => array()), $subject, $message);
  644. }
  645. $smcFunc['db_free_result']($result);
  646. }
  647. // If used our batch assume we still have a member.
  648. if ($i >= $num_at_once)
  649. $last_id_member = $context['start'];
  650. // Or we didn't have one in range?
  651. elseif (empty($last_id_member) && $context['start'] + $num_at_once < $context['max_id_member'])
  652. $last_id_member = $context['start'] + $num_at_once;
  653. // If we have no id_member then we're done.
  654. elseif (empty($last_id_member) && empty($context['recipients']['emails']))
  655. {
  656. // Log this into the admin log.
  657. logAction('newsletter', array(), 'admin');
  658. redirectexit('action=admin');
  659. }
  660. $context['start'] = $last_id_member;
  661. // Working out progress is a black art of sorts.
  662. $percentEmails = $context['total_emails'] == 0 ? 0 : ((count($context['recipients']['emails']) / $context['total_emails']) * ($context['total_emails'] / ($context['total_emails'] + $context['max_id_member'])));
  663. $percentMembers = ($context['start'] / $context['max_id_member']) * ($context['max_id_member'] / ($context['total_emails'] + $context['max_id_member']));
  664. $context['percentage_done'] = round(($percentEmails + $percentMembers) * 100, 2);
  665. $context['page_title'] = $txt['admin_newsletters'];
  666. $context['sub_template'] = 'email_members_send';
  667. }
  668. function ModifyNewsSettings($return_config = false)
  669. {
  670. global $context, $sourcedir, $modSettings, $txt, $scripturl;
  671. $config_vars = array(
  672. array('title', 'settings'),
  673. // Inline permissions.
  674. array('permissions', 'edit_news', 'help' => ''),
  675. array('permissions', 'send_mail'),
  676. '',
  677. // Just the remaining settings.
  678. array('check', 'xmlnews_enable', 'onclick' => 'document.getElementById(\'xmlnews_maxlen\').disabled = !this.checked;'),
  679. array('text', 'xmlnews_maxlen', 10),
  680. );
  681. if ($return_config)
  682. return $config_vars;
  683. $context['page_title'] = $txt['admin_edit_news'] . ' - ' . $txt['settings'];
  684. $context['sub_template'] = 'show_settings';
  685. // Needed for the inline permission functions, and the settings template.
  686. require_once($sourcedir . '/ManagePermissions.php');
  687. require_once($sourcedir . '/ManageServer.php');
  688. // Wrap it all up nice and warm...
  689. $context['post_url'] = $scripturl . '?action=admin;area=news;save;sa=settings';
  690. $context['permissions_excluded'] = array(-1);
  691. // Add some javascript at the bottom...
  692. $context['settings_insert_below'] = '
  693. <script type="text/javascript"><!-- // --><![CDATA[
  694. document.getElementById("xmlnews_maxlen").disabled = !document.getElementById("xmlnews_enable").checked;
  695. // ]]></script>';
  696. // Saving the settings?
  697. if (isset($_GET['save']))
  698. {
  699. checkSession();
  700. saveDBSettings($config_vars);
  701. redirectexit('action=admin;area=news;sa=settings');
  702. }
  703. prepareDBSettingContext($config_vars);
  704. }
  705. ?>