/sources/admin/ManageAttachments.php
PHP | 2433 lines | 1859 code | 283 blank | 291 comment | 310 complexity | c1292f73b09bb0efa1251e47c4fc5b5b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * @name ElkArte Forum
- * @copyright ElkArte Forum contributors
- * @license BSD http://opensource.org/licenses/BSD-3-Clause
- *
- * This software is a derived product, based on:
- *
- * Simple Machines Forum (SMF)
- * copyright: 2011 Simple Machines (http://www.simplemachines.org)
- * license: BSD, See included LICENSE.TXT for terms and conditions.
- *
- * @version 1.0 Alpha
- *
- * This file doing the job of attachments and avatars maintenance and management.
- * @todo refactor as controller-model
- *
- */
- if (!defined('ELKARTE'))
- die('No access...');
- /**
- * The main 'Attachments and Avatars' management function.
- * This function is the entry point for index.php?action=admin;area=manageattachments
- * and it calls a function based on the sub-action.
- * It requires the manage_attachments permission.
- *
- * @uses ManageAttachments template.
- * @uses Admin language file.
- * @uses template layer 'manage_files' for showing the tab bar.
- *
- */
- function ManageAttachments()
- {
- global $txt, $modSettings, $scripturl, $context, $options;
- // You have to be able to moderate the forum to do this.
- isAllowedTo('manage_attachments');
- // Setup the template stuff we'll probably need.
- loadTemplate('ManageAttachments');
- // If they want to delete attachment(s), delete them. (otherwise fall through..)
- $subActions = array(
- 'attachments' => 'action_attachments',
- 'attachpaths' => 'action_attachpaths',
- 'avatars' => 'action_avatars',
- 'browse' => 'action_browse',
- 'byAge' => 'action_byAge',
- 'bySize' => 'action_bySize',
- 'maintenance' => 'action_maintenance',
- 'moveAvatars' => 'action_moveAvatars',
- 'repair' => 'action_repair',
- 'remove' => 'action_remove',
- 'removeall' => 'action_removeall',
- 'transfer' => 'action_transfer',
- );
- call_integration_hook('integrate_manage_attachments', array($subActions));
- // Pick the correct sub-action.
- if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
- $context['sub_action'] = $_REQUEST['sa'];
- else
- $context['sub_action'] = 'browse';
- // Default page title is good.
- $context['page_title'] = $txt['attachments_avatars'];
- // This uses admin tabs - as it should!
- $context[$context['admin_menu_name']]['tab_data'] = array(
- 'title' => $txt['attachments_avatars'],
- 'help' => 'manage_files',
- 'description' => $txt['attachments_desc'],
- );
- // Finally fall through to what we are doing.
- $subActions[$context['sub_action']]();
- }
- /**
- * Allows to show/change attachment settings.
- * This is the default sub-action of the 'Attachments and Avatars' center.
- * Called by index.php?action=admin;area=manageattachments;sa=attachments.
- *
- * @param bool $return_config = false
- * @uses 'attachments' sub template.
- */
- function action_attachments($return_config = false)
- {
- global $txt, $modSettings, $scripturl, $context, $options;
- require_once(SUBSDIR . '/Attachments.subs.php');
- // Get the current attachment directory.
- $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
- $context['attachmentUploadDir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
- // First time here?
- if (empty($modSettings['attachment_basedirectories']) && $modSettings['currentAttachmentUploadDir'] == 1 && count($modSettings['attachmentUploadDir']) == 1)
- $modSettings['attachmentUploadDir'] = $modSettings['attachmentUploadDir'][1];
- // If not set, show a default path for the base directory
- if (!isset($_GET['save']) && empty($modSettings['basedirectory_for_attachments']))
- if (is_dir($modSettings['attachmentUploadDir'][1]))
- $modSettings['basedirectory_for_attachments'] = $modSettings['attachmentUploadDir'][1];
- else
- $modSettings['basedirectory_for_attachments'] = $context['attachmentUploadDir'];
- $context['valid_upload_dir'] = is_dir($context['attachmentUploadDir']) && is_writable($context['attachmentUploadDir']);
- if (!empty($modSettings['automanage_attachments']))
- $context['valid_basedirectory'] = !empty($modSettings['basedirectory_for_attachments']) && is_writable($modSettings['basedirectory_for_attachments']);
- else
- $context['valid_basedirectory'] = true;
- // A bit of razzle dazzle with the $txt strings. :)
- $txt['attachment_path'] = $context['attachmentUploadDir'];
- $txt['basedirectory_for_attachments_path']= isset($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : '';
- $txt['use_subdirectories_for_attachments_note'] = empty($modSettings['attachment_basedirectories']) || empty($modSettings['use_subdirectories_for_attachments']) ? $txt['use_subdirectories_for_attachments_note'] : '';
- $txt['attachmentUploadDir_multiple_configure'] = '<a href="' . $scripturl . '?action=admin;area=manageattachments;sa=attachpaths">[' . $txt['attachmentUploadDir_multiple_configure'] . ']</a>';
- $txt['attach_current_dir'] = empty($modSettings['automanage_attachments']) ? $txt['attach_current_dir'] : $txt['attach_last_dir'];
- $txt['attach_current_dir_warning'] = $txt['attach_current_dir'] . $txt['attach_current_dir_warning'];
- $txt['basedirectory_for_attachments_warning'] = $txt['basedirectory_for_attachments_current'] . $txt['basedirectory_for_attachments_warning'];
- // Perform a test to see if the GD module or ImageMagick are installed.
- $testImg = get_extension_funcs('gd') || class_exists('Imagick');
- // See if we can find if the server is set up to support the attacment limits
- $post_max_size = ini_get('post_max_size');
- $upload_max_filesize = ini_get('upload_max_filesize');
- $testPM = !empty($post_max_size) ? (memoryReturnBytes($post_max_size) >= (isset($modSettings['attachmentPostLimit']) ? $modSettings['attachmentPostLimit'] * 1024 : 0)) : true;
- $testUM = !empty($upload_max_filesize) ? (memoryReturnBytes($upload_max_filesize) >= (isset($modSettings['attachmentSizeLimit']) ? $modSettings['attachmentSizeLimit'] * 1024 : 0)) : true;
- $config_vars = array(
- array('title', 'attachment_manager_settings'),
- // Are attachments enabled?
- array('select', 'attachmentEnable', array($txt['attachmentEnable_deactivate'], $txt['attachmentEnable_enable_all'], $txt['attachmentEnable_disable_new'])),
- '',
- // Extension checks etc.
- array('check', 'attachmentRecodeLineEndings'),
- '',
- // Directory and size limits.
- 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'])),
- array('check', 'use_subdirectories_for_attachments', 'subtext' => $txt['use_subdirectories_for_attachments_note']),
- (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']))),
- empty($modSettings['attachment_basedirectories']) && $modSettings['currentAttachmentUploadDir'] == 1 && count($modSettings['attachmentUploadDir']) == 1 ? array('text', 'attachmentUploadDir', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 40, 'invalid' => !$context['valid_upload_dir']) : 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'])),
- array('int', 'attachmentDirFileLimit', 'subtext' => $txt['zero_for_no_limit'], 6),
- array('int', 'attachmentDirSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']),
- '',
- // Posting limits
- array('int', 'attachmentPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']),
- array('warning', empty($testPM) ? 'attachment_postsize_warning' : ''),
- array('int', 'attachmentSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']),
- array('warning', empty($testUM) ? 'attachment_filesize_warning' : ''),
- array('int', 'attachmentNumPerPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6),
- // Security Items
- array('title', 'attachment_security_settings'),
- // Extension checks etc.
- array('check', 'attachmentCheckExtensions'),
- array('text', 'attachmentExtensions', 40),
- '',
- // Image checks.
- array('warning', empty($testImg) ? 'attachment_img_enc_warning' : ''),
- array('check', 'attachment_image_reencode'),
- '',
- array('warning', 'attachment_image_paranoid_warning'),
- array('check', 'attachment_image_paranoid'),
- // Thumbnail settings.
- array('title', 'attachment_thumbnail_settings'),
- array('check', 'attachmentShowImages'),
- array('check', 'attachmentThumbnails'),
- array('check', 'attachment_thumb_png'),
- array('check', 'attachment_thumb_memory', 'subtext' => $txt['attachment_thumb_memory_note1'], 'postinput' => $txt['attachment_thumb_memory_note2']),
- array('warning', 'attachment_thumb_memory_note'),
- array('text', 'attachmentThumbWidth', 6),
- array('text', 'attachmentThumbHeight', 6),
- '',
- array('int', 'max_image_width', 'subtext' => $txt['zero_for_no_limit']),
- array('int', 'max_image_height', 'subtext' => $txt['zero_for_no_limit']),
- );
- $context['settings_post_javascript'] = '
- var storing_type = document.getElementById(\'automanage_attachments\');
- var base_dir = document.getElementById(\'use_subdirectories_for_attachments\');
- createEventListener(storing_type)
- storing_type.addEventListener("change", toggleSubDir, false);
- createEventListener(base_dir)
- base_dir.addEventListener("change", toggleSubDir, false);
- toggleSubDir();';
- call_integration_hook('integrate_modify_attachment_settings', array($config_vars));
- if ($return_config)
- return $config_vars;
- // These are very likely to come in handy! (i.e. without them we're doomed!)
- require_once(ADMINDIR . '/ManagePermissions.php');
- require_once(ADMINDIR . '/ManageServer.php');
- // Saving settings?
- if (isset($_GET['save']))
- {
- checkSession();
- if (isset($_POST['attachmentUploadDir']))
- {
- if (!empty($_POST['attachmentUploadDir']) && $modSettings['attachmentUploadDir'] != $_POST['attachmentUploadDir'])
- rename($modSettings['attachmentUploadDir'], $_POST['attachmentUploadDir']);
- $modSettings['attachmentUploadDir'] = array(1 => $_POST['attachmentUploadDir']);
- $_POST['attachmentUploadDir'] = serialize($modSettings['attachmentUploadDir']);
- }
- if (!empty($_POST['use_subdirectories_for_attachments']))
- {
- if (isset($_POST['use_subdirectories_for_attachments']) && empty($_POST['basedirectory_for_attachments']))
- $_POST['basedirectory_for_attachments'] = (!empty($modSettings['basedirectory_for_attachments']) ? ($modSettings['basedirectory_for_attachments']) : BOARDDIR);
- if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($modSettings['attachment_basedirectories']))
- {
- if (!is_array($modSettings['attachment_basedirectories']))
- $modSettings['attachment_basedirectories'] = unserialize($modSettings['attachment_basedirectories']);
- }
- else
- $modSettings['attachment_basedirectories'] = array();
- if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($_POST['basedirectory_for_attachments']) && !in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']))
- {
- $currentAttachmentUploadDir = $modSettings['currentAttachmentUploadDir'];
- if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachmentUploadDir']))
- {
- if (!automanage_attachments_create_directory($_POST['basedirectory_for_attachments']))
- $_POST['basedirectory_for_attachments'] = $modSettings['basedirectory_for_attachments'];
- }
- if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']))
- {
- $modSettings['attachment_basedirectories'][$modSettings['currentAttachmentUploadDir']] = $_POST['basedirectory_for_attachments'];
- updateSettings(array(
- 'attachment_basedirectories' => serialize($modSettings['attachment_basedirectories']),
- 'currentAttachmentUploadDir' => $currentAttachmentUploadDir,
- ));
- $_POST['use_subdirectories_for_attachments'] = 1;
- $_POST['attachmentUploadDir'] = serialize($modSettings['attachmentUploadDir']);
- }
- }
- }
- call_integration_hook('integrate_save_attachment_settings');
- saveDBSettings($config_vars);
- redirectexit('action=admin;area=manageattachments;sa=attachments');
- }
- $context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=attachments';
- prepareDBSettingContext($config_vars);
- $context['sub_template'] = 'show_settings';
- }
- /**
- * This allows to show/change avatar settings.
- * Called by index.php?action=admin;area=manageattachments;sa=avatars.
- * Show/set permissions for permissions: 'profile_server_avatar',
- * 'profile_upload_avatar' and 'profile_remote_avatar'.
- *
- * @param bool $return_config = false
- * @uses 'avatars' sub template.
- */
- function action_avatars($return_config = false)
- {
- global $txt, $context, $modSettings, $scripturl;
- // Perform a test to see if the GD module or ImageMagick are installed.
- $testImg = get_extension_funcs('gd') || class_exists('Imagick');
- $context['valid_avatar_dir'] = is_dir($modSettings['avatar_directory']);
- $context['valid_custom_avatar_dir'] = empty($modSettings['custom_avatar_enabled']) || (!empty($modSettings['custom_avatar_dir']) && is_dir($modSettings['custom_avatar_dir']) && is_writable($modSettings['custom_avatar_dir']));
- $config_vars = array(
- array('title', 'avatar_settings'),
- array('check', 'avatar_default'),
- // Server stored avatars!
- array('title', 'avatar_server_stored'),
- array('warning', empty($testImg) ? 'avatar_img_enc_warning' : ''),
- array('permissions', 'profile_server_avatar', 0, $txt['avatar_server_stored_groups']),
- array('text', 'avatar_directory', 40, 'invalid' => !$context['valid_avatar_dir']),
- array('text', 'avatar_url', 40),
- // External avatars?
- array('title', 'avatar_external'),
- array('permissions', 'profile_remote_avatar', 0, $txt['avatar_external_url_groups']),
- array('check', 'avatar_download_external', 0, 'onchange' => 'fUpdateStatus();'),
- array('text', 'avatar_max_width_external', 'subtext' => $txt['zero_for_no_limit'], 6),
- array('text', 'avatar_max_height_external', 'subtext' => $txt['zero_for_no_limit'], 6),
- array('select', 'avatar_action_too_large',
- array(
- 'option_refuse' => $txt['option_refuse'],
- 'option_html_resize' => $txt['option_html_resize'],
- 'option_js_resize' => $txt['option_js_resize'],
- 'option_download_and_resize' => $txt['option_download_and_resize'],
- ),
- ),
- array('title','gravatar'),
- array('permissions', 'profile_gvatar', 0, $txt['gravatar_groups']),
- array('select', 'gravatar_rating',
- array(
- 'g' => 'g',
- 'pg' => 'pg',
- 'r' => 'r',
- 'x' => 'x',
- ),
- ),
- // Uploadable avatars?
- array('title', 'avatar_upload'),
- array('permissions', 'profile_upload_avatar', 0, $txt['avatar_upload_groups']),
- array('text', 'avatar_max_width_upload', 'subtext' => $txt['zero_for_no_limit'], 6),
- array('text', 'avatar_max_height_upload', 'subtext' => $txt['zero_for_no_limit'], 6),
- array('check', 'avatar_resize_upload', 'subtext' => $txt['avatar_resize_upload_note']),
- array('check', 'avatar_reencode'),
- '',
- array('warning', 'avatar_paranoid_warning'),
- array('check', 'avatar_paranoid'),
- '',
- array('check', 'avatar_download_png'),
- array('select', 'custom_avatar_enabled', array($txt['option_attachment_dir'], $txt['option_specified_dir']), 'onchange' => 'fUpdateStatus();'),
- array('text', 'custom_avatar_dir', 40, 'subtext' => $txt['custom_avatar_dir_desc'], 'invalid' => !$context['valid_custom_avatar_dir']),
- array('text', 'custom_avatar_url', 40),
- );
- call_integration_hook('integrate_modify_avatar_settings', array($config_vars));
- if ($return_config)
- return $config_vars;
- // We need this file for the settings template.
- require_once(ADMINDIR . '/ManageServer.php');
- // Saving avatar settings?
- if (isset($_GET['save']))
- {
- checkSession();
- // Just incase the admin forgot to set both custom avatar values, we disable it to prevent errors.
- if (isset($_POST['custom_avatar_enabled']) && $_POST['custom_avatar_enabled'] == 1 && (empty($_POST['custom_avatar_dir']) || empty($_POST['custom_avatar_url'])))
- $_POST['custom_avatar_enabled'] = 0;
- call_integration_hook('integrate_save_avatar_settings');
- saveDBSettings($config_vars);
- redirectexit('action=admin;area=manageattachments;sa=avatars');
- }
- // Attempt to figure out if the admin is trying to break things.
- $context['settings_save_onclick'] = 'return document.getElementById(\'custom_avatar_enabled\').value == 1 && (document.getElementById(\'custom_avatar_dir\').value == \'\' || document.getElementById(\'custom_avatar_url\').value == \'\') ? confirm(\'' . $txt['custom_avatar_check_empty'] . '\') : true;';
- // We need this for the in-line permissions
- createToken('admin-mp');
- // Prepare the context.
- $context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=avatars';
- prepareDBSettingContext($config_vars);
- // Add a layer for the javascript.
- $context['template_layers'][] = 'avatar_settings';
- $context['sub_template'] = 'show_settings';
- }
- /**
- * Show a list of attachment or avatar files.
- * Called by ?action=admin;area=manageattachments;sa=browse for attachments
- * and ?action=admin;area=manageattachments;sa=browse;avatars for avatars.
- * Allows sorting by name, date, size and member.
- * Paginates results.
- *
- * @uses the 'browse' sub template
- */
- function action_browse()
- {
- global $context, $txt, $scripturl, $options, $modSettings;
- global $smcFunc;
- $context['sub_template'] = 'browse';
- // Attachments or avatars?
- $context['browse_type'] = isset($_REQUEST['avatars']) ? 'avatars' : (isset($_REQUEST['thumbs']) ? 'thumbs' : 'attachments');
- // Set the options for the list component.
- $listOptions = array(
- 'id' => 'file_list',
- 'title' => $txt['attachment_manager_' . ($context['browse_type'] === 'avatars' ? 'avatars' : ($context['browse_type'] === 'thumbs' ? 'thumbs' : 'attachments'))],
- 'items_per_page' => $modSettings['defaultMaxMessages'],
- 'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=browse' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
- 'default_sort_col' => 'name',
- 'no_items_label' => $txt['attachment_manager_' . ($context['browse_type'] === 'avatars' ? 'avatars' : ( $context['browse_type'] === 'thumbs' ? 'thumbs' : 'attachments')) . '_no_entries'],
- 'get_items' => array(
- 'function' => 'list_getFiles',
- 'params' => array(
- $context['browse_type'],
- ),
- ),
- 'get_count' => array(
- 'function' => 'list_getNumFiles',
- 'params' => array(
- $context['browse_type'],
- ),
- ),
- 'columns' => array(
- 'name' => array(
- 'header' => array(
- 'value' => $txt['attachment_name'],
- ),
- 'data' => array(
- 'function' => create_function('$rowData', '
- global $modSettings, $context, $scripturl;
- $link = \'<a href="\';
- // In case of a custom avatar URL attachments have a fixed directory.
- if ($rowData[\'attachment_type\'] == 1)
- $link .= sprintf(\'%1$s/%2$s\', $modSettings[\'custom_avatar_url\'], $rowData[\'filename\']);
- // By default avatars are downloaded almost as attachments.
- elseif ($context[\'browse_type\'] == \'avatars\')
- $link .= sprintf(\'%1$s?action=dlattach;type=avatar;attach=%2$d\', $scripturl, $rowData[\'id_attach\']);
- // Normal attachments are always linked to a topic ID.
- else
- $link .= sprintf(\'%1$s?action=dlattach;topic=%2$d.0;attach=%3$d\', $scripturl, $rowData[\'id_topic\'], $rowData[\'id_attach\']);
- $link .= \'"\';
- // Show a popup on click if it\'s a picture and we know its dimensions.
- if (!empty($rowData[\'width\']) && !empty($rowData[\'height\']))
- $link .= sprintf(\' onclick="return reqWin(this.href\' . ($rowData[\'attachment_type\'] == 1 ? \'\' : \' + \\\';image\\\'\') . \', %1$d, %2$d, true);"\', $rowData[\'width\'] + 20, $rowData[\'height\'] + 20);
- $link .= sprintf(\'>%1$s</a>\', preg_replace(\'~&#(\\\\d{1,7}|x[0-9a-fA-F]{1,6});~\', \'&#\\\\1;\', htmlspecialchars($rowData[\'filename\'])));
- // Show the dimensions.
- if (!empty($rowData[\'width\']) && !empty($rowData[\'height\']))
- $link .= sprintf(\' <span class="smalltext">%1$dx%2$d</span>\', $rowData[\'width\'], $rowData[\'height\']);
- return $link;
- '),
- ),
- 'sort' => array(
- 'default' => 'a.filename',
- 'reverse' => 'a.filename DESC',
- ),
- ),
- 'filesize' => array(
- 'header' => array(
- 'value' => $txt['attachment_file_size'],
- ),
- 'data' => array(
- 'function' => create_function('$rowData','
- global $txt;
- return sprintf(\'%1$s%2$s\', round($rowData[\'size\'] / 1024, 2), $txt[\'kilobyte\']);
- '),
- ),
- 'sort' => array(
- 'default' => 'a.size',
- 'reverse' => 'a.size DESC',
- ),
- ),
- 'member' => array(
- 'header' => array(
- 'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_member'] : $txt['posted_by'],
- ),
- 'data' => array(
- 'function' => create_function('$rowData', '
- global $scripturl;
- // In case of an attachment, return the poster of the attachment.
- if (empty($rowData[\'id_member\']))
- return htmlspecialchars($rowData[\'poster_name\']);
- // Otherwise it must be an avatar, return the link to the owner of it.
- else
- return sprintf(\'<a href="%1$s?action=profile;u=%2$d">%3$s</a>\', $scripturl, $rowData[\'id_member\'], $rowData[\'poster_name\']);
- '),
- ),
- 'sort' => array(
- 'default' => 'mem.real_name',
- 'reverse' => 'mem.real_name DESC',
- ),
- ),
- 'date' => array(
- 'header' => array(
- 'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_last_active'] : $txt['date'],
- ),
- 'data' => array(
- 'function' => create_function('$rowData', '
- global $txt, $context, $scripturl;
- // The date the message containing the attachment was posted or the owner of the avatar was active.
- $date = empty($rowData[\'poster_time\']) ? $txt[\'never\'] : timeformat($rowData[\'poster_time\']);
- // Add a link to the topic in case of an attachment.
- if ($context[\'browse_type\'] !== \'avatars\')
- $date .= sprintf(\'<br />%1$s <a href="%2$s?topic=%3$d.0.msg%4$d#msg%4$d">%5$s</a>\', $txt[\'in\'], $scripturl, $rowData[\'id_topic\'], $rowData[\'id_msg\'], $rowData[\'subject\']);
- return $date;
- '),
- ),
- 'sort' => array(
- 'default' => $context['browse_type'] === 'avatars' ? 'mem.last_login' : 'm.id_msg',
- 'reverse' => $context['browse_type'] === 'avatars' ? 'mem.last_login DESC' : 'm.id_msg DESC',
- ),
- ),
- 'downloads' => array(
- 'header' => array(
- 'value' => $txt['downloads'],
- ),
- 'data' => array(
- 'db' => 'downloads',
- 'comma_format' => true,
- ),
- 'sort' => array(
- 'default' => 'a.downloads',
- 'reverse' => 'a.downloads DESC',
- ),
- ),
- 'check' => array(
- 'header' => array(
- 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
- 'class' => 'centercol',
- ),
- 'data' => array(
- 'sprintf' => array(
- 'format' => '<input type="checkbox" name="remove[%1$d]" class="input_check" />',
- 'params' => array(
- 'id_attach' => false,
- ),
- ),
- 'class' => 'centercol',
- ),
- ),
- ),
- 'form' => array(
- 'href' => $scripturl . '?action=admin;area=manageattachments;sa=remove' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
- 'include_sort' => true,
- 'include_start' => true,
- 'hidden_fields' => array(
- 'type' => $context['browse_type'],
- ),
- ),
- 'additional_rows' => array(
- array(
- 'position' => 'below_table_data',
- 'value' => '<input type="submit" name="remove_submit" class="button_submit" value="' . $txt['quickmod_delete_selected'] . '" onclick="return confirm(\'' . $txt['confirm_delete_attachments'] . '\');" />',
- ),
- ),
- );
- // Create the list.
- require_once(SUBSDIR . '/List.subs.php');
- createList($listOptions);
- }
- /**
- * Returns the list of attachments files (avatars or not), recorded
- * in the database, per the parameters received.
- *
- * @param int $start
- * @param int $items_per_page
- * @param string $sort
- * @param string $browse_type can be on eof 'avatars' or ... not. :P
- */
- function list_getFiles($start, $items_per_page, $sort, $browse_type)
- {
- global $smcFunc, $txt;
- // Choose a query depending on what we are viewing.
- if ($browse_type === 'avatars')
- $request = $smcFunc['db_query']('', '
- SELECT
- {string:blank_text} AS id_msg, IFNULL(mem.real_name, {string:not_applicable_text}) AS poster_name,
- mem.last_login AS poster_time, 0 AS id_topic, a.id_member, a.id_attach, a.filename, a.file_hash, a.attachment_type,
- a.size, a.width, a.height, a.downloads, {string:blank_text} AS subject, 0 AS id_board
- FROM {db_prefix}attachments AS a
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
- WHERE a.id_member != {int:guest_id}
- ORDER BY {raw:sort}
- LIMIT {int:start}, {int:per_page}',
- array(
- 'guest_id' => 0,
- 'blank_text' => '',
- 'not_applicable_text' => $txt['not_applicable'],
- 'sort' => $sort,
- 'start' => $start,
- 'per_page' => $items_per_page,
- )
- );
- else
- $request = $smcFunc['db_query']('', '
- SELECT
- m.id_msg, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.id_topic, m.id_member,
- a.id_attach, a.filename, a.file_hash, a.attachment_type, a.size, a.width, a.height, a.downloads, mf.subject, t.id_board
- FROM {db_prefix}attachments AS a
- INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
- INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
- INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
- WHERE a.attachment_type = {int:attachment_type}
- ORDER BY {raw:sort}
- LIMIT {int:start}, {int:per_page}',
- array(
- 'attachment_type' => $browse_type == 'thumbs' ? '3' : '0',
- 'sort' => $sort,
- 'start' => $start,
- 'per_page' => $items_per_page,
- )
- );
- $files = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $files[] = $row;
- $smcFunc['db_free_result']($request);
- return $files;
- }
- /**
- * Return the number of files of the specified type recorded in the database.
- * (the specified type being attachments or avatars).
- *
- * @param string $browse_type can be one of 'avatars' or not. (in which case they're attachments)
- */
- function list_getNumFiles($browse_type)
- {
- global $smcFunc;
- // Depending on the type of file, different queries are used.
- if ($browse_type === 'avatars')
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(*)
- FROM {db_prefix}attachments
- WHERE id_member != {int:guest_id_member}',
- array(
- 'guest_id_member' => 0,
- )
- );
- else
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(*) AS num_attach
- FROM {db_prefix}attachments AS a
- INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
- INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
- INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
- WHERE a.attachment_type = {int:attachment_type}
- AND a.id_member = {int:guest_id_member}',
- array(
- 'attachment_type' => $browse_type === 'thumbs' ? '3' : '0',
- 'guest_id_member' => 0,
- )
- );
- list ($num_files) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- return $num_files;
- }
- /**
- * Show several file maintenance options.
- * Called by ?action=admin;area=manageattachments;sa=maintain.
- * Calculates file statistics (total file size, number of attachments,
- * number of avatars, attachment space available).
- *
- * @uses the 'maintain' sub template.
- */
- function action_maintenance()
- {
- global $context, $modSettings, $txt, $smcFunc;
- $context['sub_template'] = 'maintenance';
- // We're working with them attachments here!
- require_once(SUBSDIR . '/Attachments.subs.php');
- // we need our attachments directories...
- $attach_dirs = getAttachmentDirs();
- // Get the number of attachments...
- $context['num_attachments'] = comma_format(getAttachmentCount(), 0);
- // Also get the avatar amount...
- $context['num_avatars'] = comma_format(getAvatarCount(), 0);
- // Check the size of all the directories.
- $request = $smcFunc['db_query']('', '
- SELECT SUM(size)
- FROM {db_prefix}attachments
- WHERE attachment_type != {int:type}',
- array(
- 'type' => 1,
- )
- );
- list ($attachmentDirSize) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // Divide it into kilobytes.
- $attachmentDirSize /= 1024;
- $context['attachment_total_size'] = comma_format($attachmentDirSize, 2);
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(*), SUM(size)
- FROM {db_prefix}attachments
- WHERE id_folder = {int:folder_id}
- AND attachment_type != {int:type}',
- array(
- 'folder_id' => $modSettings['currentAttachmentUploadDir'],
- 'type' => 1,
- )
- );
- list ($current_dir_files, $current_dir_size) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $current_dir_size /= 1024;
- // If they specified a limit only....
- if (!empty($modSettings['attachmentDirSizeLimit']))
- $context['attachment_space'] = comma_format(max($modSettings['attachmentDirSizeLimit'] - $current_dir_size, 0), 2);
- $context['attachment_current_size'] = comma_format($current_dir_size, 2);
- if (!empty($modSettings['attachmentDirFileLimit']))
- $context['attachment_files'] = comma_format(max($modSettings['attachmentDirFileLimit'] - $current_dir_files, 0), 0);
- $context['attachment_current_files'] = comma_format($current_dir_files, 0);
- $context['attach_multiple_dirs'] = count($attach_dirs) > 1 ? true : false;
- $context['attach_dirs'] = $attach_dirs;
- $context['base_dirs'] = !empty($modSettings['attachment_basedirectories']) ? unserialize($modSettings['attachment_basedirectories']) : array();
- $context['checked'] = isset($_SESSION['checked']) ? $_SESSION['checked'] : true;
- if (!empty($_SESSION['results']))
- {
- $context['results'] = implode('<br />', $_SESSION['results']);
- unset($_SESSION['results']);
- }
- }
- /**
- * Move avatars from their current location, to the custom_avatar_dir folder.
- * Called from the maintenance screen by ?action=admin;area=manageattachments;sa=action_moveAvatars.
- */
- function action_moveAvatars()
- {
- global $modSettings, $smcFunc;
- // First make sure the custom avatar dir is writable.
- if (!is_writable($modSettings['custom_avatar_dir']))
- {
- // Try to fix it.
- @chmod($modSettings['custom_avatar_dir'], 0777);
- // Guess that didn't work?
- if (!is_writable($modSettings['custom_avatar_dir']))
- fatal_lang_error('attachments_no_write', 'critical');
- }
- $request = $smcFunc['db_query']('', '
- SELECT id_attach, id_folder, id_member, filename, file_hash
- FROM {db_prefix}attachments
- WHERE attachment_type = {int:attachment_type}
- AND id_member > {int:guest_id_member}',
- array(
- 'attachment_type' => 0,
- 'guest_id_member' => 0,
- )
- );
- $updatedAvatars = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
- if (rename($filename, $modSettings['custom_avatar_dir'] . '/' . $row['filename']))
- $updatedAvatars[] = $row['id_attach'];
- }
- $smcFunc['db_free_result']($request);
- if (!empty($updatedAvatars))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET attachment_type = {int:attachment_type}
- WHERE id_attach IN ({array_int:updated_avatars})',
- array(
- 'updated_avatars' => $updatedAvatars,
- 'attachment_type' => 1,
- )
- );
- redirectexit('action=admin;area=manageattachments;sa=maintenance');
- }
- /**
- * Remove attachments older than a given age.
- * Called from the maintenance screen by
- * ?action=admin;area=manageattachments;sa=byAge.
- * It optionally adds a certain text to the messages the attachments
- * were removed from.
- * @todo refactor this silly superglobals use...
- */
- function action_byAge()
- {
- global $modSettings, $smcFunc;
- checkSession('post', 'admin');
- // @todo Ignore messages in topics that are stickied?
- // someone has to do the dirty work
- require_once(SUBSDIR . '/Attachments.subs.php');
- // Deleting an attachment?
- if ($_REQUEST['type'] != 'avatars')
- {
- // Get rid of all the old attachments.
- $messages = removeAttachments(array('attachment_type' => 0, 'poster_time' => (time() - 24 * 60 * 60 * $_POST['age'])), 'messages', true);
- // Update the messages to reflect the change.
- if (!empty($messages) && !empty($_POST['notice']))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}messages
- SET body = CONCAT(body, {string:notice})
- WHERE id_msg IN ({array_int:messages})',
- array(
- 'messages' => $messages,
- 'notice' => '<br /><br />' . $_POST['notice'],
- )
- );
- }
- else
- {
- // Remove all the old avatars.
- removeAttachments(array('not_id_member' => 0, 'last_login' => (time() - 24 * 60 * 60 * $_POST['age'])), 'members');
- }
- redirectexit('action=admin;area=manageattachments' . (empty($_REQUEST['avatars']) ? ';sa=maintenance' : ';avatars'));
- }
- /**
- * Remove attachments larger than a given size.
- * Called from the maintenance screen by
- * ?action=admin;area=manageattachments;sa=bySize.
- * Optionally adds a certain text to the messages the attachments were
- * removed from.
- */
- function action_bySize()
- {
- global $modSettings, $smcFunc;
- checkSession('post', 'admin');
- // we'll need this
- require_once(SUBSDIR . '/Attachments.subs.php');
- // Find humungous attachments.
- $messages = removeAttachments(array('attachment_type' => 0, 'size' => 1024 * $_POST['size']), 'messages', true);
- // And make a note on the post.
- if (!empty($messages) && !empty($_POST['notice']))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}messages
- SET body = CONCAT(body, {string:notice})
- WHERE id_msg IN ({array_int:messages})',
- array(
- 'messages' => $messages,
- 'notice' => '<br /><br />' . $_POST['notice'],
- )
- );
- redirectexit('action=admin;area=manageattachments;sa=maintenance');
- }
- /**
- * Remove a selection of attachments or avatars.
- * Called from the browse screen as submitted form by
- * ?action=admin;area=manageattachments;sa=remove
- */
- function action_remove()
- {
- global $txt, $smcFunc, $language;
- checkSession('post');
- if (!empty($_POST['remove']))
- {
- // we'll need this
- require_once(SUBSDIR . '/Attachments.subs.php');
- $attachments = array();
- // There must be a quicker way to pass this safety test??
- foreach ($_POST['remove'] as $removeID => $dummy)
- $attachments[] = (int) $removeID;
- if ($_REQUEST['type'] == 'avatars' && !empty($attachments))
- removeAttachments(array('id_attach' => $attachments));
- else if (!empty($attachments))
- {
- $messages = removeAttachments(array('id_attach' => $attachments), 'messages', true);
- // And change the message to reflect this.
- if (!empty($messages))
- {
- loadLanguage('index', $language, true);
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}messages
- SET body = CONCAT(body, {string:deleted_message})
- WHERE id_msg IN ({array_int:messages_affected})',
- array(
- 'messages_affected' => $messages,
- 'deleted_message' => '<br /><br />' . $txt['attachment_delete_admin'],
- )
- );
- loadLanguage('index', $user_info['language'], true);
- }
- }
- }
- $_GET['sort'] = isset($_GET['sort']) ? $_GET['sort'] : 'date';
- redirectexit('action=admin;area=manageattachments;sa=browse;' . $_REQUEST['type'] . ';sort=' . $_GET['sort'] . (isset($_GET['desc']) ? ';desc' : '') . ';start=' . $_REQUEST['start']);
- }
- /**
- * Removes all attachments in a single click
- * Called from the maintenance screen by
- * ?action=admin;area=manageattachments;sa=removeall.
- */
- function action_removeall()
- {
- global $txt, $smcFunc;
- checkSession('get', 'admin');
- // lots of work to do
- require_once(SUBSDIR . '/Attachments.subs.php');
- $messages = removeAttachments(array('attachment_type' => 0), '', true);
- if (!isset($_POST['notice']))
- $_POST['notice'] = $txt['attachment_delete_admin'];
- // Add the notice on the end of the changed messages.
- if (!empty($messages))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}messages
- SET body = CONCAT(body, {string:deleted_message})
- WHERE id_msg IN ({array_int:messages})',
- array(
- 'messages' => $messages,
- 'deleted_message' => '<br /><br />' . $_POST['notice'],
- )
- );
- redirectexit('action=admin;area=manageattachments;sa=maintenance');
- }
- /**
- * This function should find attachments in the database that no longer exist and clear them, and fix filesize issues.
- */
- function action_repair()
- {
- global $modSettings, $context, $txt, $smcFunc;
- checkSession('get');
- // If we choose cancel, redirect right back.
- if (isset($_POST['cancel']))
- redirectexit('action=admin;area=manageattachments;sa=maintenance');
- // Try give us a while to sort this out...
- @set_time_limit(600);
- $_GET['step'] = empty($_GET['step']) ? 0 : (int) $_GET['step'];
- $context['starting_substep'] = $_GET['substep'] = empty($_GET['substep']) ? 0 : (int) $_GET['substep'];
- // Don't recall the session just in case.
- if ($_GET['step'] == 0 && $_GET['substep'] == 0)
- {
- unset($_SESSION['attachments_to_fix'], $_SESSION['attachments_to_fix2']);
- // If we're actually fixing stuff - work out what.
- if (isset($_GET['fixErrors']))
- {
- // Nothing?
- if (empty($_POST['to_fix']))
- redirectexit('action=admin;area=manageattachments;sa=maintenance');
- $_SESSION['attachments_to_fix'] = array();
- // @todo No need to do this I think.
- foreach ($_POST['to_fix'] as $key => $value)
- $_SESSION['attachments_to_fix'][] = $value;
- }
- }
- // We will work hard with attachments.
- require_once(SUBSDIR . '/Attachments.subs.php');
- // All the valid problems are here:
- $context['repair_errors'] = array(
- 'missing_thumbnail_parent' => 0,
- 'parent_missing_thumbnail' => 0,
- 'file_missing_on_disk' => 0,
- 'file_wrong_size' => 0,
- 'file_size_of_zero' => 0,
- 'attachment_no_msg' => 0,
- 'avatar_no_member' => 0,
- 'wrong_folder' => 0,
- 'files_without_attachment' => 0,
- );
- $to_fix = !empty($_SESSION['attachments_to_fix']) ? $_SESSION['attachments_to_fix'] : array();
- $context['repair_errors'] = isset($_SESSION['attachments_to_fix2']) ? $_SESSION['attachments_to_fix2'] : $context['repair_errors'];
- $fix_errors = isset($_GET['fixErrors']) ? true : false;
- // Get stranded thumbnails.
- if ($_GET['step'] <= 0)
- {
- $result = $smcFunc['db_query']('', '
- SELECT MAX(id_attach)
- FROM {db_prefix}attachments
- WHERE attachment_type = {int:thumbnail}',
- array(
- 'thumbnail' => 3,
- )
- );
- list ($thumbnails) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
- {
- $to_remove = array();
- $result = $smcFunc['db_query']('', '
- SELECT thumb.id_attach, thumb.id_folder, thumb.filename, thumb.file_hash
- FROM {db_prefix}attachments AS thumb
- LEFT JOIN {db_prefix}attachments AS tparent ON (tparent.id_thumb = thumb.id_attach)
- WHERE thumb.id_attach BETWEEN {int:substep} AND {int:substep} + 499
- AND thumb.attachment_type = {int:thumbnail}
- AND tparent.id_attach IS NULL',
- array(
- 'thumbnail' => 3,
- 'substep' => $_GET['substep'],
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- // Only do anything once... just in case
- if (!isset($to_remove[$row['id_attach']]))
- {
- $to_remove[$row['id_attach']] = $row['id_attach'];
- $context['repair_errors']['missing_thumbnail_parent']++;
- // If we are repairing remove the file from disk now.
- if ($fix_errors && in_array('missing_thumbnail_parent', $to_fix))
- {
- $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
- @unlink($filename);
- }
- }
- }
- if ($smcFunc['db_num_rows']($result) != 0)
- $to_fix[] = 'missing_thumbnail_parent';
- $smcFunc['db_free_result']($result);
- // Do we need to delete what we have?
- if ($fix_errors && !empty($to_remove) && in_array('missing_thumbnail_parent', $to_fix))
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}attachments
- WHERE id_attach IN ({array_int:to_remove})
- AND attachment_type = {int:attachment_type}',
- array(
- 'to_remove' => $to_remove,
- 'attachment_type' => 3,
- )
- );
- pauseAttachmentMaintenance($to_fix, $thumbnails);
- }
- $_GET['step'] = 1;
- $_GET['substep'] = 0;
- pauseAttachmentMaintenance($to_fix);
- }
- // Find parents which think they have thumbnails, but actually, don't.
- if ($_GET['step'] <= 1)
- {
- $thumbnails = maxThumbnails();
- for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
- {
- $to_update = array();
- $result = $smcFunc['db_query']('', '
- SELECT a.id_attach
- FROM {db_prefix}attachments AS a
- LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
- WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
- AND a.id_thumb != {int:no_thumb}
- AND thumb.id_attach IS NULL',
- array(
- 'no_thumb' => 0,
- 'substep' => $_GET['substep'],
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- $to_update[] = $row['id_attach'];
- $context['repair_errors']['parent_missing_thumbnail']++;
- }
- if ($smcFunc['db_num_rows']($result) != 0)
- $to_fix[] = 'parent_missing_thumbnail';
- $smcFunc['db_free_result']($result);
- // Do we need to delete what we have?
- if ($fix_errors && !empty($to_update) && in_array('parent_missing_thumbnail', $to_fix))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET id_thumb = {int:no_thumb}
- WHERE id_attach IN ({array_int:to_update})',
- array(
- 'to_update' => $to_update,
- 'no_thumb' => 0,
- )
- );
- pauseAttachmentMaintenance($to_fix, $thumbnails);
- }
- $_GET['step'] = 2;
- $_GET['substep'] = 0;
- pauseAttachmentMaintenance($to_fix);
- }
- // This may take forever I'm afraid, but life sucks... recount EVERY attachments!
- if ($_GET['step'] <= 2)
- {
- $result = $smcFunc['db_query']('', '
- SELECT MAX(id_attach)
- FROM {db_prefix}attachments',
- array(
- )
- );
- list ($thumbnails) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 250)
- {
- $to_remove = array();
- $errors_found = array();
- $result = $smcFunc['db_query']('', '
- SELECT id_attach, id_folder, filename, file_hash, size, attachment_type
- FROM {db_prefix}attachments
- WHERE id_attach BETWEEN {int:substep} AND {int:substep} + 249',
- array(
- 'substep' => $_GET['substep'],
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- // Get the filename.
- if ($row['attachment_type'] == 1)
- $filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
- else
- $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
- // File doesn't exist?
- if (!file_exists($filename))
- {
- // If we're lucky it might just be in a different folder.
- if (!empty($modSettings['currentAttachmentUploadDir']))
- {
- // Get the attachment name with out the folder.
- $attachment_name = !empty($row['file_hash']) ? $row['id_attach'] . '_' . $row['file_hash'] : getLegacyAttachmentFilename($row['filename'], $row['id_attach'], null, true);
- if (!is_array($modSettings['attachmentUploadDir']))
- $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
- // Loop through the other folders.
- foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
- if (file_exists($dir . '/' . $attachment_name))
- {
- $context['repair_errors']['wrong_folder']++;
- $errors_found[] = 'wrong_folder';
- // Are we going to fix this now?
- if ($fix_errors && in_array('wrong_folder', $to_fix))
- attachment_folder($row['id_attach'], $id);
- continue 2;
- }
- }
- $to_remove[] = $row['id_attach'];
- $context['repair_errors']['file_missing_on_disk']++;
- $errors_found[] = 'file_missing_on_disk';
- }
- elseif (filesize($filename) == 0)
- {
- $context['repair_errors']['file_size_of_zero']++;
- $errors_found[] = 'file_size_of_zero';
- // Fixing?
- if ($fix_errors && in_array('file_size_of_zero', $to_fix))
- {
- $to_remove[] = $row['id_attach'];
- @unlink($filename);
- }
- }
- elseif (filesize($filename) != $row['size'])
- {
- $context['repair_errors']['file_wrong_size']++;
- $errors_found[] = 'file_wrong_size';
- // Fix it here?
- if ($fix_errors && in_array('file_wrong_size', $to_fix))
- attachment_filesize($row['id_attach'], filesize($filename));
- }
- }
- if (in_array('file_missing_on_disk', $errors_found))
- $to_fix[] = 'file_missing_on_disk';
- if (in_array('file_size_of_zero', $errors_found))
- $to_fix[] = 'file_size_of_zero';
- if (in_array('file_wrong_size', $errors_found))
- $to_fix[] = 'file_wrong_size';
- if (in_array('wrong_folder', $errors_found))
- $to_fix[] = 'wrong_folder';
- $smcFunc['db_free_result']($result);
- // Do we need to delete what we have?
- if ($fix_errors && !empty($to_remove))
- removeOrphanAttachments($to_remove);
- pauseAttachmentMaintenance($to_fix, $thumbnails);
- }
- $_GET['step'] = 3;
- $_GET['substep'] = 0;
- pauseAttachmentMaintenance($to_fix);
- }
- // Get avatars with no members associated with them.
- if ($_GET['step'] <= 3)
- {
- $result = $smcFunc['db_query']('', '
- SELECT MAX(id_attach)
- FROM {db_prefix}attachments',
- array(
- )
- );
- list ($thumbnails) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
- {
- $to_remove = array();
- $result = $smcFunc['db_query']('', '
- SELECT a.id_attach, a.id_folder, a.filename, a.file_hash, a.attachment_type
- FROM {db_prefix}attachments AS a
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
- WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
- AND a.id_member != {int:no_member}
- AND a.id_msg = {int:no_msg}
- AND mem.id_member IS NULL',
- array(
- 'no_member' => 0,
- 'no_msg' => 0,
- 'substep' => $_GET['substep'],
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- $to_remove[] = $row['id_attach'];
- $context['repair_errors']['avatar_no_member']++;
- // If we are repairing remove the file from disk now.
- if ($fix_errors && in_array('avatar_no_member', $to_fix))
- {
- if ($row['attachment_type'] == 1)
- $filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
- else
- $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
- @unlink($filename);
- }
- }
- if ($smcFunc['db_num_rows']($result) != 0)
- $to_fix[] = 'avatar_no_member';
- $smcFunc['db_free_result']($result);
- // Do we need to delete what we have?
- if ($fix_errors && !empty($to_remove) && in_array('avatar_no_member', $to_fix))
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}attachments
- WHERE id_attach IN ({array_int:to_remove})
- AND id_member != {int:no_member}
- AND id_msg = {int:no_msg}',
- array(
- 'to_remove' => $to_remove,
- 'no_member' => 0,
- 'no_msg' => 0,
- )
- );
- pauseAttachmentMaintenance($to_fix, $thumbnails);
- }
- $_GET['step'] = 4;
- $_GET['substep'] = 0;
- pauseAttachmentMaintenance($to_fix);
- }
- // What about attachments, who are missing a message :'(
- if ($_GET['step'] <= 4)
- {
- $result = $smcFunc['db_query']('', '
- SELECT MAX(id_attach)
- FROM {db_prefix}attachments',
- array(
- )
- );
- list ($thumbnails) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
- {
- $to_remove = array();
- $result = $smcFunc['db_query']('', '
- SELECT a.id_attach, a.id_folder, a.filename, a.file_hash
- FROM {db_prefix}attachments AS a
- LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
- WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
- AND a.id_member = {int:no_member}
- AND a.id_msg != {int:no_msg}
- AND m.id_msg IS NULL',
- array(
- 'no_member' => 0,
- 'no_msg' => 0,
- 'substep' => $_GET['substep'],
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- $to_remove[] = $row['id_attach'];
- $context['repair_errors']['attachment_no_msg']++;
- // If we are repairing remove the file from disk now.
- if ($fix_errors && in_array('attachment_no_msg', $to_fix))
- {
- $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
- @unlink($filename);
- }
- }
- if ($smcFunc['db_num_rows']($result) != 0)
- $to_fix[] = 'attachment_no_msg';
- $smcFunc['db_free_result']($result);
- // Do we need to delete what we have?
- if ($fix_errors && !empty($to_remove) && in_array('a…
Large files files are truncated, but you can click here to view the full file