ManageNews.php 27 KB

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