
# script_ioncube_desc()
sub script_ioncube_desc
{
return "ionCube";
}

sub script_ioncube_uses
{
return ( "php" );
}

sub script_ioncube_longdesc
{
return "IonCube is a PHP module extension that loads encrypted PHP files and speeds up webpages that are being displayed";
}

# script_ioncube_versions()
sub script_ioncube_versions
{
return ( "12.0.5" );
}

sub script_ioncube_release
{
return 4;	# Support aarch64
}

sub script_ioncube_category
{
return "";
}

sub script_ioncube_overlap
{
return 1;	# Other scripts can be installed in the same dir
}

sub script_ioncube_php_vers
{
return ( 5 );
}

# script_ioncube_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing ionCube
sub script_ioncube_params
{
local ($d, $ver, $upgrade) = @_;
return "";
}

# script_ioncube_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_ioncube_parse
{
local ($d, $ver, $in, $upgrade) = @_;
return { 'path' => '/',
	 'path_real' => '~/etc',
	 'nofcount' => 1,
	 'dir' => &public_html_dir($d) };
}

# script_ioncube_check(&domain, version, &opts, &upgrade-info)
# Returns an error message if a required option is missing or invalid
sub script_ioncube_check
{
local ($d, $ver, $opts, $upgrade) = @_;

# Check for PHP mode
&get_domain_php_mode($d) eq "mod_php" &&
	return "ionCube cannot be installed when PHP is being run via mod_php";

# Check if ioncube loader can be found
local $io = &script_ioncube_get_ioncube_type();
$io || return "No ionCube loader for your operating system and CPU ".
	      "architecture could be found";

return undef;
}

# script_ioncube_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by ionCube, each of which is a hash ref
# containing a name, filename and URL
sub script_ioncube_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files;
local $io = &script_ioncube_get_ioncube_type();
push(@files, { 'name' => "ioncube",
	       'file' => "ioncube_loaders_$io.zip",
	       'nocache' => 1,
	       'url' => "http://downloads.ioncube.com/".
			"loader_downloads/ioncube_loaders_$io.zip" });
return @files;
}

sub script_ioncube_get_ioncube_type
{
local $io;
local $arch = &backquote_command("uname -m");
if ($gconfig{'os_type'} eq 'solaris' && $arch =~ /sparc/) {
	$io = "sun_sparc";
	}
elsif ($gconfig{'os_type'} eq 'solaris' && $arch =~ /86/) {
	$io = "sun_x86";
	}
elsif ($gconfig{'os_type'} eq 'freebsd' && $arch =~ /64/) {
	$io = "fre_".int($gconfig{'os_version'})."_x86-64";
	}
elsif ($gconfig{'os_type'} eq 'freebsd' && $arch !~ /64/) {
	$io = "fre_".int($gconfig{'os_version'})."_x86";
	}
elsif ($gconfig{'os_type'} eq 'macos' && $arch =~ /64/) {
	$io = "dar_x86-64";
	}
elsif ($gconfig{'os_type'} eq 'macos' && $arch !~ /64/) {
	$io = "dar_x86";
	}
elsif ($gconfig{'os_type'} =~ /-linux/ && $arch =~ /x86_64/) {
	$io = "lin_x86-64";
	}
elsif ($gconfig{'os_type'} =~ /-linux/ && $arch =~ /i[0-9]86/) {
	$io = "lin_x86";
	}
elsif ($gconfig{'os_type'} =~ /-linux/ && $arch =~ /ppc/) {
	$io = "lin_ppc";
	}
elsif ($gconfig{'os_type'} =~ /-linux/ && $arch =~ /aarch64/) {
	$io = "lin_aarch64";
	}
return $io;
}

sub script_ioncube_commands
{
return ("unzip");
}

# script_ioncube_install(&domain, version, &opts, &files, &upgrade-info,
#			username, password)
# Actually installs ionCube, and returns either 1 and an informational
# message, or 0 and an error
sub script_ioncube_install
{
local ($d, $version, $opts, $files, $upgrade, $domuser, $dompass) = @_;

# Extract ioncube loader
local $iotemp = &transname();
local $err = &extract_script_archive($files->{'ioncube'}, $iotemp, $d);
$err && return (0, "Failed to extract ionCube files : $err");
local $io = &script_ioncube_get_ioncube_type();
local $phpver = $opts->{'phpver'} || 5;
local $phpfullver = &get_php_version($phpver);
$phpfullver =~ s/^(\d+\.\d+)\..*$/$1/;
local ($sofile) = glob("$iotemp/ioncube/ioncube_loader_*_$phpfullver.so");
$sofile ||
    return (0, "No ionCube loader for PHP version $phpfullver found in file");

# Copy loader to ~/etc , adjust php.ini
local $inifile = &get_domain_php_ini($d, $phpver);
$inifile && -r $inifile || return (0, "PHP configuration file was not found!");
$sofile =~ /\/([^\/]+)$/;
local $sodest = "$d->{'home'}/etc/$1";
&copy_source_dest_as_domain_user($d, $sofile, $sodest);
&foreign_require("phpini", "phpini-lib.pl");
local $conf = &phpini::get_config($inifile);
local @allzends = grep { $_->{'name'} eq 'zend_extension' } @$conf;
local @zends = grep { $_->{'enabled'} } @allzends;
local ($got) = grep { $_->{'value'} eq $sodest } @zends;
if (!$got) {
	# Needs to be enabled
	local $lref = &read_file_lines($inifile);
	if (@zends) {
		# After current extensions
		splice(@$lref, $zends[$#zends]->{'line'}+1, 0,
		       "zend_extension=$sodest");
		}
	elsif (@allexts) {
		# After commented out extensions
		splice(@$lref, $allzends[$#allzends]->{'line'}+1, 0,
		       "zend_extension=$sodest");
		}
	else {
		# At end of file (should never happen, but..)
		push(@$lref, "zend_extension=$sodest");
		}
	&write_as_domain_user($d,
		sub { &flush_file_lines($inifile) });
	undef($phpini::get_config_cache{$inifile});
	}

local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
return (1, "ionCube installation complete. It will be used by any other PHP ".
	   "scripts installed into this domain.", "Under $rp");
}

# script_ioncube_uninstall(&domain, version, &opts)
# Un-installs a ionCube installation, by deleting the directory and database.
# Returns 1 on success and a message, or 0 on failure and an error
sub script_ioncube_uninstall
{
local ($d, $version, $opts) = @_;

# Delete loader file from ~/etc
local $phpver = $opts->{'phpver'} || 5;
local $phpfullver = &get_php_version($phpver);
$phpfullver =~ s/^(\d+\.\d+)\..*$/$1/;
local ($sodest) = glob("$d->{'home'}/etc/ioncube_loader_*_$phpfullver.so");
if (-r $sodest) {
	&unlink_file_as_domain_user($d, $sodest);
	}

# Turn off in php.ini
&foreign_require("phpini", "phpini-lib.pl");
local $inifile = &get_domain_php_ini($d, 5);
local $conf = &phpini::get_config($inifile);
local @allzends = grep { $_->{'name'} eq 'zend_extension' } @$conf;
local @zends = grep { $_->{'enabled'} } @allzends;
local ($got) = grep { $_->{'value'} eq $sodest } @zends;
if ($got) {
	local $lref = &read_file_lines($inifile);
	splice(@$lref, $got->{'line'}, 1);
	&write_as_domain_user($d,
		sub { &flush_file_lines($inifile) });
	undef($phpini::get_config_cache{$inifile});
	}

return (1, "ionCube files deleted.");
}

sub script_ioncube_latest
{
local ($ver) = @_;
if ($ver >= 10) {
	return ( "https://www.ioncube.com/loaders.php",
		 "<td>\\s+([0-9]+\\.[0-9\\.]+)\\s");
	}
else {
	return ( "https://www.ioncube.com/loaders.php",
		 "<td>\\s+([0-6]\\.[0-9\\.]+)\\s");
	}
}

sub script_ioncube_site
{
return 'https://www.ioncube.com/loaders.php';
}

sub script_ioncube_passmode
{
return 0;
}

1;

