#!/usr/bin/perl
#
#Copyright (c) 2001-2004 cPanel, Inc.
#
#This auto install script, "install" 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 installing in $installdir...\n";

system("mv $installdir/pkg/html/* $installdir/");

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

chdir("$installdir/install");

$ENV{'QUERY_STRING'} = '';
$ENV{'QUERY_STRING'} .= "database=mysql&";
$ENV{'QUERY_STRING'} .= "dbhost=${sqlserver}&";
$ENV{'QUERY_STRING'} .= "dbname=${sqldb}&";
$ENV{'QUERY_STRING'} .= "prefix=xoops&";
$ENV{'QUERY_STRING'} .= "dbuname=${sqluser}&";
$ENV{'QUERY_STRING'} .= "dbpass=${sqlpass}&";
$ENV{'QUERY_STRING'} .= "db_pconnect=no&";
$ENV{'QUERY_STRING'} .= "root_path=${installdir}&";
$ENV{'QUERY_STRING'} .= "xoops_url=http://$domain$installpath&";
$ENV{'QUERY_STRING'} .= "adminname=${username}&";
$ENV{'QUERY_STRING'} .= "adminpassword=${password}&";
$ENV{'QUERY_STRING'} .= "adminpassword2=${password}&";
$ENV{'QUERY_STRING'} .= "email=webmaster\@${domain}&";
$ENV{'QUERY_STRING'} .= "op=dbsave";
$ENV{'QUERY_STRING'} = URLEncode($ENV{'QUERY_STRING'});

$postvars = $ENV{'QUERY_STRING'};

$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 . "/install" . "/" . "index.php";
$ENV{'SCRIPT_URI'} = $installpath . "/install" . "/" . "index.php";
$ENV{'SCRIPT_FILENAME'} = "${installdir}/install/index.php";
$ENV{'REDIRECT_STATUS'} = 1;
$ENV{'REQUEST_METHOD'} = "POST";
$ENV{'CONTENT_LENGTH'} = length($ENV{'QUERY_STRING'});
$ENV{'HTTP_STATUS'} = 200;	      
delete $ENV{'QUERY_STRING'};
        if (-e "/var/cpanel/usecpphp") {
              open3(\*PHPW,\*PHPR,"","/usr/local/cpanel/3rdparty/bin/php","index.php");
        } else {
              open3(\*PHPW,\*PHPR,"","/usr/bin/php","index.php");
        }
	print PHPW "$postvars\0";
	close(PHPW);
	while(<PHPR>) {
# print;
	}
	close(PHPR);

$ENV{'QUERY_STRING'} = '';
$ENV{'QUERY_STRING'} .= "database=mysql&";
$ENV{'QUERY_STRING'} .= "dbhost=${sqlserver}&";
$ENV{'QUERY_STRING'} .= "dbname=${sqldb}&";
$ENV{'QUERY_STRING'} .= "prefix=xoops&";
$ENV{'QUERY_STRING'} .= "dbuname=${sqluser}&";
$ENV{'QUERY_STRING'} .= "dbpass=${sqlpass}&";
$ENV{'QUERY_STRING'} .= "db_pconnect=no&";
$ENV{'QUERY_STRING'} .= "root_path=${installdir}&";
$ENV{'QUERY_STRING'} .= "xoops_url=http://$domain$installpath&";
$ENV{'QUERY_STRING'} .= "adminname=${username}&";
$ENV{'QUERY_STRING'} .= "adminpassword=${password}&";
$ENV{'QUERY_STRING'} .= "adminpassword2=${password}&";
$ENV{'QUERY_STRING'} .= "email=webmaster\@${domain}&";
$ENV{'QUERY_STRING'} .= "op=createTables";
$ENV{'QUERY_STRING'} = URLEncode($ENV{'QUERY_STRING'});


$postvars = $ENV{'QUERY_STRING'};

$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 . "/install" . "/" . "index.php";
$ENV{'SCRIPT_URI'} = $installpath . "/install" . "/" . "index.php";
$ENV{'SCRIPT_FILENAME'} = "${installdir}/install/index.php";
$ENV{'REDIRECT_STATUS'} = 1;
$ENV{'REQUEST_METHOD'} = "POST";
$ENV{'CONTENT_LENGTH'} = length($ENV{'QUERY_STRING'});
$ENV{'HTTP_STATUS'} = 200;
delete $ENV{'QUERY_STRING'};
        if (-e "/var/cpanel/usecpphp") {
              open3(\*PHPW,\*PHPR,"","/usr/local/cpanel/3rdparty/bin/php","index.php");
        } else {
              open3(\*PHPW,\*PHPR,"","/usr/bin/php","index.php");
        }
        print PHPW "$postvars\0";
        close(PHPW);
        while(<PHPR>) {
  #print;
        }
        close(PHPR);

$ENV{'QUERY_STRING'} = '';
$ENV{'QUERY_STRING'} .= "database=mysql&";
$ENV{'QUERY_STRING'} .= "dbhost=${sqlserver}&";
$ENV{'QUERY_STRING'} .= "dbname=${sqldb}&";
$ENV{'QUERY_STRING'} .= "prefix=xoops&";
$ENV{'QUERY_STRING'} .= "dbuname=${sqluser}&";
$ENV{'QUERY_STRING'} .= "dbpass=${sqlpass}&";
$ENV{'QUERY_STRING'} .= "db_pconnect=no&";
$ENV{'QUERY_STRING'} .= "root_path=${installdir}&";
$ENV{'QUERY_STRING'} .= "xoops_url=http://$domain$installpath&";
$ENV{'QUERY_STRING'} .= "adminname=${username}&";
$ENV{'QUERY_STRING'} .= "adminpass=${password}&";
$ENV{'QUERY_STRING'} .= "adminpass2=${password}&";
$ENV{'QUERY_STRING'} .= "adminmail=webmaster\@${domain}&";
$ENV{'QUERY_STRING'} .= "op=insertData";
$ENV{'QUERY_STRING'} = URLEncode($ENV{'QUERY_STRING'});


$postvars = $ENV{'QUERY_STRING'};

$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 . "/install" . "/" . "index.php";
$ENV{'SCRIPT_URI'} = $installpath . "/install" . "/" . "index.php";
$ENV{'SCRIPT_FILENAME'} = "${installdir}/install/index.php";
$ENV{'REDIRECT_STATUS'} = 1;
$ENV{'REQUEST_METHOD'} = "POST";
$ENV{'CONTENT_LENGTH'} = length($ENV{'QUERY_STRING'});
$ENV{'HTTP_STATUS'} = 200;
delete $ENV{'QUERY_STRING'};

print "<br><br>";
        if (-e "/var/cpanel/usecpphp") {
              open3(\*PHPW,\*PHPR,"","/usr/local/cpanel/3rdparty/bin/php","index.php");
        } else {
              open3(\*PHPW,\*PHPR,"","/usr/bin/php","index.php");
        }
        print PHPW "$postvars\0";
        close(PHPW);
        while(<PHPR>) {
   #     print;
        }
        close(PHPR);


system("rm -rf $installdir/pkg");
system("rm -rf $installdir/install");
system("rm -rf $installdir/upgrade");
if (hasphpsuexec() eq "0") {
chmod(0777,"$installdir/uploads");
chmod(0777,"$installdir/cache");
chmod(0777,"$installdir/templates_c");
}
chmod(0444,"$installdir/mainfile.php");

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

print "<br><br>You can now access your new Xoops CMS here: <a 
href=\"http://$domain$installpath/\">http://${domain}${installpath}/</a><br>\n";
print "<br>Here is your administrator login information:\n";
print "<br><b>Administrator Username: $username</b>\n";
print "<br><b>Administrator Password: $password</b>\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);
}

