config.cgi 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/bin/sh
  2. # This serves a rudimentary webpage based on wz_mini.conf
  3. base_dir=/opt/wz_mini/
  4. base_hack_ini=/opt/wz_mini/wz_mini.conf
  5. hack_ini=$base_hack_ini
  6. www_dir=/opt/wz_mini/www/cgi-bin/
  7. camver=V3
  8. camfirmware=$(tail -n1 /configs/app.ver | cut -f2 -d= )
  9. hackver=$(cat /opt/wz_mini/usr/bin/app.ver)
  10. title="Wyze $camver on $camfirmware running wz_mini $hackver as $HOSTNAME"
  11. updated=false
  12. echo "HTTP/1.1 200"
  13. echo -e "Content-type: text/html\n\n"
  14. echo ""
  15. reboot_camera() {
  16. echo "rebooting camera (refreshing screen in 90 seconds)"
  17. echo '<script type="text/javascript">setTimeout(function(){ document.location.reload (); },90 * 1000)</script>'
  18. exit
  19. }
  20. shft() {
  21. # SE loop did not work -- thanks ash!
  22. suff=8
  23. while [ "$suff" -gt 0 ] ;
  24. do
  25. if [[ -f "$1.$suff" ]] ; then
  26. nxt=$((suff + 1))
  27. mv -f "$1.$suff" "$1.$nxt"
  28. fi
  29. suff=$((suff-1))
  30. done
  31. mv -f "$1" "$1.1"
  32. }
  33. function revert_config
  34. {
  35. mv "$hack_ini" "$hack_ini.old"
  36. mv "$hack_ini.$1" "$hack_ini"
  37. }
  38. function revert_menu
  39. {
  40. echo '<a href="revert" >Revert Menu</a>'
  41. echo '<div class="old_configs">'
  42. echo 'Prior Versions : '
  43. xuff=0
  44. while [ "$xuff" -lt 9 ] ;
  45. do
  46. xuff=$((xuff + 1))
  47. if [[ -f "$1.$xuff" ]] ; then
  48. filedate=$(date -r "$1.$xuff" )
  49. class=""
  50. if [ "$1.$xuff" = "$2" ];
  51. then
  52. class="current_revert"
  53. fi
  54. echo '<div class="revert_DIV '$class'"><div><a href="?action=show_revert&version='"$xuff"'">'"$xuff </a></div><div> $filedate</div></div>"
  55. fi
  56. done
  57. echo '</div>'
  58. }
  59. function version_info
  60. {
  61. echo "<div id='$1'>"
  62. echo "<div class='ver_DIV' vertype='Camera'>$camver</div>"
  63. echo "<div class='ver_DIV' vertype='Camera Firmware'>$camfirmware</div>"
  64. echo "<div class='ver_DIV' vertype='wz_mini'>$hackver</div>"
  65. echo "<div class='ver_DIV' vertype='Hostname'> $HOSTNAME</div>"
  66. echo "</div>"
  67. }
  68. if [[ $REQUEST_METHOD = 'GET' ]]; then
  69. #since ash does not handle arrays we create variables using eval
  70. IFS='&'
  71. for PAIR in $QUERY_STRING
  72. do
  73. K=$(echo $PAIR | cut -f1 -d=)
  74. VA=$(echo $PAIR | cut -f2 -d=)
  75. eval GET_$K=$VA
  76. done
  77. if [[ "$GET_action" = "reboot" ]]; then
  78. reboot_camera
  79. fi
  80. if [[ "$GET_action" = "revert" ]]; then
  81. revert_config "$GET_version"
  82. fi
  83. if [[ "$GET_action" = "show_revert" ]]; then
  84. hack_ini="$hack_ini.$GET_version"
  85. fi
  86. fi
  87. #test for post
  88. if [[ $REQUEST_METHOD = 'POST' ]]; then
  89. if [ "$CONTENT_LENGTH" -gt 0 ]; then
  90. read -n $CONTENT_LENGTH POST_DATA <&0
  91. while read line
  92. do eval "echo ${line}"
  93. done
  94. fi
  95. #since ash does not handle arrays we create variables using eval
  96. IFS='&'
  97. for PAIR in $POST_DATA
  98. do
  99. K=$(echo $PAIR | cut -f1 -d=)
  100. VA=$(echo $PAIR | cut -f2 -d=)
  101. VB=\"${VA//%3A/:}\"
  102. #echo "<div>$K=$VB</div>"
  103. eval POST_$K=\"$VB\"
  104. done
  105. #switch back to going through the config file
  106. output="$hack_ini.new"
  107. #name our output file
  108. while IFS= read -r \ARGUMENT; do
  109. #cycle through each line of the current config
  110. #copy through all comments
  111. if [ -z "$ARGUMENT" ]; then
  112. echo -ne "\n" >> $output
  113. elif [[ ${ARGUMENT:0:1} == "#" ]] ; then
  114. #echo $ARGUMENT $'\n'
  115. echo -ne $ARGUMENT"\n" >> $output
  116. else
  117. #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
  118. KEY=$(echo $ARGUMENT | cut -f1 -d=)
  119. test=$(eval echo \$POST_$KEY)
  120. #echo "key was $KEY test was ... $test <br /> "
  121. if [[ "$test" ]]; then
  122. #if in the fake array then we use the new value
  123. #echo "<div style=\"color:#c00\">matched </div>"
  124. echo -ne $KEY=\"$test\""\n" >> $output
  125. else
  126. #if not in the fake array we use the current value
  127. #echo "<div>key not found</div>"
  128. echo -ne $ARGUMENT"\n" >> $output
  129. fi
  130. fi
  131. done < $hack_ini
  132. shft $hack_ini
  133. mv $output $hack_ini
  134. updated=true
  135. fi
  136. function documentation_to_html
  137. {
  138. if [[ -f "$www_dir$1.md" ]]; then
  139. printf '<div class="ii_explain"><pre>'
  140. cat "$web_dir$1.md"
  141. printf '</pre></div>'
  142. fi
  143. }
  144. function ini_to_html_free
  145. {
  146. classes=""
  147. if [ "$1" = "USB_DIRECT_MAC_ADDR" ]; then
  148. classes=" mac_addr"
  149. fi
  150. if grep -q -wi "$1" numerics.txt; then
  151. classes=" numeric"
  152. fi
  153. printf '<div class="ii"><div class="ii_key_DIV">%s</div><div class="ii_value_DIV"><input class="ii_value'$classes'" type="text" name="%s" value="%s" /></div>' $1 $1 $2
  154. documentation_to_html $1
  155. printf '</div>'
  156. }
  157. function ini_to_html_tf
  158. {
  159. printf '<div class="ii"><div class="ii_key_DIV">%s</div>' $1
  160. printf '<div class="ii_value_DIV">'
  161. if [[ "$2" == "true" ]]; then
  162. printf '<input class="ii_radio" type="radio" name="%s" value="true" checked="checked" /> True &nbsp;' $1
  163. printf '<input class="ii_radio" type="radio" name="%s" value="false" /> False &nbsp;' $1
  164. else
  165. printf '<input class="ii_radio" type="radio" name="%s" value="true" /> True &nbsp;' $1
  166. printf '<input class="ii_radio" type="radio" name="%s" value="false" checked="checked" /> False &nbsp;' $1
  167. fi
  168. printf '</div>'
  169. documentation_to_html $1
  170. printf '</div>'
  171. }
  172. #function to handle camera feed
  173. function html_cam_feed
  174. {
  175. printf '<img id="current_feed" src="/cgi-bin/jpeg.cgi?channel=1" class="feed" />'
  176. }
  177. function handle_css
  178. {
  179. echo -ne "<style type=\"text/css\">"
  180. cat config.css
  181. echo -ne '</style>';
  182. }
  183. echo -ne "<html><head><title>$title</title>"
  184. handle_css wz_mini_web.css
  185. echo '<script type="text/javascript" src="/config.js" ></script>'
  186. echo -ne "</head>"
  187. echo -ne '<body>'
  188. echo -ne "<h1>$title</h1>";
  189. if [ "$updated" = true ];
  190. then
  191. 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>';
  192. fi
  193. html_cam_feed
  194. if [ $base_hack_ini != $hack_ini ]; then
  195. echo '<div><a href="?action=revert&version='$GET_version'">Revert</a> to this version</a></div>'
  196. fi
  197. echo -ne '<form name="update_config" method="POST" enctype="application/x-www-form-urlencoded" >'
  198. CONFIG_BLOCK=0
  199. while IFS= read -r ARGUMENT; do
  200. if [ -z "$ARGUMENT" ] ; then
  201. echo -ne ""
  202. elif [[ ${ARGUMENT:0:1} == "#" ]] ; then
  203. if [[ ${ARGUMENT:0:4} == "####" ]]; then
  204. if [ "$CONFIG_BLOCK" -gt 0 ]; then
  205. echo '</div>'
  206. fi
  207. CONFIG_BLOCK=$((CONFIG_BLOCK + 1))
  208. echo '<div class="ii_block" block_number="'$CONFIG_BLOCK'" >'
  209. BTITLE=${ARGUMENT//#/ }
  210. echo -ne '<div class="ii_block_name">'$BTITLE'</div>'
  211. else
  212. echo -ne '<div class="ii_info">'$ARGUMENT'</div>'
  213. fi
  214. else
  215. KEY=$(echo $ARGUMENT | cut -f1 -d=)
  216. VAL=$(echo $ARGUMENT | cut -f2 -d=)
  217. VALUE=${VAL//\"/}
  218. case "$VALUE" in
  219. "true") ini_to_html_tf $KEY $VALUE ;;
  220. "false") ini_to_html_tf $KEY $VALUE ;;
  221. *) ini_to_html_free $KEY $VALUE
  222. esac
  223. fi
  224. done < $hack_ini
  225. if [ "$CONFIG_BLOCK" -gt 0 ]; then
  226. echo '</div>'
  227. fi
  228. echo -ne '<input type="submit" name="update" value="Update" />'
  229. echo -ne '</form>'
  230. revert_menu $base_hack_ini $hack_ini
  231. version_info "display_BAR"
  232. echo -ne '</body></html>'