config.cgi 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. title="Wyze $camver on $camfirmware running wz_mini $hackver as $HOSTNAME"
  10. updated=false
  11. echo "HTTP/1.1 200"
  12. echo -e "Content-type: text/html\n\n"
  13. echo ""
  14. reboot_camera() {
  15. echo "rebooting camera (refreshing screen in 90 seconds)"
  16. echo '<script type="text/javascript">setTimeout(function(){ document.location.reload (); },90 * 1000)</script>'
  17. exit
  18. }
  19. shft() {
  20. # SE loop did not work -- thanks ash!
  21. suff=8
  22. while [ "$suff" -gt 0 ] ;
  23. do
  24. if [[ -f "$1.$suff" ]] ; then
  25. nxt=$((suff + 1))
  26. mv -f "$1.$suff" "$1.$nxt"
  27. fi
  28. suff=$((suff-1))
  29. done
  30. mv -f "$1" "$1.1"
  31. }
  32. function revert_config
  33. {
  34. mv "$hack_ini" "$hack_ini.old"
  35. mv "$hack_ini.$1" "$hack_ini"
  36. }
  37. function revert_menu
  38. {
  39. echo '<a href="revert" >Revert Menu</a>'
  40. echo '<div class="old_configs">'
  41. echo 'Prior Versions : '
  42. xuff=0
  43. while [ "$xuff" -lt 9 ] ;
  44. do
  45. xuff=$((xuff + 1))
  46. if [[ -f "$1.$xuff" ]] ; then
  47. echo '&nbsp;<a href="?action=revert&version='"$xuff"'">'"$xuff</a>"
  48. fi
  49. done
  50. echo '</div>'
  51. }
  52. if [[ $REQUEST_METHOD = 'GET' ]]; then
  53. #since ash does not handle arrays we create variables using eval
  54. IFS='&'
  55. for PAIR in $QUERY_STRING
  56. do
  57. K=$(echo $PAIR | cut -f1 -d=)
  58. VA=$(echo $PAIR | cut -f2 -d=)
  59. eval GET_$K=$VA
  60. done
  61. if [[ "$GET_action" = "reboot" ]]; then
  62. reboot_camera
  63. fi
  64. if [[ "$GET_action" = "revert" ]]; then
  65. revert_config "$GET_version"
  66. fi
  67. fi
  68. #test for post
  69. if [[ $REQUEST_METHOD = 'POST' ]]; then
  70. if [ "$CONTENT_LENGTH" -gt 0 ]; then
  71. read -n $CONTENT_LENGTH POST_DATA <&0
  72. while read line
  73. do eval "echo ${line}"
  74. done
  75. fi
  76. #since ash does not handle arrays we create variables using eval
  77. IFS='&'
  78. for PAIR in $POST_DATA
  79. do
  80. K=$(echo $PAIR | cut -f1 -d=)
  81. VA=$(echo $PAIR | cut -f2 -d=)
  82. VB=\"${VA//%3A/:}\"
  83. #echo "<div>$K=$VB</div>"
  84. eval POST_$K=\"$VB\"
  85. done
  86. #switch back to going through the config file
  87. IFS=$'\n'
  88. output="$hack_ini.new"
  89. #name our output file
  90. for ARGUMENT in $(cat $hack_ini)
  91. do
  92. #cycle through each line of the current config
  93. #copy through all comments
  94. if [[ ${ARGUMENT:0:1} == "#" ]] ; then
  95. #echo $ARGUMENT $'\n'
  96. echo -ne $ARGUMENT"\n" >> $output
  97. else
  98. #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
  99. KEY=$(echo $ARGUMENT | cut -f1 -d=)
  100. test=$(eval echo \$POST_$KEY)
  101. #echo "key was $KEY test was ... $test <br /> "
  102. if [[ "$test" ]]; then
  103. #if in the fake array then we use the new value
  104. #echo "<div style=\"color:#c00\">matched </div>"
  105. echo -ne $KEY=\"$test\""\n" >> $output
  106. else
  107. #if not in the fake array we use the current value
  108. #echo "<div>key not found</div>"
  109. echo -ne $ARGUMENT"\n" >> $output
  110. fi
  111. fi
  112. done
  113. shft $hack_ini
  114. mv $output $hack_ini
  115. updated=true
  116. fi
  117. function documentation_to_html
  118. {
  119. if [[ -f "$www_dir$1.md" ]]; then
  120. printf '<div class="ii_explain"><pre>'
  121. cat "$web_dir$1.md"
  122. printf '</pre></div>'
  123. fi
  124. }
  125. function ini_to_html_free
  126. {
  127. 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
  128. documentation_to_html $1
  129. printf '</div>'
  130. }
  131. function ini_to_html_tf
  132. {
  133. printf '<div class="ii"><div class="ii_key_DIV">%s</div>' $1
  134. printf '<div class="ii_value_DIV">'
  135. if [[ "$2" == "true" ]]; then
  136. printf '<input class="ii_radio" type="radio" name="%s" value="true" checked="checked" /> True &nbsp;' $1
  137. printf '<input class="ii_radio" type="radio" name="%s" value="false" /> False &nbsp;' $1
  138. else
  139. printf '<input class="ii_radio" type="radio" name="%s" value="true" /> True &nbsp;' $1
  140. printf '<input class="ii_radio" type="radio" name="%s" value="false" checked="checked" /> False &nbsp;' $1
  141. fi
  142. printf '</div>'
  143. documentation_to_html $1
  144. printf '</div>'
  145. }
  146. #function to handle camera feed
  147. function html_cam_feed
  148. {
  149. printf '<img id="current_feed" src="/cgi-bin/jpeg.cgi?channel=1" class="feed" />'
  150. }
  151. echo -ne "<html><head><title>$title</title>"
  152. echo -ne "<style type=\"text/css\">"
  153. cat wz_mini_web.css
  154. echo -ne '</style>';
  155. echo '<script type="text/javascript" src="/config.js" ></script>'
  156. echo -ne "</head>"
  157. echo -ne '<body>'
  158. echo -ne "<h1>$title</h1>";
  159. if [ "$updated" = true ];
  160. then
  161. echo '<div class="message_DIV">configuration file updated. <a href="?action=reboot">Reboot<a/> to use changes. Or <a href="#revert">Revert</a> to a prior configuration</div>';
  162. fi
  163. html_cam_feed
  164. echo -ne '<form name="wz_mini_hack_FORM" method="POST" enctype="application/x-www-form-urlencoded" >'
  165. IFS=$'\n'
  166. for ARGUMENT in $(cat $hack_ini)
  167. do
  168. if [[ ${ARGUMENT:0:1} == "#" ]] ; then
  169. echo -ne '<div class="ii_info">'$ARGUMENT'</div>'
  170. else
  171. KEY=$(echo $ARGUMENT | cut -f1 -d=)
  172. VAL=$(echo $ARGUMENT | cut -f2 -d=)
  173. VALUE=${VAL//\"/}
  174. case "$VALUE" in
  175. "true") ini_to_html_tf $KEY $VALUE ;;
  176. "false") ini_to_html_tf $KEY $VALUE ;;
  177. *) ini_to_html_free $KEY $VALUE
  178. esac
  179. fi
  180. done
  181. echo -ne '<input type="submit" name="update" value="Update" />'
  182. echo -ne '</form>'
  183. revert_menu $hack_ini
  184. html_cam_feed_js
  185. echo -ne '</body></html>'