ModerateAttachments::action_index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
/**
4
 * All the moderation actions for attachments, basically approve them
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 dev
14
 *
15
 */
16
17
namespace ElkArte\Controller;
18
19
use ElkArte\AbstractController;
20
use ElkArte\Cache\Cache;
21
use ElkArte\Exceptions\Exception;
22
23
/**
24
 * Class ModerateAttachments
25
 *
26
 * This class is responsible for moderating attachments.
27
 */
28
class ModerateAttachments extends AbstractController
29
{
30
	/**
31
	 * Forward to attachments approval method is the only responsibility
32
	 * of this controller.
33
	 *
34
	 * @see AbstractController::action_index
35
	 */
36
	public function action_index()
37
	{
38
		// Forward to our method(s) to do the job
39
		$this->action_attachapprove();
40
	}
41
42
	/**
43
	 * Approve an attachment
44
	 *
45
	 * - Called from a mouse click,
46
	 * - works out what we want to do with attachments and actions it.
47
	 * - Accessed by ?action=attachapprove
48
	 */
49
	public function action_attachapprove()
50
	{
51
		// Security is our primary concern...
52
		checkSession('get');
53
54
		// Is it approve or delete?
55
		$is_approve = !isset($this->_req->query->sa) || $this->_req->query->sa !== 'reject';
56
57
		$attachments = [];
58
		require_once(SUBSDIR . '/ManageAttachments.subs.php');
59
60
		// If we are approving all ID's in a message, get the ID's.
61
		if ($this->_req->query->sa === 'all' && !empty($this->_req->query->mid))
62
		{
63
			$id_msg = (int) $this->_req->query->mid;
64
			$attachments = attachmentsOfMessage($id_msg);
65
		}
66
		elseif (!empty($this->_req->query->aid))
67
		{
68
			$attachments[] = (int) $this->_req->query->aid;
69
		}
70
71
		if (empty($attachments))
72
		{
73
			throw new Exception('no_access', false);
74
		}
75
76
		// @todo nb: this requires permission to approve posts, not manage attachments
77
		// Now we have some ID's cleaned and ready to approve, but first - let's check we have permission!
78
		$allowed_boards = empty($this->user->mod_cache['ap']) ? boardsAllowedTo('approve_posts') : $this->user->mod_cache['ap'];
79
80
		if ($allowed_boards == [0])
81
		{
82
			$approve_query = '';
83
		}
84
		elseif (!empty($allowed_boards))
85
		{
86
			$approve_query = ' AND m.id_board IN (' . implode(',', $allowed_boards) . ')';
87
		}
88
		else
89
		{
90
			$approve_query = ' AND 0';
91
		}
92
93
		// Validate the attachments exist and have the right approval state.
94
		$attachments = validateAttachments($attachments, $approve_query);
95
96
		// Set up a return link based off one of the attachments for this message
97
		$attach_home = attachmentBelongsTo($attachments[0]);
98
		$redirect = 'topic=' . $attach_home['id_topic'] . '.msg' . $attach_home['id_msg'] . '#msg' . $attach_home['id_msg'];
99
100
		if (empty($attachments))
101
		{
102
			throw new Exception('no_access', false);
103
		}
104
105
		// Finally, we are there. Follow through!
106
		if ($is_approve)
107
		{
108
			// Checked and deemed worthy.
109
			approveAttachments($attachments);
110
		}
111
		else
112
		{
113
			removeAttachments(['id_attach' => $attachments, 'do_logging' => true]);
114
		}
115
116
		// We approved or removed, either way we reset those numbers
117
		Cache::instance()->remove('num_menu_errors');
118
119
		// Return to the topic....
120
		redirectexit($redirect);
121
	}
122
}
123