#!/usr/bin/perl
#
#Copyright (c) 2001-2004 cPanel, Inc.
#
#This auto install script, "upgrade" is hereinafter referred to as the "Software."  No other parts of this package
#will be considered, infered or implied to be part of the the "Software."
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
#documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
#rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
#permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
#Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
#WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

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

use IPC::Open3;
use SafeFile;
require '/scripts/regsrep.pl';

#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%';
my $system = `uname -s`;
chomp($system);

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

system("mv","-f","${installdir}/admin/config.inc.php","${installdir}/admin/config.inc.php.up");
if ($system =~ /freebsd/i) {
	system("cp -Rpf $installdir/cpanel-upgrade/pkg/* $installdir/");
} else {
	system("cp -af $installdir/cpanel-upgrade/pkg/* $installdir/");
}
system("rm","-rf","$installdir/cpanel-upgrade");

open(OLDCONF,"<","${installdir}/admin/config.inc.php.up");
while(<OLDCONF>) {
	if (/GB_DB\[\"dbName\"\][\t\s]+=[\t\s]+\"([^\"]+)\"/) {
		$sqldb = $1;
	} elsif (/GB_DB\[\"host\"\][\t\s]+=[\t\s]+\"([^\"]+)\"/) {
		$sqlserver = $1;
	} elsif (/GB_DB\[\"user\"\][\t\s]+=[\t\s]+\"([^\"]+)\"/) {
		$sqluser = $1;
	} elsif (/GB_DB\[\"pass\"\][\t\s]+=[\t\s]+\"([^\"]+)\"/) {
		$sqlpass = $1;
	}
}
close(OLDCONF);
unlink("${installdir}/admin/config.inc.php.up");
unlink("${installdir}/install.php");


regsrep("$installdir/admin/config.inc.php","GB_DB\\[\"dbName","\$GB_DB[\"dbName\"] = \"$sqldb\";");
regsrep("$installdir/admin/config.inc.php","GB_DB\\[\"host","\$GB_DB[\"host\"] = \"$sqlserver\";");
regsrep("$installdir/admin/config.inc.php","GB_DB\\[\"user","\$GB_DB[\"user\"] = \"$sqluser\";");
regsrep("$installdir/admin/config.inc.php","GB_DB\\[\"pass","\$GB_DB[\"pass\"] = \"$sqlpass\";");
regsrep("$installdir/admin/config.inc.php",'GB_PG\\[\"base\_url\"\][\s\t]+=[\s\t]+""\;',"\$GB_PG[\"base_url\"] = \"http://${domain}${installpath}\";");

if (hasphpsuexec() eq "0") {
system("chmod","0777","$installdir/tmp");
system("chmod","0777","$installdir/public");
}

system("mysql -u${sqluser} -p${sqlpass} -h${sqlserver} -D${sqldb} < $installdir/doc/db_update_v22-23.sql");
system("mysql -u${sqluser} -p${sqlpass} -h${sqlserver} -D${sqldb} < /usr/local/cpanel/addons/AdvancedGuestBook/update22-23.sql");


unlink("${installdir}/admin/config.inc.php.tmpeditlib");

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

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

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);
}
sub hasphpsuexec {
        my $hasphpsuexec = 0;
        open(SUEXEC,"<","/usr/local/apache/bin/suexec");
        while(<SUEXEC>) {
                if (/php /i) {
                        $hasphpsuexec = 1;
                }
        }
        close(SUEXEC);
        return($hasphpsuexec);
}

