#!/usr/npac/bin/perl

# HomePageMaker v.1.6 written by Dave Palmer <dave@upstatepress.com>
# http://www.upstatepress.com/dave/perl.shtml
# This will give your visitors the opportunity to set up a home page on your site.
# Its pretty basic stuff. It gives them a choice of three different backgrounds,
# they can have links, content, set the headline, and sub heads of the page, and
# place images (located on other servers). 
# This is completely secure. At no time do your visitors have access to server
# resources, or the shell.
# This code can be easily configured to work with Win servers as well.
#####################################################################################

# These variables need to be set

# Set this to your base HTML directory. This is a PATH not a URL
$base_dir = "/npac/server/foxsden00/sroot/droot/psf/f/admin/";

# This is your URL of where the new HTML pages will be kept
$baseurl = "http://www.foxsden.org/psf/f/admin/userpages/";

# This is the path for user pages. You don't really need to change this
# Just make sure to create a directory: userpages and chmod it 777
$page_dir = "$base_dir/userpages/";

# This is the index of all pages created by HPM
# This file should be chmod to 777 and placed in the userpages directory
$indexpage = "$page_dir/index.html";

# This is the location of the data.txt file. This holds each user's
# login name and e-mail address for confirmation
$data = "$page_dir/data.txt";

# This is a URL for the images sub directory in the userpages directory.
# Create the images directory in the userpages directory. This is where
# you will upload your background images
$image_dir = "/userpages/images";

# Location of the sendmail program
$sendmail = '/bin/mail';

# Your e-mail address here
$myemail = 'psf@npac.syr.edu';

# That's it.


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

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

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

	# split the name/value pairs
	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;
	$value =~ s/<([^>]|\n)*>//g;

  $FORM{$name} = $value;
    }

# Lets do some translating first
$usrname = $FORM{'usrname'};
$email = $FORM{'email'};
$background = $FORM{'background'};
$linkc = $FORM{'linkc'};
$vlinkc = $FORM{'vlinkc'};
$headline = $FORM{'headline'};
$subhead = $FORM{'subhead'};
$usrlink1 = $FORM{'usrlink1'};
$usrlink2 = $FORM{'usrlink2'};
$usrlink3 = $FORM{'usrlink3'};
$linkname1 = $FORM{'linkname1'};
$linkname2 = $FORM{'linkname2'};
$linkname3 = $FORM{'linkname3'};
$content = $FORM{'content'};
$images = $FORM{'images'};
$pagename = $FORM{'pagename'};
$login = $FORM{'login'};

# Add the line breaks for paragraph spacing
$content =~ s/&&/<br><br>/g;

# This fixes the bug of white space and
# other wierd spacing:
$content =~ s/\cM//g;
$content =~ s/\n/  /g;

# If the user tries to add more than one word in
# the page name field, this will put an underscore
# in the spaces to make it one word
$pagename =~ s/ /_/g;

# Find out what the user wants to do
if ($FORM{'action'} eq "Create Page") {
	&create;
	}
if ($FORM{'action'} eq "Edit Page") {
	&confirm;
		}
if ($FORM{'action'} eq "checkuser") {
	&checkuser;
	}

if ($FORM{'action'} eq "recreate") {
	&recreate;
	}

sub create {

# Now, lets do some error checking. Making sure they filled out each field
# This is pretty low tech now. I'll improve it later
&missing(missing_name) unless $usrname;
&missing(missing_email) unless $email;
&missing(missing_pagename) unless $pagename;

# if they try to name their page "index" This will stop them
if ($pagename eq "index") {
	print "Content-type: text/html\n\n";
	print "<html><head><title>Error</title></head>\n";
	print "<body><p>You cannot name your page <b>index</b>\n";
	print "Please go back and re-name your page</p>\n";
	exit;
		}
# if the user tries to name their page 
# something that is already taken
# this will HOPEFULLY stop them :)
# This block was written by Norm
if (-e "$page_dir$pagename\.html") {
        print "Content-type: text/html\n\n";
        print "<html><head><title>Error</title></head>\n";
        print "<body><p>The page name: <b>$pagename</b>\n";
        print "is already taken.\n";
        print "Please go back and re-name your page</p>\n";
        exit;
                }

#now, lets create our new html page
open(HTML, ">$page_dir$pagename\.html") || die "I can't create that file\n";
print HTML "<html><head><title>Welcome!</title></head>\n";
print HTML "<body background=\"$image_dir/\n";

	if ($background eq "back1") {
	print HTML "back1.gif\"\n";
		}
	if ($background eq "back2") {
	print HTML "back2.gif\"\n";
	}
	if ($background eq "back3") {
	print HTML "back3.gif\"\n";
		}

print HTML "link=$linkc vlink=$vlinkc><font face=\"Arial\">\n";
print HTML "<blockquote><blockquote><blockquote>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=+7>$headline</font></p>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=+2><b>$subhead</b></font>\n";
print HTML "<hr noshade width=70% size=1>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=-1>$content</font></p>\n";
print HTML "<p><i><font face=\"Arial, Geneva\" size=-1><b>Other Places to go:</i></b></font></p>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=-1><a href=\"$usrlink1\">$linkname1</a><br>\n";
print HTML "<a href=\"$usrlink2\">$linkname2</a><br>\n";
print HTML "<a href=\"$usrlink3\">$linkname3</a></font></p>\n";
print HTML "<hr noshade width=70% size=1>\n";
print HTML "<p><i><font face=\"Arial, Geneva\" size=-1>I can be reached at:</i><br>\n";
print HTML "<a href=\"mailto:$email\">$usrname <i>$email</i></a></font></p>\n";
print HTML "</body></html>\n";
close(HTML);


# Write all of the input into a flat file.
open(FILE, ">$page_dir$pagename\.dat") || die "I can't create that file\n";
print FILE "$usrname&&$email&&$background&&$headline&&$subhead&&$content&&$linkc&&$vlinkc&&$usrlink1&&$usrlink2&&$usrlink3&&$linkname1&&$linkname2&&$linkname3&&$pagename\n";
close(FILE);
chmod 0777, '$page_dir$pagename.dat';

# Write the login name and email address to a separate file for confirmation
# when they want to edit their page
open (FILE, ">>$data") || die "I can't open $data\n";
print FILE "$login&&$email&&$pagename\n";
close(FILE);


# Suck the index page, and write the new entry to it
open(FILE, "$indexpage") || die "I can't open that file\n";
	@lines = <FILE>;
	close(FILE);
	$sizelines = @lines;

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

 	if (/<!--begin-->/) {
	
	print FILE "<!--begin-->\n";
	print FILE "<p><font face=\"Arial, Geneva\" size=-1><a href=\"$baseurl$pagename.html\">$pagename</a></p>\n";

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


# Give the user a response
print "Content-type: text/html\n\n";
print "<html><head><title>thanks</title></head>\n";
print "<body>\n";
print "Your page was created, and you will receive an e-mail confirming this!\n";
print "Your URL is: <a href=\"$baseurl$pagename\.html\">\n";
print "$baseurl$pagename\.html</a>\n";
print "Thanks for your parcipitation!\n";
print "</body></html>\n";
	

# Send the user an e-mail confirming their page
open (MAIL, "| $sendmail -t") || die "I can't open sendmail\n";
  print MAIL "To: $usrname <$email>\n";
  print MAIL "From: HomePage Maker <$myemail>\n";
  print MAIL "Subject: Your URL\n";
  print MAIL "Your page can be viewed at the below URL:\n";
  print MAIL "\n";
  print MAIL "$baseurl$pagename\.html\n";
  close (MAIL);

# Notify us when someone creates a page
open (MAIL, "| $sendmail -t") || die "I can't open sendmail\n";
  print MAIL "To: $myemail\n";
  print MAIL "From: HomePageMaker <$myemail>\n";
  print MAIL "Subject: New Page Report\n";
  print MAIL "$usrname created a new page:\n";
  print MAIL "\n";
  print MAIL "$baseurl$pagename\.html\n";
close(MAIL);

		}

sub recreate {

#now, lets create our new html page
open(HTML, ">$page_dir$pagename\.html") || die "I can't create that file\n";
print HTML "<html><head><title>Welcome!</title></head>\n";
print HTML "<body background=\"$image_dir/\n";

	if ($background eq "back1") {
	print HTML "back1.gif\"\n";
		}
	if ($background eq "back2") {
	print HTML "back2.gif\"\n";
	}
	if ($background eq "back3") {
	print HTML "back3.gif\"\n";
		}

print HTML "link=$linkc vlink=$vlinkc><font face=\"Arial\">\n";
print HTML "<blockquote><blockquote><blockquote>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=+7>$headline</font></p>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=+2><b>$subhead</b></font>\n";
print HTML "<hr noshade width=70% size=1>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=-1>$content</font></p>\n";
print HTML "<p><i><font face=\"Arial, Geneva\" size=-1><b>Other Places to go:</i></b></font></p>\n";
print HTML "<p><font face=\"Arial, Geneva\" size=-1><a href=\"$usrlink1\">$linkname1</a><br>\n";
print HTML "<a href=\"$usrlink2\">$linkname2</a><br>\n";
print HTML "<a href=\"$usrlink3\">$linkname3</a></font></p>\n";
print HTML "<hr noshade width=70% size=1>\n";
print HTML "<p><i><font face=\"Arial, Geneva\" size=-1>I can be reached at:</i><br>\n";
print HTML "<a href=\"mailto:$email\">$usrname <i>$email</i></a></font></p>\n";
print HTML "</body></html>\n";
close(HTML);


# Write all of the input into a flat file.
open(FILE, ">$page_dir$pagename\.dat") || die "I can't create that file\n";
print FILE "$usrname&&$email&&$background&&$headline&&$subhead&&$content&&$linkc&&$vlinkc&&$usrlink1&&$usrlink2&&$usrlink3&&$linkname1&&$linkname2&&$linkname3&&$pagename\n";
close(FILE);
chmod 0777, '$page_dir$pagename.dat';

# Send the user a notice that thier page has been re-done
open (MAIL, "| $sendmail -t") || die "I can't open sendmail\n";
  print MAIL "To: $usrname <$email>\n";
  print MAIL "From: HomePage Maker <$myemail>\n";
  print MAIL "Subject: Your Changes\n";
  print MAIL "Your revised page can be viewed at the below URL:\n";
  print MAIL "\n";
  print MAIL "$baseurl$pagename\.html\n";
  close (MAIL);

# Give the user a response
print "Content-type: text/html\n\n";
print "<html><head><title>thanks</title></head>\n";
print "<body>\n";
print "Your page has been revised, and you will receive an e-mail confirming this!\n";
print "Your URL is: <a href=\"$baseurl$pagename\.html\">\n";
print "$baseurl$pagename\.html</a>\n";
print "Thanks for your parcipitation!\n";
print "</body></html>\n";
		}


# Standard error message for any missing required fields
sub missing {
local ($missing) = @_;
print "Content-type: text/html\n\n";

	print "<HTML><HEAD><TITLE>You missed something</TITLE></HEAD>\n";
	print "<BODY>\n";
	print "You forgot to fill in one of the fields. Please go back and make\n";
	print "sure that all required fields are filled in! $missing\n";
	print "</BODY></HTML>\n";
	exit;
}

sub confirm {
print "Content-type: text/html\n\n";
print "<html><head><title>Confirmation</title></head>\n";
print "<body bgcolor=\"#ffffff\">\n";
print "<p></p>\n";
print "<p><h1>Please Enter your login name, e-mail and name of your file to continue</h1></p>\n";
print "Login name:<br>\n";
print "<form action=\"/cgi-bin/pamfox/homepage.pl\" method=POST>\n";
print "<input size=40 type=text name=\"login\"><br><br>\n";
print "E-mail:<br>\n";
print "<input size=40 type=text name=\"email\"><br><br>\n";
print "Name of your file:<br>\n";
print "<input type=text size=40 name=\"pagename\"><br><br>\n";
print "<input type=submit value=submit>\n";
print "<input type=hidden name=\"action\" value=\"checkuser\">\n";
print "</form></body></html>\n";
	}

sub checkuser {
open(FILE, "$data") || die "I can't open $data\n";	

	while(<FILE>) {
	chop;		
	@all = split(/\n/);

	foreach $line (@all) {
	($loginname, $loginemail, $loginpagename) = split(/&&/, $line);

	if($loginname eq "$login" && $loginemail eq "$email" && $loginpagename eq "$pagename") {
		$match = 1;
		&edit($loginpagename);
			   }
		}
	
      }
	
close(FILE);

		if (! $match) {
		&error;
	        	}

	}

sub edit {

	local ($editfile) = @_;
	
	local($fullname, $mail, $head, $sub, $body, $bgimage, $linkcolor, $vlinkcolor, 
		$ahref1, $ahref2, $ahref3, $ahrefname1, $ahref2name, $ahref3name,
			$filename);
	open(FILE, "$page_dir$editfile\.dat") || die "I can't open $editfile\n";

	while(<FILE>) {
	chop;
	@datafile = split(/\n/);

foreach $line (@datafile) {
($fullname, $mail, $bgimage, $head, $sub, $body, $linkcolor, $vlinkcolor, $ahref1, $ahref2, $ahref3, $ahrefname1, $ahref2name, $ahref3name, $filename) = split(/&&/, $line);
            }
         }
	close(FILE);

# To avoid any security risks. Take out the HTML tags added when HPM translated
# the && to: <br><br>. They will be re-translated to: && Once the user updates
# the page, the: && will be put back to: <br><br>
$body =~ s/<br><br>/&&/g;


# print the edit-page form

print "Content-type: text/html\n\n";
print "<html><head><title>Edit Your Page</title></head>\n";
print "<body bgcolor=\"#ffffff\" link=navy vlink=maroon>\n";
print "<table width=250 cellspacing=0 cellpadding=0 border=0>\n";
print "<tr><td width=100% align=left valign=top>\n";
print "<p><font face=\"Arial, Geneva\" size=+2><b>Edit Your Page</b></font></p>\n";
print "<p><font face=\"Arial, Geneva\" size=-1>Below is a form with the contents\n";
print "of the home page you created. You can edit any part of your page</p>\n";
print "<p></p>\n";
print "<form action=\"/cgi-bin/pamfox/homepage.pl\" method=POST>\n";
print "<input type=hidden name=\"action\" value=\"recreate\">\n";
print "Your name:<br>\n";
print "<input type=text size=40 name=\"usrname\" value=\"$fullname\"><br><br>\n";
print "Your e-mail:<br>\n";
print "<input type=text size=40 name=\"email\" value=\"$mail\"><br><br>\n";
print "Headline:<br>\n";
print "<input type=text size=40 name=\"headline\" value=\"$head\"><br><br>\n";
print "Sub Head:<br>\n";
print "<input type=text size=40 name=\"subhead\" value=\"$sub\"><br><br>\n";
print "Name of HTML file: <b>$filename</b><br><br>\n";
print "<input type=hidden name=\"pagename\" value=\"$filename\">\n";
print "Body:<br>\n";
print "<textarea cols=40 rows=4 wrap=on name=\"content\">$body</textarea><br><br>\n";
print "</td></tr></table\n";
print "<table width=75% cellpadding=2 cellspacing=2 border=0>\n";
print "<tr><td width=50% align=left valign=top>\n";
print "Link 1 URL:<br>\n";
print "<input type=text size=40 name=\"usrlink1\" value=\"$ahref1\"><br><br>\n";
print "Link 2 URL:<br>\n";
print "<input type=text size=40 name=\"usrlink2\" value=\"$ahref2\"><br><br>\n";
print "Link 3 URL:<br>\n";
print "<input type=text size=40 name=\"usrlink3\" value=\"$ahref3\"><br><br>\n";
print "</td>\n";
print "<td width=50% align=left valign=top>\n";
print "Link 1 Name:<br>\n";
print "<input type=text size=40 name=\"linkname1\" value=\"$ahrefname1\"><br><br>\n";
print "Link 2 Name:<br>\n";
print "<input type=text size=40 name=\"linkname2\" value=\"$ahref2name\"><br><br>\n";
print "Link 3 Name:<br>\n";
print "<input type=text size=40 name=\"linkname3\" value=\"$ahref3name\"><br><br>\n";
print "</td></tr></table\n";
print "<table width=75% cellpadding=2 cellspacing=2>\n";
print "<tr><td width=100% align=left valign=top>\n";
print "Link color:<br>\n";
print "<input type=radio name=\"linkc\" value=\"Black\" checked>Black\n";
print "<input type=radio name=\"linkc\" value=\"Maroon\">Maroon\n";
print "<input type=radio name=\"linkc\" value=\"Blue\">Blue<br><br>\n";
print "Visited Link Color:<br>\n";
print "<input type=radio name=\"vlinkc\" value=\"Black\" checked>Black\n";
print "<input type=radio name=\"vlinkc\" value=\"Maroon\">Maroon\n";
print "<input type=radio name=\"vlinkc\" value=\"Purple\">Purple<br><br>\n";
print "Background image:<br>\n";
print "<input type=radio name=\"background\" value=\"back1\" checked>Image 1\n";
print "<input type=radio name=\"background\" value=\"back2\">Image 2\n";
print "<input type=radio name=\"background\" value=\"back3\">Image 3<br><br>\n";
print "<input type=submit value=\"update page\">\n";
print "</form>\n";
print "</tr></td></table></body></html>\n";
	}

sub error {
print "Content-type: text/html\n\n";
print "<html><head><title>Permission Denied</title></head>\n";
print "<body>\n";
print "<p><h1>Permission Denied</h1></p>\n";
print "You do not have permission to edit\n";
print "</body></html>\n";
exit;
	}



