home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / web_dev / files / perlbuilderev.exe / %MAINDIR% / tutorial / example.cgi < prev    next >
Encoding:
Text File  |  2001-06-19  |  8.1 KB  |  254 lines

  1. #!/usr/local/bin/perl
  2. $mail_prog = '/usr/lib/sendmail' ;
  3. # This script was generated automatically by Perl Builder(tm): http://www.solutionsoft.com
  4.  
  5. # ***ENDAUTOGEN:HEADER*** Do NOT modify this line!!  You may enter custom code after this line.
  6.  
  7.  
  8.  
  9. # ***AUTOGEN:INPUT*** Do NOT modify this line!! Do NOT enter custom code in this section.
  10.  
  11. &GetFormInput;
  12.  
  13. # The intermediate variables below make your script more readable
  14. # but somewhat less efficient since they are not really necessary.
  15. # If you do not want to use these variables, clear the
  16. # Intermediate Variables checkbox in the Tools | Options dialog box, CGI Wizard tab.
  17.  
  18. $first = $field{'first'} ;     
  19. $last = $field{'last'} ;     
  20. $company = $field{'company'} ;     
  21. $email = $field{'email'} ;     
  22. $whereheard = $field{'whereheard'} ;     
  23. $athome = $field{'athome'} ;     
  24. $atwork = $field{'atwork'} ;     
  25. $submit = $field{'submit'} ;     
  26.  
  27. $message = "" ;
  28. $found_err = "" ;
  29.  
  30. # ***ENDAUTOGEN:INPUT*** Do NOT modify this line!!  You may enter custom code after this line.
  31.  
  32.  
  33.  
  34. # ***AUTOGEN:VALIDATE*** Do NOT modify this line!! Do NOT enter custom code in this section.
  35.  
  36. $errmsg = "<p>Field 'first' must be filled in.</p>\n" ;
  37.  
  38. if ($first eq "") {
  39.     $message = $message.$errmsg ;
  40.     $found_err = 1 ; }
  41.  
  42.  
  43. $errmsg = "<p>Field 'last' must be filled in.</p>\n" ;
  44.  
  45. if ($last eq "") {
  46.     $message = $message.$errmsg ;
  47.     $found_err = 1 ; }
  48.  
  49.  
  50. $errmsg = "<p>Please enter a valid email address</p>\n" ;
  51.  
  52. if ($email !~ /.+\@.+\..+/) {
  53.     $message = $message.$errmsg ;
  54.     $found_err = 1 ; }
  55.  
  56. if ($found_err) {
  57.     &PrintError; }
  58.  
  59.  
  60. # ***ENDAUTOGEN:VALIDATE*** Do NOT modify this line!!  You may enter custom code after this line.
  61.  
  62.  
  63. # ***AUTOGEN:LOGFILE*** Do NOT modify this line!! Do NOT enter custom code in this section.
  64.  
  65. # This will NOT work on your server unless you create the text file and set the permissions: chmod 660 log.txt
  66.  
  67.  
  68. open (LOGFILE, ">> log.txt") ;
  69.  
  70. #The commented flock() and seek() lines below will lock the log file to prevent collisions from
  71. #simultaneous users. However, flock() is probably not implemented in Perl for Windows and will
  72. #give you a fatal error. Uncomment these lines only if/when you move your script to a Unix server.
  73.  
  74. #flock(LOGFILE, 2) ;     #LOCK - uncomment for locking on a Unix server.
  75. #seek(LOGFILE, 0, 2) ;   #LOCK - uncomment for locking on a Unix server.
  76.  
  77. print LOGFILE "$first\t$last\t$company\t$email\t$whereheard\t$athome\t$atwork\n" ;
  78.  
  79. #flock(LOGFILE, 8) ;     #UNLOCK - uncomment for locking on a Unix server.
  80.  
  81. close (LOGFILE) ;
  82.  
  83.  
  84. # ***ENDAUTOGEN:LOGFILE*** Do NOT modify this line!!  You may enter custom code after this line.
  85.  
  86.  
  87.  
  88. # ***AUTOGEN:EMAIL*** Do NOT modify this line!! Do NOT enter custom code in this section.
  89.  
  90. $recip = "webmaster\@yourownsite.com" ;
  91.  
  92. open (MAIL, "|$mail_prog -t");
  93. print MAIL "To: $recip\n";
  94. print MAIL "Reply-to: $email\n";
  95. print MAIL "From: $email\n";
  96. print MAIL "Subject: DownLoad Alert!\n";
  97. print MAIL "\n\n";
  98. print MAIL "The following user has downloaded Power Widget:\n" ;
  99. print MAIL "\n" ;
  100. print MAIL "Name: ".$first." ".$last."\n" ;
  101. print MAIL "\n" ;
  102. print MAIL "Company: ".$company."\n" ;
  103. print MAIL "\n" ;
  104. print MAIL "Where Heard about Power Widget: ".$whereheard."\n" ;
  105. print MAIL "\n" ;
  106. print MAIL "Use at home? ".$athome."\n" ;
  107. print MAIL "Use at work? ".$atwork."\n" ;
  108. print MAIL "\n\n";
  109. close (MAIL);
  110.  
  111.  
  112. $recip = $email ;
  113.  
  114. open (MAIL, "|$mail_prog -t");
  115. print MAIL "To: $recip\n";
  116. print MAIL "Reply-to: sales\@yourownsite.com\n";
  117. print MAIL "From: sales\@yourownsite.com\n";
  118. print MAIL "Subject: Power Widget\n";
  119. print MAIL "\n\n";
  120. print MAIL "Hi ".$first.",\n" ;
  121. print MAIL "\n" ;
  122. print MAIL "Thank you for downloading PowerWidget!\n" ;
  123. print MAIL "\n" ;
  124. print MAIL "Please contact us if you have any questions.\n" ;
  125. print MAIL "\n" ;
  126. print MAIL "Sincerely,\n" ;
  127. print MAIL "\n" ;
  128. print MAIL "Power Widget Sales Dept.\n" ;
  129. print MAIL "\n\n";
  130. close (MAIL);
  131.  
  132. # ***ENDAUTOGEN:EMAIL*** Do NOT modify this line!!  You may enter custom code after this line.
  133.  
  134.  
  135.  
  136. # ***AUTOGEN:HTML*** Do NOT modify this line!! Do NOT enter custom code in this section.
  137. print "Content-type: text/html\n\n";
  138. print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">'."\n" ;
  139. print "<html>\n" ;
  140. print "\n" ;
  141. print "<head>\n" ;
  142. print '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'."\n" ;
  143. print '<meta name="Author" content>'."\n" ;
  144. print '<meta name="GENERATOR" content="Microsoft FrontPage 3.0">'."\n" ;
  145. print "<title>Perl Builder Output Page Example</title>\n" ;
  146. print "</head>\n" ;
  147. print "\n" ;
  148. print '<body bgcolor="#8080C0" text="#000000" link="#800000" vlink="#000080" alink="#800000">'."\n" ;
  149. print "\n" ;
  150. print '<p align="center"><font color="#FFFFFF" face="Arial"><big><strong>Thank You, '.$first.'!</strong></big></font></p>'."\n" ;
  151. print "\n" ;
  152. print '<p align="left"> </p>'."\n" ;
  153. print "\n" ;
  154. print '<p align="center"><font color="#FFFFFF" face="Arial"><big><strong>Please click the link'."\n" ;
  155. print "below to download your software:</strong></big></font></p>\n" ;
  156. print "\n" ;
  157. print '<p align="center"><font face="Arial" color="#0000FF"><u><big><strong>DeadLink</strong></big></u></font></p>'."\n" ;
  158. print "\n" ;
  159. print "<hr>\n" ;
  160. print "\n" ;
  161. print '<p align="left"><font face="Arial" color="#000080"><big>You entered the following:</big></font></p>'."\n" ;
  162. print "\n" ;
  163. print '<p align="left"><font face="Arial" color="#000080"><big>Name: '.$first.' '.$last.'</big></font></p>'."\n" ;
  164. print "\n" ;
  165. print '<p align="left"><font face="Arial" color="#000080"><big>Company: '.$company.'</big></font></p>'."\n" ;
  166. print "\n" ;
  167. print '<p align="left"><font face="Arial" color="#000080"><big>Email: '.$email.' </big></font></p>'."\n" ;
  168. print "</body>\n" ;
  169. print "</html>\n" ;
  170.  
  171. # ***ENDAUTOGEN:HTML*** Do NOT modify this line!!  You may enter custom code after this line.
  172.  
  173.  
  174.  
  175. # ***AUTOGEN:ERRPRINT*** Do NOT modify this line!! Do NOT enter custom code in this section.
  176.  
  177. sub PrintError { 
  178. print "Content-type: text/html\n\n";
  179. print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">'."\n" ;
  180. print "<html>\n" ;
  181. print "\n" ;
  182. print "<head>\n" ;
  183. print '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'."\n" ;
  184. print '<meta name="Author" content>'."\n" ;
  185. print '<meta name="GENERATOR" content="Microsoft FrontPage 3.0">'."\n" ;
  186. print "<title>Perl Builder Error Page Example</title>\n" ;
  187. print "</head>\n" ;
  188. print "\n" ;
  189. print '<body bgcolor="#8080C0" text="#000000" link="#800000" vlink="#000080" alink="#800000">'."\n" ;
  190. print "\n" ;
  191. print '<p align="center"><font color="#FFFFFF" face="Arial"><big><strong>Processing Error</strong></big></font></p>'."\n" ;
  192. print "\n" ;
  193. print '<p align="left"><font face="Arial" color="#000080"><strong>You forgot to enter one or more'."\n" ;
  194. print "values:</strong></font></p>\n" ;
  195. print "\n" ;
  196. print '<p align="left"><font color="#FFFFFF" face="Arial"><strong>'.$message.'</strong></font></p>'."\n" ;
  197. print "\n" ;
  198. print '<p align="left"><font face="Arial" color="#000080"><strong>Please click your browser\'s'."\n" ;
  199. print "Back button and try again.</strong></font></p>\n" ;
  200. print "</body>\n" ;
  201. print "</html>\n" ;
  202.  
  203. exit 0 ;
  204. return 1 ; 
  205. }
  206.  
  207. # ***ENDAUTOGEN:ERRPRINT*** Do NOT modify this line!!  You may enter custom code after this line.
  208.  
  209.  
  210.  
  211. # ***AUTOGEN:PARSE*** Do NOT modify this line!! Do NOT enter custom code in this section.
  212. sub GetFormInput {
  213.  
  214.     (*fval) = @_ if @_ ;
  215.  
  216.     local ($buf);
  217.     if ($ENV{'REQUEST_METHOD'} eq 'POST') {
  218.         read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
  219.     }
  220.     else {
  221.         $buf=$ENV{'QUERY_STRING'};
  222.     }
  223.     if ($buf eq "") {
  224.             return 0 ;
  225.         }
  226.     else {
  227.          @fval=split(/&/,$buf);
  228.         foreach $i (0 .. $#fval){
  229.             ($name,$val)=split (/=/,$fval[$i],2);
  230.             $val=~tr/+/ /;
  231.             $val=~ s/%(..)/pack("c",hex($1))/ge;
  232.             $name=~tr/+/ /;
  233.             $name=~ s/%(..)/pack("c",hex($1))/ge;
  234.  
  235.             if (!defined($field{$name})) {
  236.                 $field{$name}=$val;
  237.             }
  238.             else {
  239.                 $field{$name} .= ",$val";
  240.                 
  241.                 #if you want multi-selects to goto into an array change to:
  242.                 #$field{$name} .= "\0$val";
  243.             }
  244.  
  245.  
  246.            }
  247.         }
  248. return 1;
  249. }
  250.  
  251.  
  252. # ***ENDAUTOGEN:PARSE*** Do NOT modify this line!!  You may enter custom code after this line.
  253.  
  254.