{$lang->step2of2_line1}
"; echo "{$lang->step_line2}
"; echo "style}/images/spinner_big.gif\" alt=\"{$lang->scanning}\" id=\"spinner\" />
"; $page->output_footer(false); flush(); $missing_attachment_files = array(); $missing_threads = array(); $incomplete_attachments = array(); $query = $db->query(" SELECT a.*, a.pid AS attachment_pid, p.pid FROM ".TABLE_PREFIX."attachments a LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) ORDER BY a.aid"); while($attachment = $db->fetch_array($query)) { // Check if the attachment exists in the file system if(!file_exists($uploadspath_abs."/{$attachment['attachname']}")) { $missing_attachment_files[$attachment['aid']] = $attachment['aid']; } // Check if the thread/post for this attachment is missing else if(!$attachment['pid'] && $attachment['attachment_pid']) { $missing_threads[$attachment['aid']] = $attachment['aid']; } // Check if the attachment was uploaded > 24 hours ago but not assigned to a thread else if(!$attachment['attachment_pid'] && $attachment['dateuploaded'] < TIME_NOW-60*60*24 && $attachment['dateuploaded'] != 0) { $incomplete_attachments[$attachment['aid']] = $attachment['aid']; } } // Now send the user to the final page $form = new Form("index.php?module=forum-attachments&action=orphans&step=3", "post", "redirect_form", 0, ""); // Scan complete if($mybb->get_input('bad_attachments')) { echo $form->generate_hidden_field("bad_attachments", $mybb->input['bad_attachments']); } if(is_array($missing_attachment_files) && count($missing_attachment_files) > 0) { $missing_attachment_files = my_serialize($missing_attachment_files); echo $form->generate_hidden_field("missing_attachment_files", $missing_attachment_files); } if(is_array($missing_threads) && count($missing_threads) > 0) { $missing_threads = my_serialize($missing_threads); echo $form->generate_hidden_field("missing_threads", $missing_threads); } if(is_array($incomplete_attachments) && count($incomplete_attachments) > 0) { $incomplete_attachments = my_serialize($incomplete_attachments); echo $form->generate_hidden_field("incomplete_attachments", $incomplete_attachments); } $form->end(); echo ""; exit; } // Running first step, scan the file system else { $plugins->run_hooks("admin_forum_attachments_orphans_step1"); /** * @param string $dir */ function scan_attachments_directory($dir="") { global $db, $mybb, $bad_attachments, $attachments_to_check, $uploadspath_abs; $real_dir = $uploadspath_abs; $false_dir = ""; if($dir) { $real_dir .= "/".$dir; $false_dir = $dir."/"; } if($dh = opendir($real_dir)) { while(false !== ($file = readdir($dh))) { if($file == "." || $file == ".." || $file == ".svn") { continue; } if(is_dir($real_dir.'/'.$file)) { scan_attachments_directory($false_dir.$file); } else if(my_substr($file, -7, 7) == ".attach") { $attachments_to_check["$false_dir$file"] = $false_dir.$file; // In allotments of 20, query the database for these attachments if(count($attachments_to_check) >= 20) { $attachments_to_check = array_map(array($db, "escape_string"), $attachments_to_check); $attachment_names = "'".implode("','", $attachments_to_check)."'"; $query = $db->simple_select("attachments", "aid, attachname", "attachname IN ($attachment_names)"); while($attachment = $db->fetch_array($query)) { unset($attachments_to_check[$attachment['attachname']]); } // Now anything left is bad! if(count($attachments_to_check) > 0) { if($bad_attachments) { $bad_attachments = @array_merge($bad_attachments, $attachments_to_check); } else { $bad_attachments = $attachments_to_check; } } $attachments_to_check = array(); } } } closedir($dh); // Any reamining to check? if(!empty($attachments_to_check)) { $attachments_to_check = array_map(array($db, "escape_string"), $attachments_to_check); $attachment_names = "'".implode("','", $attachments_to_check)."'"; $query = $db->simple_select("attachments", "aid, attachname", "attachname IN ($attachment_names)"); while($attachment = $db->fetch_array($query)) { unset($attachments_to_check[$attachment['attachname']]); } // Now anything left is bad! if(count($attachments_to_check) > 0) { if($bad_attachments) { $bad_attachments = @array_merge($bad_attachments, $attachments_to_check); } else { $bad_attachments = $attachments_to_check; } } } } } $page->output_header("{$lang->orphan_attachments_search} - {$lang->step1}"); $page->output_nav_tabs($sub_tabs, 'find_orphans'); echo "{$lang->step1of2_line1}
"; echo "{$lang->step_line2}
"; echo "style}/images/spinner_big.gif\" alt=\"{$lang->scanning}\" id=\"spinner\" />
"; $page->output_footer(false); flush(); scan_attachments_directory(); global $bad_attachments; $form = new Form("index.php?module=forum-attachments&action=orphans&step=2", "post", "redirect_form", 0, ""); // Scan complete if(is_array($bad_attachments) && count($bad_attachments) > 0) { $bad_attachments = my_serialize($bad_attachments); echo $form->generate_hidden_field("bad_attachments", $bad_attachments); } $form->end(); echo ""; exit; } } if(!$mybb->input['action']) { $plugins->run_hooks("admin_forum_attachments_start"); if($mybb->request_method == "post" || $mybb->get_input('results', MyBB::INPUT_INT) == 1) { $search_sql = '1=1'; $plugins->run_hooks("admin_forum_attachments_commit_start"); // Build the search SQL for users // List of valid LIKE search fields $user_like_fields = array("filename", "filetype"); foreach($user_like_fields as $search_field) { if($mybb->get_input($search_field)) { $search_sql .= " AND a.{$search_field} LIKE '%".$db->escape_string_like($mybb->input[$search_field])."%'"; } } $errors = array(); // Normal users only if($mybb->get_input('user_types', MyBB::INPUT_INT) == 1) { $user_types = 1; } // Guests only elseif($mybb->get_input('user_types', MyBB::INPUT_INT) == -1) { $user_types = -1; $search_sql .= " AND a.uid='0'"; } // Users & Guests else { $user_types = 0; } // Username matching if($mybb->input['username']) { $user = get_user_by_username($mybb->input['username']); if(!$user) { if($user_types == 1) { $errors[] = $lang->error_invalid_username; } else { // Don't error if we are searching for guests or users & guests $search_sql .= " AND p.username LIKE '%".$db->escape_string_like($mybb->input['username'])."%'"; } } else { $search_sql .= " AND a.uid='{$user['uid']}'"; } } $forum_cache = cache_forums(); // Searching for attachments in a specific forum, we need to fetch all child forums too if(!empty($mybb->input['forum'])) { if(!is_array($mybb->input['forum'])) { $mybb->input['forum'] = array($mybb->input['forum']); } $fid_in = array(); foreach($mybb->input['forum'] as $fid) { if(!$forum_cache[$fid]) { $errors[] = $lang->error_invalid_forums; break; } $child_forums = get_child_list($fid); $child_forums[] = $fid; $fid_in = array_merge($fid_in, $child_forums); } if(count($fid_in) > 0) { $search_sql .= " AND p.fid IN (".implode(",", $fid_in).")"; } } // LESS THAN or GREATER THAN $direction_fields = array( "dateuploaded" => $mybb->get_input('dateuploaded', MyBB::INPUT_INT), "filesize" => $mybb->get_input('filesize', MyBB::INPUT_INT), "downloads" => $mybb->get_input('downloads', MyBB::INPUT_INT) ); if(!empty($mybb->input['dateuploaded']) && $mybb->request_method == "post") { $direction_fields['dateuploaded'] = TIME_NOW-$direction_fields['dateuploaded']*60*60*24; } if(!empty($mybb->input['filesize']) && $mybb->request_method == "post") { $direction_fields['filesize'] *= 1024; } foreach($direction_fields as $field_name => $field_content) { $direction_field = $field_name."_dir"; if(!empty($mybb->input[$field_name]) && !empty($mybb->input[$direction_field])) { switch($mybb->input[$direction_field]) { case "greater_than": $direction = ">"; break; case "less_than": $direction = "<"; break; default: $direction = "="; } $search_sql .= " AND a.{$field_name}{$direction}'".$field_content."'"; } } if(!$errors) { // Lets fetch out how many results we have $query = $db->query(" SELECT COUNT(a.aid) AS num_results FROM ".TABLE_PREFIX."attachments a LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) WHERE {$search_sql} "); $num_results = $db->fetch_field($query, "num_results"); // No matching results then show an error if(!$num_results) { $errors[] = $lang->error_no_results; } } // Now we fetch the results if there were 100% no errors if(!$errors) { $mybb->input['page'] = $mybb->get_input('page', MyBB::INPUT_INT); if($mybb->input['page']) { $start = ($mybb->input['page'] - 1) * $perpage; } else { $start = 0; $mybb->input['page'] = 1; } switch($mybb->input['sortby']) { case "filesize": $sort_field = "a.filesize"; break; case "downloads": $sort_field = "a.downloads"; break; case "dateuploaded": $sort_field = "a.dateuploaded"; break; case "username": $sort_field = "u.username"; break; default: $sort_field = "a.filename"; $mybb->input['sortby'] = "filename"; } if($mybb->input['order'] != "desc") { $mybb->input['order'] = "asc"; } $plugins->run_hooks("admin_forum_attachments_commit"); $page->add_breadcrumb_item($lang->results); $page->output_header($lang->index_find_attachments); $page->output_nav_tabs($sub_tabs, 'find_attachments'); $form = new Form("index.php?module=forum-attachments&action=delete", "post"); $table = new Table; $table->construct_header($form->generate_check_box('allbox', '1', '', array('class' => 'checkall')), array( 'width' => 1)); $table->construct_header($lang->attachments, array('colspan' => 2)); $table->construct_header($lang->size, array('width' => '10%', 'class' => 'align_center')); $table->construct_header($lang->posted_by, array('width' => '20%', 'class' => 'align_center')); $table->construct_header($lang->thread, array('width' => '25%', 'class' => 'align_center')); $table->construct_header($lang->downloads, array('width' => '10%', 'class' => 'align_center')); $table->construct_header($lang->date_uploaded, array("class" => "align_center")); // Fetch matching attachments $query = $db->query(" SELECT a.*, p.tid, p.fid, t.subject, p.uid, p.username, u.username AS user_username FROM ".TABLE_PREFIX."attachments a LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid) LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=a.uid) WHERE {$search_sql} ORDER BY {$sort_field} {$mybb->input['order']} LIMIT {$start}, {$perpage} "); while($attachment = $db->fetch_array($query)) { build_attachment_row($attachment, $table, true); } // Need to draw pagination for this result set $pagination = ''; if($num_results > $perpage) { $pagination_url = "index.php?module=forum-attachments&results=1"; $pagination_vars = array('perpage', 'sortby', 'order', 'filename', 'mimetype', 'username', 'downloads', 'downloads_dir', 'dateuploaded', 'dateuploaded_dir', 'filesize', 'filesize_dir'); foreach($pagination_vars as $var) { if($mybb->input[$var]) { $pagination_url .= "&{$var}=".urlencode($mybb->input[$var]); } } if(!empty($mybb->input['forum']) && is_array($mybb->input['forum'])) { foreach($mybb->input['forum'] as $fid) { $pagination_url .= "&forum[]=".(int)$fid; } } $pagination = draw_admin_pagination($mybb->input['page'], $perpage, $num_results, $pagination_url); } echo $pagination; $table->output($lang->results); echo $pagination; $buttons[] = $form->generate_submit_button($lang->button_delete_attachments); $form->output_submit_wrapper($buttons); $form->end(); $page->output_footer(); } } $page->output_header($lang->find_attachments); $page->output_nav_tabs($sub_tabs, 'find_attachments'); // If we have any error messages, show them if($errors) { $page->output_inline_error($errors); } $form = new Form("index.php?module=forum-attachments", "post"); $form_container = new FormContainer($lang->find_where); $form_container->output_row($lang->name_contains, $lang->name_contains_desc, $form->generate_text_box('filename', $mybb->get_input('filename'), array('id' => 'filename')), 'filename'); $form_container->output_row($lang->type_contains, "", $form->generate_text_box('mimetype', $mybb->get_input('mimetype'), array('id' => 'mimetype')), 'mimetype'); $form_container->output_row($lang->forum_is, "", $form->generate_forum_select('forum[]', $mybb->get_input('forum', MyBB::INPUT_INT), array('multiple' => true, 'size' => 5, 'id' => 'forum')), 'forum'); $form_container->output_row($lang->username_is, "", $form->generate_text_box('username', htmlspecialchars_uni($mybb->get_input('username')), array('id' => 'username')), 'username'); $form_container->output_row($lang->poster_is, "", $form->generate_select_box('user_types', array('0' => $lang->poster_is_either, '1' => $lang->poster_is_user, '-1' => $lang->poster_is_guest), $mybb->get_input('user_types', MyBB::INPUT_INT), array('id' => 'guests')), 'user_types'); $more_options = array( "less_than" => $lang->more_than, "greater_than" => $lang->less_than ); $greater_options = array( "greater_than" => $lang->greater_than, "is_exactly" => $lang->is_exactly, "less_than" => $lang->less_than ); $form_container->output_row($lang->date_posted_is, "", $form->generate_select_box('dateuploaded_dir', $more_options, $mybb->get_input('dateuploaded_dir'), array('id' => 'dateuploaded_dir'))." ".$form->generate_numeric_field('dateuploaded', $mybb->get_input('dateuploaded', MyBB::INPUT_INT), array('id' => 'dateuploaded', 'min' => 0))." {$lang->days_ago}", 'dateuploaded'); $form_container->output_row($lang->file_size_is, "", $form->generate_select_box('filesize_dir', $greater_options, $mybb->get_input('filesize_dir'), array('id' => 'filesize_dir'))." ".$form->generate_numeric_field('filesize', $mybb->get_input('filesize', MyBB::INPUT_INT), array('id' => 'filesize', 'min' => 0))." {$lang->kb}", 'dateuploaded'); $form_container->output_row($lang->download_count_is, "", $form->generate_select_box('downloads_dir', $greater_options, $mybb->get_input('downloads_dir'), array('id' => 'downloads_dir'))." ".$form->generate_numeric_field('downloads', $mybb->get_input('downloads', MyBB::INPUT_INT), array('id' => 'downloads', 'min' => 0))."", 'dateuploaded'); $form_container->end(); $form_container = new FormContainer($lang->display_options); $sort_options = array( "filename" => $lang->filename, "filesize" => $lang->filesize, "downloads" => $lang->download_count, "dateuploaded" => $lang->date_uploaded, "username" => $lang->post_username ); $sort_directions = array( "asc" => $lang->asc, "desc" => $lang->desc ); $form_container->output_row($lang->sort_results_by, "", $form->generate_select_box('sortby', $sort_options, $mybb->get_input('sortby'), array('id' => 'sortby'))." {$lang->in} ".$form->generate_select_box('order', $sort_directions, $mybb->get_input('order'), array('id' => 'order')), 'sortby'); $form_container->output_row($lang->results_per_page, "", $form->generate_numeric_field('perpage', $perpage, array('id' => 'perpage', 'min' => 1)), 'perpage'); $form_container->end(); $buttons[] = $form->generate_submit_button($lang->button_find_attachments); $form->output_submit_wrapper($buttons); $form->end(); $page->output_footer(); } /** * @param array $attachment * @param DefaultTable $table * @param bool $use_form */ function build_attachment_row($attachment, &$table, $use_form=false) { global $mybb, $form, $lang; $attachment['filename'] = htmlspecialchars_uni($attachment['filename']); // Here we do a bit of detection, we want to automatically check for removal any missing attachments and any not assigned to a post uploaded > 24hours ago // Check if the attachment exists in the file system $checked = false; $title = $cell_class = ''; if(!file_exists(mk_path_abs($mybb->settings['uploadspath'])."/{$attachment['attachname']}")) { $cell_class = "bad_attachment"; $title = $lang->error_not_found; $checked = true; } elseif(!$attachment['pid'] && $attachment['dateuploaded'] < TIME_NOW-60*60*24 && $attachment['dateuploaded'] != 0) { $cell_class = "bad_attachment"; $title = $lang->error_not_attached; $checked = true; } else if(!$attachment['tid'] && $attachment['pid']) { $cell_class = "bad_attachment"; $title = $lang->error_does_not_exist; $checked = true; } else if($attachment['visible'] == 0) { $cell_class = "invisible_attachment"; } if($cell_class) { $cell_class .= " align_center"; } else { $cell_class = "align_center"; } if($use_form == true && is_object($form)) { $table->construct_cell($form->generate_check_box('aids[]', $attachment['aid'], '', array('checked' => $checked))); } $table->construct_cell(get_attachment_icon(get_extension($attachment['filename'])), array('width' => 1)); $table->construct_cell("{$attachment['filename']}"); $table->construct_cell(get_friendly_size($attachment['filesize']), array('class' => $cell_class)); if($attachment['user_username']) { $attachment['username'] = $attachment['user_username']; } $table->construct_cell(build_profile_link(htmlspecialchars_uni($attachment['username']), $attachment['uid'], "_blank"), array("class" => "align_center")); $table->construct_cell("".htmlspecialchars_uni($attachment['subject'])."", array("class" => "align_center")); $table->construct_cell(my_number_format($attachment['downloads']), array("class" => "align_center")); if($attachment['dateuploaded'] > 0) { $date = my_date('relative', $attachment['dateuploaded']); } else { $date = $lang->unknown; } $table->construct_cell($date, array("class" => "align_center")); $table->construct_row(); }