Issues (1065)

Sources/ManageAttachments.php (1 issue)

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