first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / files / show_directory_listing.sh
1 #!/bin/bash
2
3 # shows the directory passed as the first parameter.
4 # if there's a second parameter, it's used for a filtering pattern.
5
6 function send_header()
7 {
8   echo "Content-type: text/html"
9   echo ""
10   echo ""
11 }
12
13 dir="$1"; shift
14 pattern="*";
15 temp_pat="$1"; shift
16 if [ ! -z "$temp_pat" ]; then
17   # we don't allow any clever scooting up the directory hierarchy...
18   if [ -z "$(echo "$temp_pat" | grep "\.\.")" ]; then
19     pattern="$temp_pat"
20   fi
21 fi
22
23 # check that they've at least provided a directory.
24 if [ -z "$dir" ]; then
25   send_header
26   echo "$(basename $0): This needs a directory name before it can show the folder."
27   exit
28 fi
29
30 # make sure they aren't trying to go above the web root.
31 if [ ! -z "$(echo "$dir" | grep "\.\.")" ]; then
32   echo "$(basename $0): Will not go above the root web directory."
33   exit
34 fi
35
36
37 send_header
38
39 echo "[$dir]"
40 echo "<br>"
41
42 fulldir="/var/www/$dir"
43
44 for i in "$fulldir"/$pattern; do
45   dirlisting="$(ls -dsh $i | sed -e 's/ \/.*$//')"
46   echo "<a href=\"$(basename $i)\">$(basename $i) ($dirlisting)</a> <br>"
47 done
48
49 popd &>/dev/null
50
51