#! /usr/bin/perl -w

use CGI qw(:standard);

###########################################
#change $dir to the directory in which you
#will store your temporary files
#you will have to create this directory
#and change it's permissions to 777
#you must manually move files from this tmp
#directory to the html directory
###########################################

#$dir = "/home/httpd/stuff";
#$dir = "/var/www/stuff";
$dir = ".";
###########################################
#     don't change anything else          #
###########################################

$html_page = param("page");
$file = $html_page;
$color = param("color");
$input = param("input");
$title = param("title");
$font = param("font_color");
$reprint = param("reprint");
$template = "<html><body><center><body bgcolor = \"$color\"><font color = $font>";
$template_end = ("</center></body></html>");
$script="/home/httpd/html/pop.html";

if (($input && $html_page) && (($html_page ne 'yourpage.html') && ($html_page =~ /.html$/))  ){

=pod

if($input =~ /[;<>\*\|`&\$!#\(\)\[\]\{\}'"]/ ||
	$html_page =~ /[;<>\*\|`&\$!#\(\)\[\]\{\}'"]/){
	&bad_input();
	exit;
}

=cut

my $taint_value = is_tainted($input);
if ($taint_value){
	&error();
	exit;
} 
print header(), start_html("html composer");
print ("<center>");
$i =0;

@hold=split(/\n/, $input);
foreach(@hold){
  $input[$i] = $_;
  $i++;
}
chdir "$dir" or die "cannot change dirs: $!";
        open (OUT, ">$file") || die "cannot open $file to write: $!";
        system("chmod", "0777", "$file");
        print OUT $template;
        print OUT ("<H3>");
        print OUT $title;
        print OUT ("</H3>");
        print OUT ("<BR><BR><BR>");
        foreach(@input){
                if(/.gif/ || /.jpg/ || /.jpeg/){
                   s/\s+//;
                   print OUT ("<img src = $_ height = 200 width = 100>");
                   print OUT "<BR>";
                }elsif(/print line/) {
                   s/\s+//;
                   print OUT ("<BR><HR><BR>");
                }elsif($_ =~ /.com/ ||$_ =~ /.org/ || $_ =~ /.net/){
		   print OUT ('<a href="http://');
		   print OUT $_."\"";
                   print OUT ">$_</a><BR>";	
                }else {
                  print OUT $_."<BR>";
                }
        }
        print OUT $template_end;
close OUT;

open IN, "$file"  or die "can't open $file to read: $!";
        while(<IN>){
                print;

        }
close IN;
print ("</center>");
print end_html();
}

elsif(($input && !$html_page) || (($input) && (!($html_page =~ /.html$/))) || (($input) && ($html_page eq 'yourpage.html'))){
print header(), start_html("html composer");
print ("<body bgcolor = \"#FFFCC0\">");
print ("<center><p><TABLE BORDER=3   CELLSPACING=0><TR><TD><H3><center>Please enter the name of<BR> your temporary html page.<BR>For example:<BR> yourpage.html<BR>(Substitute a unique name for \"yourpage\")<BR> To continue click on your browser's back button</center><H3></TD></TR></center></TABLE><p>");
print end_html();
}

 
elsif($reprint){
print header(), start_html(), start_form();
print  "<html>";
print '<title><H1>[Web Page Composer]</H1></title>';
print '<body bgcolor="#5c4033" text="#cccccc" link="cc9999" vlink="999999">';
#<!-- nested table fu@!@# -->
print "<center>";

#print '<TABLE  cellpadding="0" cellspacing="0" border="0">';
#print "<TR>";
#print '<TD bgcolor="#000000" valign="top">';
#<!-- this is the outer table -->


print '<table  cellpadding="0" cellspacing="0" border="0">';
print '<td  colspan="2" bgcolor="#660000" valign="top">';
print "<tr>";
print '<td nowrap valign="top" bgcolor="333399">';
print '<font face="verdana, tahoma, arial, helvetica" size="1">';
print "<center><B>Instructions<BR></B>";
print "1) Enter the html page name i.e.<BR> something_unique.html<BR>";
print "2) Just type your web page content <BR>in the big white textarea.<BR>";
print "3) To include an image, it must be in your<BR> server's icons or image directory<BR>";
print "Just type the name of the image , including<BR> its extension on it's ownline, <BR>i.e. myimage.gif<BR>";
print "4) To type a horizontal line, type<BR> 'print line' on it's own line...<BR>no quotes, no spaces.<BR>";
print "5) To include a hyperlink, type the<BR> name of the link minus the 'http://'<BR>
that will be added for you.<BR>";
print "6) Use all other options as wanted<BR>";
print "<br><br><BR><BR><BR><BR><BR>";
#print ("Want to get your old page source?<BR>");
#print ("Enter the name of your html file below<BR>");
#print ("Also enter this filename in the<BR> html page name field<BR>");
#print '<INPUT TYPE="TEXT" SIZE = 15 NAME= "reprint">';
#print p(submit("get source"));


print "</td>";


print '<td  valign="top" bgcolor="336699" cellspacing="0" cellpadding="0">';
print "<BLOCKQUOTE>";
print ' <font face="verdana, tahoma, arial, helvetica" size="-1">';
print "<br> <br>";
print '<p align="justify">';

#<!-- put stuff in here --->
print "<center><TABLE><form>";
print '<center><p><TABLE BORDER=1   CELLSPACING=0><TR><TD><img src = "title.jpeg" height = 100 width = 500></TD></TR></TABLE><p>';
print '<table>';
print'<textarea name="input" cols = 20 rows = 20 > ';
chdir "$dir" or die "cannot change dirs to stuff: $!";
open(FILE, "$reprint");
foreach(<FILE>){
	s/<BR>/\n/g;
	s/<H3>.+<\/H3>//;
	s/<[^>]*>//gs;
  	print;
}
#chdir "/home/httpd/cgi-bin";
$input = <FILE>;
print '</textarea></table>';
#$input = <FILE>;
print "<BR>";
print "<BR>";
print '<table><TABLE BORDER=0   CELLSPACING=0><TR><TD ALIGN=CENTER COLSPAN=1><B><img src = "cont.jpg"></B></TD></TR>';

print "<TR><TD>";
print 'background color: <INPUT TYPE="TEXT" SIZE = 10 NAME= "color" VALUE = "white"></TD><TD>';
print 'page title: <INPUT TYPE="TEXT" SIZE = 10 NAME= "title"></TD><TD>';
#print ("font color: ",  textfield("font_color", "$font"));
print '</TD><TD>';
print 'font color: <INPUT TYPE="TEXT" SIZE = 10 NAME = "font_color"></TD><TD>';
print '<font size = 1>html page name: </font><INPUT TYPE = "TEXT" SIZE = 10 NAME = "page"></TD><TD>';

print '<INPUT TYPE="SUBMIT" NAME="BUTTON" VALUE="view page"></TD><TD>';
print '<INPUT TYPE="RESET" NAME="BUTTON" VALUE="clear"></TD>';
print '</TR>';
print "</TABLE>";
#print "</TD></TR></TABLE>";
print "</form>";

#<!-- stop putting stuff in here -->
print "<br><BR>";
print "</BLOCKQUOTE>";
#print "</td></tr>";
#print '<tr><td align="right">';
#print '<font face="verdana, tahoma, arial, helvetica" size="1">';
#print "</td> </tr>";
print "</TD>";
print "</TR>";
print "</TABLE>";
print "</center>";
print "</body>";
print "</html>";
print end_html(), end_form();
}


else {
print header(), start_html("html_composer"),start_form();


=pod
                                    
print  "<html>";
print '<HEAD>';
print '<SCRIPT LANGUAGE="JavaScript">';
print '<!--
function openWin(URL){
awindow=window.open(URL,"composerwindow");}
// -->';
print '</SCRIPT></HEAD><BODY><FORM>';
print '<P><a href="JavaScript:openWin("howto.html");">Howto</a><p>';

=cut
 
print "<title><H2>[WebPage Composer]</H2></title>";
#bgCOlor used to be 5c4033
print '<body bgcolor="#336699" text="#cccccc" link="cc9999" vlink="999999">';
#<!-- nested table fu@!@# -->
print "<center>";

#print '<TABLE  cellpadding="0" cellspacing="0" border="0">';
#print "<TR>";
#print '<TD bgcolor="#000000" valign="top">';
#<!-- this is the outer table -->


print '<table  cellpadding="1" cellspacing="0" border="0">';
print '<td  colspan="0" bgcolor="#560000" valign="top">';
print "<tr>";
print '<td nowrap valign="top" bgcolor="333999">';
print '<font face="verdana, tahoma, arial, helvetica" size="1">';
print '<body background = "bark33.jpg">';
print '<a href="howto.html" target="blank"> <B>instructions</B></a>';
#print '<input type = "button" value = "New Window" onclick=NewWindow()>';
#print "<B><center>Instructions<BR></B>";
#print "1) Enter the html page name i.e.<BR> something_unique.html<BR>";
#print "2) Just type your web page content <BR>in the big white textarea.<BR>";
#print "3) To include an image, it must be in your<BR> server's icons or image directory<BR>";
#print "Just type the name of the image , including<BR> its extension on it's ownline, <BR>i.e. myimage.gif<BR>";
#print "4) To type a horizontal line, type<BR> 'print line' on it's own line...<BR>no quotes, no spaces.<BR>";
#print "5) To include a hyperlink, type the<BR> name of the link minus the 'http://'<BR>
#that will be added for you.<BR>";
#print "6) Use all other options as wanted<BR>";
#print "<br><br><BR><BR><BR><BR><BR>";
#print ("Want to get your old page source?<BR>");

#print ("Enter the name of your html file below<BR>");
#print ("Also enter this filename in the<BR> html page name field<BR>");
#print '<INPUT TYPE="TEXT" SIZE = 15 NAME= "reprint">';  
#print p(submit("get source"));


print "</td>";

#print '<td></td>';
#bgcolor used to be 336699
print '<td  valign="top" bgcolor="000000" cellspacing="0" cellpadding="0">';
print "<BLOCKQUOTE>";
print ' <font face="verdana, tahoma, arial, helvetica" size="-1">';
print "<br> <br>";
print '<p align="justify">';


#<!-- put stuff in here --->
print "<form><center><TABLE>";
print '<center><p><TABLE BORDER=3  CELLSPACING=0><TR><TD><H3><img src = "title.jpeg" height = 50 width = 500></H3></TD></TR></TABLE><p>';
print '<table><textarea name="input" cols = 45 rows = 10 >';
print '</textarea></table>';
print "<BR>";
print "<BR>";
print '<font face="verdana, tahoma, arial, helvetica" size="3">';

print '<table><TABLE BORDER=3  body bgcolor = "5c4033" CELLSPACING=0><TR><TD ALIGN=CENTER COLSPAN=6><B>Control Panel</B></TD></TR>';
#print '<table><TABLE BORDER=0   CELLSPACING=0><TR><TD ALIGN=CENTER COLSPAN=1>
#<img src = "cont.jpg" height = 50 width = 100></TD></TR>';

print "<TR><TD>";
print '<font face="verdana, tahoma, arial, helvetica" size="1">';

print 'background color: <INPUT TYPE="TEXT" SIZE = 15 NAME= "color" VALUE = "white"></TD><TD>';
print '<font face="verdana, tahoma, arial, helvetica" size="1">';

print 'page title: <INPUT TYPE="TEXT" SIZE = 15 NAME= "title"></TD><TD>';
print '<font face="verdana, tahoma, arial, helvetica" size="1">';

print 'font color: <INPUT TYPE="TEXT" SIZE = 15 NAME = "font_color"></TD><TD>';
print '<font face="verdana, tahoma, arial, helvetica" size="1">';
print 'html page name: <INPUT TYPE = "TEXT" SIZE = 15 NAME = "page"></TD><TD>';
print '<input type=image src="view3.gif" name =button>';
#print '<INPUT TYPE="SUBMIT" NAME="BUTTON" VALUE="View Page"></TD><TD>';
#print '<INPUT TYPE="RESET" NAME="BUTTON" VALUE="clear"></TD>';
print '</font></font></font></font>';
#print '</TR>';
#print "</TABLE>";
print "</TD></TR></TABLE>";
#print "</form>";

#<!-- stop putting stuff in here -->
print "<br><BR>";
print "</BLOCKQUOTE>";
print "</td>";
print "</tr>";
print "</table>";
print "</center>";
print end_form(), end_html();       
}



sub bad_input(){
print header(), start_html();
print "Please do not use any characters which might program our system";
print end_html();
}

sub is_tainted{
	my $check = shift;
	return !eval {$check++, kill 0; 1;};
}

sub error{
	print header(), start_html();
	print "Sorry, You have entered input which is considered to be tainted and may disrupt our system.<BR>Please try again.<BR>";
	print end_html();
}
