]> git.plutz.net Git - shellwiki/blob - handlers/10_page.sh
004dd03ced270f3b524b2b0b33b9ed07c75c7791
[shellwiki] / handlers / 10_page.sh
1 #!/bin/sh
2
3 CACHE_AGE=${CACHE_AGE:-1800}
4 export MD_MACROS="$_EXEC/macros"
5 export MD_HTML="${MD_HTML:-false}"
6 export WIKI_THEME=${WIKI_THEME:-default}
7
8 . "$_EXEC/themes/${WIKI_THEME}.sh"
9
10 wiki() {
11   # Print content of a wiki page
12   # Get page from data or underlay dir, handle caching
13   local page="$(PATH "$1")" mdfile cache cachetime
14
15   cache="$_DATA/pages/$page/#page.${USER_ID}.cache"
16
17   mdfile="$(mdfile "$page")" || return 4
18   acl_read "$page" || return 3
19
20   cachetime="$(stat -c %Y -- "$mdfile" "$cache" 2>/dev/null)"
21
22   if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
23     -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
24     cat "${cache}"
25   else
26     mkdir -p -- "$_DATA/pages/$page/"
27     # Macros expect to find page directory as working dir
28     ( cd -- "$_DATA/pages/$page/";
29       md <"$mdfile" \
30       | tee -- "${cache}.$$"
31     )
32     grep -q '^%nocache' "$mdfile" \
33     && rm -- "${cache}.$$" \
34     || mv -- "${cache}.$$" "${cache}"
35   fi
36 }
37
38 case "${PATH_INFO}" in
39   /"[.]"/*)
40     FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
41     return 0
42     ;;
43   *${BR}*)
44     export ERROR_MSG='Page names containing newline character are not allowed'
45     theme_error 400
46     return 0
47     ;;
48   */\#*)
49     export ERROR_MSG='Page names starting with "#" are not allowed'
50     theme_error 400
51     return 0
52     ;;
53   */\[*\]/*)
54     return 1
55     ;;
56   */)
57     theme_page "${PATH_INFO}"
58     return 0
59     ;;
60 esac
61
62 return 1