Приглашаем посетить
Русский язык (rus-yaz.niv.ru)

Registry

#!/usr/bin/perl

# Registery v. 2 updated January 1st 1997
# Written by Dave Palmer <dave@upstatepress.com>
# Unix Version. A Windows NT version will be available soon.
# http://www.upstatepress.com/dave/perl.shtml
#################################################################
# Registry is just another guestbook script. I wanted to write
# one just to see if I could write a decent guestbook (the
# jury is still out on that one!) and I just wanted something
# a little different for the upstatepress.com site. If people
# want to use this, that's fine. I don't expect anyone to use
# it, just because there are several hundred out there on the
# net that are 100 times better than this!
#################################################################

# Define some variables first
$guestbook = "/usr/home/upstate/public_html/registry/registry.html";
$mailprog = '/usr/sbin/sendmail';
$guestbook_url = "http://www.upstatepress.com/registry/registry.html";

# What is the URL of our guestbook program
$guestbookcgi = "/cgi-bin/registry.cgi";

# Personal information
$my_email = 'dave@upstatepress.com';
$my_name = 'Dave Palmer';

# Options

# Do you want to be notified each time someone signs the registry?
$notify = "yes";  # enter "no" if you do not want to be notified

# Do you want to send an automated e-mail thank you to the person
# who just signed your registry?
$thanks = "yes";  # enter "no" if you do not want to send an
                 # automated response

#############################################################


# Get input
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

	#split name/value pairs
	@pairs = split(/&/, $input);

		#decode input
		foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);

		$name =~ tr/+/ /;
		$name =~ 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;

	$FORM{$name} = $value;
	}


# Lets do some translating
$usrname = $FORM{'usrname'};
$email = $FORM{'email'};
$url = $FORM{'url'};
$city = $FORM{'city'};
$state = $FORM{'state'};
$comments = $FORM{'comments'};
$action = $FORM{'action'};

# Lets do some error checking
&missing_name unless $usrname;
&missing_email unless $email;
&missing_comments unless $comments;

# Lets remove any HTML stuff someone tried to add
$comments =~ s/<([^>]|\n)*>//g;

# Lets decide what we are going to be doing
if ($action eq "preview") { &preview; }
	else { &addentry; }

####################################
#          Sub Routines            #
####################################

sub preview {
# Lets create a preview for our stuff!
print "Content-type: text/html\n\n";
print "<html><head><title>Your Entry!</title></head>\n";
print "<body background=\"/noisebak.gif\"><font face=\"Arial, Geneva\">\n";
print "<blockquote><blockquote><blockquote>\n";
print "<p><font size=+3>Thanks $usrname!</font></p><hr size=1><br>\n";
print "<p>Below is what you entered. If everything is okay, click on the Submit button below.\n";
print "If you need to change something, click on your browser's \"back\" button</p>\n";
print "<hr size=1 width=75%><br>\n";
print "Your Name: <b>$usrname</b><br>\n";
print "Your E-mail: <b>$email</b><br>\n";
print "Your URL (if you entered one): <b>$url</b><br>\n";
print "Your City: <b>$city</b><br>\n";
print "Your State: <b>$state</b><br>\n";
print "Comments: <b>$comments</b><br>\n";
print "<hr size=1 width=75%><br>\n";
print "<form action=\"$guestbookcgi\" method=POST>\n";
print "<input type=hidden name=\"usrname\" value=\"$usrname\">\n";
print "<input type=hidden name=\"email\" value=\"$email\">\n";
print "<input type=hidden name=\"url\" value=\"$url\">\n";
print "<input type=hidden name=\"city\" value=\"$city\">\n";
print "<input type=hidden name=\"state\" value=\"$state\">\n";
print "<input type=hidden name=\"comments\" value=\"$comments\">\n";
print "<input type=hidden name=\"action\" value=\"addentry\">\n";
print "<input type=submit name=\"submit\" value=\"submit\">\n";
print "</form>\n";
print "</blockquote></blockquote></blockquote>\n";
print "</body></html>\n";

     }

sub addentry {
# Lets send the user a thank you and then print the location 
# of our registry
print "Content-type: text/html\n\n";
print "<html><head><title>Thank You!!!</title></head>\n";
print "<body background=\"/noisebak.gif\"><font face=\"Arial, Geneva\">\n";
print "<blockquote><blockquote><blockquote>\n";
print "<p><font size=+3>Thanks $usrname!</font></p>\n";
print "<hr size=1><br>\n";
print "<p>Thanks $usrname, for signing our Registry!</p>\n";
print "<p><a href=\"$guestbook_url\">Click here to see your entry</a></p>\n";
print "</blockquote></blockquote></blockquote>\n";
print "</body></html>\n";

# Lets suck our registry file into an array
open(FILE, "$guestbook") || die "I can't\n";
	@lines = <FILE>;
	close(FILE);
	$sizelines = @lines;

# Now, re-open the links file, and add the new link
open(FILE, ">$guestbook") || die "I can't open: $guestbook\n";
	
		for ($a = 0; $a <= $sizelines; $a++) {
	
		$_ = $lines[$a];

 	if (/<!--begin-->/) {
	
	print FILE "<!--begin-->\n";
	print FILE "<font size=-1>\n";
	print FILE "<a href=\"mailto:$email\">$usrname</a><br>\n";
	
	if ($city && $state) {
	print FILE "$city , $state<br>\n";
		}

	print FILE "$comments<br>\n";
	
	if ($url) {
	print FILE "Homepage: <a href=\"$url\">$url</a><br>\n";
		}
	
	print FILE "<hr size=1><br>\n";

		} else {
			print FILE $_;
		}
	}
close(FILE);

		# Lets send our e-mail
		if ($notify eq "yes") { 
		&sendmail;
         		}	

		# Lets send our user a thank you
		if ($thanks eq "yes") {
			&thank_you;
		}

}

sub sendmail {
open(MAIL, "| $mailprog -t") || die "I can't open $mailprog\n";
	print MAIL "To: $my_name <$my_email>\n";
	print MAIL "From: $usrname <$email>\n";
	print MAIL "Subject: A new entry to the Registry\n";
	print MAIL "$usrname added a new entry to your Registry\n";
	print MAIL "Here's what they entered:\n";
	print MAIL "$comments\n";
close(MAIL);
	}

sub thank_you {
open(MAIL, "| $mailprog -t") || die "I can't open that\n";
	print MAIL "To: $usrname <$email>\n";
	print MAIL "From: $my_name <$my_email>\n";
	print MAIL "Subject: Thanks!!\n";
	print MAIL "Thanks $usrname for signing our Registry\n";
	print MAIL "Stop by again sometime!\n";
	print MAIL "$guestbook_url\n";
close(MAIL);
		}            

sub missing_name {
	print "Content-type: text/html\n\n";
	print "<html><head><title>You Forgot Your Name!</title></head>\n";
	print "<body><font face=\"Arial, Geneva\">\n";
	print "<blockquote><blockquote><blockquote>\n";
	print "<p><font size=+3>You Forgot Your Name!</font></p>\n";
	print "<p>You forgot your name. Please enter your name in the space\n";
	print "provided below!</p>\n";
	print "<form action=\"$guestbookcgi\" method=POST>\n";
	print "<p>Name: <input type=text name=\"usrname\"></p>\n";	
	print "<input type=submit name=\"submit\" value=\"submit\">\n";
	print "<input type=hidden name=\"email\" value=\"$email\">\n";
	print "<input type=hidden name=\"url\" value=\"$url\">\n";
	print "<input type=hidden name=\"city\" value=\"$city\">\n";
	print "<input type=hidden name=\"state\" value=\"$state\">\n";
	print "<input type=hidden name=\"comments\" value=\"$comments\">\n";
	print "<input type=hidden name=\"action\" value=\"$action\">\n";
	print "</form>\n";
	print "</blockquote></blockquote></blockquote>\n";
	print "</body></html>\n";
	exit;
		}

sub missing_email {
	print "Content-type: text/html\n\n";
	print "<html><head><title>You Forgot Your E-mail!</title></head>\n";
	print "<body><font face=\"Arial, Geneva\">\n";
	print "<blockquote><blockquote><blockquote>\n";
	print "<p><font size=+3>You Forgot Your E-mail!</font></p>\n";
	print "<p>You forgot your to enter your e-mail. Please enter your it in the space\n";
	print "provided below!</p>\n";
	print "<form action=\"$guestbookcgi\" method=POST>\n";
	print "<p>E-mail: <input type=text name=\"email\"></p>\n";	
	print "<input type=submit name=\"submit\" value=\"submit\">\n";
	print "<input type=hidden name=\"usrname\" value=\"$usrname\">\n";
	print "<input type=hidden name=\"url\" value=\"$url\">\n";
	print "<input type=hidden name=\"city\" value=\"$city\">\n";
	print "<input type=hidden name=\"state\" value=\"$state\">\n";
	print "<input type=hidden name=\"comments\" value=\"$comments\">\n";
	print "<input type=hidden name=\"action\" value=\"$action\">\n";
	print "</form>\n";
	print "</blockquote></blockquote></blockquote>\n";
	print "</body></html>\n";
	exit;
		}

sub missing_comments {
	print "Content-type: text/html\n\n";
	print "<html><head><title>You Forgot the Comments!</title></head>\n";
	print "<body><font face=\"Arial, Geneva\">\n";
	print "<blockquote><blockquote><blockquote>\n";
	print "<p><font size=+3>You Forgot to Entery Your Comments!</font></p>\n";
	print "<p>You forgot to enter some comments. Please enter some in the space provided\n";
	print "provided below!</p>\n";
	print "<form action=\"$guestbookcgi\" method=POST>\n";
	print "<p>comments:<br> <textarea cols=40 rows=4 name=\"comments\"></textarea></p>\n";	
	print "<input type=submit name=\"submit\" value=\"submit\">\n";
	print "<input type=hidden name=\"email\" value=\"$email\">\n";
	print "<input type=hidden name=\"url\" value=\"$url\">\n";
	print "<input type=hidden name=\"city\" value=\"$city\">\n";
	print "<input type=hidden name=\"state\" value=\"$state\">\n";
	print "<input type=hidden name=\"usrname\" value=\"$usrname\">\n";
	print "<input type=hidden name=\"action\" value=\"$action\">\n";
	print "</form>\n";
	print "</blockquote></blockquote></blockquote>\n";
	print "</body></html>\n";
	exit;
		}