libcgi.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/bin/bash
  2. # Author: darren chamberlain <dlc@users.sourceforge.net>
  3. # Co-Author: Paul Bournival <paulb-ns@cajun.nu>
  4. #
  5. # bashlib is used by sourcing it at the beginning of scripts that
  6. # needs its functionality (by using the . or source commands).
  7. PATH=/bin:/usr/bin
  8. #
  9. # Set version number
  10. #
  11. VERSION=$(/bin/echo '$Revision: 1.3 $' | /usr/bin/awk '{print $2}')
  12. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  13. # Initialization stuff begins here. These things run immediately, and
  14. # do the parameter/cookie parsing.
  15. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  16. # Global debug flag. Set to 0 to disable debugging throughout the lib
  17. DEBUG=0
  18. # capture stdin for POST methods. POST requests don't always come in
  19. # with a newline attached, so we use cat to grab stdin and append a newline.
  20. # This is a wonderful hack, and thanks to paulb.
  21. STDIN=$(/bin/cat)
  22. if [ -n "${STDIN}" ]; then
  23. QUERY_STRING="${STDIN}&${QUERY_STRING}"
  24. fi
  25. # Handle GET and POST requests... (the QUERY_STRING will be set)
  26. if [ -n "${QUERY_STRING}" ]; then
  27. # name=value params, separated by either '&' or ';'
  28. if echo ${QUERY_STRING} | grep '=' >/dev/null ; then
  29. for Q in $(/bin/echo ${QUERY_STRING} | /usr/bin/tr ";&" "\012") ; do
  30. #
  31. # Clear our local variables
  32. #
  33. unset name
  34. unset value
  35. unset tmpvalue
  36. #
  37. # get the name of the key, and decode it
  38. #
  39. name=${Q%%=*}
  40. name=$(/bin/echo ${name} | \
  41. /bin/sed -e 's/%\(\)/\\\x/g' | \
  42. /usr/bin/tr "+" " ")
  43. name=$(/bin/echo ${name} | \
  44. /usr/bin/tr -d ".-")
  45. name=$(/usr/bin/printf ${name})
  46. #
  47. # get the value and decode it. This is tricky... printf chokes on
  48. # hex values in the form \xNN when there is another hex-ish value
  49. # (i.e., a-fA-F) immediately after the first two. My (horrible)
  50. # solution is to put a space aftet the \xNN, give the value to
  51. # printf, and then remove it.
  52. #
  53. tmpvalue=${Q#*=}
  54. tmpvalue=$(/bin/echo ${tmpvalue} | \
  55. /bin/sed -e 's/%\(..\)/\\\x\1 /g')
  56. #echo "Intermediate \$value: ${tmpvalue}" 1>&2
  57. #
  58. # Iterate through tmpvalue and printf each string, and append it to
  59. # value
  60. #
  61. for i in ${tmpvalue}; do
  62. g=$(/usr/bin/printf ${i})
  63. value="${value}${g}"
  64. done
  65. #value=$(echo ${value})
  66. eval "export FORM_${name}='${value}'"
  67. done
  68. else # keywords: foo.cgi?a+b+c
  69. Q=$(echo ${QUERY_STRING} | tr '+' ' ')
  70. eval "export KEYWORDS='${Q}'"
  71. fi
  72. fi
  73. #
  74. # this section works identically to the query string parsing code,
  75. # with the (obvious) exception that variables are stuck into the
  76. # environment with the prefix COOKIE_ rather than FORM_. This is to
  77. # help distinguish them from the other variables that get set
  78. # automatically.
  79. #
  80. if [ -n "${HTTP_COOKIE}" ]; then
  81. for Q in ${HTTP_COOKIE}; do
  82. #
  83. # Clear our local variables
  84. #
  85. name=
  86. value=
  87. tmpvalue=
  88. #
  89. # Strip trailing ; off the value
  90. #
  91. Q=${Q%;}
  92. #
  93. # get the name of the key, and decode it
  94. #
  95. name=${Q%%=*}
  96. name=$(/bin/echo ${name} | \
  97. /bin/sed -e 's/%\(\)/\\\x/g' | \
  98. /usr/bin/tr "+" " ")
  99. name=$(/bin/echo ${name} | \
  100. /usr/bin/tr -d ".-")
  101. name=$(/usr/bin/printf ${name})
  102. # Decode the cookie value. See the parameter section above for
  103. # an explanation of what this is doing.
  104. tmpvalue=${Q#*=}
  105. tmpvalue=$(/bin/echo ${tmpvalue} | \
  106. /bin/sed -e 's/%\(..\)/\\\x\1 /g')
  107. #echo "Intermediate \$value: ${tmpvalue}" 1>&2
  108. #
  109. # Iterate through tmpvalue and printf each string, and append it to
  110. # value
  111. #
  112. for i in ${tmpvalue}; do
  113. g=$(/usr/bin/printf ${i})
  114. value="${value}${g}"
  115. done
  116. #value=$(echo ${value})
  117. #
  118. # Export COOKIE_${name} into the environment
  119. #
  120. #echo "exporting COOKIE_${name}=${value}" 1>&2
  121. eval "export COOKIE_${name}='${value}'"
  122. done
  123. fi
  124. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  125. # functions and all that groovy stuff
  126. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  127. #
  128. # Shameless plug, advertises verion.
  129. function version {
  130. echo "bashlib, version ${VERSION}"
  131. }
  132. function version_html {
  133. echo -n "<a href=\"http://sevenroot.org/software/bashlib/\">bashlib</a>,"
  134. echo "version ${VERSION}"
  135. }
  136. #
  137. # Parameter function.
  138. # * When called with no arguments, returns a list of parameters that
  139. # were passed in.
  140. # * When called with one argument, returns the value of that parameter
  141. # (if any)
  142. # * When called with more than one argument, assumes that the first is a
  143. # paramter name and the rest are values to be assigned to a paramter of
  144. # that name.
  145. #
  146. function param {
  147. local name
  148. local value
  149. if [ $# -eq 1 ]; then
  150. name=$1
  151. name=$(echo ${name} | /bin/sed -e 's/FORM_//')
  152. value=$(/usr/bin/env | /bin/grep "^FORM_${name}" | /bin/sed -e 's/FORM_//' | /usr/bin/cut -d= -f2-)
  153. elif [ $# -gt 1 ]; then
  154. name=$1
  155. shift
  156. eval "export 'FORM_${name}=$*'"
  157. else
  158. value=$(/usr/bin/env | /bin/grep '^FORM_' | /bin/sed -e 's/FORM_//' | /usr/bin/cut -d= -f1)
  159. fi
  160. echo ${value}
  161. unset name
  162. unset value
  163. }
  164. # cookie function. Same explanation as param
  165. function cookie {
  166. local name
  167. local value
  168. if [ $# -eq 1 ]; then
  169. name=$1
  170. name=$(echo ${name} | /bin/sed -e 's/COOKIE_//')
  171. value=$(/usr/bin/env | /bin/grep "^COOKIE_${name}" | /bin/sed -e 's/COOKIE_//' | /usr/bin/cut -d= -f2-)
  172. elif [ $# -gt 1 ]; then
  173. name=$1
  174. shift
  175. eval "export 'COOKIE_${name}=$*'"
  176. else
  177. value=$(/usr/bin/env | /bin/grep '^COOKIE_' | /bin/sed -e 's/COOKIE_//' | /usr/bin/cut -d= -f1)
  178. fi
  179. echo ${value}
  180. unset name
  181. unset value
  182. }
  183. # keywords returns a list of keywords. This is only set when the script is
  184. # called with an ISINDEX form (these are pretty rare nowadays).
  185. function keywords {
  186. echo ${KEYWORDS}
  187. }
  188. function set_cookie {
  189. local name=$1
  190. shift
  191. local value=$*
  192. bashlib_cookies="${bashlib_cookies}; ${name}=${value}"
  193. bashlib_cookies=${bashlib_cookies#;}
  194. cookie $name $value
  195. }
  196. #
  197. # send_redirect takes a URI and redirects the browser to that uri, exiting
  198. # the script along the way.
  199. #
  200. function send_redirect {
  201. local uri
  202. if [ $# -eq 1 ]; then
  203. uri=$1
  204. else
  205. uri="http://${SERVER_NAME}/${SCRIPT_NAME}"
  206. fi
  207. echo "Location: ${uri}"
  208. echo ""
  209. }