Issues (1014)

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