#!/usr/bin/perl --

if($ENV{'HTTP_USER_AGENT'} !~ /MSIE/){&browser}
#use CGI::Carp qw(fatalsToBrowser);
$hostname= $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'};
$this_url="http://".$hostname.$ENV{'SCRIPT_NAME'};
$wrxcgi=$this_url;$wrxcgi=~s/installer/wrx/;
$wrxcgi_=$ENV{'SCRIPT_NAME'};$wrxcgi_=~s/installer/wrx/;
if($ENV{'QUERY_STRING'} eq "progress"){&progress}
if($ENV{'QUERY_STRING'} =~ /new_cycle/){
	($act,$stuff)=split(/=/,$ENV{'QUERY_STRING'});
	&new_cycle;
	}

$cgi_check= eval { require CGI; } ? 1 : 0;
$socket_check= eval { require Socket; } ? 1 : 0;
$exporter_check= eval { require Exporter; } ? 1 : 0;

if($cgi_check!=1 || $socket_check!=1 || $exporter_check!=1){&mods_not}
$sendmail_check= eval { require MAIL::Sendmail; } ? 1 : 0;
$size_check= eval { require IMAGE::Size; } ? 1 : 0;

$QS=$ENV{'QUERY_STRING'};
$QS =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
($action,$cycle)=split(/=/,$QS);
&css;

$origin_path= $ENV{'PATH_TRANSLATED'};
if($origin_path !~ m!/installer\.\w{2,3}$!){$origin_path=$ENV{'SCRIPT_FILENAME'}}
if($origin_path !~ m!/installer\.\w{2,3}$!){$origin_path=$0}
$this_file=$origin_path;
$origin_path=~s!\\!/!g;
if($origin_path eq '' || $origin_path !~ m`/`){&no_origin_path}
$origin_path=~s!^\w:!!;
$origin_path=~s!/installer\.(\w+)$!!;
$CGI_EXT=$1;

if($action eq "admin_write_user"){
&get_input;
$editwrx_path=$path_to_users;
$editwrx_path=~s`/library.+``;
$crypt_pw=crypt($userpassword,"ce");
open(U,">>$path_to_users");
print U qq~$username=$crypt_pw=http://$ENV{'HTTP_HOST'}=bgcolor|bold|center|chars|code|copy|create_popup|cut|edit_file|edit_src|exit|fgcolor|find|fonts|fonts_adjust|fonts_fixed|forms|grid|help|hr|inline_link|italics|justify|keys|left|link|move_left|move_right|ol|on_off|openfile|page|para|paste|pasteword|photo|properties|redo|remove|restore|right|save|save_backup|saveas|script|speller|strikethrough|table|tagediton|text|thebuttons|ul|underline|undo|unformat|updates|upload====$useremail===site=full\n~;
$origin_path=~m`.+/([^/]+)$`;
if($fakeit==1){
	if($^O ne 'MSWin32'){$mod="<BR>Give wrx.$CGI_EXT <b>0755</b> permissions after moving it."}
	$RETURN=qq~<b>Your installation is complete.</b><P>Your server requires you to run wrx.$CGI_EXT in $origin_path.<BR>InstallPoint could not create wrx.$CGI_EXT in $origin_path so it created it in $path_to_wrx instead.<BR>You will need to move wrx.$CGI_EXT from $path_to_wrx to $origin_path with FTP.$mod<P>To login to EditWrx access <a href="$url_to_editor?welcome=$username" target="_top"> $url_to_editor?welcome=$username</a> in your browser after the move has been made.~;	
	}else{
	$RETURN=qq~<b>Your installation is complete.</b><P>Click <a href="$url_to_editor?welcome=$username" target="_top"><b>[HERE]</b></a> to login to EditWrx.~;
	}
print "Content-type: text/html\n\n";
print qq~<html><head><script>
function roll(){
	parent.document.getElementById("topic").innerHTML="<b>Installation Complete</b>"
	parent.document.getElementById("d").style.color="#aaaaaa"
	parent.document.getElementById("d").style.fontWeight="normal"
}
</script>
<style type="text/css">
TD{font-size:10px;color:#333333;font-family:Verdana,Arial,Helvetica,sans-serif;}
$css
</style>
</head>
<body onload=roll() style="background-color:#ffffff;border:0px">
<div id=hectar>
$RETURN
</div>
</body></html>~;
exit;
}#admin_write_user

if($action eq "admin_write_config"){
&get_input;
if($mode eq ''){$mode="0"}
open(C,"$path_to_config");
while(<C>){
	if(/default_file_name/){$_ = "\$default_file_name = '$default_file_name';\n"}
	if(/support_email/){$_ = "\$support_email = '$support_email';\n"}
	if(/account_email/){$_ = "\$account_email = '$default_email';\n"}
	$SAVE.=$_;
}
open(C,">$path_to_config");
print C "$SAVE";
$crypt_pw=crypt($new_password,"ce");
$path_to_users=$path_to_config;
$path_to_users=~s/config/users/;
open(U,">$path_to_users");
print U "admin=$crypt_pw\n";
&return_user;
print "Content-type: text/html\n\n";
print qq~<html><head><script>
function roll(){
	parent.document.getElementById("topic").innerHTML="<b>Default User's Set Up</b>"
	parent.document.getElementById("b").style.fontWeight="normal"
	parent.document.getElementById("b").style.color="#aaaaaa"
	parent.document.getElementById("c").style.color="#aaaaaa"
	parent.document.getElementById("c").style.fontWeight="normal"
	parent.document.getElementById("a").style.color="#aaaaaa"
	parent.document.getElementById("d").style.color="#000000"
	parent.document.getElementById("d").style.fontWeight="bold"
	parent.document.getElementById("use_cgibin").value="$use_cgibin"
}
</script>
<style type="text/css">
TD{font-size:10px;color:#333333;font-family:Verdana,Arial,Helvetica,sans-serif;}
$css
</style>
</head>
<body onload=roll() style="background-color:#ffffff;border:0px">
<div id=hectar>
$RETURN
</div>
</body></html>~;
exit;
}#admin_write_config

if($action eq 'two'){
$use_cgibin=0;
$dr=$cycle;
$cycle.="/editwrx";$dir="editwrx";
	if(!-e $cycle){mkdir($cycle,0755) || &not_writable}
	if($^O ne 'MSWin32'){chmod(0755,$cycle)}
	$test=$cycle."/test.txt";
	open(T,">$test");
	close(T);
	if(!-e $test){&no_modify}
	if($^O ne 'MSWin32'){chmod(0600,"$test")}
	open(T,">$test") || &not_suexec;
	close(T);
	unlink($test);
	$fakeCGI=$cycle."/wrx.".$CGI_EXT;
	open(TH,"$this_file");
	$shebang=<TH>;	
	$urlWRX="http://".$ENV{'HTTP_HOST'}."/$dir";
		&create_folders;
		&print_wrx_cgi;		
		$routines=$cycle."/library/routines.txt";
			$list=fetch("http://www.editwrx.com/installpoint/get_list.cgi");
			open(LOG,">log.txt");
			$LIST=$list;$LIST=~s/=/\n/g;
			print LOG "$LIST";
			close(LOG);
			@list=split(/=/,$list);
			foreach $file (@list){
				++$count;
				open(X,">count.txt");print X "$count";close(X);
				if($file =~ /size\.pm/i && $size_check ==1){next}
				if($file =~ /sendmail\.pm/i && $sendmail_check ==1){next}
				if($file=~/sample_pages/){
					$FILE=$dr."/".$file;
				}else{
					$FILE=$cycle."/".$file;
				}
				$URL="http://www.editwrx.com/installpoint/".$file;
				$content=fetch("$URL");
					if($FILE =~ m!sample_pages/full|sample_pages/block!){
						$content =~ s!/editwrx/wrx\.cgi!$wrxcgi_!g;
					} 
				open(FILE,">$FILE");
				if($^O eq 'MSWin32'){binmode FILE}
				print FILE "$content";
				close(FILE);
				if(!-d $FILE){
					chmod(0644,"$FILE");
				}
				if($count == 101){
					open(LOG,"log.txt");
					while(<LOG>){
					++$logCount;
						if($logCount >= $count){
							$SAVE_LOG.=$_;
						}
					}
					open(LOG,">log.txt");
					print LOG "$SAVE_LOG";
					next_cycle();
				}
			}
}#two

sub three{
&css;
$path_to_config=$cycle."/library/data/config.txt";
&return_complete;	
if(!$alert){$alert=qq~$count Files Were Installed~;}
unlink("log.txt");
unlink("count.txt");
print "Content-type: text/html\n\n";
print qq~<html><head><script>
function roll(){
	parent.document.getElementById("topic").innerHTML="<b>Set Up Site's Basic Configuration & Admin Login</b>"
	parent.document.getElementById("progress").style.display="none"
	parent.document.getElementById("b").style.fontWeight="normal"
	parent.document.getElementById("b").style.color="#aaaaaa"
	parent.document.getElementById("c").style.color="#000000"
	parent.document.getElementById("c").style.fontWeight="bold"
	parent.document.getElementById("a").style.color="#aaaaaa"
	parent.document.getElementById("d").style.color="#aaaaaa"
	parent.document.getElementById("use_cgibin").value="$use_cgibin"
	parent.document.getElementById("editwrx_folder").value="$dir"
	GET_COOKIE_HOLE.location="http://www.editwrx.com/affiliates/check_for_cookie.cgi?$url_to_editor"
	alert("$alert");
}
</script>
<style type="text/css">
TD{font-size:10px;color:#333333;font-family:Verdana,Arial,Helvetica,sans-serif;}
$css
</style>
</head>
<body onload=roll() style="background-color:#ffffff;border:0px">
<div id=hectar>
$RETURN
</div>
</body></html>~;
exit;
}#three

sub next_cycle{
if(!$stuff){
	$stuff=qq~$use_cgibin&$CGI_EXT&$cycle&$xurl_to_editor&$path_to_wrx&$fakeit~;
}
$ying=qq~<img style="margin:0px 10px" src="http://editwrx.com/installpoint/images/ying_yang.gif" width="16" height="16" border="0" alt="">~;
$yings=$ying;
if($nucount>200){$yings.=$ying;}
if($nucount>300){$yings.=$ying;}
if($nucount>400){$yings.=$ying;}

print "Content-type: text/html\n\n";
print qq~<html><head>
<script language=javascript>
function here(){
location="$this_url?new_cycle=$stuff"
}
</script>
</head>
<BODY onload=here() style="border:0px">
<div id=hectar><center>$yings</center>
</div>
</body></html>~;
exit;
}#next_cycle

 sub new_cycle{
($use_cgibin,$CGI_EXT,$cycle,$url_to_editor,$path_to_wrx,$fakeit)=split(/&/,$stuff);
 $dr=$cycle;
$dir="editwrx";
open(X,"count.txt");$count=<X>;close(X);$nucount=$count+101;
 	open(LOG,"log.txt");
		while(<LOG>){
			chomp();
			$file=$_;
			if($count<$nucount){
				open(X,">count.txt");print X "$count";close(X);
				if($file =~ /size\.pm/i && $size_check ==1){next}
				if($file =~ /sendmail\.pm/i && $sendmail_check ==1){next}
				if($file=~/sample_pages/){
					$FILE=$dr."/".$file;
				}else{
					$FILE=$cycle."/".$file;
				}
				$URL="http://www.editwrx.com/installpoint/".$file;
				$content=fetch("$URL");
					if($FILE =~ m!sample_pages/full|sample_pages/block!){
						$content =~ s!/editwrx/wrx\.cgi!$wrxcgi_!g;
					} 
				open(FILE,">$FILE");
				if($^O eq 'MSWin32'){binmode FILE}
				print FILE "$content";
				close(FILE);
				if(!-d $FILE){
					chmod(0644,"$FILE");
				}
				++$count;
			}else{
				$SAVE_LOG.=$_."\n";
			}
		}
		if($SAVE_LOG ne ''){
			open(LOG,">log.txt");
			print LOG "$SAVE_LOG";
			close(LOG);
			next_cycle();
		}else{
			open(X,"count.txt");$count=<X>;close(X);
			close(LOG);
			&three;
		}	 	
 }#new_cycle

if($action eq 'one'){
$origin_path=~s` `\\ `g;
@now=<$origin_path/*>;
$origin_path=~s!/[^/]+$!!;
@up=<$origin_path/*>;

foreach $u (@up){
	if(-d $u){
		$u=~m!.+/(.+)$!;$dir=$1;
		$LINES.=qq~<tr><td><img onclick='if(this.src.indexOf("/folder")!=-1){document.getElementById("$dir").style.display="";this.src="http://editwrx.com/installpoint/images/unfolder.gif"}else{document.getElementById("$dir").style.display="none";this.src="http://editwrx.com/installpoint/images/folder.gif"}' style="margin:0px 2px" src="http://editwrx.com/installpoint/images/folder.gif" width="16" height="16" border="0" alt=""> $u</td></tr>~;
	$u=~s` `\\ `g;
		@fold=<$u/*>;
		foreach $u (@fold){
			if(-T $u){
				$u=~s!(.+)/(.+)$!$1!;$f=$2;$f=~s` `&nbsp;`g;
				$INLINES.=qq~<tr><td style="padding-left:20px"><input onclick=done(this.value,"$f") type=radio name=home value="$u"> $u/$f</td></tr>~;
			}
		}
		$LINES.=qq~<tbody id=$dir style="display:none">~.$INLINES.qq~</tbody>~;
		$INLINES='';
	}
	if(-T $u){
		$u=~s!(.+)/(.+)$!$1!;$f=$2;$f=~s` `&nbsp;`g;
		$TINES.=qq~<tr><td><input onclick=done(this.value,"$f","$url") type=radio name=home value="$u"> $u/$f</td></tr>~;
	}
}
print "Content-type: text/html\n\n";
print qq~<html><head><script>
function done(obj,nm){parent.done(obj,nm)}
</script>
<style type="text/css">
TD{font-size:10px;color:#333333;font-family:Verdana,Arial,Helvetica,sans-serif;}
</style>
</head>
<body onload=parent.document.getElementById("ying").style.display="none" style="border:0px">
<img id=ying style="display:none" src="http://editwrx.com/installpoint/images/ying_yang.gif" width="16" height="16" border="0" alt="">
<div id=hectar>
<table border=0 cellpadding=0 cellspacing=0>
$LINES
$TINES
</table>
</div>
</body></html>~;
}#one

if($action eq ''){
if ($^O eq 'MSWin32'){$ms=qq~<br><br>Windows paths will not display the drive letter and will display slashes (/) instead of backslashes (\\).~}
$CGI_EXTone=$CGI_EXT."?one";
$CGI_EXTnone=$CGI_EXT."?none";
$CGI_EXTtwo=$CGI_EXT."?two";
$CGI_EXTthree=$CGI_EXT."?progress";
print "Content-type: text/html\n\n";
print qq~
<html><head><title>InstallPoint :: EditWrx</title>
<style type="text/css">
$css
</style>
<META HTTP-EQUIV="expires" CONTENT="Tue, 20 Aug 1996 14:25:27 GMT"> 
<script language=javascript>
dir="editwrx"
function done(obj,nm){
	nm=nm.replace(/&nbsp;/," ")
	if(confirm("Your Web Site's Home Page Is Named\\n\\n\\t"+nm+"\\n\\n\\nYour Web Site's Home Page Is\\n\\n\\t"+obj+"/"+nm+"\\n\\nCorrect?")){
		document.getElementById("progress").style.display=''
		document.getElementById("topic").innerHTML="<b>Creating folders and transferring files from editwrx.com</b>"
		document.getElementById("a").style.color="#aaaaaa"
		document.getElementById("a").style.fontWeight="normal"		
		document.getElementById("b").style.color="#000000"
		document.getElementById("b").style.fontWeight="bold"		
		hole.location="installer.$CGI_EXTtwo="+obj
		hole2.location="installer.$CGI_EXTthree"
	}else{
		alert("Locate and Select Your Web Site's Home Page")
		for(i=0;i<hole.document.all.length;++i){
			if(hole.document.all[i].type=="radio"){
				hole.document.all[i].checked=false
			}
		}
	}
}
function help_mode() {
alert('1) Full Page means that all pages can be edited from the open body tag to the close body tag.\\n\\n2) Restricted Pages means that a page can be edited from the open body tag to the close body tag\\nONLY if the page contains FULL_PAGE_EDITING="YES" in a javascript tag in the page\\'s head section.\\n\\n3) Block Only means that edits can only be done on pages containing Editblocks and that edits on\\nthose pages can only be done within Editblocks. If Block Only is selected, only pages containing\\nEditblocks will appear in the Open File Dialog for non-Super Users.\\n\\nAdding an Editblock to a page automatically makes a page BLOCK editable to non-Super Users\\nregardless of the above site-wide setting.\\n\\nA Super User has access to all of the web site, and all editor tools and can edit all pages from open body tag to close body tag. A Super User is created in User Set Up.')
}
function help_default_file_name(){
alert("If the user logs into the editor from a \\npage and the page's file name is not in \\nthe browser's address URL because the page\\nis the default index page for the folder,\\nthen this file name is used to locate the file so\\nthe edits are written to the correct file.\\n\\nIf appropriale, list more than one file name in\\nthe sequence that you want them considered.\\nSeparate multiple file names with a comma. ")
}
function help_default_email() {
alert('Any emails sent out by EditWrx will use this email address\\nfor the FROM in the email header.  Spam filtering software will\\ncheck with the email server for this domain to validate that this\\naddress is a legitimate email user. Emails requiring a return address\\nwill place that return in the Reply-To of the email\\'s header.')
}
function help_support_email() {
alert("This is the email address that displays on the Help system.\\n\\nEnter the email address where you want user's Help questions sent to.")
}

</script>
 </head>
<BODY scroll="no" bgcolor="#E7E2E7">
<form name=info method=post action="installer.$CGI_EXT">
<input type=hidden name=path_translated value="$origin_path">
<input type=hidden id=domain_root name=domain_root value="">
<input type=hidden id=editwrx_folder name=editwrx_folder value="editwrx">
<input type=hidden id=default_file_name name=default_file_name value="">
<input type=hidden id=use_cgibin value="">
</form>
<TABLE style="BORDER-RIGHT: #d4d9db 1px solid; BORDER-TOP: #d4d9db 1px solid; BORDER-LEFT: #d4d9db 1px solid; BORDER-BOTTOM: #d4d9db 1px solid" cellSpacing=0 cellPadding=2 width=650 align=center border=0>
<TBODY>
<TR>
<TD bgColor=#ffffff>
<TABLE style="BORDER-RIGHT: #d4d9db 1px solid; BORDER-TOP: #d4d9db 1px solid; BORDER-LEFT: #d4d9db 1px solid; BORDER-BOTTOM: #d4d9db 1px solid" cellSpacing=0 cellPadding=0 width="100%" align=center border=0>
<TBODY>
<TR>
<TD style="BACKGROUND-POSITION: 50% bottom; BACKGROUND-IMAGE: url(http://editwrx.com/installpoint/images/bttm_logo_bg.jpg); BACKGROUND-REPEAT: repeat-x; BACKGROUND-COLOR: #ffffff" vAlign=top>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width=175 vAlign=top><IMG src="http://editwrx.com/installpoint/images/bb.gif" border=0></TD>
<TD width=475 class=header style="padding-top:14px"><div id=heading>InstallPoint</div></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD vAlign=top>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD style="font-size:1px;background-image: url('http://editwrx.com/installpoint/images/x09.jpg')" bgColor=#c4c4c4 height=11 style="font.size:11">&nbsp;</TD></TR></table>
<div id=welcome class=header style="padding-top:16px"><center>Welcome</center></div>
<DIV class=pagecontent>
<div id=login_layer style="font-size:12px">
<span id=will>InstallPoint will install the EditWrx content manager in the<b> $ENV{'HTTP_HOST'} </b>domain on your server.<br><br></span>
During the installation InstallPoint will guide you through:
<UL>
<LI><span id=a>Establishing the PATHs and URLs to files</span>
<LI><span id=b>Creating folders and transferring necessary files</span>
<LI><span id=c>Configuring site's basic installation & admin login</span>
<LI><span id=d>Creating a default editing user</span>
</ul>
</DIV>
<div  id=one style="padding:8px">
<center ><span class=buttonborder><input onmouseover=this.className="uncombut" class=combut onmouseout=this.className="combut" type=button id=FFF onclick=one() value="Begin Installation" style="width:100px;"></span><div style="font-size:10px">$ms</center></div>
</div>
<div id=topic class=header></div>
<img id=ying style="display:none" src="http://editwrx.com/installpoint/images/ying_yang.gif" width="16" height="16" border="0" alt="">
<div id=progress style="display:none;margin-left:30px;width:250;"><DIV id=progress style="border:1px inset;background-color:#b0c4de;">
<DIV id=bar style="width:1px;background-color:blue;font-size:12px"></DIV></DIV>
Files Installed: <SPAN id=cnt></SPAN></div>
<iframe name=hole style="display:none;border:0px;height:expression(document.body.clientHeight-300)" class=pagecontent width=580 src="installer.$CGI_EXTnone"></iframe></td></tr></table>
<iframe name=hole2 style="display:none"></iframe>
<script language=javascript>
function one(){
	document.getElementById("topic").className="topic"
	document.getElementById("topic").innerHTML="<b>Locate and Select Your Web Site's Home Page</b>"
	document.getElementById("ying").style.display=''
	document.getElementById("one").style.display="none"
	document.getElementById("welcome").style.display="none"
	document.getElementById("will").style.display="none"
	document.getElementById("a").style.fontWeight="bold"
	document.getElementById("b").style.color="#aaaaaa"
	document.getElementById("c").style.color="#aaaaaa"
	document.getElementById("d").style.color="#aaaaaa"
	document.getElementById("hole").style.display=""
	hole.location="installer.$CGI_EXTone"
}
</script>
<iframe name=holer style="display:none"></iframe>
</BODY>
 </html>
~;
}#begin

if($action eq 'none'){
print "Content-type: text/html\n\n";
print qq~<html><body style="border:0px"></body></html>~;
}#none

sub css{
$css=qq~	body,td		{ 
			font-family: arial, verdana, times, helvetica; 
			font-size: 11px; 
			color: #000000; 
	}
	body 		{
		 
			margin:0; margin-top:10px;
	}
	.header		{ 
			font-weight:bold; 
			font-size:16px; 
			color:#1E79D2; 
			font-family:Arial; 
			padding-bottom:6px
			}
	.topic{font-size:11px;font-weight:normal;color:#1E79D2;padding-left:20px}
	.pagecontent { margin:15px 25px 10px 25px; }	
	.smhead		{ color: #1E79D2; font-weight: bold;font-size: 14px; }
	.bottom		{ color: #666666; }
	.href{cursor:hand;color:#1E79D2; text-decoration: none;}
	.nothref{cursor:default;color:#ff3300; text-decoration: none;}
	.input{border-bottom:1px #aaaaaa solid;border-right:1px #aaaaaa solid;border-top:1px #333333 solid;border-left:1px #333333 solid;
		background:#E7E2E7;
		font-family:arial;
		font-size:12px;
		padding-left:2px
		}
	select{
		background:#E7E2E7;
		font-family:tahoma;
		font-size:12px;		
		}
	.fieldset{padding:12px;}
	.legend{color:#ff3300;font-size:10px;font-family:arial}
	.combut{
	background-color:buttonface;
	color:#333333;
	border-top:1px solid #eeeeee;
	border-left:1px solid #eeeeee;
	border-right:1px solid #aaaaaa;
	border-bottom:1px solid #aaaaaa;
	font-family:tahoma;
	font-weight:bold;
	padding:1px;
	font-size:10px
	}
	.uncombut{
	background-color:buttonface;
	color:#000000;
	border-top:1px solid #ffffff;
	border-left:1px solid #ffffff;
	border-right:1px solid #666666;
	border-bottom:1px solid #666666;
	font-family:tahoma;
	font-weight:bold;
	padding:1px;
	font-size:10px
	}
	.buttonborder{
	border:1px solid #666666;
	}
	.xcombut{font-size:20px}
	form{margin:0px}
~;
}#css

sub fetch{
	my ($output,$n) = undef;
	my $fullurl = $_[0];
	$fullurl =~ s/ /\%20/g;
	$fullurl =~ s!http://!!;
	$fullurl =~ m!(.+?)/(.+)!;
	require IO::Socket;
	my $host_name = $1;
	my $file_path = $2;
	local($^W) = 0;
	my $connect = IO::Socket::INET->new(PeerAddr=>$host_name, PeerPort=>80, Proto=>'tcp', Timeout=>60);
	if(!$connect){
		if($FCH<9){
			++$FCH;
			sleep(5);			
			&fetch;
		}else{
			&not_connected;exit;
		}
	}else{
  		$connect->autoflush;
		print $connect join("\n"=>"GET /$file_path HTTP/1.0","Host: $host_name","User-Agent: Fetch1.5\n","","");
		1 while $n = sysread($connect, $output, 8*1024, length($output));
		$output =~ s/.+?\012?\015\012?\015//s;
		$output =~ s/^\s+//;
		return $output;
	}
}#fetch

sub not_suexec{
unlink($test);
if($^O ne 'MSWin32'){$message=qq~Your server is not running SuExec.<P>It is unsafe to install any content management system on this server.<P>InstallPonit has aborted the installation.~}
else{$message=qq~Your server does not allow cgi scripts to write to files in the doman space.<P>Consult with your system administrator about enabling write privileges for the domain.<P>InstallPoint has aborted the installation. Could not write to:<P>$test~}
print "Content-type: text/html\n\n";
print qq~<html><head><script>
function here(){
	parent.document.getElementById("topic").innerHTML="<b>ERROR</b>"
}
</script>
<style type="text/css">$css</style></head>
<body onload=here() style="border:2px dashed red;padding:10px">
<b>$message</b>
</body></html>~;
exit;
}

sub no_modify{
if($^O ne 'MSWin32'){$message=qq~Your server does not allow scripts to create files.<P>It is unsafe to install any content management system on this server.<P>InstallPonit has aborted the installation.<P>Could not create $test~}
else{$message=qq~Your server does not allow cgi scripts to create files in the doman space.<P>Consult with your system administrator about enabling MODIFY privileges for the domain.<P>InstallPoint has aborted the installation. Could not create :<P>$test<P><P><a href="http://www.editwrx.com/images/IIS_setup2.gif" target="_blank">(screenshot1)</a> <a href="http://www.editwrx.com/images/IIS_setup.gif" target="_blank">(screenshot2)</a>~}
print "Content-type: text/html\n\n";
print qq~<html><head><script>
function here(){
	parent.document.getElementById("topic").innerHTML="<b>ERROR</b>"
}
</script>
<style type="text/css">$css</style></head>
<body onload=here() style="border:2px dashed red;padding:10px">
<b>$message</b>
</body></html>~;
exit;
}#no_modify

sub not_found{
print "Content-type: text/html\n\n";
print qq~<html><head><script>
function here(){
	parent.document.getElementById("topic").innerHTML="<b>ERROR</b>"
}
</script>
<style type="text/css">$css</style></head>
<body onload=here() style="border:2px dashed red;padding:10px">
$_[0]
</body></html>~;
exit;
}

sub print_wrx_cgi{
$mod_dir=$cycle."/library/mods";
if(-d $mod_dir){$useit=qq~\nuse lib '$cycle/library/mods';~}
$origin_path=~m`/([^/]+)$`;$cgiDIR=$1;$cgiDIR=~s`/$``;
if($cgiDIR !~ /cgi-bin|editwrx$/){$cgiDIR=""}
$cycle=~m`(.+)/$dir`;$domain=$1;
$script = q`#!/usr/bin/perl --

use CGI::Carp qw(fatalsToBrowser);

$editwrx_folder_name="editwrx";
$child=0;
$demo_mode=0;
#################################################

$url_to_editor = "http://".$ENV{'HTTP_HOST'}.$ENV{'SCRIPT_NAME'};
&get_root;
$path_to_data = "$path_to_domain/$editwrx_folder_name/library/data";	

$lib = "$path_to_library/mods";		
push(@INC,$lib);
$routines = $path_to_library."/routines.txt";
require "$routines";

sub parent{
	$url_to_library = "url_to_library_in_parent_domain";
	$url_to_ikons = "url_to_ikons_in_parent_domain";	
	$path_to_library = "path_to_library_in_parent_domain";	
}

sub get_root{
	$path_to_domain = $ENV{'DOCUMENT_ROOT'} || $ENV{'SCRIPT_FILENAME'} || $ENV{'PATH_TRANSLATED'} ;
	$path_to_domain=~s-\\\-/-g;
	$script_uri = $ENV{'SCRIPT_NAME'} || $ENV{'PATH_INFO'};
	$path_to_domain =~ s-$script_uri$--;
	$path_to_domain=~s-^.+\:--;
	$path_to_domain=~s-/$--;
	if($path_to_domain eq ''){&no_root}
	if(!$child){
		$url_to_library = "http://$ENV{'HTTP_HOST'}/$editwrx_folder_name/library";
		$url_to_ikons = "http://$ENV{'HTTP_HOST'}/$editwrx_folder_name/ikons";	
		$path_to_library = "$path_to_domain/$editwrx_folder_name/library";		
	}else{
		&parent;
	}
	$path_to_library =~ s!/$!!;
	if(!-f $path_to_library."/routines.txt"){
		&get_root2;
	}
}

sub get_root2{
	$path_to_domain = $ENV{'SCRIPT_FILENAME'} || $ENV{'PATH_TRANSLATED'} ;
	$path_to_domain=~s-\\\-/-g;
	$script_uri = $ENV{'SCRIPT_NAME'} || $ENV{'PATH_INFO'};
	$path_to_domain =~ s-$script_uri$--;
	$path_to_domain=~s-^.+\:--;
	$path_to_domain=~s-/$--;
	if($path_to_domain eq ''){&no_root}
	if(!$child){
		$url_to_library = "http://$ENV{'HTTP_HOST'}/$editwrx_folder_name/library";
		$url_to_ikons = "http://$ENV{'HTTP_HOST'}/$editwrx_folder_name/ikons";	
		$path_to_library = "$path_to_domain/$editwrx_folder_name/library";		
	}else{
		&parent;
	}
	$path_to_library =~ s!/$!!;
	if(!-f $path_to_library."/routines.txt"){
		&no_root;
	}
}

sub no_root{
print "Content-type: text/html\n\n";
print "Your server does not have a usable environment variable for determining your site's root path<BR>$path_to_library/routines.txt does not exist<P>";
for (sort keys %ENV) {
  print "<br><tt>$_=$ENV{$_}</tt>\n";
 }
 exit;
}
`;
$first=$origin_path."/wrx.".$CGI_EXT;
open(X,">$first") || &fakeit;
if(-e $first){
	$path_to_wrx=$first;
	print X "$script";
	close(X);
	chmod(0755,"$first");
	}
$url_to_editor = "$ENV{'HTTP_HOST'}/$cgiDIR/wrx.$CGI_EXT";
$url_to_editor =~ s`//`/`g;
$url_to_editor = "http://".$url_to_editor;
}#print_wrx_cgi

sub fakeit{
$second=$cycle."/wrx.".$CGI_EXT;
open(SC,">$second");
print SC "$script";
close(SC);
chmod(0755,"$second");
$path_to_wrx=$second;
$fakeit=1;
}#fakeit

sub create_folders{
$lib=$cycle."/library";
	mkdir($lib,0755);
	chmod(0755,$lib);	
$smp=$dr."/sample_pages";
	mkdir($smp,0755);
	chmod(0755,$smp);		
if($size_check !=1){
	$mod=$lib."/mods";
	mkdir($mod,0755);
	chmod(0755,$mod);
	}
elsif($sendmail_check!=1){
	$mod=$lib."/mods";
	mkdir($mod,0755);
	chmod(0755,$mod);
	}
if($sendmail_check !=1){
	$ema=$mod."/Mail";
	mkdir($ema,0755);
	chmod(0755,$ema);
	}
if($size_check !=1){
	$img=$mod."/Image";
	mkdir($img,0755);
	chmod(0755,$img);
	}
$dat=$lib."/data";
	mkdir($dat,0755);	
	chmod(0755,$dat);
$ini=$lib."/ini";
	mkdir($ini,0755);
	chmod(0755,$ini);
$htm=$lib."/htm";
	mkdir($htm,0755);	
	chmod(0755,$htm);
$iko=$cycle."/ikons";
	mkdir($iko,0755);
	chmod(0755,$iko);
$nts=$cycle."/ikons/notes";
	mkdir($nts,0755);
	chmod(0755,$nts);	 
$lan=$lib."/lang";
	mkdir($lan,0755);	
	chmod(0755,$lan);
$eng=$lib."/lang/english";
	mkdir($eng,0755);	
	chmod(0755,$eng);
$hel=$lib."/lang/english/help";
	mkdir($hel,0755);	
	chmod(0755,$hel);

}#create_folders

sub mods_not{
if($cgi_check!=1){$MODS.=qq~CGI.pm<br>~;}
if($socket_check!=1){$MODS.=qq~Sockets.pm<br>~;}
if($exporter_check!=1){$MODS.=qq~Exporter.pm<br>~;}
print "Content-type: text/html\n\n";
print qq~REQUIRED PERL MODULES ARE NOT AVAILABLE<P>YOU CAN NOT INSTALL EDITWRX 6 ON THIS SERVER<P>
Missing Modules<BR>$MODS~;
exit;
}#mods_not

sub return_complete{
if($use_cgibin==1){$WRX="cgi-bin"}else{$WRX=$dir}
$SRC="http://editwrx.com/installpoint/ikons/point_help.gif";
$SRC=qq~src="$SRC" width="14" height="14" border="0" alt="Description"~;
$CGI_EXTadmin_write_config=$CGI_EXT."?admin_write_config";
$RETURN=qq~
<form name=site_config method=post action="installer.$CGI_EXTadmin_write_config" target=hole>
<input type=hidden name=path_to_config value="$path_to_config">
<input type=hidden name=url_to_editor value="$wrxcgi">
<input type=hidden name=path_to_wrx value="$path_to_wrx">
<input type=hidden name=fakeit value="$fakeit">

<table width=100% height=90% border=0 cellpadding=0 cellspacing=0><tr><td align=center valign=top>
<table border=0 cellpadding=0 cellspacing=0><tr><td align=center>
<fieldset><legend class=legend>Site Parameters</legend>
<table style="margin:10px" border=0 cellpadding=5 cellspacing=0>
<tr><td>Enter An Admin Password:</td><td><input class=input type=password id=new_password name=new_password style="width:150px;" value=""> </td></tr>
<tr><td>Repeat Admin Password:</td><td><input onchange=match(this.value) class=input type=password name=repeat_password style="width:150px;" value=""> </td></tr>
<tr><td>Default Email Address:</td><td><input class=input type=input name=default_email style="width:250px;" 
value=""> </td><td><img $SRC onclick=parent.help_default_email() class=href ></td></tr>
<tr><td>Help Email Address:</td><td><input class=input type=input name=support_email style="width:250px;" 
value="support\@editwrx.com"> </td><td><img $SRC onclick=parent.help_support_email() class=href ></td></tr>

<tr><td>Default File Name(s):</td><td><input class=input type=input name=default_file_name style="width:250px;" 
value=""> </td><td><img onclick=parent.help_default_file_name() class=href $SRC></td></tr>

<tr><td colspan=3 align=center style="padding-top:20px">
<span class=buttonborder><input onmouseover=this.className="uncombut" class=combut onmouseout=this.className="combut" type=button id=save_butt  onclick=this.form.submit() value="Save Site Parameters" style="width:120px;"></span>
</td></tr>
<tr><td colspan=3 align=center style="padding-top:20px">
The above parameters are essential to a basic installation. They, and<br>
other Site Parameters, may be changed by logging in as "<b>admin</b>"  at<br><b>$url_to_editor</b>
</td></tr></table><br></fieldset>
</td></tr></table></td></tr></table></form>
<iframe name=GET_COOKIE_HOLE width=0 height=0 style="visibility:hidden"></iframe>
<script language=javascript>
function match(obj){
if(obj != document.getElementById("new_password").value){
	alert("Your two password entries do not match");
	document.getElementById("repeat_password").value=""
	document.getElementById("new_password").value=""
	}
}
</script>
~;
}#return_complete

sub return_user{
if($use_cgibin==1){$WRX="cgi-bin"}else{$WRX=$editwrx_path}
$CGI_EXTadmin_write_user=$CGI_EXT."?admin_write_user";
$RETURN=qq~<form name=site_config method=post action="installer.$CGI_EXTadmin_write_user" target=hole>
<input type=hidden name=path_to_users value="$path_to_users">
<input type=hidden name=url_to_editor value="$wrxcgi">
<input type=hidden name=path_to_wrx value="$path_to_wrx">
<input type=hidden name=fakeit value="$fakeit">
<table width=100% height=90% border=0 cellpadding=0 cellspacing=0><tr><td align=center valign=top>
<table border=0 cellpadding=0 cellspacing=0><tr><td align=center>
<fieldset><legend class=legend>User Setup</legend>
<table style="margin:10px" border=0 cellpadding=5 cellspacing=0>
<tr><td>Enter A User Name:</td><td><input class=input type=text id=username name=username style="width:150px;" value=""> </td></tr>
<tr><td>Enter User Password:</td><td><input class=input type=text name=userpassword style="width:150px;" value=""> </td></tr>
<tr><td>User's Email Address:</td><td><input class=input type=text name=useremail style="width:150px;" value=""> </td></tr>
<tr><td colspan=2 align=center style="padding-top:20px">
<span class=buttonborder><input onmouseover=this.className="uncombut" class=combut onmouseout=this.className="combut" type=button id=save_butt  onclick=this.form.submit() value="Save User Setup" style="width:100px;"></span>
</td></tr>
<tr><td colspan=3 align=center style="padding-top:20px">
The above user is a Super User. Super Users have full access<br>to the domain, 
access to all editing tools, and full page editing.  To<br>change this user and create other users, login as "<b>admin</b>"  at<br>
<b>$url_to_editor</b>
</td></tr></table></fieldset></td></tr></table></td></tr></table>

~;
}#return_user


sub get_input{
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$input);
foreach $pair (@pairs){
  $pair =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  ($name, $value) = split(/=/,$pair,2);
  $value =~ tr/+/ /;
  $value =~ s/~!/ ~!/g;
  $value =~ s/[\r]//g;
$$name=$value; 
}
}#get_input

sub error{
print "Content-type: text/html\n\n";
print "$_[0]";
exit;
}

sub not_writable{
print "Content-type: text/html\n\n";
print qq~<html><head>
<style type="text/css">$css</style></head>
<body style="border:2px dashed red;padding:10px">
InstallPoint could not create a folder at $cycle/editwrx<P><P>
You need to configure your server to allow Perl scripts to create<br>
folders in your domain path.
</body></html>~;

exit
}#not_writable

sub not_connected{
print "Content-type: text/html\n\n";
print qq~<html><head>
<style type="text/css">$css</style></head>
<body style="border:2px dashed red;padding:10px;font-size:13px">
<b>InstallPoint could not connect to editwrx.com</b><P><P>
Your server has been intentionally crippled to disallow<br>
server-to-server connections. InstallPoint can not install files.<P>
You need to download the <a href="http://www.editwrx.com/download/zip/editwrx.zip">editwrx.zip</a>  and<br>
install the files manually with FTP.<P>
All files in <a href="http://www.editwrx.com/download/zip/editwrx.zip">editwrx.zip</a> must be installed in the <br>
$cycle/ folder
<P><P>
Once you have uploaded the zip's files, run installer.cgi<br>
again to complete your installation of EditWrx.<P>
Note: Your server will also not allow Automatic Updates
</body></html>~;

}#not_connected

sub progress{
open(XX,"count.txt");
$count=<XX>;
close(XX);
if($count<2){$count=2}
$cnt=$count/2;
$cnt=int($cnt);
print "Content-type: text/html\n\n";
print qq~<html><head>
<script language=javascript>
function here(){
parent.document.getElementById("bar").style.width="$cnt" 
parent.document.getElementById("cnt").innerHTML="$count"
w=setTimeout("nxt()",1000)
}
function nxt(){location.reload(true)}
</script>
</head>
<BODY onload=here()>
</body></html>~;
exit
}#progress

sub browser{
print "Content-type: text/html\n\n";
print qq~<html><head><title></title></head><BODY bgcolor=ffffff text=000000><br><br>
<center>EditWrx Installer will only run in Explorer 5.5+ browsers. </center>
</body></html>~;
exit;
}