]> git.plutz.net Git - shellwiki/commitdiff
URL selection based on handlers
authorPaul Hänsch <paul@plutz.net>
Thu, 12 May 2022 09:48:26 +0000 (11:48 +0200)
committerPaul Hänsch <paul@plutz.net>
Thu, 12 May 2022 09:48:26 +0000 (11:48 +0200)
handlers/10_page.sh [new file with mode: 0755]
handlers/20_attachment.sh [moved from attachment.sh with 100% similarity]
handlers/30_edit.sh [moved from page_edit.sh with 100% similarity]
index.cgi

diff --git a/handlers/10_page.sh b/handlers/10_page.sh
new file mode 100755 (executable)
index 0000000..e010ab1
--- /dev/null
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+CACHE_AGE=${CACHE_AGE:-1800}
+export MD_MACROS="$_EXEC/macros"
+export MD_HTML="${MD_HTML:-false}"
+export WIKI_THEME=${WIKI_THEME:-default}
+
+. "$_EXEC/themes/${WIKI_THEME}.sh"
+
+if [ "$(which awk)" ]; then
+  md() { awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+elif [ "$(which busybox)" ]; then
+  md() { busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+else
+  md() { cat; }
+fi
+
+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/";
+      sed -E '1,20{ /^%[a-z]+/d; }' "$mdfile" \
+      | md |tee -- "${cache}.$$"
+    )
+    grep -q '^%nocache' "$mdfile" \
+    && rm -- "${cache}.$$" \
+    || mv -- "${cache}.$$" "${cache}"
+  fi
+}
+
+case "${PATH_INFO}" in
+  /"[.]"/*)
+    FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
+    return 0
+    ;;
+  */\[*\]/*/)
+    return 1
+    ;;
+  */"#"*/*)
+    export ERROR_MSG="This page name is not allowed"
+    theme_error 400
+    return 0
+    ;;
+  */"[login]")
+    theme_page "/[wiki]/login/"
+    return 0
+    ;;
+  */"[register]")
+    theme_page "/[wiki]/register/"
+    return 0
+    ;;
+  */"[invite]")
+    theme_page "/[wiki]/invite/"
+    return 0
+    ;;
+  */)
+    theme_page "${PATH_INFO}"
+    return 0
+    ;;
+esac
+
+return 1
similarity index 100%
rename from attachment.sh
rename to handlers/20_attachment.sh
similarity index 100%
rename from page_edit.sh
rename to handlers/30_edit.sh
index 9b9bb5fb473ca02ba50fef4d40190303d3505d9a..0c7e914062c3d99c38420e106c32b6a29c3dc59d 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -6,20 +6,6 @@
 . "${_EXEC}/cgilite/users.sh"
 . "${_EXEC}/acl.sh"
 
-. "${_EXEC}/themes/default.sh"
-
-CACHE_AGE=${CACHE_AGE:-1800}
-export MD_MACROS="$_EXEC/macros"
-export MD_HTML="${MD_HTML:-false}"
-
-if [ "$(which awk)" ]; then
-  md() { awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
-elif [ "$(which busybox)" ]; then
-  md() { busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
-else
-  md() { cat; }
-fi
-
 mdfile(){
   local page="$(PATH "$1")"
 
@@ -42,58 +28,11 @@ wiki_text() {
   cat -- "$mdfile"
 }
 
-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/";
-      sed -E '1,20{ /^%[a-z]+/d; }' "$mdfile" \
-      | md |tee -- "${cache}.$$"
-    )
-    grep -q '^%nocache' "$mdfile" \
-    && rm -- "${cache}.$$" \
-    || mv -- "${cache}.$$" "${cache}"
-  fi
-}
-
-case "${PATH_INFO}" in
-  /"[.]"/*)
-    FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
-    ;;
-  */"#"*/*)
-    export ERROR_MSG="This page name is not allowed"
-    theme_error 400
-    ;;
-  /|*[^]]/)
-    theme_page "${PATH_INFO}"
-    ;;
-  */"[login]")
-    theme_page "/[wiki]/login/"
-    ;;
-  */"[register]")
-    theme_page "/[wiki]/register/"
-    ;;
-  */"[invite]")
-    theme_page "/[wiki]/invite/"
-    ;;
-  *)
-    . "$_EXEC/page_edit.sh" \
-    || . "$_EXEC/attachment.sh" \
-    || theme_error 404
-    ;;
-esac
+for handler in "$_EXEC"/handlers/*; do
+  . "$handler" && break
+done
 
+if [ $? != 0 ]; then
+  export ERROR_MSG="The presented URL schema cannot be handled"
+  theme_error 400
+fi