1 | <?php |
||
2 | /** |
||
3 | * phpBB Gallery - ACP CleanUp Extension |
||
4 | * |
||
5 | * @package phpbbgallery/acpcleanup |
||
6 | * @author nickvergessen |
||
7 | * @author satanasov |
||
8 | * @author Leinad4Mind |
||
9 | * @copyright 2007 nickvergessen, 2014 satanasov, 2025 Leinad4Mind |
||
10 | * @license GPL-2.0-only |
||
11 | */ |
||
12 | |||
13 | namespace phpbbgallery\acpcleanup; |
||
14 | |||
15 | class cleanup |
||
16 | { |
||
17 | /** @var \phpbb\db\driver\driver_interface */ |
||
18 | protected $db; |
||
19 | |||
20 | /** @var \phpbbgallery\core\file\file */ |
||
21 | protected $tool; |
||
22 | |||
23 | /** @var \phpbb\user */ |
||
24 | protected $user; |
||
25 | |||
26 | /** @var \phpbb\language\language */ |
||
27 | protected $language; |
||
28 | |||
29 | /** @var \phpbbgallery\core\block */ |
||
30 | protected $block; |
||
31 | |||
32 | /** @var \phpbbgallery\core\album\album */ |
||
33 | protected $album; |
||
34 | |||
35 | /** @var \phpbbgallery\core\comment */ |
||
36 | protected $comment; |
||
37 | |||
38 | /** @var \phpbbgallery\core\config */ |
||
39 | protected $gallery_config; |
||
40 | |||
41 | /** @var \phpbbgallery\core\log */ |
||
42 | protected $log; |
||
43 | |||
44 | /** @var \phpbbgallery\core\moderate */ |
||
45 | protected $moderate; |
||
46 | |||
47 | /** @var */ |
||
48 | protected $albums_table; |
||
49 | |||
50 | /** @var */ |
||
51 | protected $images_table; |
||
52 | |||
53 | |||
54 | /** |
||
55 | * cleanup constructor. |
||
56 | * |
||
57 | * @param \phpbb\db\driver\driver_interface $db |
||
58 | * @param \phpbbgallery\core\file\file $tool |
||
59 | * @param \phpbb\user $user |
||
60 | * @param \phpbb\language\language $language |
||
61 | * @param \phpbbgallery\core\block $block |
||
62 | * @param \phpbbgallery\core\album\album $album |
||
63 | * @param \phpbbgallery\core\comment $comment |
||
64 | * @param \phpbbgallery\core\config $gallery_config |
||
65 | * @param \phpbbgallery\core\log $log |
||
66 | * @param \phpbbgallery\core\moderate $moderate |
||
67 | * @param $albums_table |
||
68 | * @param $images_table |
||
69 | */ |
||
70 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbbgallery\core\file\file $tool, \phpbb\user $user, \phpbb\language\language $language, |
||
71 | \phpbbgallery\core\block $block, \phpbbgallery\core\album\album $album, \phpbbgallery\core\comment $comment, |
||
72 | \phpbbgallery\core\config $gallery_config, \phpbbgallery\core\log $log, \phpbbgallery\core\moderate $moderate, |
||
73 | $albums_table, $images_table) |
||
74 | { |
||
75 | $this->db = $db; |
||
76 | $this->tool = $tool; |
||
77 | $this->user = $user; |
||
78 | $this->language = $language; |
||
79 | $this->block = $block; |
||
80 | $this->album = $album; |
||
81 | $this->comment = $comment; |
||
82 | $this->gallery_config = $gallery_config; |
||
83 | $this->log = $log; |
||
84 | $this->moderate = $moderate; |
||
85 | $this->albums_table = $albums_table; |
||
86 | $this->images_table = $images_table; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Delete source files without a database entry. |
||
91 | * |
||
92 | * @param array $filenames An array of filenames |
||
93 | * @return string Language key for the success message. |
||
94 | */ |
||
95 | public function delete_files($filenames) |
||
96 | { |
||
97 | foreach ($filenames as $file) |
||
98 | { |
||
99 | $this->tool->delete(utf8_decode($file)); |
||
100 | $this->tool->delete_cache(utf8_decode($file)); |
||
101 | } |
||
102 | $this->log->add_log('admin', 'clean_deletefiles', 0, 0, array('LOG_CLEANUP_DELETE_FILES', count($filenames))); |
||
103 | return 'CLEAN_ENTRIES_DONE'; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Delete images, where the source file is missing. |
||
108 | * |
||
109 | * @param mixed $image_ids Either an array of integers or an integer. |
||
110 | * @return string Language key for the success message. |
||
111 | */ |
||
112 | public function delete_images($image_ids) |
||
113 | { |
||
114 | $this->log->add_log('admin', 'clean_deleteentries', 0, 0, array('LOG_CLEANUP_DELETE_ENTRIES', count($image_ids))); |
||
115 | $this->moderate->delete_images($image_ids, false); |
||
116 | |||
117 | return 'CLEAN_SOURCES_DONE'; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Delete images, where the author is missing. |
||
122 | * |
||
123 | * @param mixed $image_ids Either an array of integers or an integer. |
||
124 | * @return string Language key for the success message. |
||
125 | */ |
||
126 | public function delete_author_images($image_ids) |
||
127 | { |
||
128 | $this->log->add_log('admin', 'clean_deletenoauthors', 0, 0, array('LOG_CLEANUP_DELETE_NO_AUTHOR', count($image_ids))); |
||
129 | $this->moderate->delete_images($image_ids); |
||
130 | |||
131 | return 'CLEAN_AUTHORS_DONE'; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Delete comments, where the author is missing. |
||
136 | * |
||
137 | * @param mixed $comment_ids Either an array of integers or an integer. |
||
138 | * @return string Language key for the success message. |
||
139 | */ |
||
140 | public function delete_author_comments($comment_ids) |
||
141 | { |
||
142 | $this->log->add_log('admin', 'clean_deletecna', 0, 0, array('LOG_CLEANUP_COMMENT_DELETE_NO_AUTHOR', count($comment_ids))); |
||
143 | $this->comment->delete_comments($comment_ids); |
||
144 | |||
145 | return 'CLEAN_COMMENTS_DONE'; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Delete unwanted and obsolete personal galleries. |
||
150 | * |
||
151 | * @param array $unwanted_pegas User IDs we want to delete the pegas. |
||
152 | * @param array $obsolete_pegas User IDs we want to delete the pegas. |
||
153 | * @return array Language keys for the success messages. |
||
154 | */ |
||
155 | public function delete_pegas($unwanted_pegas, $obsolete_pegas) |
||
156 | { |
||
157 | |||
158 | $delete_pegas = array_merge($unwanted_pegas, $obsolete_pegas); |
||
159 | |||
160 | $delete_images = $delete_albums = $user_image_count = array(); |
||
161 | $num_pegas = 0; |
||
162 | |||
163 | $sql = 'SELECT album_id, parent_id |
||
164 | FROM ' . $this->albums_table . ' |
||
165 | WHERE ' . $this->db->sql_in_set('album_user_id', $delete_pegas); |
||
166 | $result = $this->db->sql_query($sql); |
||
167 | while ($row = $this->db->sql_fetchrow($result)) |
||
168 | { |
||
169 | $delete_albums[] = (int) $row['album_id']; |
||
170 | if ($row['parent_id'] == 0) |
||
171 | { |
||
172 | $num_pegas++; |
||
173 | } |
||
174 | } |
||
175 | $this->db->sql_freeresult($result); |
||
176 | |||
177 | $sql = 'SELECT image_id, image_filename, image_status, image_user_id |
||
178 | FROM ' . $this->images_table . ' |
||
179 | WHERE ' . $this->db->sql_in_set('image_album_id', $delete_albums, false, true); |
||
180 | $result = $this->db->sql_query($sql); |
||
181 | |||
182 | $filenames = array(); |
||
183 | while ($row = $this->db->sql_fetchrow($result)) |
||
184 | { |
||
185 | $delete_images[] = (int) $row['image_id']; |
||
186 | $filenames[(int) $row['image_id']] = $row['image_filename']; |
||
187 | |||
188 | if (($row['image_status'] == $this->block->get_image_status_unapproved()) || |
||
189 | ($row['image_status'] == $this->block->get_image_status_orphan())) |
||
190 | { |
||
191 | continue; |
||
192 | } |
||
193 | |||
194 | if (isset($user_image_count[(int) $row['image_user_id']])) |
||
195 | { |
||
196 | $user_image_count[(int) $row['image_user_id']]++; |
||
197 | } |
||
198 | else |
||
199 | { |
||
200 | $user_image_count[(int) $row['image_user_id']] = 1; |
||
201 | } |
||
202 | } |
||
203 | $this->db->sql_freeresult($result); |
||
204 | |||
205 | if (!empty($delete_images)) |
||
206 | { |
||
207 | $this->moderate->delete_images($delete_images, $filenames); |
||
208 | } |
||
209 | |||
210 | $sql = 'DELETE FROM ' . $this->albums_table . ' |
||
211 | WHERE ' . $this->db->sql_in_set('album_id', $delete_albums); |
||
212 | $this->db->sql_query($sql); |
||
213 | $this->gallery_config->dec('num_pegas', $num_pegas); |
||
214 | |||
215 | if (in_array($this->gallery_config->get('newest_pega_album_id'), $delete_albums)) |
||
216 | { |
||
217 | // Update the config for the statistic on the index |
||
218 | if ($this->gallery_config->get('num_pegas') > 0) |
||
219 | { |
||
220 | $sql_array = array( |
||
221 | 'SELECT' => 'a.album_id, u.user_id, u.username, u.user_colour', |
||
222 | 'FROM' => array($this->albums_table => 'a'), |
||
223 | |||
224 | 'LEFT_JOIN' => array( |
||
225 | array( |
||
226 | 'FROM' => array(USERS_TABLE => 'u'), |
||
227 | 'ON' => 'u.user_id = a.album_user_id', |
||
228 | ), |
||
229 | ), |
||
230 | |||
231 | 'WHERE' => 'a.album_user_id <> ' . (int) $this->album->get_public() . ' AND a.parent_id = 0', |
||
232 | 'ORDER_BY' => 'a.album_id DESC', |
||
233 | ); |
||
234 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
||
235 | |||
236 | $result = $this->db->sql_query_limit($sql, 1); |
||
237 | $newest_pega = $this->db->sql_fetchrow($result); |
||
238 | $this->db->sql_freeresult($result); |
||
239 | } |
||
240 | |||
241 | if (($this->gallery_config->get('num_pegas') > 0) && isset($newest_pega)) |
||
242 | { |
||
243 | $this->gallery_config->set('newest_pega_user_id', $newest_pega['user_id']); |
||
244 | $this->gallery_config->set('newest_pega_username', $newest_pega['username']); |
||
245 | $this->gallery_config->set('newest_pega_user_colour', $newest_pega['user_colour']); |
||
246 | $this->gallery_config->set('newest_pega_album_id', $newest_pega['album_id']); |
||
247 | } |
||
248 | else |
||
249 | { |
||
250 | $this->gallery_config->set('newest_pega_user_id', 0); |
||
251 | $this->gallery_config->set('newest_pega_username', ''); |
||
252 | $this->gallery_config->set('newest_pega_user_colour', ''); |
||
253 | $this->gallery_config->set('newest_pega_album_id', 0); |
||
254 | |||
255 | if (isset($newest_pega)) |
||
256 | { |
||
257 | $this->gallery_config->set('num_pegas', 0); |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | /* |
||
262 | foreach ($user_image_count as $user_id => $images) |
||
263 | { |
||
264 | //phpbb_gallery_hookup::add_image($user_id, (0 - $images)); |
||
265 | |||
266 | $uploader = new \phpbbgallery\core\user($this->db, $user_id, false); |
||
267 | $uploader->update_images((0 - $images)); |
||
268 | } |
||
269 | \phpbbgallery\core\user::update_users($delete_pegas, array('personal_album_id' => 0)); |
||
270 | */ |
||
271 | $return = array(); |
||
272 | if ($obsolete_pegas) |
||
273 | { |
||
274 | $return[] = 'CLEAN_PERSONALS_DONE'; |
||
275 | } |
||
276 | if ($unwanted_pegas) |
||
277 | { |
||
278 | $return[] = 'CLEAN_PERSONALS_BAD_DONE'; |
||
279 | } |
||
280 | |||
281 | return $return; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * |
||
286 | */ |
||
287 | public function prune($pattern) |
||
288 | { |
||
289 | $sql_where = ''; |
||
290 | if (isset($pattern['image_album_id'])) |
||
291 | { |
||
292 | $pattern['image_album_id'] = array_map('intval', explode(',', $pattern['image_album_id'])); |
||
293 | } |
||
294 | if (isset($pattern['image_user_id'])) |
||
295 | { |
||
296 | $pattern['image_user_id'] = array_map('intval', explode(',', $pattern['image_user_id'])); |
||
297 | } |
||
298 | foreach ($pattern as $field => $value) |
||
299 | { |
||
300 | if (is_array($value)) |
||
301 | { |
||
302 | $sql_where .= (($sql_where) ? ' AND ' : ' WHERE ') . $this->db->sql_in_set($field, $value); |
||
303 | continue; |
||
304 | } |
||
305 | $sql_where .= (($sql_where) ? ' AND ' : ' WHERE ') . $field . ' < ' . $value; |
||
306 | } |
||
307 | |||
308 | $sql = 'SELECT image_id, image_filename |
||
309 | FROM ' . $this->images_table . ' |
||
310 | ' . $sql_where; |
||
311 | |||
312 | $result = $this->db->sql_query($sql); |
||
313 | $image_ids = $filenames = $update_albums = array(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
314 | while ($row = $this->db->sql_fetchrow($result)) |
||
315 | { |
||
316 | $image_ids[] = (int) $row['image_id']; |
||
317 | $filenames[(int) $row['image_id']] = $row['image_filename']; |
||
318 | } |
||
319 | $this->db->sql_freeresult($result); |
||
320 | |||
321 | if ($image_ids) |
||
322 | { |
||
323 | $this->moderate->delete_images($image_ids, $filenames); |
||
324 | } |
||
325 | |||
326 | return 'CLEAN_PRUNE_DONE'; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * |
||
331 | */ |
||
332 | public function lang_prune_pattern($pattern) |
||
333 | { |
||
334 | if (isset($pattern['image_album_id'])) |
||
335 | { |
||
336 | $pattern['image_album_id'] = array_map('intval', explode(',', $pattern['image_album_id'])); |
||
337 | } |
||
338 | if (isset($pattern['image_user_id'])) |
||
339 | { |
||
340 | $pattern['image_user_id'] = array_map('intval', explode(',', $pattern['image_user_id'])); |
||
341 | } |
||
342 | |||
343 | $lang_pattern = ''; |
||
344 | foreach ($pattern as $field => $value) |
||
345 | { |
||
346 | $field = (strpos($field, 'image_') === 0) ? substr($field, 6) : $field; |
||
347 | |||
348 | switch ($field) |
||
349 | { |
||
350 | case 'album_id': |
||
351 | $sql = 'SELECT album_name |
||
352 | FROM ' . $this->albums_table . ' |
||
353 | WHERE ' . $this->db->sql_in_set('album_id', $value) . ' |
||
354 | ORDER BY album_id ASC'; |
||
355 | $result = $this->db->sql_query($sql); |
||
356 | $value = ''; |
||
357 | while ($row = $this->db->sql_fetchrow($result)) |
||
358 | { |
||
359 | $value .= (($value) ? ', ' : '') . $row['album_name']; |
||
360 | } |
||
361 | $this->db->sql_freeresult($result); |
||
362 | break; |
||
363 | |||
364 | case 'user_id': |
||
365 | $sql = 'SELECT user_id, user_colour, username |
||
366 | FROM ' . USERS_TABLE . ' |
||
367 | WHERE ' . $this->db->sql_in_set('user_id', $value) . ' |
||
368 | ORDER BY user_id ASC'; |
||
369 | $result = $this->db->sql_query($sql); |
||
370 | $value = ''; |
||
371 | while ($row = $this->db->sql_fetchrow($result)) |
||
372 | { |
||
373 | $value .= (($value) ? ', ' : '') . get_username_string('full', $row['user_id'], (($row['user_id'] != ANONYMOUS) ? $row['username'] : $this->language->lang('GUEST')), $row['user_colour']); |
||
374 | } |
||
375 | $this->db->sql_freeresult($result); |
||
376 | break; |
||
377 | |||
378 | case 'time': |
||
379 | $value = $this->user->format_date($value, false, true); |
||
380 | break; |
||
381 | |||
382 | case 'rate_avg': |
||
383 | $value = ($value / 100); |
||
384 | break; |
||
385 | } |
||
386 | $lang_pattern .= (($lang_pattern) ? '<br />' : '') . $this->language->lang('PRUNE_PATTERN_' . strtoupper($field), $value); |
||
387 | } |
||
388 | |||
389 | return $lang_pattern; |
||
390 | } |
||
391 | } |
||
392 |