Passed
Push — development ( 4b352c...7fab02 )
by Spuds
01:07 queued 20s
created

AdminLog::action_index()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 63
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 35
nc 4
nop 0
dl 0
loc 63
ccs 0
cts 24
cp 0
crap 30
rs 9.0488
c 1
b 0
f 0

How to fix   Long Method   

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 manages the admin logs and forwards to display, pruning, etc.
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
 * @version 2.0 Beta 1
11
 *
12
 */
13
14
namespace ElkArte\AdminController;
15
16
use ElkArte\AbstractController;
17
use ElkArte\Action;
18
use ElkArte\SettingsForm\SettingsForm;
19
use ElkArte\Languages\Txt;
20
21
/**
22
 * Admin logs controller.
23
 *
24
 * What it does:
25
 *
26
 * - This class manages logs and forwards to display, pruning, and other actions on logs.
27
 *
28
 */
29
class AdminLog extends AbstractController
30
{
31
	/**
32
	 * This method decides which log to load.
33
	 * Accessed by ?action=admin;area=logs
34
	 *
35
	 * @event integrate_sa_manage_logs used to add additional log viewing functions, passed subActions array
36
	 * @uses _initPruningSettingsForm
37
	 */
38
	public function action_index()
39
	{
40
		global $context, $txt, $modSettings;
41
42
		// These are the logs they can load.
43
		$subActions = [
44
			'errorlog' => ['controller' => ManageErrors::class], 'function' => 'action_index', 'disabled' => empty($modSettings['enableErrorLogging']),
45
			'adminlog' => ['controller' => Modlog::class, 'function' => 'action_log'],
46
			'modlog' => ['controller' => Modlog::class, 'function' => 'action_log', 'disabled' => !featureEnabled('ml') || empty($modSettings['modlog_enabled'])],
47
			'banlog' => ['controller' => ManageBans::class, 'function' => 'action_log'],
48
			'spiderlog' => ['controller' => ManageSearchEngines::class, 'function' => 'action_logs', 'disabled' => empty($modSettings['spider_logging_enabled'])],
49
			'tasklog' => ['controller' => ManageScheduledTasks::class, 'function' => 'action_log'],
50
			'pruning' => [$this, 'action_pruningSettings_display'],
51
		];
52
53
		// Set up the custom tabs.
54
		$context[$context['admin_menu_name']]['object']->prepareTabData([
55
				'title' => 'logs',
56
				'description' => 'maintain_info',
57
				'tabs' => [
58
					'errorlog' => [
59
						'url' => getUrl('admin', ['action' => 'admin', 'area' => 'logs', 'sa' => 'errorlog', 'desc']),
60
						'description' => sprintf($txt['errlog_desc'], $txt['remove']),
61
						'disabled' => empty($modSettings['enableErrorLogging']),
62
					],
63
					'adminlog' => [
64
						'description' => $txt['admin_log_desc'],
65
					],
66
					'modlog' => [
67
						'description' => $txt['moderation_log_desc'],
68
						'disabled' => !featureEnabled('ml') || empty($modSettings['modlog_enabled']),
69
					],
70
					'banlog' => [
71
						'description' => $txt['ban_log_description'],
72
					],
73
					'spiderlog' => [
74
						'description' => $txt['spider_log_desc'],
75
					],
76
					'tasklog' => [
77
						'description' => $txt['scheduled_log_desc'],
78
					],
79
					'pruning' => [
80
						'description' => $txt['pruning_log_desc'],
81
					],
82
				]]
83
		);
84
85
		// If there is no sa set, it must have come here for the first time,
86
		// redirect to the error log with reverse order by default instead of
87
		// mutating the request object.
88
		if (!$this->_req->hasQuery('sa') && !$this->_req->hasQuery('desc'))
89
		{
90
			redirectexit('action=admin;area=logs;sa=errorlog;desc');
91
		}
92
93
		// Set up the action control
94
		$action = new Action('manage_logs');
95
96
		// By default, do the basic settings, call integrate_sa_manage_logs
97
		$subAction = $action->initialize($subActions, 'errorlog');
98
99
		// Call the right function for this sub-action.
100
		$action->dispatch($subAction);
101
	}
102
103
	/**
104
	 * Allow editing the settings on the pruning screen.
105
	 *
106
	 * @event integrate_prune_settings add additional settings to the auto pruning display.  If you add any
107
	 * additional logs make sure to add them at the end.  Additionally, make sure you add them to the
108
	 * weekly scheduled task.
109
	 * @uses _pruningSettings form.
110
	 */
111
	public function action_pruningSettings_display(): void
112
	{
113
		global $txt, $context, $modSettings;
114
115
		// Make sure we understand what's going on.
116
		Txt::load('ManageSettings');
117
118
		$context['page_title'] = $txt['log_settings'];
119
120
		$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
121
122
		// Initialize settings
123
		$config_vars = $this->_settings();
124
		$settingsForm->setConfigVars($config_vars);
125
126
		call_integration_hook('integrate_prune_settings');
127
128
		// Saving?
129
		if ($this->_req->hasQuery('save'))
130
		{
131
			checkSession();
132
133
			$savevar = [
134
				['check', 'enableErrorLogging'],
135
				['check', 'enableErrorQueryLogging'],
136
				['check', 'modlog_enabled'],
137
				['check', 'userlog_enabled'],
138
				['text', 'pruningOptions']
139
			];
140
141
			// If pruning is enabled, compile all pruneXYZlog options into a CSV string; yes,
142
			// this is really this badly thought out.
143
			if (!empty($this->_req->post->pruningOptions))
144
			{
145
				$vals = [];
146
				foreach ($config_vars as $index => $config_value)
147
				{
148
					if (!is_array($config_value) || $index === 'pruningOptions' || !str_starts_with($config_value[1], 'prune'))
149
					{
150
						continue;
151
					}
152
153
					$vals[] = empty($this->_req->post->{$config_value[1]}) || $this->_req->post->{$config_value[1]} < 0 ? 0 : $this->_req->getPost($config_value[1], 'intval');
154
				}
155
156
				$_POST['pruningOptions'] = implode(',', $vals);
157
			}
158
			else
159
			{
160
				$_POST['pruningOptions'] = '';
161
			}
162
163
			$settingsForm->setConfigVars($savevar);
164
			$settingsForm->setConfigValues($_POST);
165
			$settingsForm->save();
166
			redirectexit('action=admin;area=logs;sa=pruning');
167
		}
168
169
		$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'logs', 'sa' => 'pruning', 'save']);
170
		$context['settings_title'] = $txt['log_settings'];
171
		$context['sub_template'] = 'show_settings';
172
173
		// Get the actual values
174
		if (!empty($modSettings['pruningOptions']))
175
		{
176
			[$modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']] = array_pad(explode(',', $modSettings['pruningOptions']), 7, 0);
177
		}
178
		else
179
		{
180
			$modSettings['pruneErrorLog'] = $modSettings['pruneModLog'] = $modSettings['pruneBanLog'] = $modSettings['pruneReportLog'] = $modSettings['pruneScheduledTaskLog'] = $modSettings['pruneSpiderHitLog'] = 0;
181
		}
182
183
		$settingsForm->prepare();
184
	}
185
186
	/**
187
	 * Returns the configuration settings for pruning logs.
188
	 */
189
	private function _settings(): array
190
	{
191
		global $txt;
192
193
		return [
194
			// See all the mistakes the developers make
195
			['check', 'enableErrorLogging'],
196
			['check', 'enableErrorQueryLogging'],
197
			// Moderation logging is a Core feature; it enables Admin, Moderation and Profile Edit logging.  This
198
			// allows some fine-tuning of that feature, e.g., only allow admin logging
199
			featureEnabled('ml') ? ['check', 'modlog_enabled'] : '',
200
			featureEnabled('ml') ? ['check', 'userlog_enabled'] : '',
201
			// Even do the pruning?
202
			['title', 'pruning_title', 'force_div_id' => 'pruning_title'],
203 2
			// The array indexes are here, so we can remove/change them before saving.
204
			'pruningOptions' => ['check', 'pruningOptions'],
205 2
			'',
206
			// Various logs that could be pruned.
207
			['int', 'pruneErrorLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Error log.
208
			['int', 'pruneModLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Moderation log.
209
			['int', 'pruneBanLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Ban hit log.
210 2
			['int', 'pruneReportLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Report to the moderator log.
211 2
			['int', 'pruneScheduledTaskLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Log of the scheduled tasks and how long they ran.
212
			['int', 'pruneSpiderHitLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Log of the scheduled tasks and how long they ran.
213 2
			// If you add any additional logs, make sure to add them after this point.  Additionally, make sure you
214 2
			// add them to the weekly scheduled task.
215 2
		];
216 2
	}
217 2
218 2
	/**
219 2
	 * Return the search engine settings for use in admin search
220
	 */
221
	public function settings_search(): array
222
	{
223
		return $this->_settings();
224
	}
225
}
226