#!/local/bin/perl
#
#        If you have ANY problems or questions on these scripts please
#contact me before making public use of them.  If you would like to see this
#script in use, start from my homepage: http://www.cs.uoregon.edu/~jhobbs/
#
##include "standard_disclaimer.h"
#
#     Jeffrey Hobbs                             Office: 503/346-3998
#     Univ of Oregon CIS GTF                    email: jhobbs@cs.uoregon.edu
#                  URL: http://www.cs.uoregon.edu/~jhobbs/
#
#PS: Remember to change the following lines in both scripts:
#* path to Perl at the top of each
#* wwwroot variable (to your web address)
#* wwwserverroot variable (to the home directory of your W3 server)
#* domain (to your domain address)
#* parse down the countryid variable to something reasonable.
#* you might want to adjust how the 'root' guestbook is stored.
#
##########################
## guestbook
##########################
## This first script is called by the user with the appropriate Q_STRING
## that includes their login name, guest page path (relative to their
## local public_html directory), and desired return page.
## It creates the forms page necessary for input to the addguest script,
## which actually adds the guest to the noted guest page.  Note that the
## guest page of a user must be in their own public_html directory and
## be readable/writeable by whatever group/user the http server is run as
## (this generally means world readable/writeable).
## The first thing you probably should change in these scripts is the
## path to the PERL executable.
##########################
#
# QUERY_STRING should be of the form:
# http://domain/scriptname?owner?guestpage?returnpage
#
# This gets the necessary info from the QUERY_STRING
($owner, $guestpage, $returnpage) = split('\?', $ENV{QUERY_STRING});

die "Lack of info sent to script, stopped" unless ($owner && $guestpage);

# W3 root addresses (http version & server version) no trailing slashes
$wwwroot       = "http://www.cs.uoregon.edu";
# This is the DocumentRoot of the W3 server, for root guestbooks
$wwwserverroot = "/cs/www";
# domain name for addressing
$domain = "\@cs.uoregon.edu";

%countryid = ('edu', 'USA', 'org', 'USA', 'com', 'USA', 'net', 'USA',
              'gov', 'USA', 'mil', 'USA', 'us', 'USA', 'um', 'USA',
              'af', 'Afghanistan',            'al', 'Albania',
              'dz', 'Algeria',                'as', 'American Samoa',
              'ad', 'Andorra',                'ao', 'Angola',
              'ai', 'Anguilla',               'aq', 'Antarctica',
              'ag', 'Antigua and Barbuda',    'ar', 'Argentina',
              'am', 'Armenia',                'aw', 'Aruba',
              'au', 'Australia',              'at', 'Austria',
              'az', 'Azerbaijan',             'bs', 'Bahamas',
              'bh', 'Bahrain',                'bd', 'Bangladesh',
              'bb', 'Barbados',               'by', 'Belarus',
              'be', 'Belgium',                'bz', 'Belize',
              'bj', 'Benin',                  'bm', 'Bermuda',
              'bt', 'Bhutan',                 'bo', 'Bolivia',
              'ba', 'Bosnia and Herzegowina', 'bw', 'Botswana',
              'bv', 'Bouvet Island',          'br', 'Brazil',
              'io', 'British Indian Ocean Territory',
              'bn', 'Brunei Darussalam',      'bg', 'Bulgaria',
              'bf', 'Burkina Faso',           'bi', 'Burundi',
              'kh', 'Cambodia',               'cm', 'Cameroon',
              'ca', 'Canada',                 'cv', 'Cape Verde',
              'ky', 'Cayman Islands',         'cf', 'Central African Republic',
              'td', 'Chad',                   'cl', 'Chile',
              'cn', 'China',                  'cx', 'Christmas Island',
              'cc', 'Cocos (Keeling) Islands',
              'co', 'Colombia',               'km', 'Comoros',
              'cg', 'Congo',                  'ck', 'Cook Islands',
              'cr', 'Costa Rica',             'ci', 'Cote d\'Ivoire',
              'hr', 'Croatia (Hrvatska)',     'cu', 'Cuba',
              'cy', 'Cyprus',                 'cz', 'Czech Republic',
              'dk', 'Denmark',                'dj', 'Djibouti',
              'dm', 'Dominica',               'do', 'Dominican Republic',
              'tp', 'East Timor',             'ec', 'Ecuador',
              'eg', 'Egypt',                  'sv', 'El Salvador',
              'gq', 'Equatorial Guinea',      'er', 'Eritrea',
              'ee', 'Estonia',                'et', 'Ethiopia',
              'fk', 'Falkland Islands',       'fo', 'Faroe Islands',
              'fj', 'Fiji',                   'fi', 'Finland',
              'fr', 'France',                 'fx', 'France,',
              'gf', 'French Guiana',          'pf', 'French Polynesia',
              'tf', 'French Southern Territories',
              'ga', 'Gabon',                  'gm', 'Gambia',
              'ge', 'Georgia',                'de', 'Germany',
              'gh', 'Ghana',                  'gi', 'Gibraltar',
              'gr', 'Greece',                 'gl', 'Greenland',
              'gd', 'Grenada',                'gp', 'Guadeloupe',
              'gu', 'Guam',                   'gt', 'Guatemala',
              'gn', 'Guinea',                 'gw', 'Guinea-Bissau',
              'gy', 'Guyana',                 'ht', 'Haiti',
              'hm', 'Heard and McDonald Islands',
              'hn', 'Honduras',               'hk', 'Hong Kong',
              'hu', 'Hungary',                'is', 'Iceland',
              'in', 'India',                  'id', 'Indonesia',
              'ir', 'Iran',                   'iq', 'Iraq',
              'ie', 'Ireland',                'il', 'Israel',
              'it', 'Italy',                  'jm', 'Jamaica',
              'jp', 'Japan',                  'jo', 'Jordan',
              'kz', 'Kazakhstan',             'ke', 'Kenya',
              'ki', 'Kiribati',
              'kp', 'Democratic People\'s Republic of Korea',
              'kr', 'Republic of Korea',      'kw', 'Kuwait',
              'kg', 'Kyrgyzstan',             'la', 'Lao Democratic Republic',
              'lv', 'Latvia',                 'lb', 'Lebanon',
              'ls', 'Lesotho',                'lr', 'Liberia',
              'ly', 'Libya',                  'li', 'Liechtenstein',
              'lt', 'Lithuania',              'lu', 'Luxembourg',
              'mo', 'Macau',                  'mk', 'Macedonia,',
              'mg', 'Madagascar',             'mw', 'Malawi',
              'my', 'Malaysia',               'mv', 'Maldives',
              'ml', 'Mali',                   'mt', 'Malta',
              'mh', 'Marshall Islands',       'mq', 'Martinique',
              'mr', 'Mauritania',             'mu', 'Mauritius',
              'yt', 'Mayotte',                'mx', 'Mexico',
              'fm', 'Micronesia',             'md', 'Moldova',
              'mc', 'Monaco',                 'mn', 'Mongolia',
              'ms', 'Montserrat',             'ma', 'Morocco',
              'mz', 'Mozambique',             'mm', 'Myanmar',
              'na', 'Namibia',                'nr', 'Nauru',
              'np', 'Nepal',                  'nl', 'Netherlands',
              'an', 'Netherlands Antilles',   'nc', 'New Caledonia',
              'nz', 'New Zealand',            'ni', 'Nicaragua',
              'ne', 'Niger',                  'ng', 'Nigeria',
              'nu', 'Niue',                   'nf', 'Norfolk Island',
              'mp', 'Northern Mariana Islands',
              'no', 'Norway',                 'om', 'Oman',
              'pk', 'Pakistan',               'pw', 'Palau',
              'pa', 'Panama',                 'pg', 'Papua New Guinea',
              'py', 'Paraguay',               'pe', 'Peru',
              'ph', 'Philippines',            'pn', 'Pitcairn',
              'pl', 'Poland',                 'pt', 'Portugal',
              'pr', 'Puerto Rico',            'qa', 'Qatar',
              're', 'Reunion',                'ro', 'Romania',
              'ru', 'Russian Federation',     'rw', 'Rwanda',
              'kn', 'Saint Kitts and Nevis',  'lc', 'Saint Lucia',
              'vc', 'Saint Vincent and the Grenadines',
              'ws', 'Samoa',                  'sm', 'San Marino',
              'st', 'Sao Tome and Principe',  'sa', 'Saudi Arabia',
              'sn', 'Senegal',                'sc', 'Seychelles',
              'sl', 'Sierra Leone',           'sg', 'Singapore',
              'sk', 'Slovakia',               'si', 'Slovenia',
              'sb', 'Solomon Islands',        'so', 'Somalia',
              'za', 'South Africa',           'gs', 'South Georgia',
              'es', 'Spain',                  'lk', 'Sri Lanka',
              'sh', 'St. Helena',             'pm', 'St. Pierre and Miquelon',
              'sd', 'Sudan',                  'sr', 'Suriname',
              'sj', 'Svalbard and Jan Mayen Islands',
              'sz', 'Swaziland',              'se', 'Sweden',
              'ch', 'Switzerland',            'sy', 'Syria',
              'tw', 'Taiwan',                 'tj', 'Tajikistan',
              'tz', 'Tanzania',               'th', 'Thailand',
              'tg', 'Togo',                   'tk', 'Tokelau',
              'to', 'Tonga',                  'tt', 'Trinidad and Tobago',
              'tn', 'Tunisia',                'tr', 'Turkey',
              'tm', 'Turkmenistan',           'tc', 'Turks and Caicos Islands',
              'tv', 'Tuvalu',                 'ug', 'Uganda',
              'ua', 'Ukraine',                'ae', 'United Arab Emirates',
              'gb', 'United Kingdom',         'uk', 'United Kingdom',
              'uy', 'Uruguay',                'uz', 'Uzbekistan',
              'vu', 'Vanuatu',                'va', 'Vatican City',
              've', 'Venezuela',              'vn', 'Viet Name',
              'vg', 'British Virgin Islands', 'vi', 'US Virgin Islands',
              'wf', 'Wallis and Futina Islands',
              'eh', 'Western Sahara',         'ye', 'Yemen',
              'yu', 'Yugoslavia',             'zr', 'Zaire',
              'zm', 'Zambia',                 'zw', 'Zimbabwe',
              );

if ($owner ne "root") {
    @val = getpwnam($owner);
    $dir = @val[7];
    if (!$dir) { die "$owner is not a valid user, stopped"; }
    $returnpage = "$wwwroot/~$owner/$returnpage";
    $htmlpage   = "$wwwroot/~$owner/$guestpage";
    $guestpage  = "$dir/public_html/$guestpage";
    $address    = "$owner$domain";
}
else {
    $dir = $wwwserverroot;
    $returnpage = "$wwwroot/$returnpage";
    $htmlpage   = "$wwwroot/$guestpage";
    $guestpage  = "$dir/$guestpage";
    $address    = "webmaster$domain";
}

@tmp = split(/\./, $ENV{REMOTE_HOST});
$country = @tmp[$#tmp];
$country =~ tr/A-Z/a-z/;
$country = $countryid{$country};

print <<GUESTPAGE;
Content-type: text/html


<HTML>
<HEAD><TITLE>$owner\'s Guest Book Registry</TITLE></HEAD>

<BODY>
<H2>Welcome to the guest book registry.</H2>

All the information below is optional, except for your name :^).  Please
note that all names registered in the guest book are viewable by anyone.
You can alternatively just <A HREF="mailto:$owner$domain">send mail</A>.
<P>
<B>Thanks for stopping by!</B>

<HR>
<FORM METHOD="POST" ACTION="/htbin/addguest?$ENV{QUERY_STRING}">
<PRE>
      Name: <INPUT TYPE="text" SIZE=44 NAME="name" VALUE="$ENV{REMOTE_IDENT}">
City/State: <INPUT TYPE="text" SIZE=24 NAME="from"> Country: <INPUT TYPE="text"
SIZE=8 NAME="country" VALUE="$country">
     Email: <INPUT TYPE="text" SIZE=44 NAME="email"
VALUE="$ENV{REMOTE_IDENT}\@$ENV{REMOTE_HOST}">

Home/Favorite HTML Page (form: http://host.domain/page):
<INPUT TYPE="text" SIZE=56 NAME="html" value="http://">
 Page name: <INPUT TYPE="text" SIZE=44 NAME="pname">

Leave a note for all to see (will be shown in &lt;PRE&gt; tags):
<TEXTAREA NAME="note" ROWS=3 COLS=55></TEXTAREA>

Leave a private note (this is only seen by $owner):
<TEXTAREA NAME="comment" ROWS=3 COLS=55></TEXTAREA>
            <INPUT TYPE="submit" VALUE="Sign Guestbook">           <INPUT
TYPE="reset" VALUE="Clear Form">
</PRE>
</FORM>

<HR>
<CENTER><A HREF="$htmlpage"><B>View the guest book</B></A></CENTER>
<HR>
The scripts for this guest book are written in Perl and available upon request
from <A HREF="http://www.cs.uoregon.edu/~jhobbs/">jhobbs\@cs.uoregon.edu</A>.
<P><ADDRESS>$address</ADDRESS>
</BODY>
</HTML>

GUESTPAGE

exit;
----------------------END MAIN GUESTBOOK SCRIPT-----------------------------

------------------------START ADDGUEST SCRIPT-------------------------------
#!/local/bin/perl
#
###############################
## addguest
################################
## This script, which I denoted "addguest", is called by submitting an entry
## from the guestbook page.  This script mails the owner that a person has
## signed the guest book, unless no name is specified, in which case an
## error page is shown to the guest.  Otherwise the guest gets a thank you
## page with a pointer to his name in the guestbook, the guestbook itself,
## and the return page specified.
################################
#
# keys: name, from, email, html, pname, note, comment

# QUERY_STRING should be of the form:
# http://domain/scriptname?owner?guestpage?returnpage

# W3 root addresses (http version & server version) no trailing slashes
$wwwroot       = "http://www.cs.uoregon.edu";
# This is the DocumentRoot of the W3 server, for root guestbooks
$wwwserverroot = "/cs/www";
# domain name for addressing
$domain = "\@cs.uoregon.edu";

# mail program that is opened with a pipe
$mailprog   = "/usr/lib/sendmail -t -oi -oem";

# This gets the necessary info from the QUERY_STRING
($owner, $guestpage, $returnpage) = split('\?', $ENV{QUERY_STRING});

die "Lack of information sent to script, stopped" unless ($owner && $guestpage);

if ($owner ne "root") {
    @val = getpwnam($owner);
    $dir = @val[7];
    if (!$dir) { die "$owner is not a valid user, stopped"; }
    $returnpage = "$wwwroot/~$owner/$returnpage";
    $htmlpage   = "$wwwroot/~$owner/$guestpage";
    $guestpage  = "$dir/public_html/$guestpage";
    $address    = "$owner$domain";
}
else {
    $dir = $wwwserverroot;
    $returnpage = "$wwwroot/$returnpage";
    $htmlpage   = "$wwwroot/$guestpage";
    $guestpage  = "$dir/$guestpage";
    $address    = "webmaster$domain";
}

$date = `date`;
chop $date;

# Get info from form and parse it
read(STDIN, $raw_data, $ENV{CONTENT_LENGTH});

$items = split('&', $raw_data);
for ($i = 0; $i < $items; $i++) {
  ($key,$value) = split('=', $_[$i]);
  $value =~ tr/+/ /;
  $value =~ s/%(..)/pack("C", hex($1))/eg;
  $data{"$key"} = $value;
}
$rname = $data{name};
$rname =~ tr/ /+/;
$rname =~ tr/[A-Z]/[a-z]/;

if ($data{name}) {
if (!$data{pname}) { $data{pname} = $data{html}; }

open(GUESTS, ">> $guestpage") || die "Can't open guest book: $!\n";
print GUESTS "<HR>\n";
print GUESTS "<A NAME=\"$rname\"><B>$data{name}</B></A><BR>\n";
print GUESTS "$data{from}\t" if $data{from};
print GUESTS "$data{country}" if $data{country};
print GUESTS "<BR>\n";
print GUESTS "<I>$data{email}</I><BR>\n"           if $data{email};
if ($data{html} ne "http://") {
        print GUESTS "<A HREF=\"$data{html}\">$data{pname}</A><BR>\n";
}
print GUESTS "<PRE>$data{note}</PRE>\n"         if $data{note};
print GUESTS "Signed on: $date<BR>\n\n";
close(GUESTS);

print <<NEWPAGE;
Content-type: text/html

<HTML>
<HEAD><TITLE>Thank You</TITLE></HEAD>
<BODY>
<H3>Thank you $data{name}.</H3>

Feel free to view <A HREF="$htmlpage#$rname">your entry</A> in the
<A HREF="$htmlpage">guest book</A> or <A HREF="$returnpage">return to
$returnpage</A>. <P>

<HR>
<ADDRESS>$address</A></ADDRESS></BODY>
</HTML>

NEWPAGE

open(MAIL, "| $mailprog") || die "Can't open mailprog $mailprog, stopped";
print MAIL <<"STOP";
To: $owner
From: $data{email}
Subject: $data{name} signed guest book

\tName:\t$data{name}
\tFrom:\t$data{from}, $data{country}
\tHREF:\t$data{html}, $data{pname}
\tEmail:\t$data{email}

\tNote:
$data{note}

\tComment:
$data{comment}
STOP
close(MAIL) || die "pipe exited $?";
}
else {
    print <<NEWPAGE;
Content-type: text/html

<HTML>
<HEAD><TITLE>Oops...</TITLE></HEAD>
<BODY>
<H3>Oops...</H3>

You did not specify your name.  If you would like to submit
an entry, please specify your name. <P>
Thank you. <P>

<HR>
<ADDRESS>$address</A></ADDRESS></BODY>
</HTML>

NEWPAGE
}
exit;
---------------------------END ADDGUEST SCRIPT----------------------------
