+if ($ENV{'HTTP_COOKIE'}) {
+ @cookies = split(/;/, $ENV{'HTTP_COOKIE'});
+ foreach $cookie (@cookies) {
+ ($name, $value) = split(/=/, $cookie);
+ $cookies{$name} = $value;
+ }
+}
+
+1;
+$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;
+}
+%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;
+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;
+#!/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;
+#!/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
+}
+#!/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;
+<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>