#!/usr/bin/perl

BEGIN {
	push(@INC,"/usr/local/cpanel");
}

use IPC::Open3;
use SafeFile;

#VARIBLES
my $domain = '%domain%';
my $installpath = '%installpath%';
my $modulename = '%modulename%';
my $modulepath = '%modulepath%';
my $homedir = '%homedir%';
my $user = '%user%';
my $installdir = '%installdir%';
my $publichtml = '%public_html%';
my $sqldb = '%sqldb%';
my $sqluser = '%sqluser%';
my $sqlpass = '%sqlpass%';
my $sqlserver = '%sqlhost%';
my $username = '%username%';
my $password = '%password%';

print "We are upgrading in $installdir...\n";

#unlink("$installdir/cpanel-upgrade/pkg/upload/conf_global.php");
#system("cp -af $installdir/cpanel-upgrade/pkg/upload/* $installdir/ 2>/dev/null");
#system("rm","-rf","$installdir/cpanel-upgrade");

#$dbhost='localhost';
#$dbuname='n3t_nuke4';
#$dbpass='E3FlEa0L4b';
#$dbname='n3t_nuke4';

#---[ Get current config information ]---

open(CFG,"<$installdir/config.php") or print "Could not open $installdir/config.php , you will probably need to set username/pass info in it manually.\n";
@conf=(<CFG>);
close(CFG);

foreach $line(@conf) {
$line=~ s/\n//;
$line=~ s/\s+\=/\=/g;
$line=~ s/\=\s+/\=/g;
($var,$val)=split(/=/,$line,2);
$val=~ s/^\"//;
$val=~ s/\"\;$//;
$val=~ s/^\'//;
$val=~ s/\'\;$//;
if($var eq "\$dbhost") { $userdbhost="$val"; next; }
if($var eq "\$dbuname") { $userdbuname="$val"; nex; }
if($var eq "\$dbpass") { $userdbpass="$val"; next; }
if($var eq "\$dbname") { $userdbname="$val"; next;  }

}

#---[ Integrate config vars into new config file ]---

open(NCFG,"<$installdir/cpanel-upgrade/pkg/html/config.php");
@newuno=(<NCFG>);
close(NCFG);

open(FCFG,">$installdir/cpanel-upgrade/pkg/html/config.php") or print "Can not overwrite new config.php ,  you will probably need to set it up manually.\n";
foreach $lin(@newuno) {
	if($lin =~ /^\$dbhost/) { print FCFG "\$dbhost = \"$userdbhost\"\;\n"; }
	elsif($lin =~ /^\$dbuname/) { print FCFG "\$dbuname = \"$userdbuname\"\;\n"; }
	elsif($lin =~ /^\$dbpass/) { print FCFG "\$dbpass = \"$userdbpass\"\;\n"; }
	elsif($lin =~ /^\$dbname/) { print FCFG "\$dbname = \"$userdbname\"\;\n"; }
	else { print FCFG "$lin"; }
	}
close(FCFG);

system("cp -af $installdir/cpanel-upgrade/pkg/html/* $installdir/ 2>/dev/null");
system("cp -af $installdir/cpanel-upgrade/pkg/60-68up.php $installdir/ 2>/dev/null");

foreach $env (sort keys %ENV) {
   if ($env ne "PATH") {
      delete $ENV{$env};
   }
}


chdir($installdir);

$ENV{'CONTENT_TYPE'} = 'application/x-www-form-urlencoded';
$ENV{'DOCUMENT_ROOT'} = $publichtml;
$ENV{'SERVER_PROTOCOL'} = 'HTTP/1.0';
$ENV{'GATEWAY_INTERFACE'} = 'CGI/1.1';
$ENV{'REQUEST_URI'} = $installpath . "/" . "60-68up.php";
$ENV{'SCRIPT_URI'} = $installpath . "/" . "60-68up.php";
$ENV{'SCRIPT_FILENAME'} = "${installdir}/60-68up.php";
$ENV{'REDIRECT_STATUS'} = 1;
$ENV{'REQUEST_METHOD'} = "GET";
$ENV{'HTTP_STATUS'} = 200;	      

print "<br><br>";

	$ENV{'QUERY_STRING'} = "";
	$ENV{'QUERY_STRING'} = URLEncode($ENV{'QUERY_STRING'});

if (-e "/usr/local/bin/php") {
        open3(\*PHPW,\*PHPR,"","/usr/local/bin/php","60-68up.php");
} else {
        open3(\*PHPW,\*PHPR,"","/usr/bin/php","60-68up.php"); }
	close(PHPW);

	while(<PHPR>) {
		#	print;
	}
	close(PHPR);


system("rm","-rf","$installdir/cpanel-upgrade");
unlink("$installdir/60-68up.php");
chmod(0777,"$installdir/ultramode.txt");

print "<b>Complete!</b>\n";

print "<br>You can access your upgraded board: <a href=\"http://$domain/$installpath/\">http://${domain}${installpath}/</a><br>\n";
print "<br><br>\n";


sub regsrep {
   my($file,$old,$new) = @_;
   $filelock = SafeFile::safeopen(\*FH,"$file");
   $fileeditlock = SafeFile::safeopen(\*FHT,">$file.tmpeditlib");
   while(<FH>) {
      if ($_ =~ /^$/ || $_ eq "\n" || $_ eq "\r\n") {
         print FHT "$_";
      } elsif($_ =~ /$old/) {
        $result = $1;
         my $mnew = $new;
        $mnew =~ s/\$1/$result/g;
         print FHT "$mnew\n";
      } else {
         print FHT $_;
      }
   }
   SafeFile::safeclose(\*FH,$filelock);
   SafeFile::safeclose(\*FHT,$fileeditlock);
   $fileeditlock = SafeFile::safeopen(\*FHT,"$file.tmpeditlib");
   $filelock = SafeFile::safeopen(\*FH,">$file");
   while(<FHT>) { print FH $_ };
   SafeFile::safeclose(\*FH,$filelock);
   SafeFile::safeclose(\*FHT,$fileeditlock);
}



           
sub URLEncode
{
   my($url)=@_;
   my(@characters)=split(/(\%[0-9a-fA-F]{2})/,$url);

   foreach(@characters)
   {
       if ( /\%[0-9a-fA-F]{2}/ ) # Escaped character set ...
       {
           # IF it is in the range of 0x00-0x20 or 0x7f-0xff
           #    or it is one of  "<", ">", """, "#", "%",
           #                     ";", "/", "?", ":", "@", "=" or "&"
           # THEN preserve its encoding
           #"
           unless ( /(20|7f|[0189a-fA-F][0-9a-fA-F])/i
                   || /2[2356fF]|3[a-fA-F]|40/i )
           {
               s/\%([2-7][0-9a-fA-F])/sprintf "%c",hex($1)/e;
           }
       }
       else # Other stuff
       {
           # 0x00-0x20, 0x7f-0xff, <, >, and " ... "
           s/([\000-\040\177-\377\074\076\042])
               /sprintf "%%%02x",unpack("C",$1)/egx;
       }
   }
   return join("",@characters);
}

