]> git.plutz.net Git - shellwiki/blob - index.cgi
attachment processing, serving and overview
[shellwiki] / index.cgi
1 #!/bin/sh
2
3 . "${_EXEC:-${0%/*}}/cgilite/cgilite.sh"
4 . "${_EXEC}/cgilite/session.sh"
5 . "${_EXEC}/cgilite/file.sh"
6 . "${_EXEC}/cgilite/users.sh"
7
8 . "${_EXEC}/themes/default.sh"
9
10 CACHE_AGE=${CACHE_AGE:-1800}
11 export MD_MACROS="$_EXEC/macros"
12 export MD_HTML="${MD_HTML:-false}"
13
14 if [ "$(which awk)" ]; then
15   md() { awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
16 elif [ "$(which busybox)" ]; then
17   md() { busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
18 else
19   md() { cat; }
20 fi
21
22 wiki_text() {
23   # Print source text of a wiki page
24   # Get page from data or underlay dir
25   local page="$(PATH "$1")"
26
27   if [ -f "$_DATA/pages/$page/#page.md" ]; then
28     cat -- "$_DATA/pages/$page/#page.md"
29   elif [ -f "$_EXEC/pages/$page/#page.md" ]; then
30     cat -- "$_EXEC/pages/$page/#page.md"
31   else
32     return 1
33   fi
34 }
35
36 wiki() {
37   # Print content of a wiki page
38   # Get page from data or underlay dir, handle caching
39   local page="$(PATH "$1")" md cache cachetime
40
41   cache="$_DATA/pages/$page/#page.cache"
42   if [ -f "$_DATA/pages/$page/#page.md" ]; then
43     md="$_DATA/pages/$page/#page.md"
44   elif [ -f "$_EXEC/pages/$page/#page.md" ]; then
45     md="$_EXEC/pages/$page/#page.md"
46   else
47     return 1
48   fi
49
50   cachetime="$(stat -c %Y -- "$md" "$cache" 2>/dev/null)"
51
52   if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
53     -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
54     cat "${cache}"
55   else
56     mkdir -p -- "$_DATA/pages/$page/"
57     # Macros expect to find page directory as working dir
58     ( cd -- "$_DATA/pages/$page/";
59       md <"$md" |tee -- "${cache}.$$"
60     )
61     mv -- "${cache}.$$" "${cache}"
62   fi
63 }
64
65 attachment() {
66   local file="$(PATH "$1")"
67
68   # TODO: deliver downscaled images, etc.
69   if [ -f "$_DATA/pages/${file%/*}/#attachments/${file#*/}" ]; then
70     FILE "$_DATA/pages/${file%/*}/#attachments/${file#*/}"
71   elif [ -f "$_EXEC/pages/${file%/*}/#attachments/${file#*/}" ]; then
72     FILE "$_EXEC/pages/${file%/*}/#attachments/${file#*/}"
73   elif [ -d "$_DATA/pages/${file}/" -o -d "$_EXEC/pages/${file}" ]; then
74     # path looks like a rogue page name (without trailing slash), so redirect
75     REDIRECT "$_BASE/${file}/"
76   else
77     return 1
78   fi
79 }
80
81 case "${PATH_INFO}" in
82   /"[.]"/*)
83     FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
84     ;;
85   */)
86     if [ -f "$_DATA/pages/$PATH_INFO/#page.md" \
87       -o -f "$_EXEC/pages/$PATH_INFO/#page.md" ]; then
88       theme_page "${PATH_INFO}"
89     fi
90     ;;
91   */"[login]")
92     [ "$USER_NAME" ] \
93     && REDIRECT "./" \
94     || theme_login
95     ;;
96   */"[register]")
97     theme_register
98     ;;
99   */"#"*)
100     :  # TODO: Invalid page name
101     ;;
102 esac
103
104 . "$_EXEC/page_edit.sh"
105 . "$_EXEC/attachment.sh"