ManageMail.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. /**
  3. * This file is all about mail, how we love it so. In particular it handles the admin side of
  4. * mail configuration, as well as reviewing the mail queue - if enabled.
  5. * @todo refactor as controller-model.
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2011 Simple Machines
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.0
  15. */
  16. if (!defined('SMF'))
  17. die('Hacking attempt...');
  18. /**
  19. * Main dispatcher. This function checks permissions and passes control through to the relevant section.
  20. */
  21. function ManageMail()
  22. {
  23. global $context, $txt, $scripturl, $modSettings, $sourcedir;
  24. // You need to be an admin to edit settings!
  25. isAllowedTo('admin_forum');
  26. loadLanguage('Help');
  27. loadLanguage('ManageMail');
  28. // We'll need the utility functions from here.
  29. require_once($sourcedir . '/ManageServer.php');
  30. $context['page_title'] = $txt['mailqueue_title'];
  31. $context['sub_template'] = 'show_settings';
  32. $subActions = array(
  33. 'browse' => 'BrowseMailQueue',
  34. 'clear' => 'ClearMailQueue',
  35. 'settings' => 'ModifyMailSettings',
  36. );
  37. // By default we want to browse
  38. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'browse';
  39. $context['sub_action'] = $_REQUEST['sa'];
  40. // Load up all the tabs...
  41. $context[$context['admin_menu_name']]['tab_data'] = array(
  42. 'title' => $txt['mailqueue_title'],
  43. 'help' => '',
  44. 'description' => $txt['mailqueue_desc'],
  45. );
  46. // Call the right function for this sub-acton.
  47. $subActions[$_REQUEST['sa']]();
  48. }
  49. /**
  50. * Display the mail queue...
  51. */
  52. function BrowseMailQueue()
  53. {
  54. global $scripturl, $context, $modSettings, $txt, $smcFunc;
  55. global $sourcedir;
  56. // First, are we deleting something from the queue?
  57. if (isset($_REQUEST['delete']))
  58. {
  59. checkSession('post');
  60. $smcFunc['db_query']('', '
  61. DELETE FROM {db_prefix}mail_queue
  62. WHERE id_mail IN ({array_int:mail_ids})',
  63. array(
  64. 'mail_ids' => $_REQUEST['delete'],
  65. )
  66. );
  67. }
  68. // How many items do we have?
  69. $request = $smcFunc['db_query']('', '
  70. SELECT COUNT(*) AS queue_size, MIN(time_sent) AS oldest
  71. FROM {db_prefix}mail_queue',
  72. array(
  73. )
  74. );
  75. list ($mailQueueSize, $mailOldest) = $smcFunc['db_fetch_row']($request);
  76. $smcFunc['db_free_result']($request);
  77. $context['oldest_mail'] = empty($mailOldest) ? $txt['mailqueue_oldest_not_available'] : time_since(time() - $mailOldest);
  78. $context['mail_queue_size'] = comma_format($mailQueueSize);
  79. $listOptions = array(
  80. 'id' => 'mail_queue',
  81. 'title' => $txt['mailqueue_browse'],
  82. 'items_per_page' => 20,
  83. 'base_href' => $scripturl . '?action=admin;area=mailqueue',
  84. 'default_sort_col' => 'age',
  85. 'no_items_label' => $txt['mailqueue_no_items'],
  86. 'get_items' => array(
  87. 'function' => 'list_getMailQueue',
  88. ),
  89. 'get_count' => array(
  90. 'function' => 'list_getMailQueueSize',
  91. ),
  92. 'columns' => array(
  93. 'subject' => array(
  94. 'header' => array(
  95. 'value' => $txt['mailqueue_subject'],
  96. ),
  97. 'data' => array(
  98. 'function' => create_function('$rowData', '
  99. global $smcFunc;
  100. return $smcFunc[\'strlen\']($rowData[\'subject\']) > 50 ? sprintf(\'%1$s...\', htmlspecialchars($smcFunc[\'substr\']($rowData[\'subject\'], 0, 47))) : htmlspecialchars($rowData[\'subject\']);
  101. '),
  102. 'class' => 'smalltext',
  103. ),
  104. 'sort' => array(
  105. 'default' => 'subject',
  106. 'reverse' => 'subject DESC',
  107. ),
  108. ),
  109. 'recipient' => array(
  110. 'header' => array(
  111. 'value' => $txt['mailqueue_recipient'],
  112. ),
  113. 'data' => array(
  114. 'sprintf' => array(
  115. 'format' => '<a href="mailto:%1$s">%1$s</a>',
  116. 'params' => array(
  117. 'recipient' => true,
  118. ),
  119. ),
  120. 'class' => 'smalltext',
  121. ),
  122. 'sort' => array(
  123. 'default' => 'recipient',
  124. 'reverse' => 'recipient DESC',
  125. ),
  126. ),
  127. 'priority' => array(
  128. 'header' => array(
  129. 'value' => $txt['mailqueue_priority'],
  130. ),
  131. 'data' => array(
  132. 'function' => create_function('$rowData', '
  133. global $txt;
  134. // We probably have a text label with your priority.
  135. $txtKey = sprintf(\'mq_mpriority_%1$s\', $rowData[\'priority\']);
  136. // But if not, revert to priority 0.
  137. return isset($txt[$txtKey]) ? $txt[$txtKey] : $txt[\'mq_mpriority_1\'];
  138. '),
  139. 'class' => 'smalltext',
  140. ),
  141. 'sort' => array(
  142. 'default' => 'priority',
  143. 'reverse' => 'priority DESC',
  144. ),
  145. ),
  146. 'age' => array(
  147. 'header' => array(
  148. 'value' => $txt['mailqueue_age'],
  149. ),
  150. 'data' => array(
  151. 'function' => create_function('$rowData', '
  152. return time_since(time() - $rowData[\'time_sent\']);
  153. '),
  154. 'class' => 'smalltext',
  155. ),
  156. 'sort' => array(
  157. 'default' => 'time_sent',
  158. 'reverse' => 'time_sent DESC',
  159. ),
  160. ),
  161. 'check' => array(
  162. 'header' => array(
  163. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  164. ),
  165. 'data' => array(
  166. 'function' => create_function('$rowData', '
  167. return \'<input type="checkbox" name="delete[]" value="\' . $rowData[\'id_mail\'] . \'" class="input_check" />\';
  168. '),
  169. 'class' => 'smalltext',
  170. ),
  171. ),
  172. ),
  173. 'form' => array(
  174. 'href' => $scripturl . '?action=admin;area=mailqueue',
  175. 'include_start' => true,
  176. 'include_sort' => true,
  177. ),
  178. 'additional_rows' => array(
  179. array(
  180. 'position' => 'below_table_data',
  181. 'value' => '[<a href="' . $scripturl . '?action=admin;area=mailqueue;sa=clear;' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(\'' . $txt['mailqueue_clear_list_warning'] . '\');">' . $txt['mailqueue_clear_list'] . '</a>] <input type="submit" name="delete_redirects" value="' . $txt['delete'] . '" onclick="return confirm(\'' . $txt['quickmod_confirm'] . '\');" class="button_submit" />',
  182. ),
  183. ),
  184. );
  185. require_once($sourcedir . '/Subs-List.php');
  186. createList($listOptions);
  187. loadTemplate('ManageMail');
  188. $context['sub_template'] = 'browse';
  189. }
  190. /**
  191. * This function grabs the mail queue items from the database, according to the params given.
  192. *
  193. * @param int $start
  194. * @param int $items_per_page
  195. * @param string $sort
  196. */
  197. function list_getMailQueue($start, $items_per_page, $sort)
  198. {
  199. global $smcFunc, $txt;
  200. $request = $smcFunc['db_query']('', '
  201. SELECT
  202. id_mail, time_sent, recipient, priority, private, subject
  203. FROM {db_prefix}mail_queue
  204. ORDER BY {raw:sort}
  205. LIMIT {int:start}, {int:items_per_page}',
  206. array(
  207. 'start' => $start,
  208. 'sort' => $sort,
  209. 'items_per_page' => $items_per_page,
  210. )
  211. );
  212. $mails = array();
  213. while ($row = $smcFunc['db_fetch_assoc']($request))
  214. {
  215. // Private PM/email subjects and similar shouldn't be shown in the mailbox area.
  216. if (!empty($row['private']))
  217. $row['subject'] = $txt['personal_message'];
  218. $mails[] = $row;
  219. }
  220. $smcFunc['db_free_result']($request);
  221. return $mails;
  222. }
  223. /**
  224. * Returns the total count of items in the mail queue.
  225. */
  226. function list_getMailQueueSize()
  227. {
  228. global $smcFunc;
  229. // How many items do we have?
  230. $request = $smcFunc['db_query']('', '
  231. SELECT COUNT(*) AS queue_size
  232. FROM {db_prefix}mail_queue',
  233. array(
  234. )
  235. );
  236. list ($mailQueueSize) = $smcFunc['db_fetch_row']($request);
  237. $smcFunc['db_free_result']($request);
  238. return $mailQueueSize;
  239. }
  240. /**
  241. * Allows to view and modify the mail settings.
  242. *
  243. * @param bool $return_config
  244. */
  245. function ModifyMailSettings($return_config = false)
  246. {
  247. global $txt, $scripturl, $context, $settings, $birthdayEmails, $modSettings;
  248. loadLanguage('EmailTemplates');
  249. $body = $birthdayEmails[empty($modSettings['birthday_email']) ? 'happy_birthday' : $modSettings['birthday_email']]['body'];
  250. $subject = $birthdayEmails[empty($modSettings['birthday_email']) ? 'happy_birthday' : $modSettings['birthday_email']]['subject'];
  251. $emails = array();
  252. foreach ($birthdayEmails as $index => $dummy)
  253. $emails[$index] = $index;
  254. $config_vars = array(
  255. // Mail queue stuff, this rocks ;)
  256. array('check', 'mail_queue'),
  257. array('int', 'mail_limit'),
  258. array('int', 'mail_quantity'),
  259. '',
  260. // SMTP stuff.
  261. array('select', 'mail_type', array($txt['mail_type_default'], 'SMTP')),
  262. array('text', 'smtp_host'),
  263. array('text', 'smtp_port'),
  264. array('text', 'smtp_username'),
  265. array('password', 'smtp_password'),
  266. '',
  267. array('select', 'birthday_email', $emails, 'value' => empty($modSettings['birthday_email']) ? 'happy_birthday' : $modSettings['birthday_email'], 'javascript' => 'onchange="fetch_birthday_preview()"'),
  268. 'birthday_subject' => array('var_message', 'birthday_subject', 'var_message' => $birthdayEmails[empty($modSettings['birthday_email']) ? 'happy_birthday' : $modSettings['birthday_email']]['subject'], 'disabled' => true, 'size' => strlen($subject) + 3),
  269. 'birthday_body' => array('var_message', 'birthday_body', 'var_message' => nl2br($body), 'disabled' => true, 'size' => ceil(strlen($body) / 25)),
  270. );
  271. if ($return_config)
  272. return $config_vars;
  273. // Saving?
  274. if (isset($_GET['save']))
  275. {
  276. // Make the SMTP password a little harder to see in a backup etc.
  277. if (!empty($_POST['smtp_password'][1]))
  278. {
  279. $_POST['smtp_password'][0] = base64_encode($_POST['smtp_password'][0]);
  280. $_POST['smtp_password'][1] = base64_encode($_POST['smtp_password'][1]);
  281. }
  282. checkSession();
  283. // We don't want to save the subject and body previews.
  284. unset($config_vars['birthday_subject'], $config_vars['birthday_body']);
  285. saveDBSettings($config_vars);
  286. redirectexit('action=admin;area=mailqueue;sa=settings');
  287. }
  288. $context['post_url'] = $scripturl . '?action=admin;area=mailqueue;save;sa=settings';
  289. $context['settings_title'] = $txt['mailqueue_settings'];
  290. prepareDBSettingContext($config_vars);
  291. $context['settings_insert_above'] = '
  292. <script type="text/javascript"><!-- // --><![CDATA[
  293. var bDay = {';
  294. $i = 0;
  295. foreach ($birthdayEmails as $index => $email)
  296. {
  297. $is_last = ++$i == count($birthdayEmails);
  298. $context['settings_insert_above'] .= '
  299. ' . $index . ': {
  300. subject: ' . JavaScriptEscape($email['subject']) . ',
  301. body: ' . JavaScriptEscape(nl2br($email['body'])) . '
  302. }' . (!$is_last ? ',' : '');
  303. }
  304. $context['settings_insert_above'] .= '
  305. };
  306. function fetch_birthday_preview()
  307. {
  308. var index = document.getElementById(\'birthday_email\').value;
  309. document.getElementById(\'birthday_subject\').innerHTML = bDay[index].subject;
  310. document.getElementById(\'birthday_body\').innerHTML = bDay[index].body;
  311. }
  312. // ]]></script>';
  313. }
  314. /**
  315. * This function clears the mail queue of all emails, and at the end redirects to browse.
  316. */
  317. function ClearMailQueue()
  318. {
  319. global $sourcedir, $smcFunc;
  320. checkSession('get');
  321. // This is certainly needed!
  322. require_once($sourcedir . '/ScheduledTasks.php');
  323. // If we don't yet have the total to clear, find it.
  324. if (!isset($_GET['te']))
  325. {
  326. // How many items do we have?
  327. $request = $smcFunc['db_query']('', '
  328. SELECT COUNT(*) AS queue_size
  329. FROM {db_prefix}mail_queue',
  330. array(
  331. )
  332. );
  333. list ($_GET['te']) = $smcFunc['db_fetch_row']($request);
  334. $smcFunc['db_free_result']($request);
  335. }
  336. else
  337. $_GET['te'] = (int) $_GET['te'];
  338. $_GET['sent'] = isset($_GET['sent']) ? (int) $_GET['sent'] : 0;
  339. // Send 50 at a time, then go for a break...
  340. while (ReduceMailQueue(50, true, true) === true)
  341. {
  342. // Sent another 50.
  343. $_GET['sent'] += 50;
  344. pauseMailQueueClear();
  345. }
  346. return BrowseMailQueue();
  347. }
  348. /**
  349. * Used for pausing the mail queue.
  350. */
  351. function pauseMailQueueClear()
  352. {
  353. global $context, $txt, $time_start;
  354. // Try get more time...
  355. @set_time_limit(600);
  356. if (function_exists('apache_reset_timeout'))
  357. @apache_reset_timeout();
  358. // Have we already used our maximum time?
  359. if (time() - array_sum(explode(' ', $time_start)) < 5)
  360. return;
  361. $context['continue_get_data'] = '?action=admin;area=mailqueue;sa=clear;te=' . $_GET['te'] . ';sent=' . $_GET['sent'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  362. $context['page_title'] = $txt['not_done_title'];
  363. $context['continue_post_data'] = '';
  364. $context['continue_countdown'] = '2';
  365. $context['sub_template'] = 'not_done';
  366. // Keep browse selected.
  367. $context['selected'] = 'browse';
  368. // What percent through are we?
  369. $context['continue_percent'] = round(($_GET['sent'] / $_GET['te']) * 100, 1);
  370. // Never more than 100%!
  371. $context['continue_percent'] = min($context['continue_percent'], 100);
  372. obExit();
  373. }
  374. /**
  375. * Little utility function to calculate how long ago a time was.
  376. *
  377. * @param long $time_diff
  378. */
  379. function time_since($time_diff)
  380. {
  381. global $txt;
  382. if ($time_diff < 0)
  383. $time_diff = 0;
  384. // Just do a bit of an if fest...
  385. if ($time_diff > 86400)
  386. {
  387. $days = round($time_diff / 86400, 1);
  388. return sprintf($days == 1 ? $txt['mq_day'] : $txt['mq_days'], $time_diff / 86400);
  389. }
  390. // Hours?
  391. elseif ($time_diff > 3600)
  392. {
  393. $hours = round($time_diff / 3600, 1);
  394. return sprintf($hours == 1 ? $txt['mq_hour'] : $txt['mq_hours'], $hours);
  395. }
  396. // Minutes?
  397. elseif ($time_diff > 60)
  398. {
  399. $minutes = (int) ($time_diff / 60);
  400. return sprintf($minutes == 1 ? $txt['mq_minute'] : $txt['mq_minutes'], $minutes);
  401. }
  402. // Otherwise must be second
  403. else
  404. return sprintf($time_diff == 1 ? $txt['mq_second'] : $txt['mq_seconds'], $time_diff);
  405. }
  406. ?>