log::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.9
cc 1
nc 1
nop 11
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * phpBB Gallery - Core Extension
4
 *
5
 * @package   phpbbgallery/core
6
 * @author    satanasov
7
 * @author    Leinad4Mind
8
 * @copyright 2014- satanasov, 2018- Leinad4Mind
9
 * @license   GPL-2.0-only
10
 */
11
12
namespace phpbbgallery\core;
13
14
class log
15
{
16
	/** @var \phpbb\db\driver\driver_interface  */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
	protected $db;
18
19
	/** @var \phpbb\user  */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
	protected $user;
21
22
	/** @var \phpbb\language\language  */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
	protected $language;
24
25
	/** @var \phpbb\user_loader  */
0 ignored issues
show
Bug introduced by
The type phpbb\user_loader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
	protected $user_loader;
27
28
	/** @var \phpbb\template\template  */
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
	protected $template;
30
31
	/** @var \phpbb\controller\helper  */
0 ignored issues
show
Bug introduced by
The type phpbb\controller\helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
	protected $helper;
33
34
	/** @var \phpbb\pagination  */
0 ignored issues
show
Bug introduced by
The type phpbb\pagination was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
	protected $pagination;
36
37
	/** @var \phpbbgallery\core\auth\auth  */
38
	protected $gallery_auth;
39
40
	/** @var \phpbbgallery\core\config  */
41
	protected $gallery_config;
42
43
	/** @var   */
44
	protected $log_table;
45
46
	/** @var   */
47
	protected $images_table;
48
49
	/**
50
	 * log constructor.
51
	 *
52
	 * @param \phpbb\db\driver\driver_interface $db
53
	 * @param \phpbb\user                       $user
54
	 * @param \phpbb\user_loader                $user_loader
55
	 * @param \phpbb\template\template          $template
56
	 * @param \phpbb\controller\helper          $helper
57
	 * @param \phpbb\pagination                 $pagination
58
	 * @param auth\auth                         $gallery_auth
59
	 * @param config                            $gallery_config
60
	 * @param                                   $log_table
61
	 * @param                                   $images_table
62
	 */
63 96
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, \phpbb\language\language $language,
64
		\phpbb\user_loader $user_loader, \phpbb\template\template $template, \phpbb\controller\helper $helper, \phpbb\pagination $pagination,
65
		\phpbbgallery\core\auth\auth $gallery_auth, \phpbbgallery\core\config $gallery_config,
66
		$log_table, $images_table)
67
	{
68 96
		$this->db = $db;
69 96
		$this->user = $user;
70 96
		$this->language = $language;
71 96
		$this->user_loader = $user_loader;
72 96
		$this->template = $template;
73 96
		$this->helper = $helper;
74 96
		$this->pagination = $pagination;
75 96
		$this->gallery_auth = $gallery_auth;
76 96
		$this->gallery_config = $gallery_config;
77 96
		$this->log_table = $log_table;
78 96
		$this->images_table = $images_table;
79 96
	}
80
81
	/**
82
	 * Add item to log
83
	 *
84
	 * @param   string			$log_type    type of action (user/mod/admin/system) max 16 chars
85
	 * @param   string			$log_action  action we are logging (add/remove/approve/unapprove/delete) max 32 chars
86
	 * @param 	int				$album
87
	 * @param   int				$image       Image we are logging for (can be 0)
88
	 * @param	array|string 	$description Description string
89
	 */
90
	public function add_log($log_type, $log_action, $album = 0, $image = 0, $description = array())
91
	{
92
		$user = (int) $this->user->data['user_id'];
93
		$time = (int) time();
94
95
		$sql_array = array(
96
			'log_time'		=> (int) $time,
97
			'log_type'		=> $this->db->sql_escape($log_type),
98
			'log_action'	=> $this->db->sql_escape($log_action),
99
			'log_user'		=> (int) $user,
100
			'log_ip'		=> $this->db->sql_escape($this->user->ip),
101
			'album'			=> (int) $album,
102
			'image'			=> (int) $image,
103
			'description'	=> $this->db->sql_escape(json_encode($description))
104
		);
105
		$sql = 'INSERT INTO ' . $this->log_table . ' ' . $this->db->sql_build_array('INSERT', $sql_array);
106
		$this->db->sql_query($sql);
107
	}
108
109
	/**
110
	* Delete logs
111
	* @param	array	$mark	Logs selected for deletion
112
	**/
113
	public function delete_logs($mark)
114
	{
115
		$sql = 'DELETE FROM ' . $this->log_table . ' WHERE ' . $this->db->sql_in_set('log_id', $mark);
116
		$this->db->sql_query($sql);
117
		$this->add_log('admin', 'log', 0, 0, array('LOG_CLEAR_GALLERY'));
118
	}
119
120
	/**
121
	 * Build log list
122
	 *
123
	 * @param   	string		$type  Type of queue to build user/mod/admin/system
124
	 * @param		int			$limit How many items to show
125
	 * @param		int			$page
126
	 * @param		int			$album
127
	 * @param		int			$image
128
	 * @param		array		$additional
129
	 * @internal	param int	$start start count used to build paging
130
	 */
131 2
	public function build_list($type, $limit = 0, $page = 1, $album = 0, $image = 0, $additional = [])
132
	{
133 2
		if ($limit == 0)
134
		{
135
			$limit = $this->gallery_config->get('items_per_page');
136
			// If its called from ACP album is -1, if from MCP then is not
137
			if ($album == -1)
138
			{
139
				$page = (int) ($page / $limit) + 1;
140
			}
141
		}
142 2
		$this->language->add_lang(['info_acp_gallery_logs'], 'phpbbgallery/core');
143
144 2
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
145
		$sql_array = array(
146
			'FROM'	=> array(
147 2
				$this->log_table	=> 'l'
148
			),
149
			'LEFT_JOIN' => array(
150
				array(
151 2
					'FROM'	=> array($this->images_table => 'i'),
152 2
					'ON'	=> 'l.image = i.image_id'
153
				)
154
			)
155
		);
156 2
		$sql_where = [];
157 2
		if ($type != 'all')
158
		{
159 2
			$sql_where[] = "l.log_type = '" . $this->db->sql_escape($type) . "'";
160
		}
161
		// If album is -1 we are calling it from ACP so ... priority!
162
		// If album is 0 we are calling it from moderator log, so we need album we can access
163 2
		$mod_array = $this->gallery_auth->acl_album_ids('m_status');
164
		// Patch for missing album
165 2
		$mod_array[] = 0;
166 2
		if ($album === 0)
167
		{
168
			// If no albums we can approve - quit building queue
169 2
			if (empty($mod_array))
170
			{
171
				return;
172
			}
173 2
			$sql_where[] = $this->db->sql_in_set('l.album', $mod_array);
174 2
			$sql_where[] = $this->db->sql_in_set('i.image_album_id', $mod_array);
175
		}
176 2
		if ($album > 0)
177
		{
178
			if (!$this->gallery_auth->acl_check('i_view', $album))
179
			{
180
				return;
181
			}
182
			$sql_where[] = 'l.album = ' . (int) $album;
183
		}
184 2
		if ($image > 0)
185
		{
186
			$sql_where[] = 'l.image = ' . (int) $image;
187
			$sql_where[] = $this->db->sql_in_set('i.image_album_id', $mod_array);
188
		}
189 2
		if (isset($additional['sort_days']))
190
		{
191
			$sql_where[] = 'l.log_time > ' . (time() - ($additional['sort_days'] * 86400));
192
		}
193
		// And additional check for "active" logs (DB admin can review logs in DB)
194 2
		$sql_where[] = 'l.deleted = 0';
195 2
		$sql_array['WHERE'] = implode(' and ', $sql_where);
196 2
		if (isset($additional['sort_key']))
197
		{
198
			switch ($additional['sort_key'])
199
			{
200
				case 'u':
201
					$sql_array['ORDER_BY'] = 'l.log_user ' . (isset($additional['sort_dir']) ? 'ASC' : 'DESC');
202
					$sql_array['GROUP_BY'] = 'l.log_user, l.log_id, i.image_id, i.image_album_id';
203
				break;
204
				case 'i':
205
					$sql_array['ORDER_BY'] = 'l.log_ip ' . (isset($additional['sort_dir']) ? 'ASC' : 'DESC');
206
					$sql_array['GROUP_BY'] = 'l.log_ip, l.log_id, i.image_id, i.image_album_id';
207
				break;
208
				case 'o':
209
					$sql_array['ORDER_BY'] = 'l.description ' . (isset($additional['sort_dir']) ? 'ASC' : 'DESC');
210
					$sql_array['GROUP_BY'] = 'l.description, l.log_id, i.image_id, i.image_album_id';
211
				break;
212
			}
213
		}
214
		else
215
		{
216 2
			$sql_array['ORDER_BY'] = 'l.log_time ' . (isset($additional['sort_dir']) ? 'ASC' : 'DESC');
217 2
			$sql_array['GROUP_BY'] = 'l.log_time, l.log_id, i.image_id, i.image_album_id';
218
		}
219
		// So we need count - so define SELECT
220 2
		$count_sql_array = $sql_array;
221
222
		// Remove SELECT for correct count
223 2
		$count_sql_array['SELECT'] = 'COUNT(DISTINCT l.log_id) as count';
224 2
		unset($count_sql_array['GROUP_BY']);
225 2
		unset($count_sql_array['ORDER_BY']);
226 2
		$filtering_on_image_album = false;
227 2
		foreach ($sql_where as $where_clause)
228
		{
229 2
			if (strpos($where_clause, 'i.image_album_id') !== false)
230
			{
231 2
				$filtering_on_image_album = true;
232 2
				break;
233
			}
234
		}
235 2
		if (!$filtering_on_image_album)
236
		{
237
			unset($count_sql_array['LEFT_JOIN']);
238
		}
239
240 2
		$sql = $this->db->sql_build_query('SELECT', $count_sql_array);
241 2
		$result = $this->db->sql_query($sql);
242 2
		$row = $this->db->sql_fetchrow($result);
243 2
		$this->db->sql_freeresult($result);
244
245 2
		$count = $row ? $row['count'] : 0;
246
247 2
		$sql_array['SELECT'] = '*';
248 2
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
249 2
		$result = $this->db->sql_query_limit($sql, $limit, ($page - 1) * $limit);
250
251 2
		$logoutput = $users_array = array();
252 2
		while ($row = $this->db->sql_fetchrow($result))
253
		{
254
			$logoutput[] = array(
255
				'id'	=> $row['log_id'],
256
				'type'	=> $row['log_type'],
257
				'action'	=> $row['log_action'],
258
				'time'	=> $row['log_time'],
259
				'user'	=> $row['log_user'],
260
				'ip'	=> $row['log_ip'],
261
				'album'	=> $row['album'],
262
				'image'	=> $row['image'],
263
				'description'	=> json_decode(stripslashes($row['description']))
264
			);
265
			$users_array[$row['log_user']] = array('');
266
		}
267 2
		$this->db->sql_freeresult($result);
268
269 2
		$this->user_loader->load_users(array_keys($users_array));
270
271
		// Let's build template vars
272 2
		if (!empty($logoutput))
273
		{
274
			foreach ($logoutput as $var)
275
			{
276
				$this->template->assign_block_vars('log', array(
277
					'U_LOG_ID'		=> $var['id'],
278
					'U_LOG_USER'	=> $this->user_loader->get_username($var['user'], 'full'),
279
					'U_TYPE'		=> $var['type'],
280
					'U_LOG_IP'		=> $var['ip'],
281
					'U_ALBUM_LINK'	=> $var['album'] != 0 ? $this->helper->route('phpbbgallery_core_album', array('album_id'	=> $var['album'])) : false,
282
					'U_IMAGE_LINK'	=> $var['image'] != 0 ? $this->helper->route('phpbbgallery_core_image', array('image_id'	=> $var['image'])) : false,
283
					'U_LOG_ACTION' => isset($var['description']) && is_array($var['description']) ? $this->language->lang($var['description'][0], $var['description'][1] ?? false, $var['description'][2] ?? false, $var['description'][3] ?? false) : '',
284
					'U_TIME'		=> $this->user->format_date($var['time']),
285
				));
286
			}
287
		}
288 2
		$this->template->assign_vars(array(
289 2
			'S_HAS_LOGS' => $count > 0 ? true : false,
290 2
			'TOTAL_PAGES'	=> $this->language->lang('PAGE_TITLE_NUMBER', $page),
291
		));
292
		// Here we do some routes magic
293 2
		if ($album == 0)
294
		{
295 2
			$this->pagination->generate_template_pagination(array(
296
				'routes' => array(
297 2
					'phpbbgallery_core_moderate_action_log',
298
					'phpbbgallery_core_moderate_action_log_page',
299
				),
300
				'params' => array(
301
				),
302 2
			), 'pagination', 'page', $count, $limit, ($page-1) * $limit);
303
		}
304
		else if ($album == -1)
305
		{
306
			$url_array = array(
307
				'i' => '-phpbbgallery-core-acp-gallery_logs_module',
308
				'mode' => 'main',
309
				'lf' => $type
310
			);
311
			if (isset($additional['sort_days']))
312
			{
313
				$url_array['st'] = $additional['sort_days'];
314
			}
315
			if (isset($additional['sort_key']))
316
			{
317
				$url_array['sk'] = $additional['sort_key'];
318
			}
319
			if (isset($additional['sort_dir']))
320
			{
321
				$url_array['sd'] = $additional['sort_dir'];
322
			}
323
			$url = http_build_query($url_array,'','&');
324
325
			$this->pagination->generate_template_pagination(append_sid('index.php?' . $url), 'pagination', 'page', $count, $limit, ($page-1) * $limit);
0 ignored issues
show
Bug introduced by
The function append_sid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

325
			$this->pagination->generate_template_pagination(/** @scrutinizer ignore-call */ append_sid('index.php?' . $url), 'pagination', 'page', $count, $limit, ($page-1) * $limit);
Loading history...
326
		}
327
		else
328
		{
329
			$this->pagination->generate_template_pagination(array(
330
				'routes' => array(
331
					'phpbbgallery_core_moderate_action_log_album',
332
					'phpbbgallery_core_moderate_action_log_album_page',
333
				),
334
				'params' => array(
335
					'album_id'	=> $album,
336
				),
337
			), 'pagination', 'page', $count, $limit, ($page-1) * $limit);
338
		}
339 2
	}
340
}
341