Guest User

Jimmy Johns Fax ordering CGI example, jj.c

a guest
Oct 7th, 2014
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.16 KB | None | 0 0
  1. /*
  2.  * Submarine ordering form through FAX gateway
  3.  *
  4.  * Rob McCool
  5.  *
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #ifndef NO_STDLIB_H
  12. #include <stdlib.h>
  13. #else
  14. char *getenv();
  15. #endif
  16.  
  17. #if 1
  18. #define JJ_FAX "[email protected]"
  19. #else
  20. #define JJ_FAX "[email protected]"
  21. #endif
  22.  
  23. #define PASSWORD "HTTPdRocKs"
  24. #define LF 10
  25.  
  26. void getword(char *word, char *line, char stop);
  27. char x2c(char *what);
  28. void unescape_url(char *url);
  29. void plustospace(char *str);
  30.  
  31.  
  32. char *sublist[] = {
  33.     "The Pepe Gourmet Sub",
  34.     "Big John Gourmet Sub",
  35.     "Sorry Charlie Gourmet Sub",
  36.     "Turkey Tom Gourmet Sub",
  37.     "Vito Gourmet Sub",
  38.     "Vegetarian Gourmet Sub",
  39.     "Gourmet Smoked Ham Club",
  40.     "Billy Club",
  41.     "Italian Night Club",
  42.     "Hunter's Club",
  43.     "Country Club",
  44.     "The Beach Club"
  45.     };
  46.  
  47. char *slimlist[] = {
  48.     "Ham and Cheese",
  49.     "Rare Roast Beef",
  50.     "California Tuna",
  51.     "Sliced Turkey",
  52.     "Salami and Capacola",
  53.     "Double Provolone"
  54.     };
  55.  
  56. char *sidelist[] = {
  57.     "Lay's Potato Chips",
  58.     "Jumbo Kosher Dill"
  59.     };
  60.  
  61. char *poplist[] = {
  62.     "Pepsi",
  63.     "Mountain Dew",
  64.     "Diet Pepsi",
  65.     "Iced Tea"
  66.     };
  67.  
  68. void dump_form() {
  69.     printf("<TITLE>Form for Submarine Order</TITLE>%c",LF);
  70.     printf("<H1>Jimmy John's Submarine Order Form</H1>%c",LF);
  71.     printf("This form will send a faxed order to Jimmy John's in Champaign. Prop
  72. er password is requred%c",LF);
  73.     printf("for order to be submitted, otherwise a copy of the order that would
  74. have been submitted will%c",LF);
  75.     printf("will be displayed.<P>%c",LF);
  76.     printf("<HR>%c",LF);
  77.     printf("<FORM ACTION=\"http://hoohoo.ncsa.uiuc.edu:80/htbin/jj\">%c",LF);
  78.     printf("Password: <INPUT TYPE=\"text\" NAME=\"pwd\" MAXLENGTH=\"20\"><P>%c",
  79. LF);
  80.     printf("<H3>Sub Type</H3>%c",LF);
  81.     printf("Select which you would like of the following:<P>%c",LF);
  82.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"0\">%s:%c",sublist[0],
  83. LF);
  84.     printf("Smoked virginia ham and provolone cheese topped with lettuce, tomato
  85. , and mayo.<P>%c",LF);
  86.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"1\">%s:%c",sublist[1],
  87. LF);
  88.     printf("Medium rare shaved roast beef topped with mayo, lettuce, and tomato.
  89. <P>%c",LF);
  90.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"2\">%s:%c",sublist[2],
  91. LF);
  92.     printf("Tuna, mixed with celery, onions, and sauce, topped with lettuce,%c",
  93. LF);
  94.     printf("tomato, and alfalfa sprouts.<P>%c",LF);
  95.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"3\">%s:%c",sublist[3],
  96. LF);
  97.     printf("Turkey breast topped with lettuce, mayo, alfalfa sprouts, and mayo.<
  98. P>%c",LF);
  99.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"4\">%s:%c",sublist[4],
  100. LF);
  101.     printf("Genoa salami and provolone cheese topped with capacola, onion, lettu
  102. ce, tomato, and Italian sauce.<P>%c",LF);
  103.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"5\">%s:%c",sublist[5],
  104. LF);
  105.     printf("Layers of provolone cheese, separated by avocado, sprouts, lettuce,
  106. tomato, and mayo.<P>%c",LF);
  107.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"6\">%s:%c",sublist[6],
  108. LF);
  109.     printf("1/4 pound of smoked ham, provolone cheese, topped with lettuce,%c",L
  110. F);
  111.     printf("tomato, and mayo.<P>%c",LF);
  112.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"7\">%s:%c",sublist[7],
  113. LF);
  114.     printf("Shaved roast beef, provolone cheese, french dijon mustard, topped wi
  115. th shaved ham, lettuce,%c",LF);
  116.     printf("tomato, and mayo.<P>%c",LF);
  117.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"8\">%s:%c",sublist[8],
  118. LF);
  119.     printf("Genoa salami, Italian capacola, smoked ham, and provolone cheese top
  120. ped with lettuce,%c",LF);
  121.     printf("tomato, onions, mayo, and Italian sauce.<P>%c",LF);
  122.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"9\">%s:%c",sublist[9],
  123. LF);
  124.     printf("1/4 pound of sliced roast beef, provolone cheese, topped with lettuc
  125. e, tomato, and mayo.<P>%c",LF);
  126.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"10\">%s:%c",sublist[10
  127. ],LF);
  128.     printf("Turkey breast, smoked ham, and provolonecheese topped with lettuce,
  129. tomato, and mayo.<P>%c",LF);
  130.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sub\" VALUE=\"11\">%s:%c",sublist[11
  131. ],LF);
  132.     printf("Turkey breast, avocado, and cheese topped with lettuce, mayo, alfalf
  133. a, and tomato.<P>%c",LF);
  134.     printf("<H3>Slim Jim Subs</H3>%c",LF);
  135.     printf("Subs without veggies or sauce.<P>%c",LF);
  136.     printf("<INPUT TYPE=\"checkbox\" NAME=\"slj\" VALUE=\"0\">%s<P>%c",slimlist[
  137. 0],LF);
  138.     printf("<INPUT TYPE=\"checkbox\" NAME=\"slj\" VALUE=\"1\">%s<P>%c",slimlist[
  139. 1],LF);
  140.     printf("<INPUT TYPE=\"checkbox\" NAME=\"slj\" VALUE=\"2\">%s<P>%c",slimlist[
  141. 2],LF);
  142.     printf("<INPUT TYPE=\"checkbox\" NAME=\"slj\" VALUE=\"3\">%s<P>%c",slimlist[
  143. 3],LF);
  144.     printf("<INPUT TYPE=\"checkbox\" NAME=\"slj\" VALUE=\"4\">%s<P>%c",slimlist[
  145. 4],LF);
  146.     printf("<INPUT TYPE=\"checkbox\" NAME=\"slj\" VALUE=\"5\">%s<P>%c",slimlist[
  147. 5],LF);
  148.     printf("<H3>Side orders</H3>%c",LF);
  149.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sde\" VALUE=\"0\">%s<P>%c",sidelist[
  150. 0],LF);
  151.     printf("<INPUT TYPE=\"checkbox\" NAME=\"sde\" VALUE=\"1\">%s<P>%c",sidelist[
  152. 1],LF);
  153.     printf("<H3>Drinks</H3>%c",LF);
  154.     printf("<INPUT TYPE=\"checkbox\" NAME=\"pop\" VALUE=\"0\">%s<P>%c",poplist[0
  155. ],LF);
  156.     printf("<INPUT TYPE=\"checkbox\" NAME=\"pop\" VALUE=\"1\">%s<P>%c",poplist[1
  157. ],LF);
  158.     printf("<INPUT TYPE=\"checkbox\" NAME=\"pop\" VALUE=\"2\">%s<P>%c",poplist[2
  159. ],LF);
  160.     printf("<INPUT TYPE=\"checkbox\" NAME=\"pop\" VALUE=\"3\">%s<P>%c",poplist[3
  161. ],LF);
  162.     printf("<H3>Your Address, Phone Number, and Name</H3>%c",LF);
  163.     printf("<INPUT TYPE=\"text\" NAME=\"name\" MAXLENGTH=\"32\">Name<P>%c",LF);
  164.     printf("<INPUT TYPE=\"text\" NAME=\"adr\" MAXLENGTH=\"64\">Address<P>%c",LF)
  165. ;
  166.     printf("<INPUT TYPE=\"text\" NAME=\"phone\" MAXLENGTH=\"10\">Phone Number<P>
  167. %c",LF);
  168.     printf("<INPUT type=\"submit\">%c",LF);
  169.     printf("</FORM>%c",LF);
  170.     exit(0);
  171. }
  172.  
  173. void print_error(char *reason) {
  174.     printf("<TITLE>Order Not Submitted</TITLE>%c",LF);
  175.     printf("<H1>Order Not Submitted</H1>%c",LF);
  176.     printf("Your order has not been submitted, because %s.<P>%c",reason,LF);
  177.     exit(1);
  178. }
  179.  
  180. main(int argc, char *argv[]) {
  181.     register int x,m=0;
  182.     char *cl;
  183.     char w[256];
  184.     char tfile[L_tmpnam];
  185.     int subs,slims,sides,drinks,allow;
  186.     char name[32];
  187.     char phone[10];
  188.     char address[64];
  189.     FILE *tfp,*order;
  190.  
  191.     printf("Content-type: text/html%c%c",LF,LF);
  192.  
  193.     cl=getenv("QUERY_STRING");
  194.     if((!cl) || (!cl[0]))
  195.         dump_form();
  196.  
  197.     tmpnam(tfile);
  198.     if(!(tfp=fopen(tfile,"w"))) {
  199.         printf("<TITLE>Server Error</TITLE>%c",LF);
  200.         printf("<H1>Server Error</H1>%c",LF);
  201.         printf("Server unable to get a temporary file. Please try again later.<P
  202. >%c",LF);
  203.         exit(1);
  204.     }
  205.  
  206.     subs=0;slims=0;sides=0;drinks=0;allow=0;
  207.     name[0]='\0';
  208.     phone[0]='\0';
  209.     address[0]='\0';
  210.  
  211.     for(x=0;cl[0] != '\0'; x++) {
  212.         m=x;
  213.         getword(w,cl,'=');
  214.         plustospace(w);
  215.         unescape_url(w);
  216.         if(!strcmp(w,"pwd")) {
  217.             getword(w,cl,'&');
  218.             plustospace(w);
  219.             unescape_url(w);
  220.             allow=(strcmp(w,PASSWORD) ? 0 : 1);
  221.         }
  222.         if(!strcmp(w,"sub")) {
  223.             getword(w,cl,'&');
  224.             plustospace(w);
  225.             unescape_url(w);
  226.             subs |= (1 << atoi(w));
  227.         }
  228.         else if(!strcmp(w,"slj")) {
  229.             getword(w,cl,'&');
  230.             plustospace(w);
  231.             unescape_url(w);
  232.             slims |= (1 << atoi(w));
  233.         }
  234.         else if(!strcmp(w,"sde")) {
  235.             getword(w,cl,'&');
  236.             plustospace(w);
  237.             unescape_url(w);
  238.             sides |= (1 << atoi(w));
  239.         }
  240.         else if(!strcmp(w,"pop")) {
  241.             getword(w,cl,'&');
  242.             plustospace(w);
  243.             unescape_url(w);
  244.             drinks |= (1 << atoi(w));
  245.         }
  246.         else if(!strcmp(w,"name")) {
  247.             getword(w,cl,'&');
  248.             plustospace(w);
  249.             unescape_url(w);
  250.             strcpy(name,w);
  251.         }
  252.         else if(!strcmp(w,"phone")) {
  253.             getword(w,cl,'&');
  254.             plustospace(w);
  255.             unescape_url(w);
  256.             strcpy(phone,w);
  257.         }
  258.         else if(!strcmp(w,"adr")) {
  259.             getword(w,cl,'&');
  260.             plustospace(w);
  261.             unescape_url(w);
  262.             strcpy(address,w);
  263.         }
  264.     }
  265.  
  266.     if(!name[0]) print_error("you didn't give your name");
  267.     if(!address[0]) print_error("you didn't give your address");
  268.     if(!phone[0]) print_error("you didn't give your phone number");
  269.     if((!subs) && (!slims) && (!sides) && (!drinks)) print_error("you didn't ord
  270. er anything");
  271.  
  272.     if(allow) {
  273.         char t[256];
  274.         sprintf(t,"/bin/mail %s",JJ_FAX);
  275.         if(!(order=popen(t,"w")))
  276.             print_error("the server was unable to open a pipe to mail");
  277.         printf("<TITLE>Order Sent</TITLE>%c",LF);
  278.         printf("<H1>Order Sent</H1>%c",LF);
  279.         printf("Your order has been sent to the UIUC e-mail to FAX gateway.<P>%c
  280. ",LF);
  281.     } else {
  282.         printf("<TITLE>Your Order</TITLE>%c",LF);
  283.         printf("<H1>Your Order</H1>%c",LF);
  284.         printf("This is how your order would have looked if it had been sent.<P>
  285. <PLAINTEXT>%c",LF);
  286.         order=stdout;
  287.     }
  288.  
  289.     fprintf(order,"My name is %s, and I would like to have the following%c",
  290.             name,LF);
  291.     fprintf(order,"order delivered to %s:%c%c",address,LF,LF);
  292.     for(x=0;x<12;x++)
  293.         if(subs & (1 << x))
  294.             fprintf(order,"\t(1) %s%c",sublist[x],LF);
  295.     for(x=0;x<6;x++)
  296.         if(slims & (1 << x))
  297.             fprintf(order,"\t(1) %s Slim Jim%c",slimlist[x],LF);
  298.     for(x=0;x<2;x++)
  299.         if(sides & (1 << x))
  300.             fprintf(order,"\t(1) %s%c",sidelist[x],LF);
  301.     for(x=0;x<4;x++)
  302.         if(drinks & (1 << x))
  303.             fprintf(order,"\t(1) %s%c",poplist[x],LF);
  304.     fprintf(order,"%cPlease feel free to call me at %s if there is any%c",LF,
  305.             phone,LF);
  306.     fprintf(order,"problem. Thank you.%c%c.%c",LF,LF,LF);
  307.     fclose(order);
  308.     exit(0);
  309. }
Advertisement
Add Comment
Please, Sign In to add comment