index.cgi 4.4 KB

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