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