]> git.plutz.net Git - confetti/blob - index.cgi
1188621ec86451107ef2bb07df205e895e6b428f
[confetti] / index.cgi
1 #!/bin/zsh
2
3 # basic functions
4 die() {
5   echo "$*" >/dev/stderr
6   exit 1
7 }
8 debug() { #change to false to disable debugging
9   true && echo "$*" >>debug
10 }
11
12 # this program is supposed to be symlinked into a http root directory
13 # we will use the http root as object storage and call sub programs
14 # from the directory in which the real executable resides
15 # therefore we need to identify the code and data directories _EXEC and _STOR
16 call=$0
17 real=$call
18 while [ -L "$real" ]; do
19   real="$(stat -c %N "$real" |sed -r "s:..*. -> .(.*).$:\1:")"
20 done
21
22 _EXEC="$(dirname "$real")"  #execution directory
23 _STOR="$(dirname "$call")"  #storage directory
24
25 debug "Execution dir: $_EXEC"
26 debug "Storage dir: $_EXEC"
27
28 [ -w "$_EXEC" ] && [ -d "$_EXEC" ] || die "storage directory must be writable"
29
30 # create directories for object storage
31 for each in "$_STOR"/{vcard,mappings}; do
32   [ ! -e "$each" ] && mkdir "$each"
33   [ -w "$each" ] && [ -d "$each" ] || die "storage $each must be a writable directory"
34 done
35
36 # create htaccess file
37 [ -f .htaccess ] || cat >.htaccess <<EOF
38 Options         +ExecCGI
39 AddHandler      cgi-script .cgi
40 DirectoryIndex  index.cgi
41 EOF
42 [ -f .htaccess ] || die "no htaccess file present and unable to create one"
43
44 # parse HTTP GET string
45 declare -A _GET
46 echo "$QUERY_STRING" |tr '&' '\n' |while read query; do
47   key="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')"
48   val="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')"
49   _GET["$key"]="$(echo -e "$(echo "$val" |sed 's:+: :g;s:%:\\x:g')")"
50 done
51 debug "$_GET"
52