]> git.plutz.net Git - shellwiki/blob - handlers/30_page.sh
8a222092855e88cc2fa2de843f59aa933bdad478
[shellwiki] / handlers / 30_page.sh
1 #!/bin/sh
2
3 # Copyright 2022 - 2023 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 . "$_EXEC/cgilite/file.sh"
18
19 CACHE_AGE=${CACHE_AGE:-300}
20 export MD_MACROS="$_EXEC/macros"
21 export MD_HTML="${MD_HTML:-false}"
22
23 wiki() {
24   # Print content of a wiki page
25   # Get page from data or underlay dir, handle caching
26   local page="$(PATH "$1")" mdfile cache cachetime
27
28   cache="$_DATA/pages/$page/#page:${LANGUAGE}.${USER_ID}.cache"
29
30   mdfile="$(mdfile "$page")" || return 4
31   acl_read "$page" || return 3
32
33   cachetime="$(stat -c %Y -- "$mdfile" "$cache" 2>/dev/null)"
34
35   if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
36     -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
37     cat "${cache}"
38   else
39     mkdir -p -- "$_DATA/pages/$page/"
40     # Macros expect to find page directory as working dir
41     ( cd -- "$_DATA/pages/$page/";
42       md <"$mdfile" \
43       | tee -- "${cache}.$$"
44     )
45     grep -q '^%nocache' "$mdfile" \
46     && rm -- "${cache}.$$" \
47     || mv -- "${cache}.$$" "${cache}"
48   fi
49 }
50
51 case "${PATH_INFO}" in
52   /"[.]"/*)
53     # usually some file related to theme
54     # let file server handle errors
55     FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
56     return 0
57     ;;
58   *${BR}*)
59     export ERROR_MSG='Page names containing newline character are not allowed'
60     theme_error 400
61     return 0
62     ;;
63   */\#*)
64     export ERROR_MSG='Page names starting with "#" are not allowed'
65     theme_error 400
66     return 0
67     ;;
68   */\[*\]/*|*/\[*\])
69     # looks like some kind of handler
70     return 1
71     ;;
72   */)
73     if ! mdfile "$PATH_INFO" >&-; then
74       theme_error 404
75     elif ! acl_read "$PATH_INFO"; then
76       theme_error 403
77     else
78       theme_page "${PATH_INFO}"
79     fi
80     return 0
81     ;;
82 esac
83
84 return 1