#!/bin/sh
-# Copyright 2022 - 2023 Paul Hänsch
+# Copyright 2022 - 2023, 2026 Paul Hänsch
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
edit_file="$_DATA/pages/$edit_page/#page.md"
[ "$REQUEST_METHOD" = POST ] && edit_action="$(POST action)"
+edit_handler_pagetext() {
+ # Custom POST data parser because cgilite POST is only
+ # fast for small text
+ local pagetext="$(
+ printf %s \\n "${cgilite_post}" \
+ | sed -En '
+ /^(.*&)?pagetext=([^&]*)(&.*)?$/{
+ s;^(.*&)?pagetext=([^&]*)(&.*)?$;\2;;
+ s;\+; ;g;
+
+ s;%.[08];&0;g; s;%.[19];&1;g;
+ s;%.[2aA];&2;g; s;%.[3bB];&3;g;
+ s;%.[4cC];&4;g; s;%.[5dD];&5;g;
+ s;%.[6eE];&6;g; s;%.[7fF];&7;g;
+
+ s;%[048cC][0-7];&0;g; s;%[048cC][89a-fA-F];&1;g;
+ s;%[159dD][0-7];&2;g; s;%[159dD][89a-fA-F];&3;g;
+ s;%[26aAeE][0-7];&4;g; s;%[26aAeE][89a-fA-F];&5;g;
+ s;%[37bBfF][0-7];&6;g; s;%[37bBfF][89a-fA-F];&7;g;
+
+ s;%[0123].;\\0;g; s;%[4567].;\\1;g;
+ s;%[89aAbB].;\\2;g; s;%[c-fC-F].;\\3;g;
+ p;
+ }
+ ')"
+ printf -- "$pagetext"
+}
+
if ! acl_write "$edit_page"; then
theme_error 403
return 0
elif [ "$edit_action" = update ]; then
if mkdir -p -- "${edit_file%/#page.md}" \
&& S_LOCK "$edit_file"; then
- POST pagetext >"$edit_file"
+ edit_handler_pagetext >"$edit_file"
S_RELEASE "$edit_file"
else
export ERROR_MSG="Unable to lock page for editing"