Completed
Pull Request — development (#3073)
by John
10:11
created

ManageMaillist_Controller::action_settings()   F

Complexity

Conditions 20
Paths 2576

Size

Total Lines 133
Code Lines 70

Duplication

Lines 6
Ratio 4.51 %

Code Coverage

Tests 28
CRAP Score 144.7766

Importance

Changes 0
Metric Value
cc 20
eloc 70
nc 2576
nop 0
dl 6
loc 133
ccs 28
cts 87
cp 0.3217
crap 144.7766
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file contains maillist functions that are specifically done by administrators
5
 * and those with approve email permission
6
 *
7
 * @name      ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
10
 *
11
 * @version 1.1
12
 *
13
 */
14
15
/**
16
 * This class is the administration maillist controller.
17
 *
18
 *  - handles maillist configuration
19
 *  - handles the showing, repairing, deleting and bouncing failed emails
20
 *  - handles the adding / editing / removing of both filters and parsers
21
 *
22
 * @package Maillist
23
 */
24
class ManageMaillist_Controller extends Action_Controller
25
{
26
	/**
27
	 * Main dispatcher.
28
	 *
29
	 * This function checks permissions and passes control to the sub action.
30
	 *
31
	 * @event integrate_sa_manage_maillist Used to add more sub actions
32
	 * @see Action_Controller::action_index()
33
	 * @uses Maillist template
34
	 */
35
	public function action_index()
36
	{
37
		global $context, $txt;
38
39
		// Template & language
40
		theme()->getTemplates()->load('Maillist');
41
		theme()->getTemplates()->loadLanguageFile('Maillist');
42
43
		// All the functions available
44
		$subActions = array(
45
			'emaillist' => array($this, 'action_unapproved_email', 'permission' => 'approve_emails'),
46
			'approve' => array($this, 'action_approve_email', 'permission' => 'approve_emails'),
47
			'delete' => array($this, 'action_delete_email', 'permission' => 'approve_emails'),
48
			'bounce' => array($this, 'action_bounce_email', 'permission' => 'approve_emails'),
49
			'emailtemplates' => array($this, 'action_view_bounce_templates', 'permission' => 'approve_emails'),
50
			'view' => array($this, 'action_view_email', 'permission' => 'approve_emails'),
51
			'emailsettings' => array($this, 'action_settings', 'permission' => 'admin_forum'),
52
			'emailfilters' => array($this, 'action_list_filters', 'permission' => 'admin_forum'),
53
			'editfilter' => array($this, 'action_edit_filters', 'permission' => 'admin_forum'),
54
			'deletefilter' => array($this, 'action_delete_filters', 'permission' => 'admin_forum'),
55
			'emailparser' => array($this, 'action_list_parsers', 'permission' => 'admin_forum'),
56
			'editparser' => array($this, 'action_edit_parsers', 'permission' => 'admin_forum'),
57
			'deleteparser' => array($this, 'action_delete_parsers', 'permission' => 'admin_forum'),
58
			'sortparsers' => array($this, 'action_sort_parsers', 'permission' => 'admin_forum'),
59
			'sortfilters' => array($this, 'action_sort_filters', 'permission' => 'admin_forum'),
60
		);
61
62
		// Action Controller
63
		$action = new Action('manage_maillist');
64
65
		// Help is needed in most places, so load it up front
66
		require_once(SUBSDIR . '/Maillist.subs.php');
67
68
		// Create the title area for the template.
69
		$context[$context['admin_menu_name']]['tab_data'] = array(
70
			'title' => $txt['ml_admin_configuration'],
71
			'help' => 'maillist_help_short',
72
			'description' => $txt['ml_configuration_desc'],
73
		);
74
75
		// Default to sub action 'emaillist' if none was given, call integrate_sa_manage_maillist
76
		$subAction = isset($this->_req->query->sa) && isset($subActions[$this->_req->query->sa]) && (empty($subActions[$this->_req->query->sa]['permission']) || allowedTo($subActions[$this->_req->query->sa]['permission'])) ? $this->_req->query->sa : 'emaillist';
77
		$subAction = $action->initialize($subActions, $subAction);
78
79
		// Final bits
80
		$context['page_title'] = $txt['ml_admin_configuration'];
81
		$context['sub_action'] = $subAction;
82
83
		// If you have the permissions, then go Play
84
		$action->dispatch($subAction);
85
	}
86
87
	/**
88
	 * Main listing of failed emails.
89
	 *
90
	 * What it does
91
	 *
92
	 * - Shows the sender, key and subject of the email
93
	 * - Will show the found key if it was missing or possible sender if it was wrong
94
	 * - Icons/Actions to view, bounce, delete or approve a failure
95
	 * - Accessed by ?action=admin;area=maillist;sa=emaillist
96
	 *
97
	 * @event integrate_list_view_email_errors
98
	 * @uses showlist sub template
99
	 */
100
	public function action_unapproved_email()
101
	{
102
		global $context, $scripturl, $modSettings, $txt;
103
104
		// Set an id if none was supplied
105
		$id = $this->_req->getQuery('e_id', 'intval', 0);
106
		if (empty($id) || $id <= 0)
107
			$id = 0;
108
109
		createToken('admin-ml', 'get');
110
111
		// Build the list option array to display the email data
112
		$listOptions = array(
113
			'id' => 'view_email_errors',
114
			'title' => $txt['ml_emailerror'],
115
			'items_per_page' => $modSettings['defaultMaxMessages'],
116
			'no_items_label' => $txt['ml_emailerror_none'],
117
			'base_href' => $scripturl . '?action=admin;area=maillist',
118
			'default_sort_col' => 'id_email',
119
			'get_items' => array(
120
				'function' => array($this, 'list_maillist_unapproved'),
121
				'params' => array(
122
					$id,
123
				),
124
			),
125
			'get_count' => array(
126
				'function' => 'list_maillist_count_unapproved',
127
			),
128
			'columns' => array(
129
				'id_email' => array(
130
					'header' => array(
131
						'value' => $txt['id'],
132
						'class' => 'nowrap',
133
					),
134
					'data' => array(
135
						'db' => 'id_email',
136
					),
137
					'sort' => array(
138
						'default' => 'id_email ',
139
						'reverse' => 'id_email DESC',
140
					),
141
				),
142
				'error' => array(
143
					'header' => array(
144
						'value' => $txt['error'],
145
					),
146
					'data' => array(
147
						'function' => function ($rowData) {
148
							$error = $rowData['error_code'];
149
							if ($error === 'error_pm_not_found')
150
								return '<span class="error">' . $rowData['error'] . '<span>';
151
							else
152
								return $rowData['error'];
153
						},
154
					),
155
					'sort' => array(
156
						'default' => 'error ',
157
						'reverse' => 'error DESC',
158
					),
159
				),
160
				'subject' => array(
161
					'header' => array(
162
						'value' => $txt['subject'],
163
					),
164
					'data' => array(
165
						'db' => 'subject',
166
					),
167
					'sort' => array(
168
						'default' => 'subject',
169
						'reverse' => 'subject DESC',
170
					),
171
				),
172
				'key' => array(
173
					'header' => array(
174
						'value' => $txt['key'],
175
					),
176
					'data' => array(
177
						'db' => 'key',
178
						'class' => 'wordbreak'
179
					),
180
					'sort' => array(
181
						'default' => 'message_key',
182
						'reverse' => 'message_key DESC',
183
					),
184
				),
185
				'message' => array(
186
					'header' => array(
187
						'value' => $txt['message_id'],
188
					),
189
					'data' => array(
190
						'sprintf' => array(
191
							'format' => '<a href="%1$s">%2$s</a>',
192
							'params' => array(
193
								'link' => true,
194
								'message' => true,
195
							),
196
						),
197
					),
198
					'sort' => array(
199
						'default' => 'message_id',
200
						'reverse' => 'message_id DESC',
201
					),
202
				),
203
				'from' => array(
204
					'header' => array(
205
						'value' => $txt['from'],
206
					),
207
					'data' => array(
208
						'db' => 'from',
209
					),
210
					'sort' => array(
211
						'default' => 'email_from',
212
						'reverse' => 'email_from DESC',
213
					),
214
				),
215
				'type' => array(
216
					'header' => array(
217
						'value' => $txt['message_type'],
218
					),
219
					'data' => array(
220
						'function' => function ($rowData) {
221
							global $txt;
222
223
							// Do we have a type?
224
							if (empty($rowData['type']))
225
								return $txt['not_applicable'];
226
							// Personal?
227
							elseif ($rowData['type'] === 'p')
228
								return $txt['personal_message'];
229
							// New Topic?
230
							elseif ($rowData['type'] === 'x')
231
								return $txt['new_topic'];
232
							// Ah a Reply then
233
							else
234
								return $txt['topic'] . ' ' . $txt['reply'];
235
						},
236
					),
237
					'sort' => array(
238
						'default' => 'message_type',
239
						'reverse' => 'message_type DESC',
240
					),
241
				),
242
				'action' => array(
243
					'header' => array(
244
						'value' => $txt['message_action'],
245
					),
246
					'data' => array(
247
						'function' => function ($rowData) {
248
							global $context, $txt;
249
250
							$id = $rowData['id_email'] . ';';
251
							$commands = array();
252
							$security = $context['session_var'] . '=' . $context['session_id'] . ';' . $context['admin-ml_token_var'] . '=' . $context['admin-ml_token'];
253
254
							if ($rowData['error_code'] === 'error_pm_not_found')
255
								$commands[] = '<a href="?action=admin;area=maillist;sa=approve;item=' . $id . $security . '" onclick="return confirm(' . JavaScriptEscape($txt['pm_approve_warning']) . ') && submitThisOnce(this);"><i class="icon i-check" title="' . $txt['approve'] . '"></i></a>&nbsp;';
256
							else
257
								$commands[] = '<a href="?action=admin;area=maillist;sa=approve;item=' . $id . $security . '"><i class="icon i-check" title="' . $txt['approve'] . '"></i></a>&nbsp;';
258
259
							$commands[] = '<a href="?action=admin;area=maillist;sa=delete;item=' . $id . $security . '" onclick="return confirm(' . JavaScriptEscape($txt['delete_warning']) . ') && submitThisOnce(this);" accesskey="d"><i class="icon i-delete" title="' . $txt['delete'] . '"></i></a><br />';
260
							$commands[] = '<a href="?action=admin;area=maillist;sa=bounce;item=' . $id . $security . '"><i class="icon i-sign-out" title="' . $txt['bounce'] . '"></i></a>&nbsp;';
261
							$commands[] = '<a href="?action=admin;area=maillist;sa=view;item=' . $id . $security . '"><i class="icon i-view" title="' . $txt['view'] . '"></i></a>';
262
263
							return implode('', $commands);
264
						},
265
					),
266
					'class' => 'listaction',
267
				),
268
			),
269
			'form' => array(
270
				'href' => $scripturl . '?action=admin;area=maillist;sa=emaillist',
271
				'include_sort' => true,
272
				'include_start' => true,
273
			),
274
			'additional_rows' => array(
275
				array(
276
					'position' => 'top_of_list',
277
					'value' => isset($this->_req->session->email_error) ? '<div class="' . (isset($this->_req->session->email_error_type) ? 'successbox' : 'errorbox') . '">' . $this->_req->session->email_error . '</div>' : $txt['heading'],
278
				),
279
			),
280
		);
281
282
		// Clear any errors
283
		unset($_SESSION['email_error'], $_SESSION['email_error_type']);
284
285
		// Set the context values for the template
286
		$context['page_title'] = $txt['emailerror_title'];
287
		$context['sub_template'] = 'show_list';
288
		$context['default_list'] = 'view_email_errors';
289
290
		// Create the list.
291
		createList($listOptions);
292
	}
293
294
	/**
295
	 * Show a failed email for review by the moderation team
296
	 *
297
	 * What it does:
298
	 *
299
	 * - Will not show a PM if it has been identified as such
300
	 * - Accessed by ?action=admin;area=maillist;sa=view;item=?
301
	 *
302
	 * @uses show_email sub template
303
	 */
304
	public function action_view_email()
305
	{
306
		global $txt, $context;
307
308
		allowedTo('approve_emails');
309
		checkSession('get');
310
		validateToken('admin-ml', 'get');
311
312
		$id = (int) $this->_req->query->item;
313
		if (!empty($id))
314
		{
315
			// Load up the email details, no funny biz ;)
316
			$temp_email = list_maillist_unapproved($id);
317
318
			if (!empty($temp_email))
319
			{
320
				if ($temp_email[0]['type'] !== 'p' && allowedTo('approve_emails'))
321
				{
322
					// The raw email that failed
323
					$data = $temp_email[0]['body'];
324
325
					// Read/parse this message for viewing
326
					$controller = new Emailpost_Controller();
327
					$result = $controller->action_pbe_preview($data);
328
					$text = isset($result['body']) ? $result['body'] : '';
329
					$email_to = isset($result['to']) ? $result['to'] : '';
330
				}
331
				else
332
				{
333
					// PM's mean just that ...
334
					$text = $txt['noaccess'];
335
					$email_to = $txt['private'];
336
				}
337
			}
338
			else
339
				$text = $txt['badid'];
340
		}
341
		else
342
			$text = $txt['badid'];
343
344
		$parser = \BBC\ParserWrapper::instance();
345
346
		// Prep and show the template with what we found
347
		$context['body'] = $parser->parseEmail($text);
348
		$context['to'] = $txt['to'] . ' ' . (isset($email_to) ? $email_to : '');
349
		$context['notice_subject'] = isset($temp_email[0]['subject']) ? $txt['subject'] . ': ' . $temp_email[0]['subject'] : '';
350
		$context['notice_from'] = isset($temp_email[0]['from']) ? $txt['from'] . ': ' . $temp_email[0]['from'] : '';
351
		$context['page_title'] = $txt['show_notice'];
352
		$context['error_code'] = isset($temp_email[0]['error_code']) && isset($txt[$temp_email[0]['error_code']]) ? $txt[$temp_email[0]['error_code']] : '';
353
		$context['sub_template'] = 'show_email';
354
	}
355
356
	/**
357
	 * Deletes an entry from the database
358
	 *
359
	 * What it does:
360
	 *
361
	 * - Flushes the moderator menu todo numbers so the menu numbers update
362
	 * - Accessed by ?action=admin;area=maillist;sa=delete;item=?'
363
	 * - Redirects to ?action=admin;area=maillist;sa=emaillist
364
	 */
365
	public function action_delete_email()
366
	{
367
		allowedTo('approve_emails');
368
		checkSession('get');
369
		validateToken('admin-ml', 'get');
370
371
		$id = (int) $this->_req->query->item;
372
373
		// Remove this entry
374
		if (!empty($id))
375
			maillist_delete_error_entry($id);
376
377
		// Flush the cache
378
		Cache::instance()->remove('num_menu_errors');
379
380
		// Back to the failed list we go
381
		redirectexit('action=admin;area=maillist;sa=emaillist');
382
	}
383
384
	/**
385
	 * Attempts to approve and post a failed email
386
	 *
387
	 * What it does:
388
	 *
389
	 * - Reviews the data to see if the email error function fixed typical issues like key and wrong id
390
	 * - Submits the fixed email to the main function which will post it or fail it again
391
	 * - If successful will remove the entry from the failed log
392
	 * - Accessed by ?action=admin;area=maillist;sa=approve;item=?'
393
	 * - Redirects to action=admin;area=maillist;sa=emaillist
394
	 */
395
	public function action_approve_email()
396
	{
397
		global $txt;
398
399
		allowedTo('approve_emails');
400
		checkSession('get');
401
		validateToken('admin-ml', 'get');
402
403
		// Get the id to approve
404
		$id = (int) $this->_req->query->item;
405
406
		if (!empty($id) && $id !== -1)
407
		{
408
			// Load up the email data
409
			$temp_email = list_maillist_unapproved($id);
410
			if (!empty($temp_email))
411
			{
412
				// Do we have the needed data to approve this, after all it failed for a reason yes?
413
				if (!empty($temp_email[0]['key']) && (!in_array($temp_email[0]['error_code'], array('error_no_message', 'error_not_find_board', 'error_topic_gone'))))
414
				{
415
					// Set up the details needed to get this posted
416
					$force = true;
417
					$key = $temp_email[0]['key'];
418
					$data = $temp_email[0]['body'];
419
420
					// Unknown from email?  Update the message ONLY if we found an appropriate one during the error checking process
421
					if (in_array($temp_email[0]['error_code'], array('error_not_find_member', 'error_key_sender_match')))
422
					{
423
						// did we actually find a potential correct name, if so we post from the valid member
424
						$check_emails = array_pad(explode('=>', $temp_email[0]['from']), 2, '');
425
426
						if (!empty($check_emails[1]))
427
							$data = preg_replace('~(From: )(.*<)?(' . preg_quote(trim($check_emails[0])) . ')(>)?(\n)~i', '$1$2' . trim($check_emails[1]) . '$4$5', $data);
428
					}
429
430
					// Lets TRY AGAIN to make a post!
431
					include_once(CONTROLLERDIR . '/Emailpost.controller.php');
432
					$controller = new Emailpost_Controller();
433
					$text = $controller->action_pbe_post($data, $force, $key);
434
435
					// Assuming all went well, remove this entry and file since we are done.
436
					if ($text === true)
437
					{
438
						maillist_delete_error_entry($id);
439
440
						// Flush the menu count cache
441
						Cache::instance()->remove('num_menu_errors');
442
443
						$_SESSION['email_error'] = $txt['approved'];
444
						$_SESSION['email_error_type'] = 1;
445
					}
446
					else
447
						$_SESSION['email_error'] = $txt['error_approved'];
448
				}
449
				else
450
					$_SESSION['email_error'] = $txt['cant_approve'];
451
			}
452
			else
453
				$_SESSION['email_error'] = $txt['badid'];
454
		}
455
		else
456
			$_SESSION['email_error'] = $txt['badid'];
457
458
		// back to the list we go
459
		redirectexit('action=admin;area=maillist;sa=emaillist');
460
	}
461
462
	/**
463
	 * Allows the admin to choose from predefined and custom templates
464
	 *
465
	 * What it does:
466
	 *
467
	 * - Uses the selected template to send a bounce notification with
468
	 * details as specified by the template
469
	 * - Accessed by ?action=admin;area=maillist;sa=bounce;item=?'
470
	 * - Redirects to action=admin;area=maillist;sa=bounced
471
	 * - Provides {MEMBER}, {SCRIPTURL}, {FORUMNAME}, {REGARDS}, {SUBJECT}, {ERROR},
472
	 * {FORUMNAMESHORT}, {EMAILREGARDS} replaceable values to the template
473
	 *
474
	 * @uses bounce_email sub-template
475
	 */
476
	public function action_bounce_email()
477
	{
478
		global $context, $txt, $modSettings, $scripturl, $mbname;
479
480
		if (!isset($this->_req->query->bounce))
481
		{
482
			checkSession('get');
483
			validateToken('admin-ml', 'get');
484
		}
485
486
		require_once(SUBSDIR . '/Mail.subs.php');
487
488
		// We should have been sent an email ID
489
		if (isset($this->_req->query->item))
490
		{
491
			// Needs to be an int!
492
			$id = (int) $this->_req->query->item;
493
494
			// Load up the email details, no funny biz yall ;)
495
			$temp_email = list_maillist_unapproved($id);
496
497
			if (!empty($temp_email))
498
			{
499
				// Set the options
500
				$this->_req->post->item = (int) $temp_email[0]['id_email'];
501
				$fullerrortext = $txt[$temp_email[0]['error_code']];
502
503
				// Build the template selection area, first the standard ones
504
				$bounce = array('bounce', 'inform');
505
				foreach ($bounce as $k => $type)
506
				{
507
					$context['bounce_templates'][$k]['body'] = $txt['ml_' . $type . '_body'];
508
					$context['bounce_templates'][$k]['subject'] = $txt['ml_' . $type . '_subject'];
509
					$context['bounce_templates'][$k]['title'] = $txt['ml_' . $type . '_title'];
510
				}
511
512
				// And now any custom ones available for this moderator
513
				$context['bounce_templates'] += array_merge($context['bounce_templates'], maillist_templates('bnctpl', $txt['ml_bounce_template_subject_default']));
514
515
				// Replace all the variables in the templates
516
				foreach ($context['bounce_templates'] as $k => $name)
517
				{
518
					$context['bounce_templates'][$k]['body'] = strtr($name['body'], array(
519
						'{MEMBER}' => un_htmlspecialchars($temp_email[0]['name']),
520
						'{SCRIPTURL}' => $scripturl,
521
						'{FORUMNAME}' => $mbname,
522
						'{REGARDS}' => replaceBasicActionUrl($txt['regards_team']),
523
						'{SUBJECT}' => $temp_email[0]['subject'],
524
						'{ERROR}' => $fullerrortext,
525
						'{FORUMNAMESHORT}' => (!empty($modSettings['maillist_sitename']) ? $modSettings['maillist_sitename'] : $mbname),
526
						'{EMAILREGARDS}' => (!empty($modSettings['maillist_sitename_regards']) ? $modSettings['maillist_sitename_regards'] : ''),
527
					));
528
				}
529
			}
530
			else
531
				$context['settings_message'] = $txt['badid'];
532
		}
533
		else
534
			$context['settings_message'] = $txt['badid'];
535
536
		// Check if they are sending the notice
537
		if (isset($this->_req->query->bounce) && isset($temp_email))
538
		{
539
			checkSession('post');
540
			validateToken('admin-ml');
541
542
			// They did check the box, how else could they have posted
543
			if (isset($this->_req->post->warn_notify))
544
			{
545
				// lets make sure we have the items to send it
546
				$check_emails = explode('=>', $temp_email[0]['from']);
547
				$to = trim($check_emails[0]);
548
				$subject = trim($this->_req->post->warn_sub);
549
				$body = trim($this->_req->post->warn_body);
550
551
				if (empty($body) || empty($subject))
552
					$context['settings_message'] = $txt['bad_bounce'];
553
				else
554
				{
555
					// Time for someone to get a we're so sorry message!
556
					sendmail($to, $subject, $body, null, null, false, 5);
557
					redirectexit('action=admin;area=maillist;bounced');
558
				}
559
			}
560
		}
561
562
		// Prepare and show the template
563
		createToken('admin-ml');
564
		$context['warning_data'] = array('notify' => '', 'notify_subject' => '', 'notify_body' => '');
565
		$context['body'] = isset($fullerrortext) ? \BBC\ParserWrapper::instance()->parseEmail($fullerrortext) : '';
566
		$context['item'] = isset($this->_req->post->item) ? $this->_req->post->item : '';
567
		$context['notice_to'] = $txt['to'] . ' ' . isset($temp_email[0]['from']) ? $temp_email[0]['from'] : '';
0 ignored issues
show
Bug introduced by
The variable $temp_email does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
568
		$context['page_title'] = $txt['bounce_title'];
569
		$context['sub_template'] = 'bounce_email';
570
	}
571
572
	/**
573
	 * List all the filters in the system
574
	 *
575
	 * What it does:
576
	 *
577
	 * - Allows to add/edit or delete filters
578
	 * - Filters are used to alter text in a post, to remove crud that comes with emails
579
	 * - Filters can be defined as regex, the system will check it for valid syntax
580
	 * - Accessed by ?action=admin;area=maillist;sa=emailfilters;
581
	 *
582
	 * @event integrate_list_email_filter
583
	 */
584
	public function action_list_filters()
585
	{
586
		global $context, $scripturl, $txt, $modSettings;
587
588
		$id = 0;
589
590
		// Build the listoption array to display the filters
591
		$listOptions = array(
592
			'id' => 'email_filter',
593
			'title' => $txt['filters'],
594
			'items_per_page' => $modSettings['defaultMaxMessages'],
595
			'no_items_label' => $txt['no_filters'],
596
			'base_href' => $scripturl . '?action=admin;area=maillist;sa=emailfilters',
597
			'default_sort_col' => 'name',
598
			'get_items' => array(
599
				'function' => array($this, 'load_filter_parser'),
600
				'params' => array(
601
					$id,
602
					'filter'
603
				),
604
			),
605
			'get_count' => array(
606
				'function' => array($this, 'count_filter_parser'),
607
				'params' => array(
608
					$id,
609
					'filter'
610
				),
611
			),
612
			'columns' => array(
613
				'name' => array(
614
					'header' => array(
615
						'value' => $txt['filter_name'],
616
						'style' => 'white-space: nowrap;'
617
					),
618
					'data' => array(
619
						'db' => 'filter_name',
620
					),
621
					'sort' => array(
622
						'default' => 'filter_name, id_filter',
623
						'reverse' => 'filter_name DESC, id_filter DESC',
624
					),
625
				),
626
				'from' => array(
627
					'header' => array(
628
						'value' => $txt['filter_from'],
629
					),
630
					'data' => array(
631
						'db' => 'filter_from',
632
					),
633
					'sort' => array(
634
						'default' => 'filter_from, id_filter',
635
						'reverse' => 'filter_from DESC, id_filter DESC',
636
					),
637
				),
638
				'to' => array(
639
					'header' => array(
640
						'value' => $txt['filter_to'],
641
						'style' => 'width:10em;',
642
					),
643
					'data' => array(
644
						'db' => 'filter_to',
645
					),
646
					'sort' => array(
647
						'default' => 'filter_to, id_filter',
648
						'reverse' => 'filter_to DESC, id_filter DESC',
649
					),
650
				),
651
				'type' => array(
652
					'header' => array(
653
						'value' => $txt['filter_type'],
654
					),
655
					'data' => array(
656
						'db' => 'filter_type',
657
					),
658
					'sort' => array(
659
						'default' => 'filter_type, id_filter',
660
						'reverse' => 'filter_type DESC, id_filter DESC',
661
					),
662
				),
663
				'action' => array(
664
					'header' => array(
665
						'value' => $txt['message_action'],
666
						'class' => 'centertext',
667
					),
668
					'data' => array(
669
						'sprintf' => array(
670
							'format' => '<a href="?action=admin;area=maillist;sa=editfilter;f_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '">
671
										<i class="icon i-modify" title="' . $txt['modify'] . '"></i>
672
									</a>
673
									<a href="?action=admin;area=maillist;sa=deletefilter;f_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(' . JavaScriptEscape($txt['filter_delete_warning']) . ') && submitThisOnce(this);" accesskey="d">
674
										<i class="icon i-delete" title="' . $txt['delete'] . '"></i>
675
									</a>',
676
							'params' => array(
677
								'id_filter' => true,
678
							),
679
						),
680
						'class' => 'centertext',
681
						'style' => 'white-space:nowrap;',
682
					),
683
				),
684
			),
685
			'form' => array(
686
				'href' => $scripturl . '?action=admin;area=maillist;sa=editfilter;',
687
				'include_sort' => true,
688
				'include_start' => true,
689
				'hidden_fields' => array(
690
					$context['session_var'] => $context['session_id'],
691
				),
692
			),
693
			'additional_rows' => array(
694
				array(
695
					'position' => isset($this->_req->query->saved) ? 'top_of_list' : 'after_title',
696
					'value' => isset($this->_req->query->saved) ? '<div class="successbox">' . $txt['saved'] . '</div>' : $txt['filters_title'],
697
				),
698
				array(
699
					'position' => 'below_table_data',
700
					'class' => 'submitbutton',
701
					'value' => '
702
						<input type="submit" name="addfilter" value="' . $txt['add_filter'] . '" />
703
						<a class="linkbutton" href="' . $scripturl . '?action=admin;area=maillist;sa=sortfilters">' . $txt['sort_filter'] . '</a>',
704
				),
705
			),
706
		);
707
708
		// Set the context values
709
		$context['page_title'] = $txt['filters'];
710
		$context['sub_template'] = 'show_list';
711
		$context['default_list'] = 'email_filter';
712
713
		// Create the list.
714
		createList($listOptions);
715
	}
716
717
	/**
718
	 * Show a full list of all the filters in the system for drag/drop sorting
719
	 *
720
	 * @event integrate_list_sort_email_fp
721
	 */
722
	public function action_sort_filters()
723
	{
724
		global $context, $scripturl, $txt;
725
726
		$id = 0;
727
		$token = createToken('admin-sort');
728
729
		// build the listoption array to display the data
730
		$listOptions = array(
731
			'id' => 'sort_email_fp',
732
			'title' => $txt['sort_filter'],
733
			'sortable' => true,
734
			'items_per_page' => 0,
735
			'no_items_label' => $txt['no_filters'],
736
			'base_href' => $scripturl . '?action=admin;area=maillist;sa=sortfilters',
737
			'get_items' => array(
738
				'function' => array($this, 'load_filter_parser'),
739
				'params' => array(
740
					$id,
741
					'filter'
742
				),
743
			),
744
			'get_count' => array(
745
				'function' => array($this, 'count_filter_parser'),
746
				'params' => array(
747
					$id,
748
					'filter'
749
				),
750
			),
751
			'columns' => array(
752
				'filterorder' => array(
753
					'header' => array(
754
						'value' => '',
755
						'class' => 'hide',
756
					),
757
					'data' => array(
758
						'db' => 'filter_order',
759
						'class' => 'hide',
760
					),
761
				),
762
				'name' => array(
763
					'header' => array(
764
						'value' => $txt['filter_name'],
765
						'style' => 'white-space: nowrap;width: 10em'
766
					),
767
					'data' => array(
768
						'db' => 'filter_name',
769
					),
770
				),
771
				'from' => array(
772
					'header' => array(
773
						'value' => $txt['filter_from'],
774
					),
775
					'data' => array(
776
						'db' => 'filter_from',
777
					),
778
				),
779
				'to' => array(
780
					'header' => array(
781
						'value' => $txt['filter_to'],
782
						'style' => 'width:10em;',
783
					),
784
					'data' => array(
785
						'db' => 'filter_to',
786
					),
787
				),
788
				'type' => array(
789
					'header' => array(
790
						'value' => $txt['filter_type'],
791
					),
792
					'data' => array(
793
						'db' => 'filter_type',
794
					),
795
				),
796
			),
797
			'form' => array(
798
				'href' => $scripturl . '?action=admin;area=maillist;sa=sortfilters',
799
				'hidden_fields' => array(
800
					$context['session_var'] => $context['session_id'],
801
				),
802
			),
803
			'additional_rows' => array(
804
				array(
805
					'position' => 'after_title',
806
					'value' => $txt['filter_sort_description'],
807
				),
808
			),
809
			'javascript' => '
810
				$().elkSortable({
811
					sa: "parserorder",
812
					placeholder: "ui-state-highlight",
813
					containment: "#sort_email_fp",
814
					error: "' . $txt['admin_order_error'] . '",
815
					title: "' . $txt['admin_order_title'] . '",
816
					href: "?action=admin;area=maillist;sa=sortfilters",
817
					token: {token_var: "' . $token['admin-sort_token_var'] . '", token_id: "' . $token['admin-sort_token'] . '"}
818
				});
819
			',
820
		);
821
822
		// Set the context values
823
		$context['page_title'] = $txt['filters'];
824
		$context['sub_template'] = 'show_list';
825
		$context['default_list'] = 'sort_email_fp';
826
		$context[$context['admin_menu_name']]['current_subsection'] = 'emailfilters';
827
828
		// Create the list.
829
		createList($listOptions);
830
	}
831
832
	/**
833
	 * Returns the number of filters or parsers in the system
834
	 *
835
	 * - Callback for createList()
836
	 *
837
	 * @param int $id 0 for all of a certain style
838
	 * @param string $style one of filter or parser
839
	 */
840
	public function count_filter_parser($id, $style)
841
	{
842
		return list_count_filter_parser($id, $style);
843
	}
844
845
	/**
846
	 * Returns the details for the filters or parsers in the system
847
	 *
848
	 * - Callback for createList()
849
	 *
850
	 * @param int $start The item to start with (for pagination purposes)
851
	 * @param int $items_per_page The number of items to show per page
852
	 * @param string $sort A string indicating how to sort the results
853
	 * @param int $id
854
	 * @param string $style
855
	 */
856
	public function load_filter_parser($start, $items_per_page, $sort, $id, $style)
857
	{
858
		return list_get_filter_parser($start, $items_per_page, $sort, $id, $style);
859
	}
860
861
	/**
862
	 * Edit or Add a filter
863
	 *
864
	 * - If regex will check for proper syntax before saving to the database
865
	 *
866
	 * @event integrate_save_filter_settings
867
	 *
868
	 */
869
	public function action_edit_filters()
870
	{
871
		global $context, $scripturl, $txt, $modSettings;
872
873
		// Editing an existing filter?
874
		if (isset($this->_req->query->f_id))
875
		{
876
			// Needs to be an int!
877
			$id = (int) $this->_req->query->f_id;
878
			if (empty($id) || $id <= 0)
879
				throw new Elk_Exception('error_no_id_filter');
880
881
			// Load it up and set it as the current values
882
			$row = maillist_load_filter_parser($id, 'filter');
883
			$modSettings['id_filter'] = $row['id_filter'];
884
			$modSettings['filter_type'] = $row['filter_type'];
885
			$modSettings['filter_to'] = $row['filter_to'];
886
			$modSettings['filter_from'] = $row['filter_from'];
887
			$modSettings['filter_name'] = $row['filter_name'];
888
889
			// Some items for the form
890
			$context['page_title'] = $txt['edit_filter'];
891
			$context['editing'] = true;
892
			$context['settings_message'] = array();
893
		}
894
		else
895
		{
896
			// Setup place holders for adding a new one instead
897
			$modSettings['filter_type'] = '';
898
			$modSettings['filter_to'] = '';
899
			$modSettings['filter_from'] = '';
900
			$modSettings['filter_name'] = '';
901
902
			$context['page_title'] = $txt['add_filter'];
903
			$context['editing'] = false;
904
			$context['settings_message'] = array();
905
		}
906
907
		// Initialize the form
908
		$settingsForm = new Settings_Form(Settings_Form::DB_ADAPTER);
909
910
		// Initialize it with our settings
911
		$config_vars = $this->_filtersSettings();
912
		$settingsForm->setConfigVars($config_vars);
913
914
		// Saving the new or edited entry?
915
		if (isset($this->_req->query->save))
916
		{
917
			checkSession();
918
919
			call_integration_hook('integrate_save_filter_settings');
920
921
			// Editing an entry?
922
			$editId = (isset($this->_req->query->edit)) ? (int) $this->_req->query->edit : -1;
923
			$editName = (isset($this->_req->query->edit)) ? 'id_filter' : '';
924
925
			// If its regex we do a quick check to see if its valid or not
926
			if ($this->_req->post->filter_type === 'regex')
927
			{
928
				$valid = (@preg_replace($this->_req->post->filter_from, $this->_req->post->filter_to, 'ElkArte') === null) ? false : true;
929
				if (!$valid)
930
				{
931
					// Seems to be bad ... reload the form, set the message
932
					$context['error_type'] = 'notice';
933
					$context['settings_message'][] = $txt['regex_invalid'];
934
					$modSettings['filter_type'] = $this->_req->post->filter_type;
935
					$modSettings['filter_to'] = $this->_req->post->filter_to;
936
					$modSettings['filter_from'] = $this->_req->post->filter_from;
937
					$modSettings['filter_name'] = $this->_req->post->filter_name;
938
				}
939
			}
940
941 View Code Duplication
			if (empty($this->_req->post->filter_type) || empty($this->_req->post->filter_from))
942
			{
943
				$context['error_type'] = 'notice';
944
				$context['settings_message'][] = $txt['filter_invalid'];
945
			}
946
947
			// if we are good to save, so save it ;)
948 View Code Duplication
			if (empty($context['settings_message']))
949
			{
950
				// And ... its a filter
951
				$config_vars[] = array('text', 'filter_style');
952
				$this->_req->post->filter_style = 'filter';
953
954
				Email_Settings::saveTableSettings($config_vars, 'postby_emails_filters', $this->_req->post, array('id_filter'), $editId, $editName);
955
				redirectexit('action=admin;area=maillist;sa=emailfilters;saved');
956
			}
957
		}
958
959
		// Prepare some final context for the template
960
		$title = !empty($this->_req->query->saved) ? 'saved_filter' : ($context['editing'] === true ? 'edit_filter' : 'add_filter');
961
		$context['post_url'] = $scripturl . '?action=admin;area=maillist;sa=editfilter' . ($context['editing'] ? ';edit=' . $modSettings['id_filter'] : ';new') . ';save';
962
		$context['settings_title'] = $txt[$title];
963
		$context['linktree'][] = array(
964
			'url' => $scripturl . '?action=admin;area=maillist;sa=editfilter',
965
			'name' => ($context['editing']) ? $txt['edit_filter'] : $txt['add_filter'],
966
		);
967
		$context[$context['admin_menu_name']]['tab_data'] = array(
968
			'title' => $txt[$title],
969
			'description' => $txt['filters_title'],
970
		);
971
		$context[$context['admin_menu_name']]['current_subsection'] = 'emailfilters';
972
973
		// Load and show
974
		$settingsForm->prepare();
975
		theme()->getTemplates()->load('Admin');
976
		loadCSSFile('admin.css');
977
		$context['sub_template'] = 'show_settings';
978
	}
979
980
	/**
981
	 * Initialize Mailist settings form.
982
	 *
983
	 * @event integrate_modify_maillist_filter_settings Add new settings to the maillist filter area
984
	 */
985
	private function _filtersSettings()
986
	{
987
		global $txt;
988
989
		$config_vars = array(
990
			array('text', 'filter_name', 25, 'subtext' => $txt['filter_name_desc']),
991
			array('select', 'filter_type',
992
				array(
993
					'standard' => $txt['option_standard'],
994
					'regex' => $txt['option_regex'],
995
				),
996
			),
997
			array('large_text', 'filter_from', 4, 'subtext' => $txt['filter_from_desc']),
998
			array('text', 'filter_to', 25, 'subtext' => $txt['filter_to_desc']),
999
		);
1000
1001
		call_integration_hook('integrate_modify_maillist_filter_settings', array(&$config_vars));
1002
1003
		return $config_vars;
1004
	}
1005
1006
	/**
1007
	 * Deletes a filter from the system / database
1008
	 */
1009 View Code Duplication
	public function action_delete_filters()
1010
	{
1011
		// Removing the filter?
1012
		if (isset($this->_req->query->f_id))
1013
		{
1014
			checkSession('get');
1015
			$id = (int) $this->_req->query->f_id;
1016
1017
			maillist_delete_filter_parser($id);
1018
			redirectexit('action=admin;area=maillist;sa=emailfilters;deleted');
1019
		}
1020
	}
1021
1022
	/**
1023
	 * Show a list of all the parsers in the system
1024
	 *
1025
	 * What it does:
1026
	 *
1027
	 * - Allows to add/edit or delete parsers
1028
	 * - Parsers are used to split a message at a line of text
1029
	 * - Parsers can only be defined as regex, the system will check it for valid syntax
1030
	 * - Accessed by ?action=admin;area=maillist;sa=emailparser;
1031
	 *
1032
	 * @event integrate_list_email_parser
1033
	 */
1034
	public function action_list_parsers()
1035
	{
1036
		global $context, $scripturl, $txt, $modSettings;
1037
1038
		$id = 0;
1039
1040
		// Build the listoption array to display the data
1041
		$listOptions = array(
1042
			'id' => 'email_parser',
1043
			'title' => $txt['parsers'],
1044
			'items_per_page' => $modSettings['defaultMaxMessages'],
1045
			'no_items_label' => $txt['no_parsers'],
1046
			'base_href' => $scripturl . '?action=admin;area=maillist;sa=emailparser',
1047
			'get_items' => array(
1048
				'function' => array($this, 'load_filter_parser'),
1049
				'params' => array(
1050
					$id,
1051
					'parser'
1052
				),
1053
			),
1054
			'get_count' => array(
1055
				'function' => array($this, 'count_filter_parser'),
1056
				'params' => array(
1057
					$id,
1058
					'parser'
1059
				),
1060
			),
1061
			'columns' => array(
1062
				'name' => array(
1063
					'header' => array(
1064
						'value' => $txt['parser_name'],
1065
						'style' => 'white-space: nowrap;'
1066
					),
1067
					'data' => array(
1068
						'db' => 'filter_name',
1069
					),
1070
					'sort' => array(
1071
						'default' => 'filter_name',
1072
						'reverse' => 'filter_name DESC',
1073
					),
1074
				),
1075
				'from' => array(
1076
					'header' => array(
1077
						'value' => $txt['parser_from'],
1078
					),
1079
					'data' => array(
1080
						'db' => 'filter_from',
1081
					),
1082
					'sort' => array(
1083
						'default' => 'filter_from',
1084
						'reverse' => 'filter_from DESC',
1085
					),
1086
				),
1087
				'type' => array(
1088
					'header' => array(
1089
						'value' => $txt['parser_type'],
1090
					),
1091
					'data' => array(
1092
						'db' => 'filter_type',
1093
					),
1094
					'sort' => array(
1095
						'default' => 'filter_type',
1096
						'reverse' => 'filter_type DESC',
1097
					),
1098
				),
1099
				'action' => array(
1100
					'header' => array(
1101
						'value' => $txt['message_action'],
1102
						'class' => 'centertext',
1103
					),
1104
					'data' => array(
1105
						'sprintf' => array(
1106
							'format' => '<a href="?action=admin;area=maillist;sa=editparser;f_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '">
1107
										<i class="icon i-modify" title="' . $txt['modify'] . '"></i>
1108
									</a>
1109
									<a href="?action=admin;area=maillist;sa=deleteparser;f_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(' . JavaScriptEscape($txt['parser_delete_warning']) . ') && submitThisOnce(this);" accesskey="d">
1110
										<i class="icon i-delete" title="' . $txt['delete'] . '"></i>
1111
									</a>',
1112
							'params' => array(
1113
								'id_filter' => true,
1114
							),
1115
						),
1116
						'class' => 'centertext',
1117
						'style' => 'white-space:nowrap;',
1118
					),
1119
				),
1120
			),
1121
			'form' => array(
1122
				'href' => $scripturl . '?action=admin;area=maillist;sa=editparser',
1123
				'include_sort' => true,
1124
				'include_start' => true,
1125
				'hidden_fields' => array(
1126
					$context['session_var'] => $context['session_id'],
1127
				),
1128
			),
1129
			'additional_rows' => array(
1130
				array(
1131
					'position' => isset($this->_req->query->saved) ? 'top_of_list' : 'after_title',
1132
					'value' => isset($this->_req->query->saved) ? '<div class="successbox">' . $txt['saved'] . '</div>' : $txt['parsers_title'],
1133
				),
1134
				array(
1135
					'position' => 'below_table_data',
1136
					'class' => 'submitbutton',
1137
					'value' => '
1138
						<input type="submit" name="addparser" value="' . $txt['add_parser'] . '" />
1139
						<a class="linkbutton" href="' . $scripturl . '?action=admin;area=maillist;sa=sortparsers">' . $txt['sort_parser'] . '</a>',
1140
					),
1141
			),
1142
		);
1143
1144
		// Set the context values
1145
		$context['page_title'] = $txt['parsers'];
1146
		$context['sub_template'] = 'show_list';
1147
		$context['default_list'] = 'email_parser';
1148
1149
		// Create the list.
1150
		createList($listOptions);
1151
	}
1152
1153
	/**
1154
	 * Show a full list of all the parsers in the system for drag/drop sorting
1155
	 *
1156
	 * @event integrate_list_email_parser
1157
	 */
1158
	public function action_sort_parsers()
1159
	{
1160
		global $context, $scripturl, $txt;
1161
1162
		$id = 0;
1163
		$token = createToken('admin-sort');
1164
1165
		// Build the listoption array to display the data
1166
		$listOptions = array(
1167
			'id' => 'sort_email_fp',
1168
			'title' => $txt['sort_parser'],
1169
			'sortable' => true,
1170
			'items_per_page' => 0,
1171
			'no_items_label' => $txt['no_parsers'],
1172
			'base_href' => $scripturl . '?action=admin;area=maillist;sa=sortparsers',
1173
			'get_items' => array(
1174
				'function' => array($this, 'load_filter_parser'),
1175
				'params' => array(
1176
					$id,
1177
					'parser'
1178
				),
1179
			),
1180
			'get_count' => array(
1181
				'function' => array($this, 'count_filter_parser'),
1182
				'params' => array(
1183
					$id,
1184
					'parser'
1185
				),
1186
			),
1187
			'columns' => array(
1188
				'filterorder' => array(
1189
					'header' => array(
1190
						'value' => '',
1191
						'class' => 'hide',
1192
					),
1193
					'data' => array(
1194
						'db' => 'filter_order',
1195
						'class' => 'hide',
1196
					),
1197
				),
1198
				'name' => array(
1199
					'header' => array(
1200
						'value' => $txt['parser_name'],
1201
						'style' => 'white-space: nowrap;width: 10em'
1202
					),
1203
					'data' => array(
1204
						'db' => 'filter_name',
1205
					),
1206
				),
1207
				'from' => array(
1208
					'header' => array(
1209
						'value' => $txt['parser_from'],
1210
					),
1211
					'data' => array(
1212
						'db' => 'filter_from',
1213
					),
1214
				),
1215
				'type' => array(
1216
					'header' => array(
1217
						'value' => $txt['parser_type'],
1218
					),
1219
					'data' => array(
1220
						'db' => 'filter_type',
1221
					),
1222
				),
1223
			),
1224
			'form' => array(
1225
				'href' => $scripturl . '?action=admin;area=maillist;sa=sortparsers',
1226
				'hidden_fields' => array(
1227
					$context['session_var'] => $context['session_id'],
1228
				),
1229
			),
1230
			'additional_rows' => array(
1231
				array(
1232
					'position' => 'after_title',
1233
					'value' => $txt['parser_sort_description'],
1234
				),
1235
			),
1236
			'javascript' => '
1237
				$().elkSortable({
1238
					sa: "parserorder",
1239
					placeholder: "ui-state-highlight",
1240
					containment: "#sort_email_fp",
1241
					error: "' . $txt['admin_order_error'] . '",
1242
					title: "' . $txt['admin_order_title'] . '",
1243
					href: "?action=admin;;area=maillist;sa=sortparsers",
1244
					token: {token_var: "' . $token['admin-sort_token_var'] . '", token_id: "' . $token['admin-sort_token'] . '"}
1245
				});
1246
			',
1247
		);
1248
1249
		// Set the context values
1250
		$context['page_title'] = $txt['parsers'];
1251
		$context['sub_template'] = 'show_list';
1252
		$context['default_list'] = 'sort_email_fp';
1253
		$context[$context['admin_menu_name']]['current_subsection'] = 'emailparser';
1254
1255
		// Create the list.
1256
		createList($listOptions);
1257
	}
1258
1259
	/**
1260
	 * Adds or Edits an existing parser
1261
	 *
1262
	 * - All parsers are assumed regex
1263
	 *
1264
	 * @event integrate_save_parser_settings
1265
	 */
1266
	public function action_edit_parsers()
1267
	{
1268
		global $context, $scripturl, $txt, $modSettings;
1269
1270
		// Editing an existing filter?
1271
		if (isset($this->_req->query->f_id))
1272
		{
1273
			// Needs to be an int!
1274
			$id = (int) $this->_req->query->f_id;
1275
			if (empty($id) || $id < 0)
1276
				throw new Elk_Exception('error_no_id_filter');
1277
1278
			// Load this filter so we can edit it
1279
			$row = maillist_load_filter_parser($id, 'parser');
1280
1281
			$modSettings['id_filter'] = $row['id_filter'];
1282
			$modSettings['filter_type'] = $row['filter_type'];
1283
			$modSettings['filter_from'] = $row['filter_from'];
1284
			$modSettings['filter_name'] = $row['filter_name'];
1285
1286
			$context['page_title'] = $txt['edit_parser'];
1287
			$context['editing'] = true;
1288
		}
1289
		else
1290
		{
1291
			// Setup place holders for adding a new one instead
1292
			$modSettings['filter_type'] = '';
1293
			$modSettings['filter_name'] = '';
1294
			$modSettings['filter_from'] = '';
1295
1296
			// To the template we go
1297
			$context['page_title'] = $txt['add_parser'];
1298
			$context['editing'] = false;
1299
		}
1300
1301
		// Initialize the form
1302
		$settingsForm = new Settings_Form(Settings_Form::DB_ADAPTER);
1303
1304
		// Initialize it with our settings
1305
		$config_vars = $this->_parsersSettings();
1306
		$settingsForm->setConfigVars($config_vars);
1307
1308
		// Check if they are saving the changes
1309
		if (isset($this->_req->query->save))
1310
		{
1311
			checkSession();
1312
1313
			call_integration_hook('integrate_save_parser_settings');
1314
1315
			// Editing a parser?
1316
			$editId = isset($this->_req->query->edit) ? (int) $this->_req->query->edit : -1;
1317
			$editName = isset($this->_req->query->edit) ? 'id_filter' : '';
1318
1319
			// Test the regex
1320
			if ($this->_req->post->filter_type === 'regex' && !empty($this->_req->post->filter_from))
1321
			{
1322
				$valid = (preg_replace($this->_req->post->filter_from, '', 'ElkArte') === null) ? false : true;
1323
				if (!$valid)
1324
				{
1325
					// Regex did not compute .. Danger, Will Robinson
1326
					$context['settings_message'] = $txt['regex_invalid'];
1327
					$context['error_type'] = 'notice';
1328
1329
					$modSettings['filter_type'] = $this->_req->post->filter_type;
1330
					$modSettings['filter_from'] = $this->_req->post->filter_from;
1331
					$modSettings['filter_name'] = $this->_req->post->filter_name;
1332
				}
1333
			}
1334
1335 View Code Duplication
			if (empty($this->_req->post->filter_type) || empty($this->_req->post->filter_from))
1336
			{
1337
				$context['error_type'] = 'notice';
1338
				$context['settings_message'][] = $txt['filter_invalid'];
1339
			}
1340
1341
			// All clear to save?
1342 View Code Duplication
			if (empty($context['settings_message']))
1343
			{
1344
				// Shhh ... its really a parser
1345
				$config_vars[] = array('text', 'filter_style');
1346
				$this->_req->post->filter_style = 'parser';
1347
1348
				// Save, log, show
1349
				Email_Settings::saveTableSettings($config_vars, 'postby_emails_filters', $this->_req->post, array('id_filter'), $editId, $editName);
1350
				redirectexit('action=admin;area=maillist;sa=emailparser;saved');
1351
			}
1352
		}
1353
1354
		// Prepare the context for viewing
1355
		$title = ((isset($this->_req->query->saved) && $this->_req->query->saved == '1') ? 'saved_parser' : ($context['editing'] === true ? 'edit_parser' : 'add_parser'));
1356
		$context['settings_title'] = $txt[$title];
1357
		$context['post_url'] = $scripturl . '?action=admin;area=maillist;sa=editparser' . ($context['editing'] ? ';edit=' . $modSettings['id_filter'] : ';new') . ';save';
1358
		$context['linktree'][] = array(
1359
			'url' => $scripturl . '?action=admin;area=maillist;sa=editparser',
1360
			'name' => ($context['editing']) ? $txt['edit_parser'] : $txt['add_parser'],
1361
		);
1362
		$context[$context['admin_menu_name']]['tab_data'] = array(
1363
			'title' => $txt[$title],
1364
			'description' => $txt['parsers_title'],
1365
		);
1366
		$context[$context['admin_menu_name']]['current_subsection'] = 'emailparser';
1367
1368
		// prep it, load it, show it
1369
		$settingsForm->prepare();
1370
		theme()->getTemplates()->load('Admin');
1371
		loadCSSFile('admin.css');
1372
		$context['sub_template'] = 'show_settings';
1373
	}
1374
1375
	/**
1376
	 * Initialize Mailist settings form.
1377
	 *
1378
	 * @event integrate_modify_maillist_parser_settings Add settings to the maillist parser screen
1379
	 */
1380
	private function _parsersSettings()
1381
	{
1382
		global $txt;
1383
1384
		$config_vars = array(
1385
			array('text', 'filter_name', 25, 'subtext' => $txt['parser_name_desc']),
1386
			array('select', 'filter_type', 'subtext' => $txt['parser_type_desc'],
1387
				array(
1388
					'regex' => $txt['option_regex'],
1389
					'standard' => $txt['option_standard'],
1390
				),
1391
			),
1392
			array('large_text', 'filter_from', 4, 'subtext' => $txt['parser_from_desc']),
1393
		);
1394
1395
		call_integration_hook('integrate_modify_maillist_parser_settings', array(&$config_vars));
1396
1397
		return $config_vars;
1398
	}
1399
1400
	/**
1401
	 * Removes a parser from the system and database
1402
	 */
1403 View Code Duplication
	public function action_delete_parsers()
1404
	{
1405
		// Removing the filter?
1406
		if (isset($this->_req->query->f_id))
1407
		{
1408
			checkSession('get');
1409
			$id = (int) $this->_req->query->f_id;
1410
1411
			maillist_delete_filter_parser($id);
1412
			redirectexit('action=admin;area=maillist;sa=emailparser;deleted');
1413
		}
1414
	}
1415
1416
	/**
1417
	 * All the post by email settings, used to control how the feature works
1418
	 *
1419
	 * @event integrate_save_maillist_settings
1420
	 * @uses Admin language
1421
	 */
1422
	public function action_settings()
1423
	{
1424
		global $scripturl, $context, $txt, $modSettings;
1425
1426
		// Be nice, show them we did something
1427
		if (isset($this->_req->query->saved))
1428
			$context['settings_message'] = $txt['saved'];
1429
1430
		// Templates and language
1431
		theme()->getTemplates()->loadLanguageFile('Admin');
1432
		theme()->getTemplates()->load('Admin');
1433
		loadCSSFile('admin.css');
1434
1435
		// Load any existing email => board values used for new topic creation
1436
		$context['maillist_from_to_board'] = array();
1437
		$data = (!empty($modSettings['maillist_receiving_address'])) ? Util::unserialize($modSettings['maillist_receiving_address']) : array();
1438
		foreach ($data as $key => $addr)
1439
		{
1440
			$context['maillist_from_to_board'][$key] = array(
1441
				'id' => $key,
1442
				'emailfrom' => $addr[0],
1443
				'boardto' => $addr[1],
1444
			);
1445
		}
1446
1447
		// Initialize the maillist settings form
1448
		$settingsForm = new Settings_Form(Settings_Form::DB_ADAPTER);
1449
1450
		// Initialize it with our settings
1451
		$settingsForm->setConfigVars($this->_settings());
1452
1453
		// Saving settings?
1454
		if (isset($this->_req->query->save))
1455
		{
1456
			checkSession();
1457
1458
			call_integration_hook('integrate_save_maillist_settings');
1459
1460
			$email_error = false;
1461
			$board_error = false;
1462
			$maillist_receiving_address = array();
1463
1464
			// Basic checking of the email addresses
1465 View Code Duplication
			if (!Data_Validator::is_valid($this->_req->post, array('maillist_sitename_address' => 'valid_email'), array('maillist_sitename_address' => 'trim')))
1466
				$email_error = $this->_req->post->maillist_sitename_address;
1467 View Code Duplication
			if (!Data_Validator::is_valid($this->_req->post, array('maillist_sitename_help' => 'valid_email'), array('maillist_sitename_help' => 'trim')))
1468
				$email_error = $this->_req->post->maillist_sitename_help;
1469 View Code Duplication
			if (!Data_Validator::is_valid($this->_req->post, array('maillist_mail_from' => 'valid_email'), array('maillist_mail_from' => 'trim')))
1470
				$email_error = $this->_req->post->maillist_mail_from;
1471
1472
			// Inbound email set up then we need to check for both valid email and valid board
1473
			if (!$email_error && !empty($this->_req->post->emailfrom))
1474
			{
1475
				// Get the board ids for a quick check
1476
				$boards = maillist_board_list();
1477
1478
				// Check the receiving emails and the board id as well
1479
				$boardtocheck = !empty($this->_req->post->boardto) ? $this->_req->post->boardto : array();
1480
				$addresstocheck = !empty($this->_req->post->emailfrom) ? $this->_req->post->emailfrom : array();
1481
1482
				foreach ($addresstocheck as $key => $checkme)
1483
				{
1484
					// Valid email syntax
1485
					if (!Data_Validator::is_valid($addresstocheck, array($key => 'valid_email'), array($key => 'trim')))
1486
					{
1487
						$email_error = $checkme;
1488
						$context['error_type'] = 'notice';
1489
						continue;
1490
					}
1491
1492
					// Valid board id?
1493
					if (!isset($boardtocheck[$key]) || !isset($boards[$key]))
1494
					{
1495
						$board_error = $checkme;
1496
						$context['error_type'] = 'notice';
1497
						continue;
1498
					}
1499
1500
					// Decipher as [0] emailaddress and [1] board id
1501
					$maillist_receiving_address[] = array($checkme, $boardtocheck[$key]);
1502
				}
1503
			}
1504
1505
			// Enable or disable the fake cron
1506
			enable_maillist_imap_cron(!empty($this->_req->post->maillist_imap_cron));
1507
1508
			// Check and set any errors or give the go ahead to save
1509
			if ($email_error)
1510
				$context['settings_message'] = sprintf($txt['email_not_valid'], $email_error);
1511
			elseif ($board_error)
1512
				$context['settings_message'] = sprintf($txt['board_not_valid'], $board_error);
1513
			else
1514
			{
1515
				// Clear the moderation count cache
1516
				Cache::instance()->remove('num_menu_errors');
1517
1518
				// Should be off if mail posting is on, we ignore it anyway but this at least updates the ACP
1519
				if (!empty($this->_req->post->maillist_enabled))
1520
					updateSettings(array('disallow_sendBody' => ''));
1521
1522
				updateSettings(array('maillist_receiving_address' => serialize($maillist_receiving_address)));
1523 6
				$settingsForm->setConfigValues((array) $this->_req->post);
1524
				$settingsForm->save();
1525 6
				writeLog();
1526
				redirectexit('action=admin;area=maillist;sa=emailsettings;saved');
1527
			}
1528
		}
1529 6
1530 4
		// Javascript vars for the "add more" buttons in the receive_email callback
1531 4
		$board_list = maillist_board_list();
1532 4
		$script = '';
1533 6
		$i = 0;
1534 6
1535 6
		// Create the board selection list
1536 4
		foreach ($board_list as $board_id => $board_name)
1537 4
			$script .= $i++ . ': {id:' . $board_id . ', name:' . JavaScriptEscape($board_name) . '},';
0 ignored issues
show
Coding Style introduced by
Increment and decrement operators must be bracketed when used in string concatenation
Loading history...
1538 4
1539 4
		theme()->addInlineJavascript('
1540 6
		var sEmailParent = \'add_more_email_placeholder\',
1541 6
			oEmailOptionsdt = {size: \'50\', name: \'emailfrom[]\', class: \'input_text\'},
1542 6
			oEmailOptionsdd = {size: \'1\', type: \'select\', name: \'boardto[]\', class: \'input_select\'},
1543 6
			oEmailSelectData = {' . $script . '};
1544 6
1545 4
			document.getElementById(\'add_more_board_div\').style.display = \'block\';', true
1546 4
		);
1547 4
1548 6
		$context['boards'] = $board_list;
1549 4
		$context['settings_title'] = $txt['ml_emailsettings'];
1550 4
		$context['page_title'] = $txt['ml_emailsettings'];
1551 4
		$context['post_url'] = $scripturl . '?action=admin;area=maillist;sa=emailsettings;save';
1552 6
		$context['sub_template'] = 'show_settings';
1553 6
		$settingsForm->prepare();
1554 6
	}
1555 6
1556 6
	/**
1557 4
	 * Load up the config var array for settings display etc.
1558
	 *
1559
	 * @event integrate_modify_maillist_settings
1560 6
	 */
1561 4
	private function _settings()
1562
	{
1563
		global $txt;
1564
1565
		// Define the menu
1566
		$config_vars = array(
1567 6
				array('desc', 'maillist_help'),
1568
				array('check', 'maillist_enabled'),
1569 6
				array('check', 'pbe_post_enabled'),
1570 4
				array('check', 'pbe_pm_enabled'),
1571 6
				array('check', 'pbe_no_mod_notices', 'subtext' => $txt['pbe_no_mod_notices_desc'], 'postinput' => $txt['recommended']),
1572 6
				array('check', 'pbe_bounce_detect', 'subtext' => $txt['pbe_bounce_detect_desc'], 'postinput' => $txt['experimental']),
1573 6
				array('check', 'pbe_bounce_record', 'subtext' => $txt['pbe_bounce_record_desc'], 'postinput' => $txt['experimental']),
1574 6
			array('title', 'maillist_outbound'),
1575 6
				array('desc', 'maillist_outbound_desc'),
1576
				array('check', 'maillist_group_mode'),
1577 6
				array('check', 'maillist_digest_enabled'),
1578 6
				array('text', 'maillist_sitename', 40, 'subtext' => $txt['maillist_sitename_desc'], 'postinput' => $txt['maillist_sitename_post']),
1579 6
				array('text', 'maillist_sitename_address', 40, 'subtext' => $txt['maillist_sitename_address_desc'], 'postinput' => $txt['maillist_sitename_address_post']),
1580 6
				array('text', 'maillist_mail_from', 40, 'subtext' => $txt['maillist_mail_from_desc'], 'postinput' => $txt['maillist_mail_from_post']),
1581 6
				array('text', 'maillist_sitename_help', 40, 'subtext' => $txt['maillist_sitename_help_desc'], 'postinput' => $txt['maillist_sitename_help_post']),
1582 6
				array('text', 'maillist_sitename_regards', 40, 'subtext' => $txt['maillist_sitename_regards_desc']),
1583 6
			array('title', 'maillist_inbound'),
1584 4
				array('desc', 'maillist_inbound_desc'),
1585 6
				array('check', 'maillist_newtopic_change'),
1586 6
				array('check', 'maillist_newtopic_needsapproval', 'subtext' => $txt['maillist_newtopic_needsapproval_desc'], 'postinput' => $txt['recommended']),
1587
				array('callback', 'maillist_receive_email_list'),
1588 4
			array('title', 'misc'),
1589
				array('check', 'maillist_allow_attachments'),
1590 6
				array('int', 'maillist_key_active', 2, 'subtext' => $txt['maillist_key_active_desc']),
1591
			'',
1592 6
				array('text', 'maillist_leftover_remove', 40, 'subtext' => $txt['maillist_leftover_remove_desc']),
1593
				array('text', 'maillist_sig_keys', 40, 'subtext' => $txt['maillist_sig_keys_desc']),
1594
				array('int', 'maillist_short_line', 2, 'subtext' => $txt['maillist_short_line_desc']),
1595
		);
1596
1597
		// Imap?
1598 6
		if (!function_exists('imap_open'))
1599
			$config_vars = array_merge($config_vars,
1600 6
				array(
1601
					array('title', 'maillist_imap_missing'),
1602
				)
1603
			);
1604
		else
1605
			$config_vars = array_merge($config_vars,
1606
				array(
1607
					array('title', 'maillist_imap'),
1608
					array('desc', 'maillist_imap_reason'),
1609
						array('text', 'maillist_imap_host', 45, 'subtext' => $txt['maillist_imap_host_desc'], 'disabled' => !function_exists('imap_open')),
1610
						array('text', 'maillist_imap_mailbox', 20, 'postinput' => $txt['maillist_imap_mailbox_desc'], 'disabled' => !function_exists('imap_open')),
1611
						array('text', 'maillist_imap_uid', 20, 'postinput' => $txt['maillist_imap_uid_desc'], 'disabled' => !function_exists('imap_open')),
1612
						array('password', 'maillist_imap_pass', 20, 'postinput' => $txt['maillist_imap_pass_desc'], 'disabled' => !function_exists('imap_open')),
1613
						array('select', 'maillist_imap_connection',
1614
							array(
1615
								'imap' => $txt['maillist_imap_unsecure'],
1616
								'pop3' => $txt['maillist_pop3_unsecure'],
1617
								'imaptls' => $txt['maillist_imap_tls'],
1618
								'imapssl' => $txt['maillist_imap_ssl'],
1619
								'pop3tls' => $txt['maillist_pop3_tls'],
1620
								'pop3ssl' => $txt['maillist_pop3_ssl']
1621
							), 'postinput' => $txt['maillist_imap_connection_desc'], 'disabled' => !function_exists('imap_open'),
1622
						),
1623
						array('check', 'maillist_imap_delete', 20, 'subtext' => $txt['maillist_imap_delete_desc'], 'disabled' => !function_exists('imap_open')),
1624
						array('check', 'maillist_imap_cron', 20, 'subtext' => $txt['maillist_imap_cron_desc'], 'disabled' => !function_exists('imap_open')),
1625
				)
1626
			);
1627
1628
		call_integration_hook('integrate_modify_maillist_settings', array(&$config_vars));
1629
1630
		return $config_vars;
1631
	}
1632
1633
	/**
1634
	 * Return the form settings for use in admin search
1635
	 */
1636
	public function settings_search()
1637
	{
1638
		return $this->_settings();
1639
	}
1640
1641
	/**
1642
	 * View all the custom email bounce templates.
1643
	 *
1644
	 * What it does:
1645
	 *
1646
	 * - Shows all the bounce templates in the system available to this user
1647
	 * - Provides for actions to add or delete them
1648
	 * - Accessed by ?action=admin;area=maillist;sa=emailtemplates;
1649
	 *
1650
	 * @event integrate_list_bounce_template_list
1651
	 */
1652
	public function action_view_bounce_templates()
1653
	{
1654
		global $modSettings, $context, $txt, $scripturl;
1655
1656
		// We'll need this, because bounce templates are stored with warning templates.
1657
		require_once(SUBSDIR . '/Moderation.subs.php');
1658
1659
		// Submitting a new one or editing an existing one then pass this request off
1660
		if (isset($this->_req->post->add) || isset($this->_req->post->save) || isset($this->_req->query->tid))
1661
			return $this->action_modify_bounce_templates();
1662
		// Deleting and existing one
1663 View Code Duplication
		elseif (isset($this->_req->post->delete) && !empty($this->_req->post->deltpl))
1664
		{
1665
			checkSession('post');
1666
			validateToken('mod-mlt');
1667
			removeWarningTemplate($this->_req->post->deltpl, 'bnctpl');
1668
		}
1669
1670
		// This is all the information required for showing the email templates.
1671
		$listOptions = array(
1672
			'id' => 'bounce_template_list',
1673
			'title' => $txt['ml_bounce_templates_title'],
1674
			'items_per_page' => $modSettings['defaultMaxMessages'],
1675
			'no_items_label' => $txt['ml_bounce_templates_none'],
1676
			'base_href' => $scripturl . '?action=admin;area=maillist;sa=emailtemplates;' . $context['session_var'] . '=' . $context['session_id'],
1677
			'default_sort_col' => 'title',
1678
			'get_items' => array(
1679
				'function' => array($this, 'list_getBounceTemplates'),
1680
			),
1681
			'get_count' => array(
1682
				'function' => array($this, 'list_getBounceTemplateCount'),
1683
				'params' => array('bnctpl'),
1684
			),
1685
			'columns' => array(
1686
				'title' => array(
1687
					'header' => array(
1688
						'value' => $txt['ml_bounce_templates_name'],
1689
					),
1690
					'data' => array(
1691
						'sprintf' => array(
1692
							'format' => '<a href="' . $scripturl . '?action=admin;area=maillist;sa=emailtemplates;tid=%1$d">%2$s</a>',
1693
							'params' => array(
1694
								'id_comment' => false,
1695
								'title' => false,
1696
								'body' => false,
1697
							),
1698
						),
1699
					),
1700
					'sort' => array(
1701
						'default' => 'template_title',
1702
						'reverse' => 'template_title DESC',
1703
					),
1704
				),
1705
				'creator' => array(
1706
					'header' => array(
1707
						'value' => $txt['ml_bounce_templates_creator'],
1708
					),
1709
					'data' => array(
1710
						'db' => 'creator',
1711
					),
1712
					'sort' => array(
1713
						'default' => 'creator_name',
1714
						'reverse' => 'creator_name DESC',
1715
					),
1716
				),
1717
				'time' => array(
1718
					'header' => array(
1719
						'value' => $txt['ml_bounce_templates_time'],
1720
					),
1721
					'data' => array(
1722
						'db' => 'time',
1723
					),
1724
					'sort' => array(
1725
						'default' => 'lc.log_time DESC',
1726
						'reverse' => 'lc.log_time',
1727
					),
1728
				),
1729
				'delete' => array(
1730
					'header' => array(
1731
						'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
1732
						'style' => 'width: 4%;',
1733
						'class' => 'centertext',
1734
					),
1735
					'data' => array(
1736
						'function' => function ($rowData) {
1737
							return '<input type="checkbox" name="deltpl[]" value="' . $rowData['id_comment'] . '" class="input_check" />';
1738
						},
1739
						'class' => 'centertext',
1740
					),
1741
				),
1742
			),
1743
			'form' => array(
1744
				'href' => $scripturl . '?action=admin;area=maillist;sa=emailtemplates',
1745
				'token' => 'mod-mlt',
1746
			),
1747
			'additional_rows' => array(
1748
				array(
1749
					'class' => 'submitbutton',
1750
					'position' => 'below_table_data',
1751
					'value' => '
1752
					<input type="submit" name="delete" value="' . $txt['ml_bounce_template_delete'] . '" onclick="return confirm(\'' . $txt['ml_bounce_template_delete_confirm'] . '\');" class="right_submit" />
1753
					<input type="submit" name="add" value="' . $txt['ml_bounce_template_add'] . '" class="right_submit" />',
1754
				),
1755
			),
1756
		);
1757
1758
		// Create the template list.
1759
		$context['page_title'] = $txt['ml_bounce_templates_title'];
1760
		createToken('mod-mlt');
1761
1762
		createList($listOptions);
1763
1764
		// Show the list
1765
		$context['sub_template'] = 'show_list';
1766
		$context['default_list'] = 'bounce_template_list';
1767
	}
1768
1769
	/**
1770
	 * Edit a 'it bounced' template.
1771
	 *
1772
	 * @uses bounce_template sub template
1773
	 */
1774
	public function action_modify_bounce_templates()
1775
	{
1776
		global $context, $txt, $user_info;
1777
1778
		require_once(SUBSDIR . '/Moderation.subs.php');
1779
1780
		$context['id_template'] = isset($this->_req->query->tid) ? (int) $this->_req->query->tid : 0;
1781
		$context['is_edit'] = (bool) $context['id_template'];
1782
1783
		// Standard template things, you know the drill
1784
		$context['page_title'] = $context['is_edit'] ? $txt['ml_bounce_template_modify'] : $txt['ml_bounce_template_add'];
1785
		$context['sub_template'] = 'bounce_template';
1786
		$context[$context['admin_menu_name']]['current_subsection'] = 'templates';
1787
1788
		// Defaults to show
1789
		$context['template_data'] = array(
1790
			'title' => '',
1791
			'body' => $txt['ml_bounce_template_body_default'],
1792
			'subject' => $txt['ml_bounce_template_subject_default'],
1793
			'personal' => false,
1794
			'can_edit_personal' => true,
1795
		);
1796
1797
		// If it's an edit load it.
1798
		if ($context['is_edit'])
1799
			modLoadTemplate($context['id_template'], 'bnctpl');
1800
1801
		// Wait, we are saving?
1802 View Code Duplication
		if (isset($this->_req->post->save))
1803
		{
1804
			checkSession('post');
1805
			validateToken('mod-mlt');
1806
1807
			// To check the BBC is good...
1808
			require_once(SUBSDIR . '/Post.subs.php');
1809
1810
			// Bit of cleaning!
1811
			$template_body = trim($this->_req->post->template_body);
1812
			$template_title = trim($this->_req->post->template_title);
1813
1814
			// Need something in both boxes.
1815
			if (!empty($template_body) && !empty($template_title))
1816
			{
1817
				// Safety first.
1818
				$template_title = Util::htmlspecialchars($template_title);
1819
1820
				// Clean up BBC.
1821
				preparsecode($template_body);
1822
1823
				// But put line breaks back!
1824
				$template_body = strtr($template_body, array('<br />' => "\n"));
1825
1826
				// Is this personal?
1827
				$recipient_id = !empty($this->_req->post->make_personal) ? $user_info['id'] : 0;
1828
1829
				// Updating or adding ?
1830
				if ($context['is_edit'])
1831
				{
1832
					// Simple update...
1833
					modAddUpdateTemplate($recipient_id, $template_title, $template_body, $context['id_template'], true, 'bnctpl');
1834
1835
					// If it wasn't visible and now is they've effectively added it.
1836
					if ($context['template_data']['personal'] && !$recipient_id)
1837
						logAction('add_bounce_template', array('template' => $template_title));
1838
					// Conversely if they made it personal it's a delete.
1839
					elseif (!$context['template_data']['personal'] && $recipient_id)
1840
						logAction('delete_bounce_template', array('template' => $template_title));
1841
					// Otherwise just an edit.
1842
					else
1843
						logAction('modify_bounce_template', array('template' => $template_title));
1844
				}
1845
				else
1846
				{
1847
					modAddUpdateTemplate($recipient_id, $template_title, $template_body, $context['id_template'], false, 'bnctpl');
1848
					logAction('add_bounce_template', array('template' => $template_title));
1849
				}
1850
1851
				// Get out of town...
1852
				redirectexit('action=admin;area=maillist;sa=emailtemplates');
1853
			}
1854
			else
1855
			{
1856
				$context['warning_errors'] = array();
1857
				$context['template_data']['title'] = !empty($template_title) ? $template_title : '';
1858
				$context['template_data']['body'] = !empty($template_body) ? $template_body : $txt['ml_bounce_template_body_default'];
1859
				$context['template_data']['personal'] = !empty($this->_req->post->make_personal);
1860
1861
				if (empty($template_title))
1862
					$context['warning_errors'][] = $txt['ml_bounce_template_error_no_title'];
1863
1864
				if (empty($template_body))
1865
					$context['warning_errors'][] = $txt['ml_bounce_template_error_no_body'];
1866
			}
1867
		}
1868
1869
		createToken('mod-mlt');
1870
	}
1871
1872
	/**
1873
	 * Get all the bounce templates from the system
1874
	 *
1875
	 * - Callback for createList()
1876
	 *
1877
	 * @param int $start The item to start with (for pagination purposes)
1878
	 * @param int $items_per_page  The number of items to show per page
1879
	 * @param string $sort A string indicating how to sort the results
1880
	 */
1881
	public function list_getBounceTemplates($start, $items_per_page, $sort)
1882
	{
1883
		return warningTemplates($start, $items_per_page, $sort, 'bnctpl');
1884
	}
1885
1886
	/**
1887
	 * Get the number of bounce templates in the system
1888
	 *
1889
	 * - Callback for createList() to warningTemplateCount
1890
	 */
1891
	public function list_getBounceTemplateCount()
1892
	{
1893
		return warningTemplateCount('bnctpl');
1894
	}
1895
1896
	/**
1897
	 * Get the number of unapproved emails
1898
	 *
1899
	 * - Callback for createList() to list_maillist_unapproved
1900
	 *
1901
	 * @param int $start The item to start with (for pagination purposes)
1902
	 * @param int $items_per_page The number of items to show per page
1903
	 * @param string $sort A string indicating how to sort the results
1904
	 * @param int $id = 0
1905
	 */
1906
	public function list_maillist_unapproved($start, $items_per_page, $sort = '', $id = 0)
1907
	{
1908
		return list_maillist_unapproved($id, $start, $items_per_page, $sort);
1909
	}
1910
}
1911