File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/phpextensionmgr
#!/usr/bin/perl
# cpanel - phpextensionmgr Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use warnings;
use Cpanel::OSSys ();
use Cpanel::HttpRequest ();
use Cpanel::SafeRun ();
use Cpanel::SafeDir ();
use Cpanel::CPAN::Digest::MD5::File ('file_md5_hex');
use Getopt::Long;
use Pod::Usage;
use YAML::Syck;
my $help;
my $prefix;
my $action;
my $extension;
my %extensions = (
'Zendopt' => 'binary',
'PHPSuHosin' => 'source',
'IonCubeLoader' => 'binary',
'SourceGuardian' => 'binary',
'EAccelerator' => 'source',
);
GetOptions( 'help' => \$help, 'prefix=s' => \$prefix );
$action = shift || ( $help = 1 );
$action = lc($action);
$extension = shift || ( $help = 1 );
if ( $action eq 'list' ) {
print "Available Extensions:\n";
print join( "\n", keys %extensions );
print "\n";
exit;
}
elsif ( $action ne 'status' && $action ne 'install' && $action ne 'uninstall' ) {
print "Invalid action specified\n";
$help = 1;
}
pod2usage(1) if $help;
if ( $> != 0 ) {
print "This script must be run by the root user.\n";
exit 1;
}
# check that extension is in list
my $found = 0;
foreach my $avail_ext ( keys %extensions ) {
if ( lc($extension) eq lc($avail_ext) ) {
$extension = $avail_ext;
$found = 1;
last;
}
}
unless ($found) {
print "Extension ${extension} is not available\n";
exit 1;
}
my $tarball_filename = get_tarball_filename( $extension, \%extensions );
# setup working directory
my $home = -d '/usr/home' ? '/usr/home' : '/home';
mkdir $home . '/cpeasyapache' unless -d $home . '/cpeasyapache';
mkdir $home . '/cpeasyapache/phpextensions' unless -d $home . '/cpeasyapache/phpextensions';
unless ( -d $home . '/cpeasyapache/phpextensions' ) {
die "Could not mkdir $home/cpeasyapache/phpextensions";
}
chdir $home . '/cpeasyapache/phpextensions' || die "Could not chdir to $home/cpeasyapache/phpextensions: $!";
# download tarball
download_tarball_if_needed($tarball_filename);
# make certain php.ini is available for isntallations
if ( $action eq 'install' ) {
if ($prefix) {
system( '/scripts/updatephpconf', $prefix );
}
else {
system('/scripts/updatephpconf');
}
}
# untar
my $file_list = Cpanel::SafeRun::saferunnoerror( 'tar', 'tfz', $tarball_filename );
my @FILES = split( /\n/, $file_list, 2 );
my $extension_dir = $FILES[0];
unless ( $extension_dir =~ m{[^\./]+[^/]*/?} ) {
die "Extension directory in downloaded tarball appears to be invalid: $extension_dir";
}
if ( -d $extension_dir ) {
# unlink the exististing directory
Cpanel::SafeDir::safermdir($extension_dir);
}
Cpanel::SafeRun::saferunallerrors( 'tar', 'xzf', $tarball_filename );
# cd
chdir $extension_dir || die "Could not chdir into $extension_dir: $!";
# execute ./cpanel-install (prefix)
my @cmd = ();
if ( $action eq 'install' ) {
print "Installing $extension\n";
@cmd = ('./cpanel-install');
}
elsif ( $action eq 'uninstall' ) {
print "Uninstalling $extension\n";
@cmd = ('./cpanel-uninstall');
}
else {
print "Determining status of $extension\n";
@cmd = ('./cpanel-status');
}
push @cmd, $prefix if $prefix;
system(@cmd);
# cleanup?
sub get_tarball_filename {
my $extension = shift;
my $extensions_ref = shift;
my $filename = $extension . '.pm.tar.gz';
if ( $extensions_ref->{$extension} eq 'source' ) {
return $filename;
}
my ( $system, $release, $machine ) = ( Cpanel::OSSys::uname() )[ 0, 2, 4 ];
if ( $system =~ /freebsd/i ) {
$system = 'freebsd';
if ( $release =~ m/^5/ ) {
$release = '5';
$machine = '32';
}
elsif ( $release =~ m/^([678])/ ) {
$release = $1;
if ( $machine =~ m/64/ ) {
$machine = '64';
}
else {
$machine = '32';
}
}
else {
$release = '4';
$machine = '32';
}
}
else {
$system = 'linux';
$release = 'generic';
if ( $machine =~ /64/i ) {
$machine = '64';
}
else {
$machine = '32';
}
}
return $filename . ".$system-$release-$machine";
}
sub download_tarball_if_needed {
my $tarball_filename = shift;
my $httpClient = Cpanel::HttpRequest->new( 'hideOutput' => 0 );
if ( !-e 'targz.yaml' || ( stat(_) )[9] < time() - 24 * 60 * 60 ) {
print "Updating md5sum list\n";
$httpClient->download( 'http://httpupdate.cpanel.net/cpanelsync/easy/targz.yaml', 'targz.yaml' );
die "Could not download targz.yaml" unless -e 'targz.yaml';
system('touch targz.yaml'); # make sure mtime is updated
}
my $targz_md5s = YAML::Syck::LoadFile('targz.yaml') || die "Couldn't load targz.yaml: $!";
my $md5sum;
foreach my $key ( keys %{$targz_md5s} ) {
if ( $key =~ /\/\Q$tarball_filename\E$/ ) {
$md5sum = $targz_md5s->{$key};
last;
}
}
die "Could not determine md5sum for $tarball_filename" unless $md5sum;
if ( -e $tarball_filename && file_md5_hex($tarball_filename) eq $md5sum ) {
# Local copy available...good to go.
return;
}
elsif ( -e '/var/cpanel/perl/easy/Cpanel/Easy/' . $tarball_filename && file_md5_hex( '/var/cpanel/perl/easy/Cpanel/Easy/' . $tarball_filename ) eq $md5sum ) {
# EA3 copy available
unlink $tarball_filename if ( -e $tarball_filename );
system( 'cp', '/var/cpanel/perl/easy/Cpanel/Easy/' . $tarball_filename, $tarball_filename );
}
else {
print "Downloading tarball for $extension\n";
$httpClient->download( "http://httpupdate.cpanel.net/cpanelsync/easy/targz/Cpanel/Easy/${tarball_filename}", $tarball_filename );
die "Could not download $tarball_filename" unless -e $tarball_filename;
die "$tarball_filename md5sum mismatch" if ( file_md5_hex($tarball_filename) ne $md5sum );
}
}
__END__
=head1 NAME
phpextensionmgr - Installer/Uninstaller for common PHP extensions
=head1 SYNOPSIS
phpextensionmgr [options] [action] [extension]
Options:
--help Help message
--prefix Installation prefix for PHP (normally /usr/local or /usr/local/php4)
Actions:
install Install or update the extension
uninstall Uninstall the extension
status Display the installation status of the extension
list Show available extensions
=cut