-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.php
More file actions
324 lines (281 loc) · 8.47 KB
/
cleanup.php
File metadata and controls
324 lines (281 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
*
* @package phpBB Gallery
* @version $Id$
* @copyright (c) 2007 nickvergessen nickvergessen@gmx.de http://www.flying-bits.org
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class phpbb_ext_gallery_core_cleanup
{
/**
* Delete source files without a database entry.
*
* @param array $filenames An array of filenames
* @return string Language key for the success message.
*/
static public function delete_files($filenames)
{
foreach ($filenames as $file)
{
phpbb_gallery_image_file::delete(utf8_decode($file));
}
return 'CLEAN_ENTRIES_DONE';
}
/**
* Delete images, where the source file is missing.
*
* @param mixed $image_ids Either an array of integers or an integer.
* @return string Language key for the success message.
*/
static public function delete_images($image_ids)
{
phpbb_gallery_image::delete_images($image_ids, false, true, true);
return 'CLEAN_SOURCES_DONE';
}
/**
* Delete images, where the author is missing.
*
* @param mixed $image_ids Either an array of integers or an integer.
* @return string Language key for the success message.
*/
static public function delete_author_images($image_ids)
{
phpbb_gallery_image::delete_images($image_ids);
return 'CLEAN_AUTHORS_DONE';
}
/**
* Delete comments, where the author is missing.
*
* @param mixed $comment_ids Either an array of integers or an integer.
* @return string Language key for the success message.
*/
static public function delete_author_comments($comment_ids)
{
phpbb_gallery_comment::delete_comments($comment_ids);
return 'CLEAN_COMMENTS_DONE';
}
/**
* Delete unwanted and obsolent personal galleries.
*
* @param array $unwanted_pegas User IDs we want to delete the pegas.
* @param array $obsolent_pegas User IDs we want to delete the pegas.
* @return array Language keys for the success messages.
*/
static public function delete_pegas($unwanted_pegas, $obsolent_pegas)
{
global $db;
$delete_pegas = array_merge($unwanted_pegas, $obsolent_pegas);
$delete_images = $delete_albums = $user_image_count = array();
$num_pegas = 0;
$sql = 'SELECT album_id, parent_id
FROM ' . GALLERY_ALBUMS_TABLE . '
WHERE ' . $db->sql_in_set('album_user_id', $delete_pegas);
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$delete_albums[] = (int) $row['album_id'];
if ($row['parent_id'] == 0)
{
$num_pegas++;
}
}
$db->sql_freeresult($result);
$sql = 'SELECT image_id, image_filename, image_status, image_user_id
FROM ' . GALLERY_IMAGES_TABLE . '
WHERE ' . $db->sql_in_set('image_album_id', $delete_albums, false, true);
$result = $db->sql_query($sql);
$filenames = array();
while ($row = $db->sql_fetchrow($result))
{
$delete_images[] = (int) $row['image_id'];
$filenames[(int) $row['image_id']] = $row['image_filename'];
if (($row['image_status'] == phpbb_gallery_image::STATUS_UNAPPROVED) ||
($row['image_status'] == phpbb_gallery_image::STATUS_ORPHAN))
{
continue;
}
if (isset($user_image_count[(int) $row['image_user_id']]))
{
$user_image_count[(int) $row['image_user_id']]++;
}
else
{
$user_image_count[(int) $row['image_user_id']] = 1;
}
}
$db->sql_freeresult($result);
if (!empty($delete_images))
{
phpbb_gallery_image::delete_images($delete_images, $filenames);
}
$sql = 'DELETE FROM ' . GALLERY_ALBUMS_TABLE . '
WHERE ' . $db->sql_in_set('album_id', $delete_albums);
$db->sql_query($sql);
phpbb_gallery_config::dec('num_pegas', $num_pegas);
if (in_array(phpbb_gallery_config::get('newest_pega_album_id'), $delete_albums))
{
// Update the config for the statistic on the index
if (phpbb_gallery_config::get('num_pegas') > 0)
{
$sql_array = array(
'SELECT' => 'a.album_id, u.user_id, u.username, u.user_colour',
'FROM' => array(GALLERY_ALBUMS_TABLE => 'a'),
'LEFT_JOIN' => array(
array(
'FROM' => array(USERS_TABLE => 'u'),
'ON' => 'u.user_id = a.album_user_id',
),
),
'WHERE' => 'a.album_user_id <> ' . phpbb_gallery_album::PUBLIC_ALBUM . ' AND a.parent_id = 0',
'ORDER_BY' => 'a.album_id DESC',
);
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query_limit($sql, 1);
$newest_pega = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
}
if ((phpbb_gallery_config::get('num_pegas') > 0) && isset($newest_pega))
{
phpbb_gallery_config::set('newest_pega_user_id', $newest_pega['user_id']);
phpbb_gallery_config::set('newest_pega_username', $newest_pega['username']);
phpbb_gallery_config::set('newest_pega_user_colour', $newest_pega['user_colour']);
phpbb_gallery_config::set('newest_pega_album_id', $newest_pega['album_id']);
}
else
{
phpbb_gallery_config::set('newest_pega_user_id', 0);
phpbb_gallery_config::set('newest_pega_username', '');
phpbb_gallery_config::set('newest_pega_user_colour', '');
phpbb_gallery_config::set('newest_pega_album_id', 0);
if (isset($newest_pega))
{
phpbb_gallery_config::set('num_pegas', 0);
}
}
}
foreach ($user_image_count as $user_id => $images)
{
phpbb_gallery_hookup::add_image($user_id, (0 - $images));
$uploader = new phpbb_gallery_user($db, $user_id, false);
$uploader->update_images((0 - $images));
}
phpbb_gallery_user::update_users($delete_pegas, array('personal_album_id' => 0));
$return = array();
if ($obsolent_pegas)
{
$return[] = 'CLEAN_PERSONALS_DONE';
}
if ($unwanted_pegas)
{
$return[] = 'CLEAN_PERSONALS_BAD_DONE';
}
return $return;
}
/**
*
*/
static public function prune($pattern)
{
global $db;
$sql_where = '';
if (isset($pattern['image_album_id']))
{
$pattern['image_album_id'] = array_map('intval', explode(',', $pattern['image_album_id']));
}
if (isset($pattern['image_user_id']))
{
$pattern['image_user_id'] = array_map('intval', explode(',', $pattern['image_user_id']));
}
foreach ($pattern as $field => $value)
{
if (is_array($value))
{
$sql_where .= (($sql_where) ? ' AND ' : ' WHERE ') . $db->sql_in_set($field, $value);
continue;
}
$sql_where .= (($sql_where) ? ' AND ' : ' WHERE ') . $field . ' < ' . $value;
}
$sql = 'SELECT image_id, image_filename
FROM ' . GALLERY_IMAGES_TABLE . '
' . $sql_where;
$result = $db->sql_query($sql);
$image_ids = $filenames = $update_albums = array();
while ($row = $db->sql_fetchrow($result))
{
$image_ids[] = (int) $row['image_id'];
$filenames[(int) $row['image_id']] = $row['image_filename'];
}
$db->sql_freeresult($result);
if ($image_ids)
{
phpbb_gallery_image::delete_images($image_ids, $filenames);
}
return 'CLEAN_PRUNE_DONE';
}
/**
*
*/
static public function lang_prune_pattern($pattern)
{
global $db, $user;
if (isset($pattern['image_album_id']))
{
$pattern['image_album_id'] = array_map('intval', explode(',', $pattern['image_album_id']));
}
if (isset($pattern['image_user_id']))
{
$pattern['image_user_id'] = array_map('intval', explode(',', $pattern['image_user_id']));
}
$lang_pattern = '';
foreach ($pattern as $field => $value)
{
$field = (strpos($field, 'image_') === 0) ? substr($field, 6) : $field;
switch ($field)
{
case 'album_id':
$sql = 'SELECT album_name
FROM ' . GALLERY_ALBUMS_TABLE . '
WHERE ' . $db->sql_in_set('album_id', $value) . '
ORDER BY album_id ASC';
$result = $db->sql_query($sql);
$value = '';
while ($row = $db->sql_fetchrow($result))
{
$value .= (($value) ? ', ' : '') . $row['album_name'];
}
$db->sql_freeresult($result);
break;
case 'user_id':
$sql = 'SELECT user_id, user_colour, username
FROM ' . USERS_TABLE . '
WHERE ' . $db->sql_in_set('user_id', $value) . '
ORDER BY user_id ASC';
$result = $db->sql_query($sql);
$value = '';
while ($row = $db->sql_fetchrow($result))
{
$value .= (($value) ? ', ' : '') . get_username_string('full', $row['user_id'], (($row['user_id'] != ANONYMOUS) ? $row['username'] : $user->lang['GUEST']), $row['user_colour']);
}
$db->sql_freeresult($result);
break;
case 'time':
$value = $user->format_date($value, false, true);
break;
case 'rate_avg':
$value = ($value / 100);
break;
}
$lang_pattern .= (($lang_pattern) ? '<br />' : '') . $user->lang('PRUNE_PATTERN_' . strtoupper($field), $value);
}
return $lang_pattern;
}
}