Advertisement
Guest User

Untitled

a guest
May 19th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use CGI qw(:standard);
  4. use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
  5. use DBI;
  6. use strict;
  7.  
  8.  
  9. my $cgi = new CGI;
  10. my $fname;
  11. my $lname;
  12. my $email;
  13. my $userid;
  14. my $register;
  15. my @error;
  16.  
  17. $register = $cgi->param('login');
  18.  
  19. if ($register) {
  20. if ($cgi->param('fname') && $cgi->param('lname') && $cgi->param('email')) {
  21. #Copy user input into variables in the program
  22. $fname = $cgi->param('fname');
  23. $lname = $cgi->param('lname');
  24. $email = $cgi->param('email');
  25.  
  26. # Validate user input
  27. my $ok_data;
  28. $ok_data = &validate_data;
  29.  
  30. #If data is good, generate a userid
  31. if ($ok_data == 1) {
  32. $userid = &create_id;
  33. #connect to the database;
  34. my $dbh = DBI->connect("DBI:mysql:host=db-mysql;database=int322_101b06;port=zenit.senecac.on.ca:20684", "", "") or die("Can't connect to database.");
  35. #Get the table "users"
  36. #Select all data and try to match lname||userid. If lname||userid does not
  37. # exist, then lname||userid can be stored.
  38. my $sth = $dbh->prepare("SELECT * FROM users");
  39. $sth->execute;
  40. my $username_taken = 0;
  41. my @result;
  42. while (($username_taken == 0) && (@result = $sth->fetchrow_array())) {
  43. if (($lname == $result[2]) && ($userid == $result[0])) {
  44. $username_taken = 1;
  45. }
  46. }
  47. $dbh->disconnect();
  48. #If lname||userid already, exists, ask user for a new set of info
  49. if ($username_taken == 1) {
  50. push(@error, "Username already exists. Please register a different name.");
  51. &show_form(\@error);
  52. }
  53. else { #if lname and userid combo is unique, add to the database
  54. # Display user info back to the user
  55. &add_to_database;
  56. &repeat_info;
  57. }
  58. } #if ok_data != 1
  59. else {
  60. #Then @error would be filled. Pass this on to the form.
  61. &show_form(\@error);
  62. }
  63. }
  64. else {
  65. &show_form;
  66. }
  67. }
  68.  
  69. sub add_to_database {
  70. my $dbh = DBI->connect("DBI:mysql:host=db-mysql;database=int322_101b06;port=zenit.senecac.on.ca:20684", "", "") or die("Can't connect to database.");
  71. my $sth = $dbh->prepare("INSERT INTO users VALUES(?,?,?,?)");
  72. $sth->execute($userid, $fname, $lname, $email);
  73. $sth->finish;
  74. $dbh->disconnect();
  75. my $userfile = $lname . $userid . '.' . 'acct';
  76. open FILE, ">accounts/$userfile";
  77. my $string = $userid . ',' . $fname . ',' . $lname . ',' . $email;
  78. print FILE $string;
  79. close(FILE);
  80. }
  81.  
  82. sub repeat_info {
  83. print $cgi->header(),
  84. $cgi->start_html('Your account information'),
  85. $cgi->h3('Account information'),
  86. $cgi->p('You need your last name and userid to sign in next time'),
  87. $cgi->p('Userid: ', $userid);
  88. print $cgi->table({-border=>'0', -cellspacing=>'3', -cellpadding=>'3'},
  89. Tr(
  90. [
  91. td(['First Name: ', $fname]),
  92. td(['Last Name: ', $lname]),
  93. td(['Email: ', $email])
  94. ]
  95. )
  96. );
  97. print $cgi->a({-href=>"start.cgi"}, "Go here to start");
  98. print $cgi->end_html;
  99. }
  100.  
  101. sub validate_data {
  102. #Validate first name
  103. if ($fname =~ /^[A-Za-z\ ]+$/) {
  104. push(@error, "ok-fname");
  105. }
  106. else {
  107. push(@error, "First name can only have letters<br/>");
  108. }
  109. #Validate last name
  110. if ($lname =~ /^[A-Za-z\ ]+$/) {
  111. push(@error, "ok-lname");
  112. }
  113. else {
  114. push(@error, "Last name can only have letters<br/>");
  115. }
  116. #Validate email
  117. if ($email =~ /^[A-Za-z:\/.~\?=\+&@]+$/){
  118. push(@error, "ok-email");
  119. }
  120. else {
  121. push(@error, "Email field has invalid characters<br/>");
  122. }
  123. #Count the number of errors in the register page
  124. my $i; #counter for the elements in the @error array
  125. my $numErrors = 3;
  126. for ($i = 0; $i < 3; $i++) {
  127. if ($error[$i] =~ /^ok/) {
  128. delete $error[$i];
  129. $numErrors -= 1;
  130. }
  131. }
  132. #If there are errors, display form with user input and msg where errors are
  133. if ($numErrors > 0) {
  134. return 0; #Return false, data is not good
  135. }
  136. else {
  137. return 1; #Return true, data is good
  138. }
  139. }
  140.  
  141. sub show_form {
  142. print $cgi->header(),
  143. $cgi->start_html("Register - Soccer Pool"),
  144. $cgi->h3('Register to join the soccer pool');
  145. if (@error) {
  146. print join("", @error);
  147. }
  148. print $cgi->startform();
  149. print $cgi->table({-border=>'0', -cellspacing=>'3'},
  150. Tr(
  151. [
  152. td(['First Name', $cgi->textfield(-name=>'fname', -maxlength=>'25', -value=>"$fname")]),
  153. td(['Last Name', $cgi->textfield(-name=>'lname', -maxlength=>'25', -value=>"$lname")]),
  154. td(['Email', $cgi->textfield(-name=>'email', -maxlength=>'60', -value=>"$email")])
  155. ]
  156. )
  157. );
  158. print $cgi->submit(-value=>'Submit'), $cgi->reset();
  159. print $cgi->endform;
  160. print $cgi->end_html;
  161. }
  162.  
  163. sub create_id {
  164. my $number = int(rand(9999999999));
  165. while (length($number) < 10) {
  166. $number = '0' . $number;
  167. }
  168. return $number;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement