| <!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
| <HTML> |
| <HEAD> |
| <TITLE>emailform</TITLE> |
| </HEAD> |
| <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#1F00FF" ALINK="#FF0000" VLINK="#9900DD"> |
| <A NAME="top"> |
| <A NAME="file1"> |
| <H1>emailform.ws3</H1> |
| |
| <PRE> |
| <I><FONT COLOR="#B22222"># "order" form example |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># The "customer" fills out a form, say an order form. |
| </FONT></I><I><FONT COLOR="#B22222"># The form data is validated and a confirmation page |
| </FONT></I><I><FONT COLOR="#B22222"># displayed. ALso, a confirmation e-mail is sent to the |
| </FONT></I><I><FONT COLOR="#B22222"># customer. |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># In addition, logging is used. |
| </FONT></I> |
| |
| <I><FONT COLOR="#B22222"># turn on logging |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># web::logfilter determines which log messages will be sent to the log |
| </FONT></I><I><FONT COLOR="#B22222"># destination. The rule here is: let all log messages pass which have |
| </FONT></I><I><FONT COLOR="#B22222"># facilities that match "*" and which have a level up to and including |
| </FONT></I><I><FONT COLOR="#B22222"># level "debug" |
| </FONT></I>web::logfilter add *.-debug |
| |
| <I><FONT COLOR="#B22222"># define where to send log messages to. Here we use a file |
| </FONT></I><I><FONT COLOR="#B22222"># and again define a log destination based filter, which is again |
| </FONT></I><I><FONT COLOR="#B22222"># "*.-debug" as above |
| </FONT></I>web::logdest add *.-debug <B><FONT COLOR="#A020F0">file</FONT></B> /tmp/websh/emailform.log |
| |
| <I><FONT COLOR="#B22222"># because log directory might not exist in this environment |
| </FONT></I><I><FONT COLOR="#B22222"># (note: in a production environment you make sure that this exists |
| </FONT></I><I><FONT COLOR="#B22222"># at install time. You don't want to create that directory with every request) |
| </FONT></I><B><FONT COLOR="#A020F0">catch</FONT></B> {<B><FONT COLOR="#A020F0">file</FONT></B> mkdir /tmp/websh} |
| |
| <I><FONT COLOR="#B22222"># utility command to handle an HTML page |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">page</FONT></B> {<FONT COLOR="#B8860B">title code</FONT>} { |
| web::putx {<html><head><title>{web::put $<FONT COLOR="#B8860B">title</FONT>}</title></head> |
| <body bgcolor=<B><FONT COLOR="#BC8F8F">"#ffffff"</FONT></B> <B><FONT COLOR="#A020F0">text</FONT></B>=<B><FONT COLOR="#BC8F8F">"#000000"</FONT></B>> |
| <h1>{web::put $<FONT COLOR="#B8860B">title</FONT>}</h1> |
| } |
| <B><FONT COLOR="#A020F0">uplevel</FONT></B> $<FONT COLOR="#B8860B">code</FONT> |
| web::put <B><FONT COLOR="#BC8F8F">"</body></html>\n"</FONT></B> |
| } |
| |
| <I><FONT COLOR="#B22222"># utility command to handle an html form |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">form</FONT></B> {<FONT COLOR="#B8860B">page code</FONT>} { |
| web::put <B><FONT COLOR="#BC8F8F">"<form method=\"post\" action=\"[web::cmdurl $page]\">"</FONT></B> |
| <B><FONT COLOR="#A020F0">uplevel</FONT></B> $<FONT COLOR="#B8860B">code</FONT> |
| web::put <B><FONT COLOR="#BC8F8F">"</form>"</FONT></B> |
| } |
| |
| <I><FONT COLOR="#B22222"># define the form where address is entered |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">showForm</FONT></B> {<FONT COLOR="#B8860B">error</FONT>} { |
| |
| <I><FONT COLOR="#B22222"># generate a page with Title "Form" |
| </FONT></I> page <B><FONT COLOR="#BC8F8F">"Order Form"</FONT></B> { |
| <I><FONT COLOR="#B22222"># generate a form with action "submit" |
| </FONT></I> form <B><FONT COLOR="#BC8F8F">"submit"</FONT></B> { |
| web::putx { |
| <dl> |
| <dt><b>Name:</b> { |
| <I><FONT COLOR="#B22222"># if "error" flag is set, show the red error message asking for input |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> {$<FONT COLOR="#B8860B">error</FONT> == 1} { |
| web::put <B><FONT COLOR="#BC8F8F">"<font color=\"\#990000\">Please enter your name</font>\n"</FONT></B> |
| } |
| } |
| <dd><input type=<B><FONT COLOR="#BC8F8F">"text"</FONT></B> name=<B><FONT COLOR="#BC8F8F">"name"</FONT></B> value=<B><FONT COLOR="#BC8F8F">"{web::put [web::htmlify [web::formvar name]]}"</FONT></B> size=<B><FONT COLOR="#BC8F8F">"30"</FONT></B>><p> |
| <dt><b>Address:</b> |
| <dd><textarea name=<B><FONT COLOR="#BC8F8F">"addr"</FONT></B> rows=<B><FONT COLOR="#BC8F8F">"4"</FONT></B> cols=<B><FONT COLOR="#BC8F8F">"30"</FONT></B> wrap=<B><FONT COLOR="#BC8F8F">"auto"</FONT></B>>{web::put [web::htmlify [web::formvar addr]]}</textarea><p> |
| <dt><b>E-Mail:</b> { |
| <I><FONT COLOR="#B22222"># if "error" flag is set, ask for valid e-mail address |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> {$<FONT COLOR="#B8860B">error</FONT> == 2} { |
| web::put <B><FONT COLOR="#BC8F8F">"<font color=\"\#990000\">Please enter a valid email addres</font>\n"</FONT></B> |
| } |
| } |
| <dd><input type=<B><FONT COLOR="#BC8F8F">"text"</FONT></B> name=<B><FONT COLOR="#BC8F8F">"email"</FONT></B> value=<B><FONT COLOR="#BC8F8F">"{web::put [web::htmlify [web::formvar email]]}"</FONT></B> size=<B><FONT COLOR="#BC8F8F">"30"</FONT></B>><p> |
| </dl> |
| <input type=<B><FONT COLOR="#BC8F8F">"submit"</FONT></B> name=<B><FONT COLOR="#BC8F8F">"ok"</FONT></B> value=<B><FONT COLOR="#BC8F8F">"Send"</FONT></B>> |
| } |
| } |
| } |
| } |
| |
| <I><FONT COLOR="#B22222"># validator: |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># make sure we have a name of non-zero length. |
| </FONT></I><I><FONT COLOR="#B22222"># Also, make sure the e-mail address is not completely wrong. |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">checkFormData</FONT></B> {<FONT COLOR="#B8860B"></FONT>} { |
| |
| <I><FONT COLOR="#B22222"># check if a value is in the name field |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> { [<B><FONT COLOR="#A020F0">string</FONT></B> length [web::formvar name]] < 1} { |
| <I><FONT COLOR="#B22222"># return error code |
| </FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B> 1 |
| } |
| |
| <I><FONT COLOR="#B22222"># log (facility: emailform, level: debug) |
| </FONT></I> web::log emailform.debug {name [web::formvar name] is valid} |
| |
| <I><FONT COLOR="#B22222"># check email |
| </FONT></I> <B><FONT COLOR="#A020F0">set</FONT></B> email [web::formvar email] |
| |
| <I><FONT COLOR="#B22222"># make sure we have alpha-numeric stuff separated by "@" |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> {![<B><FONT COLOR="#A020F0">regexp</FONT></B> -nocase {^([a-z._0-9-]+)@([a-z._0-9-]+)\.+([a-z]+)$} \ |
| $<FONT COLOR="#B8860B">email</FONT> email name domain]} { |
| <I><FONT COLOR="#B22222"># return error code |
| </FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B> 2 |
| } |
| |
| <I><FONT COLOR="#B22222"># check length of domain |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> {[<B><FONT COLOR="#A020F0">string</FONT></B> length $<FONT COLOR="#B8860B">domain</FONT>] < 3} { |
| <B><FONT COLOR="#A020F0">return</FONT></B> 2 |
| } |
| |
| web::log emailform.debug {email [web::formvar email] is valid} |
| |
| <I><FONT COLOR="#B22222"># looks good: no error |
| </FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B> 0 |
| } |
| |
| <I><FONT COLOR="#B22222"># sendEmail |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># create the e-mail message and send it to the given e-mail address |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">sendEmail</FONT></B> {<FONT COLOR="#B8860B"></FONT>} { |
| |
| <I><FONT COLOR="#B22222"># disabled for security reasons, and because there is no |
| </FONT></I> <I><FONT COLOR="#B22222"># sendmail on www.apache.org ;-) |
| </FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B> 1 |
| |
| <B><FONT COLOR="#A020F0">set</FONT></B> emailtxt { |
| |
| Thank you <B><FONT COLOR="#A020F0">for</FONT></B> your submission. |
| |
| We have recieved the following information:} |
| |
| <B><FONT COLOR="#A020F0">append</FONT></B> emailtxt <B><FONT COLOR="#BC8F8F">"\nName:\n[web::formvar name]\n"</FONT></B> |
| <B><FONT COLOR="#A020F0">append</FONT></B> emailtxt <B><FONT COLOR="#BC8F8F">"Address:\n"</FONT></B> |
| <B><FONT COLOR="#A020F0">append</FONT></B> emailtxt [web::formvar addr] |
| <B><FONT COLOR="#A020F0">append</FONT></B> emailtxt { |
| |
| Find more information about Webshell at http://tcl.apache.org/websh/ |
| |
| The team. |
| } |
| <I><FONT COLOR="#B22222"># log message |
| </FONT></I> web::log emailform.debug <B><FONT COLOR="#BC8F8F">"e-mail: $emailtxt"</FONT></B> |
| |
| <B><FONT COLOR="#A020F0">if</FONT></B> { [<B><FONT COLOR="#A020F0">catch</FONT></B> { |
| <I><FONT COLOR="#B22222"># Open pipe for e-mail |
| </FONT></I> <B><FONT COLOR="#A020F0">set</FONT></B> fh [<B><FONT COLOR="#A020F0">open</FONT></B> <B><FONT COLOR="#BC8F8F">"| /usr/lib/sendmail [web::formvar email]"</FONT></B> w] |
| <B><FONT COLOR="#A020F0">puts</FONT></B> $<FONT COLOR="#B8860B">fh</FONT> <B><FONT COLOR="#BC8F8F">"From: info@tcl.apache.org"</FONT></B> |
| <B><FONT COLOR="#A020F0">puts</FONT></B> $<FONT COLOR="#B8860B">fh</FONT> <B><FONT COLOR="#BC8F8F">"Subject: websh3 sample application - sample confirmation"</FONT></B> |
| <B><FONT COLOR="#A020F0">puts</FONT></B> $<FONT COLOR="#B8860B">fh</FONT> <B><FONT COLOR="#BC8F8F">""</FONT></B> |
| <B><FONT COLOR="#A020F0">puts</FONT></B> $<FONT COLOR="#B8860B">fh</FONT> $<FONT COLOR="#B8860B">emailtxt</FONT> |
| close $<FONT COLOR="#B8860B">fh</FONT> |
| } cmsg ] } { |
| <B><FONT COLOR="#A020F0">return</FONT></B> 0 |
| } |
| <B><FONT COLOR="#A020F0">return</FONT></B> 1 |
| } |
| |
| |
| <B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">showErrorPage</FONT></B> {<FONT COLOR="#B8860B"></FONT>} { |
| |
| page <B><FONT COLOR="#BC8F8F">"Error"</FONT></B> { |
| |
| web::putx { |
| An <B><FONT COLOR="#A020F0">error</FONT></B> occurred <B><FONT COLOR="#A020F0">while</FONT></B> processing your request. |
| Please {web::put <B><FONT COLOR="#BC8F8F">"<a href=\"[web::cmdurl default]\">try</a>"</FONT></B>} again |
| <br><br> |
| If the problem persists, please contact the |
| {web::put <B><FONT COLOR="#BC8F8F">"<a href=\"mailto:webmaster@websh.com\">webmaster</a>"</FONT></B>}. |
| } |
| } |
| } |
| |
| <B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">showConfirmationPage</FONT></B> {<FONT COLOR="#B8860B"></FONT>} { |
| |
| page <B><FONT COLOR="#BC8F8F">"Confirmation"</FONT></B> { |
| |
| web::putx { |
| <h3>Thank you <B><FONT COLOR="#A020F0">for</FONT></B> your order</h3> |
| We have recieved the following information: |
| <dl> |
| <dt><b>Name:</b> |
| <dd>{web::put [web::htmlify [web::formvar name]]}<p> |
| <dt><b>Address:</b> |
| <dd>{ |
| <I><FONT COLOR="#B22222"># take care of linebreaks in address |
| </FONT></I> <B><FONT COLOR="#A020F0">regsub</FONT></B> -all <B><FONT COLOR="#BC8F8F">"\r\n"</FONT></B> [web::htmlify [web::formvar addr]] <B><FONT COLOR="#BC8F8F">"<br>"</FONT></B> addr |
| web::put $<FONT COLOR="#B8860B">addr</FONT> |
| }<p> |
| <dt><b>E-Mail:</b> |
| <dd>{web::put [web::htmlify [web::formvar email]]}<p> |
| </dl> |
| You should recieve a confirmation by e-mail shortly. |
| |
| <p><b>Note: to prevent misue, sending the actual email is disabled, so please don't wait <B><FONT COLOR="#A020F0">for</FONT></B> the mail ;-)</b></p> |
| |
| {web::put <B><FONT COLOR="#BC8F8F">"<a href=\"[web::cmdurl default]\">Order more</a>"</FONT></B>} |
| cool stuff. |
| } |
| } |
| } |
| |
| <I><FONT COLOR="#B22222"># register the "default" command |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># This command will be used whenever no specific command has been specified. |
| </FONT></I><I><FONT COLOR="#B22222"># We use it to show an empty form for address submission. |
| </FONT></I>web::command default { |
| showForm 0 |
| } |
| |
| |
| <I><FONT COLOR="#B22222"># register command "submit" |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># This is the "action" of our form. The form data is validated. If |
| </FONT></I><I><FONT COLOR="#B22222"># the formdata is incomplete or invalid, the form is re-displayed with |
| </FONT></I><I><FONT COLOR="#B22222"># an error info, where the original input is displayed as well. |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># If the data is valid, the confirmation page is shown and |
| </FONT></I><I><FONT COLOR="#B22222"># an e-mail is sent to the specified address. |
| </FONT></I> |
| web::command submit { |
| |
| <B><FONT COLOR="#A020F0">if</FONT></B> { [<B><FONT COLOR="#A020F0">set</FONT></B> res [checkFormData]] == 0 } { |
| <B><FONT COLOR="#A020F0">if</FONT></B> {[sendEmail]} { |
| showConfirmationPage |
| } else { |
| showErrorPage |
| } |
| } else { |
| showForm $<FONT COLOR="#B8860B">res</FONT> |
| } |
| } |
| |
| |
| web::dispatch |
| </PRE> |
| <HR> |
| <ADDRESS>Generated by <A HREF="http://www.iki.fi/~mtr/genscript/">GNU enscript 1.6.3</A>.</ADDRESS> |
| </BODY> |
| </HTML> |