Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.15 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # written by andrewt@cse.unsw.edu.au September 2016
  4. # as a starting point for COMP2041/9041 assignment 2
  5. # http://cgi.cse.unsw.edu.au/~cs2041/assignments/matelook/
  6.  
  7. use CGI qw/:all/;
  8. use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
  9.  
  10. sub main() {
  11.     # print start of HTML ASAP to assist debugging if there is an error in the script
  12.     print page_header();
  13.  
  14.     # Now tell CGI::Carp to embed any warning in HTML
  15.     warningsToBrowser(1);
  16.  
  17.     # define some global variables
  18.     $debug = 1;
  19.     $users_dir = "dataset-medium";
  20.  
  21.     verify_user();
  22. }
  23.  
  24. # Verifies username and password
  25. sub verify_user {
  26.     $username = param('username') || '';
  27.     $password = param('password') || '';
  28.     $current_user = param('current_user');
  29.  
  30.     #sanitise username
  31.     $username = substr $username, 0, 256;
  32.     $username =~ s/\W//g;
  33.  
  34.     if ($username && $password) {
  35.         $details_file = "$users_dir/$username/user.txt";
  36.         open my $p, "$details_file";
  37.         my $correct_password = "";
  38.         while ($line = <$p>) {
  39.             if ($line =~ /password=/) {
  40.                 $line =~ s/password=//;
  41.                 $correct_password = $line;
  42.             }
  43.             if ($line =~ /zid=/) {
  44.                 $line =~ s/zid=//;
  45.                 $username = $line;
  46.             }
  47.         }
  48.         close $p;
  49.         chomp $correct_password;
  50.         if ($password eq $correct_password) {
  51.             print "Welcome to Matelook, $username! :)";
  52.             $current_user = $username;
  53.             print user_search($username);
  54.             print user_page($username);
  55.             print page_trailer();
  56.         } else {
  57.             print "Incorrect password/username!\n";
  58.         }
  59.     } else {
  60.         if ($current_user ne '') { #already someone logged in
  61.             print <<eof;
  62. <form method="post" action="">
  63. <input type="hidden" name="current_user" value="$username">
  64. </form>
  65.  
  66. eof
  67.             print "Welcome to Matelook, $current_user! :)";
  68.             print user_search($current_user);
  69.             print user_page($current_user);
  70.             print page_trailer();
  71.         } else {
  72.             print start_form, "\n";
  73.             if ($username) {
  74.                 print hidden('username'), "\n";
  75.             } else {
  76.                 print "Username:\n", textfield('username'), "\n";
  77.             }
  78.             if ($password) {
  79.                 print hidden('password');
  80. #               <input type="hidden" name="current_user" value="$current_user">
  81.             } else {
  82.                 print "Password:\n", textfield('password'), "\n";
  83.             }
  84.             print submit(value => Login), "\n";
  85.             print end_form;
  86.         }
  87.     }
  88.  
  89.  
  90. }
  91.  
  92.  
  93. sub user_search {
  94.     my $username = param('username');
  95.     my $search_name = param('search_name');
  96.     my @users = sort(glob("$users_dir/*"));
  97.  
  98.     if (defined $search_name) {
  99.         foreach $user_details(@users) {
  100.             $user_details = $user_details."/user.txt";
  101.             open my $p, "$user_details";
  102.             while ($line = <$p>) {
  103.                 chomp $line;
  104.                 if ($line =~ /full_name=/) {
  105.                     $line =~ s/full_name=//;
  106.                     if ($line =~ m/$search_name/g) {
  107.                         if ($line =~ /zid=/) {
  108.                             $line =~ s/zid=//;
  109.                             $username = $line;
  110.                             print user_page($username);
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.     print <<eof
  119. <form method="post">
  120. Search Name: <input type="text" name="search_name">
  121. <input type="hidden" name="current_user" value="$username">
  122. <input type="submit" class="btn btn-link" value="Search user">
  123.  
  124. </form>
  125.  
  126. eof
  127.  
  128. }
  129.  
  130.  
  131.  
  132. #
  133. # Show unformatted details for user "n".
  134. # Increment parameter n and store it as a hidden variable
  135. #
  136. sub user_page {
  137.     chomp $username;
  138.     my $user_to_show = shift;
  139.     my $details_filename = "$users_dir/$user_to_show/user.txt";
  140.     my $dp_filename = "$users_dir/$user_to_show/profile.jpg";
  141.     my $posts_filename = "$users_dir/$user_to_show/posts/0/post.txt";
  142.  
  143.     open my $p, "$details_filename" or die "can not open $details_filename: $!";
  144.     while ($line = <$p>) {
  145.         $line =~ s/\n//g;
  146.         if ($line =~ /email=/ || $line =~ /password=/ || $line =~ /courses=/ || $line =~ /home_latitude=/ || $line =~ /home_longitude=/) {
  147.             next;
  148.         } elsif ($line =~ /mates=/) {
  149.             $line =~ s/mates=\[//;
  150.             $line =~ s/\]//;
  151.             $line =~ s/, /\n/g;
  152.             $mates = $line;
  153.             next;
  154.         } elsif ($line =~ /birthday=/) {
  155.             $line =~ s/birthday=//;
  156.             $birthday = $line;
  157.             next;
  158.         } elsif ($line =~ /program=/) {
  159.             $line =~ s/program=//;
  160.             $program = $line;
  161.             next;
  162.         } elsif ($line =~ /full_name=/) {
  163.             $line =~ s/full_name=//;
  164.             $name = $line;
  165.             next;
  166.         } elsif ($line =~ /zid=/) {
  167.             $line =~ s/zid=//;
  168.             next;
  169.         } elsif ($line =~ /home_suburb=/) {
  170.             $line =~ s/home_suburb=//;
  171.             $suburb = $line;
  172.             next;
  173.         }
  174.     }
  175.     close $p;
  176.  
  177.     open my $p, "$posts_filename" or die "cannot open $posts_filename: $!";
  178.     my @posts_indexes = ();
  179.     my %posts_hash = ();
  180.     my @posts= ();
  181.     while ($line = <$p>) {
  182.         if ($line =~ /\d{10}/) {
  183.             push @posts_indexes, $line;
  184.         }
  185.     }
  186.     close $p;
  187.  
  188.     foreach $posts_index(@posts_indexes) {
  189.         $post_address = "dataset-medium/posts/$post_index/[0-9]";
  190.         open my $p, "$post_address" or die "dcannot open $post_address: $!";
  191.         my $time = 0;
  192.         my $post = " ";
  193.         while ($line = <$p>) {
  194.             if ($line =~ /message=/) {
  195.                 $line =~ s/message=//;
  196.                 $post = $line;
  197.             }
  198.             if ($line =~ /time=/) {
  199.                 $line =~ s/time=//;
  200.                 $time = $line;
  201.             }
  202.         }
  203.         close $p;
  204.         $posts_hash{$post} = $time;
  205.     }
  206.  
  207.     @posts = sort {$posts_hash{$b}<=>$posts_hash{$a}} keys %posts_hash;
  208.     my $post_print = join('', @posts);
  209.  
  210.     return <<eof
  211. <div class="matelook_user_details">
  212. <img src="$dp_filename" class="img-thumbnail">
  213. <h3>$name</h3>ZID: $zid
  214. Program: $program
  215. Birthday: $birthday
  216. Lives in: $suburb
  217.  
  218. Posts:
  219.  
  220. $post_print
  221. </div>
  222. <p>
  223. <form method="POST" action="">
  224.     <input type="submit" value="Log Out :(" class="matelook_button">
  225. </form>
  226. eof
  227.  
  228. }
  229.  
  230.  
  231. #
  232. # HTML placed at the top of every page
  233. #
  234. sub page_header {
  235.     return <<eof
  236. Content-Type: text/html;charset=utf-8
  237.  
  238. <!DOCTYPE html>
  239. <html lang="en">
  240. <head>
  241. <title>matelook</title>
  242. <link href="matelook.css" rel="stylesheet">
  243. </head>
  244. <body>
  245. <div class="matelook_heading">
  246. matelook
  247. </div>
  248. eof
  249. }
  250.  
  251.  
  252. #
  253. # HTML placed at the bottom of every page
  254. # It includes all supplied parameter values as a HTML comment
  255. # if global variable $debug is set
  256. #
  257. sub page_trailer {
  258.     my $html = "";
  259.     $html .= join("", map("<!-- $_=".param($_)." -->\n", param())) if $debug;
  260.     $html .= end_html;
  261.     return $html;
  262. }
  263.  
  264. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement