]> git.plutz.net Git - shellwiki/blob - handlers/10_page.sh
Merge commit 'bfe458e83c0157879767976ba97bf95d1e27918c'
[shellwiki] / handlers / 10_page.sh
1 #!/bin/sh
2
3 . "$_EXEC/cgilite/file.sh"
4 . "$_EXEC/css.sh"
5
6 CACHE_AGE=${CACHE_AGE:-300}
7 export MD_MACROS="$_EXEC/macros"
8 export MD_HTML="${MD_HTML:-false}"
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     # usually some file related to theme
41     # let file server handle errors
42     FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
43     return 0
44     ;;
45   *${BR}*)
46     export ERROR_MSG='Page names containing newline character are not allowed'
47     theme_error 400
48     return 0
49     ;;
50   */\#*)
51     export ERROR_MSG='Page names starting with "#" are not allowed'
52     theme_error 400
53     return 0
54     ;;
55   */\[*\]/*|*/\[*\])
56     # looks like some kind of handler
57     return 1
58     ;;
59   */)
60     if [ ! "$(mdfile "$PATH_INFO")" ]; then
61       theme_error 404
62     elif ! acl_read "$PATH_INFO"; then
63       theme_error 403
64     else
65       PAGE_CSS="$(css "${PATH_INFO}")"
66       theme_page "${PATH_INFO}"
67     fi
68     return 0
69     ;;
70 esac
71
72 return 1