#!/bin/sh # This serves a rudimentary webpage based on wz_mini.conf base_dir=/opt/wz_mini/ hack_ini=/opt/wz_mini/wz_mini.conf www_dir=/opt/wz_mini/www/cgi-bin/ camver=V3 camfirmware=$(tail -n1 /configs/app.ver | cut -f2 -d= ) hackver=$(cat /opt/wz_mini/usr/bin/app.ver) title="Wyze $camver on $camfirmware running wz_mini $hackver as $HOSTNAME" updated=false echo "HTTP/1.1 200" echo -e "Content-type: text/html\n\n" echo "" reboot_camera() { echo "rebooting camera (refreshing screen in 90 seconds)" echo '' exit } shft() { # SE loop did not work -- thanks ash! suff=8 while [ "$suff" -gt 0 ] ; do if [[ -f "$1.$suff" ]] ; then nxt=$((suff + 1)) mv -f "$1.$suff" "$1.$nxt" fi suff=$((suff-1)) done mv -f "$1" "$1.1" } function revert_config { mv "$hack_ini" "$hack_ini.old" mv "$hack_ini.$1" "$hack_ini" } function revert_menu { echo 'Revert Menu' echo '
' echo 'Prior Versions : ' xuff=0 while [ "$xuff" -lt 9 ] ; do xuff=$((xuff + 1)) if [[ -f "$1.$xuff" ]] ; then echo ' '"$xuff" fi done echo '
' } if [[ $REQUEST_METHOD = 'GET' ]]; then #since ash does not handle arrays we create variables using eval IFS='&' for PAIR in $QUERY_STRING do K=$(echo $PAIR | cut -f1 -d=) VA=$(echo $PAIR | cut -f2 -d=) eval GET_$K=$VA done if [[ "$GET_action" = "reboot" ]]; then reboot_camera fi if [[ "$GET_action" = "revert" ]]; then revert_config "$GET_version" fi fi #test for post if [[ $REQUEST_METHOD = 'POST' ]]; then if [ "$CONTENT_LENGTH" -gt 0 ]; then read -n $CONTENT_LENGTH POST_DATA <&0 while read line do eval "echo ${line}" done fi #since ash does not handle arrays we create variables using eval IFS='&' for PAIR in $POST_DATA do K=$(echo $PAIR | cut -f1 -d=) VA=$(echo $PAIR | cut -f2 -d=) VB=\"${VA//%3A/:}\" #echo "
$K=$VB
" eval POST_$K=\"$VB\" done #switch back to going through the config file IFS=$'\n' output="$hack_ini.new" #name our output file for ARGUMENT in $(cat $hack_ini) do #cycle through each line of the current config #copy through all comments if [[ ${ARGUMENT:0:1} == "#" ]] ; then #echo $ARGUMENT $'\n' echo -ne $ARGUMENT"\n" >> $output else #for non-comments check to see if we have an entry in the POST data by deciphering the key from the ini file and using eval for our fake array KEY=$(echo $ARGUMENT | cut -f1 -d=) test=$(eval echo \$POST_$KEY) #echo "key was $KEY test was ... $test
" if [[ "$test" ]]; then #if in the fake array then we use the new value #echo "
matched
" echo -ne $KEY=\"$test\""\n" >> $output else #if not in the fake array we use the current value #echo "
key not found
" echo -ne $ARGUMENT"\n" >> $output fi fi done shft $hack_ini mv $output $hack_ini updated=true fi function documentation_to_html { if [[ -f "$www_dir$1.md" ]]; then printf '
'
                cat "$web_dir$1.md"
                printf '
' fi } function ini_to_html_free { printf '
%s
' $1 $1 $2 documentation_to_html $1 printf '
' } function ini_to_html_tf { printf '
%s
' $1 printf '
' if [[ "$2" == "true" ]]; then printf ' True  ' $1 printf ' False  ' $1 else printf ' True  ' $1 printf ' False  ' $1 fi printf '
' documentation_to_html $1 printf '
' } #function to handle camera feed function html_cam_feed { printf '' } echo -ne "$title" echo -ne "'; echo '' echo -ne "" echo -ne '' echo -ne "

$title

"; if [ "$updated" = true ]; then echo '
configuration file updated. Reboot to use changes. Or Revert to a prior configuration
'; fi html_cam_feed echo -ne '
' IFS=$'\n' for ARGUMENT in $(cat $hack_ini) do if [[ ${ARGUMENT:0:1} == "#" ]] ; then echo -ne '
'$ARGUMENT'
' else KEY=$(echo $ARGUMENT | cut -f1 -d=) VAL=$(echo $ARGUMENT | cut -f2 -d=) VALUE=${VAL//\"/} case "$VALUE" in "true") ini_to_html_tf $KEY $VALUE ;; "false") ini_to_html_tf $KEY $VALUE ;; *) ini_to_html_free $KEY $VALUE esac fi done echo -ne '' echo -ne '
' revert_menu $hack_ini html_cam_feed_js echo -ne ''