#!/bin/bash # arcview.sh - Archive Viewer # Author: Sean B. Palmer, inamidst.com # Source: http://inamidst.com/code/arcview # License: MIT License; share and enjoy! function doc() { echo 'This is a CGI script for viewing archives' echo 'Try using the following in an .htaccess: ' echo 'RewriteRule \.tar\.(gz|bz2)/$ arcview.cgi [L]' } # The following variables are configurable # But the script will work with the defaults STYLE=/trove/arcview.css NOTFOUND=/bin/404.cgi # The rest is not so configurable case $REQUEST_URI in *.tar.gz/) TYPE=z; SUFFIX=.tar.gz;; *.tar.bz2/) TYPE=j; SUFFIX=.tar.bz2;; *) notfound;; esac AFN=${REQUEST_URI%%$SUFFIX/*}$SUFFIX TZIP=${AFN%/} AFN=$DOCUMENT_ROOT${AFN#/} ANAME=$(basename $AFN $SUFFIX) FN=${REQUEST_URI#*$SUFFIX/} FN=${FN%/} if [ "$REQUEST_METHOD" = 'POST' ] then GETVALUE='import cgi; print cgi.FieldStorage()["filename"].value' FILEPATH=$(python -c "$GETVALUE") fi function serve() { echo Content-Type: ${1:-text/html} echo cat } function mimetype() { case $1 in html) echo text/html;; jpg) echo image/jpeg;; gif) echo image/gif;; png) echo image/png;; *) echo text/plain;; esac } function directory() { echo "$ANAME" echo "" if [ -f "$DOCUMENT_ROOT$STYLE" ] then echo "" fi echo "

$ANAME

" echo "

Download archive: $ANAME$SUFFIX" echo '

' ARCVIEW='http://inamidst.com/code/arcview' echo "

(Powered by arcview.)" } # Main Program if [ -z "$SCRIPT_NAME" ] then doc && exit # @@ Warn about badly designed archives? elif [ "$REQUEST_METHOD" = 'POST' ] && \ tar -${TYPE}tf $AFN $ANAME/$FILEPATH &> /dev/null then tar -O${TYPE}xvf $AFN $ANAME/$FILEPATH | \ serve $(mimetype ${FILEPATH##*.}) elif [ -z "$FN" ] then tar -${TYPE}tf $AFN | directory | serve elif [ -x "$DOCUMENT_ROOT$NOTFOUND" ] then $DOCUMENT_ROOT$NOTFOUND else echo Status: 404 echo Content-Type: text/plain echo echo Not Found fi # [EOF]