ManageNews.php 26 KB

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