]> git.plutz.net Git - shellwiki/commitdiff
Squashed 'cgilite/' changes from 8f729b0..0591b08
authorPaul Hänsch <paul@plutz.net>
Mon, 26 Feb 2024 22:10:30 +0000 (23:10 +0100)
committerPaul Hänsch <paul@plutz.net>
Mon, 26 Feb 2024 22:10:30 +0000 (23:10 +0100)
0591b08 Bugfix: faulty email address check
e7fcaf2 json export function
e1c0b4b more consistent db2/json structure and jpath selector
0a8a851 get json values via jpath
b8a4246 read json data into recursive DB2 structure
6ab6900 more reliable include path

git-subtree-dir: cgilite
git-subtree-split: 0591b0842c75fd078d19e488f5e4a8d047052551

db23.sh
json.sh [new file with mode: 0755]
users.sh

diff --git a/db23.sh b/db23.sh
index a3a25bb052d392dd7b1df7d2751a30fb6fbbf7be..e8a0d64da8bf0b15c7c74398fe7200d68ad16680 100755 (executable)
--- a/db23.sh
+++ b/db23.sh
@@ -17,7 +17,7 @@
 [ -n "$include_db23" ] && return 0
 include_db23="$0"
 
-. "$_EXEC/cgilite/storage.sh"
+. "${_EXEC:-.}/cgilite/storage.sh"
 
 DB2() {
   local call data file key val seq
diff --git a/json.sh b/json.sh
new file mode 100755 (executable)
index 0000000..f43f8ec
--- /dev/null
+++ b/json.sh
@@ -0,0 +1,360 @@
+#!/bin/sh
+
+[ -n "$include_json" ] && return 0
+include_json="$0"
+
+. "${_EXEC:-.}/cgilite/db23.sh"
+
+# debug(){ [ $# -gt 0 ] && printf '%s\n' "$@" >&2 || tee -a /dev/stderr; }
+
+json_except() {
+  printf '%s\n' "$@" >&2;
+  printf 'Exc: %s\n' "$json_document" >&2
+}
+
+json_space() {
+  while true; do case "$json_document" in
+    [" ${BR}${CR}      "]*) json_document="${json_document#?}";;
+    *) break ;;
+  esac; done
+}
+
+json_string() {
+  local string json_document="$json_document" end=0
+
+  json_space
+  case $json_document in
+    \"*) json_document="${json_document#?}"
+      ;;
+    *) json_except "Expected string specifyer starting with (\")"
+      return 1
+      ;;
+  esac
+  while [ "$json_document" ]; do case $json_document in
+    \\?*)
+      string="${string}${json_document%"${json_document#??}"}"
+      json_document="${json_document#??}"
+      ;;
+    \"*)
+      json_document="${json_document#?}"
+      end=1
+      break
+      ;;
+    *) 
+      string="${string}${json_document%"${json_document#?}"}"
+      json_document="${json_document#?}"
+      ;;
+  esac; done
+
+  if [ $end -eq 0 ]; then
+    json_except "Document ended mid-string"
+    return 1
+  fi
+
+  printf "%s   %s\n" "$(STRING "$string")" "$json_document"
+}
+
+json_key() {
+  local key json_document="$json_document"
+
+  json_space
+  case $json_document in
+    \"*)
+      key="$(json_string)" || return 1
+      json_document="${key#*   }"
+      key="${key%%     *}"
+      ;;
+    *) json_except "Expected key specifyer starting with '\"'"
+      return 1
+      ;;
+  esac
+  json_space
+  case $json_document in
+    :*) json_document="${json_document#?}"
+      ;;
+    *) json_except "Expected value separator \":\""
+      return 1
+      ;;
+  esac
+
+  printf '%s   %s\n' "$key" "$json_document"
+}
+
+json_number() {
+  local number json_document="$json_document"
+
+  json_space
+  number="${json_document%%["  ${BR}${CR} ,}]"]*}"
+  json_document="${json_document#"$number"}"
+  if ! number="$(printf %f "$number")"; then
+    json_except "Invalid number format"
+    return 1
+  fi
+
+  printf '%s   %s\n' "${number%.000000}" "$json_document"
+}
+
+json_array() {
+  local struct="$(DB2 new)" value json_document="$json_document"
+
+  json_space
+  case $json_document in
+    "["*) json_document="${json_document#?}"
+      ;;
+    *) json_except "Expected array starting with \"[\""
+      return 1
+      ;;
+  esac
+
+  json_space
+  case $json_document in
+    "]"*)
+      printf "%s       %s\n" "" "${json_document#?}"
+      return 0
+      ;;
+  esac
+
+  while :; do
+    json_space
+
+    value="$(json_value)" || return 1
+    json_document="${value#*   }"
+    value="$(UNSTRING "${value%%       *}")"
+
+       struct="$(DB2 "$struct" append "@" "$value")" \
+    || struct="$(DB2 "$struct" set    "@" "$value")"
+
+    json_space
+    case $json_document in
+      ,*) json_document="${json_document#?}"
+        ;;
+      "]"*) json_document="${json_document#?}"
+        break
+        ;;
+      *) json_except "Unexpected character mid-array"
+        return 1
+        ;;
+    esac
+  done
+
+  printf "%s   %s\n" "$(STRING "$struct")" "$json_document"
+}
+
+json_object() {
+  local struct="$(DB2 new)" key value json_document="$json_document"
+
+  json_space
+  case $json_document in
+    "{"*) json_document="${json_document#?}"
+      ;;
+    *) json_except "Expected object starting with \"{\""
+      return 1
+      ;;
+  esac
+
+  json_space
+  case $json_document in
+    "}"*)
+      printf "%s       %s\n" "" "${json_document#?}"
+      return 0
+      ;;
+  esac
+
+  while :; do
+    json_space
+
+    key="$(json_key)" || return 1
+    json_document="${key#*     }"
+    key="$(UNSTRING "${key%%   *}")"
+
+    value="$(json_value)" || return 1
+    json_document="${value#*   }"
+    value="$(UNSTRING "${value%%       *}")"
+
+    struct="$(DB2 "$struct" set "$key" "$value")"
+
+    json_space
+    case $json_document in
+      ,*) json_document="${json_document#?}"
+        ;;
+      "}"*) json_document="${json_document#?}"
+        break
+        ;;
+      *) json_except "Unexpected character mid-object"
+        return 1
+        ;;
+    esac
+  done
+
+  printf "%s   %s\n" "$(STRING "$struct")" "$json_document"
+}
+
+json_value() {
+  local value json_document="$json_document"
+  json_type=""
+
+  json_space
+  case $json_document in
+    \"*)
+      value="$(json_string)" || return 1
+      json_document="${value#* }"
+      value="str:${value%%     *}"
+      json_type=string
+      ;;
+    [+-.0-9]*)
+      value="$(json_number)" || return 1
+      json_document="${value#* }"
+      value="num:${value%%     *}"
+      json_type=number
+      ;;
+    "{"*)
+      value="$(json_object)" || return 1
+      json_document="${value#* }"
+      value="obj:${value%%     *}"
+      json_type=object
+      ;;
+    "["*)
+      value="$(json_array)" || return 1
+      json_document="${value#* }"
+      value="arr:${value%%     *}"
+      json_type=array
+      ;;
+    null*)
+      json_document="${json_document#null}"
+      value="null"
+      json_type=null
+      ;;
+    true*)
+      json_document="${json_document#true}"
+      value="true"
+      json_type=boolean
+      ;;
+    false*)
+      json_document="${json_document#false}"
+      value="false"
+      json_type=boolean
+      ;;
+  esac
+
+  printf "%s   %s\n" "$value" "$json_document"
+}
+
+json_load() {
+  local json_document="$1" json
+
+  json_value |UNSTRING
+}
+
+json_get() {
+  local json="$1" jpath="${2#.}" key idx
+  json_type=''
+
+  case $json in
+    str:*) json_type="string";;
+    arr:*) json_type="array";;
+    obj:*) json_type="object";;
+    num:*) json_type="number";;
+    true|false)
+           json_type="boolean";;
+    null)  json_type="null";;
+  esac
+
+  case $jpath in
+    "")
+      printf %s\\n "${json#???:}"
+      return 0
+      ;;
+    "["[0-9]*"]"*)
+      idx="${jpath%%"]"*}" idx="${idx#"["}"
+      jpath="${jpath#"["*"]"}"
+      ;;
+    "['"*"']"*)
+      key="${jpath%%"']"*}" key="${key#"['"}"
+      jpath="${jpath#"['"*"']"}"
+      ;;
+    "$"*)
+      jpath="${jpath#?}"
+      ;;
+    *) key="${jpath%%[".["]*}"
+      jpath="${jpath#"$key"}"
+      ;;
+  esac
+
+  if   [ "$key" -a "$json_type" = object ]; then
+    if ! json="$(DB2 "${json#obj:}" get "$key")"; then
+      debug "Key not found: \"$key\""
+      return 1
+    fi
+  elif [ "$idx" -a "$json_type" = array ]; then
+    if ! json="$(DB2 "${json#arr:}" get @ "$(( idx + 1 ))")"; then
+      debug "Array index not found: \"$idx\""
+      return 1
+    fi
+  elif [ "$key" ]; then
+    debug "Cannot select key (\"$key\") from value of type \"$json_type\""
+    return 1
+  elif [ "$idx" ]; then
+    debug "Cannot select index ($idx) from value of type \"$json_type\""
+    return 1
+  fi
+  json_get "$json" "$jpath"
+  return $?
+}
+
+json_dump_string() {
+  local in="$1" out=''
+  while [ "$in" ]; do case $in in
+    \\*) out="${out}\\\\"; in="${in#\\}" ;;
+    "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
+    "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
+    "   "*) out="${out}\\t"; in="${in#  }" ;;
+    \"*) out="${out}\\\""; in="${in#\"}" ;;
+    *) out="${out}${in%%[\\${CR}${BR}  \"]*}"; in="${in#"${in%%[\\${BR}${CR}   \"]*}"}" ;;
+  esac; done
+  printf '"%s"' "${out}"
+}
+
+json_dump_array() {
+  local json="$1" value out=''
+
+  for value in $(DB2 "$json" iterate @); do
+    out="${out},$(json_dump "$(UNSTRING "$value")")"
+  done
+  printf '[%s]' "${out#,}"
+}
+
+json_dump_object() {
+  local json="$1" key value out=''
+
+  while read -r key value; do
+    out="${out},$(json_dump_string "$(UNSTRING "$key")"):$(json_dump "$(UNSTRING "$value")")"
+  done <<-EOF
+       ${json}
+       EOF
+  printf '{%s}' "${out#,}"
+}
+
+json_dump() {
+  local json="$1"
+
+  case $json in
+    str:*)
+      json_dump_string "${json#str:}"
+      ;;
+    arr:*)
+      json_dump_array "${json#arr:}"
+      ;;
+    obj:*)
+      json_dump_object "${json#obj:}"
+      ;;
+    num:*)
+      printf "${json#num:}"
+      ;;
+    true|false|null)
+      printf %s\\n "$json"
+      ;;
+    *)
+      json_dump_string "${json}"
+      ;;
+  esac
+}
index 18fde077d6a99ec191d8aa016c34addd19c65fc7..be68a8acbe04ca0864917f92b0aef9541536429a 100755 (executable)
--- a/users.sh
+++ b/users.sh
@@ -17,8 +17,8 @@
 [ -n "$include_users" ] && return 0
 include_users="$0"
 
-. "${_EXEC}/cgilite/session.sh"
-. "${_EXEC}/cgilite/storage.sh"
+. "${_EXEC:-.}/cgilite/session.sh"
+. "${_EXEC:-.}/cgilite/storage.sh"
 
 SENDMAIL=${SENDMAIL-sendmail}
 
@@ -274,7 +274,7 @@ user_register(){
   fi
 
   if   [ "$USER_REQUIREEMAIL" = true ]; then
-    if [ ! "email" ]; then
+    if [ ! "$email" ]; then
       REDIRECT "${_BASE}${PATH_INFO}#ERROR_EMAIL_INVALID"
     elif user_emailexist "$email"; then
       REDIRECT "${_BASE}${PATH_INFO}#ERROR_EMAIL_EXISTS"
@@ -342,7 +342,7 @@ user_invite(){
   local email="$(POST email |user_checkemail)"
   local message="$(POST message)"
 
-  if [ ! "email" ]; then
+  if [ ! "$email" ]; then
     REDIRECT "${_BASE}${PATH_INFO}#ERROR_EMAIL_INVALID"
   elif user_emailexist "$email"; then
     REDIRECT "${_BASE}${PATH_INFO}#ERROR_EMAIL_EXISTS"