config.cgi 7.3 KB

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