index.cgi 4.5 KB

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