Advertisement
Guest User

Untitled

a guest
May 20th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. #!/usr/bin/perl -wT
  2.  
  3. use DBI;
  4.  
  5. $db="int420_101a20";
  6. $user="int420_101a20";
  7. $passwd="18639171";
  8. $host="db-mysql.zenit";
  9. $connectionInfo="dbi:mysql:$db;$host";
  10.  
  11.  
  12. # print "Content-type:text/html\n\n";
  13.  
  14.  
  15. if ($ENV{REQUEST_METHOD} eq "GET")
  16. {
  17. &browsecatalog;
  18. exit;
  19. }
  20. else
  21. {
  22. &parseform;
  23.  
  24. if ($form{submit} eq "Buy")
  25. {
  26. &getcookiedata;
  27. if ($cookie{uid} ne "")
  28. {
  29. &showcheckout;
  30. }
  31. else
  32. {
  33. &senditemcookie;
  34. &showlogin;
  35.  
  36. }
  37. }
  38. elsif ($form{submit} eq "login")
  39. {
  40. if (&validateuser)
  41. {
  42. &sendlogincookie;
  43. &showcheckout;
  44. }
  45. else
  46. {
  47. &showlogin;
  48. }
  49. }
  50. elsif ($form{submit} eq "checkout")
  51. {
  52. if (&validatecheckout)
  53. {
  54. &inserttrans;
  55. &emailreceipt;
  56. &showreceipt;
  57. }
  58. else
  59. {
  60. &showcheckout;
  61. }
  62. }
  63. }
  64.  
  65.  
  66. sub senditemcookie
  67. {
  68.  
  69. print "Content-type:text/html\n";
  70. print "Set-Cookie:product=$from{hidid} \n";
  71. }
  72.  
  73.  
  74. sub browsecatalog
  75. {
  76. print "Content-type:text/html\n";
  77. print qq~
  78. <html>
  79. <title>Ninja Warehouse Inventory</title>
  80. </head>
  81. <body bgcolor="black">
  82. <font color="white">
  83. <center>
  84. <br>
  85. <h1> Ninja Items </h1>
  86. <br>
  87. <br>
  88. <table border cellpadding=10>
  89. <tr style="color:#00FF00">
  90. <th>ID Number</th><th>Prod Name</th><th>Description</th><th>Price</th><th>Picture</th><th>Options</th>
  91. </tr>~;
  92.  
  93. $select=qq~select id, idnum, name, descr, price, pic from inventory order by idnum;~;
  94. $dbh=DBI->connect($connectionInfo,$user,$passwd);
  95. $sth=$dbh->prepare($select);
  96. $sth->execute();
  97. $itemno = $row[0];
  98. while (@row=$sth->fetchrow_array())
  99. {
  100. print qq~
  101. <tr style="color:#00FF00"><td>$row[1]</td><td>$row[2] $row[0]</td> <td>$row[3]</td> <td>\$ $row[4]</td> <td><img src="/img/$row[5]"/></td>
  102. <td>
  103. <form action="stage4.cgi" method="post">
  104. <input type="hidden" name="hidid" value="$row[0]">
  105. <input type="submit" name="submit" value="Buy">
  106.  
  107. </td>
  108. </tr>~
  109. }
  110. print qq~
  111. </table>
  112. </body>
  113. </html>
  114. ~;
  115.  
  116. }
  117.  
  118. sub parseform
  119. {
  120. read(STDIN, $qstring, $ENV{'CONTENT_LENGTH'});
  121.  
  122. @pairs = split(/&/, $qstring);
  123.  
  124. foreach (@pairs) {
  125. ($key, $value) = split(/=/);
  126. $value =~ tr/+/ /;
  127. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  128. $form{$key} = $value;
  129. }
  130.  
  131. }
  132.  
  133.  
  134. sub showlogin
  135. {
  136. print "Content-type:text/html\n";
  137. print qq~
  138. <html>
  139. <head>
  140. <title>Ninja Warehouse</title>
  141. </head>
  142. <body bgcolor="black">
  143. <form action="stage4.cgi" method=POST>
  144. <p>
  145. <font color="white">
  146. <center>
  147. <h2>Ninja Warehouse Customer Registration</h2>
  148. <table border = "0">
  149.  
  150. <tr style="color:#00Ff00"><td> Login Name: </td><td><input type=text name=logn value="$form{logn}" ></td><td> $errors{logn}</td><td> $found</td>
  151. </tr>
  152.  
  153. <tr style="color:#00Ff00"><td> Password: </td><td><input type=text name=passw value="$form{passw}"></td><td> $errors{logn}</td><td> $error{pass}</td>
  154. </tr>
  155.  
  156. </table>
  157. </font>
  158. <input type=submit name="submit" value="login" name"submit">
  159. <br>
  160. <a href=/cgi-bin/stage2.cgi> Register now!<a>
  161. </form>
  162. </p>
  163. </center>
  164. </body>
  165. </html>
  166. ~
  167. }
  168.  
  169. sub validateuser
  170. {
  171. $dbh=DBI->connect($connectionInfo,$user,$passwd);
  172. $select=qq~select id,logn,passw from customers where logn='$form{logn}';~;
  173. $sth=$dbh->prepare($select);
  174. $sth->execute();
  175.  
  176. if (@row=$sth->fetchrow_array())
  177. {}
  178. $pass = crypt($form{passw},"ab");
  179.  
  180. if($form{logn} ne $row[1])
  181. {
  182. $found = "Username Not found";
  183. &showlogin;
  184. exit;
  185. }elsif($pass ne $row[2])
  186. {
  187. $error{pass}="Incorrect Password";
  188. &showlogin;
  189. exit;
  190. }else{
  191. &sendlogincookie;
  192. # &showcheckout;
  193. }
  194. }
  195.  
  196. sub sendlogincookie
  197. {
  198. print "Content-type:text/html\n";
  199. print "Set-Cookie: uid=$row[0]\n";
  200. }
  201.  
  202. sub getcookiedata
  203. {
  204. @cookiepairs=split(/; /, $ENV{'HTTP_COOKIE'});
  205. foreach (@cookiepairs){
  206. ($cookie,$value)=split(/=/);
  207. $value=~ tr/+/ /;
  208. $value=~ s/%([A-Za-z0-9][A-Za-z0-9])/pack("C", hex($1))/eg;
  209. $cookiedata{$cookie}=$value;
  210. }
  211. }
  212.  
  213. sub showcheckout
  214. {
  215.  
  216. &getcookiedata;
  217.  
  218. print "Content-type:text/html\n";
  219.  
  220. $getship= qq~select fname,lname,sadd,city,zip,cnt from customers where id = $cookiedata{uid};~;
  221. $dbh=DBI->connect($connectionInfo,$user,$passwd);
  222. $sth=$dbh->prepare($getship);
  223. $sth->execute();
  224. @srow=$sth->fetchrow_array();
  225.  
  226. print qq~
  227. UID$cookiedata{uid}\n
  228. Product:$cookiedata{product}
  229. <html>
  230. <head>
  231. <title> Ninja Checkout </title>
  232. </head>
  233. <body>
  234. <form action="stage4.cgi" method=post>
  235. <center>
  236. <h2> Confirm your checkout information : </h2>
  237. First Name: <input type=text name=fname value=$srow[0]> $errors{fname}
  238. <br>
  239. Last Name: <input type=text name=lname value=$srow[1]> $errors{lname}
  240. <br>
  241. Address: <input type=text name=address value=$srow[2]> $errors{sadd}
  242. <br>
  243. City <input type=text name=zip value=$srow[3]> $errors{city}
  244. <br>
  245. Postal Code: <input type=text name=zip value=$srow[4]> $errors{zip}
  246. <br>
  247. Country: <input type=text name=cnt value=$srow[5]> $errors{cnt}
  248. <br>
  249. <br>
  250. Credit Card Type: <input type="radio" name=cctype value="Visa">Visa
  251. <input type="radio" name="cctype" value="Mastercard">Mastercard
  252. <input type="radio" name="cctype" value="Amex">American Express
  253. <br>
  254. Credit Card Number: <input type=text name=ccnum value="$form{ccnum}">$errors{ccnum}
  255. <br>
  256. Credit Card Security Code: <input type=text name=cccode value="$form{cccode}">$errors{cccode}
  257. <br>
  258.  
  259. <input type=submit value="Checkout" name=submit>
  260. </form>
  261. </body>
  262. </html>
  263. ~;
  264. }
  265.  
  266. sub validatecheckout
  267. {
  268. %patterns=( 'sadd' => '^[^@$%/*&!]',
  269. 'zip' => '^[a-zA-Z][0-9][a-zA-Z][ -][0-9][a-zA-Z][0-9]$',
  270. 'city' => '^[A-Za-z].*$',
  271. 'ccnum'=>'\d{4}-\d{4}-\d{4}-\d{4}$',
  272. 'cccode'=>'^\d{3}$');
  273. $valid=1;
  274. foreach (keys %form)
  275. {
  276. if($form{$_}!~$patterns{$_})
  277. {
  278. $errormsg="Format is incorrect for this field";
  279. $valid=0;
  280. }else{
  281. $errormsg="";
  282. }
  283. $errors{$_}=$errormsg;
  284. }
  285. return $valid;
  286. }
  287.  
  288. sub inserttrans
  289. {
  290. &getcookiedata;
  291. $insert = qq~insert into transaction (userid, prodnum,cctype,ccnum,cccode)values('$cookiedata{uid}','$cookiedata{product}','$form{cctype}','$form{ccnum}','$form{cccode}');~;
  292. $dbh=DBI->connect($connectionInfo,$user,$passwd);
  293. $sth=$dbh->prepare($insert);
  294. $sth->execute();
  295. $dbh->disconnect();
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement