commit:f147c1fb5e23cff0bfe603db3624be714d76ad5f
author:Chip Black
committer:Chip Black
date:Wed Aug 9 00:26:21 2017 -0500
parents:
Initial commit
diff --git a/cgi-bin/cookies.lib b/cgi-bin/cookies.lib
line changes: +9/-0
index 0000000..cb4dae4
--- /dev/null
+++ b/cgi-bin/cookies.lib
@@ -0,0 +1,9 @@
+if ($ENV{'HTTP_COOKIE'}) {
+    @cookies = split(/;/, $ENV{'HTTP_COOKIE'});
+    foreach $cookie (@cookies) {
+        ($name, $value) = split(/=/, $cookie);
+        $cookies{$name} = $value;
+    }
+}
+
+1;

diff --git a/cgi-bin/data.lib b/cgi-bin/data.lib
line changes: +37/-0
index 0000000..2e7ca12
--- /dev/null
+++ b/cgi-bin/data.lib
@@ -0,0 +1,37 @@
+$datadir = "/home/friendnet/data";
+
+sub make_safe {
+    $_ = shift;
+    s/([^\x20-\x7e])/sprintf("%%%02X", $1)/eg;
+    $_;
+}
+
+sub unmake_safe {
+    $_ = shift;
+    s/%([A-F0-9]{2})/chr(hex(2))/eg;
+    $_;
+}
+
+sub profile_load {
+    ($username) = @_;
+
+    %profile = ();
+    open PROFILE, "$datadir/profiles/$username";
+    while (<PROFILE>) {
+        chomp;
+        ($key, $value) = split(/\s*=\s*/);
+        $profile{$key} = unmake_safe($value);
+    }
+
+    return %profile;
+}
+
+sub profile_save {
+    ($username, %profile) = @_;
+
+    open PROFILE, ">$datadir/profiles/$username";
+    while (($key, $value) = each %profile) {
+        print PROFILE "$key = ", make_safe($value), "\n";
+    }
+    close PROFILE;
+}

diff --git a/cgi-bin/form.lib b/cgi-bin/form.lib
line changes: +29/-0
index 0000000..641a9f8
--- /dev/null
+++ b/cgi-bin/form.lib
@@ -0,0 +1,29 @@
+%formdata = ();
+
+# CGI parsing routine taken from "Perl and CGI for the World Wide Web" by
+# Elizabeth Castro
+if ($ENV{'REQUEST_METHOD'} eq 'GET') {
+    @pairs = split(/&/, $ENV{'QUERY_STRING'});
+} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
+    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
+    @pairs = split(/&/, $buffer);
+}
+
+foreach $pair (@pairs) {
+    ($key, $value) = split(/=/, $pair);
+    $key =~ tr/+/ /;
+    $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
+    $value =~ tr/+/ /;
+    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
+
+    # I don't even understand this -cb
+    $value =~ s/<!--(.|\n)*-->//g;
+
+    if ($formdata{$key}) {
+        $formdata{$key} .= ", $value";
+    } else {
+        $formdata{$key} = $value;
+    }
+}
+
+1;

diff --git a/cgi-bin/html.lib b/cgi-bin/html.lib
line changes: +18/-0
index 0000000..f1cb9aa
--- /dev/null
+++ b/cgi-bin/html.lib
@@ -0,0 +1,18 @@
+sub html_head {
+    print <<EOD
+<html>
+<head>
+<title>FriendNet! - A place for friends</title>
+</head>
+<body bgcolor=lightblue color=black>
+EOD
+}
+
+sub html_foot {
+    print <<EOD
+</body>
+</html>
+EOD
+}
+
+1;

diff --git a/cgi-bin/join.pl b/cgi-bin/join.pl
line changes: +72/-0
index 0000000..0e242c5
--- /dev/null
+++ b/cgi-bin/join.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+require "html.lib";
+require "form.lib";
+require "data.lib";
+
+print "Content-type: text/html\r\n\r\n";
+
+&html_head;
+
+if (%formdata > 0) {
+    if (!$formdata{username}) {
+        print "You need a username";
+    } elsif (!$formdata{password1}) {
+        print "You need to put in a password";
+    } elsif ($formdata{password1} ne $formdata{password2}) {
+        print "Your passwords don't match";
+    } else {
+        %profile = %formdata{username,dogs,pearljam,sex,college,photo};
+        $profile{password} = $formdata{password1};
+        &profile_save($formdata{username}, %profile);
+        print <<EOD
+<h2>Success!</h2>
+<p>Your user account has been created!
+<p>Please <a href=/cgi-bin/login.pl>login</a> now.
+EOD
+    }
+} else {
+    print <<EOD;
+<h2>Join FriendNet</h2>
+
+<form>
+<p>Username: <input name=username type=text size=8 maxlength=8>
+<p>Password: <input name=password1 type=password size=8 maxlength=8>
+<p>Password again: <input name=password2 type=password size=8 maxlength=8>
+<br><font size=-1>choose a secure password like "p4ssword1"</font>
+<p>Dogs: <input type=checkbox name=dogs value=yes>
+<p>Favorite Pearl Jam Album: <select name=pearljam>
+  <option>Ten</option>
+  <option>Vs.</option>
+  <option>Vitalogy</option>
+</select>
+<p>Sex: <select name=sex>
+  <option>Male</option>
+  <option>Female</option>
+  <option>male</option>
+</select>
+<p>College: <select name=college>
+  <option>None</option>
+  <option>Syracuse University</option>
+  <option>University of Iowa</option>
+  <option>University of California at Santa Barbara</option>
+  <option>West Virginia University</option>
+  <option>University of Illinois at Urbana-Champaign</option>
+  <option>Lehigh University</option>
+  <option>Pennsylvania State University at University Park</option>
+  <option>University of Wisconsin at Madison</option>
+  <option>Bucknell University</option>
+  <option>University of Florida</option>
+  <option>Miami University</option>
+  <option>Florida State University</option>
+  <option>Ohio University at Athens</option>
+  <option>DePauw University</option>
+  <option>University of Georgia</option>
+  <option>University of Mississippi</option>
+</select>
+<p>A photo of you: <input name=photo type=file>
+<p>Ready? <input type="image" src="/img/clickhere.gif"> to join!
+</form>
+EOD
+}
+
+&html_foot;

diff --git a/cgi-bin/login.pl b/cgi-bin/login.pl
line changes: +31/-0
index 0000000..cebdd45
--- /dev/null
+++ b/cgi-bin/login.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+require "html.lib";
+require "form.lib";
+require "data.lib";
+
+if (%formdata > 0 && $formdata{success} eq 'true') {
+    print "Set-Cookie: loggedin=$formdata{username}\r\n";
+    print "Location: /cgi-bin/profile.pl?u=$formdata{u}\r\n\r\n";
+} elsif (%formdata > 0) {
+    %profile = &profile_load($formdata{username});
+    if ($formdata{password} eq $profile{password}) {
+        print "Location: /cgi-bin/login.pl?u=$formdata{username}&success=true\r\n\r\n"
+    } else {
+        print "Content-type: text/html\r\n\r\n";
+        &html_head;
+        print "Authorization failed";
+        &html_foot;
+    }
+} else {
+    print "Content-type: text/html\r\n\r\n";
+
+    &html_head;
+
+    print <<EOD;
+<h1>Log in to FriendNet!</h1>
+<form>
+<p>Username: <input name=username type=text size=8 maxlength=8>
+<p>Password: <input name=password type=password size=8 maxlength=8>
+<p><input type=image src=/img/clickhere.gif> to log in!
+EOD
+}

diff --git a/cgi-bin/profile.pl b/cgi-bin/profile.pl
line changes: +39/-0
index 0000000..8651a98
--- /dev/null
+++ b/cgi-bin/profile.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+require "html.lib";
+require "form.lib";
+require "data.lib";
+require "cookies.lib";
+
+$logged_in = $cookies{loggedin} eq $formdata{u};
+
+print "Content-type: text/html\r\n\r\n";
+
+&html_head;
+
+%profile = profile_load($formdata{u});
+
+print <<EOD;
+<h1>FriendNet!</h1>
+<h2>$formdata{u}</h2>
+EOD
+
+if ($profile{dogs}) {
+    print <<EOD
+<style>
+body { background-image: url(/img/dog.gif) }
+</style>
+EOD
+}
+
+print <<EOD;
+<table>
+  <tr>
+    <td>Favorite Pearl Jam album:</td><td>$profile{pearljam}</td>
+  </tr>
+  <tr>
+    <td>Sex:</td><td>$profile{sex}</td>
+  </tr>
+</table>
+EOD
+
+&html_foot;

diff --git a/www/img/clickhere.gif b/www/img/clickhere.gif
line changes: +0/-0
index 0000000..b0d03d5
--- /dev/null
+++ b/www/img/clickhere.gif

diff --git a/www/img/construction.gif b/www/img/construction.gif
line changes: +0/-0
index 0000000..23b7d20
--- /dev/null
+++ b/www/img/construction.gif

diff --git a/www/img/dog.gif b/www/img/dog.gif
line changes: +0/-0
index 0000000..11b1d89
--- /dev/null
+++ b/www/img/dog.gif

diff --git a/www/index.html b/www/index.html
line changes: +16/-0
index 0000000..2ae2bdd
--- /dev/null
+++ b/www/index.html
@@ -0,0 +1,16 @@
+<html>
+<head>
+<title>FriendNet! - A place for friends</title>
+</head>
+<body bgcolor=lightblue color=black>
+
+<h1>Welcome to <font color=red>Friend</font><font color=blue>Net!</font></h1>
+
+<p><img src="img/construction.gif">Pardon our dust! We're under construction!<img src="img/construction.gif">
+
+<p><a href="/cgi-bin/join.pl">Join FriendNet!</a>
+
+<p><a href="/cgi-bin/login.pl">Log in!</a>
+
+</body>
+</html>