Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # written by andrewt@cse.unsw.edu.au September 2016
- # as a starting point for COMP2041/9041 assignment 2
- # http://cgi.cse.unsw.edu.au/~cs2041/assignments/matelook/
- use CGI qw/:all/;
- use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
- sub main() {
- # print start of HTML ASAP to assist debugging if there is an error in the script
- print page_header();
- # Now tell CGI::Carp to embed any warning in HTML
- warningsToBrowser(1);
- # define some global variables
- $debug = 1;
- $users_dir = "dataset-medium";
- verify_user();
- }
- # Verifies username and password
- sub verify_user {
- $username = param('username') || '';
- $password = param('password') || '';
- $current_user = param('current_user');
- #sanitise username
- $username = substr $username, 0, 256;
- $username =~ s/\W//g;
- if ($username && $password) {
- $details_file = "$users_dir/$username/user.txt";
- open my $p, "$details_file";
- my $correct_password = "";
- while ($line = <$p>) {
- if ($line =~ /password=/) {
- $line =~ s/password=//;
- $correct_password = $line;
- }
- if ($line =~ /zid=/) {
- $line =~ s/zid=//;
- $username = $line;
- }
- }
- close $p;
- chomp $correct_password;
- if ($password eq $correct_password) {
- print "Welcome to Matelook, $username! :)";
- $current_user = $username;
- print user_search($username);
- print user_page($username);
- print page_trailer();
- } else {
- print "Incorrect password/username!\n";
- }
- } else {
- if ($current_user ne '') { #already someone logged in
- print <<eof;
- <form method="post" action="">
- <input type="hidden" name="current_user" value="$username">
- </form>
- eof
- print "Welcome to Matelook, $current_user! :)";
- print user_search($current_user);
- print user_page($current_user);
- print page_trailer();
- } else {
- print start_form, "\n";
- if ($username) {
- print hidden('username'), "\n";
- } else {
- print "Username:\n", textfield('username'), "\n";
- }
- if ($password) {
- print hidden('password');
- # <input type="hidden" name="current_user" value="$current_user">
- } else {
- print "Password:\n", textfield('password'), "\n";
- }
- print submit(value => Login), "\n";
- print end_form;
- }
- }
- }
- sub user_search {
- my $username = param('username');
- my $search_name = param('search_name');
- my @users = sort(glob("$users_dir/*"));
- if (defined $search_name) {
- foreach $user_details(@users) {
- $user_details = $user_details."/user.txt";
- open my $p, "$user_details";
- while ($line = <$p>) {
- chomp $line;
- if ($line =~ /full_name=/) {
- $line =~ s/full_name=//;
- if ($line =~ m/$search_name/g) {
- if ($line =~ /zid=/) {
- $line =~ s/zid=//;
- $username = $line;
- print user_page($username);
- }
- }
- }
- }
- }
- }
- print <<eof
- <form method="post">
- Search Name: <input type="text" name="search_name">
- <input type="hidden" name="current_user" value="$username">
- <input type="submit" class="btn btn-link" value="Search user">
- </form>
- eof
- }
- #
- # Show unformatted details for user "n".
- # Increment parameter n and store it as a hidden variable
- #
- sub user_page {
- chomp $username;
- my $user_to_show = shift;
- my $details_filename = "$users_dir/$user_to_show/user.txt";
- my $dp_filename = "$users_dir/$user_to_show/profile.jpg";
- my $posts_filename = "$users_dir/$user_to_show/posts/0/post.txt";
- open my $p, "$details_filename" or die "can not open $details_filename: $!";
- while ($line = <$p>) {
- $line =~ s/\n//g;
- if ($line =~ /email=/ || $line =~ /password=/ || $line =~ /courses=/ || $line =~ /home_latitude=/ || $line =~ /home_longitude=/) {
- next;
- } elsif ($line =~ /mates=/) {
- $line =~ s/mates=\[//;
- $line =~ s/\]//;
- $line =~ s/, /\n/g;
- $mates = $line;
- next;
- } elsif ($line =~ /birthday=/) {
- $line =~ s/birthday=//;
- $birthday = $line;
- next;
- } elsif ($line =~ /program=/) {
- $line =~ s/program=//;
- $program = $line;
- next;
- } elsif ($line =~ /full_name=/) {
- $line =~ s/full_name=//;
- $name = $line;
- next;
- } elsif ($line =~ /zid=/) {
- $line =~ s/zid=//;
- next;
- } elsif ($line =~ /home_suburb=/) {
- $line =~ s/home_suburb=//;
- $suburb = $line;
- next;
- }
- }
- close $p;
- open my $p, "$posts_filename" or die "cannot open $posts_filename: $!";
- my @posts_indexes = ();
- my %posts_hash = ();
- my @posts= ();
- while ($line = <$p>) {
- if ($line =~ /\d{10}/) {
- push @posts_indexes, $line;
- }
- }
- close $p;
- foreach $posts_index(@posts_indexes) {
- $post_address = "dataset-medium/posts/$post_index/[0-9]";
- open my $p, "$post_address" or die "dcannot open $post_address: $!";
- my $time = 0;
- my $post = " ";
- while ($line = <$p>) {
- if ($line =~ /message=/) {
- $line =~ s/message=//;
- $post = $line;
- }
- if ($line =~ /time=/) {
- $line =~ s/time=//;
- $time = $line;
- }
- }
- close $p;
- $posts_hash{$post} = $time;
- }
- @posts = sort {$posts_hash{$b}<=>$posts_hash{$a}} keys %posts_hash;
- my $post_print = join('', @posts);
- return <<eof
- <div class="matelook_user_details">
- <img src="$dp_filename" class="img-thumbnail">
- <h3>$name</h3>ZID: $zid
- Program: $program
- Birthday: $birthday
- Lives in: $suburb
- Posts:
- $post_print
- </div>
- <p>
- <form method="POST" action="">
- <input type="submit" value="Log Out :(" class="matelook_button">
- </form>
- eof
- }
- #
- # HTML placed at the top of every page
- #
- sub page_header {
- return <<eof
- Content-Type: text/html;charset=utf-8
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>matelook</title>
- <link href="matelook.css" rel="stylesheet">
- </head>
- <body>
- <div class="matelook_heading">
- matelook
- </div>
- eof
- }
- #
- # HTML placed at the bottom of every page
- # It includes all supplied parameter values as a HTML comment
- # if global variable $debug is set
- #
- sub page_trailer {
- my $html = "";
- $html .= join("", map("<!-- $_=".param($_)." -->\n", param())) if $debug;
- $html .= end_html;
- return $html;
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement