#!/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...<br>\n";

#---[ grab passes from running config ]---

open(CFG,"<$installdir/includes/configure.php") or print "Could not open configure.php file.\n\n";
@cfig=(<CFG>);
close(CFG);

#---[ this will break if any of the four values have a ' in them. not very likely but...hrmm... ]---

foreach $cf(@cfig) {
if($cf =~ /define\(\'DB_SERVER\'\,/i) { (undef,undef,undef,$sqlserver,undef)=split(/\'/,$cf,5); }
if($cf =~ /define\(\'DB_SERVER_USERNAME\'\,/i) { (undef,undef,undef,$sqluser,undef)=split(/\'/,$cf,5); }
if($cf =~ /define\(\'DB_SERVER_PASSWORD\'\,/i) { (undef,undef,undef,$sqlpass,undef)=split(/\'/,$cf,5); }
if($cf =~ /define\(\'DB_DATABASE\'\,/i) { (undef,undef,undef,$sqldb,undef)=split(/\'/,$cf,5); }
}

#---[ install new files ]---

system("cp -af $installdir/cpanel-upgrade/pkg/catalog/* $installdir/ 2>/dev/null");

#---[ update new config files with info ]---

regsrep("$installdir/includes/configure.php","\\(\'HTTP_SERVER\'","define('HTTP_SERVER', 'http://$domain');");
regsrep("$installdir/includes/configure.php","\\(\'HTTPS_SERVER\'","define('HTTPS_SERVER', 'https://$domain');");
regsrep("$installdir/includes/configure.php","\\(\'HTTP_COOKIE_DOMAIN\'","define('HTTP_COOKIE_DOMAIN', 'http://$domain');");

regsrep("$installdir/includes/configure.php","\\(\'HTTPS_COOKIE_DOMAIN\'","define('HTTPS_COOKIE_DOMAIN', 'https://$domain');");
regsrep("$installdir/includes/configure.php","\\(\'HTTP_COOKIE_PATH\'","define('HTTP_COOKIE_PATH', '/');");
regsrep("$installdir/includes/configure.php","\\(\'HTTPS_COOKIE_PATH\'","define('HTTPS_COOKIE_PATH', '/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_HTTP_CATALOG\'","define('DIR_WS_HTTP_CATALOG', '${installpath}/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_HTTPS_CATALOG\'","define('DIR_WS_HTTPS_CATALOG', '${installpath}/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_IMAGES\'","define('DIR_WS_IMAGES', '${installpath}/images/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_ICONS\'","define('DIR_WS_ICONS', '${installpath}/icons/');");
regsrep("$installdir/includes/configure.php","\\(\'DB_SERVER\'","define('DB_SERVER', '$sqlserver');");
regsrep("$installdir/includes/configure.php","\\(\'DB_SERVER_USERNAME\'","define('DB_SERVER_USERNAME', '$sqluser');");
regsrep("$installdir/includes/configure.php","\\(\'DB_SERVER_PASSWORD\'","define('DB_SERVER_PASSWORD', '$sqlpass');");
regsrep("$installdir/includes/configure.php","\\(\'DB_DATABASE\'","define('DB_DATABASE', '$sqldb');");
unlink("$installdir/includes/configure.php.tmpeditlib");

regsrep("$installdir/admin/includes/configure.php","\\(\'HTTP_SERVER\'","define('HTTP_SERVER', 'http://$domain');");
regsrep("$installdir/admin/includes/configure.php","\\(\'HTTPS_SERVER\'","define('HTTPS_SERVER', 'https://$domain');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DIR_WS_ADMIN\'","define('DIR_WS_ADMIN', '${installpath}/admin/');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DIR_WS_CATALOG\'","define('DIR_WS_CATALOG', '${installpath}/');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_SERVER\'","define('DB_SERVER', '$sqlserver');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_SERVER_USERNAME\'","define('DB_SERVER_USERNAME', '$sqluser');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_SERVER_PASSWORD\'","define('DB_SERVER_PASSWORD', '$sqlpass');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_DATABASE\'","define('DB_DATABASE', '$sqldb');");
unlink("$installdir/admin/includes/configure.php.tmpeditlib");

#---[ we can edit the install/modify files and skip some junk or fake POST's w/o a webserver. hmm. ]---

open(TMP3,"<$installdir/cpanel-upgrade/pkg/upgrade_3.php") or print "Can not find upgrade script template.<br>\n";
@cont=(<TMP3>);
close(TMP3);

open(FINUP,">$installdir/install/cpup.php") or print "Could not open final upgrade script for writing.<br>\n";
foreach $line(@cont) {
$line =~ s/__sqldb__/$sqldb/g;
$line =~ s/__sqluser__/$sqluser/g;
$line =~ s/__sqlpass__/$sqlpass/g;
$line =~ s/__sqlserver__/$sqlserver/g;
print FINUP "$line";
}
close(FINUP);

chdir("$installdir/install");

#---[ something about cpanel's php doesn't let this work ]---

if (-e "/usr/local/bin/php") {
     open3(\*PHPW,\*PHPR,"","/usr/local/bin/php","$installdir/install/cpup.php");
} else {
     open3(\*PHPW,\*PHPR,"","/usr/bin/php","$installdir/install/cpup.php");
}
close(PHPW);
while(<PHPR>) {
       #print;
}
close(PHPR);

#---[ need to check for fileprotect here and chown/chmod appropriately. need root for the chown though; not gonna make them world readable. :/ ]---

if(-e "/var/cpanel/fileprotect") {
print "Fileprotection appears to be enabled, setting permissions on configure.php files. *CURRENTLY BROKEN*<br>\n";
chmod(0440,"$installdir/includes/configure.php"); 
chmod(0440,"$installdir/admin/includes/configure.php"); 
$llamo=sprintf(scalar getpwuid($>));
$uid = (getpwuid($>))[3];
$gid = (getgrnam("nobody"))[2];
chown $uid, $gid, "$installdir/includes/configure.php","$installdir/admin/includes/configure.php";
#print "$uid, $gid, \"$installdir/includes/configure.php\",\"$installdir/admin/includes/configure.php<br><br>\n";
} else { 
chmod(0400,"$installdir/includes/configure.php");
chmod(0400,"$installdir/admin/includes/configure.php");
}

system("rm -rf $installdir/cpanel-upgrade");
system("rm -rf $installdir/install");

print "<b>Complete!</b><br><br>\n";
print "<br>You can access your upgraded osCommerce at <a href=\"http://$domain/$installpath/\">http://${domain}${installpath}/</a><br>\n";
print "<br>Or log into the admin area at <a href=\"http://$domain/$installpath/admin/\">http://${domain}${installpath}/admin/</a><br>\n";
print "<br><br>\n";

exit(0);

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);
}

