#!/usr/bin/perl
#
#Copyright (c) 2001-2005 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;
use strict;
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 $mysqlversion = '%mysql-version%';

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

system("mv -f $installdir/pkg/* $installdir/");

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


chdir("${installdir}/install");

my(@BVARS);
if (${mysqlversion} eq "3") {
   push(@BVARS,"dbms=mysql");
} else {
   push(@BVARS,"dbms=mysql4");
}
push(@BVARS,"upgrade=0");
push(@BVARS,"language=english");
push(@BVARS,"dbhost=${sqlserver}");
push(@BVARS,"dbname=${sqldb}");
push(@BVARS,"dbuser=${sqluser}");
push(@BVARS,"dbpasswd=${sqlpass}");
push(@BVARS,"prefix=phpbb_");
push(@BVARS,"email=admin\@${domain}");
push(@BVARS,"board_email=admin\@${domain}");
push(@BVARS,"server_name=${domain}");
push(@BVARS,"server_port=80");
push(@BVARS,"script_path=${installpath}/");
push(@BVARS,"admin_name=${username}");
push(@BVARS,"admin_pass1=${password}");
push(@BVARS,"admin_pass2=${password}");
push(@BVARS,"install_step=1");
push(@BVARS,"cur_lang=english");

my $ct = join("&", @BVARS);;
my $ok = 0;

$ENV{'PATH_TRANSLATED'} = './install.php';
$ENV{'REQUEST_METHOD'} = 'POST';
delete $ENV{'QUERY_STRING'};
$ENV{'CONTENT_LENGTH'} = length($ct);
$ENV{'CONTENT_TYPE'} = 'application/x-www-form-urlencoded';
my $pid;
if (-e "/var/cpanel/usecpphp") {
   $pid = open3(\*WTR, \*RDR, "", "/usr/local/cpanel/3rdparty/bin/php", "install.php");
} else {
   $pid = open3(\*WTR, \*RDR, "", "/usr/bin/php", "install.php");
}	
print WTR "$ct";
close(WTR);
while(<RDR>) {
   if (-e "/var/cpanel/phpinstalldebug") {
      print;
   }
   if (/is complete/i)  {
      $ok = 1;
   }
}
close(RDR);


print "<br><br>";

system("rm","-rf","$installdir/install");
system("rm","-rf","$installdir/pkg");
system("rm","-rf","$installdir/contrib");
if (hasphpsuexec() eq "0") {
   chmod(0777,"$installdir/cache");
   chmod(0777,"$installdir/images/avatars");
}

if ($ok) { 
   print "<b>The Install is done!</b>\n"; 
   print "<br>You can access your new phpBB forum at: <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";
} else {
   print "<b>There was a problem with the install!</b>\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);
}
