Completed
Pull Request — master (#3325)
by Emanuele
11:19
created

BadBehavior_Controller   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 205
rs 9.6
c 0
b 0
f 0
wmc 35
ccs 0
cts 116
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A action_index() 0 4 1
A _prepareFilters() 0 23 5
C action_log() 0 64 14
A _prepareMembers() 0 22 6
A _action_delete() 0 19 5
A _setFilter() 0 26 4
1
<?php
2
3
/**
4
 * The main purpose of this file is to show a list of all badbehavior entries
5
 * and allow filtering and deleting them.
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
 * Class to show a list of all badbehavior log entries
17
 *
18
 * @package BadBehavior
19
 */
20
class BadBehavior_Controller extends Action_Controller
21
{
22
	/**
23
	 * Call the appropriate action method.
24
	 */
25
	public function action_index()
26
	{
27
		// all we know how to do is...
28
		$this->action_log();
29
	}
30
31
	/**
32
	 * View the forum's badbehavior log.
33
	 *
34
	 * What it does:
35
	 *
36
	 * - This function sets all the context up to show the badbehavior log for review.
37
	 * - It requires the maintain_forum permission.
38
	 * - It is accessed from ?action=admin;area=logs;sa=badbehaviorlog.
39
	 *
40
	 * @uses the BadBehavior template and badbehavior_log sub template.
41
	 */
42
	public function action_log()
43
	{
44
		global $scripturl, $txt, $context, $modSettings;
45
46
		// Check for the administrative permission to do this.
47
		isAllowedTo('admin_forum');
48
49
		// Templates, etc...
50
		loadLanguage('BadBehaviorlog');
51
		loadTemplate('BadBehavior');
52
53
		// Functions we will need
54
		require_once(SUBSDIR . '/BadBehavior.subs.php');
55
56
		// Set up the filtering...
57
		$filter = array();
58
		if (isset($this->_req->query->value, $this->_req->query->filter))
59
			$filter = $this->_setFilter();
60
61
		if ($filter === false)
62
		{
63
			// Bad filter or something else going on, back to the start you go
64
			redirectexit('action=admin;area=logs;sa=badbehaviorlog' . (isset($this->_req->query->desc) ? ';desc' : ''));
65
		}
66
67
		// Deleting or just doing a little weeding?
68
		if (isset($this->_req->post->delall) || isset($this->_req->post->delete))
69
			$this->_action_delete($filter);
70
71
		// Just how many entries are there?
72
		$num_errors = getBadBehaviorLogEntryCount($filter);
73
74
		// If this filter turns up empty, just return
75
		if (empty($num_errors) && !empty($filter))
76
			redirectexit('action=admin;area=logs;sa=badbehaviorlog' . (isset($this->_req->query->desc) ? ';desc' : ''));
77
78
		// Clean up start.
79
		$start = $this->_req->getQuery('start', 'intval', 0);
80
		$start = $start < 0 ? 0 : $start;
81
82
		// Do we want to reverse the listing?
83
		$sort = isset($this->_req->query->desc) ? 'down' : 'up';
84
85
		// Set the page listing up.
86
		$context['page_index'] = constructPageIndex($scripturl . '?action=admin;area=logs;sa=badbehaviorlog' . ($sort == 'down' ? ';desc' : '') . (!empty($filter) ? $filter['href'] : ''), $start, $num_errors, $modSettings['defaultMaxMessages']);
87
88
		// Find and sort out the log entries.
89
		$context['bb_entries'] = getBadBehaviorLogEntries($start, $modSettings['defaultMaxMessages'], $sort, $filter);
90
91
		// Load member data if needed
92
		$this->_prepareMembers();
93
94
		// Filtering?
95
		if (!empty($filter))
96
			$this->_prepareFilters($filter);
97
98
		// And the standard template goodies
99
		$context['page_title'] = $txt['badbehaviorlog_log'];
100
		$context['has_filter'] = !empty($filter);
101
		$context['sub_template'] = 'badbehavior_log';
102
		$context['sort_direction'] = $sort;
103
		$context['start'] = $start;
104
105
		createToken('admin-bbl');
106
	}
107
108
	/**
109
	 * Loads basic member data for any members that are in the log
110
	 */
111
	protected function _prepareMembers()
112
	{
113
		global $context, $txt, $scripturl;
114
115
		$members = array();
116
		foreach ($context['bb_entries'] as $member)
117
			$members[] = $member['member']['id'];
118
119
		// Load any member data so we have more information available
120
		if (!empty($members))
121
		{
122
			require_once(SUBSDIR . '/Members.subs.php');
123
			$members = getBasicMemberData($members, array('add_guest' => true));
124
125
			// Go through each entry and add the member data.
126
			foreach ($context['bb_entries'] as $id => $dummy)
127
			{
128
				$memID = $context['bb_entries'][$id]['member']['id'];
129
				$context['bb_entries'][$id]['member']['username'] = $members[$memID]['member_name'];
130
				$context['bb_entries'][$id]['member']['name'] = $members[$memID]['real_name'];
131
				$context['bb_entries'][$id]['member']['href'] = empty($memID) ? '' : $scripturl . '?action=profile;u=' . $memID;
132
				$context['bb_entries'][$id]['member']['link'] = empty($memID) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $memID . '">' . $context['bb_entries'][$id]['member']['name'] . '</a>';
133
			}
134
		}
135
	}
136
137
	/**
138
	 * Prepares the filter index of the $context variable
139
	 *
140
	 * @param string[] $filter - an array describing the current filter
141
	 */
142
	protected function _prepareFilters($filter)
143
	{
144
		global $context, $scripturl, $user_profile;
145
146
		$context['filter'] = $filter;
147
148
		// Set the filtering context.
149
		switch ($filter['variable'])
150
		{
151
			case 'id_member':
152
				$id = $filter['value']['sql'];
153
				loadMemberData($id, false, 'minimal');
154
				$context['filter']['value']['html'] = '<a href="' . $scripturl . '?action=profile;u=' . $id . '">' . $user_profile[$id]['real_name'] . '</a>';
155
				break;
156
			case 'url':
157
				$context['filter']['value']['html'] = '\'' . strtr(htmlspecialchars((substr($filter['value']['sql'], 0, 1) === '?' ? $scripturl : '') . $filter['value']['sql'], ENT_COMPAT, 'UTF-8'), array('\_' => '_')) . '\'';
158
				break;
159
			case 'headers':
160
				$context['filter']['value']['html'] = '\'' . strtr(htmlspecialchars($filter['value']['sql'], ENT_COMPAT, 'UTF-8'), array("\n" => '<br />', '&lt;br /&gt;' => '<br />', "\t" => '&nbsp;&nbsp;&nbsp;', '\_' => '_', '\\%' => '%', '\\\\' => '\\')) . '\'';
161
				$context['filter']['value']['html'] = preg_replace('~&amp;lt;span class=&amp;quot;remove&amp;quot;&amp;gt;(.+?)&amp;lt;/span&amp;gt;~', '$1', $context['filter']['value']['html']);
162
				break;
163
			default:
164
				$context['filter']['value']['html'] = $filter['value']['sql'];
165
		}
166
	}
167
168
	/**
169
	 * Populates the $filter array with data from $_GET
170
	 */
171
	protected function _setFilter()
172
	{
173
		global $txt;
174
175
		$db = database();
176
177
		// You can filter by any of the following columns:
178
		$filters = array(
179
			'id_member' => $txt['badbehaviorlog_username'],
180
			'ip' => $txt['badbehaviorlog_ip'],
181
			'session' => $txt['badbehaviorlog_session'],
182
			'valid' => $txt['badbehaviorlog_key'],
183
			'request_uri' => $txt['badbehaviorlog_request'],
184
			'user_agent' => $txt['badbehaviorlog_agent'],
185
		);
186
187
		if (!isset($filters[$this->_req->query->filter]))
188
			return false;
189
190
		return array(
191
			'variable' => $this->_req->query->filter == 'useragent' ? 'user_agent' : $this->_req->query->filter,
192
			'value' => array(
193
				'sql' => in_array($this->_req->query->filter, array('request_uri', 'user_agent')) ? base64_decode(strtr($this->_req->query->value, array(' ' => '+'))) : $db->escape_wildcard_string($this->_req->query->value),
194
			),
195
			'href' => ';filter=' . $this->_req->query->filter . ';value=' . $this->_req->query->value,
196
			'entity' => $filters[$this->_req->query->filter]
197
		);
198
	}
199
200
	/**
201
	 * Performs the removal of one or multiple log entries
202
	 *
203
	 * @param array $filter - an array describing the current filter
204
	 * @throws Elk_Exception
205
	 */
206
	protected function _action_delete($filter)
207
	{
208
		$type = isset($this->_req->post->delall) ? 'delall' : 'delete';
209
210
		// Make sure the session exists and the token is correct
211
		checkSession();
212
		validateToken('admin-bbl');
213
214
		$redirect = deleteBadBehavior($type, $filter);
215
		$redirect_path = 'action=admin;area=logs;sa=badbehaviorlog' . (isset($this->_req->query->desc) ? ';desc' : '');
216
217
		if ($redirect === 'delete')
218
		{
219
			$start = $this->_req->getQuery('start', 'intval', 0);
220
221
			// Go back to where we were.
222
			redirectexit($redirect_path . ';start=' . $start . (!empty($filter) ? $filter['href'] : ''));
223
		}
224
		redirectexit($redirect_path);
225
	}
226
}
227