|
|
@ -2,144 +2,156 @@ |
|
|
|
$ #!/bin/bash |
|
|
|
$ #!/bin/bash |
|
|
|
Content-Type: text/html |
|
|
|
Content-Type: text/html |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<{ |
|
|
|
# Constants |
|
|
|
# Constants |
|
|
|
# ========= |
|
|
|
# ========= |
|
|
|
$ true=1; |
|
|
|
true=1; |
|
|
|
$ false=0; |
|
|
|
false=0; |
|
|
|
# |
|
|
|
|
|
|
|
$ restaurant_file="restaurants.txt"; |
|
|
|
restaurant_file="restaurants.txt"; |
|
|
|
$ title="FeedMe"; |
|
|
|
title="FeedMe"; |
|
|
|
# |
|
|
|
|
|
|
|
# Components |
|
|
|
# Components |
|
|
|
# ========== |
|
|
|
# ========== |
|
|
|
# |
|
|
|
|
|
|
|
# $1: string: input value |
|
|
|
# $1: string: input value |
|
|
|
# $2: number: 0=existing restaurant, 1=new restaurant |
|
|
|
# $2: number: 0=existing restaurant, 1=new restaurant |
|
|
|
$ restaurant_form () { |
|
|
|
restaurant_form () { |
|
|
|
$ enforce_arguments "${FUNCNAME[0]}" "$#" 2; |
|
|
|
enforce_arguments "${FUNCNAME[0]}" "$#" 2; |
|
|
|
$ local icon_class; |
|
|
|
local icon_class; |
|
|
|
$ if [ "$2" -eq 0 ]; then |
|
|
|
if [ "$2" -eq 0 ]; then |
|
|
|
$ icon_class="ri-save-2-fill"; |
|
|
|
icon_class="ri-save-2-fill"; |
|
|
|
$ else |
|
|
|
else |
|
|
|
$ icon_class="ri-add-line"; |
|
|
|
icon_class="ri-add-line"; |
|
|
|
$ fi |
|
|
|
fi |
|
|
|
<form class="restaurant-form" method="POST" action=""> |
|
|
|
}> |
|
|
|
<input type="hidden" name="old" value="<% $1 %>" /> |
|
|
|
<form class="restaurant-form" method="POST" action=""> |
|
|
|
<input type="text" placeholder="Restaurant" name="new" value="<% $1 %>" /> |
|
|
|
<input type="hidden" name="old" value="<% $1 %>" /> |
|
|
|
<label class="button <% $icon_class %>"> |
|
|
|
<input |
|
|
|
$ local action; |
|
|
|
type="text" |
|
|
|
$ if [ "$2" -eq 0 ]; then |
|
|
|
placeholder="Restaurant" |
|
|
|
$ action="save"; |
|
|
|
name="new" |
|
|
|
$ else |
|
|
|
value="<% $1 %>" |
|
|
|
$ action="add"; |
|
|
|
/> |
|
|
|
$ fi; |
|
|
|
<label class="button <% $icon_class %>"> |
|
|
|
<input type="submit" name="action" value="<% $action %>" /> |
|
|
|
$ local action; |
|
|
|
</label> |
|
|
|
|
|
|
|
$ if [ "$2" -eq 0 ]; then |
|
|
|
$ if [ "$2" -eq 0 ]; then |
|
|
|
<label class="button ri-delete-bin-2-fill"> |
|
|
|
$ action="save"; |
|
|
|
<input type="submit" name="action" value="delete" /> |
|
|
|
$ else |
|
|
|
</label> |
|
|
|
$ action="add"; |
|
|
|
$ fi |
|
|
|
$ fi; |
|
|
|
</form> |
|
|
|
<input type="submit" name="action" value="<% $action %>" /> |
|
|
|
$ } |
|
|
|
</label> |
|
|
|
# |
|
|
|
$ if [ "$2" -eq 0 ]; then |
|
|
|
|
|
|
|
<label class="button ri-delete-bin-2-fill"> |
|
|
|
|
|
|
|
<input type="submit" name="action" value="delete" /> |
|
|
|
|
|
|
|
</label> |
|
|
|
|
|
|
|
$ fi |
|
|
|
|
|
|
|
</form> |
|
|
|
|
|
|
|
<{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# Utility Functions |
|
|
|
# Utility Functions |
|
|
|
# ================= |
|
|
|
# ================= |
|
|
|
# |
|
|
|
|
|
|
|
# $1: string: function name |
|
|
|
# $1: string: function name |
|
|
|
# $2: number: $# |
|
|
|
# $2: number: $# |
|
|
|
# $3: number: expected number of arguments |
|
|
|
# $3: number: expected number of arguments |
|
|
|
$ enforce_arguments () { |
|
|
|
enforce_arguments () { |
|
|
|
$ if [ "$2" -lt "$3" ]; then |
|
|
|
if [ "$2" -lt "$3" ]; then |
|
|
|
$ printf '%s %s %s\n' 'Function:' "$1" 'called with too few arguments'; |
|
|
|
printf '%s %s %s\n' 'Function:' "$1" 'called with too few arguments'; |
|
|
|
$ exit 1; |
|
|
|
exit 1; |
|
|
|
$ elif [ "$2" -gt "$3" ]; then |
|
|
|
elif [ "$2" -gt "$3" ]; then |
|
|
|
$ printf '%s %s %s\n' 'Function:' "$1" 'called with too many arguments'; |
|
|
|
printf '%s %s %s\n' 'Function:' "$1" 'called with too many arguments'; |
|
|
|
$ exit 1; |
|
|
|
exit 1; |
|
|
|
$ fi; |
|
|
|
fi; |
|
|
|
$ } |
|
|
|
} |
|
|
|
# |
|
|
|
|
|
|
|
$ decode_uri () { |
|
|
|
decode_uri () { |
|
|
|
$ local i="${*//+/ }"; |
|
|
|
local i="${*//+/ }"; |
|
|
|
$ echo -e "${i//%/\\x}"; |
|
|
|
echo -e "${i//%/\\x}"; |
|
|
|
$ } |
|
|
|
} |
|
|
|
# |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# Restaurant List Functions |
|
|
|
# Restaurant List Functions |
|
|
|
# ========================= |
|
|
|
# ========================= |
|
|
|
# |
|
|
|
|
|
|
|
# $1: string: restaurant to remove |
|
|
|
# $1: string: restaurant to remove |
|
|
|
$ remove_restaurant () { |
|
|
|
remove_restaurant () { |
|
|
|
$ enforce_arguments "${FUNCNAME[0]}" "$#" 1; |
|
|
|
enforce_arguments "${FUNCNAME[0]}" "$#" 1; |
|
|
|
$ local temp_file; |
|
|
|
local temp_file; |
|
|
|
$ temp_file=$(mktemp); |
|
|
|
temp_file=$(mktemp); |
|
|
|
$ grep -Fxv "$1" "$restaurant_file" > "$temp_file"; |
|
|
|
grep -Fxv "$1" "$restaurant_file" > "$temp_file"; |
|
|
|
$ mv "$temp_file" "$restaurant_file"; |
|
|
|
mv "$temp_file" "$restaurant_file"; |
|
|
|
$ } |
|
|
|
} |
|
|
|
# |
|
|
|
|
|
|
|
# $1: string: restuarant to add |
|
|
|
# $1: string: restuarant to add |
|
|
|
$ add_restaurant () { |
|
|
|
add_restaurant () { |
|
|
|
$ enforce_arguments "${FUNCNAME[0]}" "$#" 1; |
|
|
|
enforce_arguments "${FUNCNAME[0]}" "$#" 1; |
|
|
|
$ local temp_file; |
|
|
|
local temp_file; |
|
|
|
$ temp_file=$(mktemp); |
|
|
|
temp_file=$(mktemp); |
|
|
|
$ (grep -Fxv "$1" "$restaurant_file"; printf '%s\n' "$1") > "$temp_file"; |
|
|
|
(grep -Fxv "$1" "$restaurant_file"; printf '%s\n' "$1") > "$temp_file"; |
|
|
|
$ mv "$temp_file" "$restaurant_file"; |
|
|
|
mv "$temp_file" "$restaurant_file"; |
|
|
|
$ } |
|
|
|
} |
|
|
|
# |
|
|
|
|
|
|
|
# $1 (optional): number: 0=don't overwrite, 1=overwrite |
|
|
|
# $1 (optional): number: 0=don't overwrite, 1=overwrite |
|
|
|
$ todays_restaurant () { |
|
|
|
todays_restaurant () { |
|
|
|
$ local today; |
|
|
|
local today; |
|
|
|
$ today="$(date +'%Y%m%d')"; |
|
|
|
today="$(date +'%Y%m%d')"; |
|
|
|
$ local filename="$today.pick"; |
|
|
|
local filename="$today.pick"; |
|
|
|
$ if [ -n "$1" ] && [ "$1" -ne 0 ]; then |
|
|
|
if [ -n "$1" ] && [ "$1" -ne 0 ]; then |
|
|
|
$ rm "$filename"; |
|
|
|
rm "$filename"; |
|
|
|
$ fi; |
|
|
|
fi; |
|
|
|
$ if [ ! -f "$filename" ]; then |
|
|
|
if [ ! -f "$filename" ]; then |
|
|
|
$ shuf -n 1 "$restaurant_file" > "$filename"; |
|
|
|
shuf -n 1 "$restaurant_file" > "$filename"; |
|
|
|
$ fi; |
|
|
|
fi; |
|
|
|
$ cat "$filename"; |
|
|
|
cat "$filename"; |
|
|
|
$ } |
|
|
|
} |
|
|
|
# |
|
|
|
|
|
|
|
# Initialization |
|
|
|
# Initialization |
|
|
|
# ============== |
|
|
|
# ============== |
|
|
|
# |
|
|
|
|
|
|
|
# Make sure restaurant file eixsts |
|
|
|
# Make sure restaurant file eixsts |
|
|
|
$ touch -a "$restaurant_file"; |
|
|
|
touch -a "$restaurant_file"; |
|
|
|
# |
|
|
|
|
|
|
|
# Parse body |
|
|
|
# Parse body |
|
|
|
# ---------- |
|
|
|
# ---------- |
|
|
|
$ declare -A body; |
|
|
|
declare -A body; |
|
|
|
$ while IFS= read -d '&' -r pair || [ "$pair" ]; do |
|
|
|
while IFS= read -d '&' -r pair || [ "$pair" ]; do |
|
|
|
$ name=$(decode_uri "${pair%%=*}"); |
|
|
|
name=$(decode_uri "${pair%%=*}"); |
|
|
|
$ value=$(decode_uri "${pair#*=}"); |
|
|
|
value=$(decode_uri "${pair#*=}"); |
|
|
|
$ if [ -n "$name" ]; then |
|
|
|
if [ -n "$name" ]; then |
|
|
|
$ body["$name"]="$value"; |
|
|
|
body["$name"]="$value"; |
|
|
|
$ fi; |
|
|
|
fi; |
|
|
|
$ done #> |
|
|
|
done |
|
|
|
# |
|
|
|
|
|
|
|
# Perform requested action |
|
|
|
# Perform requested action |
|
|
|
# ------------------------ |
|
|
|
# ------------------------ |
|
|
|
$ case "${body['action']}" in |
|
|
|
case "${body['action']}" in |
|
|
|
$ add) |
|
|
|
add) |
|
|
|
$ add_restaurant "${body['new']}"; |
|
|
|
add_restaurant "${body['new']}"; |
|
|
|
$ ;; |
|
|
|
;; |
|
|
|
$ delete) |
|
|
|
delete) |
|
|
|
$ remove_restaurant "${body['old']}"; |
|
|
|
remove_restaurant "${body['old']}"; |
|
|
|
$ ;; |
|
|
|
;; |
|
|
|
$ save) |
|
|
|
save) |
|
|
|
$ remove_restaurant "${body['old']}"; |
|
|
|
remove_restaurant "${body['old']}"; |
|
|
|
$ add_restaurant "${body['new']}"; |
|
|
|
add_restaurant "${body['new']}"; |
|
|
|
$ ;; |
|
|
|
;; |
|
|
|
$ new_restaurant) |
|
|
|
new_restaurant) |
|
|
|
$ todays_restaurant $true >/dev/null; |
|
|
|
todays_restaurant $true >/dev/null; |
|
|
|
$ ;; |
|
|
|
;; |
|
|
|
$ esac |
|
|
|
esac |
|
|
|
# |
|
|
|
|
|
|
|
# |
|
|
|
}><!doctype html> |
|
|
|
<!doctype html> |
|
|
|
|
|
|
|
<html> |
|
|
|
<html> |
|
|
|
<head> |
|
|
|
<head> |
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
|
|
|
|
|
<meta property="og:title" content="FeedMe"> |
|
|
|
|
|
|
|
<meta property="og:type" content="website"> |
|
|
|
|
|
|
|
<meta property="og:url" content="https://n0m.org/feedme"> |
|
|
|
|
|
|
|
<meta property="og:image" content="https://n0m.org/feedme/images/food.png"> |
|
|
|
|
|
|
|
<meta property="og:description" content="Today's Restaurant: <$% todays_restaurant %>"> |
|
|
|
<title><% $title %></title> |
|
|
|
<title><% $title %></title> |
|
|
|
<style> |
|
|
|
<style> |
|
|
|
:root { |
|
|
|
:root { |
|
|
|