comparison forum/Sources/ManageMail.php @ 76:e3e11437ecea website

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