#!/bin/sh . "${_EXEC:-${0%/*}}/cgilite/cgilite.sh" . "${_EXEC}/cgilite/session.sh" . "${_EXEC}/cgilite/file.sh" . "${_EXEC}/cgilite/users.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 wiki_text() { # Print source text of a wiki page # Get page from data or underlay dir local page="$(PATH "$1")" if [ -f "$_DATA/pages/$page/#page.md" ]; then cat -- "$_DATA/pages/$page/#page.md" elif [ -f "$_EXEC/pages/$page/#page.md" ]; then cat -- "$_EXEC/pages/$page/#page.md" else return 1 fi } wiki() { # Print content of a wiki page # Get page from data or underlay dir, handle caching local page="$(PATH "$1")" md cache cachetime cache="$_DATA/pages/$page/#page.cache" if [ -f "$_DATA/pages/$page/#page.md" ]; then md="$_DATA/pages/$page/#page.md" elif [ -f "$_EXEC/pages/$page/#page.md" ]; then md="$_EXEC/pages/$page/#page.md" else return 1 fi cachetime="$(stat -c %Y -- "$md" "$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 <"$md" |tee -- "${cache}.$$" ) mv -- "${cache}.$$" "${cache}" fi } attachment() { local file="$(PATH "$1")" # TODO: deliver downscaled images, etc. if [ -f "$_DATA/pages/${file%/*}/#attachments/${file#*/}" ]; then FILE "$_DATA/pages/${file%/*}/#attachments/${file#*/}" elif [ -f "$_EXEC/pages/${file%/*}/#attachments/${file#*/}" ]; then FILE "$_EXEC/pages/${file%/*}/#attachments/${file#*/}" elif [ -d "$_DATA/pages/${file}/" -o -d "$_EXEC/pages/${file}" ]; then # path looks like a rogue page name (without trailing slash), so redirect REDIRECT "$_BASE/${file}/" else return 1 fi } case "${PATH_INFO}" in /"[.]"/*) FILE "${_EXEC}/${PATH_INFO#/\[.\]}" ;; */) if [ -f "$_DATA/pages/$PATH_INFO/#page.md" \ -o -f "$_EXEC/pages/$PATH_INFO/#page.md" ]; then theme_page "${PATH_INFO}" else theme_404 fi ;; */"[login]") [ "$USER_NAME" ] \ && REDIRECT "./" \ || theme_login ;; */"[register]") theme_register ;; */"#"*) : # TODO: Invalid page name ;; esac . "$_EXEC/page_edit.sh" . "$_EXEC/attachment.sh"