config.cgi 6.8 KB

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