-#!/bin/zsh
+#!/bin/sh
-# Copyright 2014, 2015 Paul Hänsch
+# Copyright 2014, 2015, 2020, 2021 Paul Hänsch
#
# This file is part of Confetti.
#
# You should have received a copy of the GNU Affero General Public License
# along with Confetti. If not, see <http://www.gnu.org/licenses/>.
-cgi_post
+. "$_EXEC/pdiread.sh"
+. "$_EXEC/session_lock.sh"
+. "$_EXEC/cgilite/storage.sh"
-course="${_POST[course]}"
-tempfile="temp/$course"
-coursefile="ical/$course"
+unset coursefile attfile tempfile
+
+course="$(POST course |PATH)"; course="${course##*/}"
+coursefile="$_DATA/ical/$course"
attfile="$_DATA/mappings/attendance"
-# DURATION:
-uid="${_POST[UID]}"
+if ! tempfile="$(CHECK_SLOCK "$coursefile")"; then
+ SET_COOKIE 0 message="NO VALID FILE LOCK"
+ REDIRECT "/courses/?e=${course}"
+ exit 0
+elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
+ SET_COOKIE 0 message="INVALID TRANSACTION ID"
+ REDIRECT "/courses/?e=${course}"
+ exit 0
+fi
+
+vcf_escape(){
+ for each in "$@"; do
+ printf %s\\n "$each" \
+ | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
+ done \
+ | sed -E ':X;$!{N;bX}; s;\n;\;;g'
+}
+
+ics="$(pdi_load "$coursefile")"
tzid=$(cat /etc/timezone)
-tstamp=$(TZ="$tzid" date +%Y%m%dT%H%M%S)
-
-dts_year="${_POST[DTSYEAR]}"
-dts_month="${_POST[DTSMONTH]}"
-dts_day="${_POST[DTSDAY]}"
-[ -n "${_POST[DTSDAY0]}" ] && dts_day="${_POST[DTSDAY0]}"
-dts_hour="${_POST[DTSHOUR]}"
-dts_minute="${_POST[DTSMINUTE]}"
-
-[ -z $dts_year ] && dts_year=$(date +%Y)
-[ -z $dts_month ] && dts_month=$(date +%m)
-[ -z $dts_day ] && dts_day=$(date +%d)
-date -d ${dts_year}-${dts_month}-${dts_day} >/dev/null 2>/dev/null || dts_day="01"
-[ -z $dts_hour ] && dts_hour=$(date +%H)
-[ -z $dts_minute ] && dts_minute=$(date +%M)
-
-dtstart="TZID=${tzid}:${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
-
-rr_int="${_POST[RRULE_INTERVAL]}"
-rr_freq="${_POST[RRULE_FREQ]}"
-rr_limit="${_POST[RRULE_LIMIT]}"
-case "$rr_limit" in
- ETERN)
- rrule="FREQ=$rr_freq;INTERVAL=$rr_int"
- ;;
- COUNT)
- t="${_POST[RRULE_COUNT]}"
- rrule="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$t"
- ;;
- UNTIL)
- uy="${_POST[RRULE_UYEAR]}"
- um="${_POST[RRULE_UMONTH]}"
- ud="${_POST[RRULE_UDAY]}"
- rrule="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${uy}${um}${ud}T000000Z"
- ;;
+
+ics="$(pdi_update_attrib "$ics" DTSTAMP 1 "TZID=${tzid}")"
+ics="$(pdi_update_value "$ics" DTSTAMP 1 "$(TZ="$tzid" date +%Y%m%dT%H%M%S)")"
+
+dts_year="$( POST DTS_YEAR |grep -m1 -xE '[0-9]{4}' || date +%Y)"
+dts_month="$( POST DTS_MONTH |grep -m1 -xE '0[1-9]|1[012]' || date +%m)"
+dts_day="$( POST DTS_DAY |grep -m1 -xE '0[1-9]|[12][0-9]|3[01]' || date +%d)"
+dts_hour="$( POST DTS_HOUR |grep -m1 -xE '[0-9]|1[0-9]|2[0-3]' || date +%H)"
+dts_minute="$(POST DTS_MINUTE |grep -m1 -xE '[0-9]|[1-5][0-9]' || date +%M)"
+[ ${#dts_hour} -eq 1 ] && dts_minute="0$dts_hour"
+[ ${#dts_minute} -eq 1 ] && dts_minute="0$dts_minute"
+DTSTART="${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
+
+ics="$(pdi_update_attrib "$ics" DTSTART 1 "TZID=${tzid}")"
+ics="$(pdi_update_value "$ics" DTSTART 1 "$DTSTART")"
+
+rr_int=$( POST RRULE_INTERVAL |grep -m1 -xE '[0-9]+' || printf 1)
+rr_count=$(POST RRULE_COUNT |grep -m1 -xE '[0-9]+' || printf 1)
+rr_freq=$( POST RRULE_FREQ |grep -m1 -xE 'DAILY|WEEKLY|MONTHLY|YEARLY' || printf MONTHLY)
+rr_uy=$( POST RRULE_UYEAR |grep -m1 -xE '[0-9]{4}' || date +%Y)
+rr_um=$( POST RRULE_UMONTH |grep -m1 -xE '[1-9]|1[012]' || date +%m)
+rr_ud=$( POST RRULE_UDAY |grep -m1 -xE '[1-9]|[12][0-9]|3[01]' || date +%d)
+[ ${#rr_um} -eq 1 ] && rr_um="0$rr_um"
+[ ${#rr_ud} -eq 1 ] && rr_ud="0$rr_ud"
+
+case $(POST RRULE_LIMIT) in
+ COUNT) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$rr_count";;
+ UNTIL) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${rr_uy}${rr_um}${rr_ud}T000000Z";;
+ ETERN|*) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int";;
esac
-echo "BEGIN:VCALENDAR\r" >"$tempfile"
-echo "VERSION:2.0\r" >>"$tempfile"
-echo "PRODID:Berlin RAW Confetti\r" >>"$tempfile"
-echo "BEGIN:VEVENT\r" >>"$tempfile"
-echo "UID:$uid\r" >>"$tempfile"
-echo "DTSTAMP:TZID=${tzid}:${tstamp}\r" >>"$tempfile"
-echo "DTSTART:${dtstart}\r" >>"$tempfile"
-echo "RRULE:${rrule}\r" >>"$tempfile"
-for field in SUMMARY COMMENT; do
- value="${_POST[$field]}"
- n=0
- while [ -n "$value" ]; do
- value="$(echo "$value" |sed -r ':a;N;$!ba;s:\n:\\\\n:g;s:\r:\\\\r:g')"
- echo "${field}:${value}\r"
- value="${_POST[$field$n]}"
- n=$(($n + 1))
- done
-done >>"$tempfile"
-
-case "${_POST[action]}" in
+ics="$(pdi_update_value "$ics" RRULE 1 "$RRULE")"
+
+for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
+ for cnt in $(seq 1 $(POST_COUNT "$field")); do
+ case "$field" in
+ *)
+ ics="$(pdi_update_value "$ics" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")"
+ ;;
+ esac
+done; done
+
+# delete fields, first mark for deletion using delete_key
+# this way the field enumeration is preserved during the process
+# finally filter marked lines
+delete_key="$(randomid)"
+for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do
+ f="${delete%%_*}"; c="${delete##*_}";
+ [ "$(POST "$delete")" = "true" ] && ics="$(pdi_update_value "$ics" "$f" "$c" "delete=${delete_key}")"
+done
+ics="$(printf '%s\n' "$ics" |sed -E "/^[^:]+:delete=${delete_key}\$/d")"
+
+case "$(POST action)" in
addfield)
- echo "${_POST[newfield]}:\r" >>"$tempfile"
- echo "END:VEVENT\r" >>"$tempfile"
- echo "END:VCALENDAR\r" >>"$tempfile"
- echo -n "Location: ?p=courses&edit=$course\n\n"
+ newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
+ ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
+ printf '%s' "$ics" |grep -vx '' >"$tempfile"
+ REDIRECT "/courses/?e=${course}"
+ ;;
+ addfield\ [A-Z]*)
+ newfield="$(POST action |sed -rn '1s;^addfield ([A-Z][A-Z0-9-]*)$;\1;p')"
+ ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
+ printf '%s' "$ics" |grep -vx '' >"$tempfile"
+ REDIRECT "/courses/?e=${course}"
;;
update)
- attendance=()
- for att in attendance attendance{0..100}; do
- [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
- done
- sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
- touch "$_DATA/vcard/$card"
- done
- sed -i -r '/^'$course'\t(.+)$/d' "$attfile"
- for each in $attendance; do
- echo "$course\t$each"
- done >>"$attfile"
- sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
- touch "$_DATA/vcard/$card"
- done
-
- echo "END:VEVENT\r" >>"$tempfile"
- echo "END:VCALENDAR\r" >>"$tempfile"
- mv "$tempfile" "$coursefile"
- echo -n "Location: ?p=courses#$course\n\n"
+ if LOCK "$attfile"; then
+ grep -F "${course} " "$attfile" |while read junk card; do
+ touch "$_DATA/vcard/${card}"
+ done
+ sed -i -r "/^${course} .+\$/d" "$attfile"
+ seq 1 $(POST_COUNT attendance) |while read n; do
+ printf '%s %s\n' "$course" "$(POST attendance $n)"
+ done >>"$attfile"
+ grep -F "${course} " "$attfile" |while read junk card; do
+ touch "$_DATA/vcard/${card}"
+ done
+ RELEASE "$attfile"
+ else
+ SET_COOKIE 0 message="COULD NOT UPDATE COURSE MAPPINGS"
+ fi
+
+ printf '%s' "$ics" |grep -vx '' >"${tempfile}.cp"
+ mv "${tempfile}.cp" "$coursefile"
+ RELEASE_SLOCK "$coursefile"
+ REDIRECT "/courses/#${course}"
;;
cancel)
- rm "$tempfile"
+ RELEASE_SLOCK "$coursefile"
[ -f "$coursefile" ] \
- && echo -n "Location: ?p=courses#$course\n\n" \
- || echo -n "Location: ?p=courses\n\n"
+ && REDIRECT "/courses/#${course}" \
+ || REDIRECT "/courses/"
;;
delete)
- rm "$tempfile" "$coursefile"
- echo -n "Location: ?p=courses\n\n"
+ rm "$coursefile"
+ RELEASE_SLOCK "$coursefile"
+ REDIRECT "/courses/"
;;
*)
- echo "END:VEVENT\r" >>"$tempfile"
- echo "END:VCALENDAR\r" >>"$tempfile"
- echo -n "Location: ?p=courses&edit=$course\n\n"
+ printf '%s' "$ics" |grep -vx '' >"$tempfile"
+ REDIRECT "/courses/?e=${course}"
;;
esac