#!/bin/sh

ID=$QUERY_STRING

validId() {
	case "$ID" in
		/*)
			return 1
			;;
		../*)
			return 1
			;;
		*/../*)
			return 1
			;;
		*/..)
			return 1
			;;
		*)
		return 0
	esac
}
	
cat << EOF
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
EOF

bad() {
	cat << EOF
<head>
  <link rel="stylesheet" type="text/css" href="default.css"/>
  <title>Invalid argument</title>
</head>
<body>
	<p>Invalid argument</p>
</body>
EOF
	exit 1
}

validId
if [ $? -ne 0 ]; then
	bad
fi

case "$ID" in
	video/*)
		TYPE=video
		;;
	image/*)
		TYPE=image
		;;
	*)
		bad
		;;
esac

showVideo() {
	local FILE
	FILE=$1

	cat << EOF
<head>
  <link rel="stylesheet" type="text/css" href="default.css"/>
  <title>$NAME</title>
</head>
<body>
EOF

	cat << EOF
  <table class="videoBox"><tr><td>
    <div class="inlineVideo">
      <object type="video/x-msvideo" data="$FILE">
        <param name="src" value="$FILE"/>
        <div class="noVideo">
		  You'd see a video here if you had the correct plugin installed.
		</div>
      </object>
    </div>
  </td></tr></table>
  <p class="downloadVideo">
    <a href="$FILE">Download</a>
  </p>
EOF
}

showImage() {
	local FILE
	FILE=$1

	cat << EOF
<head>
  <link rel="stylesheet" type="text/css" href="default.css"/>
  <title>$NAME</title>
</head>
<body>
EOF

	cat << EOF
  <table class="videoBox"><tr><td>
    <div class="imageBox">
	  <img class="image" src="$FILE" alt="$FILE"/>
    </div>
  </td></tr></table>
EOF
}

case "$TYPE" in
	"video")
		showVideo "$ID"
		;;
	"image")
		showImage "$ID"
		;;
	*)
		bad
		;;
esac

cat << EOF
  <hr/>
  <p>
    <span>
      <a href="http://validator.w3.org/check?uri=referer">
        <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" />
      </a>
    </span>
    <span>
      <a href="http://jigsaw.w3.org/css-validator/">
        <img style="border:0;width:88px;height:31px" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" />
      </a>
    </span>
  </p>
</body>
</html>
EOF


