config.cgi 6.9 KB

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