#!/usr/bin/perl
#
#Copyright (c) 2001-2004 cPanel, Inc.
#Copyright (c) 2004 Lifeless People Networks.
#
#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.
#
############################################################################################
#PHP-Nuke Open Installer CORE1 by http://www.openinstaller.com/
#
#See http://www.openinstaller.com/ for the latest updates to this installer.
############################################################################################
                                                                                                                             
BEGIN {
	push(@INC,"/usr/local/cpanel");
}

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%';

##Begin Sitekey Generation##
$lower=1000; 
$upper=2000000; 
$random = int(rand( $upper-$lower+1 ) ) + $lower; 
$random2 = int(rand( $upper-$lower+10 ) ) + $lower;

require Digest::MD5;
$md5 = Digest::MD5->new;
$md5->add($random);
$hashedrandom = $md5->hexdigest;

$md52 = Digest::MD5->new;
$md52->add($random2);
$hashedrandom2 = $md52->hexdigest;
##End Sitekey Generation##

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

system("mysql -u${sqluser} -p${sqlpass} ${sqldb} < $installdir/pkg/sql/nuke.sql");

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

regsrep("$installdir/config.php","dbhost = \"","\$dbhost='$sqlserver';");
regsrep("$installdir/config.php","dbuname = \"","\$dbuname='$sqluser';");
regsrep("$installdir/config.php","dbpass = \"","\$dbpass='$sqlpass';");
regsrep("$installdir/config.php","dbname = \"","\$dbname='$sqldb';");
regsrep("$installdir/config.php","sitekey = \"","\$sitekey = '$hashedrandom\+$hashedrandom2\!';");
regsrep("$installdir/config.php","domainname = \"","\$domainname = 'www.$domain';");
unlink("$installdir/config.php.tmpeditlib");
system("rm","-rf","$installdir/pkg/");
if (hasphpsuexec() eq "0") {
chmod(0777,"$installdir/ultramode.txt");
}

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

print "<br>You can access your new PHP Nuke installation at: <a href=\"http://$domain$installpath/\">http://${domain}${installpath}/</a><br>You should setup a superuser right away.\n";
print "<br><br>\n";

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

