X-Git-Url: https://git.plutz.net/?a=blobdiff_plain;f=therapies%2Fupdate_therapy.sh;h=e084e6ddc9e7dfe361e625f44c541fdf62fd2cda;hb=b67065bfb57b072dd1c344eedabfbb545eae6629;hp=6ba06cbd41214d84efe623d7c640020cd049d191;hpb=531f71462dfbc0eeebd74c74c84323c14475f51a;p=lobster diff --git a/therapies/update_therapy.sh b/therapies/update_therapy.sh index 6ba06cb..e084e6d 100755 --- a/therapies/update_therapy.sh +++ b/therapies/update_therapy.sh @@ -1,6 +1,6 @@ #!/bin/zsh -# Copyright 2016 Paul Hänsch +# Copyright 2016, 2020 Paul Hänsch # # This file is part of Confetti. # @@ -17,54 +17,116 @@ # You should have received a copy of the GNU Affero General Public License # along with Confetti. If not, see . -BR=' -' -tpy="${_POST[id]}" +. "$_EXEC/pdiread.sh" +tpy="$(POST id)" -tpyfile="$_DATA/therapies/$tpy" -tempfile="$_DATA/temp/$tpy" +tpyfile="$_DATA/therapies/${tpy}.tpy" +tempfile="$_DATA/lock/${tpy}.$$.tpy" + +if [ "$(POST tid)" != "$(transid "$tpyfile")" ]; then + if [ "$(POST autosubmit)" = "true" ]; then + printf 'Status: 409 Conflict\r\nContent-Length: 0\r\n\r\n' + exit 0 + else + SET_COOKIE session message="TRANSACTION_CONFLICT" + REDIRECT "/therapies/${tpy%.*}/${tpy#*.}" + fi +fi + +if [ "$(POST formend)" != "formend" ]; then + if [ "$(POST autosubmit)" = "true" ]; then + printf 'Status: 409 Conflict\r\nContent-Length: 0\r\n\r\n' + exit 0 + else + SET_COOKIE session message="INCOMPLETE_SUBMIT" + REDIRECT "/therapies/${tpy%.*}/${tpy#*.}" + fi +fi # serialize POST array into file -for key in ${(k)_POST}; do - [ "$key" != imagedata ] && printf %s:%s\\n "$key" "${_POST[$key]//$BR/\\n}" -done >"$tempfile" +for key in $(POST_KEYS); do + case "$key" in + imagedata|tid|formend) : ;; + session*_date) + value="$(POST "$key")" + y=0 mon=0 dom=0 + case $value in + *.*.*) IFS=. read dom mon y <<-END + ${value} + END + ;; + *.*.) IFS=. read dom mon <<-END + ${value} + END + ;; + */*/*) IFS=/ read mon dom y <<-END + ${value} + END + ;; + */*) IFS=/ read mon dom <<-END + ${value} + END + ;; + *-*-*) IFS=- read y mon dom <<-END + ${value} + END + ;; + esac + [ ! "$y" ] && y="$(date +%Y)" + [ "$y" -gt 0 -a "$y" -lt 100 ] && y="$((y + 2000))" + date -d "${y}-${mon}-${dom}" + && printf %s:%s\\n "$key" "$(date -d "${y}-${mon}-${dom}" +%F)" \ + || printf %s:\\n "$key" + ;; + *) printf %s:%s\\n "$key" "$(pdi_escape "$(POST "$key")")" ;; + esac +done >"$tempfile" 2>&- -if [ -n "$_POST[delete_session]" ]; then - n="$_POST[delete_session]" - sed -i -r '/^session'$n'[_:]/d' "$tempfile" +if [ "$(POST delete_session)" ]; then + n="$(POST delete_session)" + sed -Ei '/^session'$n'[_:]/d' "$tempfile" rm "${tpyfile%.tpy}_session${n}.png" while grep -Eq '^session'$(($n + 1))'_' "$tempfile"; do - sed -i -r 's;^session'$(($n + 1))'(_|:);session'$n'\1;' "$tempfile" + sed -Ei 's;^session'$(($n + 1))'(_|:);session'$n'\1;' "$tempfile" mv "${tpyfile%.tpy}_session$(($n+1)).png" "${tpyfile%.tpy}_session${n}.png" n=$(($n+1)) done -elif [ -n "$_POST[new_session]" ]; then - sid="$_POST[new_session]" +elif [ "$(POST new_session)" ]; then + sid="$(POST new_session)" - identify "$_EXEC/static/therapy_background.png" \ - | sed -r 's;^.* ([0-9]+x[0-9]+) .*$;\1;' \ - | read dim + read junkx junky dim junkz <<-E_READ + $(identify "$_EXEC/therapies/therapy_background.png") + E_READ convert -size "$dim" xc:transparent "${tpyfile%.tpy}_${sid}.png" printf '%s:exists\n' "$sid" >>"$tempfile" printf '%s_open:checked\n' "$sid" >>"$tempfile" -elif [ -n "$_POST[imagedata]" ]; then - sed -rn 's;^(session[0-9]+)_open:checked$;\1;p' "$tempfile" \ - | sort -n \ - | tail -n1 \ - | read sid +elif [ "$(POST imagedata)" ]; then + sid="$(sed -En 's;^(session[0-9]+)_open:checked$;\1;p' "$tempfile" \ + | sort -n \ + | tail -n1 + )" convert "${tpyfile%.tpy}_${sid}.png" \ - -strokewidth 2 -fill '#00000000' \ - -draw "${_POST[imagedata]}" -transparent white \ + -draw "$(POST imagedata)" -transparent white \ "${tpyfile%.tpy}_${sid}.png" sync fi -mv "$tempfile" "$tpyfile" +if ! diff -q "$tempfile" "$tpyfile" >/dev/null; then + mv "$tempfile" "$tpyfile" + rm -f -- "${_DATA}/cache/${tpy%%.*}.vcf.cache" +fi -redirect "?p=therapy&id=${tpy}" +if [ "$(POST autosubmit)" = "true" ]; then + msg="$(transid "$tpyfile")" + printf 'HTTP/1.1 200 OK\r\nContent-Length: %i\r\n\r\n%s' \ + "${#msg}" "${msg}" +elif [ "$(POST vcfreturn)" ]; then + REDIRECT "/cards/#${tpy%.*}.vcf" +else + REDIRECT "/therapies/${tpy%.*}/${tpy#*.}" +fi