[ Index ]

PHP Cross Reference of MyBB 1.8.27

title

Body

[close]

/admin/modules/forum/ -> attachments.php (source)

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


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref