Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: CGI Form / Email

From: tomc:darkwing:uoregon at: 31-Mar-2002 12:56

last year I was asked to modify an existing cgiemail to work a bit differently... Instead I wrote a a workalike replacement (it was quicker) -- I still don't think anyone knows. I here is a cleaned up version of what I am using I hope I did not introduce any tyops. #! /private/bin/rebol -csq rebol[ Author: "Tom Conlin" Date: 2001-Jul-19 File: %cgiemail.cgi purpose: { drop-in replacement for MIT's standard cgiemail with a couple of tweaks. First does not use sendmail which was improperly translating email addressed from a vitual hostname to the machine name when it was not asked to. (caused problems in out particular case) Second I am also saving a log of each transaction to a file. (because I was told to) the basic idea is that with this program you can validate and email arbitary forms by writing your forms using certain conventions and having a text template of the email you would like to be sent to various people when the form is submitted. their doc: http://web.mit.edu/wwwdev/cgiemail/ their source ~100k <snicker>: http://www.mit.edu/afs/net.mit.edu/reference/source/web-cgi/ } ] ;;; see: hhtp://www.rebol.com for free "rebol/core" interperter print "Content-Type: text/html^/" ;;; Edit these to suit your enviroment accept-domain: <domain-your-form-is-on> ;otherwise you may end up a spammer maintainer-email: <your-email-adress> set-net [maintainer-email <mailhost.your.domain> none none none] ; only used if you are writing out a log lock-file: %/path-to-your-lockfile output-file: %/path-to-your-logfile ;;; TO-DO read a local config file that contains domains to accept ;;; template files from ;;; url-decode and parse the string into a block of name-value pairs cgi: copy "" either system/options/cgi/request-method = "POST"[ ;;; read the block of name = value pairs ;;; see how long the string we need to read is len: load system/options/cgi/content-length ;;; make an empty string that is long enough ;;; read the string from stdin buffer: make string! (:len + 8) while [not zero? read-io system/ports/input buffer :len] [append cgi buffer clear buffer] ] [ either system/options/cgi/request-method = "GET" [cgi: copy system/options/cgi/query-string] [print "implement command line testing already"] ] cgi: decode-cgi cgi keyval: copy [] foreach x cgi [append/only keyval to-string :x ] ;;; check-that the template is from somwhere we have agreed to read from ;;; this really needs to be done some better way if not find (select keyval "template") accept-domain[ print "we are coming to get you now" q ] template: read to-url (select keyval "template") placeholders: copy [] ;alfa: charset {#"A"=#"Z" #"a"-#"z"} mark: copy "" parse template [ any [ (mark: copy "") to "[" copy mark thru "]" (append placeholders mark) ] to end ] sort/compare placeholders func[a b][(length? a ) > (length? b )] ;;; replace the place holders in the template with the values from the cgi foreach [k v] keyval [ k: rejoin[ "[" :k "]" ] if all[ (equal? v "") (equal? copy/part k 10 [required_ ) ][ v: rejoin[ {<font color="red">**} :k {</font>} ] ] replace/all template k v ] ;;; balk if a required field is missing if find template {<font color="red">**[required_}[ template: copy next find template "^/^/" print {<html> <head> <title>Failed</title> </head> <body> <h1> Required fields missing</h1> <b>Please go back and fill in all required fields</b><p> } print replace/all template "^/" "<br>^/" print "</body></html>" quit ] ;;; remove leftover place holders ;;; such as those attached to unchecked checkboxes foreach ph placeholders[ replace/all template ph ""] ;;; strip the header info out of the template ;;; build a block of addressed to send to addy: copy/part next find template ":" find template "^/" template: copy next find template "^/" frm: load copy/part next find template ":" find template "^/" template: copy next find template "^/" sbj: copy/part next find template ":" find template "^/" template: copy next find template "^/" ;;; clean off the top of the template while [(pick template 1) = "^/" ][remove template] foreach ad parse addy "," [ ;;; build a mail header header: make system/standard/email [ To: ad From: frm Subject: sbj ] send/header to-email ad template header ] ;;; append a copy of the email to a logfile ;;; make sure only one cgi writes to the file at a time while [not error? try[read lock-file]][wait 2] write lock-file "" write/append output-file rejoin ["# "now/date "--" now/time "^/"] write/append output-file template delete lock-file ;;; output the happy ending version print read to-url (select keyval "success")