attachDirStatus()   B
last analyzed

Complexity

Conditions 7
Paths 11

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nop 2
dl 0
loc 28
rs 8.8333
c 0
b 0
f 0
nc 11
1
<?php
2
3
/**
4
 * This file doing the job of attachments and avatars maintenance and management.
5
 *
6
 * @todo refactor as controller-model
7
 *
8
 * Simple Machines Forum (SMF)
9
 *
10
 * @package SMF
11
 * @author Simple Machines http://www.simplemachines.org
12
 * @copyright 2019 Simple Machines and individual contributors
13
 * @license http://www.simplemachines.org/about/smf/license.php BSD
14
 *
15
 * @version 2.1 RC2
16
 */
17
18
if (!defined('SMF'))
19
	die('No direct access...');
20
21
/**
22
 * The main 'Attachments and Avatars' management function.
23
 * This function is the entry point for index.php?action=admin;area=manageattachments
24
 * and it calls a function based on the sub-action.
25
 * It requires the manage_attachments permission.
26
 *
27
 * @uses ManageAttachments template.
28
 * @uses Admin language file.
29
 * @uses template layer 'manage_files' for showing the tab bar.
30
 *
31
 */
32
function ManageAttachments()
33
{
34
	global $txt, $context;
35
36
	// You have to be able to moderate the forum to do this.
37
	isAllowedTo('manage_attachments');
38
39
	// Setup the template stuff we'll probably need.
40
	loadTemplate('ManageAttachments');
41
42
	// If they want to delete attachment(s), delete them. (otherwise fall through..)
43
	$subActions = array(
44
		'attachments' => 'ManageAttachmentSettings',
45
		'attachpaths' => 'ManageAttachmentPaths',
46
		'avatars' => 'ManageAvatarSettings',
47
		'browse' => 'BrowseFiles',
48
		'byAge' => 'RemoveAttachmentByAge',
49
		'bySize' => 'RemoveAttachmentBySize',
50
		'maintenance' => 'MaintainFiles',
51
		'repair' => 'RepairAttachments',
52
		'remove' => 'RemoveAttachment',
53
		'removeall' => 'RemoveAllAttachments',
54
		'transfer' => 'TransferAttachments',
55
	);
56
57
	// This uses admin tabs - as it should!
58
	$context[$context['admin_menu_name']]['tab_data'] = array(
59
		'title' => $txt['attachments_avatars'],
60
		'help' => 'manage_files',
61
		'description' => $txt['attachments_desc'],
62
	);
63
64
	call_integration_hook('integrate_manage_attachments', array(&$subActions));
65
66
	// Pick the correct sub-action.
67
	if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
68
		$context['sub_action'] = $_REQUEST['sa'];
69
	else
70
		$context['sub_action'] = 'browse';
71
72
	// Default page title is good.
73
	$context['page_title'] = $txt['attachments_avatars'];
74
75
	// Finally fall through to what we are doing.
76
	call_helper($subActions[$context['sub_action']]);
77
}
78
79
/**
80
 * Allows to show/change attachment settings.
81
 * This is the default sub-action of the 'Attachments and Avatars' center.
82
 * Called by index.php?action=admin;area=manageattachments;sa=attachments.
83
 *
84
 * @param bool $return_config Whether to return the array of config variables (used for admin search)
85
 * @return void|array If $return_config is true, simply returns the config_vars array, otherwise returns nothing
86
 * @uses 'attachments' sub template.
87
 */
88
89
function ManageAttachmentSettings($return_config = false)
90
{
91
	global $smcFunc, $txt, $modSettings, $scripturl, $context, $sourcedir, $boarddir;
92
93
	require_once($sourcedir . '/Subs-Attachments.php');
94
95
	$context['attachmentUploadDir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
96
97
	// If not set, show a default path for the base directory
98
	if (!isset($_GET['save']) && empty($modSettings['basedirectory_for_attachments']))
99
		if (is_dir($modSettings['attachmentUploadDir'][1]))
100
			$modSettings['basedirectory_for_attachments'] = $modSettings['attachmentUploadDir'][1];
101
102
		else
103
			$modSettings['basedirectory_for_attachments'] = $context['attachmentUploadDir'];
104
105
	$context['valid_upload_dir'] = is_dir($context['attachmentUploadDir']) && is_writable($context['attachmentUploadDir']);
106
107
	if (!empty($modSettings['automanage_attachments']))
108
		$context['valid_basedirectory'] = !empty($modSettings['basedirectory_for_attachments']) && is_writable($modSettings['basedirectory_for_attachments']);
109
110
	else
111
		$context['valid_basedirectory'] = true;
112
113
	// A bit of razzle dazzle with the $txt strings. :)
114
	$txt['attachment_path'] = $context['attachmentUploadDir'];
115
	$txt['basedirectory_for_attachments_path'] = isset($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : '';
116
	$txt['use_subdirectories_for_attachments_note'] = empty($modSettings['attachment_basedirectories']) || empty($modSettings['use_subdirectories_for_attachments']) ? $txt['use_subdirectories_for_attachments_note'] : '';
117
	$txt['attachmentUploadDir_multiple_configure'] = '<a href="' . $scripturl . '?action=admin;area=manageattachments;sa=attachpaths">[' . $txt['attachmentUploadDir_multiple_configure'] . ']</a>';
118
	$txt['attach_current_dir'] = empty($modSettings['automanage_attachments']) ? $txt['attach_current_dir'] : $txt['attach_last_dir'];
119
	$txt['attach_current_dir_warning'] = $txt['attach_current_dir'] . $txt['attach_current_dir_warning'];
120
	$txt['basedirectory_for_attachments_warning'] = $txt['basedirectory_for_attachments_current'] . $txt['basedirectory_for_attachments_warning'];
121
122
	// Perform a test to see if the GD module or ImageMagick are installed.
123
	$testImg = get_extension_funcs('gd') || class_exists('Imagick') || get_extension_funcs('MagickWand');
124
125
	// See if we can find if the server is set up to support the attachment limits
126
	$post_max_kb = floor(memoryReturnBytes(ini_get('post_max_size')) / 1024);
127
	$file_max_kb = floor(memoryReturnBytes(ini_get('upload_max_filesize')) / 1024);
128
129
	$config_vars = array(
130
		array('title', 'attachment_manager_settings'),
131
		// Are attachments enabled?
132
		array('select', 'attachmentEnable', array($txt['attachmentEnable_deactivate'], $txt['attachmentEnable_enable_all'], $txt['attachmentEnable_disable_new'])),
133
		'',
134
135
		// Directory and size limits.
136
		array('select', 'automanage_attachments', array(0 => $txt['attachments_normal'], 1 => $txt['attachments_auto_space'], 2 => $txt['attachments_auto_years'], 3 => $txt['attachments_auto_months'], 4 => $txt['attachments_auto_16'])),
137
		array('check', 'use_subdirectories_for_attachments', 'subtext' => $txt['use_subdirectories_for_attachments_note']),
138
		(empty($modSettings['attachment_basedirectories']) ? array('text', 'basedirectory_for_attachments', 40,) : array('var_message', 'basedirectory_for_attachments', 'message' => 'basedirectory_for_attachments_path', 'invalid' => empty($context['valid_basedirectory']), 'text_label' => (!empty($context['valid_basedirectory']) ? $txt['basedirectory_for_attachments_current'] : $txt['basedirectory_for_attachments_warning']))),
139
		empty($modSettings['attachment_basedirectories']) && $modSettings['currentAttachmentUploadDir'] == 1 && count($modSettings['attachmentUploadDir']) == 1 ? array('json', 'attachmentUploadDir', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 40, 'invalid' => !$context['valid_upload_dir'], 'disabled' => true) : array('var_message', 'attach_current_directory', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 'message' => 'attachment_path', 'invalid' => empty($context['valid_upload_dir']), 'text_label' => (!empty($context['valid_upload_dir']) ? $txt['attach_current_dir'] : $txt['attach_current_dir_warning'])),
140
		array('int', 'attachmentDirFileLimit', 'subtext' => $txt['zero_for_no_limit'], 6),
141
		array('int', 'attachmentDirSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']),
142
		array('check', 'dont_show_attach_under_post', 'subtext' => $txt['dont_show_attach_under_post_sub']),
143
		'',
144
145
		// Posting limits
146
		array('int', 'attachmentPostLimit', 'subtext' => sprintf($txt['attachment_ini_max'], $post_max_kb . ' ' . $txt['kilobyte']), 6, 'postinput' => $txt['kilobyte'], 'min' => 1, 'max' => $post_max_kb, 'disabled' => empty($post_max_kb)),
147
		array('int', 'attachmentSizeLimit', 'subtext' => sprintf($txt['attachment_ini_max'], $file_max_kb . ' ' . $txt['kilobyte']), 6, 'postinput' => $txt['kilobyte'], 'min' => 1, 'max' => $file_max_kb, 'disabled' => empty($file_max_kb)),
148
		array('int', 'attachmentNumPerPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6),
149
		// Security Items
150
		array('title', 'attachment_security_settings'),
151
		// Extension checks etc.
152
		array('check', 'attachmentCheckExtensions'),
153
		array('text', 'attachmentExtensions', 40),
154
		'',
155
156
		// Image checks.
157
		array('warning', empty($testImg) ? 'attachment_img_enc_warning' : ''),
158
		array('check', 'attachment_image_reencode'),
159
		'',
160
161
		array('warning', 'attachment_image_paranoid_warning'),
162
		array('check', 'attachment_image_paranoid'),
163
		// Thumbnail settings.
164
		array('title', 'attachment_thumbnail_settings'),
165
		array('check', 'attachmentShowImages'),
166
		array('check', 'attachmentThumbnails'),
167
		array('check', 'attachment_thumb_png'),
168
		array('check', 'attachment_thumb_memory'),
169
		array('warning', 'attachment_thumb_memory_note'),
170
		array('text', 'attachmentThumbWidth', 6),
171
		array('text', 'attachmentThumbHeight', 6),
172
		'',
173
174
		array('int', 'max_image_width', 'subtext' => $txt['zero_for_no_limit']),
175
		array('int', 'max_image_height', 'subtext' => $txt['zero_for_no_limit']),
176
	);
177
178
	$context['settings_post_javascript'] = '
179
	var storing_type = document.getElementById(\'automanage_attachments\');
180
	var base_dir = document.getElementById(\'use_subdirectories_for_attachments\');
181
182
	createEventListener(storing_type)
183
	storing_type.addEventListener("change", toggleSubDir, false);
184
	createEventListener(base_dir)
185
	base_dir.addEventListener("change", toggleSubDir, false);
186
	toggleSubDir();';
187
188
	call_integration_hook('integrate_modify_attachment_settings', array(&$config_vars));
189
190
	if ($return_config)
191
		return $config_vars;
192
193
	// These are very likely to come in handy! (i.e. without them we're doomed!)
194
	require_once($sourcedir . '/ManagePermissions.php');
195
	require_once($sourcedir . '/ManageServer.php');
196
197
	// Saving settings?
198
	if (isset($_GET['save']))
199
	{
200
		checkSession();
201
202
		if (isset($_POST['attachmentUploadDir']))
203
			unset($_POST['attachmentUploadDir']);
204
205
		if (!empty($_POST['use_subdirectories_for_attachments']))
206
		{
207
			if (isset($_POST['use_subdirectories_for_attachments']) && empty($_POST['basedirectory_for_attachments']))
208
				$_POST['basedirectory_for_attachments'] = (!empty($modSettings['basedirectory_for_attachments']) ? ($modSettings['basedirectory_for_attachments']) : $boarddir);
209
210
			if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($modSettings['attachment_basedirectories']))
211
			{
212
				if (!is_array($modSettings['attachment_basedirectories']))
213
					$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
214
			}
215
			else
216
				$modSettings['attachment_basedirectories'] = array();
217
218
			if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($_POST['basedirectory_for_attachments']) && !in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']))
219
			{
220
				$currentAttachmentUploadDir = $modSettings['currentAttachmentUploadDir'];
221
222
				if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachmentUploadDir']))
223
				{
224
					if (!automanage_attachments_create_directory($_POST['basedirectory_for_attachments']))
225
						$_POST['basedirectory_for_attachments'] = $modSettings['basedirectory_for_attachments'];
226
				}
227
228
				if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']))
229
				{
230
					$modSettings['attachment_basedirectories'][$modSettings['currentAttachmentUploadDir']] = $_POST['basedirectory_for_attachments'];
231
					updateSettings(array(
232
						'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
233
						'currentAttachmentUploadDir' => $currentAttachmentUploadDir,
234
					));
235
236
					$_POST['use_subdirectories_for_attachments'] = 1;
237
					$_POST['attachmentUploadDir'] = $smcFunc['json_encode']($modSettings['attachmentUploadDir']);
238
				}
239
			}
240
		}
241
242
		call_integration_hook('integrate_save_attachment_settings');
243
244
		saveDBSettings($config_vars);
245
		$_SESSION['adm-save'] = true;
246
		redirectexit('action=admin;area=manageattachments;sa=attachments');
247
	}
248
249
	$context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=attachments';
250
	prepareDBSettingContext($config_vars);
251
252
	$context['sub_template'] = 'show_settings';
253
}
254
255
/**
256
 * This allows to show/change avatar settings.
257
 * Called by index.php?action=admin;area=manageattachments;sa=avatars.
258
 * Show/set permissions for permissions: 'profile_server_avatar',
259
 * 	'profile_upload_avatar' and 'profile_remote_avatar'.
260
 *
261
 * @param bool $return_config Whether to return the config_vars array (used for admin search)
262
 * @return void|array Returns the config_vars array if $return_config is true, otherwise returns nothing
263
 * @uses 'avatars' sub template.
264
 */
265
function ManageAvatarSettings($return_config = false)
266
{
267
	global $txt, $context, $modSettings, $sourcedir, $scripturl;
268
	global $boarddir, $boardurl;
269
270
	// Perform a test to see if the GD module or ImageMagick are installed.
271
	$testImg = get_extension_funcs('gd') || class_exists('Imagick');
272
273
	$context['valid_avatar_dir'] = is_dir($modSettings['avatar_directory']);
274
	$context['valid_custom_avatar_dir'] = !empty($modSettings['custom_avatar_dir']) && is_dir($modSettings['custom_avatar_dir']) && is_writable($modSettings['custom_avatar_dir']);
275
276
	$config_vars = array(
277
		// Server stored avatars!
278
		array('title', 'avatar_server_stored'),
279
		array('warning', empty($testImg) ? 'avatar_img_enc_warning' : ''),
280
		array('permissions', 'profile_server_avatar', 0, $txt['avatar_server_stored_groups']),
281
		array('warning', !$context['valid_avatar_dir'] ? 'avatar_directory_wrong' : ''),
282
		array('text', 'avatar_directory', 40, 'invalid' => !$context['valid_avatar_dir']),
283
		array('text', 'avatar_url', 40),
284
		// External avatars?
285
		array('title', 'avatar_external'),
286
		array('permissions', 'profile_remote_avatar', 0, $txt['avatar_external_url_groups']),
287
		array('check', 'avatar_download_external', 0, 'onchange' => 'fUpdateStatus();'),
288
		array('text', 'avatar_max_width_external', 'subtext' => $txt['zero_for_no_limit'], 6),
289
		array('text', 'avatar_max_height_external', 'subtext' => $txt['zero_for_no_limit'], 6),
290
		array('select', 'avatar_action_too_large',
291
			array(
292
				'option_refuse' => $txt['option_refuse'],
293
				'option_css_resize' => $txt['option_css_resize'],
294
				'option_download_and_resize' => $txt['option_download_and_resize'],
295
			),
296
		),
297
		// Uploadable avatars?
298
		array('title', 'avatar_upload'),
299
		array('permissions', 'profile_upload_avatar', 0, $txt['avatar_upload_groups']),
300
		array('text', 'avatar_max_width_upload', 'subtext' => $txt['zero_for_no_limit'], 6),
301
		array('text', 'avatar_max_height_upload', 'subtext' => $txt['zero_for_no_limit'], 6),
302
		array('check', 'avatar_resize_upload', 'subtext' => $txt['avatar_resize_upload_note']),
303
		array('check', 'avatar_download_png'),
304
		array('check', 'avatar_reencode'),
305
		'',
306
307
		array('warning', 'avatar_paranoid_warning'),
308
		array('check', 'avatar_paranoid'),
309
		'',
310
311
		array('warning', !$context['valid_custom_avatar_dir'] ? 'custom_avatar_dir_wrong' : ''),
312
		array('text', 'custom_avatar_dir', 40, 'subtext' => $txt['custom_avatar_dir_desc'], 'invalid' => !$context['valid_custom_avatar_dir']),
313
		array('text', 'custom_avatar_url', 40),
314
		// Grvatars?
315
		array('title', 'gravatar_settings'),
316
		array('check', 'gravatarEnabled'),
317
		array('check', 'gravatarOverride'),
318
		array('check', 'gravatarAllowExtraEmail'),
319
		'',
320
321
		array('select', 'gravatarMaxRating',
322
			array(
323
				'G' => $txt['gravatar_maxG'],
324
				'PG' => $txt['gravatar_maxPG'],
325
				'R' => $txt['gravatar_maxR'],
326
				'X' => $txt['gravatar_maxX'],
327
			),
328
		),
329
		array('select', 'gravatarDefault',
330
			array(
331
				'mm' => $txt['gravatar_mm'],
332
				'identicon' => $txt['gravatar_identicon'],
333
				'monsterid' => $txt['gravatar_monsterid'],
334
				'wavatar' => $txt['gravatar_wavatar'],
335
				'retro' => $txt['gravatar_retro'],
336
				'blank' => $txt['gravatar_blank'],
337
			),
338
		),
339
	);
340
341
	call_integration_hook('integrate_modify_avatar_settings', array(&$config_vars));
342
343
	if ($return_config)
344
		return $config_vars;
345
346
	// We need this file for the settings template.
347
	require_once($sourcedir . '/ManageServer.php');
348
349
	// Saving avatar settings?
350
	if (isset($_GET['save']))
351
	{
352
		checkSession();
353
354
		// These settings cannot be left empty!
355
		if (empty($_POST['custom_avatar_dir']))
356
			$_POST['custom_avatar_dir'] = $boarddir . '/custom_avatar';
357
358
		if (empty($_POST['custom_avatar_url']))
359
			$_POST['custom_avatar_url'] = $boardurl . '/custom_avatar';
360
361
		if (empty($_POST['avatar_directory']))
362
			$_POST['avatar_directory'] = $boarddir . '/avatars';
363
364
		if (empty($_POST['avatar_url']))
365
			$_POST['avatar_url'] = $boardurl . '/avatars';
366
367
		call_integration_hook('integrate_save_avatar_settings');
368
369
		saveDBSettings($config_vars);
370
		$_SESSION['adm-save'] = true;
371
		redirectexit('action=admin;area=manageattachments;sa=avatars');
372
	}
373
374
	// Attempt to figure out if the admin is trying to break things.
375
	$context['settings_save_onclick'] = 'return (document.getElementById(\'custom_avatar_dir\').value == \'\' || document.getElementById(\'custom_avatar_url\').value == \'\') ? confirm(\'' . $txt['custom_avatar_check_empty'] . '\') : true;';
376
377
	// We need this for the in-line permissions
378
	createToken('admin-mp');
379
380
	// Prepare the context.
381
	$context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=avatars';
382
	prepareDBSettingContext($config_vars);
383
384
	// Add a layer for the javascript.
385
	$context['template_layers'][] = 'avatar_settings';
386
	$context['sub_template'] = 'show_settings';
387
}
388
389
/**
390
 * Show a list of attachment or avatar files.
391
 * Called by ?action=admin;area=manageattachments;sa=browse for attachments
392
 *  and ?action=admin;area=manageattachments;sa=browse;avatars for avatars.
393
 * Allows sorting by name, date, size and member.
394
 * Paginates results.
395
 */
396
function BrowseFiles()
397
{
398
	global $context, $txt, $scripturl, $modSettings;
399
	global $smcFunc, $sourcedir, $settings;
400
401
	// Attachments or avatars?
402
	$context['browse_type'] = isset($_REQUEST['avatars']) ? 'avatars' : (isset($_REQUEST['thumbs']) ? 'thumbs' : 'attachments');
403
404
	$titles = array(
405
		'attachments' => array('?action=admin;area=manageattachments;sa=browse', $txt['attachment_manager_attachments']),
406
		'avatars' => array('?action=admin;area=manageattachments;sa=browse;avatars', $txt['attachment_manager_avatars']),
407
		'thumbs' => array('?action=admin;area=manageattachments;sa=browse;thumbs', $txt['attachment_manager_thumbs']),
408
	);
409
410
	$list_title = $txt['attachment_manager_browse_files'] . ': ';
411
	foreach ($titles as $browse_type => $details)
412
	{
413
		if ($browse_type != 'attachments')
414
			$list_title .= ' | ';
415
416
		if ($context['browse_type'] == $browse_type)
417
			$list_title .= '<img src="' . $settings['images_url'] . '/selected.png" alt="&gt;"> ';
418
419
		$list_title .= '<a href="' . $scripturl . $details[0] . '">' . $details[1] . '</a>';
420
	}
421
422
	// Set the options for the list component.
423
	$listOptions = array(
424
		'id' => 'file_list',
425
		'title' => $list_title,
426
		'items_per_page' => $modSettings['defaultMaxListItems'],
427
		'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=browse' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
428
		'default_sort_col' => 'name',
429
		'no_items_label' => $txt['attachment_manager_' . ($context['browse_type'] === 'avatars' ? 'avatars' : ($context['browse_type'] === 'thumbs' ? 'thumbs' : 'attachments')) . '_no_entries'],
430
		'get_items' => array(
431
			'function' => 'list_getFiles',
432
			'params' => array(
433
				$context['browse_type'],
434
			),
435
		),
436
		'get_count' => array(
437
			'function' => 'list_getNumFiles',
438
			'params' => array(
439
				$context['browse_type'],
440
			),
441
		),
442
		'columns' => array(
443
			'name' => array(
444
				'header' => array(
445
					'value' => $txt['attachment_name'],
446
				),
447
				'data' => array(
448
					'function' => function($rowData) use ($modSettings, $context, $scripturl, $smcFunc)
449
					{
450
						$link = '<a href="';
451
452
						// In case of a custom avatar URL attachments have a fixed directory.
453
						if ($rowData['attachment_type'] == 1)
454
							$link .= sprintf('%1$s/%2$s', $modSettings['custom_avatar_url'], $rowData['filename']);
455
456
						// By default avatars are downloaded almost as attachments.
457
						elseif ($context['browse_type'] == 'avatars')
458
							$link .= sprintf('%1$s?action=dlattach;type=avatar;attach=%2$d', $scripturl, $rowData['id_attach']);
459
460
						// Normal attachments are always linked to a topic ID.
461
						else
462
							$link .= sprintf('%1$s?action=dlattach;topic=%2$d.0;attach=%3$d', $scripturl, $rowData['id_topic'], $rowData['id_attach']);
463
464
						$link .= '"';
465
466
						// Show a popup on click if it's a picture and we know its dimensions.
467
						if (!empty($rowData['width']) && !empty($rowData['height']))
468
							$link .= sprintf(' onclick="return reqWin(this.href' . ($rowData['attachment_type'] == 1 ? '' : ' + \';image\'') . ', %1$d, %2$d, true);"', $rowData['width'] + 20, $rowData['height'] + 20);
469
470
						$link .= sprintf('>%1$s</a>', preg_replace('~&amp;#(\\\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\\\1;', $smcFunc['htmlspecialchars']($rowData['filename'])));
471
472
						// Show the dimensions.
473
						if (!empty($rowData['width']) && !empty($rowData['height']))
474
							$link .= sprintf(' <span class="smalltext">%1$dx%2$d</span>', $rowData['width'], $rowData['height']);
475
476
						return $link;
477
					},
478
				),
479
				'sort' => array(
480
					'default' => 'a.filename',
481
					'reverse' => 'a.filename DESC',
482
				),
483
			),
484
			'filesize' => array(
485
				'header' => array(
486
					'value' => $txt['attachment_file_size'],
487
				),
488
				'data' => array(
489
					'function' => function($rowData) use ($txt)
490
					{
491
						return sprintf('%1$s%2$s', round($rowData['size'] / 1024, 2), $txt['kilobyte']);
492
					},
493
				),
494
				'sort' => array(
495
					'default' => 'a.size',
496
					'reverse' => 'a.size DESC',
497
				),
498
			),
499
			'member' => array(
500
				'header' => array(
501
					'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_member'] : $txt['posted_by'],
502
				),
503
				'data' => array(
504
					'function' => function($rowData) use ($scripturl, $smcFunc)
505
					{
506
						// In case of an attachment, return the poster of the attachment.
507
						if (empty($rowData['id_member']))
508
							return $smcFunc['htmlspecialchars']($rowData['poster_name']);
509
510
						// Otherwise it must be an avatar, return the link to the owner of it.
511
						else
512
							return sprintf('<a href="%1$s?action=profile;u=%2$d">%3$s</a>', $scripturl, $rowData['id_member'], $rowData['poster_name']);
513
					},
514
				),
515
				'sort' => array(
516
					'default' => 'mem.real_name',
517
					'reverse' => 'mem.real_name DESC',
518
				),
519
			),
520
			'date' => array(
521
				'header' => array(
522
					'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_last_active'] : $txt['date'],
523
				),
524
				'data' => array(
525
					'function' => function($rowData) use ($txt, $context, $scripturl)
526
					{
527
						// The date the message containing the attachment was posted or the owner of the avatar was active.
528
						$date = empty($rowData['poster_time']) ? $txt['never'] : timeformat($rowData['poster_time']);
529
530
						// Add a link to the topic in case of an attachment.
531
						if ($context['browse_type'] !== 'avatars')
532
							$date .= sprintf('<br>%1$s <a href="%2$s?topic=%3$d.msg%4$d#msg%4$d">%5$s</a>', $txt['in'], $scripturl, $rowData['id_topic'], $rowData['id_msg'], $rowData['subject']);
533
534
						return $date;
535
					},
536
				),
537
				'sort' => array(
538
					'default' => $context['browse_type'] === 'avatars' ? 'mem.last_login' : 'm.id_msg',
539
					'reverse' => $context['browse_type'] === 'avatars' ? 'mem.last_login DESC' : 'm.id_msg DESC',
540
				),
541
			),
542
			'downloads' => array(
543
				'header' => array(
544
					'value' => $txt['downloads'],
545
				),
546
				'data' => array(
547
					'db' => 'downloads',
548
					'comma_format' => true,
549
				),
550
				'sort' => array(
551
					'default' => 'a.downloads',
552
					'reverse' => 'a.downloads DESC',
553
				),
554
			),
555
			'check' => array(
556
				'header' => array(
557
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
558
					'class' => 'centercol',
559
				),
560
				'data' => array(
561
					'sprintf' => array(
562
						'format' => '<input type="checkbox" name="remove[%1$d]">',
563
						'params' => array(
564
							'id_attach' => false,
565
						),
566
					),
567
					'class' => 'centercol',
568
				),
569
			),
570
		),
571
		'form' => array(
572
			'href' => $scripturl . '?action=admin;area=manageattachments;sa=remove' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
573
			'include_sort' => true,
574
			'include_start' => true,
575
			'hidden_fields' => array(
576
				'type' => $context['browse_type'],
577
			),
578
		),
579
		'additional_rows' => array(
580
			array(
581
				'position' => 'above_table_headers',
582
				'value' => '<input type="submit" name="remove_submit" class="button you_sure" value="' . $txt['quickmod_delete_selected'] . '" data-confirm="' . $txt['confirm_delete_attachments'] . '">',
583
			),
584
			array(
585
				'position' => 'below_table_data',
586
				'value' => '<input type="submit" name="remove_submit" class="button you_sure" value="' . $txt['quickmod_delete_selected'] . '" data-confirm="' . $txt['confirm_delete_attachments'] . '">',
587
			),
588
		),
589
	);
590
591
	// Does a hook want to display their attachments better?
592
	call_integration_hook('integrate_attachments_browse', array(&$listOptions, &$titles, &$list_title));
593
594
	// Create the list.
595
	require_once($sourcedir . '/Subs-List.php');
596
	createList($listOptions);
597
598
	$context['sub_template'] = 'show_list';
599
	$context['default_list'] = 'file_list';
600
}
601
602
/**
603
 * Returns the list of attachments files (avatars or not), recorded
604
 * in the database, per the parameters received.
605
 *
606
 * @param int $start The item to start with
607
 * @param int $items_per_page How many items to show per page
608
 * @param string $sort A string indicating how to sort results
609
 * @param string $browse_type can be one of 'avatars' or ... not. :P
610
 * @return array An array of file info
611
 */
612
function list_getFiles($start, $items_per_page, $sort, $browse_type)
613
{
614
	global $smcFunc, $txt;
615
616
	// Choose a query depending on what we are viewing.
617
	if ($browse_type === 'avatars')
618
		$request = $smcFunc['db_query']('', '
619
			SELECT
620
				{string:blank_text} AS id_msg, COALESCE(mem.real_name, {string:not_applicable_text}) AS poster_name,
621
				mem.last_login AS poster_time, 0 AS id_topic, a.id_member, a.id_attach, a.filename, a.file_hash, a.attachment_type,
622
				a.size, a.width, a.height, a.downloads, {string:blank_text} AS subject, 0 AS id_board
623
			FROM {db_prefix}attachments AS a
624
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
625
			WHERE a.id_member != {int:guest_id}
626
			ORDER BY {raw:sort}
627
			LIMIT {int:start}, {int:per_page}',
628
			array(
629
				'guest_id' => 0,
630
				'blank_text' => '',
631
				'not_applicable_text' => $txt['not_applicable'],
632
				'sort' => $sort,
633
				'start' => $start,
634
				'per_page' => $items_per_page,
635
			)
636
		);
637
	else
638
		$request = $smcFunc['db_query']('', '
639
			SELECT
640
				m.id_msg, COALESCE(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.id_topic, m.id_member,
641
				a.id_attach, a.filename, a.file_hash, a.attachment_type, a.size, a.width, a.height, a.downloads, mf.subject, t.id_board
642
			FROM {db_prefix}attachments AS a
643
				INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
644
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
645
				INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
646
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
647
			WHERE a.attachment_type = {int:attachment_type}
648
			ORDER BY {raw:sort}
649
			LIMIT {int:start}, {int:per_page}',
650
			array(
651
				'attachment_type' => $browse_type == 'thumbs' ? '3' : '0',
652
				'sort' => $sort,
653
				'start' => $start,
654
				'per_page' => $items_per_page,
655
			)
656
		);
657
	$files = array();
658
	while ($row = $smcFunc['db_fetch_assoc']($request))
659
		$files[] = $row;
660
	$smcFunc['db_free_result']($request);
661
662
	return $files;
663
}
664
665
/**
666
 * Return the number of files of the specified type recorded in the database.
667
 * (the specified type being attachments or avatars).
668
 *
669
 * @param string $browse_type can be one of 'avatars' or not. (in which case they're attachments)
670
 * @return int The number of files
671
 */
672
function list_getNumFiles($browse_type)
673
{
674
	global $smcFunc;
675
676
	// Depending on the type of file, different queries are used.
677
	if ($browse_type === 'avatars')
678
		$request = $smcFunc['db_query']('', '
679
			SELECT COUNT(*)
680
			FROM {db_prefix}attachments
681
			WHERE id_member != {int:guest_id_member}',
682
			array(
683
				'guest_id_member' => 0,
684
			)
685
		);
686
	else
687
		$request = $smcFunc['db_query']('', '
688
			SELECT COUNT(*) AS num_attach
689
			FROM {db_prefix}attachments AS a
690
				INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
691
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
692
				INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
693
			WHERE a.attachment_type = {int:attachment_type}
694
				AND a.id_member = {int:guest_id_member}',
695
			array(
696
				'attachment_type' => $browse_type === 'thumbs' ? '3' : '0',
697
				'guest_id_member' => 0,
698
			)
699
		);
700
701
	list ($num_files) = $smcFunc['db_fetch_row']($request);
702
	$smcFunc['db_free_result']($request);
703
704
	return $num_files;
705
}
706
707
/**
708
 * Show several file maintenance options.
709
 * Called by ?action=admin;area=manageattachments;sa=maintain.
710
 * Calculates file statistics (total file size, number of attachments,
711
 * number of avatars, attachment space available).
712
 *
713
 * @uses the 'maintain' sub template.
714
 */
715
function MaintainFiles()
716
{
717
	global $context, $modSettings, $smcFunc;
718
719
	$context['sub_template'] = 'maintenance';
720
721
	$attach_dirs = $modSettings['attachmentUploadDir'];
722
723
	// Get the number of attachments....
724
	$request = $smcFunc['db_query']('', '
725
		SELECT COUNT(*)
726
		FROM {db_prefix}attachments
727
		WHERE attachment_type = {int:attachment_type}
728
			AND id_member = {int:guest_id_member}',
729
		array(
730
			'attachment_type' => 0,
731
			'guest_id_member' => 0,
732
		)
733
	);
734
	list ($context['num_attachments']) = $smcFunc['db_fetch_row']($request);
735
	$smcFunc['db_free_result']($request);
736
	$context['num_attachments'] = comma_format($context['num_attachments'], 0);
737
738
	// Also get the avatar amount....
739
	$request = $smcFunc['db_query']('', '
740
		SELECT COUNT(*)
741
		FROM {db_prefix}attachments
742
		WHERE id_member != {int:guest_id_member}',
743
		array(
744
			'guest_id_member' => 0,
745
		)
746
	);
747
	list ($context['num_avatars']) = $smcFunc['db_fetch_row']($request);
748
	$smcFunc['db_free_result']($request);
749
	$context['num_avatars'] = comma_format($context['num_avatars'], 0);
750
751
	// Check the size of all the directories.
752
	$request = $smcFunc['db_query']('', '
753
		SELECT SUM(size)
754
		FROM {db_prefix}attachments
755
		WHERE attachment_type != {int:type}',
756
		array(
757
			'type' => 1,
758
		)
759
	);
760
	list ($attachmentDirSize) = $smcFunc['db_fetch_row']($request);
761
	$smcFunc['db_free_result']($request);
762
763
	// Divide it into kilobytes.
764
	$attachmentDirSize /= 1024;
765
	$context['attachment_total_size'] = comma_format($attachmentDirSize, 2);
766
767
	$request = $smcFunc['db_query']('', '
768
		SELECT COUNT(*), SUM(size)
769
		FROM {db_prefix}attachments
770
		WHERE id_folder = {int:folder_id}
771
			AND attachment_type != {int:type}',
772
		array(
773
			'folder_id' => $modSettings['currentAttachmentUploadDir'],
774
			'type' => 1,
775
		)
776
	);
777
	list ($current_dir_files, $current_dir_size) = $smcFunc['db_fetch_row']($request);
778
	$smcFunc['db_free_result']($request);
779
	$current_dir_size /= 1024;
780
781
	// If they specified a limit only....
782
	if (!empty($modSettings['attachmentDirSizeLimit']))
783
		$context['attachment_space'] = comma_format(max($modSettings['attachmentDirSizeLimit'] - $current_dir_size, 0), 2);
784
	$context['attachment_current_size'] = comma_format($current_dir_size, 2);
785
786
	if (!empty($modSettings['attachmentDirFileLimit']))
787
		$context['attachment_files'] = comma_format(max($modSettings['attachmentDirFileLimit'] - $current_dir_files, 0), 0);
788
	$context['attachment_current_files'] = comma_format($current_dir_files, 0);
789
790
	$context['attach_multiple_dirs'] = count($attach_dirs) > 1 ? true : false;
791
	$context['attach_dirs'] = $attach_dirs;
792
	$context['base_dirs'] = !empty($modSettings['attachment_basedirectories']) ? $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true) : array();
793
	$context['checked'] = isset($_SESSION['checked']) ? $_SESSION['checked'] : true;
794
	if (!empty($_SESSION['results']))
795
	{
796
		$context['results'] = implode('<br>', $_SESSION['results']);
797
		unset($_SESSION['results']);
798
	}
799
}
800
801
/**
802
 * Remove attachments older than a given age.
803
 * Called from the maintenance screen by
804
 *   ?action=admin;area=manageattachments;sa=byAge.
805
 * It optionally adds a certain text to the messages the attachments
806
 *  were removed from.
807
 *
808
 * @todo refactor this silly superglobals use...
809
 */
810
function RemoveAttachmentByAge()
811
{
812
	global $smcFunc;
813
814
	checkSession('post', 'admin');
815
816
	// @todo Ignore messages in topics that are stickied?
817
818
	// Deleting an attachment?
819
	if ($_REQUEST['type'] != 'avatars')
820
	{
821
		// Get rid of all the old attachments.
822
		$messages = removeAttachments(array('attachment_type' => 0, 'poster_time' => (time() - 24 * 60 * 60 * $_POST['age'])), 'messages', true);
823
824
		// Update the messages to reflect the change.
825
		if (!empty($messages) && !empty($_POST['notice']))
826
			$smcFunc['db_query']('', '
827
				UPDATE {db_prefix}messages
828
				SET body = CONCAT(body, {string:notice})
829
				WHERE id_msg IN ({array_int:messages})',
830
				array(
831
					'messages' => $messages,
832
					'notice' => '<br><br>' . $_POST['notice'],
833
				)
834
			);
835
	}
836
	else
837
	{
838
		// Remove all the old avatars.
839
		removeAttachments(array('not_id_member' => 0, 'last_login' => (time() - 24 * 60 * 60 * $_POST['age'])), 'members');
840
	}
841
	redirectexit('action=admin;area=manageattachments' . (empty($_REQUEST['avatars']) ? ';sa=maintenance' : ';avatars'));
842
}
843
844
/**
845
 * Remove attachments larger than a given size.
846
 * Called from the maintenance screen by
847
 *  ?action=admin;area=manageattachments;sa=bySize.
848
 * Optionally adds a certain text to the messages the attachments were
849
 * 	removed from.
850
 */
851
function RemoveAttachmentBySize()
852
{
853
	global $smcFunc;
854
855
	checkSession('post', 'admin');
856
857
	// Find humungous attachments.
858
	$messages = removeAttachments(array('attachment_type' => 0, 'size' => 1024 * $_POST['size']), 'messages', true);
859
860
	// And make a note on the post.
861
	if (!empty($messages) && !empty($_POST['notice']))
862
		$smcFunc['db_query']('', '
863
			UPDATE {db_prefix}messages
864
			SET body = CONCAT(body, {string:notice})
865
			WHERE id_msg IN ({array_int:messages})',
866
			array(
867
				'messages' => $messages,
868
				'notice' => '<br><br>' . $_POST['notice'],
869
			)
870
		);
871
872
	redirectexit('action=admin;area=manageattachments;sa=maintenance');
873
}
874
875
/**
876
 * Remove a selection of attachments or avatars.
877
 * Called from the browse screen as submitted form by
878
 *  ?action=admin;area=manageattachments;sa=remove
879
 */
880
function RemoveAttachment()
881
{
882
	global $txt, $smcFunc, $language, $user_info;
883
884
	checkSession();
885
886
	if (!empty($_POST['remove']))
887
	{
888
		$attachments = array();
889
		// There must be a quicker way to pass this safety test??
890
		foreach ($_POST['remove'] as $removeID => $dummy)
891
			$attachments[] = (int) $removeID;
892
893
		// If the attachments are from a 3rd party, let them remove it. Hooks should remove their ids from the array.
894
		$filesRemoved = false;
895
		call_integration_hook('integrate_attachment_remove', array(&$filesRemoved, $attachments));
896
897
		if ($_REQUEST['type'] == 'avatars' && !empty($attachments))
898
			removeAttachments(array('id_attach' => $attachments));
899
		elseif (!empty($attachments))
900
		{
901
			$messages = removeAttachments(array('id_attach' => $attachments), 'messages', true);
902
903
			// And change the message to reflect this.
904
			if (!empty($messages))
905
			{
906
				loadLanguage('index', $language, true);
907
				$smcFunc['db_query']('', '
908
					UPDATE {db_prefix}messages
909
					SET body = CONCAT(body, {string:deleted_message})
910
					WHERE id_msg IN ({array_int:messages_affected})',
911
					array(
912
						'messages_affected' => $messages,
913
						'deleted_message' => '<br><br>' . $txt['attachment_delete_admin'],
914
					)
915
				);
916
				loadLanguage('index', $user_info['language'], true);
917
			}
918
		}
919
	}
920
921
	$_GET['sort'] = isset($_GET['sort']) ? $_GET['sort'] : 'date';
922
	redirectexit('action=admin;area=manageattachments;sa=browse;' . $_REQUEST['type'] . ';sort=' . $_GET['sort'] . (isset($_GET['desc']) ? ';desc' : '') . ';start=' . $_REQUEST['start']);
923
}
924
925
/**
926
 * Removes all attachments in a single click
927
 * Called from the maintenance screen by
928
 *  ?action=admin;area=manageattachments;sa=removeall.
929
 */
930
function RemoveAllAttachments()
931
{
932
	global $txt, $smcFunc;
933
934
	checkSession('get', 'admin');
935
936
	$messages = removeAttachments(array('attachment_type' => 0), '', true);
937
938
	if (!isset($_POST['notice']))
939
		$_POST['notice'] = $txt['attachment_delete_admin'];
940
941
	// Add the notice on the end of the changed messages.
942
	if (!empty($messages))
943
		$smcFunc['db_query']('', '
944
			UPDATE {db_prefix}messages
945
			SET body = CONCAT(body, {string:deleted_message})
946
			WHERE id_msg IN ({array_int:messages})',
947
			array(
948
				'messages' => $messages,
949
				'deleted_message' => '<br><br>' . $_POST['notice'],
950
			)
951
		);
952
953
	redirectexit('action=admin;area=manageattachments;sa=maintenance');
954
}
955
956
/**
957
 * Removes attachments or avatars based on a given query condition.
958
 * Called by several remove avatar/attachment functions in this file.
959
 * It removes attachments based that match the $condition.
960
 * It allows query_types 'messages' and 'members', whichever is need by the
961
 * $condition parameter.
962
 * It does no permissions check.
963
 *
964
 * @internal
965
 *
966
 * @param array $condition An array of conditions
967
 * @param string $query_type The query type. Can be 'messages' or 'members'
968
 * @param bool $return_affected_messages Whether to return an array with the IDs of affected messages
969
 * @param bool $autoThumbRemoval Whether to automatically remove any thumbnails associated with the removed files
970
 * @return void|int[] Returns an array containing IDs of affected messages if $return_affected_messages is true
971
 */
972
function removeAttachments($condition, $query_type = '', $return_affected_messages = false, $autoThumbRemoval = true)
973
{
974
	global $modSettings, $smcFunc;
975
976
	// @todo This might need more work!
977
	$new_condition = array();
978
	$query_parameter = array(
979
		'thumb_attachment_type' => 3,
980
	);
981
	$do_logging = array();
982
983
	if (is_array($condition))
0 ignored issues
show
introduced by
The condition is_array($condition) is always true.
Loading history...
984
	{
985
		foreach ($condition as $real_type => $restriction)
986
		{
987
			// Doing a NOT?
988
			$is_not = substr($real_type, 0, 4) == 'not_';
989
			$type = $is_not ? substr($real_type, 4) : $real_type;
990
991
			if (in_array($type, array('id_member', 'id_attach', 'id_msg')))
992
				$new_condition[] = 'a.' . $type . ($is_not ? ' NOT' : '') . ' IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
993
			elseif ($type == 'attachment_type')
994
				$new_condition[] = 'a.attachment_type = {int:' . $real_type . '}';
995
			elseif ($type == 'poster_time')
996
				$new_condition[] = 'm.poster_time < {int:' . $real_type . '}';
997
			elseif ($type == 'last_login')
998
				$new_condition[] = 'mem.last_login < {int:' . $real_type . '}';
999
			elseif ($type == 'size')
1000
				$new_condition[] = 'a.size > {int:' . $real_type . '}';
1001
			elseif ($type == 'id_topic')
1002
				$new_condition[] = 'm.id_topic IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
1003
1004
			// Add the parameter!
1005
			$query_parameter[$real_type] = $restriction;
1006
1007
			if ($type == 'do_logging')
1008
				$do_logging = $condition['id_attach'];
1009
		}
1010
		$condition = implode(' AND ', $new_condition);
1011
	}
1012
1013
	// Delete it only if it exists...
1014
	$msgs = array();
1015
	$attach = array();
1016
	$parents = array();
1017
1018
	// Get all the attachment names and id_msg's.
1019
	$request = $smcFunc['db_query']('', '
1020
		SELECT
1021
			a.id_folder, a.filename, a.file_hash, a.attachment_type, a.id_attach, a.id_member' . ($query_type == 'messages' ? ', m.id_msg' : ', a.id_msg') . ',
1022
			thumb.id_folder AS thumb_folder, COALESCE(thumb.id_attach, 0) AS id_thumb, thumb.filename AS thumb_filename, thumb.file_hash AS thumb_file_hash, thumb_parent.id_attach AS id_parent
1023
		FROM {db_prefix}attachments AS a' . ($query_type == 'members' ? '
1024
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)' : ($query_type == 'messages' ? '
1025
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)' : '')) . '
1026
			LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
1027
			LEFT JOIN {db_prefix}attachments AS thumb_parent ON (thumb.attachment_type = {int:thumb_attachment_type} AND thumb_parent.id_thumb = a.id_attach)
1028
		WHERE ' . $condition,
1029
		$query_parameter
1030
	);
1031
	while ($row = $smcFunc['db_fetch_assoc']($request))
1032
	{
1033
		// Figure out the "encrypted" filename and unlink it ;).
1034
		if ($row['attachment_type'] == 1)
1035
		{
1036
			// if attachment_type = 1, it's... an avatar in a custom avatar directory.
1037
			// wasn't it obvious? :P
1038
			// @todo look again at this.
1039
			@unlink($modSettings['custom_avatar_dir'] . '/' . $row['filename']);
1040
		}
1041
		else
1042
		{
1043
			$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
1044
			@unlink($filename);
1045
1046
			// If this was a thumb, the parent attachment should know about it.
1047
			if (!empty($row['id_parent']))
1048
				$parents[] = $row['id_parent'];
1049
1050
			// If this attachments has a thumb, remove it as well.
1051
			if (!empty($row['id_thumb']) && $autoThumbRemoval)
1052
			{
1053
				$thumb_filename = getAttachmentFilename($row['thumb_filename'], $row['id_thumb'], $row['thumb_folder'], false, $row['thumb_file_hash']);
1054
				@unlink($thumb_filename);
1055
				$attach[] = $row['id_thumb'];
1056
			}
1057
		}
1058
1059
		// Make a list.
1060
		if ($return_affected_messages && empty($row['attachment_type']))
1061
			$msgs[] = $row['id_msg'];
1062
1063
		$attach[] = $row['id_attach'];
1064
	}
1065
	$smcFunc['db_free_result']($request);
1066
1067
	// Removed attachments don't have to be updated anymore.
1068
	$parents = array_diff($parents, $attach);
1069
	if (!empty($parents))
1070
		$smcFunc['db_query']('', '
1071
			UPDATE {db_prefix}attachments
1072
			SET id_thumb = {int:no_thumb}
1073
			WHERE id_attach IN ({array_int:parent_attachments})',
1074
			array(
1075
				'parent_attachments' => $parents,
1076
				'no_thumb' => 0,
1077
			)
1078
		);
1079
1080
	if (!empty($do_logging))
1081
	{
1082
		// In order to log the attachments, we really need their message and filename
1083
		$request = $smcFunc['db_query']('', '
1084
			SELECT m.id_msg, a.filename
1085
			FROM {db_prefix}attachments AS a
1086
				INNER JOIN {db_prefix}messages AS m ON (a.id_msg = m.id_msg)
1087
			WHERE a.id_attach IN ({array_int:attachments})
1088
				AND a.attachment_type = {int:attachment_type}',
1089
			array(
1090
				'attachments' => $do_logging,
1091
				'attachment_type' => 0,
1092
			)
1093
		);
1094
1095
		while ($row = $smcFunc['db_fetch_assoc']($request))
1096
			logAction(
1097
				'remove_attach',
1098
				array(
1099
					'message' => $row['id_msg'],
1100
					'filename' => preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($row['filename'])),
1101
				)
1102
			);
1103
		$smcFunc['db_free_result']($request);
1104
	}
1105
1106
	if (!empty($attach))
1107
		$smcFunc['db_query']('', '
1108
			DELETE FROM {db_prefix}attachments
1109
			WHERE id_attach IN ({array_int:attachment_list})',
1110
			array(
1111
				'attachment_list' => $attach,
1112
			)
1113
		);
1114
1115
	call_integration_hook('integrate_remove_attachments', array($attach));
1116
1117
	if ($return_affected_messages)
1118
		return array_unique($msgs);
1119
}
1120
1121
/**
1122
 * This function should find attachments in the database that no longer exist and clear them, and fix filesize issues.
1123
 */
1124
function RepairAttachments()
1125
{
1126
	global $modSettings, $context, $txt, $smcFunc;
1127
1128
	checkSession('get');
1129
1130
	// If we choose cancel, redirect right back.
1131
	if (isset($_POST['cancel']))
1132
		redirectexit('action=admin;area=manageattachments;sa=maintenance');
1133
1134
	// Try give us a while to sort this out...
1135
	@set_time_limit(600);
1136
1137
	$_GET['step'] = empty($_GET['step']) ? 0 : (int) $_GET['step'];
1138
	$context['starting_substep'] = $_GET['substep'] = empty($_GET['substep']) ? 0 : (int) $_GET['substep'];
1139
1140
	// Don't recall the session just in case.
1141
	if ($_GET['step'] == 0 && $_GET['substep'] == 0)
1142
	{
1143
		unset($_SESSION['attachments_to_fix'], $_SESSION['attachments_to_fix2']);
1144
1145
		// If we're actually fixing stuff - work out what.
1146
		if (isset($_GET['fixErrors']))
1147
		{
1148
			// Nothing?
1149
			if (empty($_POST['to_fix']))
1150
				redirectexit('action=admin;area=manageattachments;sa=maintenance');
1151
1152
			$_SESSION['attachments_to_fix'] = array();
1153
			// @todo No need to do this I think.
1154
			foreach ($_POST['to_fix'] as $value)
1155
				$_SESSION['attachments_to_fix'][] = $value;
1156
		}
1157
	}
1158
1159
	// All the valid problems are here:
1160
	$context['repair_errors'] = array(
1161
		'missing_thumbnail_parent' => 0,
1162
		'parent_missing_thumbnail' => 0,
1163
		'file_missing_on_disk' => 0,
1164
		'file_wrong_size' => 0,
1165
		'file_size_of_zero' => 0,
1166
		'attachment_no_msg' => 0,
1167
		'avatar_no_member' => 0,
1168
		'wrong_folder' => 0,
1169
		'files_without_attachment' => 0,
1170
	);
1171
1172
	$to_fix = !empty($_SESSION['attachments_to_fix']) ? $_SESSION['attachments_to_fix'] : array();
1173
	$context['repair_errors'] = isset($_SESSION['attachments_to_fix2']) ? $_SESSION['attachments_to_fix2'] : $context['repair_errors'];
1174
	$fix_errors = isset($_GET['fixErrors']) ? true : false;
1175
1176
	// Get stranded thumbnails.
1177
	if ($_GET['step'] <= 0)
1178
	{
1179
		$result = $smcFunc['db_query']('', '
1180
			SELECT MAX(id_attach)
1181
			FROM {db_prefix}attachments
1182
			WHERE attachment_type = {int:thumbnail}',
1183
			array(
1184
				'thumbnail' => 3,
1185
			)
1186
		);
1187
		list ($thumbnails) = $smcFunc['db_fetch_row']($result);
1188
		$smcFunc['db_free_result']($result);
1189
1190
		for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
1191
		{
1192
			$to_remove = array();
1193
1194
			$result = $smcFunc['db_query']('', '
1195
				SELECT thumb.id_attach, thumb.id_folder, thumb.filename, thumb.file_hash
1196
				FROM {db_prefix}attachments AS thumb
1197
					LEFT JOIN {db_prefix}attachments AS tparent ON (tparent.id_thumb = thumb.id_attach)
1198
				WHERE thumb.id_attach BETWEEN {int:substep} AND {int:substep} + 499
1199
					AND thumb.attachment_type = {int:thumbnail}
1200
					AND tparent.id_attach IS NULL',
1201
				array(
1202
					'thumbnail' => 3,
1203
					'substep' => $_GET['substep'],
1204
				)
1205
			);
1206
			while ($row = $smcFunc['db_fetch_assoc']($result))
1207
			{
1208
				// Only do anything once... just in case
1209
				if (!isset($to_remove[$row['id_attach']]))
1210
				{
1211
					$to_remove[$row['id_attach']] = $row['id_attach'];
1212
					$context['repair_errors']['missing_thumbnail_parent']++;
1213
1214
					// If we are repairing remove the file from disk now.
1215
					if ($fix_errors && in_array('missing_thumbnail_parent', $to_fix))
1216
					{
1217
						$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
1218
						@unlink($filename);
1219
					}
1220
				}
1221
			}
1222
			if ($smcFunc['db_num_rows']($result) != 0)
1223
				$to_fix[] = 'missing_thumbnail_parent';
1224
			$smcFunc['db_free_result']($result);
1225
1226
			// Do we need to delete what we have?
1227
			if ($fix_errors && !empty($to_remove) && in_array('missing_thumbnail_parent', $to_fix))
1228
				$smcFunc['db_query']('', '
1229
					DELETE FROM {db_prefix}attachments
1230
					WHERE id_attach IN ({array_int:to_remove})
1231
						AND attachment_type = {int:attachment_type}',
1232
					array(
1233
						'to_remove' => $to_remove,
1234
						'attachment_type' => 3,
1235
					)
1236
				);
1237
1238
			pauseAttachmentMaintenance($to_fix, $thumbnails);
1239
		}
1240
1241
		$_GET['step'] = 1;
1242
		$_GET['substep'] = 0;
1243
		pauseAttachmentMaintenance($to_fix);
1244
	}
1245
1246
	// Find parents which think they have thumbnails, but actually, don't.
1247
	if ($_GET['step'] <= 1)
1248
	{
1249
		$result = $smcFunc['db_query']('', '
1250
			SELECT MAX(id_attach)
1251
			FROM {db_prefix}attachments
1252
			WHERE id_thumb != {int:no_thumb}',
1253
			array(
1254
				'no_thumb' => 0,
1255
			)
1256
		);
1257
		list ($thumbnails) = $smcFunc['db_fetch_row']($result);
1258
		$smcFunc['db_free_result']($result);
1259
1260
		for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
1261
		{
1262
			$to_update = array();
1263
1264
			$result = $smcFunc['db_query']('', '
1265
				SELECT a.id_attach
1266
				FROM {db_prefix}attachments AS a
1267
					LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
1268
				WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
1269
					AND a.id_thumb != {int:no_thumb}
1270
					AND thumb.id_attach IS NULL',
1271
				array(
1272
					'no_thumb' => 0,
1273
					'substep' => $_GET['substep'],
1274
				)
1275
			);
1276
			while ($row = $smcFunc['db_fetch_assoc']($result))
1277
			{
1278
				$to_update[] = $row['id_attach'];
1279
				$context['repair_errors']['parent_missing_thumbnail']++;
1280
			}
1281
			if ($smcFunc['db_num_rows']($result) != 0)
1282
				$to_fix[] = 'parent_missing_thumbnail';
1283
			$smcFunc['db_free_result']($result);
1284
1285
			// Do we need to delete what we have?
1286
			if ($fix_errors && !empty($to_update) && in_array('parent_missing_thumbnail', $to_fix))
1287
				$smcFunc['db_query']('', '
1288
					UPDATE {db_prefix}attachments
1289
					SET id_thumb = {int:no_thumb}
1290
					WHERE id_attach IN ({array_int:to_update})',
1291
					array(
1292
						'to_update' => $to_update,
1293
						'no_thumb' => 0,
1294
					)
1295
				);
1296
1297
			pauseAttachmentMaintenance($to_fix, $thumbnails);
1298
		}
1299
1300
		$_GET['step'] = 2;
1301
		$_GET['substep'] = 0;
1302
		pauseAttachmentMaintenance($to_fix);
1303
	}
1304
1305
	// This may take forever I'm afraid, but life sucks... recount EVERY attachments!
1306
	if ($_GET['step'] <= 2)
1307
	{
1308
		$result = $smcFunc['db_query']('', '
1309
			SELECT MAX(id_attach)
1310
			FROM {db_prefix}attachments',
1311
			array(
1312
			)
1313
		);
1314
		list ($thumbnails) = $smcFunc['db_fetch_row']($result);
1315
		$smcFunc['db_free_result']($result);
1316
1317
		for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 250)
1318
		{
1319
			$to_remove = array();
1320
			$errors_found = array();
1321
1322
			$result = $smcFunc['db_query']('', '
1323
				SELECT id_attach, id_folder, filename, file_hash, size, attachment_type
1324
				FROM {db_prefix}attachments
1325
				WHERE id_attach BETWEEN {int:substep} AND {int:substep} + 249',
1326
				array(
1327
					'substep' => $_GET['substep'],
1328
				)
1329
			);
1330
			while ($row = $smcFunc['db_fetch_assoc']($result))
1331
			{
1332
				// Get the filename.
1333
				if ($row['attachment_type'] == 1)
1334
					$filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
1335
				else
1336
					$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
1337
1338
				// File doesn't exist?
1339
				if (!file_exists($filename))
1340
				{
1341
					// If we're lucky it might just be in a different folder.
1342
					if (!empty($modSettings['currentAttachmentUploadDir']))
1343
					{
1344
						// Get the attachment name with out the folder.
1345
						$attachment_name = $row['id_attach'] . '_' . $row['file_hash'] . '.dat';
1346
1347
						// Loop through the other folders.
1348
						foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
1349
							if (file_exists($dir . '/' . $attachment_name))
1350
							{
1351
								$context['repair_errors']['wrong_folder']++;
1352
								$errors_found[] = 'wrong_folder';
1353
1354
								// Are we going to fix this now?
1355
								if ($fix_errors && in_array('wrong_folder', $to_fix))
1356
									$smcFunc['db_query']('', '
1357
										UPDATE {db_prefix}attachments
1358
										SET id_folder = {int:new_folder}
1359
										WHERE id_attach = {int:id_attach}',
1360
										array(
1361
											'new_folder' => $id,
1362
											'id_attach' => $row['id_attach'],
1363
										)
1364
									);
1365
1366
								continue 2;
1367
							}
1368
					}
1369
1370
					$to_remove[] = $row['id_attach'];
1371
					$context['repair_errors']['file_missing_on_disk']++;
1372
					$errors_found[] = 'file_missing_on_disk';
1373
				}
1374
				elseif (filesize($filename) == 0)
1375
				{
1376
					$context['repair_errors']['file_size_of_zero']++;
1377
					$errors_found[] = 'file_size_of_zero';
1378
1379
					// Fixing?
1380
					if ($fix_errors && in_array('file_size_of_zero', $to_fix))
1381
					{
1382
						$to_remove[] = $row['id_attach'];
1383
						@unlink($filename);
1384
					}
1385
				}
1386
				elseif (filesize($filename) != $row['size'])
1387
				{
1388
					$context['repair_errors']['file_wrong_size']++;
1389
					$errors_found[] = 'file_wrong_size';
1390
1391
					// Fix it here?
1392
					if ($fix_errors && in_array('file_wrong_size', $to_fix))
1393
					{
1394
						$smcFunc['db_query']('', '
1395
							UPDATE {db_prefix}attachments
1396
							SET size = {int:filesize}
1397
							WHERE id_attach = {int:id_attach}',
1398
							array(
1399
								'filesize' => filesize($filename),
1400
								'id_attach' => $row['id_attach'],
1401
							)
1402
						);
1403
					}
1404
				}
1405
			}
1406
1407
			if (in_array('file_missing_on_disk', $errors_found))
1408
				$to_fix[] = 'file_missing_on_disk';
1409
			if (in_array('file_size_of_zero', $errors_found))
1410
				$to_fix[] = 'file_size_of_zero';
1411
			if (in_array('file_wrong_size', $errors_found))
1412
				$to_fix[] = 'file_wrong_size';
1413
			if (in_array('wrong_folder', $errors_found))
1414
				$to_fix[] = 'wrong_folder';
1415
			$smcFunc['db_free_result']($result);
1416
1417
			// Do we need to delete what we have?
1418
			if ($fix_errors && !empty($to_remove))
1419
			{
1420
				$smcFunc['db_query']('', '
1421
					DELETE FROM {db_prefix}attachments
1422
					WHERE id_attach IN ({array_int:to_remove})',
1423
					array(
1424
						'to_remove' => $to_remove,
1425
					)
1426
				);
1427
				$smcFunc['db_query']('', '
1428
					UPDATE {db_prefix}attachments
1429
					SET id_thumb = {int:no_thumb}
1430
					WHERE id_thumb IN ({array_int:to_remove})',
1431
					array(
1432
						'to_remove' => $to_remove,
1433
						'no_thumb' => 0,
1434
					)
1435
				);
1436
			}
1437
1438
			pauseAttachmentMaintenance($to_fix, $thumbnails);
1439
		}
1440
1441
		$_GET['step'] = 3;
1442
		$_GET['substep'] = 0;
1443
		pauseAttachmentMaintenance($to_fix);
1444
	}
1445
1446
	// Get avatars with no members associated with them.
1447
	if ($_GET['step'] <= 3)
1448
	{
1449
		$result = $smcFunc['db_query']('', '
1450
			SELECT MAX(id_attach)
1451
			FROM {db_prefix}attachments',
1452
			array(
1453
			)
1454
		);
1455
		list ($thumbnails) = $smcFunc['db_fetch_row']($result);
1456
		$smcFunc['db_free_result']($result);
1457
1458
		for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
1459
		{
1460
			$to_remove = array();
1461
1462
			$result = $smcFunc['db_query']('', '
1463
				SELECT a.id_attach, a.id_folder, a.filename, a.file_hash, a.attachment_type
1464
				FROM {db_prefix}attachments AS a
1465
					LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
1466
				WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
1467
					AND a.id_member != {int:no_member}
1468
					AND a.id_msg = {int:no_msg}
1469
					AND mem.id_member IS NULL',
1470
				array(
1471
					'no_member' => 0,
1472
					'no_msg' => 0,
1473
					'substep' => $_GET['substep'],
1474
				)
1475
			);
1476
			while ($row = $smcFunc['db_fetch_assoc']($result))
1477
			{
1478
				$to_remove[] = $row['id_attach'];
1479
				$context['repair_errors']['avatar_no_member']++;
1480
1481
				// If we are repairing remove the file from disk now.
1482
				if ($fix_errors && in_array('avatar_no_member', $to_fix))
1483
				{
1484
					if ($row['attachment_type'] == 1)
1485
						$filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
1486
					else
1487
						$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
1488
					@unlink($filename);
1489
				}
1490
			}
1491
			if ($smcFunc['db_num_rows']($result) != 0)
1492
				$to_fix[] = 'avatar_no_member';
1493
			$smcFunc['db_free_result']($result);
1494
1495
			// Do we need to delete what we have?
1496
			if ($fix_errors && !empty($to_remove) && in_array('avatar_no_member', $to_fix))
1497
				$smcFunc['db_query']('', '
1498
					DELETE FROM {db_prefix}attachments
1499
					WHERE id_attach IN ({array_int:to_remove})
1500
						AND id_member != {int:no_member}
1501
						AND id_msg = {int:no_msg}',
1502
					array(
1503
						'to_remove' => $to_remove,
1504
						'no_member' => 0,
1505
						'no_msg' => 0,
1506
					)
1507
				);
1508
1509
			pauseAttachmentMaintenance($to_fix, $thumbnails);
1510
		}
1511
1512
		$_GET['step'] = 4;
1513
		$_GET['substep'] = 0;
1514
		pauseAttachmentMaintenance($to_fix);
1515
	}
1516
1517
	// What about attachments, who are missing a message :'(
1518
	if ($_GET['step'] <= 4)
1519
	{
1520
		$result = $smcFunc['db_query']('', '
1521
			SELECT MAX(id_attach)
1522
			FROM {db_prefix}attachments',
1523
			array(
1524
			)
1525
		);
1526
		list ($thumbnails) = $smcFunc['db_fetch_row']($result);
1527
		$smcFunc['db_free_result']($result);
1528
1529
		for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
1530
		{
1531
			$to_remove = array();
1532
			$ignore_ids = array(0);
1533
1534
			// returns an array of ints of id_attach's that should not be deleted
1535
			call_integration_hook('integrate_repair_attachments_nomsg', array(&$ignore_ids, $_GET['substep'], $_GET['substep'] + 500));
1536
1537
			$result = $smcFunc['db_query']('', '
1538
				SELECT a.id_attach, a.id_folder, a.filename, a.file_hash
1539
				FROM {db_prefix}attachments AS a
1540
					LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
1541
				WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
1542
					AND a.id_member = {int:no_member}
1543
					AND (a.id_msg = {int:no_msg} OR m.id_msg IS NULL)
1544
					AND a.id_attach NOT IN ({array_int:ignore_ids})
1545
					AND a.attachment_type IN ({array_int:attach_thumb})',
1546
				array(
1547
					'no_member' => 0,
1548
					'no_msg' => 0,
1549
					'substep' => $_GET['substep'],
1550
					'ignore_ids' => $ignore_ids,
1551
					'attach_thumb' => array(0, 3),
1552
				)
1553
			);
1554
1555
			while ($row = $smcFunc['db_fetch_assoc']($result))
1556
			{
1557
				$to_remove[] = $row['id_attach'];
1558
				$context['repair_errors']['attachment_no_msg']++;
1559
1560
				// If we are repairing remove the file from disk now.
1561
				if ($fix_errors && in_array('attachment_no_msg', $to_fix))
1562
				{
1563
					$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
1564
					@unlink($filename);
1565
				}
1566
			}
1567
			if ($smcFunc['db_num_rows']($result) != 0)
1568
				$to_fix[] = 'attachment_no_msg';
1569
			$smcFunc['db_free_result']($result);
1570
1571
			// Do we need to delete what we have?
1572
			if ($fix_errors && !empty($to_remove) && in_array('attachment_no_msg', $to_fix))
1573
				$smcFunc['db_query']('', '
1574
					DELETE FROM {db_prefix}attachments
1575
					WHERE id_attach IN ({array_int:to_remove})
1576
						AND id_member = {int:no_member}
1577
						AND attachment_type IN ({array_int:attach_thumb})',
1578
					array(
1579
						'to_remove' => $to_remove,
1580
						'no_member' => 0,
1581
						'attach_thumb' => array(0, 3),
1582
					)
1583
				);
1584
1585
			pauseAttachmentMaintenance($to_fix, $thumbnails);
1586
		}
1587
1588
		$_GET['step'] = 5;
1589
		$_GET['substep'] = 0;
1590
		pauseAttachmentMaintenance($to_fix);
1591
	}
1592
1593
	// What about files who are not recorded in the database?
1594
	if ($_GET['step'] <= 5)
1595
	{
1596
		$attach_dirs = $modSettings['attachmentUploadDir'];
1597
1598
		$current_check = 0;
1599
		$max_checks = 500;
1600
		$files_checked = empty($_GET['substep']) ? 0 : $_GET['substep'];
1601
		foreach ($attach_dirs as $attach_dir)
1602
		{
1603
			if ($dir = @opendir($attach_dir))
1604
			{
1605
				while ($file = readdir($dir))
1606
				{
1607
					if (in_array($file, array('.', '..', '.htaccess', 'index.php')))
1608
						continue;
1609
1610
					if ($files_checked <= $current_check)
1611
					{
1612
						// Temporary file, get rid of it!
1613
						if (strpos($file, 'post_tmp_') !== false)
1614
						{
1615
							// Temp file is more than 5 hours old!
1616
							if (filemtime($attach_dir . '/' . $file) < time() - 18000)
1617
								@unlink($attach_dir . '/' . $file);
1618
						}
1619
						// That should be an attachment, let's check if we have it in the database
1620
						elseif (strpos($file, '_') !== false)
1621
						{
1622
							$attachID = (int) substr($file, 0, strpos($file, '_'));
1623
							if (!empty($attachID))
1624
							{
1625
								$request = $smcFunc['db_query']('', '
1626
									SELECT  id_attach
1627
									FROM {db_prefix}attachments
1628
									WHERE id_attach = {int:attachment_id}
1629
									LIMIT 1',
1630
									array(
1631
										'attachment_id' => $attachID,
1632
									)
1633
								);
1634
								if ($smcFunc['db_num_rows']($request) == 0)
1635
								{
1636
									if ($fix_errors && in_array('files_without_attachment', $to_fix))
1637
									{
1638
										@unlink($attach_dir . '/' . $file);
1639
									}
1640
									else
1641
									{
1642
										$context['repair_errors']['files_without_attachment']++;
1643
										$to_fix[] = 'files_without_attachment';
1644
									}
1645
								}
1646
								$smcFunc['db_free_result']($request);
1647
							}
1648
						}
1649
						else
1650
						{
1651
							if ($fix_errors && in_array('files_without_attachment', $to_fix))
1652
							{
1653
								@unlink($attach_dir . '/' . $file);
1654
							}
1655
							else
1656
							{
1657
								$context['repair_errors']['files_without_attachment']++;
1658
								$to_fix[] = 'files_without_attachment';
1659
							}
1660
						}
1661
					}
1662
					$current_check++;
1663
					$_GET['substep'] = $current_check;
1664
					if ($current_check - $files_checked >= $max_checks)
1665
						pauseAttachmentMaintenance($to_fix);
1666
				}
1667
				closedir($dir);
1668
			}
1669
		}
1670
1671
		$_GET['step'] = 5;
1672
		$_GET['substep'] = 0;
1673
		pauseAttachmentMaintenance($to_fix);
1674
	}
1675
1676
	// Got here we must be doing well - just the template! :D
1677
	$context['page_title'] = $txt['repair_attachments'];
1678
	$context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
1679
	$context['sub_template'] = 'attachment_repair';
1680
1681
	// What stage are we at?
1682
	$context['completed'] = $fix_errors ? true : false;
1683
	$context['errors_found'] = !empty($to_fix) ? true : false;
1684
1685
}
1686
1687
/**
1688
 * Function called in-between each round of attachments and avatar repairs.
1689
 * Called by repairAttachments().
1690
 * If repairAttachments() has more steps added, this function needs updated!
1691
 *
1692
 * @param array $to_fix IDs of attachments to fix
1693
 * @param int $max_substep The maximum substep to reach before pausing
1694
 */
1695
function pauseAttachmentMaintenance($to_fix, $max_substep = 0)
1696
{
1697
	global $context, $txt, $time_start;
1698
1699
	// Try get more time...
1700
	@set_time_limit(600);
1701
	if (function_exists('apache_reset_timeout'))
1702
		@apache_reset_timeout();
1703
1704
	// Have we already used our maximum time?
1705
	if ((time() - $time_start) < 3 || $context['starting_substep'] == $_GET['substep'])
1706
		return;
1707
1708
	$context['continue_get_data'] = '?action=admin;area=manageattachments;sa=repair' . (isset($_GET['fixErrors']) ? ';fixErrors' : '') . ';step=' . $_GET['step'] . ';substep=' . $_GET['substep'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1709
	$context['page_title'] = $txt['not_done_title'];
1710
	$context['continue_post_data'] = '';
1711
	$context['continue_countdown'] = '2';
1712
	$context['sub_template'] = 'not_done';
1713
1714
	// Specific stuff to not break this template!
1715
	$context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
1716
1717
	// Change these two if more steps are added!
1718
	if (empty($max_substep))
1719
		$context['continue_percent'] = round(($_GET['step'] * 100) / 25);
1720
	else
1721
		$context['continue_percent'] = round(($_GET['step'] * 100 + ($_GET['substep'] * 100) / $max_substep) / 25);
1722
1723
	// Never more than 100%!
1724
	$context['continue_percent'] = min($context['continue_percent'], 100);
1725
1726
	$_SESSION['attachments_to_fix'] = $to_fix;
1727
	$_SESSION['attachments_to_fix2'] = $context['repair_errors'];
1728
1729
	obExit();
1730
}
1731
1732
/**
1733
 * Called from a mouse click, works out what we want to do with attachments and actions it.
1734
 */
1735
function ApproveAttach()
1736
{
1737
	global $smcFunc;
1738
1739
	// Security is our primary concern...
1740
	checkSession('get');
1741
1742
	// If it approve or delete?
1743
	$is_approve = !isset($_GET['sa']) || $_GET['sa'] != 'reject' ? true : false;
1744
1745
	$attachments = array();
1746
	// If we are approving all ID's in a message , get the ID's.
1747
	if ($_GET['sa'] == 'all' && !empty($_GET['mid']))
1748
	{
1749
		$id_msg = (int) $_GET['mid'];
1750
1751
		$request = $smcFunc['db_query']('', '
1752
			SELECT id_attach
1753
			FROM {db_prefix}attachments
1754
			WHERE id_msg = {int:id_msg}
1755
				AND approved = {int:is_approved}
1756
				AND attachment_type = {int:attachment_type}',
1757
			array(
1758
				'id_msg' => $id_msg,
1759
				'is_approved' => 0,
1760
				'attachment_type' => 0,
1761
			)
1762
		);
1763
		while ($row = $smcFunc['db_fetch_assoc']($request))
1764
			$attachments[] = $row['id_attach'];
1765
		$smcFunc['db_free_result']($request);
1766
	}
1767
	elseif (!empty($_GET['aid']))
1768
		$attachments[] = (int) $_GET['aid'];
1769
1770
	if (empty($attachments))
1771
		fatal_lang_error('no_access', false);
1772
1773
	// Now we have some ID's cleaned and ready to approve, but first - let's check we have permission!
1774
	$allowed_boards = boardsAllowedTo('approve_posts');
1775
1776
	// Validate the attachments exist and are the right approval state.
1777
	$request = $smcFunc['db_query']('', '
1778
		SELECT a.id_attach, m.id_board, m.id_msg, m.id_topic
1779
		FROM {db_prefix}attachments AS a
1780
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
1781
		WHERE a.id_attach IN ({array_int:attachments})
1782
			AND a.attachment_type = {int:attachment_type}
1783
			AND a.approved = {int:is_approved}',
1784
		array(
1785
			'attachments' => $attachments,
1786
			'attachment_type' => 0,
1787
			'is_approved' => 0,
1788
		)
1789
	);
1790
	$attachments = array();
1791
	while ($row = $smcFunc['db_fetch_assoc']($request))
1792
	{
1793
		// We can only add it if we can approve in this board!
1794
		if ($allowed_boards = array(0) || in_array($row['id_board'], $allowed_boards))
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $allowed_boards = (array...rd'], $allowed_boards)), Probably Intended Meaning: ($allowed_boards = array...ard'], $allowed_boards)
Loading history...
1795
		{
1796
			$attachments[] = $row['id_attach'];
1797
1798
			// Also come up with the redirection URL.
1799
			$redirect = 'topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'];
1800
		}
1801
	}
1802
	$smcFunc['db_free_result']($request);
1803
1804
	if (empty($attachments))
1805
		fatal_lang_error('no_access', false);
1806
1807
	// Finally, we are there. Follow through!
1808
	if ($is_approve)
1809
	{
1810
		// Checked and deemed worthy.
1811
		ApproveAttachments($attachments);
1812
	}
1813
	else
1814
		removeAttachments(array('id_attach' => $attachments, 'do_logging' => true));
1815
1816
	// Return to the topic....
1817
	redirectexit($redirect);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $redirect does not seem to be defined for all execution paths leading up to this point.
Loading history...
1818
}
1819
1820
/**
1821
 * Approve an attachment, or maybe even more - no permission check!
1822
 *
1823
 * @param array $attachments The IDs of the attachments to approve
1824
 * @return void|int Returns 0 if the operation failed, otherwise returns nothing
1825
 */
1826
function ApproveAttachments($attachments)
1827
{
1828
	global $smcFunc;
1829
1830
	if (empty($attachments))
1831
		return 0;
1832
1833
	// For safety, check for thumbnails...
1834
	$request = $smcFunc['db_query']('', '
1835
		SELECT
1836
			a.id_attach, a.id_member, COALESCE(thumb.id_attach, 0) AS id_thumb
1837
		FROM {db_prefix}attachments AS a
1838
			LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
1839
		WHERE a.id_attach IN ({array_int:attachments})
1840
			AND a.attachment_type = {int:attachment_type}',
1841
		array(
1842
			'attachments' => $attachments,
1843
			'attachment_type' => 0,
1844
		)
1845
	);
1846
	$attachments = array();
1847
	while ($row = $smcFunc['db_fetch_assoc']($request))
1848
	{
1849
		// Update the thumbnail too...
1850
		if (!empty($row['id_thumb']))
1851
			$attachments[] = $row['id_thumb'];
1852
1853
		$attachments[] = $row['id_attach'];
1854
	}
1855
	$smcFunc['db_free_result']($request);
1856
1857
	if (empty($attachments))
1858
		return 0;
1859
1860
	// Approving an attachment is not hard - it's easy.
1861
	$smcFunc['db_query']('', '
1862
		UPDATE {db_prefix}attachments
1863
		SET approved = {int:is_approved}
1864
		WHERE id_attach IN ({array_int:attachments})',
1865
		array(
1866
			'attachments' => $attachments,
1867
			'is_approved' => 1,
1868
		)
1869
	);
1870
1871
	// In order to log the attachments, we really need their message and filename
1872
	$request = $smcFunc['db_query']('', '
1873
		SELECT m.id_msg, a.filename
1874
		FROM {db_prefix}attachments AS a
1875
			INNER JOIN {db_prefix}messages AS m ON (a.id_msg = m.id_msg)
1876
		WHERE a.id_attach IN ({array_int:attachments})
1877
			AND a.attachment_type = {int:attachment_type}',
1878
		array(
1879
			'attachments' => $attachments,
1880
			'attachment_type' => 0,
1881
		)
1882
	);
1883
1884
	while ($row = $smcFunc['db_fetch_assoc']($request))
1885
		logAction(
1886
			'approve_attach',
1887
			array(
1888
				'message' => $row['id_msg'],
1889
				'filename' => preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($row['filename'])),
1890
			)
1891
		);
1892
	$smcFunc['db_free_result']($request);
1893
1894
	// Remove from the approval queue.
1895
	$smcFunc['db_query']('', '
1896
		DELETE FROM {db_prefix}approval_queue
1897
		WHERE id_attach IN ({array_int:attachments})',
1898
		array(
1899
			'attachments' => $attachments,
1900
		)
1901
	);
1902
1903
	call_integration_hook('integrate_approve_attachments', array($attachments));
1904
}
1905
1906
/**
1907
 * This function lists and allows updating of multiple attachments paths.
1908
 */
1909
function ManageAttachmentPaths()
1910
{
1911
	global $modSettings, $scripturl, $context, $txt, $sourcedir, $boarddir, $smcFunc, $settings;
1912
1913
	// Since this needs to be done eventually.
1914
	if (!isset($modSettings['attachment_basedirectories']))
1915
		$modSettings['attachment_basedirectories'] = array();
1916
1917
	elseif (!is_array($modSettings['attachment_basedirectories']))
1918
		$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
1919
1920
	$errors = array();
1921
1922
	// Saving?
1923
	if (isset($_REQUEST['save']))
1924
	{
1925
		checkSession();
1926
1927
		$_POST['current_dir'] = (int) $_POST['current_dir'];
1928
		$new_dirs = array();
1929
		foreach ($_POST['dirs'] as $id => $path)
1930
		{
1931
			$error = '';
1932
			$id = (int) $id;
1933
			if ($id < 1)
1934
				continue;
1935
1936
			// Sorry, these dirs are NOT valid
1937
			$invalid_dirs = array($boarddir, $settings['default_theme_dir'], $sourcedir);
1938
			if (in_array($path, $invalid_dirs))
1939
			{
1940
				$errors[] = $path . ': ' . $txt['attach_dir_invalid'];
1941
				continue;
1942
			}
1943
1944
			// Hmm, a new path maybe?
1945
			// Don't allow empty paths
1946
			if (!array_key_exists($id, $modSettings['attachmentUploadDir']) && !empty($path))
1947
			{
1948
				// or is it?
1949
				if (in_array($path, $modSettings['attachmentUploadDir']) || in_array($boarddir . DIRECTORY_SEPARATOR . $path, $modSettings['attachmentUploadDir']))
1950
				{
1951
					$errors[] = $path . ': ' . $txt['attach_dir_duplicate_msg'];
1952
					continue;
1953
				}
1954
				elseif (empty($path))
1955
				{
1956
					// Ignore this and set $id to one less
1957
					continue;
1958
				}
1959
1960
				// OK, so let's try to create it then.
1961
				require_once($sourcedir . '/Subs-Attachments.php');
1962
				if (automanage_attachments_create_directory($path))
1963
					$_POST['current_dir'] = $modSettings['currentAttachmentUploadDir'];
1964
				else
1965
					$errors[] = $path . ': ' . $txt[$context['dir_creation_error']];
1966
			}
1967
1968
			// Changing a directory name?
1969
			if (!empty($modSettings['attachmentUploadDir'][$id]) && !empty($path) && $path != $modSettings['attachmentUploadDir'][$id])
1970
			{
1971
				if ($path != $modSettings['attachmentUploadDir'][$id] && !is_dir($path))
1972
				{
1973
					if (!@rename($modSettings['attachmentUploadDir'][$id], $path))
1974
					{
1975
						$errors[] = $path . ': ' . $txt['attach_dir_no_rename'];
1976
						$path = $modSettings['attachmentUploadDir'][$id];
1977
					}
1978
				}
1979
				else
1980
				{
1981
					$errors[] = $path . ': ' . $txt['attach_dir_exists_msg'];
1982
					$path = $modSettings['attachmentUploadDir'][$id];
1983
				}
1984
1985
				// Update the base directory path
1986
				if (!empty($modSettings['attachment_basedirectories']) && array_key_exists($id, $modSettings['attachment_basedirectories']))
1987
				{
1988
					$base = $modSettings['basedirectory_for_attachments'] == $modSettings['attachmentUploadDir'][$id] ? $path : $modSettings['basedirectory_for_attachments'];
1989
1990
					$modSettings['attachment_basedirectories'][$id] = $path;
1991
					updateSettings(array(
1992
						'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
1993
						'basedirectory_for_attachments' => $base,
1994
					));
1995
					$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
1996
				}
1997
			}
1998
1999
			if (empty($path))
2000
			{
2001
				$path = $modSettings['attachmentUploadDir'][$id];
2002
2003
				// It's not a good idea to delete the current directory.
2004
				if ($id == (!empty($_POST['current_dir']) ? $_POST['current_dir'] : $modSettings['currentAttachmentUploadDir']))
2005
					$errors[] = $path . ': ' . $txt['attach_dir_is_current'];
2006
				// Or the current base directory
2007
				elseif (!empty($modSettings['basedirectory_for_attachments']) && $modSettings['basedirectory_for_attachments'] == $modSettings['attachmentUploadDir'][$id])
2008
					$errors[] = $path . ': ' . $txt['attach_dir_is_current_bd'];
2009
				else
2010
				{
2011
					// Let's not try to delete a path with files in it.
2012
					$request = $smcFunc['db_query']('', '
2013
						SELECT COUNT(id_attach) AS num_attach
2014
						FROM {db_prefix}attachments
2015
						WHERE id_folder = {int:id_folder}',
2016
						array(
2017
							'id_folder' => (int) $id,
2018
						)
2019
					);
2020
2021
					list ($num_attach) = $smcFunc['db_fetch_row']($request);
2022
					$smcFunc['db_free_result']($request);
2023
2024
					// A check to see if it's a used base dir.
2025
					if (!empty($modSettings['attachment_basedirectories']))
2026
					{
2027
						// Count any sub-folders.
2028
						foreach ($modSettings['attachmentUploadDir'] as $sub)
2029
							if (strpos($sub, $path . DIRECTORY_SEPARATOR) !== false)
2030
								$num_attach++;
2031
					}
2032
2033
					// It's safe to delete. So try to delete the folder also
2034
					if ($num_attach == 0)
2035
					{
2036
						if (is_dir($path))
2037
							$doit = true;
2038
						elseif (is_dir($boarddir . DIRECTORY_SEPARATOR . $path))
2039
						{
2040
							$doit = true;
2041
							$path = $boarddir . DIRECTORY_SEPARATOR . $path;
2042
						}
2043
2044
						if (isset($doit) && realpath($path) != realpath($boarddir))
2045
						{
2046
							unlink($path . '/.htaccess');
2047
							unlink($path . '/index.php');
2048
							if (!@rmdir($path))
2049
								$error = $path . ': ' . $txt['attach_dir_no_delete'];
2050
						}
2051
2052
						// Remove it from the base directory list.
2053
						if (empty($error) && !empty($modSettings['attachment_basedirectories']))
2054
						{
2055
							unset($modSettings['attachment_basedirectories'][$id]);
2056
							updateSettings(array('attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories'])));
2057
							$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
2058
						}
2059
					}
2060
					else
2061
						$error = $path . ': ' . $txt['attach_dir_no_remove'];
2062
2063
					if (empty($error))
2064
						continue;
2065
					else
2066
						$errors[] = $error;
2067
				}
2068
			}
2069
2070
			$new_dirs[$id] = $path;
2071
		}
2072
2073
		// We need to make sure the current directory is right.
2074
		if (empty($_POST['current_dir']) && !empty($modSettings['currentAttachmentUploadDir']))
2075
			$_POST['current_dir'] = $modSettings['currentAttachmentUploadDir'];
2076
2077
		// Find the current directory if there's no value carried,
2078
		if (empty($_POST['current_dir']) || empty($new_dirs[$_POST['current_dir']]))
2079
		{
2080
			if (array_key_exists($modSettings['currentAttachmentUploadDir'], $modSettings['attachmentUploadDir']))
2081
				$_POST['current_dir'] = $modSettings['currentAttachmentUploadDir'];
2082
			else
2083
				$_POST['current_dir'] = max(array_keys($modSettings['attachmentUploadDir']));
2084
		}
2085
2086
		// If the user wishes to go back, update the last_dir array
2087
		if ($_POST['current_dir'] != $modSettings['currentAttachmentUploadDir'] && !empty($modSettings['last_attachments_directory']) && (isset($modSettings['last_attachments_directory'][$_POST['current_dir']]) || isset($modSettings['last_attachments_directory'][0])))
2088
		{
2089
			if (!is_array($modSettings['last_attachments_directory']))
2090
				$modSettings['last_attachments_directory'] = $smcFunc['json_decode']($modSettings['last_attachments_directory'], true);
2091
			$num = substr(strrchr($modSettings['attachmentUploadDir'][$_POST['current_dir']], '_'), 1);
2092
2093
			if (is_numeric($num))
2094
			{
2095
				// Need to find the base folder.
2096
				$bid = -1;
2097
				$use_subdirectories_for_attachments = 0;
2098
				if (!empty($modSettings['attachment_basedirectories']))
2099
					foreach ($modSettings['attachment_basedirectories'] as $bid => $base)
2100
						if (strpos($modSettings['attachmentUploadDir'][$_POST['current_dir']], $base . DIRECTORY_SEPARATOR) !== false)
2101
						{
2102
							$use_subdirectories_for_attachments = 1;
2103
							break;
2104
						}
2105
2106
				if ($use_subdirectories_for_attachments == 0 && strpos($modSettings['attachmentUploadDir'][$_POST['current_dir']], $boarddir . DIRECTORY_SEPARATOR) !== false)
2107
					$bid = 0;
2108
2109
				$modSettings['last_attachments_directory'][$bid] = (int) $num;
2110
				$modSettings['basedirectory_for_attachments'] = !empty($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : '';
2111
				$modSettings['use_subdirectories_for_attachments'] = !empty($modSettings['use_subdirectories_for_attachments']) ? $modSettings['use_subdirectories_for_attachments'] : 0;
2112
				updateSettings(array(
2113
					'last_attachments_directory' => $smcFunc['json_encode']($modSettings['last_attachments_directory']),
2114
					'basedirectory_for_attachments' => $bid == 0 ? $modSettings['basedirectory_for_attachments'] : $modSettings['attachment_basedirectories'][$bid],
2115
					'use_subdirectories_for_attachments' => $use_subdirectories_for_attachments,
2116
				));
2117
			}
2118
		}
2119
2120
		// Going back to just one path?
2121
		if (count($new_dirs) == 1)
2122
		{
2123
			// We might need to reset the paths. This loop will just loop through once.
2124
			foreach ($new_dirs as $id => $dir)
2125
			{
2126
				if ($id != 1)
2127
					$smcFunc['db_query']('', '
2128
						UPDATE {db_prefix}attachments
2129
						SET id_folder = {int:default_folder}
2130
						WHERE id_folder = {int:current_folder}',
2131
						array(
2132
							'default_folder' => 1,
2133
							'current_folder' => $id,
2134
						)
2135
					);
2136
2137
				$update = array(
2138
					'currentAttachmentUploadDir' => 1,
2139
					'attachmentUploadDir' => $smcFunc['json_encode'](array(1 => $dir)),
2140
				);
2141
			}
2142
		}
2143
		else
2144
		{
2145
			// Save it to the database.
2146
			$update = array(
2147
				'currentAttachmentUploadDir' => $_POST['current_dir'],
2148
				'attachmentUploadDir' => $smcFunc['json_encode']($new_dirs),
2149
			);
2150
		}
2151
2152
		if (!empty($update))
2153
			updateSettings($update);
2154
2155
		if (!empty($errors))
2156
			$_SESSION['errors']['dir'] = $errors;
2157
2158
		redirectexit('action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id']);
2159
	}
2160
2161
	// Saving a base directory?
2162
	if (isset($_REQUEST['save2']))
2163
	{
2164
		checkSession();
2165
2166
		// Changing the current base directory?
2167
		$_POST['current_base_dir'] = isset($_POST['current_base_dir']) ? (int) $_POST['current_base_dir'] : 1;
2168
		if (empty($_POST['new_base_dir']) && !empty($_POST['current_base_dir']))
2169
		{
2170
			if ($modSettings['basedirectory_for_attachments'] != $modSettings['attachmentUploadDir'][$_POST['current_base_dir']])
2171
				$update = (array(
2172
					'basedirectory_for_attachments' => $modSettings['attachmentUploadDir'][$_POST['current_base_dir']],
2173
				));
2174
		}
2175
2176
		if (isset($_POST['base_dir']))
2177
		{
2178
			foreach ($_POST['base_dir'] as $id => $dir)
2179
			{
2180
				if (!empty($dir) && $dir != $modSettings['attachmentUploadDir'][$id])
2181
				{
2182
					if (@rename($modSettings['attachmentUploadDir'][$id], $dir))
2183
					{
2184
						$modSettings['attachmentUploadDir'][$id] = $dir;
2185
						$modSettings['attachment_basedirectories'][$id] = $dir;
2186
						$update = (array(
2187
							'attachmentUploadDir' => $smcFunc['json_encode']($modSettings['attachmentUploadDir']),
2188
							'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
2189
							'basedirectory_for_attachments' => $modSettings['attachmentUploadDir'][$_POST['current_base_dir']],
2190
						));
2191
					}
2192
				}
2193
2194
				if (empty($dir))
2195
				{
2196
					if ($id == $_POST['current_base_dir'])
2197
					{
2198
						$errors[] = $modSettings['attachmentUploadDir'][$id] . ': ' . $txt['attach_dir_is_current'];
2199
						continue;
2200
					}
2201
2202
					unset($modSettings['attachment_basedirectories'][$id]);
2203
					$update = (array(
2204
						'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
2205
						'basedirectory_for_attachments' => $modSettings['attachmentUploadDir'][$_POST['current_base_dir']],
2206
					));
2207
				}
2208
			}
2209
		}
2210
2211
		// Or adding a new one?
2212
		if (!empty($_POST['new_base_dir']))
2213
		{
2214
			require_once($sourcedir . '/Subs-Attachments.php');
2215
			$_POST['new_base_dir'] = $smcFunc['htmlspecialchars']($_POST['new_base_dir'], ENT_QUOTES);
2216
2217
			$current_dir = $modSettings['currentAttachmentUploadDir'];
2218
2219
			if (!in_array($_POST['new_base_dir'], $modSettings['attachmentUploadDir']))
2220
			{
2221
				if (!automanage_attachments_create_directory($_POST['new_base_dir']))
2222
					$errors[] = $_POST['new_base_dir'] . ': ' . $txt['attach_dir_base_no_create'];
2223
			}
2224
2225
			$modSettings['currentAttachmentUploadDir'] = array_search($_POST['new_base_dir'], $modSettings['attachmentUploadDir']);
2226
			if (!in_array($_POST['new_base_dir'], $modSettings['attachment_basedirectories']))
2227
				$modSettings['attachment_basedirectories'][$modSettings['currentAttachmentUploadDir']] = $_POST['new_base_dir'];
2228
			ksort($modSettings['attachment_basedirectories']);
2229
2230
			$update = (array(
2231
				'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
2232
				'basedirectory_for_attachments' => $_POST['new_base_dir'],
2233
				'currentAttachmentUploadDir' => $current_dir,
2234
			));
2235
		}
2236
2237
		if (!empty($errors))
2238
			$_SESSION['errors']['base'] = $errors;
2239
2240
		if (!empty($update))
2241
			updateSettings($update);
2242
2243
		redirectexit('action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id']);
2244
	}
2245
2246
	if (isset($_SESSION['errors']))
2247
	{
2248
		if (is_array($_SESSION['errors']))
2249
		{
2250
			$errors = array();
2251
			if (!empty($_SESSION['errors']['dir']))
2252
				foreach ($_SESSION['errors']['dir'] as $error)
2253
					$errors['dir'][] = $smcFunc['htmlspecialchars']($error, ENT_QUOTES);
2254
2255
			if (!empty($_SESSION['errors']['base']))
2256
				foreach ($_SESSION['errors']['base'] as $error)
2257
					$errors['base'][] = $smcFunc['htmlspecialchars']($error, ENT_QUOTES);
2258
		}
2259
		unset($_SESSION['errors']);
2260
	}
2261
2262
	$listOptions = array(
2263
		'id' => 'attach_paths',
2264
		'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
2265
		'title' => $txt['attach_paths'],
2266
		'get_items' => array(
2267
			'function' => 'list_getAttachDirs',
2268
		),
2269
		'columns' => array(
2270
			'current_dir' => array(
2271
				'header' => array(
2272
					'value' => $txt['attach_current'],
2273
					'class' => 'centercol',
2274
				),
2275
				'data' => array(
2276
					'function' => function($rowData)
2277
					{
2278
						return '<input type="radio" name="current_dir" value="' . $rowData['id'] . '"' . ($rowData['current'] ? ' checked' : '') . (!empty($rowData['disable_current']) ? ' disabled' : '') . '>';
2279
					},
2280
					'style' => 'width: 10%;',
2281
					'class' => 'centercol',
2282
				),
2283
			),
2284
			'path' => array(
2285
				'header' => array(
2286
					'value' => $txt['attach_path'],
2287
				),
2288
				'data' => array(
2289
					'function' => function($rowData)
2290
					{
2291
						return '<input type="hidden" name="dirs[' . $rowData['id'] . ']" value="' . $rowData['path'] . '"><input type="text" size="40" name="dirs[' . $rowData['id'] . ']" value="' . $rowData['path'] . '"' . (!empty($rowData['disable_base_dir']) ? ' disabled' : '') . ' style="width: 100%">';
2292
					},
2293
					'style' => 'width: 40%;',
2294
				),
2295
			),
2296
			'current_size' => array(
2297
				'header' => array(
2298
					'value' => $txt['attach_current_size'],
2299
				),
2300
				'data' => array(
2301
					'db' => 'current_size',
2302
					'style' => 'width: 15%;',
2303
				),
2304
			),
2305
			'num_files' => array(
2306
				'header' => array(
2307
					'value' => $txt['attach_num_files'],
2308
				),
2309
				'data' => array(
2310
					'db' => 'num_files',
2311
					'style' => 'width: 15%;',
2312
				),
2313
			),
2314
			'status' => array(
2315
				'header' => array(
2316
					'value' => $txt['attach_dir_status'],
2317
					'class' => 'centercol',
2318
				),
2319
				'data' => array(
2320
					'db' => 'status',
2321
					'style' => 'width: 25%;',
2322
					'class' => 'centercol',
2323
				),
2324
			),
2325
		),
2326
		'form' => array(
2327
			'href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
2328
		),
2329
		'additional_rows' => array(
2330
			array(
2331
				'position' => 'below_table_data',
2332
				'value' => '
2333
				<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '">
2334
				<input type="submit" name="save" value="' . $txt['save'] . '" class="button">
2335
				<input type="submit" name="new_path" value="' . $txt['attach_add_path'] . '" class="button">',
2336
			),
2337
			empty($errors['dir']) ? array(
2338
				'position' => 'top_of_list',
2339
				'value' => $txt['attach_dir_desc'],
2340
				'class' => 'information'
2341
			) : array(
2342
				'position' => 'top_of_list',
2343
				'value' => $txt['attach_dir_save_problem'] . '<br>' . implode('<br>', $errors['dir']),
2344
				'style' => 'padding-left: 35px;',
2345
				'class' => 'noticebox',
2346
			),
2347
		),
2348
	);
2349
	require_once($sourcedir . '/Subs-List.php');
2350
	createList($listOptions);
2351
2352
	if (!empty($modSettings['attachment_basedirectories']))
2353
	{
2354
		$listOptions2 = array(
2355
			'id' => 'base_paths',
2356
			'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
2357
			'title' => $txt['attach_base_paths'],
2358
			'get_items' => array(
2359
				'function' => 'list_getBaseDirs',
2360
			),
2361
			'columns' => array(
2362
				'current_dir' => array(
2363
					'header' => array(
2364
						'value' => $txt['attach_current'],
2365
						'class' => 'centercol',
2366
					),
2367
					'data' => array(
2368
						'function' => function($rowData)
2369
						{
2370
							return '<input type="radio" name="current_base_dir" value="' . $rowData['id'] . '"' . ($rowData['current'] ? ' checked' : '') . '>';
2371
						},
2372
						'style' => 'width: 10%;',
2373
						'class' => 'centercol',
2374
					),
2375
				),
2376
				'path' => array(
2377
					'header' => array(
2378
						'value' => $txt['attach_path'],
2379
					),
2380
					'data' => array(
2381
						'db' => 'path',
2382
						'style' => 'width: 45%;',
2383
					),
2384
				),
2385
				'num_dirs' => array(
2386
					'header' => array(
2387
						'value' => $txt['attach_num_dirs'],
2388
					),
2389
					'data' => array(
2390
						'db' => 'num_dirs',
2391
						'style' => 'width: 15%;',
2392
					),
2393
				),
2394
				'status' => array(
2395
					'header' => array(
2396
						'value' => $txt['attach_dir_status'],
2397
					),
2398
					'data' => array(
2399
						'db' => 'status',
2400
						'style' => 'width: 15%;',
2401
						'class' => 'centercol',
2402
					),
2403
				),
2404
			),
2405
			'form' => array(
2406
				'href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
2407
			),
2408
			'additional_rows' => array(
2409
				array(
2410
					'position' => 'below_table_data',
2411
					'value' => '<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '"><input type="submit" name="save2" value="' . $txt['save'] . '" class="button">
2412
					<input type="submit" name="new_base_path" value="' . $txt['attach_add_path'] . '" class="button">',
2413
				),
2414
				empty($errors['base']) ? array(
2415
					'position' => 'top_of_list',
2416
					'value' => $txt['attach_dir_base_desc'],
2417
					'style' => 'padding: 5px 10px;',
2418
					'class' => 'windowbg smalltext'
2419
				) : array(
2420
					'position' => 'top_of_list',
2421
					'value' => $txt['attach_dir_save_problem'] . '<br>' . implode('<br>', $errors['base']),
2422
					'style' => 'padding-left: 35px',
2423
					'class' => 'noticebox',
2424
				),
2425
			),
2426
		);
2427
		createList($listOptions2);
2428
	}
2429
2430
	// Fix up our template.
2431
	$context[$context['admin_menu_name']]['current_subsection'] = 'attachpaths';
2432
	$context['page_title'] = $txt['attach_path_manage'];
2433
	$context['sub_template'] = 'attachment_paths';
2434
}
2435
2436
/**
2437
 * Prepare the actual attachment directories to be displayed in the list.
2438
 *
2439
 * @return array An array of information about the attachment directories
2440
 */
2441
function list_getAttachDirs()
2442
{
2443
	global $smcFunc, $modSettings, $context, $txt;
2444
2445
	$request = $smcFunc['db_query']('', '
2446
		SELECT id_folder, COUNT(id_attach) AS num_attach, SUM(size) AS size_attach
2447
		FROM {db_prefix}attachments
2448
		WHERE attachment_type != {int:type}
2449
		GROUP BY id_folder',
2450
		array(
2451
			'type' => 1,
2452
		)
2453
	);
2454
2455
	$expected_files = array();
2456
	$expected_size = array();
2457
	while ($row = $smcFunc['db_fetch_assoc']($request))
2458
	{
2459
		$expected_files[$row['id_folder']] = $row['num_attach'];
2460
		$expected_size[$row['id_folder']] = $row['size_attach'];
2461
	}
2462
	$smcFunc['db_free_result']($request);
2463
2464
	$attachdirs = array();
2465
	foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
2466
	{
2467
		// If there aren't any attachments in this directory this won't exist.
2468
		if (!isset($expected_files[$id]))
2469
			$expected_files[$id] = 0;
2470
2471
		// Check if the directory is doing okay.
2472
		list ($status, $error, $files) = attachDirStatus($dir, $expected_files[$id]);
2473
2474
		// If it is one, let's show that it's a base directory.
2475
		$sub_dirs = 0;
2476
		$is_base_dir = false;
2477
		if (!empty($modSettings['attachment_basedirectories']))
2478
		{
2479
			$is_base_dir = in_array($dir, $modSettings['attachment_basedirectories']);
2480
2481
			// Count any sub-folders.
2482
			foreach ($modSettings['attachmentUploadDir'] as $sid => $sub)
2483
				if (strpos($sub, $dir . DIRECTORY_SEPARATOR) !== false)
2484
				{
2485
					$expected_files[$id]++;
2486
					$sub_dirs++;
2487
				}
2488
		}
2489
2490
		$attachdirs[] = array(
2491
			'id' => $id,
2492
			'current' => $id == $modSettings['currentAttachmentUploadDir'],
2493
			'disable_current' => isset($modSettings['automanage_attachments']) && $modSettings['automanage_attachments'] > 1,
2494
			'disable_base_dir' => $is_base_dir && $sub_dirs > 0 && !empty($files) && empty($error) && empty($save_errors),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $save_errors seems to never exist and therefore empty should always be true.
Loading history...
2495
			'path' => $dir,
2496
			'current_size' => !empty($expected_size[$id]) ? comma_format($expected_size[$id] / 1024, 0) : 0,
2497
			'num_files' => comma_format($expected_files[$id] - $sub_dirs, 0) . ($sub_dirs > 0 ? ' (' . $sub_dirs . ')' : ''),
2498
			'status' => ($is_base_dir ? $txt['attach_dir_basedir'] . '<br>' : '') . ($error ? '<div class="error">' : '') . sprintf($txt['attach_dir_' . $status], $context['session_id'], $context['session_var']) . ($error ? '</div>' : ''),
2499
		);
2500
	}
2501
2502
	// Just stick a new directory on at the bottom.
2503
	if (isset($_REQUEST['new_path']))
2504
		$attachdirs[] = array(
2505
			'id' => max(array_merge(array_keys($expected_files), array_keys($modSettings['attachmentUploadDir']))) + 1,
2506
			'current' => false,
2507
			'path' => '',
2508
			'current_size' => '',
2509
			'num_files' => '',
2510
			'status' => '',
2511
		);
2512
2513
	return $attachdirs;
2514
}
2515
2516
/**
2517
 * Prepare the base directories to be displayed in a list.
2518
 *
2519
 * @return void|array Returns nothing if there are no base directories, otherwise returns an array of info about the directories
2520
 */
2521
function list_getBaseDirs()
2522
{
2523
	global $modSettings, $txt;
2524
2525
	if (empty($modSettings['attachment_basedirectories']))
2526
		return;
2527
2528
	$basedirs = array();
2529
	// Get a list of the base directories.
2530
	foreach ($modSettings['attachment_basedirectories'] as $id => $dir)
2531
	{
2532
		// Loop through the attach directory array to count any sub-directories
2533
		$expected_dirs = 0;
2534
		foreach ($modSettings['attachmentUploadDir'] as $sid => $sub)
2535
			if (strpos($sub, $dir . DIRECTORY_SEPARATOR) !== false)
2536
				$expected_dirs++;
2537
2538
		if (!is_dir($dir))
2539
			$status = 'does_not_exist';
2540
		elseif (!is_writeable($dir))
2541
			$status = 'not_writable';
2542
		else
2543
			$status = 'ok';
2544
2545
		$basedirs[] = array(
2546
			'id' => $id,
2547
			'current' => $dir == $modSettings['basedirectory_for_attachments'],
2548
			'path' => $expected_dirs > 0 ? $dir : ('<input type="text" name="base_dir[' . $id . ']" value="' . $dir . '" size="40">'),
2549
			'num_dirs' => $expected_dirs,
2550
			'status' => $status == 'ok' ? $txt['attach_dir_ok'] : ('<span class="error">' . $txt['attach_dir_' . $status] . '</span>'),
2551
		);
2552
	}
2553
2554
	if (isset($_REQUEST['new_base_path']))
2555
		$basedirs[] = array(
2556
			'id' => '',
2557
			'current' => false,
2558
			'path' => '<input type="text" name="new_base_dir" value="" size="40">',
2559
			'num_dirs' => '',
2560
			'status' => '',
2561
		);
2562
2563
	return $basedirs;
2564
}
2565
2566
/**
2567
 * Checks the status of an attachment directory and returns an array
2568
 *  of the status key, if that status key signifies an error, and
2569
 *  the file count.
2570
 *
2571
 * @param string $dir The directory to check
2572
 * @param int $expected_files How many files should be in that directory
2573
 * @return array An array containing the status of the directory, whether the number of files was what we expected and how many were in the directory
2574
 */
2575
function attachDirStatus($dir, $expected_files)
2576
{
2577
	if (!is_dir($dir))
2578
		return array('does_not_exist', true, '');
2579
	elseif (!is_writable($dir))
2580
		return array('not_writable', true, '');
2581
2582
	// Everything is okay so far, start to scan through the directory.
2583
	$num_files = 0;
2584
	$dir_handle = dir($dir);
2585
	while ($file = $dir_handle->read())
2586
	{
2587
		// Now do we have a real file here?
2588
		if (in_array($file, array('.', '..', '.htaccess', 'index.php')))
2589
			continue;
2590
2591
		$num_files++;
2592
	}
2593
	$dir_handle->close();
2594
2595
	if ($num_files < $expected_files)
2596
		return array('files_missing', true, $num_files);
2597
	// Empty?
2598
	elseif ($expected_files == 0)
2599
		return array('unused', false, $num_files);
2600
	// All good!
2601
	else
2602
		return array('ok', false, $num_files);
2603
}
2604
2605
/**
2606
 * Maintance function to move attachments from one directory to another
2607
 */
2608
function TransferAttachments()
2609
{
2610
	global $modSettings, $smcFunc, $sourcedir, $txt, $boarddir;
2611
2612
	checkSession();
2613
2614
	if (!empty($modSettings['attachment_basedirectories']))
2615
		$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
2616
	else
2617
		$modSettings['basedirectory_for_attachments'] = array();
2618
2619
	$_POST['from'] = (int) $_POST['from'];
2620
	$_POST['auto'] = !empty($_POST['auto']) ? (int) $_POST['auto'] : 0;
2621
	$_POST['to'] = (int) $_POST['to'];
2622
	$start = !empty($_POST['empty_it']) ? 0 : $modSettings['attachmentDirFileLimit'];
2623
	$_SESSION['checked'] = !empty($_POST['empty_it']) ? true : false;
2624
	$limit = 501;
2625
	$results = array();
2626
	$dir_files = 0;
2627
	$current_progress = 0;
2628
	$total_moved = 0;
2629
	$total_not_moved = 0;
2630
2631
	if (empty($_POST['from']) || (empty($_POST['auto']) && empty($_POST['to'])))
2632
		$results[] = $txt['attachment_transfer_no_dir'];
2633
2634
	if ($_POST['from'] == $_POST['to'])
2635
		$results[] = $txt['attachment_transfer_same_dir'];
2636
2637
	if (empty($results))
2638
	{
2639
		// Get the total file count for the progess bar.
2640
		$request = $smcFunc['db_query']('', '
2641
			SELECT COUNT(*)
2642
			FROM {db_prefix}attachments
2643
			WHERE id_folder = {int:folder_id}
2644
				AND attachment_type != {int:attachment_type}',
2645
			array(
2646
				'folder_id' => $_POST['from'],
2647
				'attachment_type' => 1,
2648
			)
2649
		);
2650
		list ($total_progress) = $smcFunc['db_fetch_row']($request);
2651
		$smcFunc['db_free_result']($request);
2652
		$total_progress -= $start;
2653
2654
		if ($total_progress < 1)
2655
			$results[] = $txt['attachment_transfer_no_find'];
2656
	}
2657
2658
	if (empty($results))
2659
	{
2660
		// Where are they going?
2661
		if (!empty($_POST['auto']))
2662
		{
2663
			require_once($sourcedir . '/Subs-Attachments.php');
2664
2665
			$modSettings['automanage_attachments'] = 1;
2666
			$modSettings['use_subdirectories_for_attachments'] = $_POST['auto'] == -1 ? 0 : 1;
2667
			$modSettings['basedirectory_for_attachments'] = $_POST['auto'] > 0 ? $modSettings['attachmentUploadDir'][$_POST['auto']] : $modSettings['basedirectory_for_attachments'];
2668
2669
			automanage_attachments_check_directory();
2670
			$new_dir = $modSettings['currentAttachmentUploadDir'];
2671
		}
2672
		else
2673
			$new_dir = $_POST['to'];
2674
2675
		$modSettings['currentAttachmentUploadDir'] = $new_dir;
2676
2677
		$break = false;
2678
		while ($break == false)
2679
		{
2680
			@set_time_limit(300);
2681
			if (function_exists('apache_reset_timeout'))
2682
				@apache_reset_timeout();
2683
2684
			// If limits are set, get the file count and size for the destination folder
2685
			if ($dir_files <= 0 && (!empty($modSettings['attachmentDirSizeLimit']) || !empty($modSettings['attachmentDirFileLimit'])))
2686
			{
2687
				$request = $smcFunc['db_query']('', '
2688
					SELECT COUNT(*), SUM(size)
2689
					FROM {db_prefix}attachments
2690
					WHERE id_folder = {int:folder_id}
2691
						AND attachment_type != {int:attachment_type}',
2692
					array(
2693
						'folder_id' => $new_dir,
2694
						'attachment_type' => 1,
2695
					)
2696
				);
2697
				list ($dir_files, $dir_size) = $smcFunc['db_fetch_row']($request);
2698
				$smcFunc['db_free_result']($request);
2699
			}
2700
2701
			// Find some attachments to move
2702
			$request = $smcFunc['db_query']('', '
2703
				SELECT id_attach, filename, id_folder, file_hash, size
2704
				FROM {db_prefix}attachments
2705
				WHERE id_folder = {int:folder}
2706
					AND attachment_type != {int:attachment_type}
2707
				LIMIT {int:start}, {int:limit}',
2708
				array(
2709
					'folder' => $_POST['from'],
2710
					'attachment_type' => 1,
2711
					'start' => $start,
2712
					'limit' => $limit,
2713
				)
2714
			);
2715
2716
			if ($smcFunc['db_num_rows']($request) === 0)
2717
			{
2718
				if (empty($current_progress))
2719
					$results[] = $txt['attachment_transfer_no_find'];
2720
				break;
2721
			}
2722
2723
			if ($smcFunc['db_num_rows']($request) < $limit)
2724
				$break = true;
2725
2726
			// Move them
2727
			$moved = array();
2728
			while ($row = $smcFunc['db_fetch_assoc']($request))
2729
			{
2730
				$source = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
2731
				$dest = $modSettings['attachmentUploadDir'][$new_dir] . '/' . basename($source);
2732
2733
				// Size and file count check
2734
				if (!empty($modSettings['attachmentDirSizeLimit']) || !empty($modSettings['attachmentDirFileLimit']))
2735
				{
2736
					$dir_files++;
2737
					$dir_size += !empty($row['size']) ? $row['size'] : filesize($source);
2738
2739
					// If we've reached a limit. Do something.
2740
					if (!empty($modSettings['attachmentDirSizeLimit']) && $dir_size > $modSettings['attachmentDirSizeLimit'] * 1024 || (!empty($modSettings['attachmentDirFileLimit']) && $dir_files > $modSettings['attachmentDirFileLimit']))
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! empty($modSettings['a...ttachmentDirFileLimit'], Probably Intended Meaning: ! empty($modSettings['at...tachmentDirFileLimit'])
Loading history...
2741
					{
2742
						if (!empty($_POST['auto']))
2743
						{
2744
							// Since we're in auto mode. Create a new folder and reset the counters.
2745
							automanage_attachments_by_space();
2746
2747
							$results[] = sprintf($txt['attachments_transferred'], $total_moved, $modSettings['attachmentUploadDir'][$new_dir]);
2748
							if (!empty($total_not_moved))
2749
								$results[] = sprintf($txt['attachments_not_transferred'], $total_not_moved);
2750
2751
							$dir_files = 0;
2752
							$total_moved = 0;
2753
							$total_not_moved = 0;
2754
2755
							$break = false;
2756
							break;
2757
						}
2758
						else
2759
						{
2760
							// Hmm, not in auto. Time to bail out then...
2761
							$results[] = $txt['attachment_transfer_no_room'];
2762
							$break = true;
2763
							break;
2764
						}
2765
					}
2766
				}
2767
2768
				if (@rename($source, $dest))
2769
				{
2770
					$total_moved++;
2771
					$current_progress++;
2772
					$moved[] = $row['id_attach'];
2773
				}
2774
				else
2775
					$total_not_moved++;
2776
			}
2777
			$smcFunc['db_free_result']($request);
2778
2779
			if (!empty($moved))
2780
			{
2781
				// Update the database
2782
				$smcFunc['db_query']('', '
2783
					UPDATE {db_prefix}attachments
2784
					SET id_folder = {int:new}
2785
					WHERE id_attach IN ({array_int:attachments})',
2786
					array(
2787
						'attachments' => $moved,
2788
						'new' => $new_dir,
2789
					)
2790
				);
2791
			}
2792
2793
			$new_dir = $modSettings['currentAttachmentUploadDir'];
2794
2795
			// Create the progress bar.
2796
			if (!$break)
2797
			{
2798
				$percent_done = min(round($current_progress / $total_progress * 100, 0), 100);
2799
				$prog_bar = '
2800
					<div class="progress_bar">
2801
						<div class="bar" style="width: ' . $percent_done . '%;"></div>
2802
						<span>' . $percent_done . '%</span>
2803
					</div>';
2804
				// Write it to a file so it can be displayed
2805
				$fp = fopen($boarddir . '/progress.php', "w");
2806
				fwrite($fp, $prog_bar);
2807
				fclose($fp);
2808
				usleep(500000);
2809
			}
2810
		}
2811
2812
		$results[] = sprintf($txt['attachments_transferred'], $total_moved, $modSettings['attachmentUploadDir'][$new_dir]);
2813
		if (!empty($total_not_moved))
2814
			$results[] = sprintf($txt['attachments_not_transferred'], $total_not_moved);
2815
	}
2816
2817
	$_SESSION['results'] = $results;
2818
	if (file_exists($boarddir . '/progress.php'))
2819
		unlink($boarddir . '/progress.php');
2820
2821
	redirectexit('action=admin;area=manageattachments;sa=maintenance#transfer');
2822
}
2823
2824
?>