
# script_gallery_desc()
sub script_gallery_desc
{
return "Gallery";
}

sub script_gallery_uses
{
return ( "php" );
}

sub script_gallery_longdesc
{
return "Gallery is a web based software product that lets you manage your photos";
}

# script_gallery_versions()
sub script_gallery_versions
{
return ( "3.1.5" );
}

sub script_gallery_category
{
return "Photos";
}

sub script_gallery_php_vers
{
return ( 5 );
}

sub script_gallery_php_modules
{
return ("mysql", "xml", "mbstring", "json");
}

sub script_gallery_dbs
{
local ($d, $ver) = @_;
return ("mysql");
}

# script_gallery_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing PHP-NUKE
sub script_gallery_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 Gallery 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" ]);
	$rv .= &ui_table_row("Database for Gallery tables",
		     &ui_database_select("db", undef, \@dbs, $d, "gallery"));
	$rv .= &ui_table_row("Install sub-directory under <tt>$hdir</tt>",
			     &ui_opt_textbox("dir", &substitute_scriptname_template("gallery", $d), 30, "At top level"));
	}
return $rv;
}

# script_gallery_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_gallery_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_gallery_check(&domain, version, &opts, &upgrade-info)
# Returns an error message if a required option is missing or invalid
sub script_gallery_check
{
local ($d, $ver, $opts, $upgrade) = @_;
$opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
if (-r "$opts->{'dir'}/index.php") {
	return "Gallery appears to be already installed in the selected directory";
	}
return undef;
}

# script_gallery_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by Gallery, each of which is a hash ref
# containing a name, filename and URL
sub script_gallery_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files = ( { 'name' => "source",
	   'file' => "gallery-$ver.zip",
	   'url' => "https://github.com/bwdutton/gallery3/archive/refs/tags/$ver.zip" } );
return @files;
}

sub script_gallery_commands
{
return ("unzip");
}

# script_gallery_install(&domain, version, &opts, &files, &upgrade-info)
# Actually installs Gallery, and returns either 1 and an informational
# message, or 0 and an error
sub script_gallery_install
{
local ($d, $version, $opts, $files, $upgrade) = @_;
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 = &mysql_user($d);
local $dbpass = &mysql_pass($d);
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);

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

# Create files for writing by webserver
&run_as_domain_user($d, "touch ".quotemeta("$opts->{'dir'}/.htaccess"));
&make_file_php_writable($d, "$opts->{'dir'}/.htaccess");
&run_as_domain_user($d, "touch ".quotemeta("$opts->{'dir'}/config.php"));
&make_file_php_writable($d, "$opts->{'dir'}/config.php");
&run_as_domain_user($d, "mkdir ".quotemeta("$opts->{'dir'}/albums"));
&make_file_php_writable($d, "$opts->{'dir'}/albums", 1);

# Return a URL for the user
my $userurl = &script_path_url($d, $opts);
my $url = $upgrade ? $userurl : $userurl."installer/index.php";
my $rp = $opts->{'dir'};
my $db_msg = $upgrade ? "" : "<br>For filling database credentials use for host <tt>$dbhost</tt>, for name <tt>$dbname</tt>, for user <tt>$dbuser</tt> and for password use <tt>$dbpass</tt>.";
my $post_msg = $upgrade ? "start using Gallery" : "complete Gallery installation process";
$rp =~ s/^$d->{'home'}\///;
return (1, "Go to <a target=_blank href='$url'>$url</a> to $post_msg.$db_msg", "Under $rp", $userurl);
}

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

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

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

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

sub script_gallery_db_conn_desc
{
my $db_conn_desc = 
    { 'var/database.php' =>
        {
           'dbpass' =>
           {
               'func'        => 'php_quotemeta',
               'func_params' => 1,
               'replace'     => [ '[\'"]pass[\'"]\s*=>.*?' =>
                                  '\'pass\' => \'$$sdbpass\',' ],
           },
           'dbuser' =>
           {
               'replace'     => [ '[\'"]user[\'"]\s*=>.*?' =>
                                  '\'user\' => \'$$sdbuser\',' ],
           },
        }
    };
return $db_conn_desc;
}

# script_gallery_php_vars(&domain)
# Returns an array of extra PHP variables needed for this script
sub script_gallery_php_vars
{
return ( [ 'register_globals', 'Off' ],
         [ 'safe_mode', 'Off' ],
         [ 'short_open_tag', 'On' ] );
}

# script_gallery_check_latest()
sub script_gallery_latest
{
return ( "https://github.com/bwdutton/gallery3/releases",
	 "\/([\\d\\.]+)\\.zip" );
}

sub script_gallery_site
{
return 'https://galleryrevival.com/';
}

sub script_gallery_disabled
{
return 0;
}

1;

