Issues (1061)

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