You are here

function filebrowser_get_list in Filebrowser 5

Returns a list of files in a subfolder under the admin specified filebrowser root. File system details (size, last modification) is added, plus a metafile is parsed to gather more information, if available.

1 call to filebrowser_get_list()
filebrowser_page in ./filebrowser.module
Prints a folder layout

File

./filebrowser.module, line 170

Code

function filebrowser_get_list($subfolder = '') {
  global $base_path;
  $folder = filebrowser_safe_folder($subfolder);
  $inroot = $folder == variable_get('filebrowser_root', '');

  // Signal error in case of bogus directory name
  if (!(file_exists($folder) && is_dir($folder) && ($dir = opendir($folder)))) {
    return FALSE;
  }

  // Collect folders and files separately and check for a metainfo file
  $files = $folders = array();
  $infofile = FALSE;
  while (($entry = readdir($dir)) !== FALSE) {
    if (is_dir("{$folder}/{$entry}")) {

      // Skip version control system folders
      if (!in_array($entry, array(
        ".svn",
        "CVS",
      ))) {
        $folders[] = $entry;
      }
    }
    else {

      // Skip .htaccess file from being displayed
      if ($entry == '.htaccess') {
        continue;
      }

      // Grab information file, decide whether it should be shown or not
      if (in_array(strtolower($entry), array(
        "descript.ion",
        "files.bbs",
      ))) {
        $infofile = $entry;
        if (!variable_get('filebrowser_hide_description_files', 0)) {
          $files[] = $entry;
        }
      }
      else {
        $files[] = $entry;
      }
    }
  }
  closedir($dir);

  // Order folders first, then files
  sort($folders);
  sort($files);
  $files = array_merge($folders, $files);

  // Get metainformation about files, and construct table spaceholder
  // for files, which have no metainformation in that file
  if ($infofile) {
    $info = filebrowser_get_fileinfo("{$folder}/{$infofile}", $subfolder);
    $count = count(filebrowser_get_fileinfo());
    $emptyinfo = $count ? array_fill(0, $count, '') : array();
  }
  else {
    $info = $emptyinfo = array();
  }

  // Build detailed list of files
  $details = array();
  foreach ($files as $file) {
    $extrainfo = isset($info[$file]) ? $info[$file] : $emptyinfo;

    // Some real folder or file
    if (!in_array($file, array(
      ".",
      "..",
    ))) {
      $completepath = "{$folder}/{$file}";
      if ($stat = stat($completepath)) {
        $icon = filebrowser_get_icon($completepath);
        $age = time() - $stat['mtime'];
        if (is_dir($completepath)) {
          $link = l("{$icon} {$file}", filebrowser_proper_path("{$subfolder}/{$file}"), array(), NULL, NULL, FALSE, TRUE);
          $size = '';
        }
        else {
          $link = "<a href=\"{$base_path}{$completepath}\">{$icon} {$file}</a>";
          $size = format_size($stat['size']);
        }
        $details[] = array_merge(array(
          array(
            'data' => $link,
            'class' => 'filename',
            'sv' => $file,
          ),
          array(
            'data' => $size,
            'sv' => $size ? $stat['size'] : 0,
          ),
          array(
            'data' => format_interval($age),
            'sv' => $age,
          ),
        ), $extrainfo);
      }
    }
    elseif ($file == ".." && !$inroot) {
      $icon = filebrowser_get_icon(NULL, 'folder');
      $link = "{$icon} {$file}";
      $parts = explode("/", $subfolder);
      array_pop($parts);
      $up = t('up');
      $link = l("{$icon} {$file} &lt;{$up}&gt;", filebrowser_proper_path(join("/", $parts)), array(), NULL, NULL, FALSE, TRUE);
      $details[] = array_merge(array(
        array(
          'data' => $link,
          'class' => 'filename',
          'sv' => $file,
        ),
        array(
          'data' => '',
          'sv' => 0,
        ),
        array(
          'data' => '',
          'sv' => 0,
        ),
      ), $extrainfo);
    }
  }
  return $details;
}