<?php
include '/root/common.php';
$STATEMENT = 1;
$ndownload = 0;

/* scandir() to sort by date modified
 *   https://stackoverflow.com/questions/11923235/scandir-to-sort-by-date-modified
 */
/*
function scandir_replacement($dir)
{
  $files_array = scandir($dir);
  $img_array   = array();
  $img_dsort   = array();
  $final_array = array();

  foreach ($files_array as $file) {
    if (($file != ".") && ($file != "..") && ($file != ".svn") && ($file != ".htaccess")) {
        $img_array[] = $file;
        $img_dsort[] = filemtime($dir . '/' . $file);   
    }
  }

  $merge_arrays = array_combine($img_dsort, $img_array);
  krsort($merge_arrays);

  foreach($merge_arrays as $key => $value)
    $final_array[] = $value;    

  return (is_array($final_array)) ? $final_array : false;
}
*/

function bullet($stmt, $path, $entry, $options=NULL)
{
  /*
  while (preg_match(',/$,', $path))
    $path = substr($path, 0, strlen($path) - 1);
  */
//log_append($path . ' {');
  $len = strlen($path);

  while ($len && '/' == substr($path, $len)) {
    $path = substr($path, 0, $len);
    --$len;
  }
//log_append($path . ' }');

  if (is_link($entry)) {
    $target = readlink($entry);
    $target = substr($target, 2);
    $target = substr($target, 0, strlen($target) - 1);
    $param = $path . '/' . $target;
  }
  else
    $param = $path . '/' . $entry;

  if (! mysqli_stmt_bind_param($stmt, "s", $param)
      || ! mysqli_stmt_execute($stmt)
      || ! mysqli_stmt_bind_result($stmt, $count)
      || ! mysqli_stmt_fetch($stmt))
    $count = 0;

  if ($options) {
?>
<li><a href="<?php echo '/count/download.php' . $options; ?>"><?php echo $entry; ?></a>
<?php
  }
  else {
?>
<li><a href="<?php echo $entry; ?>"><?php echo $entry; ?></a>
<?php
  }

  echo $count ? '<b>' : '' ?>(<?php echo $count; ?>)<?php echo $count ? '</b>' : '';
  mysqli_free_result($result); // ]

  if (is_file($entry))
    ++$GLOBALS['ndownload'];
}

$depth = 1;
$parent = basename(dirname(__FILE__,$depth));
$title = $parent;
$path = $parent;
for (;;) {
  if (20 == ++$depth) break;
  if ('html' == ($parent = basename(dirname(__FILE__,$depth)))) break;
  $title = $parent . ' &rarr; ' . $title;
  $path = $parent . '/' . $path;
}
?>
<html>
<header>
<title><?php echo $title; ?></title>
</header>
<body style="font-family:arial;font-size:14px">
<?php swr(); ?>
<!-- <?php echo '/var/www/html/' .  $path; ?> -->
<!-- <?php echo __FILE__; ?> -->
<h2><?php echo $title; ?></h2>
<p><a href="..">Up&hellip;</a>
<?php
// connect to mysql
$link = mysqli_connect($HOST, $USER, $PASSWORD, $DOWNLOAD); // [

if (false === $link)
  log_and_exit('Error connecting. ' . mysqli_connect_error());

$stmt = mysqli_stmt_init($link); // [

if (false === $stmt) {
  log_end_exit('Error initializing statement. ' . mysqli_error());
  mysqli_close($link);
}

$query = 'SELECT count FROM aggregate WHERE path = ?';

if (! mysqli_stmt_prepare($stmt, $query)) {
  log_end_exit('Error preparing statement. ' . mysqli_error());
  mysqli_stmt_close($stmt);
  mysqli_close($link);
}
?>

<ul><!-- [ -->
<?php $entries = scandir(".");
foreach ($entries as $entry) {
  if ('.' == $entry)
    continue;
  else if ('..' == $entry)
    continue;
  else if ('distribute' == $entry)
    continue;
  else if ('index.php' == $entry)
    continue;
  else if ('download.php' == $entry)
    continue;
  else if ('aggregate' == $entry)
    continue;
  else if ('aggregate.log' == $entry)
    continue;

//$path_parts = pathinfo($entry);
//echo '<li>', $path_parts['extension'];

  else if (is_dir($entry))
    bullet($stmt, $path, $entry);
  else if ('html' == pathinfo($entry)['extension']) {
    if ('#' == substr($entry, 0, 1))
      continue;
?>
<li>
<?php
    $fh = fopen($entry, "rb");

    while (!feof($fh)) {
      echo fgets($fh);
      flush();
    }

    fclose($fh);
    continue;
  }
  else if ('README' == $entry && $readme = file_get_contents($entry)) {
    $readme = preg_replace(
        '/http:\/\/(.+)\.sourceforge\.net\/index\.html/',
        'http://pbelkner.de/projects/web/$1',
        $readme);
    $readme = preg_replace(
        '/(https?:'
        .  '[^ )\n#]+'
        . '(#[^ )\n]+)?'
        . ')/',
        '<a target="_blank" href="$1">$1</a>',
        $readme);
  }
  else
    bullet($stmt, $path, $entry, '?path=' . $path . '&file=' . $entry);
}

if ($readme) {
  bullet($stmt, $path, 'README', '?path=' . $path . '&file=README');
?>
<blockquote><pre><?php echo $readme; ?></pre></blockquote>
<?php
}
?>
</ul><!-- ] -->

<?php
mysqli_stmt_close($stmt); // ]
mysqli_close($link); // ]
?>
<p><a href="..">Up&hellip;</a>
<?php
if (0 < $GLOBALS['ndownload']) {
?>
<p><hr>
<font style="font-size:12px"><b>Disclaimer:</b> In case you download a file your ip-address/domain might be stored in order to build-up a download statistics.</font>
<?php
}
?>
</body>
</html>
