| [ Index ] |
PHP Cross Reference of MyBB 1.6.5 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * MyBB 1.6 4 * Copyright 2010 MyBB Group, All Rights Reserved 5 * 6 * Website: http://mybb.com 7 * License: http://mybb.com/about/license 8 * 9 * $Id: attachments.php 5453 2011-04-21 23:58:33Z jammerx2 $ 10 */ 11 12 // Disallow direct access to this file for security reasons 13 if(!defined("IN_MYBB")) 14 { 15 die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined."); 16 } 17 18 $page->add_breadcrumb_item($lang->attachments, "index.php?module=forum-attachments"); 19 20 if($mybb->input['action'] == "stats" || $mybb->input['action'] == "orphans" || !$mybb->input['action']) 21 { 22 $sub_tabs['find_attachments'] = array( 23 'title' => $lang->find_attachments, 24 'link' => "index.php?module=forum-attachments", 25 'description' => $lang->find_attachments_desc 26 ); 27 28 $sub_tabs['find_orphans'] = array( 29 'title' => $lang->find_orphans, 30 'link' => "index.php?module=forum-attachments&action=orphans", 31 'description' => $lang->find_orphans_desc 32 ); 33 34 $sub_tabs['stats'] = array( 35 'title' => $lang->attachment_stats, 36 'link' => "index.php?module=forum-attachments&action=stats", 37 'description' => $lang->attachment_stats_desc 38 ); 39 } 40 41 $plugins->run_hooks("admin_forum_attachments_begin"); 42 43 if($mybb->input['action'] == "delete") 44 { 45 $plugins->run_hooks("admin_forum_attachments_delete"); 46 47 if(!is_array($mybb->input['aids'])) 48 { 49 $mybb->input['aids'] = array(intval($mybb->input['aid'])); 50 } 51 else 52 { 53 $mybb->input['aids'] = array_map("intval", $mybb->input['aids']); 54 } 55 56 if(count($mybb->input['aids']) < 1) 57 { 58 flash_message($lang->error_nothing_selected, 'error'); 59 admin_redirect("index.php?module=forum-attachments"); 60 } 61 62 if($mybb->request_method == "post") 63 { 64 require_once MYBB_ROOT."inc/functions_upload.php"; 65 66 $query = $db->simple_select("attachments", "aid,pid,posthash, filename", "aid IN (".implode(",", $mybb->input['aids']).")"); 67 while($attachment = $db->fetch_array($query)) 68 { 69 if(!$attachment['pid']) 70 { 71 remove_attachment(null, $attachment['posthash'], $attachment['aid']); 72 // Log admin action 73 log_admin_action($attachment['aid'], $attachment['filename']); 74 } 75 else 76 { 77 remove_attachment($attachment['pid'], null, $attachment['aid']); 78 // Log admin action 79 log_admin_action($attachment['aid'], $attachment['filename'], $attachment['pid']); 80 } 81 } 82 83 $plugins->run_hooks("admin_forum_attachments_delete_commit"); 84 85 flash_message($lang->success_deleted, 'success'); 86 admin_redirect("index.php?module=forum-attachments"); 87 } 88 else 89 { 90 foreach($mybb->input['aids'] as $aid) 91 { 92 $aids .= "&aids[]=$aid"; 93 } 94 $page->output_confirm_action("index.php?module=forum-attachments&action=delete&aids={$aids}", $lang->confirm_delete); 95 } 96 } 97 98 if($mybb->input['action'] == "stats") 99 { 100 $plugins->run_hooks("admin_forum_attachments_stats"); 101 102 $query = $db->simple_select("attachments", "COUNT(*) AS total_attachments, SUM(filesize) as disk_usage, SUM(downloads*filesize) as bandwidthused", "visible='1'"); 103 $attachment_stats = $db->fetch_array($query); 104 105 $page->add_breadcrumb_item($lang->stats); 106 $page->output_header($lang->stats_attachment_stats); 107 108 $page->output_nav_tabs($sub_tabs, 'stats'); 109 110 if($attachment_stats['total_attachments'] == 0) 111 { 112 $page->output_inline_error(array($lang->error_no_attachments)); 113 $page->output_footer(); 114 exit; 115 } 116 117 $table = new Table; 118 119 $table->construct_cell($lang->num_uploaded, array('width' => '25%')); 120 $table->construct_cell(my_number_format($attachment_stats['total_attachments']), array('width' => '25%')); 121 $table->construct_cell($lang->space_used, array('width' => '200')); 122 $table->construct_cell(get_friendly_size($attachment_stats['disk_usage']), array('width' => '200')); 123 $table->construct_row(); 124 125 $table->construct_cell($lang->bandwidth_used, array('width' => '25%')); 126 $table->construct_cell(get_friendly_size(round($attachment_stats['bandwidthused'])), array('width' => '25%')); 127 $table->construct_cell($lang->average_size, array('width' => '25%')); 128 $table->construct_cell(get_friendly_size(round($attachment_stats['disk_usage']/$attachment_stats['total_attachments'])), array('width' => '25%')); 129 $table->construct_row(); 130 131 $table->output($lang->general_stats); 132 133 // Fetch the most popular attachments 134 $table = new Table; 135 $table->construct_header($lang->attachments, array('colspan' => 2)); 136 $table->construct_header($lang->size, array('width' => '10%', 'class' => 'align_center')); 137 $table->construct_header($lang->posted_by, array('width' => '20%', 'class' => 'align_center')); 138 $table->construct_header($lang->thread, array('width' => '25%', 'class' => 'align_center')); 139 $table->construct_header($lang->downloads, array('width' => '10%', 'class' => 'align_center')); 140 $table->construct_header($lang->date_uploaded, array("class" => "align_center")); 141 142 $query = $db->query(" 143 SELECT a.*, p.tid, p.fid, t.subject, p.uid, p.username, u.username AS user_username 144 FROM ".TABLE_PREFIX."attachments a 145 LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) 146 LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid) 147 LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=a.uid) 148 ORDER BY a.downloads DESC 149 LIMIT 5 150 "); 151 while($attachment = $db->fetch_array($query)) 152 { 153 build_attachment_row($attachment, $table); 154 } 155 $table->output($lang->popular_attachments); 156 157 // Fetch the largest attachments 158 $table = new Table; 159 $table->construct_header($lang->attachments, array('colspan' => 2)); 160 $table->construct_header($lang->size, array('width' => '10%', 'class' => 'align_center')); 161 $table->construct_header($lang->posted_by, array('width' => '20%', 'class' => 'align_center')); 162 $table->construct_header($lang->thread, array('width' => '25%', 'class' => 'align_center')); 163 $table->construct_header($lang->downloads, array('width' => '10%', 'class' => 'align_center')); 164 $table->construct_header($lang->date_uploaded, array("class" => "align_center")); 165 166 $query = $db->query(" 167 SELECT a.*, p.tid, p.fid, t.subject, p.uid, p.username, u.username AS user_username 168 FROM ".TABLE_PREFIX."attachments a 169 LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) 170 LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid) 171 LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=a.uid) 172 ORDER BY a.filesize DESC 173 LIMIT 5 174 "); 175 while($attachment = $db->fetch_array($query)) 176 { 177 build_attachment_row($attachment, $table); 178 } 179 $table->output($lang->largest_attachments); 180 181 // Fetch users who've uploaded the most attachments 182 $table = new Table; 183 $table->construct_header($lang->username); 184 $table->construct_header($lang->total_size, array('width' => '20%', 'class' => 'align_center')); 185 186 switch($db->type) 187 { 188 case "pgsql": 189 $query = $db->query(" 190 SELECT a.*, u.uid AS useruid, u.username, SUM(a.filesize) as totalsize 191 FROM ".TABLE_PREFIX."attachments a 192 LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=a.uid) 193 GROUP BY ".$db->build_fields_string("attachments", "a.").",u.uid,u.username 194 ORDER BY totalsize DESC 195 LIMIT 5 196 "); 197 break; 198 default: 199 $query = $db->query(" 200 SELECT a.*, u.uid AS useruid, u.username, SUM(a.filesize) as totalsize 201 FROM ".TABLE_PREFIX."attachments a 202 LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=a.uid) 203 GROUP BY a.uid 204 ORDER BY totalsize DESC 205 LIMIT 5 206 "); 207 } 208 while($user = $db->fetch_array($query)) 209 { 210 if(!$user['useruid']) 211 { 212 $user['username'] = $lang->na; 213 } 214 $table->construct_cell(build_profile_link($user['username'], $user['useruid'], "_blank")); 215 $table->construct_cell("<a href=\"index.php?module=forum-attachments&results=1&username=".urlencode($user['username'])."\" target=\"_blank\">".get_friendly_size($user['totalsize'])."</a>", array('class' => 'align_center')); 216 $table->construct_row(); 217 } 218 $table->output($lang->users_diskspace); 219 220 $page->output_footer(); 221 } 222 223 if($mybb->input['action'] == "delete_orphans" && $mybb->request_method == "post") 224 { 225 $plugins->run_hooks("admin_forum_attachments_delete_orphans"); 226 227 // Deleting specific attachments from uploads directory 228 if(is_array($mybb->input['orphaned_files'])) 229 { 230 function clean_filename($string) 231 { 232 return str_replace(array(".."), "", $string); 233 } 234 $mybb->input['orphaned_files'] = array_map("clean_filename", $mybb->input['orphaned_files']); 235 foreach($mybb->input['orphaned_files'] as $file) 236 { 237 if(!@unlink(MYBB_ROOT.$mybb->settings['uploadspath']."/".$file)) 238 { 239 $error = true; 240 } 241 } 242 } 243 244 // Deleting physical attachments which exist in database 245 if(is_array($mybb->input['orphaned_attachments'])) 246 { 247 $mybb->input['orphaned_attachments'] = array_map("intval", $mybb->input['orphaned_attachments']); 248 require_once MYBB_ROOT."inc/functions_upload.php"; 249 250 $query = $db->simple_select("attachments", "aid,pid,posthash", "aid IN (".implode(",", $mybb->input['orphaned_attachments']).")"); 251 while($attachment = $db->fetch_array($query)) 252 { 253 if(!$attachment['pid']) 254 { 255 remove_attachment(null, $attachment['posthash'], $attachment['aid']); 256 } 257 else 258 { 259 remove_attachment($attachment['pid'], null, $attachment['aid']); 260 } 261 } 262 } 263 264 $plugins->run_hooks("admin_forum_attachments_delete_orphans_commit"); 265 266 // Log admin action 267 log_admin_action(); 268 269 if($error == true) 270 { 271 flash_message($lang->error_not_all_removed, 'error'); 272 } 273 else 274 { 275 flash_message($lang->success_orphan_deleted, 'success'); 276 } 277 admin_redirect("index.php?module=forum-attachments"); 278 } 279 280 if($mybb->input['action'] == "orphans") 281 { 282 $plugins->run_hooks("admin_forum_attachments_orphans"); 283 284 // Oprhans are defined as: 285 // - Uploaded files in the uploads directory that don't exist in the database 286 // - Attachments for which the uploaded file is missing 287 // - Attachments for which the thread or post has been deleted 288 // - Files uploaded > 24h ago not attached to a real post 289 290 // This process is quite intensive so we split it up in to 2 steps, one which scans the file system and the other which scans the database. 291 292 // Finished second step, show results 293 if($mybb->input['step'] == 3) 294 { 295 $plugins->run_hooks("admin_forum_attachments_step3"); 296 297 $reults = 0; 298 // Incoming attachments which exist as files but not in database 299 if($mybb->input['bad_attachments']) 300 { 301 $bad_attachments = unserialize($mybb->input['bad_attachments']); 302 $results = count($bad_attachments); 303 } 304 305 $aids = array(); 306 if($mybb->input['missing_attachment_files']) 307 { 308 $missing_attachment_files = unserialize($mybb->input['missing_attachment_files']); 309 $aids = array_merge($aids, $missing_attachment_files); 310 } 311 312 if($mybb->input['missing_threads']) 313 { 314 $missing_threads = unserialize($mybb->input['missing_threads']); 315 $aids = array_merge($aids, $missing_threads); 316 } 317 318 if($mybb->input['incomplete_attachments']) 319 { 320 $incomplete_attachments = unserialize($mybb->input['incomplete_attachments']); 321 $aids = array_merge($aids, $incomplete_attachments); 322 } 323 324 foreach($aids as $key => $aid) 325 { 326 $aids[$key] = intval($aid); 327 } 328 329 $results += count($aids); 330 331 if($results == 0) 332 { 333 flash_message($lang->success_no_orphans, 'success'); 334 admin_redirect("index.php?module=forum-attachments"); 335 } 336 337 $page->output_header($lang->orphan_results); 338 $page->output_nav_tabs($sub_tabs, 'find_orphans'); 339 340 $form = new Form("index.php?module=forum-attachments&action=delete_orphans", "post"); 341 342 $table = new Table; 343 $table->construct_header($form->generate_check_box('checkall', '1', '', array('class' => 'checkall')), array( 'width' => 1)); 344 $table->construct_header($lang->size_attachments, array('colspan' => 2)); 345 $table->construct_header($lang->reason_orphaned, array('width' => '20%', 'class' => 'align_center')); 346 $table->construct_header($lang->date_uploaded, array("class" => "align_center")); 347 348 if(is_array($bad_attachments)) 349 { 350 foreach($bad_attachments as $file) 351 { 352 $file_path = MYBB_ROOT.$mybb->settings['uploadspath']."/".$file; 353 $filesize = get_friendly_size(filesize($file_path)); 354 $table->construct_cell($form->generate_check_box('orphaned_files[]', $file, '', array('checked' => true))); 355 $table->construct_cell(get_attachment_icon(get_extension($attachment['filename'])), array('width' => 1)); 356 $table->construct_cell("<span class=\"float_right\">{$filesize}</span>{$file}"); 357 $table->construct_cell($lang->reason_not_in_table, array('class' => 'align_center')); 358 $table->construct_cell(my_date($mybb->settings['dateformat'], filemtime($file_path)).", ".my_date($mybb->settings['timeformat'], filemtime($file_path)), array('class' => 'align_center')); 359 $table->construct_row(); 360 } 361 } 362 363 if(count($aids) > 0) 364 { 365 $query = $db->simple_select("attachments", "*", "aid IN (".implode(",", $aids).")"); 366 while($attachment = $db->fetch_array($query)) 367 { 368 if($missing_attachment_files[$attachment['aid']]) 369 { 370 $reason = $lang->reason_file_missing; 371 } 372 else if($missing_threads[$attachment['aid']]) 373 { 374 $reason = $lang->reason_thread_deleted; 375 } 376 else if($incomplete_attachments[$attachment['aid']]) 377 { 378 $reason = $lang->reason_post_never_made; 379 } 380 $table->construct_cell($form->generate_check_box('orphaned_attachments[]', $attachment['aid'], '', array('checked' => true))); 381 $table->construct_cell(get_attachment_icon(get_extension($attachment['filename'])), array('width' => 1)); 382 $table->construct_cell("<span class=\"float_right\">".get_friendly_size($attachment['filesize'])."</span><a href=\"../attachment.php?aid={$attachment['aid']}\" target=\"_blank\">{$attachment['filename']}</a>", array('class' => $cell_class)); 383 $table->construct_cell($reason, array('class' => 'align_center')); 384 if($attachment['dateuploaded']) 385 { 386 $table->construct_cell(my_date($mybb->settings['dateformat'], $attachment['dateuploaded']).", ".my_date($mybb->settings['timeformat'], $attachment['dateuploaded']), array('class' => 'align_center')); 387 } 388 else 389 { 390 $table->construct_cell($lang->unknown, array('class' => 'align_center')); 391 } 392 $table->construct_row(); 393 } 394 } 395 396 $table->output("{$lang->orphan_attachments_search} - {$results} {$lang->results}"); 397 398 $buttons[] = $form->generate_submit_button($lang->button_delete_orphans); 399 $form->output_submit_wrapper($buttons); 400 $form->end(); 401 $page->output_footer(); 402 } 403 404 // Running second step - scan the database 405 else if($mybb->input['step'] == 2) 406 { 407 $plugins->run_hooks("admin_forum_attachments_orphans_step2"); 408 409 $page->output_header("{$lang->orphan_attachments_search} - {$lang->step2}"); 410 411 $page->output_nav_tabs($sub_tabs, 'find_orphans'); 412 echo "<h3>{$lang->step2of2}</h3>"; 413 echo "<p class=\"align_center\">{$lang->step2of2_line1}</p>"; 414 echo "<p class=\"align_center\">{$lang->step_line2}</p>"; 415 echo "<p class=\"align_center\"><img src=\"styles/{$page->style}/images/spinner_big.gif\" alt=\"Scanning..\" id=\"spinner\" /></p>"; 416 417 $page->output_footer(false); 418 flush(); 419 420 $missing_attachment_files = array(); 421 $missing_threads = array(); 422 $incomplete_attachments = array(); 423 424 $query = $db->query(" 425 SELECT a.*, a.pid AS attachment_pid, p.pid 426 FROM ".TABLE_PREFIX."attachments a 427 LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) 428 ORDER BY a.aid"); 429 while($attachment = $db->fetch_array($query)) 430 { 431 // Check if the attachment exists in the file system 432 if(!file_exists(MYBB_ROOT.$mybb->settings['uploadspath']."/{$attachment['attachname']}")) 433 { 434 $missing_attachment_files[$attachment['aid']] = $attachment['aid']; 435 } 436 // Check if the thread/post for this attachment is missing 437 else if(!$attachment['pid'] && $attachment['attachment_pid']) 438 { 439 $missing_threads[$attachment['aid']] = $attachment['aid']; 440 } 441 // Check if the attachment was uploaded > 24 hours ago but not assigned to a thread 442 else if(!$attachment['attachment_pid'] && $attachment['dateuploaded'] < TIME_NOW-60*60*24 && $attachment['dateuploaded'] != 0) 443 { 444 $incomplete_attachments[$attachment['aid']] = $attachment['aid']; 445 } 446 } 447 448 // Now send the user to the final page 449 $form = new Form("index.php?module=forum-attachments&action=orphans&step=3", "post", "redirect_form", 0, ""); 450 // Scan complete 451 if($mybb->input['bad_attachments']) 452 { 453 echo $form->generate_hidden_field("bad_attachments", $mybb->input['bad_attachments']); 454 } 455 if(is_array($missing_attachment_files) && count($missing_attachment_files) > 0) 456 { 457 $missing_attachment_files = serialize($missing_attachment_files); 458 echo $form->generate_hidden_field("missing_attachment_files", $missing_attachment_files); 459 } 460 if(is_array($missing_threads) && count($missing_threads) > 0) 461 { 462 $missing_threads = serialize($missing_threads); 463 echo $form->generate_hidden_field("missing_threads", $missing_threads); 464 } 465 if(is_array($incomplete_attachments) && count($incomplete_attachments) > 0) 466 { 467 $incomplete_attachments = serialize($incomplete_attachments); 468 echo $form->generate_hidden_field("incomplete_attachments", $incomplete_attachments); 469 } 470 $form->end(); 471 echo "<script type=\"text/javascript\">Event.observe(window, 'load', function() { 472 window.setTimeout( 473 function() { 474 $('redirect_form').submit(); 475 }, 100 476 ); 477 });</script>"; 478 exit; 479 } 480 // Running first step, scan the file system 481 else 482 { 483 $plugins->run_hooks("admin_forum_attachments_orphans_step1"); 484 485 function scan_attachments_directory($dir="") 486 { 487 global $db, $mybb, $bad_attachments, $attachments_to_check; 488 489 $real_dir = MYBB_ROOT.$mybb->settings['uploadspath']; 490 $false_dir = ""; 491 if($dir) 492 { 493 $real_dir .= "/".$dir; 494 $false_dir = $dir."/"; 495 } 496 497 if($dh = opendir($real_dir)) 498 { 499 while(false !== ($file = readdir($dh))) 500 { 501 if($file == "." || $file == ".." || $file == ".svn") 502 { 503 continue; 504 } 505 506 if(is_dir($real_dir.'/'.$file)) 507 { 508 scan_attachments_directory($false_dir.$file); 509 } 510 else if(my_substr($file, -7, 7) == ".attach") 511 { 512 $attachments_to_check["$false_dir$file"] = $false_dir.$file; 513 // In allotments of 20, query the database for these attachments 514 if(count($attachments_to_check) >= 20) 515 { 516 $attachments_to_check = array_map(array($db, "escape_string"), $attachments_to_check); 517 $attachment_names = "'".implode("','", $attachments_to_check)."'"; 518 $query = $db->simple_select("attachments", "aid, attachname", "attachname IN ($attachment_names)"); 519 while($attachment = $db->fetch_array($query)) 520 { 521 unset($attachments_to_check[$attachment['attachname']]); 522 } 523 524 // Now anything left is bad! 525 if(count($attachments_to_check) > 0) 526 { 527 if($bad_attachments) 528 { 529 $bad_attachments = @array_merge($bad_attachments, $attachments_to_check); 530 } 531 else 532 { 533 $bad_attachments = $attachments_to_check; 534 } 535 } 536 $attachments_to_check = array(); 537 } 538 } 539 } 540 closedir($dh); 541 // Any reamining to check? 542 if(count($attachments_to_check) > 0) 543 { 544 $attachments_to_check = array_map(array($db, "escape_string"), $attachments_to_check); 545 $attachment_names = "'".implode("','", $attachments_to_check)."'"; 546 $query = $db->simple_select("attachments", "aid, attachname", "attachname IN ($attachment_names)"); 547 while($attachment = $db->fetch_array($query)) 548 { 549 unset($attachments_to_check[$attachment['attachname']]); 550 } 551 552 // Now anything left is bad! 553 if(count($attachments_to_check) > 0) 554 { 555 if($bad_attachments) 556 { 557 $bad_attachments = @array_merge($bad_attachments, $attachments_to_check); 558 } 559 else 560 { 561 $bad_attachments = $attachments_to_check; 562 } 563 } 564 } 565 } 566 } 567 568 $page->output_header("{$lang->orphan_attachments_search} - {$lang->step1}"); 569 570 $page->output_nav_tabs($sub_tabs, 'find_orphans'); 571 echo "<h3>{$lang->step1of2}</h3>"; 572 echo "<p class=\"align_center\">{$lang->step1of2_line1}</p>"; 573 echo "<p class=\"align_center\">{$lang->step_line2}</p>"; 574 echo "<p class=\"align_center\"><img src=\"styles/{$page->style}/images/spinner_big.gif\" alt=\"Scanning..\" id=\"spinner\" /></p>"; 575 576 $page->output_footer(false); 577 578 flush(); 579 580 scan_attachments_directory(); 581 global $bad_attachments; 582 583 $form = new Form("index.php?module=forum-attachments&action=orphans&step=2", "post", "redirect_form", 0, ""); 584 // Scan complete 585 if(is_array($bad_attachments) && count($bad_attachments) > 0) 586 { 587 $bad_attachments = serialize($bad_attachments); 588 echo $form->generate_hidden_field("bad_attachments", $bad_attachments); 589 } 590 $form->end(); 591 echo "<script type=\"text/javascript\">Event.observe(window, 'load', function() { 592 window.setTimeout( 593 function() { 594 $('redirect_form').submit(); 595 }, 100 596 ); 597 });</script>"; 598 exit; 599 } 600 } 601 602 if(!$mybb->input['action']) 603 { 604 $plugins->run_hooks("admin_forum_attachments_start"); 605 606 if($mybb->request_method == "post" || $mybb->input['results'] == 1) 607 { 608 $search_sql = '1=1'; 609 610 // Build the search SQL for users 611 612 // List of valid LIKE search fields 613 $user_like_fields = array("filename", "filetype"); 614 foreach($user_like_fields as $search_field) 615 { 616 if($mybb->input[$search_field]) 617 { 618 $search_sql .= " AND a.{$search_field} LIKE '%".$db->escape_string_like($mybb->input[$search_field])."%'"; 619 } 620 } 621 622 // Username matching 623 if($mybb->input['username']) 624 { 625 $query = $db->simple_select("users", "uid", "LOWER(username)='".$db->escape_string(my_strtolower($mybb->input['username']))."'"); 626 $user = $db->fetch_array($query); 627 if(!$user['uid']) 628 { 629 $errors[] = $lang->error_invalid_username; 630 } 631 else 632 { 633 $search_sql .= " AND a.uid='{$user['uid']}'"; 634 } 635 } 636 637 $forum_cache = cache_forums(); 638 639 // Searching for attachments in a specific forum, we need to fetch all child forums too 640 if($mybb->input['forum']) 641 { 642 if(!is_array($mybb->input['forum'])) 643 { 644 $mybb->input['forum'] = array($mybb->input['forum']); 645 } 646 647 $fid_in = array(); 648 foreach($mybb->input['forum'] as $fid) 649 { 650 if(!$forum_cache[$fid]) 651 { 652 $errors[] = $lang->error_invalid_forums; 653 break; 654 } 655 $child_forums = get_child_list($fid); 656 $child_forums[] = $fid; 657 $fid_in = array_merge($fid_in, $child_forums); 658 } 659 660 if(count($fid_in) > 0) 661 { 662 $search_sql .= " AND p.fid IN (".implode(",", $fid_in).")"; 663 } 664 } 665 666 // LESS THAN or GREATER THAN 667 if($mybb->input['dateuploaded'] && $mybb->request_method == "post") 668 { 669 $mybb->input['dateuploaded'] = TIME_NOW-$mybb->input['dateuploaded']*60*60*24; 670 } 671 if($mybb->input['filesize'] && $mybb->request_method == "post") 672 { 673 $mybb->input['filesize'] *= 1024; 674 } 675 676 $direction_fields = array("dateuploaded", "filesize", "downloads"); 677 foreach($direction_fields as $search_field) 678 { 679 $direction_field = $search_field."_dir"; 680 if($mybb->input[$search_field] && $mybb->input[$direction_field]) 681 { 682 switch($mybb->input[$direction_field]) 683 { 684 case "greater_than": 685 $direction = ">"; 686 break; 687 case "less_than": 688 $direction = "<"; 689 break; 690 default: 691 $direction = "="; 692 } 693 $search_sql .= " AND a.{$search_field}{$direction}'".$db->escape_string($mybb->input[$search_field])."'"; 694 } 695 } 696 if(!$errors) 697 { 698 // Lets fetch out how many results we have 699 $query = $db->query(" 700 SELECT COUNT(a.aid) AS num_results 701 FROM ".TABLE_PREFIX."attachments a 702 LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) 703 WHERE {$search_sql} 704 "); 705 $num_results = $db->fetch_field($query, "num_results"); 706 707 // No matching results then show an error 708 if(!$num_results) 709 { 710 $errors[] = $lang->error_no_results; 711 } 712 } 713 714 // Now we fetch the results if there were 100% no errors 715 if(!$errors) 716 { 717 if(!$mybb->input['perpage']) 718 { 719 $mybb->input['perpage'] = 20; 720 } 721 $mybb->input['perpage'] = intval($mybb->input['perpage']); 722 723 $mybb->input['page'] = intval($mybb->input['page']); 724 if($mybb->input['page']) 725 { 726 $start = ($mybb->input['page'] - 1) * $mybb->input['perpage']; 727 } 728 else 729 { 730 $start = 0; 731 $mybb->input['page'] = 1; 732 } 733 734 switch($mybb->input['sortby']) 735 { 736 case "lastactive": 737 $sort_field = "a.filesize"; 738 break; 739 case "downloads": 740 $sort_field = "a.downloads"; 741 break; 742 case "dateuploaded": 743 $sort_field = "a.dateuploaded"; 744 break; 745 case "username": 746 $sort_field = "u.username"; 747 break; 748 default: 749 $sort_field = "a.filename"; 750 $mybb->input['sortby'] = "filename"; 751 } 752 753 if($mybb->input['sortorder'] != "desc") 754 { 755 $mybb->input['sortorder'] = "asc"; 756 } 757 758 $page->add_breadcrumb_item($lang->results); 759 $page->output_header($lang->index_find_attachments); 760 761 $page->output_nav_tabs($sub_tabs, 'find_attachments'); 762 763 $form = new Form("index.php?module=forum-attachments&action=delete", "post"); 764 765 $table = new Table; 766 $table->construct_header($form->generate_check_box('checkall', '1', '', array('class' => 'checkall')), array( 'width' => 1)); 767 $table->construct_header($lang->attachments, array('colspan' => 2)); 768 $table->construct_header($lang->size, array('width' => '10%', 'class' => 'align_center')); 769 $table->construct_header($lang->posted_by, array('width' => '20%', 'class' => 'align_center')); 770 $table->construct_header($lang->thread, array('width' => '25%', 'class' => 'align_center')); 771 $table->construct_header($lang->downloads, array('width' => '10%', 'class' => 'align_center')); 772 $table->construct_header($lang->date_uploaded, array("class" => "align_center")); 773 774 // Fetch matching attachments 775 $query = $db->query(" 776 SELECT a.*, p.tid, p.fid, t.subject, p.uid, p.username, u.username AS user_username 777 FROM ".TABLE_PREFIX."attachments a 778 LEFT JOIN ".TABLE_PREFIX."posts p ON (p.pid=a.pid) 779 LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid) 780 LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=a.uid) 781 WHERE {$search_sql} 782 ORDER BY {$sort_field} {$mybb->input['sortorder']} 783 LIMIT {$start}, {$mybb->input['perpage']} 784 "); 785 while($attachment = $db->fetch_array($query)) 786 { 787 build_attachment_row($attachment, $table, true); 788 } 789 790 // Need to draw pagination for this result set 791 if($num_results > $mybb->input['perpage']) 792 { 793 $pagination_url = "index.php?module=forum-attachments&results=1"; 794 $pagination_vars = array('filename', 'mimetype', 'username', 'fid', 'downloads', 'downloads_dir', 'dateuploaded', 'dateuploaded_dir', 'filesize', 'filesize_dir'); 795 foreach($pagination_vars as $var) 796 { 797 if($mybb->input[$var]) 798 { 799 $pagination_url .= "&{$var}=".urlencode($mybb->input[$var]); 800 } 801 } 802 $pagination = draw_admin_pagination($mybb->input['page'], $mybb->input['perpage'], $num_results, $pagination_url); 803 } 804 805 echo $pagination; 806 $table->output($lang->results); 807 echo $pagination; 808 809 $buttons[] = $form->generate_submit_button($lang->button_delete_attachments); 810 811 $form->output_submit_wrapper($buttons); 812 $form->end(); 813 814 $page->output_footer(); 815 } 816 } 817 818 $page->output_header($lang->find_attachments); 819 820 $page->output_nav_tabs($sub_tabs, 'find_attachments'); 821 822 // If we have any error messages, show them 823 if($errors) 824 { 825 $page->output_inline_error($errors); 826 } 827 828 $form = new Form("index.php?module=forum-attachments", "post"); 829 830 $form_container = new FormContainer($lang->find_where); 831 $form_container->output_row($lang->name_contains, $lang->name_contains_desc, $form->generate_text_box('filename', $mybb->input['filename'], array('id' => 'filename')), 'filename'); 832 $form_container->output_row($lang->type_contains, "", $form->generate_text_box('mimetype', $mybb->input['mimetype'], array('id' => 'mimetype')), 'mimetype'); 833 $form_container->output_row($lang->forum_is, "", $form->generate_forum_select('forum[]', $mybb->input['forum'], array('multiple' => true, 'size' => 5, 'id' => 'forum')), 'forum'); 834 $form_container->output_row($lang->username_is, "", $form->generate_text_box('username', $mybb->input['username'], array('id' => 'username')), 'username'); 835 836 $more_options = array( 837 "less_than" => $lang->more_than, 838 "greater_than" => $lang->less_than 839 ); 840 841 $greater_options = array( 842 "greater_than" => $lang->greater_than, 843 "is_exactly" => $lang->is_exactly, 844 "less_than" => $lang->less_than 845 ); 846 847 $form_container->output_row($lang->date_posted_is, "", $form->generate_select_box('dateuploaded_dir', $more_options, $mybb->input['dateuploaded_dir'], array('id' => 'dateuploaded_dir'))." ".$form->generate_text_box('dateuploaded', $mybb->input['dateuploaded'], array('id' => 'dateuploaded'))." {$lang->days_ago}", 'dateuploaded'); 848 $form_container->output_row($lang->file_size_is, "", $form->generate_select_box('filesize_dir', $greater_options, $mybb->input['filesize_dir'], array('id' => 'filesize_dir'))." ".$form->generate_text_box('filesize', $mybb->input['filesize'], array('id' => 'filesize'))." {$lang->kb}", 'dateuploaded'); 849 $form_container->output_row($lang->download_count_is, "", $form->generate_select_box('downloads_dir', $greater_options, $mybb->input['downloads_dir'], array('id' => 'downloads_dir'))." ".$form->generate_text_box('downloads', $mybb->input['downloads'], array('id' => 'downloads'))."", 'dateuploaded'); 850 $form_container->end(); 851 852 $form_container = new FormContainer($lang->display_options); 853 $sort_options = array( 854 "filename" => $lang->filename, 855 "filesize" => $lang->filesize, 856 "downloads" => $lang->download_count, 857 "dateuploaded" => $lang->date_uploaded, 858 "username" => $lang->post_username 859 ); 860 $sort_directions = array( 861 "asc" => $lang->asc, 862 "desc" => $lang->desc 863 ); 864 $form_container->output_row($lang->sort_results_by, "", $form->generate_select_box('sortby', $sort_options, $mybb->input['sortby'], array('id' => 'sortby'))." {$lang->in} ".$form->generate_select_box('order', $sort_directions, $mybb->input['order'], array('id' => 'order')), 'sortby'); 865 $form_container->output_row($lang->results_per_page, "", $form->generate_text_box('perpage', $mybb->input['perpage'], array('id' => 'perpage')), 'perpage'); 866 $form_container->end(); 867 868 $buttons[] = $form->generate_submit_button($lang->button_find_attachments); 869 $form->output_submit_wrapper($buttons); 870 $form->end(); 871 872 $page->output_footer(); 873 } 874 875 function build_attachment_row($attachment, &$table, $use_form=false) 876 { 877 global $mybb, $form; 878 $attachment['filename'] = htmlspecialchars($attachment['filename']); 879 880 // 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 881 // Check if the attachment exists in the file system 882 $checked = false; 883 $title = $cell_class = ''; 884 if(!file_exists(MYBB_ROOT.$mybb->settings['uploadspath']."/{$attachment['attachname']}")) 885 { 886 $cell_class = "bad_attachment"; 887 $title = $lang->error_not_found; 888 $checked = true; 889 } 890 elseif(!$attachment['pid'] && $attachment['dateuploaded'] < TIME_NOW-60*60*24 && $attachment['dateuploaded'] != 0) 891 { 892 $cell_class = "bad_attachment"; 893 $title = $lang->error_not_attached; 894 $checked = true; 895 } 896 else if(!$attachment['tid'] && $attachment['pid']) 897 { 898 $cell_class = "bad_attachment"; 899 $title = $lang->error_does_not_exist; 900 $checked = true; 901 } 902 else if($attachment['visible'] == 0) 903 { 904 $cell_class = "invisible_attachment"; 905 } 906 907 if($cell_class) 908 { 909 $cell_class .= " align_center"; 910 } 911 else 912 { 913 $cell_class = "align_center"; 914 } 915 916 if($use_form == true && is_object($form)) 917 { 918 $table->construct_cell($form->generate_check_box('aids[]', $attachment['aid'], '', array('checked' => $checked))); 919 } 920 $table->construct_cell(get_attachment_icon(get_extension($attachment['filename'])), array('width' => 1)); 921 $table->construct_cell("<a href=\"../attachment.php?aid={$attachment['aid']}\" target=\"_blank\">{$attachment['filename']}</a>"); 922 $table->construct_cell(get_friendly_size($attachment['filesize']), array('class' => $cell_class)); 923 924 if($attachment['user_username']) 925 { 926 $attachment['username'] = $attachment['username']; 927 } 928 $table->construct_cell(build_profile_link($attachment['username'], $attachment['uid'], "_blank"), array("class" => "align_center")); 929 $table->construct_cell("<a href=\"../".get_post_link($attachment['pid'])."\" target=\"_blank\">".htmlspecialchars($attachment['subject'])."</a>", array("class" => "align_center")); 930 $table->construct_cell(my_number_format($attachment['downloads']), array("class" => "align_center")); 931 if($attachment['dateuploaded'] > 0) 932 { 933 $date = my_date($mybb->settings['dateformat'], $attachment['dateuploaded']).", ".my_date($mybb->settings['timeformat'], $attachment['dateuploaded']); 934 } 935 else 936 { 937 $date = $lang->unknown; 938 } 939 $table->construct_cell($date, array("class" => "align_center")); 940 $table->construct_row(); 941 } 942 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Dec 11 14:16:27 2011 | Cross-referenced by PHPXref 0.7.1 |