summaryrefslogtreecommitdiff
path: root/cgi/bashlib.sh
diff options
context:
space:
mode:
Diffstat (limited to 'cgi/bashlib.sh')
-rw-r--r--cgi/bashlib.sh235
1 files changed, 235 insertions, 0 deletions
diff --git a/cgi/bashlib.sh b/cgi/bashlib.sh
new file mode 100644
index 0000000..58e8ac2
--- /dev/null
+++ b/cgi/bashlib.sh
@@ -0,0 +1,235 @@
+#!/bin/bash
+
+# Author: darren chamberlain <dlc@users.sourceforge.net>
+# Co-Author: Paul Bournival <paulb-ns@cajun.nu>
+#
+# bashlib is used by sourcing it at the beginning of scripts that
+# needs its functionality (by using the . or source commands).
+
+PATH=/bin:/usr/bin
+
+#
+# Set version number
+#
+VERSION=$(@ECHO@ '$Revision: 1.3 $' | @AWK@ '{print $2}')
+
+# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+# Initialization stuff begins here. These things run immediately, and
+# do the parameter/cookie parsing.
+# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+
+# Global debug flag. Set to 0 to disable debugging throughout the lib
+DEBUG=0
+
+# capture stdin for POST methods. POST requests don't always come in
+# with a newline attached, so we use cat to grab stdin and append a newline.
+# This is a wonderful hack, and thanks to paulb.
+STDIN=$(@CAT@)
+if [ -n "${STDIN}" ]; then
+ QUERY_STRING="${STDIN}&${QUERY_STRING}"
+fi
+
+# Handle GET and POST requests... (the QUERY_STRING will be set)
+if [ -n "${QUERY_STRING}" ]; then
+ # name=value params, separated by either '&' or ';'
+ if echo ${QUERY_STRING} | grep '=' >/dev/null ; then
+ for Q in $(@ECHO@ ${QUERY_STRING} | @TR@ ";&" "\012") ; do
+ #
+ # Clear our local variables
+ #
+ unset name
+ unset value
+ unset tmpvalue
+
+ #
+ # get the name of the key, and decode it
+ #
+ name=${Q%%=*}
+ name=$(@ECHO@ ${name} | \
+ @SED@ -e 's/%\(\)/\\\x/g' | \
+ @TR@ "+" " ")
+ name=$(@ECHO@ ${name} | \
+ @TR@ -d ".-")
+ name=$(@PRINTF@ ${name})
+
+ #
+ # get the value and decode it. This is tricky... printf chokes on
+ # hex values in the form \xNN when there is another hex-ish value
+ # (i.e., a-fA-F) immediately after the first two. My (horrible)
+ # solution is to put a space aftet the \xNN, give the value to
+ # printf, and then remove it.
+ #
+ tmpvalue=${Q#*=}
+ tmpvalue=$(@ECHO@ ${tmpvalue} | \
+ @SED@ -e 's/%\(..\)/\\\x\1 /g')
+ #echo "Intermediate \$value: ${tmpvalue}" 1>&2
+
+ #
+ # Iterate through tmpvalue and printf each string, and append it to
+ # value
+ #
+ for i in ${tmpvalue}; do
+ g=$(@PRINTF@ ${i})
+ value="${value}${g}"
+ done
+ #value=$(echo ${value})
+
+ eval "export FORM_${name}='${value}'"
+ done
+ else # keywords: foo.cgi?a+b+c
+ Q=$(echo ${QUERY_STRING} | tr '+' ' ')
+ eval "export KEYWORDS='${Q}'"
+ fi
+fi
+
+#
+# this section works identically to the query string parsing code,
+# with the (obvious) exception that variables are stuck into the
+# environment with the prefix COOKIE_ rather than FORM_. This is to
+# help distinguish them from the other variables that get set
+# automatically.
+#
+if [ -n "${HTTP_COOKIE}" ]; then
+ for Q in ${HTTP_COOKIE}; do
+ #
+ # Clear our local variables
+ #
+ name=
+ value=
+ tmpvalue=
+
+ #
+ # Strip trailing ; off the value
+ #
+ Q=${Q%;}
+
+ #
+ # get the name of the key, and decode it
+ #
+ name=${Q%%=*}
+ name=$(@ECHO@ ${name} | \
+ @SED@ -e 's/%\(\)/\\\x/g' | \
+ @TR@ "+" " ")
+ name=$(@ECHO@ ${name} | \
+ @TR@ -d ".-")
+ name=$(@PRINTF@ ${name})
+
+ # Decode the cookie value. See the parameter section above for
+ # an explanation of what this is doing.
+ tmpvalue=${Q#*=}
+ tmpvalue=$(@ECHO@ ${tmpvalue} | \
+ @SED@ -e 's/%\(..\)/\\\x\1 /g')
+ #echo "Intermediate \$value: ${tmpvalue}" 1>&2
+
+ #
+ # Iterate through tmpvalue and printf each string, and append it to
+ # value
+ #
+ for i in ${tmpvalue}; do
+ g=$(@PRINTF@ ${i})
+ value="${value}${g}"
+ done
+ #value=$(echo ${value})
+
+ #
+ # Export COOKIE_${name} into the environment
+ #
+ #echo "exporting COOKIE_${name}=${value}" 1>&2
+ eval "export COOKIE_${name}='${value}'"
+ done
+fi
+
+# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+# functions and all that groovy stuff
+# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+#
+# Shameless plug, advertises verion.
+function version {
+ echo "bashlib, version ${VERSION}"
+}
+
+function version_html {
+ echo -n "<a href=\"http://sevenroot.org/software/bashlib/\">bashlib</a>,"
+ echo "version ${VERSION}"
+}
+
+#
+# Parameter function.
+# * When called with no arguments, returns a list of parameters that
+# were passed in.
+# * When called with one argument, returns the value of that parameter
+# (if any)
+# * When called with more than one argument, assumes that the first is a
+# paramter name and the rest are values to be assigned to a paramter of
+# that name.
+#
+function param {
+ local name
+ local value
+ if [ $# -eq 1 ]; then
+ name=$1
+ name=$(echo ${name} | @SED@ -e 's/FORM_//')
+ value=$(@ENV@ | @GREP@ "^FORM_${name}" | @SED@ -e 's/FORM_//' | @CUT@ -d= -f2-)
+ elif [ $# -gt 1 ]; then
+ name=$1
+ shift
+ eval "export 'FORM_${name}=$*'"
+ else
+ value=$(@ENV@ | @GREP@ '^FORM_' | @SED@ -e 's/FORM_//' | @CUT@ -d= -f1)
+ fi
+ echo ${value}
+ unset name
+ unset value
+}
+
+# cookie function. Same explanation as param
+function cookie {
+ local name
+ local value
+ if [ $# -eq 1 ]; then
+ name=$1
+ name=$(echo ${name} | @SED@ -e 's/COOKIE_//')
+ value=$(@ENV@ | @GREP@ "^COOKIE_${name}" | @SED@ -e 's/COOKIE_//' | @CUT@ -d= -f2-)
+ elif [ $# -gt 1 ]; then
+ name=$1
+ shift
+ eval "export 'COOKIE_${name}=$*'"
+ else
+ value=$(@ENV@ | @GREP@ '^COOKIE_' | @SED@ -e 's/COOKIE_//' | @CUT@ -d= -f1)
+ fi
+ echo ${value}
+ unset name
+ unset value
+}
+
+# keywords returns a list of keywords. This is only set when the script is
+# called with an ISINDEX form (these are pretty rare nowadays).
+function keywords {
+ echo ${KEYWORDS}
+}
+
+function set_cookie {
+ local name=$1
+ shift
+ local value=$*
+ bashlib_cookies="${bashlib_cookies}; ${name}=${value}"
+
+ bashlib_cookies=${bashlib_cookies#;}
+
+ cookie $name $value
+}
+
+#
+# send_redirect takes a URI and redirects the browser to that uri, exiting
+# the script along the way.
+#
+function send_redirect {
+ local uri
+ if [ $# -eq 1 ]; then
+ uri=$1
+ else
+ uri="http://${SERVER_NAME}/${SCRIPT_NAME}"
+ fi
+ echo "Location: ${uri}"
+ echo ""
+}