ManageMail.php 13 KB

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