3 # Copyright 2014, 2015, 2020, 2021 Paul Hänsch
5 # This file is part of Confetti.
7 # Confetti is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # Confetti is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Confetti. If not, see <http://www.gnu.org/licenses/>.
21 . "$_EXEC/session_lock.sh"
22 . "$_EXEC/cgilite/storage.sh"
24 unset coursefile attfile tempfile
26 course="$(POST course |PATH)"; course="${course##*/}"
27 coursefile="$_DATA/ical/$course"
28 attfile="$_DATA/mappings/attendance"
30 if ! tempfile="$(CHECK_SLOCK "$coursefile")"; then
31 SET_COOKIE 0 message="NO VALID FILE LOCK"
32 REDIRECT "${_BASE}/courses/?e=${course}"
34 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
35 SET_COOKIE 0 message="INVALID TRANSACTION ID"
36 REDIRECT "${_BASE}/courses/?e=${course}"
42 printf %s\\n "$each" \
43 | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
45 | sed -E ':X;$!{N;bX}; s;\n;\;;g'
48 ics="$(pdi_load "$tempfile")"
50 tzid=$(cat /etc/timezone)
52 ics="$(pdi_update_attrib "$ics" DTSTAMP 1 "TZID=${tzid}")"
53 ics="$(pdi_update_value "$ics" DTSTAMP 1 "$(TZ="$tzid" date +%Y%m%dT%H%M%S)")"
55 dts_year="$( POST DTS_YEAR |grep -m1 -xE '[0-9]{4}' || date +%Y)"
56 dts_month="$( POST DTS_MONTH |grep -m1 -xE '0[1-9]|1[012]' || date +%m)"
57 dts_day="$( POST DTS_DAY |grep -m1 -xE '0[1-9]|[12][0-9]|3[01]' || date +%d)"
58 dts_hour="$( POST DTS_HOUR |grep -m1 -xE '[0-9]|1[0-9]|2[0-3]' || date +%H)"
59 dts_minute="$(POST DTS_MINUTE |grep -m1 -xE '[0-9]|[1-5][0-9]' || date +%M)"
60 [ ${#dts_hour} -eq 1 ] && dts_minute="0$dts_hour"
61 [ ${#dts_minute} -eq 1 ] && dts_minute="0$dts_minute"
62 DTSTART="${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
64 ics="$(pdi_update_attrib "$ics" DTSTART 1 "TZID=${tzid}")"
65 ics="$(pdi_update_value "$ics" DTSTART 1 "$DTSTART")"
67 rr_int=$( POST RRULE_INTERVAL |grep -m1 -xE '[0-9]+' || printf 1)
68 rr_count=$(POST RRULE_COUNT |grep -m1 -xE '[0-9]+' || printf 1)
69 rr_freq=$( POST RRULE_FREQ |grep -m1 -xE 'DAILY|WEEKLY|MONTHLY|YEARLY' || printf MONTHLY)
70 rr_uy=$( POST RRULE_UYEAR |grep -m1 -xE '[0-9]{4}' || date +%Y)
71 rr_um=$( POST RRULE_UMONTH |grep -m1 -xE '[1-9]|1[012]' || date +%m)
72 rr_ud=$( POST RRULE_UDAY |grep -m1 -xE '[1-9]|[12][0-9]|3[01]' || date +%d)
73 [ ${#rr_um} -eq 1 ] && rr_um="0$rr_um"
74 [ ${#rr_ud} -eq 1 ] && rr_ud="0$rr_ud"
76 case $(POST RRULE_LIMIT) in
77 COUNT) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$rr_count";;
78 UNTIL) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${rr_uy}${rr_um}${rr_ud}T000000Z";;
79 ETERN|*) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int";;
82 ics="$(pdi_update_value "$ics" RRULE 1 "$RRULE")"
84 for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
85 for cnt in $(seq 1 $(POST_COUNT "$field")); do
88 ics="$(pdi_update_value "$ics" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")"
93 # delete fields, first mark for deletion using delete_key
94 # this way the field enumeration is preserved during the process
95 # finally filter marked lines
96 delete_key="$(randomid)"
97 for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do
98 f="${delete%%_*}"; c="${delete##*_}";
99 [ "$(POST "$delete")" = "true" ] && ics="$(pdi_update_value "$ics" "$f" "$c" "delete=${delete_key}")"
101 ics="$(printf '%s\n' "$ics" |sed -E "/^[^:]+:delete=${delete_key}\$/d")"
103 case "$(POST action)" in
105 newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
106 ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
107 printf '%s' "$ics" |grep -vx '' >"$tempfile"
108 REDIRECT "${_BASE}/courses/?e=${course}"
111 newfield="$(POST action |sed -nE '1s;^addfield ([A-Z][A-Z0-9-]*)$;\1;p')"
112 ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
113 printf '%s' "$ics" |grep -vx '' >"$tempfile"
114 REDIRECT "${_BASE}/courses/?e=${course}"
117 if LOCK "$attfile"; then
118 grep -F "${course} " "$attfile" |while read junk card; do
119 touch "$_DATA/vcard/${card}"
121 sed -E -i "/^${course} .+\$/d" "$attfile"
122 seq 1 $(POST_COUNT attendance) |while read n; do
123 printf '%s %s\n' "$course" "$(POST attendance $n)"
125 grep -F "${course} " "$attfile" |while read junk card; do
126 touch "$_DATA/vcard/${card}"
130 SET_COOKIE 0 message="COULD NOT UPDATE COURSE MAPPINGS"
133 printf '%s' "$ics" |grep -vx '' >"${tempfile}.cp"
134 mv "${tempfile}.cp" "$coursefile"
135 RELEASE_SLOCK "$coursefile"
136 REDIRECT "${_BASE}/courses/#${course}"
139 RELEASE_SLOCK "$coursefile"
140 [ -f "$coursefile" ] \
141 && REDIRECT "${_BASE}/courses/#${course}" \
142 || REDIRECT "${_BASE}/courses/"
146 RELEASE_SLOCK "$coursefile"
147 REDIRECT "${_BASE}/courses/"
150 printf '%s' "$ics" |grep -vx '' >"$tempfile"
151 REDIRECT "${_BASE}/courses/?e=${course}"