Issues (902)

acpcleanup/cleanup.php (11 issues)

1
<?php
2
/**
3
*
4
* @package phpBB Gallery
5
* @version $Id$
6
* @copyright (c) 2007 nickvergessen [email protected] http://www.flying-bits.org
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
namespace phpbbgallery\acpcleanup;
12
13
class cleanup
14
{
15
	/** @var \phpbb\db\driver\driver_interface  */
0 ignored issues
show
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 \phpbbgallery\core\file\file  */
19
	protected $tool;
20
21
	/** @var \phpbb\user  */
0 ignored issues
show
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...
22
	protected $user;
23
24
	/** @var \phpbb\language\language  */
0 ignored issues
show
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...
25
	protected $language;
26
27
	/** @var \phpbbgallery\core\block  */
28
	protected $block;
29
30
	/** @var \phpbbgallery\core\album\album  */
31
	protected $album;
32
33
	/** @var \phpbbgallery\core\comment  */
34
	protected $comment;
35
36
	/** @var \phpbbgallery\core\config  */
37
	protected $gallery_config;
38
39
	/** @var \phpbbgallery\core\log  */
40
	protected $log;
41
42
	/** @var \phpbbgallery\core\moderate  */
43
	protected $moderate;
44
45
	/** @var   */
46
	protected $albums_table;
47
48
	/** @var   */
49
	protected $images_table;
50
51
52
	/**
53
	 * cleanup constructor.
54
	 *
55
	 * @param \phpbb\db\driver\driver_interface $db
56
	 * @param \phpbbgallery\core\file\file      $tool
57
	 * @param \phpbb\user                       $user
58
	 * @param \phpbb\language\language          $language
59
	 * @param \phpbbgallery\core\block          $block
60
	 * @param \phpbbgallery\core\album\album    $album
61
	 * @param \phpbbgallery\core\comment        $comment
62
	 * @param \phpbbgallery\core\config         $gallery_config
63
	 * @param \phpbbgallery\core\log            $log
64
	 * @param \phpbbgallery\core\moderate       $moderate
65
	 * @param                                   $albums_table
66
	 * @param                                   $images_table
67
	 */
68
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbbgallery\core\file\file $tool, \phpbb\user $user, \phpbb\language\language $language,
69
		\phpbbgallery\core\block $block, \phpbbgallery\core\album\album $album, \phpbbgallery\core\comment $comment,
70
		\phpbbgallery\core\config $gallery_config, \phpbbgallery\core\log $log, \phpbbgallery\core\moderate $moderate,
71
		$albums_table, $images_table)
72
	{
73
		$this->db = $db;
74
		$this->tool = $tool;
75
		$this->user = $user;
76
		$this->language = $language;
77
		$this->block = $block;
78
		$this->album = $album;
79
		$this->comment = $comment;
80
		$this->gallery_config = $gallery_config;
81
		$this->log = $log;
82
		$this->moderate = $moderate;
83
		$this->albums_table = $albums_table;
84
		$this->images_table = $images_table;
85
	}
86
87
	/**
88
	* Delete source files without a database entry.
89
	*
90
	* @param	array	$filenames		An array of filenames
91
	* @return	string	Language key for the success message.
92
	*/
93
	public function delete_files($filenames)
94
	{
95
		foreach ($filenames as $file)
96
		{
97
			$this->tool->delete(utf8_decode($file));
98
			$this->tool->delete_cache(utf8_decode($file));
99
		}
100
		$this->log->add_log('admin', 'clean_deletefiles', 0, 0, array('LOG_CLEANUP_DELETE_FILES', count($filenames)));
101
		return 'CLEAN_ENTRIES_DONE';
102
	}
103
104
	/**
105
	* Delete images, where the source file is missing.
106
	*
107
	* @param	mixed	$image_ids		Either an array of integers or an integer.
108
	* @return	string	Language key for the success message.
109
	*/
110
	public function delete_images($image_ids)
111
	{
112
		$this->log->add_log('admin', 'clean_deleteentries', 0, 0, array('LOG_CLEANUP_DELETE_ENTRIES', count($image_ids)));
113
		$this->moderate->delete_images($image_ids, false);
114
115
		return 'CLEAN_SOURCES_DONE';
116
	}
117
118
	/**
119
	* Delete images, where the author is missing.
120
	*
121
	* @param	mixed	$image_ids		Either an array of integers or an integer.
122
	* @return	string	Language key for the success message.
123
	*/
124
	public function delete_author_images($image_ids)
125
	{
126
		$this->log->add_log('admin', 'clean_deletenoauthors', 0, 0, array('LOG_CLEANUP_DELETE_NO_AUTHOR', count($image_ids)));
127
		$this->moderate->delete_images($image_ids);
128
129
		return 'CLEAN_AUTHORS_DONE';
130
	}
131
132
	/**
133
	* Delete comments, where the author is missing.
134
	*
135
	* @param	mixed	$comment_ids	Either an array of integers or an integer.
136
	* @return	string	Language key for the success message.
137
	*/
138
	public function delete_author_comments($comment_ids)
139
	{
140
		$this->log->add_log('admin', 'clean_deletecna', 0, 0, array('LOG_CLEANUP_COMMENT_DELETE_NO_AUTHOR', count($comment_ids)));
141
		$this->comment->delete_comments($comment_ids);
142
143
		return 'CLEAN_COMMENTS_DONE';
144
	}
145
146
	/**
147
	* Delete unwanted and obsolete personal galleries.
148
	*
149
	* @param	array	$unwanted_pegas		User IDs we want to delete the pegas.
150
	* @param	array	$obsolete_pegas		User IDs we want to delete the pegas.
151
	* @return	array	Language keys for the success messages.
152
	*/
153
	public function delete_pegas($unwanted_pegas, $obsolete_pegas)
154
	{
155
156
		$delete_pegas = array_merge($unwanted_pegas, $obsolete_pegas);
157
158
		$delete_images = $delete_albums = $user_image_count = array();
159
		$num_pegas = 0;
160
161
		$sql = 'SELECT album_id, parent_id
162
			FROM ' . $this->albums_table . '
163
			WHERE ' . $this->db->sql_in_set('album_user_id', $delete_pegas);
164
		$result = $this->db->sql_query($sql);
165
		while ($row = $this->db->sql_fetchrow($result))
166
		{
167
			$delete_albums[] = (int) $row['album_id'];
168
			if ($row['parent_id'] == 0)
169
			{
170
				$num_pegas++;
171
			}
172
		}
173
		$this->db->sql_freeresult($result);
174
175
		$sql = 'SELECT image_id, image_filename, image_status, image_user_id
176
			FROM ' . $this->images_table . '
177
			WHERE ' . $this->db->sql_in_set('image_album_id', $delete_albums, false, true);
178
		$result = $this->db->sql_query($sql);
179
180
		$filenames = array();
181
		while ($row = $this->db->sql_fetchrow($result))
182
		{
183
			$delete_images[] = (int) $row['image_id'];
184
			$filenames[(int) $row['image_id']] = $row['image_filename'];
185
186
			if (($row['image_status'] == $this->block->get_image_status_unapproved()) ||
187
			($row['image_status'] == $this->block->get_image_status_orphan()))
188
			{
189
				continue;
190
			}
191
192
			if (isset($user_image_count[(int) $row['image_user_id']]))
193
			{
194
				$user_image_count[(int) $row['image_user_id']]++;
195
			}
196
			else
197
			{
198
				$user_image_count[(int) $row['image_user_id']] = 1;
199
			}
200
		}
201
		$this->db->sql_freeresult($result);
202
203
		if (!empty($delete_images))
204
		{
205
			$this->moderate->delete_images($delete_images, $filenames);
206
		}
207
208
		$sql = 'DELETE FROM ' . $this->albums_table . '
209
			WHERE ' . $this->db->sql_in_set('album_id', $delete_albums);
210
		$this->db->sql_query($sql);
211
		$this->gallery_config->dec('num_pegas', $num_pegas);
212
213
		if (in_array($this->gallery_config->get('newest_pega_album_id'), $delete_albums))
214
		{
215
			// Update the config for the statistic on the index
216
			if ($this->gallery_config->get('num_pegas') > 0)
217
			{
218
				$sql_array = array(
219
					'SELECT'		=> 'a.album_id, u.user_id, u.username, u.user_colour',
220
					'FROM'			=> array($this->albums_table => 'a'),
221
222
					'LEFT_JOIN'		=> array(
223
						array(
224
							'FROM'		=> array(USERS_TABLE => 'u'),
0 ignored issues
show
The constant phpbbgallery\acpcleanup\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
225
							'ON'		=> 'u.user_id = a.album_user_id',
226
						),
227
					),
228
229
					'WHERE'			=> 'a.album_user_id <> ' . (int) $this->album->get_public() . ' AND a.parent_id = 0',
0 ignored issues
show
The method get_public() does not exist on phpbbgallery\core\album\album. Did you maybe mean get_public_albums()? ( Ignorable by Annotation )

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

229
					'WHERE'			=> 'a.album_user_id <> ' . (int) $this->album->/** @scrutinizer ignore-call */ get_public() . ' AND a.parent_id = 0',

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
230
					'ORDER_BY'		=> 'a.album_id DESC',
231
				);
232
				$sql = $this->db->sql_build_query('SELECT', $sql_array);
233
234
				$result = $this->db->sql_query_limit($sql, 1);
235
				$newest_pega = $this->db->sql_fetchrow($result);
236
				$this->db->sql_freeresult($result);
237
			}
238
239
			if (($this->gallery_config->get('num_pegas') > 0) && isset($newest_pega))
240
			{
241
				$this->gallery_config->set('newest_pega_user_id', $newest_pega['user_id']);
242
				$this->gallery_config->set('newest_pega_username', $newest_pega['username']);
243
				$this->gallery_config->set('newest_pega_user_colour', $newest_pega['user_colour']);
244
				$this->gallery_config->set('newest_pega_album_id', $newest_pega['album_id']);
245
			}
246
			else
247
			{
248
				$this->gallery_config->set('newest_pega_user_id', 0);
249
				$this->gallery_config->set('newest_pega_username', '');
250
				$this->gallery_config->set('newest_pega_user_colour', '');
251
				$this->gallery_config->set('newest_pega_album_id', 0);
252
253
				if (isset($newest_pega))
254
				{
255
					$this->gallery_config->set('num_pegas', 0);
256
				}
257
			}
258
		}
259
/*
260
		foreach ($user_image_count as $user_id => $images)
261
		{
262
			//phpbb_gallery_hookup::add_image($user_id, (0 - $images));
263
264
			$uploader = new \phpbbgallery\core\user($this->db, $user_id, false);
265
			$uploader->update_images((0 - $images));
266
		}
267
		\phpbbgallery\core\user::update_users($delete_pegas, array('personal_album_id' => 0));
268
*/
269
		$return = array();
270
		if ($obsolete_pegas)
0 ignored issues
show
Bug Best Practice introduced by
The expression $obsolete_pegas of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
271
		{
272
			$return[] = 'CLEAN_PERSONALS_DONE';
273
		}
274
		if ($unwanted_pegas)
0 ignored issues
show
Bug Best Practice introduced by
The expression $unwanted_pegas of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
275
		{
276
			$return[] = 'CLEAN_PERSONALS_BAD_DONE';
277
		}
278
279
		return $return;
280
	}
281
282
	/**
283
	*
284
	*/
285
	public function prune($pattern)
286
	{
287
		$sql_where = '';
288
		if (isset($pattern['image_album_id']))
289
		{
290
			$pattern['image_album_id'] = array_map('intval', explode(',', $pattern['image_album_id']));
291
		}
292
		if (isset($pattern['image_user_id']))
293
		{
294
			$pattern['image_user_id'] = array_map('intval', explode(',', $pattern['image_user_id']));
295
		}
296
		foreach ($pattern as $field => $value)
297
		{
298
			if (is_array($value))
299
			{
300
				$sql_where .= (($sql_where) ? ' AND ' : ' WHERE ') . $this->db->sql_in_set($field, $value);
301
				continue;
302
			}
303
			$sql_where .= (($sql_where) ? ' AND ' : ' WHERE ') . $field . ' < ' . $value;
304
		}
305
306
		$sql = 'SELECT image_id, image_filename
307
			FROM ' . $this->images_table . '
308
			' . $sql_where;
309
310
		$result = $this->db->sql_query($sql);
311
		$image_ids = $filenames = $update_albums = array();
0 ignored issues
show
The assignment to $update_albums is dead and can be removed.
Loading history...
312
		while ($row = $this->db->sql_fetchrow($result))
313
		{
314
			$image_ids[] = (int) $row['image_id'];
315
			$filenames[(int) $row['image_id']] = $row['image_filename'];
316
		}
317
		$this->db->sql_freeresult($result);
318
319
		if ($image_ids)
320
		{
321
			$this->moderate->delete_images($image_ids, $filenames);
322
		}
323
324
		return 'CLEAN_PRUNE_DONE';
325
	}
326
327
	/**
328
	*
329
	*/
330
	public function lang_prune_pattern($pattern)
331
	{
332
		if (isset($pattern['image_album_id']))
333
		{
334
			$pattern['image_album_id'] = array_map('intval', explode(',', $pattern['image_album_id']));
335
		}
336
		if (isset($pattern['image_user_id']))
337
		{
338
			$pattern['image_user_id'] = array_map('intval', explode(',', $pattern['image_user_id']));
339
		}
340
341
		$lang_pattern = '';
342
		foreach ($pattern as $field => $value)
343
		{
344
			$field = (strpos($field, 'image_') === 0) ? substr($field, 6) : $field;
345
346
			switch ($field)
347
			{
348
				case 'album_id':
349
					$sql = 'SELECT album_name
350
						FROM ' . $this->albums_table . '
351
						WHERE ' . $this->db->sql_in_set('album_id', $value) . '
352
						ORDER BY album_id ASC';
353
					$result = $this->db->sql_query($sql);
354
					$value = '';
355
					while ($row = $this->db->sql_fetchrow($result))
356
					{
357
						$value .= (($value) ? ', ' : '') . $row['album_name'];
358
					}
359
					$this->db->sql_freeresult($result);
360
				break;
361
362
				case 'user_id':
363
					$sql = 'SELECT user_id, user_colour, username
364
						FROM ' . USERS_TABLE . '
0 ignored issues
show
The constant phpbbgallery\acpcleanup\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
365
						WHERE ' . $this->db->sql_in_set('user_id', $value) . '
366
						ORDER BY user_id ASC';
367
					$result = $this->db->sql_query($sql);
368
					$value = '';
369
					while ($row = $this->db->sql_fetchrow($result))
370
					{
371
						$value .= (($value) ? ', ' : '') . get_username_string('full', $row['user_id'], (($row['user_id'] != ANONYMOUS) ? $row['username'] : $this->language->lang('GUEST')), $row['user_colour']);
0 ignored issues
show
The function get_username_string 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

371
						$value .= (($value) ? ', ' : '') . /** @scrutinizer ignore-call */ get_username_string('full', $row['user_id'], (($row['user_id'] != ANONYMOUS) ? $row['username'] : $this->language->lang('GUEST')), $row['user_colour']);
Loading history...
The constant phpbbgallery\acpcleanup\ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
372
					}
373
					$this->db->sql_freeresult($result);
374
				break;
375
376
				case 'time':
377
					$value = $this->user->format_date($value, false, true);
378
				break;
379
380
				case 'rate_avg':
381
					$value = ($value / 100);
382
				break;
383
			}
384
			$lang_pattern .= (($lang_pattern) ? '<br />' : '') . $this->language->lang('PRUNE_PATTERN_' . strtoupper($field), $value);
385
		}
386
387
		return $lang_pattern;
388
	}
389
}
390