Passed
Push — master ( eb00b7...bfc209 )
by Stanislav
03:42 queued 13s
created

log   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Test Coverage

Coverage 45.58%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 38
eloc 166
c 4
b 2
f 0
dl 0
loc 324
ccs 67
cts 147
cp 0.4558
rs 9.36

4 Methods

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

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