Files
2026-11-18 02:18:54 +01:00

75 lines
2.5 KiB
Bash
Executable File

#!/bin/sh
# Fills the Impressum and the privacy policy with the operator's details from the environment.
# Runs from the nginx image's /docker-entrypoint.d/ hook on every start. It works from pristine
# copies, so changing a variable and restarting the container is enough.
set -eu
HTML=/usr/share/nginx/html
TEMPLATES=/opt/legal-templates
PAGES="impressum/index.html datenschutz/index.html"
missing=""
for var in IMPRESSUM_NAME IMPRESSUM_STREET IMPRESSUM_CITY IMPRESSUM_EMAIL; do
eval "value=\${$var:-}"
[ -n "$value" ] || missing="$missing $var"
done
if [ -n "$missing" ]; then
echo "40-legal-notice: missing required variables:$missing" >&2
echo "40-legal-notice: refusing to serve the site without an Impressum (see .env.example)" >&2
exit 1
fi
case "$IMPRESSUM_EMAIL" in
*@*.*) ;;
*)
echo "40-legal-notice: IMPRESSUM_EMAIL does not look like an email address" >&2
exit 1
;;
esac
# One line, HTML-escaped.
html() {
printf '%s' "$1" | tr '\r\n' ' ' |
sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's/"/\&quot;/g' -e "s/'/\&#39;/g"
}
# Every character as a numeric entity, which keeps the address away from naive scrapers.
entities() {
printf '%s' "$1" | od -An -tu1 | tr -s ' \n' '\n\n' | sed -e '/^$/d' -e 's/.*/\&#&;/' | tr -d '\n'
}
# Escapes a replacement for sed's s|…|…| (backslash, the | delimiter and &).
repl() {
printf '%s' "$1" | sed -e 's/[\\|&]/\\&/g'
}
script=$(mktemp)
trap 'rm -f "$script"' EXIT
{
echo "s|%%IMPRESSUM_NAME%%|$(repl "$(html "$IMPRESSUM_NAME")")|g"
echo "s|%%IMPRESSUM_STREET%%|$(repl "$(html "$IMPRESSUM_STREET")")|g"
echo "s|%%IMPRESSUM_CITY%%|$(repl "$(html "$IMPRESSUM_CITY")")|g"
echo "s|%%IMPRESSUM_COUNTRY%%|$(repl "$(html "${IMPRESSUM_COUNTRY:-Deutschland}")")|g"
echo "s|%%IMPRESSUM_EMAIL%%|$(repl "$(entities "$IMPRESSUM_EMAIL")")|g"
for pair in phone:IMPRESSUM_PHONE hoster:HOSTER; do
key=${pair%%:*}
var=${pair#*:}
eval "value=\${$var:-}"
if [ -n "$value" ]; then
echo "s|%%$var%%|$(repl "$(html "$value")")|g"
else
echo "s|<p data-optional=\"$key\"[^>]*>[^<]*</p>||g"
fi
done
} >"$script"
for page in $PAGES; do
sed -f "$script" "$TEMPLATES/$page" >"$HTML/$page"
if grep -q '%%[A-Z_]*%%' "$HTML/$page"; then
echo "40-legal-notice: unfilled placeholder left in $page" >&2
exit 1
fi
done
echo "40-legal-notice: filled in the Impressum and the privacy policy"