]> git.plutz.net Git - cgilite/blob - index.cgi
introduced cookie array, extended urlsave function, IMPORTANT: removed " from array...
[cgilite] / index.cgi
1 #!/bin/zsh
2
3 # Copyright 2014, 2015 Paul Hänsch
4 #
5 # This file is part of shcgi.
6
7 # shcgi is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # shcgi is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with shcgi.  If not, see <http://www.gnu.org/licenses/>. 
19
20 export LC_ALL=C LANG=C
21 DBG_ENABLED=false
22 DBG_FILE=/dev/stderr
23
24 # this program is supposed to be symlinked into a http root directory
25 # we will use the http root as object storage (data directory) and call sub
26 # programs from the directory in which the real executable resides
27 # therefore we need to identify the code and data directories _EXEC and _DATA
28 call="$0"
29 real="$(readlink -f $call)"
30 _DATA="$(dirname "$call")"  #storage directory
31 _EXEC="${real%/shcgi/index.cgi}"  #execution directory
32
33 # put debug options in the local.opts file
34 [ -r "$_DATA/local.opts" ] && . "$_DATA/local.opts"
35
36 # basic functions
37 debug() { #change to false to disable debugging
38   if [ "$DBG_ENABLED" = true -a -n "$*" ]; then
39     printf '%s\n' "$*" >>"$DBG_FILE"
40   elif [ "$DBG_ENABLED" = true -a -z "$*" ]; then
41     tee -a "$DBG_FILE"
42   elif [ -z "$*" ]; then
43     cat
44   fi
45 }
46
47 die() {
48   debug FATAL: "$*"
49   printf '%s\n' "$*" >&2
50   exit 1
51 }
52
53 data_dirs(){
54   # create directories for object storage
55
56   [ -d "$_DATA" -a -w "$_DATA" ] || die "storage directory must be writable"
57   for each in "$@"; do
58     [ ! -e "$_DATA/$each" ] && mkdir "$_DATA/$each"
59     [ -d "$_DATA/$each" -a -w "$_DATA/$each" ] || die "storage \"$_DATA/$each\" must be a writable directory"
60   done
61 }
62
63 # create htaccess file
64 [ -f "$_DATA/.htaccess" ] || cat >"$_DATA/.htaccess" <<EOF
65 Options         +ExecCGI
66 AddHandler      cgi-script .cgi
67 DirectoryIndex  index.cgi
68 EOF
69 [ -f "$_DATA/.htaccess" ] || die "no htaccess file present and unable to create one"
70
71 . "$_EXEC/shcgi/cgi.sh"
72
73 cgi_get
74
75 [ -x "$_EXEC/constants.sh" ] && . "$_EXEC/constants.sh"
76
77 if [ -n "${_GET[action]}" ]; then
78   . "$_EXEC/shcgi/action.sh"
79 else
80   . "$_EXEC/shcgi/page.sh"
81 fi