wz_mini_web.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/sh
  2. # This serves a rudimentary webpage based on wz_mini.conf
  3. hack_ini=/opt/wz_mini/wz_mini.conf
  4. camver=V3
  5. camfirmware=$(tail -n1 /configs/app.ver | cut -f2 -d= )
  6. hackver="unknown"
  7. hostname=$(uname -n)
  8. title="Wyze $camver on $camfirmware running wz_mini $hackver as $hostname"
  9. echo "HTTP/1.1 200"
  10. echo -e "Content-type: text/html\n\n"
  11. echo ""
  12. shft() {
  13. # https://stackoverflow.com/questions/3690936/change-file-name-suffixes-using-sed/3691279#3691279
  14. # Change this '8' to one less than your desired maximum rollover file.
  15. # Must be in reverse order for renames to work (n..1, not 1..n).
  16. for suff in {8..1} ; do
  17. if [[ -f "$1.${suff}" ]] ; then
  18. ((nxt = suff + 1))
  19. mv -f "$1.${suff}" "$1.${nxt}"
  20. fi
  21. done
  22. mv -f "$1" "$1.1"
  23. }
  24. #test for post
  25. if [[ $REQUEST_METHOD = 'POST' ]]; then
  26. if [ "$CONTENT_LENGTH" -gt 0 ]; then
  27. read -n $CONTENT_LENGTH POST_DATA <&0
  28. while read line
  29. do eval "echo ${line}"
  30. done
  31. fi
  32. #since ash does not handle arrays we create variables using eval
  33. IFS='&'
  34. for PAIR in $POST_DATA
  35. do
  36. K=$(echo $PAIR | cut -f1 -d=)
  37. VA=$(echo $PAIR | cut -f2 -d=)
  38. VB=\"${VA//%3A/:}\"
  39. #echo "<div>$K=$VB</div>"
  40. eval POST_$K=\"$VB\"
  41. done
  42. #switch back to going through the config file
  43. IFS=$'\n'
  44. output=wz_mini.conf.new
  45. shft $output
  46. #name our output file
  47. for ARGUMENT in $(cat $hack_ini)
  48. do
  49. #cycle through each line of the current config
  50. #copy through all comments
  51. if [[ ${ARGUMENT:0:1} == "#" ]] ; then
  52. #echo $ARGUMENT $'\n'
  53. echo $ARGUMENT >> $output
  54. else
  55. #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
  56. KEY=$(echo $ARGUMENT | cut -f1 -d=)
  57. test=$(eval echo \$POST_$KEY)
  58. #echo "key was $KEY test was ... $test <br /> "
  59. if [[ "$test" ]]; then
  60. #if in the fake array then we use the new value
  61. #echo "<div style=\"color:#c00\">matched </div>"
  62. echo $KEY="$test" >> $output
  63. else
  64. #if not in the fake array we use the current value
  65. #echo "<div>key not found</div>"
  66. echo $ARGUMENT >> $output
  67. fi
  68. fi
  69. done
  70. fi
  71. function ini_to_html_free
  72. {
  73. printf '<div class="ii"><div class="ii_key_DIV">%s</div><div class="ii_value_DIV"><input class="ii_value" type="text" name="%s" value="%s" /></div></div>' $1 $1 $2
  74. }
  75. function ini_to_html_tf
  76. {
  77. printf '<div class="ii"><div class="ii_key_DIV">%s</div>' $1
  78. printf '<div class="ii_value_DIV">'
  79. if [[ "$2" == "true" ]]; then
  80. printf '<input class="ii_radio" type="radio" name="%s" value="true" checked="checked" /> True &nbsp;' $1
  81. printf '<input class="ii_radio" type="radio" name="%s" value="false" /> False &nbsp;' $1
  82. else
  83. printf '<input class="ii_radio" type="radio" name="%s" value="true" /> True &nbsp;' $1
  84. printf '<input class="ii_radio" type="radio" name="%s" value="false" checked="checked" /> False &nbsp;' $1
  85. fi
  86. printf '</div></div>'
  87. }
  88. echo -ne "<html><head><title>$title</title>"
  89. echo -ne "<style type=\"text/css\">"
  90. cat wz_mini_web.css
  91. echo -ne '</style>';
  92. echo -ne "</head>"
  93. echo -ne '<body>'
  94. echo -ne "<h1>$title</h1>";
  95. echo -ne '<form name="wz_mini_hack_FORM" method="POST" enctype="application/x-www-form-urlencoded" >'
  96. IFS=$'\n'
  97. for ARGUMENT in $(cat $hack_ini)
  98. do
  99. if [[ ${ARGUMENT:0:1} == "#" ]] ; then
  100. echo -ne '<div class="ii_info">'$ARGUMENT'</div>'
  101. else
  102. KEY=$(echo $ARGUMENT | cut -f1 -d=)
  103. VAL=$(echo $ARGUMENT | cut -f2 -d=)
  104. VALUE=${VAL//\"/}
  105. case "$VALUE" in
  106. "true") ini_to_html_tf $KEY $VALUE ;;
  107. "false") ini_to_html_tf $KEY $VALUE ;;
  108. *) ini_to_html_free $KEY $VALUE
  109. esac
  110. fi
  111. done
  112. echo -ne '<input type="submit" name="update" value="Update" />'
  113. echo -ne '</form>'
  114. echo -ne '</body></html>'