You are here

function filebrowser_page in Filebrowser 6

Same name and namespace in other branches
  1. 5 filebrowser.module \filebrowser_page()
1 string reference to 'filebrowser_page'
filebrowser_menu in ./filebrowser.module

File

./filebrowser.module, line 28

Code

function filebrowser_page($path) {
  global $user;

  // Grab info for the listing we're viewing
  $listing = db_fetch_object(db_query("SELECT location, can_explore FROM {filebrowser} WHERE path = '%s'", $path));
  $listing->can_explore = (bool) (int) $listing->can_explore;
  $listing->path = $path;

  // Grab full Drupal path
  $curr_dir = str_replace($listing->path, $listing->location, $_GET['q']);

  // Are we in a subdirectory?
  $is_subdir = $listing->location != $curr_dir;

  // If we shouldn't be in a subdir, redirect to root_dir
  if ($is_subdir && !$listing->can_explore) {
    drupal_set_message(t('You\'re not allowed to view subdirectories.'), 'error');
    drupal_goto($listing->path);
  }

  // Reflect current directory in title
  drupal_set_title(t('Displaying contents of directory %dir', array(
    '%dir' => $curr_dir,
  )));
  $dir = $curr_dir;
  $files = array();
  $total_size = 0;
  if (is_dir($dir) && ($dh = opendir($dir))) {
    while (($file = readdir($dh)) !== false && is_readable($dir . '/' . $file)) {
      $full_path = $dir . '/' . $file;
      if (is_file($full_path)) {
        $f_stats = stat($full_path);
        if ($f_stats !== false) {
          $total_size += $f_stats['size'];

          // Mark this file new or updated
          $mark_value = MARK_READ;
          if ($user->access < $f_stats['ctime']) {
            $mark_value = MARK_NEW;
          }
          else {
            if ($user->access < $f_stats['mtime']) {
              $mark_value = MARK_UPDATED;
            }
          }
          $files[] = array(
            l($file, $full_path) . theme('mark', $mark_value),
            format_size($f_stats['size']),
          );
        }
        else {
          $files[] = array(
            l($file, $full_path),
            t('Unknown'),
          );
        }
      }
      else {
        if (is_dir($full_path)) {
          if ($listing->can_explore) {
            $dirs[] = $file;
          }
        }
      }
    }
    closedir($dh);
  }
  if ($listing->can_explore) {
    rsort($dirs);
    foreach ($dirs as $dir) {

      // Always remove '.'
      if ($dir == '.') {
        continue;
      }

      // Only allow parent directory link in subdirectories
      if (!$is_subdir && $dir == '..') {
        continue;
      }
      $dir_row = array(
        l($dir, $_GET['q'] . '/' . $dir),
        t('Directory'),
      );
      array_unshift($files, $dir_row);
    }
  }
  $header = array(
    t('Name'),
    t('Size'),
  );
  $output = theme('filebrowser_page', $files, $header, $total_size);
  return $output;
}