123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881 |
- <?php
- /**
- * This file doing the job of attachments and avatars maintenance and management.
- * @todo refactor as controller-model
- *
- * Simple Machines Forum (SMF)
- *
- * @package SMF
- * @author Simple Machines http://www.simplemachines.org
- * @copyright 2011 Simple Machines
- * @license http://www.simplemachines.org/about/smf/license.php BSD
- *
- * @version 2.0
- */
- if (!defined('SMF'))
- die('Hacking attempt...');
- /**
- * 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' => 'ManageAttachmentSettings',
- 'attachpaths' => 'ManageAttachmentPaths',
- 'avatars' => 'ManageAvatarSettings',
- 'browse' => 'BrowseFiles',
- 'byAge' => 'RemoveAttachmentByAge',
- 'bySize' => 'RemoveAttachmentBySize',
- 'maintenance' => 'MaintainFiles',
- 'moveAvatars' => 'MoveAvatars',
- 'repair' => 'RepairAttachments',
- 'remove' => 'RemoveAttachment',
- 'removeall' => 'RemoveAllAttachments'
- );
- // 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=attachements.
- *
- * @param bool $return_config = false
- * @uses 'attachments' sub template.
- */
- function ManageAttachmentSettings($return_config = false)
- {
- global $txt, $modSettings, $scripturl, $context, $options, $sourcedir;
- $context['valid_upload_dir'] = is_dir($modSettings['attachmentUploadDir']) && is_writable($modSettings['attachmentUploadDir']);
- // Perform a test to see if the GD module is installed.
- $testGD = get_extension_funcs('gd');
- $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', 'attachmentCheckExtensions'),
- array('text', 'attachmentExtensions', 40),
- array('check', 'attachmentRecodeLineEndings'),
- '',
- // Directory and size limits.
- empty($modSettings['currentAttachmentUploadDir']) ? array('text', 'attachmentUploadDir', 40, 'invalid' => !$context['valid_upload_dir']) : array('var_message', 'attachmentUploadDir_multiple', 'message' => 'attachmentUploadDir_multiple_configure'),
- array('text', 'attachmentDirSizeLimit', 6, 'postinput' => $txt['kilobyte']),
- array('text', 'attachmentPostLimit', 6, 'postinput' => $txt['kilobyte']),
- array('text', 'attachmentSizeLimit', 6, 'postinput' => $txt['kilobyte']),
- array('text', 'attachmentNumPerPostLimit', 6),
- '',
- // Image settings.
- array('warning', empty($testGD) ? 'attachment_gd_warning' : ''),
- array('check', 'attachment_image_reencode'),
- '',
- array('warning', 'attachment_image_paranoid_warning'),
- array('check', 'attachment_image_paranoid'),
- '',
- // Thumbnail settings.
- array('check', 'attachmentShowImages'),
- array('check', 'attachmentThumbnails'),
- array('check', 'attachment_thumb_png'),
- array('text', 'attachmentThumbWidth', 6),
- array('text', 'attachmentThumbHeight', 6),
- );
- if ($return_config)
- return $config_vars;
- // These are very likely to come in handy! (i.e. without them we're doomed!)
- require_once($sourcedir . '/ManagePermissions.php');
- require_once($sourcedir . '/ManageServer.php');
- // Saving settings?
- if (isset($_GET['save']))
- {
- checkSession();
- 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 $return_config
- * @uses 'avatars' sub template.
- */
- function ManageAvatarSettings($return_config = false)
- {
- global $txt, $context, $modSettings, $sourcedir, $scripturl;
- // Perform a test to see if the GD module is installed.
- $testGD = get_extension_funcs('gd');
- $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(
- // Server stored avatars!
- array('title', 'avatar_server_stored'),
- array('warning', empty($testGD) ? 'avatar_gd_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', 6),
- array('text', 'avatar_max_height_external', 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'],
- ),
- ),
- // Uploadable avatars?
- array('title', 'avatar_upload'),
- array('permissions', 'profile_upload_avatar', 0, $txt['avatar_upload_groups']),
- array('text', 'avatar_max_width_upload', 6),
- array('text', 'avatar_max_height_upload', 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),
- );
- if ($return_config)
- return $config_vars;
- // We need these files for the inline permission settings, and the settings template.
- require_once($sourcedir . '/ManagePermissions.php');
- require_once($sourcedir . '/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;
- 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;';
- // 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 BrowseFiles()
- {
- global $context, $txt, $scripturl, $options, $modSettings;
- global $smcFunc, $sourcedir;
- $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\']);
- '),
- 'class' => 'windowbg',
- ),
- '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;
- '),
- 'class' => 'windowbg',
- ),
- '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(
- 'function' => create_function('$rowData','
- global $txt;
- return comma_format($rowData[\'downloads\']);
- '),
- 'class' => 'windowbg',
- ),
- '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" />',
- ),
- 'data' => array(
- 'sprintf' => array(
- 'format' => '<input type="checkbox" name="remove[%1$d]" class="input_check" />',
- 'params' => array(
- 'id_attach' => false,
- ),
- ),
- 'style' => 'text-align: center',
- ),
- ),
- ),
- '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'] . '\');" />',
- 'style' => 'text-align: right;',
- ),
- ),
- );
- // Create the list.
- require_once($sourcedir . '/Subs-List.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 '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 '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 MaintainFiles()
- {
- global $context, $modSettings, $txt, $smcFunc;
- $context['sub_template'] = 'maintenance';
- if (!empty($modSettings['currentAttachmentUploadDir']))
- $attach_dirs = unserialize($modSettings['attachmentUploadDir']);
- else
- $attach_dirs = array($modSettings['attachmentUploadDir']);
- // Get the number of attachments....
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(*)
- 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,
- )
- );
- list ($context['num_attachments']) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // Also get the avatar amount....
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(*)
- FROM {db_prefix}attachments
- WHERE id_member != {int:guest_id_member}',
- array(
- 'guest_id_member' => 0,
- )
- );
- list ($context['num_avatars']) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // Find out how big the directory is. We have to loop through all our attachment paths in case there's an old temp file in one of them.
- $attachmentDirSize = 0;
- foreach ($attach_dirs as $id => $attach_dir)
- {
- $dir = @opendir($attach_dir) or fatal_lang_error('cant_access_upload_path', 'critical');
- while ($file = readdir($dir))
- {
- if ($file == '.' || $file == '..')
- continue;
- if (preg_match('~^post_tmp_\d+_\d+$~', $file) != 0)
- {
- // Temp file is more than 5 hours old!
- if (filemtime($attach_dir . '/' . $file) < time() - 18000)
- @unlink($attach_dir . '/' . $file);
- continue;
- }
- // We're only counting the size of the current attachment directory.
- if (empty($modSettings['currentAttachmentUploadDir']) || $modSettings['currentAttachmentUploadDir'] == $id)
- $attachmentDirSize += filesize($attach_dir . '/' . $file);
- }
- closedir($dir);
- }
- // Divide it into kilobytes.
- $attachmentDirSize /= 1024;
- // If they specified a limit only....
- if (!empty($modSettings['attachmentDirSizeLimit']))
- $context['attachment_space'] = max(round($modSettings['attachmentDirSizeLimit'] - $attachmentDirSize, 2), 0);
- $context['attachment_total_size'] = round($attachmentDirSize, 2);
- $context['attach_multiple_dirs'] = !empty($modSettings['currentAttachmentUploadDir']);
- }
- /**
- * Move avatars from their current location, to the custom_avatar_dir folder.
- * Called from the maintenance screen by ?action=admin;area=manageattachments;sa=moveAvatars.
- */
- function 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 RemoveAttachmentByAge()
- {
- global $modSettings, $smcFunc;
- checkSession('post', 'admin');
- // @todo Ignore messages in topics that are stickied?
- // 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))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}messages
- SET body = CONCAT(body, ' . (!empty($_POST['notice']) ? '{string:notice}' : '') . ')
- WHERE id_msg IN ({array_int:messages})',
- array(
- 'messages' => $messages,
- 'notice' => empty($_POST['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 RemoveAttachmentBySize()
- {
- global $modSettings, $smcFunc;
- checkSession('post', 'admin');
- // 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))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}messages
- SET body = CONCAT(body, ' . (!empty($_POST['notice']) ? '{string:notice}' : '') . ')
- WHERE id_msg IN ({array_int:messages})',
- array(
- 'messages' => $messages,
- 'notice' => empty($_POST['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 RemoveAttachment()
- {
- global $modSettings, $txt, $smcFunc;
- checkSession('post');
- if (!empty($_POST['remove']))
- {
- $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))
- $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'],
- )
- );
- }
- }
- $_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 RemoveAllAttachments()
- {
- global $txt, $smcFunc;
- checkSession('get', 'admin');
- $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');
- }
- /**
- * Removes attachments or avatars based on a given query condition.
- * Called by several remove avatar/attachment functions in this file.
- * It removes attachments based that match the $condition.
- * It allows query_types 'messages' and 'members', whichever is need by the
- * $condition parameter.
- * It does no permissions check.
- *
- * @param array $condition
- * @param string $query_type
- * @param bool $return_affected_messages = false
- * @param bool $autoThumbRemoval = true
- */
- function removeAttachments($condition, $query_type = '', $return_affected_messages = false, $autoThumbRemoval = true)
- {
- global $modSettings, $smcFunc;
- //!!! This might need more work!
- $new_condition = array();
- $query_parameter = array(
- 'thumb_attachment_type' => 3,
- );
- if (is_array($condition))
- {
- foreach ($condition as $real_type => $restriction)
- {
- // Doing a NOT?
- $is_not = substr($real_type, 0, 4) == 'not_';
- $type = $is_not ? substr($real_type, 4) : $real_type;
- if (in_array($type, array('id_member', 'id_attach', 'id_msg')))
- $new_condition[] = 'a.' . $type . ($is_not ? ' NOT' : '') . ' IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
- elseif ($type == 'attachment_type')
- $new_condition[] = 'a.attachment_type = {int:' . $real_type . '}';
- elseif ($type == 'poster_time')
- $new_condition[] = 'm.poster_time < {int:' . $real_type . '}';
- elseif ($type == 'last_login')
- $new_condition[] = 'mem.last_login < {int:' . $real_type . '}';
- elseif ($type == 'size')
- $new_condition[] = 'a.size > {int:' . $real_type . '}';
- elseif ($type == 'id_topic')
- $new_condition[] = 'm.id_topic IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
- // Add the parameter!
- $query_parameter[$real_type] = $restriction;
- }
- $condition = implode(' AND ', $new_condition);
- }
- // Delete it only if it exists...
- $msgs = array();
- $attach = array();
- $parents = array();
- // Get all the attachment names and id_msg's.
- $request = $smcFunc['db_query']('', '
- SELECT
- 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') . ',
- thumb.id_folder AS thumb_folder, IFNULL(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
- FROM {db_prefix}attachments AS a' .($query_type == 'members' ? '
- INNER JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)' : ($query_type == 'messages' ? '
- INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)' : '')) . '
- LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
- LEFT JOIN {db_prefix}attachments AS thumb_parent ON (thumb.attachment_type = {int:thumb_attachment_type} AND thumb_parent.id_thumb = a.id_attach)
- WHERE ' . $condition,
- $query_parameter
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // Figure out the "encrypted" filename and unlink it ;).
- if ($row['attachment_type'] == 1)
- @unlink($modSettings['custom_avatar_dir'] . '/' . $row['filename']);
- else
- {
- $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
- @unlink($filename);
- // If this was a thumb, the parent attachment should know about it.
- if (!empty($row['id_parent']))
- $parents[] = $row['id_parent'];
- // If this attachments has a thumb, remove it as well.
- if (!empty($row['id_thumb']) && $autoThumbRemoval)
- {
- $thumb_filename = getAttachmentFilename($row['thumb_filename'], $row['id_thumb'], $row['thumb_folder'], false, $row['thumb_file_hash']);
- @unlink($thumb_filename);
- $attach[] = $row['id_thumb'];
- }
- }
- // Make a list.
- if ($return_affected_messages && empty($row['attachment_type']))
- $msgs[] = $row['id_msg'];
- $attach[] = $row['id_attach'];
- }
- $smcFunc['db_free_result']($request);
- // Removed attachments don't have to be updated anymore.
- $parents = array_diff($parents, $attach);
- if (!empty($parents))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET id_thumb = {int:no_thumb}
- WHERE id_attach IN ({array_int:parent_attachments})',
- array(
- 'parent_attachments' => $parents,
- 'no_thumb' => 0,
- )
- );
- if (!empty($attach))
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}attachments
- WHERE id_attach IN ({array_int:attachment_list})',
- array(
- 'attachment_list' => $attach,
- )
- );
- if ($return_affected_messages)
- return array_unique($msgs);
- }
- /**
- * This function should find attachments in the database that no longer exist and clear them, and fix filesize issues.
- */
- function RepairAttachments()
- {
- 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'];
- $_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();
- //!!! No need to do this I think.
- foreach ($_POST['to_fix'] as $key => $value)
- $_SESSION['attachments_to_fix'][] = $value;
- }
- }
- // 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,
- );
- $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)
- {
- $result = $smcFunc['db_query']('', '
- SELECT MAX(id_attach)
- FROM {db_prefix}attachments
- WHERE id_thumb != {int:no_thumb}',
- array(
- 'no_thumb' => 0,
- )
- );
- list ($thumbnails) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- 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))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET id_folder = {int:new_folder}
- WHERE id_attach = {int:id_attach}',
- array(
- 'new_folder' => $id,
- 'id_attach' => $row['id_attach'],
- )
- );
- 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))
- {
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET size = {int:filesize}
- WHERE id_attach = {int:id_attach}',
- array(
- 'filesize' => filesize($filename),
- 'id_attach' => $row['id_attach'],
- )
- );
- }
- }
- }
- 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))
- {
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}attachments
- WHERE id_attach IN ({array_int:to_remove})',
- array(
- 'to_remove' => $to_remove,
- )
- );
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET id_thumb = {int:no_thumb}
- WHERE id_thumb IN ({array_int:to_remove})',
- array(
- 'to_remove' => $to_remove,
- 'no_thumb' => 0,
- )
- );
- }
- 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('attachment_no_msg', $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'] = 5;
- $_GET['substep'] = 0;
- pauseAttachmentMaintenance($to_fix);
- }
- // Got here we must be doing well - just the template! :D
- $context['page_title'] = $txt['repair_attachments'];
- $context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
- $context['sub_template'] = 'attachment_repair';
- // What stage are we at?
- $context['completed'] = $fix_errors ? true : false;
- $context['errors_found'] = !empty($to_fix) ? true : false;
- }
- /**
- * Function called in-between each round of attachments and avatar repairs.
- * Called by repairAttachments().
- * If repairAttachments() has more steps added, this function needs updated!
- *
- * @param array $to_fix attachments to fix
- * @param int $max_substep = 0
- */
- function pauseAttachmentMaintenance($to_fix, $max_substep = 0)
- {
- global $context, $txt, $time_start;
- // Try get more time...
- @set_time_limit(600);
- if (function_exists('apache_reset_timeout'))
- @apache_reset_timeout();
- // Have we already used our maximum time?
- if (time() - array_sum(explode(' ', $time_start)) < 3)
- return;
- $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'];
- $context['page_title'] = $txt['not_done_title'];
- $context['continue_post_data'] = '';
- $context['continue_countdown'] = '2';
- $context['sub_template'] = 'not_done';
- // Specific stuff to not break this template!
- $context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
- // Change these two if more steps are added!
- if (empty($max_substep))
- $context['continue_percent'] = round(($_GET['step'] * 100) / 25);
- else
- $context['continue_percent'] = round(($_GET['step'] * 100 + ($_GET['substep'] * 100) / $max_substep) / 25);
- // Never more than 100%!
- $context['continue_percent'] = min($context['continue_percent'], 100);
- $_SESSION['attachments_to_fix'] = $to_fix;
- $_SESSION['attachments_to_fix2'] = $context['repair_errors'];
- obExit();
- }
- /**
- * Called from a mouse click, works out what we want to do with attachments and actions it.
- */
- function ApproveAttach()
- {
- global $smcFunc;
- // Security is our primary concern...
- checkSession('get');
- // If it approve or delete?
- $is_approve = !isset($_GET['sa']) || $_GET['sa'] != 'reject' ? true : false;
- $attachments = array();
- // If we are approving all ID's in a message , get the ID's.
- if ($_GET['sa'] == 'all' && !empty($_GET['mid']))
- {
- $id_msg = (int) $_GET['mid'];
- $request = $smcFunc['db_query']('', '
- SELECT id_attach
- FROM {db_prefix}attachments
- WHERE id_msg = {int:id_msg}
- AND approved = {int:is_approved}
- AND attachment_type = {int:attachment_type}',
- array(
- 'id_msg' => $id_msg,
- 'is_approved' => 0,
- 'attachment_type' => 0,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $attachments[] = $row['id_attach'];
- $smcFunc['db_free_result']($request);
- }
- elseif (!empty($_GET['aid']))
- $attachments[] = (int) $_GET['aid'];
- if (empty($attachments))
- fatal_lang_error('no_access', false);
- // Now we have some ID's cleaned and ready to approve, but first - let's check we have permission!
- $allowed_boards = boardsAllowedTo('approve_posts');
- // Validate the attachments exist and are the right approval state.
- $request = $smcFunc['db_query']('', '
- SELECT a.id_attach, m.id_board, m.id_msg, m.id_topic
- FROM {db_prefix}attachments AS a
- INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
- WHERE a.id_attach IN ({array_int:attachments})
- AND a.attachment_type = {int:attachment_type}
- AND a.approved = {int:is_approved}',
- array(
- 'attachments' => $attachments,
- 'attachment_type' => 0,
- 'is_approved' => 0,
- )
- );
- $attachments = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // We can only add it if we can approve in this board!
- if ($allowed_boards = array(0) || in_array($row['id_board'], $allowed_boards))
- {
- $attachments[] = $row['id_attach'];
- // Also come up witht he redirection URL.
- $redirect = 'topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'];
- }
- }
- $smcFunc['db_free_result']($request);
- if (empty($attachments))
- fatal_lang_error('no_access', false);
- // Finally, we are there. Follow through!
- if ($is_approve)
- {
- // Checked and deemed worthy.
- ApproveAttachments($attachments);
- }
- else
- removeAttachments(array('id_attach' => $attachments));
- // Return to the topic....
- redirectexit($redirect);
- }
- /**
- * Approve an attachment, or maybe even more - no permission check!
- *
- * @param $attachments
- */
- function ApproveAttachments($attachments)
- {
- global $smcFunc;
- if (empty($attachments))
- return 0;
- // For safety, check for thumbnails...
- $request = $smcFunc['db_query']('', '
- SELECT
- a.id_attach, a.id_member, IFNULL(thumb.id_attach, 0) AS id_thumb
- FROM {db_prefix}attachments AS a
- LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
- WHERE a.id_attach IN ({array_int:attachments})
- AND a.attachment_type = {int:attachment_type}',
- array(
- 'attachments' => $attachments,
- 'attachment_type' => 0,
- )
- );
- $attachments = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // Update the thumbnail too...
- if (!empty($row['id_thumb']))
- $attachments[] = $row['id_thumb'];
- $attachments[] = $row['id_attach'];
- }
- $smcFunc['db_free_result']($request);
- // Approving an attachment is not hard - it's easy.
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET approved = {int:is_approved}
- WHERE id_attach IN ({array_int:attachments})',
- array(
- 'attachments' => $attachments,
- 'is_approved' => 1,
- )
- );
- // Remove from the approval queue.
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}approval_queue
- WHERE id_attach IN ({array_int:attachments})',
- array(
- 'attachments' => $attachments,
- )
- );
- }
- /**
- * This function lists and allows updating of multiple attachments paths.
- */
- function ManageAttachmentPaths()
- {
- global $modSettings, $scripturl, $context, $txt, $sourcedir, $smcFunc;
- // Saving?
- if (isset($_REQUEST['save']))
- {
- checkSession();
- $new_dirs = array();
- foreach ($_POST['dirs'] as $id => $path)
- {
- $id = (int) $id;
- if ($id < 1)
- continue;
- if (empty($path))
- {
- // Let's not try to delete a path with files in it.
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(id_attach) AS num_attach
- FROM {db_prefix}attachments
- WHERE id_folder = {int:id_folder}',
- array(
- 'id_folder' => (int) $id,
- )
- );
- list ($num_attach) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // It's safe to delete.
- if ($num_attach == 0)
- continue;
- }
- $new_dirs[$id] = $path;
- }
- // We need to make sure the current directory is right.
- $_POST['current_dir'] = (int) $_POST['current_dir'];
- if (empty($_POST['current_dir']) || empty($new_dirs[$_POST['current_dir']]))
- fatal_lang_error('attach_path_current_bad', false);
- // Going back to just one path?
- if (count($new_dirs) == 1)
- {
- // We might need to reset the paths. This loop will just loop through once.
- foreach ($new_dirs as $id => $dir)
- {
- if ($id != 1)
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}attachments
- SET id_folder = {int:default_folder}
- WHERE id_folder = {int:current_folder}',
- array(
- 'default_folder' => 1,
- 'current_folder' => $id,
- )
- );
- updateSettings(array(
- 'currentAttachmentUploadDir' => 0,
- 'attachmentUploadDir' => $dir,
- ));
- }
- }
- else
- // Save it to the database.
- updateSettings(array(
- 'currentAttachmentUploadDir' => $_POST['current_dir'],
- 'attachmentUploadDir' => serialize($new_dirs),
- ));
- }
- // Are they here for the first time?
- if (empty($modSettings['currentAttachmentUploadDir']))
- {
- $modSettings['attachmentUploadDir'] = array(
- 1 => $modSettings['attachmentUploadDir']
- );
- $modSettings['currentAttachmentUploadDir'] = 1;
- }
- // Otherwise just load up their attachment paths.
- else
- $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
- $listOptions = array(
- 'id' => 'attach_paths',
- 'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
- 'title' => $txt['attach_paths'],
- 'get_items' => array(
- 'function' => 'list_getAttachDirs',
- ),
- 'columns' => array(
- 'current_dir' => array(
- 'header' => array(
- 'value' => $txt['attach_current_dir'],
- ),
- 'data' => array(
- 'function' => create_function('$rowData', '
- return \'<input type="radio" name="current_dir" value="\' . $rowData[\'id\'] . \'" \' . ($rowData[\'current\'] ? \'checked="checked"\' : \'\') . \' class="input_radio" />\';
- '),
- 'style' => 'text-align: center; width: 15%;',
- ),
- ),
- 'path' => array(
- 'header' => array(
- 'value' => $txt['attach_path'],
- ),
- 'data' => array(
- 'function' => create_function('$rowData', '
- return \'<input type="text" size="30" name="dirs[\' . $rowData[\'id\'] . \']" value="\' . $rowData[\'path\'] . \'" class="input_text" style="width: 100%" />\';
- '),
- 'style' => 'text-align: center; width: 30%;',
- ),
- ),
- 'current_size' => array(
- 'header' => array(
- 'value' => $txt['attach_current_size'],
- ),
- 'data' => array(
- 'db' => 'current_size',
- 'style' => 'text-align: center; width: 15%;',
- ),
- ),
- 'num_files' => array(
- 'header' => array(
- 'value' => $txt['attach_num_files'],
- ),
- 'data' => array(
- 'db' => 'num_files',
- 'style' => 'text-align: center; width: 15%;',
- ),
- ),
- 'status' => array(
- 'header' => array(
- 'value' => $txt['attach_dir_status'],
- ),
- 'data' => array(
- 'db' => 'status',
- 'style' => 'text-align: center; width: 25%;',
- ),
- ),
- ),
- 'form' => array(
- 'href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
- ),
- 'additional_rows' => array(
- array(
- 'position' => 'below_table_data',
- 'value' => '<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '" /><input type="submit" name="new_path" value="' . $txt['attach_add_path'] . '" class="button_submit" /> <input type="submit" name="save" value="' . $txt['save'] . '" class="button_submit" />',
- 'style' => 'text-align: right;',
- ),
- ),
- );
- require_once($sourcedir . '/Subs-List.php');
- createList($listOptions);
- // Fix up our template.
- $context[$context['admin_menu_name']]['current_subsection'] = 'attachments';
- $context['page_title'] = $txt['attach_path_manage'];
- $context['sub_template'] = 'attachment_paths';
- }
- /**
- * Prepare the actual attachment directories to be displayed in the list.
- */
- function list_getAttachDirs()
- {
- global $smcFunc, $modSettings, $context, $txt;
- // The dirs should already have been unserialized but just in case...
- if (!is_array($modSettings['attachmentUploadDir']))
- $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
- $request = $smcFunc['db_query']('', '
- SELECT id_folder, COUNT(id_attach) AS num_attach
- FROM {db_prefix}attachments' . (empty($modSettings['custom_avatar_enabled']) ? '' : '
- WHERE attachment_type != {int:type_avatar}') . '
- GROUP BY id_folder',
- array(
- 'type_avatar' => 1,
- )
- );
- $expected_files = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $expected_files[$row['id_folder']] = $row['num_attach'];
- $smcFunc['db_free_result']($request);
- $attachdirs = array();
- foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
- {
- // If there aren't any attachments in this directory this won't exist.
- if (!isset($expected_files[$id]))
- $expected_files[$id] = 0;
- // Check if the directory is doing okay.
- list ($status, $error, $size) = attachDirStatus($dir, $expected_files[$id]);
- $attachdirs[] = array(
- 'id' => $id,
- 'current' => $id == $modSettings['currentAttachmentUploadDir'],
- 'path' => $dir,
- 'current_size' => $size,
- 'num_files' => $expected_files[$id],
- 'status' => ($error ? '<span class="error">' : '') . sprintf($txt['attach_dir_' . $status], $context['session_id'], $context['session_var']) . ($error ? '</span>' : ''),
- );
- }
- // Just stick a new directory on at the bottom.
- if (isset($_REQUEST['new_path']))
- $attachdirs[] = array(
- 'id' => max(array_merge(array_keys($expected_files), array_keys($modSettings['attachmentUploadDir']))) + 1,
- 'current' => false,
- 'path' => '',
- 'current_size' => '',
- 'num_files' => '',
- 'status' => '',
- );
- return $attachdirs;
- }
- /**
- * Checks the status of an attachment directory and returns an array
- * of the status key, if that status key signifies an error, and
- * the folder size.
- *
- * @param string $dir
- * @param int $expected_files
- */
- function attachDirStatus($dir, $expected_files)
- {
- if (!is_dir($dir))
- return array('does_not_exist', true, '');
- elseif (!is_writable($dir))
- return array('not_writable', true, '');
- // Everything is okay so far, start to scan through the directory.
- $dir_size = 0;
- $num_files = 0;
- $dir_handle = dir($dir);
- while ($file = $dir_handle->read())
- {
- // Now do we have a real file here?
- if (in_array($file, array('.', '..', '.htaccess', 'index.php')))
- continue;
- $dir_size += filesize($dir . '/' . $file);
- $num_files++;
- }
- $dir_handle->close();
- $dir_size = round($dir_size / 1024, 2);
- if ($num_files < $expected_files)
- return array('files_missing', true, $dir_size);
- // Empty?
- elseif ($expected_files == 0)
- return array('unused', false, $dir_size);
- // All good!
- else
- return array('ok', false, $dir_size);
- }
- ?>
|