diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-21 15:05:01 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-21 15:05:01 -0400 |
commit | 6bbdf29d6cc28d4080f1c29352ff38539549c856 (patch) | |
tree | 6ca59d18c65f101d055d070af66a2a992262af33 /index.php | |
parent | bc520e10b6c1e57271d25c3caed541c65e3c11da (diff) |
tidy path handling
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 48 |
1 files changed, 27 insertions, 21 deletions
@@ -1,5 +1,5 @@ <?php -# Copyright © 2014 Luke Shumaker <lukeshu@sbcglobal.net> +# Copyright © 2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net> # This work is free. You can redistribute it and/or modify it under the # terms of the Do What The Fuck You Want To Public License, Version 2, # as published by Sam Hocevar. See the ./COPYING file for more details. @@ -33,16 +33,20 @@ function show_404() { <?php } -function is_root($root, $dirname) { - $path = $root.'/'.$dirname; - - $parts = preg_split("|/+|", $root, -1, PREG_SPLIT_NO_EMPTY); - $root = implode('/', $parts); +function normalizeN($filename) { + $parts = preg_split("|/+|", $filename, -1, PREG_SPLIT_NO_EMPTY); + $abs = substr($filename, 0, 1) === '/'; + return array(($abs?'/':'').implode('/', $parts), count($parts)); +} - $parts = preg_split("|/+|", $path, -1, PREG_SPLIT_NO_EMPTY); - $path = implode('/', $parts); +function normalize($filename) { + list ($new_filename, $n) = normalizeN($filename); + return $new_filename; +} - return ($path === $root); +function is_root($root, $dirname) { + $path = $root.'/'.$dirname; + return (normalize($path) === normalize($root)); } function classify($root, $dirname, $filename) { @@ -51,13 +55,14 @@ function classify($root, $dirname, $filename) { if ($filename === '.') { return [ 'dir' ]; } if ($filename === '..') { return [ 'dir', 'parent' ]; } - $parts = preg_split("|/+|", $dirname, -1, PREG_SPLIT_NO_EMPTY); - $dirname = implode('/', $parts); + list ($dirname, $parts) = normalizeN($dirname); + $dirpath = normalize($root.'/'.$dirname); + $filepath = normalize($dirpath.'/'.$filename); $classes = array(); - if (count($parts) == 0) { + if ($parts == 0) { // in the top-level - if (is_dir($root.'/'.$dirname.'/'.$filename.'/os')) { + if (is_dir($filepath) && is_dir($filepath.'/os')) { array_push($classes, 'repo'); if (in_array($filename, $repos_arch_project )) { array_push($classes, 'arch'); } if (in_array($filename, $repos_para_project )) { array_push($classes, 'para'); } @@ -73,9 +78,9 @@ function classify($root, $dirname, $filename) { } } if ($filename[0] === '.' || substr($filename, -1) === '~') { $classes[] = 'hidden'; } - if (is_link($root.'/'.$dirname.'/'.$filename)) { $classes[] = 'link'; } - if (is_dir( $root.'/'.$dirname.'/'.$filename)) { $classes[] = 'dir'; } - if (is_file($root.'/'.$dirname.'/'.$filename)) { $classes[] = 'file'; } + if (is_link($filepath)) { $classes[] = 'link'; } + if (is_dir( $filepath)) { $classes[] = 'dir'; } + if (is_file($filepath)) { $classes[] = 'file'; } if (preg_match("/\.pkg\.tar(\..z)?$/" , $filename) == 1) { $classes[] = 'pkg'; } if (preg_match("/\.tar(\..z|\.bz2)?$/", $filename) == 1) { $classes[] = 'tar'; } @@ -145,10 +150,11 @@ if (!is_dir($root.'/'.$dirname)) { foreach ($filenames as $filename) { if ($filename === '.') { continue; } if ($filename === '..' && is_root($root,$dirname)) { continue; } + $filepath = normalize($root.'/'.$dirname.'/'.$filename); $classes = classify($root, $dirname, $filename); echo "\t\t\t\t<tr class=\"".implode(' ', $classes)."\">"; - if (is_link($root.'/'.$dirname.'/'.$filename)) { - echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a> -> ".htmlentities(readlink($root.'/'.$dirname.'/'.$filename))."</td>"; + if (is_link($filepath)) { + echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a> -> ".htmlentities(readlink($filepath))."</td>"; } else { echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a></td>"; } @@ -156,11 +162,11 @@ if (!is_dir($root.'/'.$dirname)) { if ($filename == '..') { echo '<td></td><td></td>'; } else { - echo "<td>".date("Y-m-d H:i", filemtime($root.'/'.$dirname.'/'.$filename))."</td>"; - if (is_dir($root.'/'.$dirname.'/'.$filename)) { + echo "<td>".date("Y-m-d H:i", filemtime($filepath))."</td>"; + if (is_dir($filepath)) { echo "<td class=number>-</td>"; } else { - echo "<td class=number>".filesize($root.'/'.$dirname.'/'.$filename)."</td>"; + echo "<td class=number>".filesize($filepath)."</td>"; } echo "</tr>\n"; } |