]> git.plutz.net Git - lobster/blobdiff - therapies/update_therapy.sh
quick fix: put temp file into lock, should switch to session_lock functions
[lobster] / therapies / update_therapy.sh
index ec8ccdc0878128c43a27872cecfcd26b1bcdf60f..e084e6ddc9e7dfe361e625f44c541fdf62fd2cda 100755 (executable)
 # You should have received a copy of the GNU Affero General Public License
 # along with Confetti.  If not, see <http://www.gnu.org/licenses/>. 
 
-BR='
-'
-tpy=$(POST id)
+. "$_EXEC/pdiread.sh"
+tpy="$(POST id)"
 
 tpyfile="$_DATA/therapies/${tpy}.tpy"
-tempfile="$_DATA/temp/${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 $(POST_KEYS); do
-  [ "$key" != imagedata ] && printf %s:%s\\n "$key" "$(POST "$key" |sed ':X;N;$!bX; s;\n;\\n;g;')"
-done >"$tempfile"
+  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 [ "$(POST delete_session)" ]; then
   n="$(POST delete_session)"
@@ -64,6 +116,17 @@ elif [ "$(POST imagedata)" ]; then
   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 "/therapies/${tpy%.*}/${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