<?PHP
/* Copyright 2010, Lime Technology LLC.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 */
/* Adapted by Bergware International (May 2012) */
?>
<?
// Create list of objects in directory given by global $dir variable.  The list
// is sorted by $field (type, name, size, or time), either SORT_ASC or SORT_DESC
// as given by $opt.
//
function sort_by( $field, $opt) {
// read directory contents into 'list' array
  global $dir;
  $dir_path = '/usr/local/emhttp'.$dir;
  $list = array();
  foreach (array_diff(scandir($dir_path),array('.','..')) as $entry) {
    $path = $dir_path.'/'.$entry;
    $type = filetype($path);
// follow symlinks
    if ($type == 'link')
      $type = filetype(readlink($path));
// use 'stat' command because filesize() for files > 2GB is buggy
    $list[] = array(
     'type' => $type,
     'name' => $entry,
     'fext' => strtolower(pathinfo($entry, PATHINFO_EXTENSION)),
     'size' => is_dir($path) ? '' : trim(`stat -c%s "{$path}"`),
     'time' => filemtime($path));
  }
// sort by input 'field'
  if ($field=='name') {
    $type = array();
    $name = array();
    foreach ($list as $row) {
      $type[] = $row['type'];
      $name[] = strtolower($row['name']);
    }
    array_multisort( $type,$opt, $name,$opt, $list);
  }
  else {
    $type = array();
    $indx = array();
    $name = array();
    foreach ($list as $row) {
      $type[] = $row['type'];
      $indx[] = $row[$field];
      $name[] = strtolower($row['name']);
    }
    if ($field=='size'||$field=='time')
      array_multisort( $type,$opt, $indx,$opt,SORT_NUMERIC, $name,$opt, $list);
    else
      array_multisort( $type,$opt, $indx,$opt, $name,$opt, $list);
  }
// return sorted list
  return $list;
}
function file_icon( $fext) {
  $filetypes = array (
   'asc' => 'asc.png',
   'avi' => 'video.png',
   'bin' => 'bin.png',
   'bmp' => 'image.png',
   'csv' => 'csv.png',
   'divx' => 'divx.png',
   'doc' => 'doc.png',
   'docb' => 'doc.png',
   'docx' => 'doc.png',
   'dot' => 'dot.png',
   'eml' => 'eml.png',
   'gif' => 'gif.png',
   'htm' => 'htm.png',
   'html' => 'html.png',
   'ifo' => 'sig.png',
   'iso' => 'iso.png',
   'jar' => 'jar.png',
   'java' => 'java.png',
   'jpg' => 'jpg.png',
   'jpeg' => 'jpeg.png',
   'js' => 'js.png',
   'midi' => 'audio.png',
   'mkv' => 'video.png',
   'mov' => 'video.png',
   'mp3' => 'mp3.png',
   'mpg' => 'mpg.png',
   'mts' => 'video.png',
   'm2ts' => 'video.png',
   'ogg' => 'ogg.png',
   'pdf' => 'pdf.png',
   'php' => 'php.png',
   'pl' => 'pl.png',
   'png' => 'png.png',
   'ppt' => 'ppt.png',
   'pptb' => 'ppt.png',
   'pptx' => 'ppt.png',
   'ps' => 'ps.png',
   'rar' => 'rar.png',
   'rtf' => 'rtf.png',
   'tar' => 'tar.png',
   'tgz' => 'tgz.png',
   'tif' => 'image.png',
   'tiff' => 'image.png',
   'txt' => 'txt.png',
   'url' => 'link.png',
   'vcf' => 'vcf.png',
   'vob' => 'video.png',
   'wav' => 'wav.png',
   'wma' => 'video.png',
   'mp4' => 'video.png',
   'wmv' => 'wmv.png',
   'xls' => 'xls.png',
   'xlsb' => 'xls.png',
   'xlsx' => 'xls.png',
   'xml' => 'xml.png',
   'xvid' => 'xvid.png',
   'zip' => 'zip.png'
  );
  if (array_key_exists( $fext, $filetypes))
    $icon = $filetypes[$fext];
  else
    $icon = 'file.png';
  return "/plugins/indexer/icons/{$icon}";
}
function dir_icon() {
  return "/plugins/indexer/icons/folder.png";
}
function dirup_icon() {
  return "/plugins/indexer/icons/dirup.png";
}
function safe_dirname($path) {
  $dirname = dirname($path);
  return $dirname == '/' ? '' : $dirname;
}
// here we go..
clearstatcache();
if (!isset($column)) $column='name';
if (!isset($order)) $order='A';
$list = sort_by( $column, $order=='A'?SORT_ASC:SORT_DESC);

$order=($order=='A'?'D':'A');
$fext_order=($column=='fext'?$order:'A');
$name_order=($column=='name'?$order:'A');
$size_order=($column=='size'?$order:'A');
$time_order=($column=='time'?$order:'A');
?>
<style>
#indexer { margin-top: 5px; }
#indexer td { border-bottom: 1px solid #F9F9F9; line-height: 18px; }
#indexer tr:hover { background: #F3F3F3 }
#indexer tr > td { width: 4%; text-align: right; }
#indexer tr:first-child > td { padding-right: 6px; }
#indexer tr:first-child { font-weight: bold; }
#indexer tr > td + td { width: auto; text-align: left; }
#indexer tr > td + td + td { width: 12%; }
#indexer span { font-size: smaller; }
</style>

<table id="indexer" class="small">
  <tr>
  <td><a href="<?='/'.$path.'?dir='.$dir.'&column=fext&order='.$fext_order;?>">Type</a></td>
  <td><a href="<?='/'.$path.'?dir='.$dir.'&column=name&order='.$name_order;?>">Name</a></td>
  <td><a href="<?='/'.$path.'?dir='.$dir.'&column=size&order='.$size_order;?>">Size</a></td>
  <td><a href="<?='/'.$path.'?dir='.$dir.'&column=time&order='.$time_order;?>">Last Modified</a></td>
  </tr>
  <tr>
  <td><img src="<?=dirup_icon();?>" class="icon";></td>
  <td><a href="<?='/'.$path.'?dir='.safe_dirname($dir);?>">Parent Directory</a></td>
  <td></td>
  <td></td>
  </tr>
<?$dirs=0; $files=0; $total=0;
  foreach ($list as $entry):
?>  <tr>
<?  if ($entry['type']=='dir'):
      $dirs++;
?>    <td><img src="<?=dir_icon();?>" class="icon";></td>
      <td><a href="<?='/'.$path.'?dir='.urlencode_path($dir.'/'.$entry['name']);?>"><?=$entry['name'];?></a></td>
      <td></td>
<?  else:
      $files++;
      $total+=$entry['size'];
?>    <td><img src="<?=file_icon($entry['fext']);?>" class="icon";></td>
      <td><a href="<?=$dir.'/'.$entry['name'];?>"><?=$entry['name'];?></a></td>
      <td><?=my_scale($entry['size'],$units).' '.$units;?></td>
<?  endif; ?>
    <td><?=my_time($entry['time'],"%F {$display['time']}");?></td>
    </tr>
<?endforeach;
  $objs = $dirs + $files;
  $objtext = ($objs == 1)? "1 object" : "{$objs} objects";
  $dirtext = ($dirs == 1)? "1 directory" : "{$dirs} directories";
  $filetext = ($files == 1)? "1 file" : "{$files} files";
  $totaltext = ($files == 0)? "" : '('.my_scale($total,$units).' '.$units.' total)';
?><tr>
  <td></td>
  <td colspan=3><span><?=$objtext;?>: <?=$dirtext;?>, <?=$filetext;?> <?=$totaltext?></span></td>
  </tr>
</table>