# script_typo3_desc()
sub script_typo3_desc
{
return "TYPO3 CMS";
}

sub script_typo3_uses
{
return ( "php" );
}

sub script_typo3_longdesc
{
return "TYPO3 CMS is a free and open-source web content management system";
}

# script_typo3_versions()
sub script_typo3_versions
{
return ( "12.0.0", "11.5.19", "10.4.34", "9.5.31" );
}

sub script_typo3_version_desc
{
local ($ver) = @_;
return &compare_versions($ver, 11) < 0 ? "$ver (LTS)" : "$ver";
}

# script_typo3_can_upgrade(&sinfo, newver)
sub script_typo3_can_upgrade
{
local ($sinfo, $newver) = @_;
if ($newver >= 6 && $sinfo->{'version'} < 6) {
	# Cannot upgrade from 4.x to 6.x
	return 0;
	}
return 1;
}

sub script_typo3_category
{
return "CMS";
}

sub script_typo3_php_vers
{
return ( 5 );
}

sub script_typo3_php_modules
{
return ("mysql", "gd", "soap", "zip", "json",
        "xml", "mbstring", "fileinfo", "intl");
}

sub script_typo3_php_vars
{
return ( [ 'memory_limit', '256M', '+' ],
         [ 'max_execution_time', '240' ],
         [ 'max_input_vars', '2000' ],
       );
}

sub script_typo3_dbs
{
return ("mysql", "postgres");
}

# script_typo3_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing TYPO3 CMS
sub script_typo3_params
{
local ($d, $ver, $upgrade) = @_;
local $rv;
local $hdir = &public_html_dir($d, 1);
if ($upgrade) {
	# Options are fixed when upgrading
	local ($dbtype, $dbname) = split(/_/, $upgrade->{'opts'}->{'db'}, 2);
	$rv .= &ui_table_row("Database for TYPO3 CMS tables", $dbname);
	local $dir = $upgrade->{'opts'}->{'dir'};
	$dir =~ s/^$d->{'home'}\///;
	$rv .= &ui_table_row("Install directory", $dir);
	}
else {
	# Show editable install options
	local @dbs = &domain_databases($d, [ "mysql", "postgres" ]);
	$rv .= &ui_table_row("Database for TYPO3 CMS tables",
		     &ui_database_select("db", undef, \@dbs, $d, "typo3"));
	$rv .= &ui_table_row("Install sub-directory under <tt>$hdir</tt>",
			     &ui_opt_textbox("dir", &substitute_scriptname_template("typo3", $d), 30, "At top level"));
	}
return $rv;
}

# script_typo3_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_typo3_parse
{
local ($d, $ver, $in, $upgrade) = @_;
if ($upgrade) {
	# Options are always the same
	return $upgrade->{'opts'};
	}
else {
	local $hdir = &public_html_dir($d, 0);
	$in{'dir_def'} || $in{'dir'} =~ /\S/ && $in{'dir'} !~ /\.\./ ||
		return "Missing or invalid installation directory";
	local $dir = $in{'dir_def'} ? $hdir : "$hdir/$in{'dir'}";
	local ($newdb) = ($in->{'db'} =~ s/^\*//);
	return { 'db' => $in->{'db'},
		 'newdb' => $newdb,
	         'dir' => $dir,
		 'path' => $in{'dir_def'} ? "/" : "/$in{'dir'}", };
	}
}

# script_typo3_check(&domain, version, &opts, &upgrade-info)
# Returns an error message if a required option is missing or invalid
sub script_typo3_check
{
local ($d, $ver, $opts, $upgrade) = @_;
$opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
$opts->{'db'} || return "Missing database";
if (-r "$opts->{'dir'}/typo3conf/localconf.php") {
	return "TYPO3 CMS appears to be already installed in the selected directory";
	}
local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
my @typo3_tables = qw(be_groups be_sessions be_users cache_extensions cache_hash);
foreach my $t (@typo3_tables) {
	local $clash = &find_database_table($dbtype, $dbname, $t);
	$clash && return "TYPO3 CMS appears to be already using the selected database (table <tt>$clash</tt> already exists)";
	}
return undef;
}

# script_typo3_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by TYPO3 CMS, each of which is a hash ref
# containing a name, filename and URL
sub script_typo3_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files;
push(@files, { 'name' => "source",
	       'file' => "typo3-$ver.tar.gz",
	       'url' => "https://get.typo3.org/$ver/tar.gz" });
return @files;
}

sub script_typo3_commands
{
return ("tar", "gunzip", "wget");
}

# script_typo3_install(&domain, version, &opts, &files, &upgrade-info)
# Actually installs TYPO3 CMS, and returns either 1 and an informational
# message, or 0 and an error
sub script_typo3_install
{
local ($d, $version, $opts, $files, $upgrade, $domuser, $dompass) = @_;
local ($out, $ex);
if ($opts->{'newdb'} && !$upgrade) {
	local $err = &create_script_database($d, $opts->{'db'});
	return (0, "Database creation failed : $err") if ($err);
	}
local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
local $dbuser = $dbtype eq "mysql" ? &mysql_user($d) : &postgres_user($d);
local $dbpass = $dbtype eq "mysql" ? &mysql_pass($d) : &postgres_pass($d, 1);
local $dbphptype = $dbtype eq "mysql" ? "mysql" : "psql";
local $dbhost = &get_database_host($dbtype, $d);
local $dberr = &check_script_db_connection($dbtype, $dbname, $dbuser, $dbpass);
return (0, "Database connection failed : $dberr") if ($dberr);

# Preserve old config file (for versions 4.x only)
local $cfile = "$opts->{'dir'}/typo3conf/localconf.php";
local $oldcfile = &transname();
if ($upgrade && -r $cfile) {
	&copy_source_dest($cfile, $oldcfile);
	}

# Extract tar file to temp dir and copy to target
local $temp = &transname();
local $err = &extract_script_archive($files->{'source'}, $temp, $d,
                                     $opts->{'dir'}, "typo3_src-$ver");
$err && return (0, "Failed to extract source : $err");

# Make files and sub-directories writable
foreach my $dir ("fileadmin", "typo3conf", "typo3temp", "uploads") {
	&make_file_php_writable($d, "$opts->{'dir'}/$dir");
	}

local $url = &script_path_url($d, $opts);

# Create flag file to enable installer
local $iflag = $version < 6 ? "$opts->{'dir'}/typo3conf/ENABLE_INSTALL_TOOL"
			    : "$opts->{'dir'}/FIRST_INSTALL";
&open_tempfile_as_domain_user($d, IFLAG, ">$iflag", 0, 1);
&close_tempfile_as_domain_user($d, IFLAG);
#push(@main::temporary_files, $iflag);

# Rename _.htaccess file
if (-r "$opts->{'dir'}/_.htaccess") {
	&rename_as_domain_user($d, "$opts->{'dir'}/_.htaccess",
				   "$opts->{'dir'}/.htaccess");
	}

if ($upgrade) {
	# Put back original config file
	if (-r $oldcfile) {
		&copy_source_dest_as_domain_user($d, $oldcfile, $cfile);
		}
	&unlink_file_as_domain_user($d, $iflag);
	}

# Remove installer flag file and install folder

# Tell the user about the new install
local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
local $adminurl = $url."typo3/install.php";
return (1, "Initial TYPO3 CMS installation complete. Go to <a href='$adminurl' target='_blank'>$adminurl</a> to complete the installation process.<br>For filling database credentials use for user <tt>$dbuser</tt> and for password use <tt>$dbpass</tt>. The database name is <tt>$dbname</tt>.", "Under $rp", $url);
}

# script_typo3_uninstall(&domain, version, &opts)
# Un-installs a TYPO3 CMS installation, by removing it's files
# Returns 1 on success and a message, or 0 on failure and an error
sub script_typo3_uninstall
{
local ($d, $version, $opts) = @_;


# Remove the contents of the target directory
local $derr = &delete_script_install_directory($d, $opts);
return (0, $derr) if ($derr);

# Remove typo3 tables from the database
&cleanup_script_database($d, $opts->{'db'}, '(.*)');

# Take out the DB
if ($opts->{'newdb'}) {
	&delete_script_database($d, $opts->{'db'});
	}

return (1, "Deleted TYPO3 CMS directory and tables.");
}

# script_typo3_latest(version)
sub script_typo3_latest
{
my ($ver) = @_;
return ( "https://get.typo3.org/#download",
	 $ver >= 11 ? "Version:\\s*<.*?>([\\d.]+)<\/.*?>" :
	 $ver >= 10 ? "Version:\\s*<.*?>(10\\.[\\d.]+)<\/.*?>" :
	              "Version:\\s*<.*?>(9\\.[\\d.]+)<\/.*?>" );
}

sub script_typo3_site
{
return 'http://www.typo3.com/';
}

1;

