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