]> git.plutz.net Git - shellwiki/blobdiff - handlers/20_page.sh
change numbering for handlers
[shellwiki] / handlers / 20_page.sh
diff --git a/handlers/20_page.sh b/handlers/20_page.sh
new file mode 100755 (executable)
index 0000000..87af685
--- /dev/null
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+. "$_EXEC/cgilite/file.sh"
+
+CACHE_AGE=${CACHE_AGE:-300}
+export MD_MACROS="$_EXEC/macros"
+export MD_HTML="${MD_HTML:-false}"
+
+wiki() {
+  # Print content of a wiki page
+  # Get page from data or underlay dir, handle caching
+  local page="$(PATH "$1")" mdfile cache cachetime
+
+  cache="$_DATA/pages/$page/#page.${USER_ID}.cache"
+
+  mdfile="$(mdfile "$page")" || return 4
+  acl_read "$page" || return 3
+
+  cachetime="$(stat -c %Y -- "$mdfile" "$cache" 2>/dev/null)"
+
+  if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
+    -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
+    cat "${cache}"
+  else
+    mkdir -p -- "$_DATA/pages/$page/"
+    # Macros expect to find page directory as working dir
+    ( cd -- "$_DATA/pages/$page/";
+      md <"$mdfile" \
+      | tee -- "${cache}.$$"
+    )
+    grep -q '^%nocache' "$mdfile" \
+    && rm -- "${cache}.$$" \
+    || mv -- "${cache}.$$" "${cache}"
+  fi
+}
+
+case "${PATH_INFO}" in
+  /"[.]"/*)
+    # usually some file related to theme
+    # let file server handle errors
+    FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
+    return 0
+    ;;
+  *${BR}*)
+    export ERROR_MSG='Page names containing newline character are not allowed'
+    theme_error 400
+    return 0
+    ;;
+  */\#*)
+    export ERROR_MSG='Page names starting with "#" are not allowed'
+    theme_error 400
+    return 0
+    ;;
+  */\[*\]/*|*/\[*\])
+    # looks like some kind of handler
+    return 1
+    ;;
+  */)
+    if [ ! "$(mdfile "$PATH_INFO")" ]; then
+      theme_error 404
+    elif ! acl_read "$PATH_INFO"; then
+      theme_error 403
+    else
+      theme_page "${PATH_INFO}"
+    fi
+    return 0
+    ;;
+esac
+
+return 1