Guest User

Untitled

a guest
Jun 7th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.35 KB | None | 0 0
  1. #!/usr/bin/perl -wT
  2. use DBI;
  3.  
  4. #Declare MySQL connection variables
  5. $db = "int420_113c10";
  6. $user = "int420_113c10";
  7. $passwd = "cqTN5238";
  8. $host = "db-mysql.zenit";
  9. $connectionInfo = "dbi:mysql:$db;$host";
  10.  
  11. print "Content-type: text/html\n";
  12.  
  13.  
  14. #If first time user is loading page display login
  15. #ELSE
  16. if ($ENV{'REQUEST_METHOD'} eq "GET") {
  17.         &displayLogin();
  18. } else {
  19.         &parseForm();
  20.         $dbh = DBI->connect($connectionInfo,$user,$passwd);
  21.         if ($FORM{'submit'} eq "Login") {
  22.                 &verifyLogin();
  23.                 &createNewSession();
  24.                 print qq~Set-Cookie: DATA=$session:1; path=/; ~;
  25.                 print qq~expires Mon, 30-Jan-2012 12:00:00 GMT
  26. ~;
  27.                 $query = "SELECT QID, question FROM survey WHERE QID = 1";
  28.                 &queryDB();
  29.                 @Qsurvey = $sth->fetchrow_array();
  30.                 &displaySurvey();
  31.         } else {
  32.                 &parseCookie();
  33.                 &verifyAwnser();
  34.                 $qnum = $qnum + 1;
  35.                 print qq~Set-Cookie: DATA=$session:$qnum; path=/; ~;
  36.                 print qq~expires Mon, 30-2012 12:00:00 GMT
  37. ~;
  38.                 $query = "SELECT QID, question FROM survey WHERE QID = $qnum";
  39.                 &queryDB();
  40.                 @Qsurvey = $sth->fetchrow_array();
  41.                 if ($Qsurvey[0] ne "") {
  42.                         &displaySurvey();
  43.                 } else {
  44.                         $query = "INSERT INTO final SELECT * FROM awnsers WHERE sessionID = $session ";
  45.                         &queryDB();
  46.                         &displayFinal();
  47.                 }
  48.         }
  49. }
  50.  
  51.  
  52. #Standard form parseing for POST data
  53. sub parseForm {
  54.         read(STDIN, $qstring, $ENV{'CONTENT_LENGTH'});
  55.         @pairs = split(/&/, $qstring);
  56.         foreach (@pairs) {
  57.                 ($key,$value) = split(/=/);
  58.                 $value =~ tr/+/ /;
  59.                 $value =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg;
  60.                 $FORM{$key} = $value;
  61.         }
  62. }
  63.  
  64.  
  65. #Creates new session
  66. sub createNewSession {
  67.         $query = "SELECT MAX(sessionID) FROM awnsers";
  68.         &queryDB();
  69.         $session = $sth->fetchrow();
  70.         if ($session eq "") {
  71.                 $session = 1;
  72.         } else {
  73.                 $session++;
  74.         }
  75. }
  76.  
  77.  
  78. #Prases information from cookies
  79. sub parseCookie {
  80.         $cookieData = $ENV{'HTTP_COOKIE'};
  81.         ($name,$data) = split(/=/,$cookieData);
  82.         $name = 0;
  83.         ($session,$qnum) = split(/:/,$data);
  84. }
  85.  
  86.  
  87. #Verifies login information matches a record in the DB
  88. #If any data is incorrect, display an appropriate error
  89. sub verifyLogin {
  90.         $query = "SELECT name, password, id FROM login WHERE name = '$FORM{username}'";
  91.         &queryDB();
  92.         if (@Qlogin = $sth->fetchrow_array()) {
  93.                 $salt = "asdf";
  94.                 $pass = $FORM{'password'};
  95.                 $cryptPasswd = crypt($pass,$salt);
  96.                 if ($cryptPasswd ne $Qlogin[1]) {
  97.                         $error = "\n<font color=red>Incorrect password</font>";
  98.                         &displayLogin();
  99.                         exit;
  100.                 }
  101.         } else {
  102.                 $error = "\n<font color=red>Username does not exist</font>";
  103.                 &displayLogin();
  104.                 exit;
  105.         }
  106. }
  107.  
  108.  
  109. #Verifies an awnser was given in the survey
  110. sub verifyAwnser {
  111.         $query = "SELECT QID, question FROM survey WHERE QID = $qnum";
  112.         &queryDB();
  113.         @Qsurvey = $sth->fetchrow_array();
  114.         if ($FORM{'awnser'} eq "") {
  115.                 $error = "<br>\n<font color=red>Please awnser the question!</font>";
  116.                 &displaySurvey();
  117.                 exit;
  118.         } else {
  119.                 $query = "INSERT INTO awnsers (sessionID, awnser, question) VALUES ($session, '$FORM {'awnser'}', '$Qsurvey[1]')";
  120.                 &queryDB();
  121.         }
  122. }
  123.  
  124.  
  125. #Quries database with '$query' variable
  126. sub queryDB {
  127.         $sth = $dbh->prepare($query);
  128.         $sth->execute() or print qq~
  129. <html>
  130. <head>
  131. <title>DATABASE ERROR</title>
  132. </head>
  133. <body>
  134. <center>
  135. <h1><font color="red">DATABASE ERROR!</font></h1>
  136. </center>
  137. </body>
  138. </html>~;
  139. }
  140.  
  141.  
  142. #Displays login webpage
  143. sub displayLogin {
  144.         print qq~
  145. <html>
  146. <head>
  147. <title>Login</title>
  148. </head>
  149. <body>
  150. <center>
  151. <h3>Login:</h3>
  152. <hr><br>
  153. <form action="survey.cgi" method="post">
  154.         Username:&nbsp;<input type="text" name="username" value="$FORM{'username'}">
  155.         <br>
  156.         Password:&nbsp;<input type="password" name="password" value="$FORM{'password'}">
  157.         <br><br>
  158.         <input type="submit" name="submit" value="Login">
  159. </form>$error
  160. </center>
  161. </body>
  162. </html>~;
  163. }
  164.  
  165.  
  166. #Displays survey webpage
  167. sub displaySurvey {
  168.         print qq~
  169. <html>
  170. <head>
  171. <title>Survey</title>
  172. </head>
  173. <body>
  174. <center>
  175. <h3>Survey</h3>
  176. <hr><br>
  177. <table width="80%" align="center" border="1">
  178.         <tr>
  179.                 <td width="20%" align="center"><strong>Number</strong></td>
  180.                 <td width="80%" align="center"><strong>Question</strong></td>
  181.         </tr><tr>
  182.                 <td width="20%" align="center"><br>$Qsurvey[0]<br>&nbsp;</td>
  183.                 <td width="80%" align="center"><br>$Qsurvey[1]<br>&nbsp;</td>
  184.         </tr><tr>
  185.                 <td colspan="2" align="center">
  186.                 <br>
  187.                 <form action="survey.cgi" method="post">
  188.                         <input type="text" size="80%" name="awnser">
  189.                 </td>
  190.         </tr>
  191. </table>
  192. <br>
  193. <center>
  194.         <input type="submit" name="submit" value="Next">
  195. </center>
  196. </form>$error
  197. </body>
  198. </html>~;
  199. }
  200.  
  201.  
  202. #Displays webpage for results
  203. sub displayFinal {
  204.         print qq~
  205. <html>
  206. <head>
  207. <title>Results</title>
  208. </head>
  209. <body>
  210. <center>
  211. <h3>Results</h3>
  212. <hr><br>
  213. <table width="90%" align="center" border="1">
  214.         <tr>
  215.                 <td width="50%" align="center"><strong>Question</strong></td>
  216.                 <td width="50%" align="center"><strong>Awnser</strong></td></tr>~;
  217.         $query = "SELECT question, awnser FROM final WHERE sessionID = $session";
  218.         &queryDB();
  219.         @Qresults = $sth->fetchrow_array();
  220.         while ($Qresults[0] ne "") {
  221.                 print "<tr><td>$Qresults[0]</td>\n<td>$Qresults[1]</td></tr>\n";
  222.                 @Qresults = $sth->fetchrow_array();
  223.         }
  224.         print qq~
  225. </table>
  226. </center>
  227. </form>
  228. </html>~;
  229. }
Add Comment
Please, Sign In to add comment