]> git.plutz.net Git - lobster/blob - cards/update_card.sh
updated escape vcf functions
[lobster] / cards / update_card.sh
1 #!/bin/sh
2
3 # Copyright 2014, 2016, 2019, 2020, 2021 Paul Hänsch
4 #
5 # This file is part of Confetti.
6
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.
11
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.
16
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/>. 
19
20 . "$_EXEC/pdiread.sh"
21 . "$_EXEC/session_lock.sh"
22 . "$_EXEC/cgilite/storage.sh"
23
24 unset filter order card action newfield
25 unset cardfile tempfile
26 unset vcf field cnt delete_key
27
28 filter="$(REF f)"
29 order="$(REF o)"
30
31 card="$(POST card |PATH)"; card="${card##*/}"
32 cardfile="$_DATA/vcard/${card}"
33
34 action="$(POST action)"
35 newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
36
37 if printf '%s\n' "$action" |grep -qxE 'addfield [A-Z][A-Z0-9]*'; then
38   newfield="${action##* }"
39   action=addfield
40 fi
41
42 if ! tempfile=$(CHECK_SLOCK "$cardfile"); then
43   SET_COOKIE 0 message="NO VALID FILE LOCK"
44   REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
45   exit 0
46 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
47   SET_COOKIE 0 message="INVALID TRANSACTION ID"
48   REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
49   exit 0
50 fi
51
52 # [ "${_POST[hi_select]}" = "list" ] || _POST[hi_company]="${_POST[hi_other]}"
53 # [ -n "${_POST[hi_company]}${_POST[hi_number]}${_POST[hi_status]}" ] \
54 # && _POST[X-HEALTH-INSURANCE]="$(pdi_escape "${_POST[hi_company]}" "${_POST[hi_number]}" "${_POST[hi_status]}")"
55
56 vcf="$(pdi_load "$tempfile")"
57
58 n1="$(POST 1N)" n2="$(POST 2N)" n3="$(POST 3N)" n4="$(POST 4N)" n5="$(POST 5N)"
59 # 3N (Middle Names) is not actually used
60 n3="${n2#${n2%% *}}"
61
62 vcf="$(pdi_update_value "$vcf"  N 1 "$(pdi_escape "$n1" "${n2%% *}" "${n3# }" "$n4" "$n5")")"
63 vcf="$(pdi_update_value "$vcf" FN 1 "$(pdi_escape "$n4 $n2 $n1 $n5" |sed -E 's;(^ +| +$);;g; s; +; ;g;')")"
64 vcf="$(printf '%s\n' "$vcf" |sed -E "/^CATEGORIES;[^:]*:.*$/d")"
65
66 for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
67   for cnt in $(seq 1 $(POST_COUNT "$field")); do
68     case "$field" in
69       # (TEL)
70       #   printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(pdi_escape "$(POST "$field" "$cnt")")"
71       #   ;;
72       X-HEALTH-INSURANCE)
73         hi_select="$(POST "$field" "$cnt")"
74         if [ "$hi_select" = list ]; then
75           vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "hi_company" "$cnt")" \
76                                                                        "$(POST "hi_number" "$cnt")" \
77                                                                        "$(POST "hi_status" "$cnt")" \
78                                                           )")"
79         elif [ "$hi_select" = other ]; then
80           vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "hi_other" "$cnt")" \
81                                                                        "$(POST "hi_number" "$cnt")" \
82                                                                        "$(POST "hi_status" "$cnt")" \
83                                                           )")"
84         fi
85         ;;
86       TEL)
87          vcf="$(pdi_update_attrib "$vcf" TEL $cnt TYPE="$(POST teltype $cnt |grep -Exm1 'HOME|WORK|CELL|FAX')")"
88          vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "$field" "$cnt")")")"
89          ;;
90       *)
91          vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "$field" "$cnt")")")"
92         ;;
93     esac
94 done; done
95
96 # delete fields, first mark for deletion using delete_key
97 # this way the field enumeration is preserved during the process
98 # finally filter marked lines
99 delete_key="$(randomid)"
100 for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do
101   f="${delete%%_*}"; c="${delete##*_}";
102   [ "$(POST "$delete")" = "true" ] && vcf="$(pdi_update_value "$vcf" "$f" "$c" "delete=${delete_key}")"
103 done
104 vcf="$(printf '%s\n' "$vcf" |sed -E "/^[^:]+:delete=${delete_key}\$/d")"
105
106 if [ "$action" = addfield ]; then
107   vcf="$(pdi_update_value "$vcf" "$newfield" $(( $(pdi_count "$vcf" "$newfield") + 1 )) '')"
108 fi
109 printf '%s' "$vcf" | sed -E '/^$/d; s/^([^:]+);:/\1:/;' >"$tempfile"
110
111 case "$action" in
112   addfield)
113     REDIRECT "/cards/?o=${order}&f=${filter}&e=${card#/}"
114     ;;
115   update)
116     cp "$tempfile" "$cardfile"
117     RELEASE_SLOCK "$cardfile"
118     REDIRECT "/cards/?o=${order}&f=${filter}#${card#/}"
119     ;;
120   cancel)
121     RELEASE_SLOCK "$cardfile"
122     [ -f "$cardfile" ] \
123     && REDIRECT "/cards/?o=${order}&f=${filter}#${card#/}" \
124     || REDIRECT "/cards/?o=${order}&f=${filter}"
125     ;;
126   delete)
127     rm "$cardfile"
128     RELEASE_SLOCK "$cardfile"
129     REDIRECT "/cards/?o=${order}&f=${filter}"
130     ;;
131 esac