Index: gsdl/trunk/cgi-bin/gliserver.pl
===================================================================
--- gsdl/trunk/cgi-bin/gliserver.pl	(revision 15928)
+++ gsdl/trunk/cgi-bin/gliserver.pl	(revision 16467)
@@ -1,8 +1,10 @@
 #!/usr/bin/perl -w
-#!perl -w
 # Need to specify the full path of Perl above
 
+# This file merges Michael Dewsnip's gliserver.pl for GS2 and Quan Qiu's gliserver4gs3.pl (GS3)
 
 use strict;
+no strict 'subs';
+no strict 'refs'; # allow filehandles to be variables and viceversa
 
 
@@ -10,15 +12,19 @@
 my $iis6_mode = 0;
 
-
+##
 # IIS 6: for some reason, IIS runs this script with the working directory set to the Greenstone
 #   directory rather than the cgi-bin directory, causing lots of stuff to fail
 if ($iis6_mode)
 {
-    # Change into cgi-bin directory
-    chdir("cgi-bin");
-}
-
-
-# We use require and an eval here (instead of "use") to catch any errors loading the module (for IIS)
+    # Change into cgi-bin directory - need to ensure it exists, since gliserver deals with both GS2 and GS3
+    if(-e "cgi-bin" && -d "cgi-bin") { # GS2
+	chdir("cgi-bin");
+    } else { # iis6_mode is not applicable for Greenstone 3
+	$iis6_mode = 0;
+    }
+}
+
+
+# We use require and an eval here (instead of "use package") to catch any errors loading the module (for IIS)
 eval("require \"gsdlCGI.pm\"");
 if ($@)
@@ -30,5 +36,6 @@
 
 
-my $debugging_enabled = 0;
+#my $authentication_enabled = 0;
+my $debugging_enabled = 0; # if 1, debugging is enabled and unlinking intermediate files (deleting files) will not happen
 
 my $mail_enabled = 0;
@@ -38,5 +45,5 @@
 
 sub main
-{
+{	
     my $gsdl_cgi = new gsdlCGI();
 
@@ -44,12 +51,9 @@
     $gsdl_cgi->setup_gsdl();
     my $gsdlhome = $ENV{'GSDLHOME'};
+
     $gsdl_cgi->checked_chdir($gsdlhome);
-    require "$gsdlhome/perllib/util.pm";  # This is OK on Windows
-    require "$gsdlhome/perllib/cpan/Crypt/UnixCrypt.pm";  # This is OK on Windows
 
     # Encrypt the password
-    if (defined $gsdl_cgi->param("pw")) {
-	$gsdl_cgi->param('-name' => "pw", '-value' => &Crypt::UnixCrypt::crypt($gsdl_cgi->clean_param("pw"), "Tp"));
-    }
+    $gsdl_cgi->encrypt_password();
 
     $gsdl_cgi->parse_cgi_args();
@@ -65,7 +69,15 @@
     $gsdl_cgi->delete("cmd");
 
-    # The check-installation command has no arguments
+    # The check-installation, greenstone-server-version and get-library-url commands have no arguments
     if ($cmd eq "check-installation") {
 	&check_installation($gsdl_cgi);
+	return;
+    }
+    elsif ($cmd eq "greenstone-server-version") {
+	&greenstone_server_version($gsdl_cgi);
+	return;
+    }
+    elsif ($cmd eq "get-library-url-suffix") {
+	&get_library_url_suffix($gsdl_cgi);
 	return;
     }
@@ -76,5 +88,4 @@
 	$gsdl_cgi->generate_error("No username specified.");
     }
-    # Remove the un argument (since this can mess up other scripts)
     $gsdl_cgi->delete("un");
 
@@ -86,33 +97,43 @@
     $gsdl_cgi->delete("ts");
 
+    my $site; # undefined on declaration, see http://perldoc.perl.org/perlsyn.html
+    if($gsdl_cgi->greenstone_version() != 2) { # all GS versions after 2 may define site
+	$site = $gsdl_cgi->clean_param("site");   
+	if (!defined $site) {
+	    $gsdl_cgi->generate_error("No site specified.");
+	}
+	$gsdl_cgi->delete("site");
+    }
+
+
     if ($cmd eq "delete-collection") {
-	&delete_collection($gsdl_cgi, $username, $timestamp);
+	&delete_collection($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "download-collection") {
-	&download_collection($gsdl_cgi, $username, $timestamp);
+	&download_collection($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "download-collection-archives") {
-	&download_collection_archives($gsdl_cgi, $username, $timestamp);
+	&download_collection_archives($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "download-collection-configurations") {
-	&download_collection_configurations($gsdl_cgi, $username, $timestamp);
+	&download_collection_configurations($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "download-collection-file") {
-	&download_collection_file($gsdl_cgi, $username, $timestamp);
+	&download_collection_file($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "delete-collection-file") {
-	&delete_collection_file($gsdl_cgi, $username, $timestamp);
+	&delete_collection_file($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "get-script-options") {
-	&get_script_options($gsdl_cgi, $username, $timestamp);
+	&get_script_options($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "move-collection-file") {
-	&move_collection_file($gsdl_cgi, $username, $timestamp);
+	&move_collection_file($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "new-collection-directory") {
-	&new_collection_directory($gsdl_cgi, $username, $timestamp);
+	&new_collection_directory($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "run-script") {
-	&run_script($gsdl_cgi, $username, $timestamp);
+	&run_script($gsdl_cgi, $username, $timestamp, $site);
     }
     elsif ($cmd eq "timeout-test") {
@@ -120,13 +141,27 @@
     }
     elsif ($cmd eq "upload-collection-file") {
-	&upload_collection_file($gsdl_cgi, $username, $timestamp);
-    } 
+	&upload_collection_file($gsdl_cgi, $username, $timestamp, $site);
+    }
     elsif ($cmd eq "file-exists") { 
-	&file_exists($gsdl_cgi);
-    }   
+	&file_exists($gsdl_cgi, $site);
+    }
+    # cmds not in Greenstone 2:
+    elsif ($gsdl_cgi->greenstone_version() != 2) { 	
+	if ($cmd eq "download-web-xml-file") {
+	    &download_web_xml_file($gsdl_cgi, $username, $timestamp, $site);
+	} 
+	elsif ($cmd eq "user-validation") {
+	    &user_validation($gsdl_cgi, $username, $timestamp, $site);
+	}
+	elsif ($cmd eq "get-site-names") {
+	    &get_site_names($gsdl_cgi, $username, $timestamp, $site);
+	}
+    }
     else {
 	$gsdl_cgi->generate_error("Unrecognised command: '$cmd'");
     }
-}
+        
+}
+
 
 sub authenticate_user
@@ -135,28 +170,52 @@
     my $username = shift(@_);
     my $collection = shift(@_);
-
-    # Remove the pw argument (since this can mess up other scripts)
+    my $site = shift(@_);
+
+    # Even if we're not authenticating remove the un and pw arguments, since these can mess up other scripts
     my $user_password = $gsdl_cgi->clean_param("pw");
     $gsdl_cgi->delete("pw");
 
+    # Only authenticate if it is enabled
+    # return "all-collections-editor" if (!$authentication_enabled);
+
     if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) {
 	$gsdl_cgi->generate_error("Authentication failed: no password specified.");
     }
 
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $etc_directory = &util::filename_cat($gsdlhome, "etc");
-    my $users_db_file_path = &util::filename_cat($etc_directory, "users.db");
-
-    # Use db2txt instead of GDBM_File to get the user accounts information
-    my $users_db_content = "";
-    open(USERS_DB, "db2txt \"$users_db_file_path\" |");
-    while (<USERS_DB>) {
-	$users_db_content .= $_;
-    }
-
-    # Get the user account information from the users.db database
+    my $users_db_content;
+    if($gsdl_cgi->greenstone_version() == 2) {
+	my $etc_directory = &util::filename_cat($ENV{'GSDLHOME'}, "etc");
+	my $users_db_file_path = &util::filename_cat($etc_directory, "users.db");
+	
+	# Use db2txt instead of GDBM_File to get the user accounts information
+	$users_db_content = "";
+	open(USERS_DB, "db2txt \"$users_db_file_path\" |");
+	while (<USERS_DB>) {
+	    $users_db_content .= $_;
+	}
+    }
+    elsif($gsdl_cgi->greenstone_version() == 3) {
+	my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
+
+	my $java = $gsdl_cgi->get_java_path();
+	my $java_gsdl3_classpath = &util::filename_cat($gsdl3srchome, "web", "WEB-INF", "lib", "gsdl3.jar");
+	my $java_derby_classpath = &util::filename_cat($gsdl3srchome, "web", "WEB-INF", "lib", "derby.jar");
+	my $java_classpath;
+	my $gsdlos = $ENV{'GSDLOS'};
+	if ($gsdlos !~ m/windows/){
+	    $java_classpath = $java_gsdl3_classpath . ":" . $java_derby_classpath;
+	}else{
+	    $java_classpath = $java_gsdl3_classpath . ";" . $java_derby_classpath;
+	}
+	my $java_args = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "etc", "usersDB");
+	$gsdl_cgi->checked_chdir($java_args);
+	my $java_command="\"$java\" -classpath \"$java_classpath\" org.greenstone.gsdl3.util.usersDB2txt \"$java_args\" 2>&1";
+	$users_db_content = `$java_command`;
+    }
+    
+    # Get the user account information from the usersDB database
     my %users_db_data = ();
     foreach my $users_db_entry (split(/-{70}/, $users_db_content)) {
-	if ($users_db_entry =~ /\n?\[(.+)\]\n/) {
+	if ($users_db_entry =~ m/\n?\[(.+)\]\n/) {
 	    $users_db_data{$1} = $users_db_entry;
 	}
@@ -170,5 +229,5 @@
 
     # Check password
-    my ($valid_user_password) = ($user_data =~ /\<password\>(.*)/);
+    my ($valid_user_password) = ($user_data =~ m/\<password\>(.*)/);
     if ($user_password ne $valid_user_password) {
 	$gsdl_cgi->generate_error("Authentication failed: incorrect password.");
@@ -176,9 +235,11 @@
 
     # Check group
-    my ($user_groups) = ($user_data =~ /\<groups\>(.*)/);
+    my ($user_groups) = ($user_data =~ m/\<groups\>(.*)/);
+
     if ($collection eq "") {
 	# If we're not editing a collection then the user doesn't need to be in a particular group
 	return $user_groups;  # Authentication successful
     }
+
     foreach my $user_group (split(/\,/, $user_groups)) {
 	# Does this user have access to all collections?
@@ -187,5 +248,5 @@
 	}
 	# Does this user have access to personal collections, and is this one?
-	if ($user_group eq "personal-collections-editor" && $collection =~ /^$username\-/) {
+	if ($user_group eq "personal-collections-editor" && $collection =~ m/^$username\-/) {
 	    return $user_groups;  # Authentication successful
 	}
@@ -195,5 +256,4 @@
 	}
     }
-
     $gsdl_cgi->generate_error("Authentication failed: user is not in the required group.");
 }
@@ -205,10 +265,10 @@
     my $username = shift(@_);
     my $collection = shift(@_);
+    my $site = shift(@_);
 
     my $steal_lock = $gsdl_cgi->clean_param("steal_lock");
     $gsdl_cgi->delete("steal_lock");
 
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
+    my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
     $gsdl_cgi->checked_chdir($collection_directory);
 
@@ -225,5 +285,5 @@
 
 	# Pick out the owner of the lock file
-	$lock_file_content =~ /\<User\>(.*?)\<\/User\>/;
+	$lock_file_content =~ m/\<User\>(.*?)\<\/User\>/;
 	my $lock_file_owner = $1;
 
@@ -257,5 +317,23 @@
 #   ACTIONS
 # ----------------------------------------------------------------------------------------------------
-
+# This routine, which uses the variable site, won't get called by GS2, 
+sub user_validation{
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
+   
+    # Users can be in any group to perform this action
+    my $user_err = &authenticate_user($gsdl_cgi, $username, "", $site);
+     if (defined $user_err && $user_err!~ m/ERROR/){
+	 if ($user_err!~ m/ERROR/){
+	     #$gsdl_cgi->generate_error("Authentication failed: $username is not valid");
+	     $gsdl_cgi->generate_ok($user_err);
+	     #print $user_err;
+	 }else{
+	     $gsdl_cgi->generate_error($user_err);
+	     #print "not valid" . $user_err;
+	 }
+     }else{
+	 $gsdl_cgi->generate_error("Authentication failed: $username is not valid");	 
+     }
+}
 
 sub check_installation
@@ -266,18 +344,17 @@
     my $installation_status = "";
 
-    print STDOUT "Content-type:text/plain\n\n";
-
     # Check that Java is installed and accessible
     my $java = $gsdl_cgi->get_java_path();
-    my $java_command = "$java -version 2>&1";
-
+    my $java_command = "\"$java\" -version 2>&1";
+    
     # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
     #   directly out to the page
-    if ($iis6_mode)
-    {
-	$java_command = "java -version";
+    if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) { ##
+	print STDOUT "Content-type:text/plain\n\n";
+	$java_command = "\"$java\" -version";
     }
 
     my $java_output = `$java_command`;
+        
     my $java_status = $?;
     if ($java_status < 0) {
@@ -292,13 +369,30 @@
     # Show the values of some important environment variables
     $installation_status .= "\n";
+    if($gsdl_cgi->greenstone_version() != 2) {
+	$installation_status .= "GSDL3SRCHOME: " . $ENV{'GSDL3SRCHOME'} . "\n";
+	$installation_status .= "GSDL3HOME: " . $ENV{'GSDL3HOME'} . "\n";
+    }
     $installation_status .= "GSDLHOME: " . $ENV{'GSDLHOME'} . "\n";
     $installation_status .= "GSDLOS: " . $ENV{'GSDLOS'} . "\n";
+    $installation_status .= "JAVA_HOME: " . $ENV{'JAVA_HOME'} . "\n" if defined($ENV{'JAVA_HOME'}); # on GS2, Java's only on the PATH
     $installation_status .= "PATH: " . $ENV{'PATH'} . "\n";
-
-    if ($installation_ok) {
-	print STDOUT $installation_status . "\nInstallation OK!";
+    if(defined $ENV{'FEDORA_VERSION'}) { # not using FLI unless version set
+	$installation_status .= "FEDORA_HOME: ".$ENV{'FEDORA_HOME'} . "\n";
+	$installation_status .= "FEDORA_VERSION: ".$ENV{'FEDORA_VERSION'};
+    }
+    
+    if ($installation_ok) { ## M. Dewsnip's svn log comment stated that for iis6_mode output needs to go to STDOUT
+	if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) {
+	    print STDOUT $installation_status . "\nInstallation OK!";
+	} else {
+	    $gsdl_cgi->generate_ok_message($installation_status . "\nInstallation OK!");
+	}
     }
     else {
-	print STDOUT $installation_status;
+	if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) {
+	    print STDOUT $installation_status;
+	} else {
+	    $gsdl_cgi->generate_error($installation_status);
+	}
     }
 }
@@ -307,5 +401,5 @@
 sub delete_collection
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -315,8 +409,8 @@
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
+
+
+    my $collect_directory = $gsdl_cgi->get_collection_dir($site);
     $gsdl_cgi->checked_chdir($collect_directory);
 
@@ -327,5 +421,5 @@
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     $gsdl_cgi->checked_chdir($collect_directory);
@@ -343,5 +437,5 @@
 sub delete_collection_file
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -356,21 +450,26 @@
 
     # Make sure we don't try to delete anything outside the collection
-    if ($file =~ /\.\./) {
+    if ($file =~ m/\.\./) {
 	$gsdl_cgi->generate_error("Illegal file specified.");
     }
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
+
+    my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
+    if (!-d $collection_directory){ ## wasn't there in gs2, ok_msg or error_msg?
+	$gsdl_cgi->generate_ok_message("Directory $collection_directory does not exist."); 
+	die;
+    }
+
     $gsdl_cgi->checked_chdir($collection_directory);
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     # Check that the collection file exists
-    if (!-e $file) {
+    if (!-e $file) { ## original didn't have 'die', but it was an ok message
 	$gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
+	die;
     }
     $gsdl_cgi->local_rm_r("$file");
@@ -387,5 +486,5 @@
 sub download_collection
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -395,24 +494,27 @@
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
+
+    my $collect_directory = $gsdl_cgi->get_collection_dir($site);
     $gsdl_cgi->checked_chdir($collect_directory);
 
     # Check that the collection exists
     if (!-d $collection) {
-	$gsdl_cgi->generate_error("Collection $collection does not exist.");
+	$gsdl_cgi->generate_ok_message("Collection $collection does not exist."); ## original had an error msg (from where it would die)
+	die;
     }
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     # Zip up the collection
     my $java = $gsdl_cgi->get_java_path();
-    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
+    my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
     my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-" . $timestamp . ".zip");
     my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
-    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args"; 
+    if($gsdl_cgi->greenstone_version() != 2) {
+	$java_args .= " gsdl3"; ## must this be done elsewhere as well?
+    }
+    my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args"; 
 
     my $java_output = `$java_command`;
@@ -427,6 +529,6 @@
     }
 
-    &put_file($gsdl_cgi, $zip_file_path, "application/zip");
-    unlink("$zip_file_path") unless $debugging_enabled;
+    &put_file($gsdl_cgi, $zip_file_path, "application/zip"); # file is transferred to client
+    unlink("$zip_file_path") unless $debugging_enabled;      # deletes the local intermediate zip file
 }
 
@@ -434,5 +536,5 @@
 sub download_collection_archives
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -442,8 +544,7 @@
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
+
+    my $collect_directory = $gsdl_cgi->get_collection_dir($site);
     $gsdl_cgi->checked_chdir($collect_directory);
 
@@ -454,12 +555,12 @@
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     # Zip up the collection archives
     my $java = $gsdl_cgi->get_java_path();
-    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
+    my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
     my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-archives-" . $timestamp . ".zip");
     my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
-    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args"; 
+    my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args"; 
 
     my $java_output = `$java_command`;
@@ -482,20 +583,18 @@
 sub download_collection_configurations
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
-
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
+   
     # Users can be in any group to perform this action
-    my $user_groups = &authenticate_user($gsdl_cgi, $username, "");
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
+    my $user_groups = &authenticate_user($gsdl_cgi, $username, "", $site);
+
+    my $collect_directory = $gsdl_cgi->get_collection_dir($site);
     $gsdl_cgi->checked_chdir($collect_directory);
 
     # Zip up the collection configurations
     my $java = $gsdl_cgi->get_java_path();
-    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
+    my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
     my $zip_file_path = &util::filename_cat($collect_directory, "collection-configurations-" . $timestamp . ".zip");
     my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$username\" \"$user_groups\"";
-    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args"; 
-
+    my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args"; 
     my $java_output = `$java_command`;
     my $java_status = $?;
@@ -518,5 +617,5 @@
 sub file_exists
 {
-    my ($gsdl_cgi) = @_;
+    my ($gsdl_cgi, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -530,10 +629,9 @@
     $file =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS
    
-    # Not necessary: checking whether the user is authenticated to query existance of the file
+    # Not necessary: checking whether the user is authenticated to query existence of the file
     #&authenticate_user($gsdl_cgi, $username, $collection);
 
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
-    $gsdl_cgi->checked_chdir($collection_directory);
+    my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
+    $gsdl_cgi->checked_chdir($collection_directory); # cd into the directory of that collection
 
     # Check that the collection file exists
@@ -547,5 +645,5 @@
 sub download_collection_file
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -560,29 +658,29 @@
 
     # Make sure we don't try to download anything outside the collection
-    if ($file =~ /\.\./) {
+    if ($file =~ m/\.\./) {
 	$gsdl_cgi->generate_error("Illegal file specified.");
     }
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
+
+    my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
     $gsdl_cgi->checked_chdir($collection_directory);
 
     # Check that the collection file exists
     if (!-e $file) {
-	$gsdl_cgi->generate_error("Collection file $file does not exist.");
+	$gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
+	die;
     }
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     # Zip up the collection file
     my $java = $gsdl_cgi->get_java_path();
-    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
+    my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
     my $zip_file_path = &util::filename_cat($collection_directory, $collection . "-file-" . $timestamp . ".zip");
     my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
-    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args"; 
+    my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args"; 
 
     my $java_output = `$java_command`;
@@ -601,9 +699,58 @@
 }
 
+# download web.xml from the server
+sub download_web_xml_file
+{
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
+
+    # Users can be in any group to perform this action
+    my $user_groups = &authenticate_user($gsdl_cgi, $username, "", $site);
+
+    my $file = $gsdl_cgi->clean_param("file");
+    if ((!defined $file) || ($file =~ m/^\s*$/)) {
+	$gsdl_cgi->generate_error("No file specified.");
+    }
+    $file =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS
+
+    # Make sure we don't try to download anything else
+    if ($file =~ m/\.\./) {
+	$gsdl_cgi->generate_error("Illegal file specified.");
+    }
+
+    my $web_inf_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "WEB-INF");
+    $gsdl_cgi->checked_chdir($web_inf_directory);
+
+    # Check that the collection file exists
+    if (!-e $file) {
+	$gsdl_cgi->generate_error("file $file does not exist.");
+    }
+
+    # Zip up the collection file
+    my $java = $gsdl_cgi->get_java_path();
+    my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
+    my $zip_file_path = &util::filename_cat($web_inf_directory, "webxml" . $timestamp . ".zip");
+    my $java_args = "\"$zip_file_path\" \"$web_inf_directory\" \"$file\"";
+    my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args"; 
+    my $java_output = `$java_command`;
+
+    my $java_status = $?;
+    if ($java_status > 0) {
+	$gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
+    }
+
+    # Check that the zip file was created successfully
+    if (!-e $zip_file_path || -z $zip_file_path) {
+	$gsdl_cgi->generate_error("web.xml zip file $zip_file_path could not be created.");
+    }
+
+    &put_file($gsdl_cgi, $zip_file_path, "application/zip");
+
+    unlink("$zip_file_path") unless $debugging_enabled;
+}
 
 # Collection locking unnecessary because this action isn't related to a particular collection
 sub get_script_options
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $script = $gsdl_cgi->clean_param("script");
@@ -614,5 +761,8 @@
 
     # Users can be in any group to perform this action
-    &authenticate_user($gsdl_cgi, $username, "");
+    &authenticate_user($gsdl_cgi, $username, "", $site);
+    $gsdl_cgi->delete("ts"); ## two lines from GS3 version, doesn't seem to harm GS2
+    $gsdl_cgi->delete("pw"); 
+    
 
     my $perl_args = "";
@@ -637,13 +787,14 @@
     }
 
-    print STDOUT "Content-type:text/plain\n\n";
-
-    my $perl_command = "perl -S $script $perl_args 2>&1";
 
     # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
     #   directly out to the page
-    if ($iis6_mode)
+    print STDOUT "Content-type:text/plain\n\n";
+    my $perl_command;
+    if($iis6_mode && $gsdl_cgi->greenstone_version() == 2)
     {
 	$perl_command = "perl -S $script $perl_args";
+    } else {
+	$perl_command = "perl -S $script $perl_args 2>&1";
     }
 
@@ -660,8 +811,44 @@
 }
 
+# get the names of all sites available on the server
+sub get_site_names
+{
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
+    my $sites_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "sites");
+
+    my @sites_dir;
+    my @site_dir;
+    
+    $gsdl_cgi->checked_chdir($sites_directory);
+    opendir(DIR, $sites_directory);
+    @sites_dir= readdir(DIR);
+    my $sites_dir;
+    my $sub_dir_file;
+
+    print STDOUT "Content-type:text/plain\n\n";
+    foreach $sites_dir(@sites_dir)
+    {
+	if (!(($sites_dir eq ".") || ($sites_dir eq "..") || ($sites_dir eq "CVS")))
+	{
+	    my $site_dir_path= &util::filename_cat($sites_directory,$sites_dir);
+	    $gsdl_cgi->checked_chdir($site_dir_path);
+	    opendir(DIR,$site_dir_path);
+	    @site_dir=readdir(DIR);
+	    closedir(DIR);
+	    
+	    foreach $sub_dir_file(@site_dir)
+	    {
+		if ($sub_dir_file eq "siteConfig.xml"){
+		    print STDOUT "$sites_dir" . "-----";
+		}
+	    }
+	}
+    }
+
+}
 
 sub move_collection_file
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -681,13 +868,12 @@
 
     # Make sure we don't try to move anything outside the collection
-    if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
+    if ($source_file =~ m/\.\./ || $target_file =~ m/\.\./) {
 	$gsdl_cgi->generate_error("Illegal file specified.");
     }
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
+
+    my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
     $gsdl_cgi->checked_chdir($collection_directory);
 
@@ -698,5 +884,5 @@
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     &util::mv($source_file, $target_file);
@@ -704,5 +890,5 @@
     # Check that the collection source file was moved
     if (-e $source_file || !-e $target_file) {
-	$gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
+	$gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file."); # dies
     }
 
@@ -713,5 +899,5 @@
 sub new_collection_directory
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $collection = $gsdl_cgi->clean_param("c");
@@ -726,24 +912,26 @@
 
     # Make sure we don't try to create anything outside the collection
-    if ($directory =~ /\.\./) {
+    if ($directory =~ m/\.\./) {
 	$gsdl_cgi->generate_error("Illegal directory specified.");
     }
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
+
+    my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
     $gsdl_cgi->checked_chdir($collection_directory);
 
     # Check that the collection directory doesn't already exist
-    # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicity
+    # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicitly
     # try to create the import directory
-#    if (-d $directory) {
-#	$gsdl_cgi->generate_error("Collection directory $directory already exists.");
-#    }
+## log -r13497 for GS2's gliserver.pl, Katherine Don explains:
+# "commented out checking for existence of a directory in new_collection_directory 
+# as it throws an error which we don't want"
+    #if($gsdl_cgi->greenstone_version() != 2 && -d $directory) {
+	#$gsdl_cgi->generate_error("Collection directory $directory already exists.");
+    #}
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     &util::mk_dir($directory);
@@ -760,5 +948,5 @@
 sub run_script
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
 
     my $script = $gsdl_cgi->clean_param("script");
@@ -767,4 +955,5 @@
     }
     $gsdl_cgi->delete("script");
+ 
     my $collection = $gsdl_cgi->clean_param("c");
     if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
@@ -773,9 +962,12 @@
     $gsdl_cgi->delete("c");
 
+    # confuse other, so delete timestamp
+    $gsdl_cgi->delete("ts");
+
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
+    &authenticate_user($gsdl_cgi, $username, $collection, $site);
 
     # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
-    &lock_collection($gsdl_cgi, $username, $collection) unless ($script eq "mkcol.pl");
+    &lock_collection($gsdl_cgi, $username, $collection, $site) unless ($script eq "mkcol.pl");
 
     # Last argument is the collection name, except for explode_metadata_database.pl and
@@ -783,5 +975,5 @@
     my $perl_args = $collection;
     if ($script eq "explode_metadata_database.pl" || $script eq "replace_srcdoc_with_html.pl") {
-	# Last argument is the file to be exploded
+	# Last argument is the file to be exploded or it is the file to be replaced with its html version
 	my $file = $gsdl_cgi->clean_param("file");
 	if ((!defined $file) || ($file =~ m/^\s*$/)) {
@@ -789,6 +981,7 @@
 	}
 	$gsdl_cgi->delete("file");
-	$file =~ s/ /\\ /g; # escape all spaces in filename with a backslash, i.e. "\ ".
-	$perl_args = $file;	
+	#$file =~ s/ /\\ /g; # Works on linux: escape all spaces in filename with a backslash, i.e. "\ ".
+	$file = "\"$file\""; # Windows: bookend the relative filepath with quotes in case it contains spaces 
+	$perl_args = $file;
     }
 
@@ -803,18 +996,25 @@
     }
 
-    print STDOUT "Content-type:text/plain\n\n";
+    # mkcol.pl and import.pl, buildcol.pl, g2f-import.pl, g2f-buildcol.pl all need the -collectdir option passed
+    my $import_pl = "import.pl"; # import is a reserved word, need to put it in quotes
+    
+    if (($script =~ m/$import_pl|buildcol.pl/) || ($script eq "mkcol.pl")) { # || ($script eq "schedule.pl")
+	my $collect_directory = $gsdl_cgi->get_collection_dir($site); 
+	$perl_args = $perl_args . " -collectdir \"$collect_directory\"";
+    }
 
     my $perl_command = "perl -S $script $perl_args 2>&1";
-
     # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
     #   directly out to the page
-    if ($iis6_mode)
+    if($gsdl_cgi->greenstone_version() == 2 && $iis6_mode)
     {
 	$perl_command = "perl -S $script $perl_args";
     }
-
     if (!open(PIN, "$perl_command |")) {
 	$gsdl_cgi->generate_error("Unable to execute command: $perl_command");
     }
+
+    print STDOUT "Content-type:text/plain\n\n";
+    print "$perl_command  \n";
 
     while (defined (my $perl_output_line = <PIN>)) {
@@ -834,9 +1034,8 @@
 }
 
-
 sub upload_collection_file
 {
-    my ($gsdl_cgi, $username, $timestamp) = @_;
-
+    my ($gsdl_cgi, $username, $timestamp, $site) = @_;
+   
     my $collection = $gsdl_cgi->clean_param("c");
     if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
@@ -852,20 +1051,21 @@
 
     # Make sure we don't try to upload anything outside the collection
-    if ($file =~ /\.\./) {
+    if ($file =~ m/\.\./) {
 	$gsdl_cgi->generate_error("Illegal file specified.");
     }
-    if ($directory =~ /\.\./) {
+    if ($directory =~ m/\.\./) {
 	$gsdl_cgi->generate_error("Illegal directory specified.");
     }
 
     # Ensure the user is allowed to edit this collection
-    &authenticate_user($gsdl_cgi, $username, $collection);
-
-    my $gsdlhome = $ENV{'GSDLHOME'};
-    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
+    if($gsdl_cgi->greenstone_version() == 2) { ## Quan commented this out for GS3 in r14325
+	&authenticate_user($gsdl_cgi, $username, $collection, $site); # site will be undefined for GS2, of course
+    }
+
+    my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
     $gsdl_cgi->checked_chdir($collection_directory);
 
     # Make sure the collection isn't locked by someone else
-    &lock_collection($gsdl_cgi, $username, $collection);
+    &lock_collection($gsdl_cgi, $username, $collection, $site);
 
     my $directory_path = &util::filename_cat($collection_directory, $directory);
@@ -877,33 +1077,56 @@
     }
 
-    my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
+    #my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp); 
+    my $file_path = "";
+    if($gsdl_cgi->greenstone_version() == 2) {
+	$file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp); 
+    } else {
+	$file_path = &util::filename_cat($directory_path, $file); 
+    }
+   
     if (!open(FOUT, ">$file_path")) {
+	print STDERR "Unable to write file $file_path\n";
 	$gsdl_cgi->generate_error("Unable to write file $file_path");
     }
 
     # Read the uploaded data and write it out to file
-    # We have to pass the size of the uploaded data in the "fs" argument because IIS 6 seems to be
-    #   completely incapable of working this out otherwise (causing the old code to crash)
     my $buf;
     my $num_bytes = 0;
-    my $num_bytes_remaining = $gsdl_cgi->clean_param("fs");
-    my $bytes_to_read = $num_bytes_remaining;
-    if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
     binmode(FOUT);
-    while (read(STDIN, $buf, $bytes_to_read) > 0) {
-	print FOUT $buf;
-	$num_bytes += length($buf);
-	$num_bytes_remaining -= length($buf);
-	$bytes_to_read = $num_bytes_remaining;
+    if($gsdl_cgi->greenstone_version() == 2) { ##
+	# We have to pass the size of the uploaded data in the "fs" argument because IIS 6 seems to be
+	#   completely incapable of working this out otherwise (causing the old code to crash)
+	my $num_bytes_remaining = $gsdl_cgi->clean_param("fs");
+	my $bytes_to_read = $num_bytes_remaining;
 	if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
+
+	while (read(STDIN, $buf, $bytes_to_read) > 0) {
+	    print FOUT $buf;
+	    $num_bytes += length($buf);
+	    $num_bytes_remaining -= length($buf);
+	    $bytes_to_read = $num_bytes_remaining;
+	    if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
+	}
+    } else { # GS3 and later
+    	my $bread;
+	my $fh = $gsdl_cgi->clean_param("uploaded_file");
+
+	if (!defined $fh) {
+	    print STDERR "ERROR. Filehandle undefined. No file uploaded onto GS3 server.\n";
+	    $gsdl_cgi->generate_error("ERROR. Filehandle undefined. No file uploaded (GS3 server).");
+	} else {
+	    while ($bread=read($fh, $buf, 1024)) {
+		print FOUT $buf;
+	    }
+	}
     }
     close(FOUT);
-
+       
     # If we have downloaded a zip file, unzip it
     if (defined $zip) {
 	my $java = $gsdl_cgi->get_java_path();
-	my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
+	my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
 	my $java_args = "\"$file_path\" \"$directory_path\"";
-	my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args"; 
+	my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args"; 
 
 	my $java_output = `$java_command`;
@@ -911,8 +1134,8 @@
 
 	# Remove the zip file once we have unzipped it, since it is an intermediate file only
-	unlink("$file_path");
-
+	unlink("$file_path") unless $debugging_enabled;
+	
 	if ($java_status > 0) {
-	    $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
+	    $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home()); # dies
 	}
     }
@@ -920,5 +1143,4 @@
     $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
 }
-
 
 sub put_file
@@ -928,7 +1150,10 @@
     my $content_type = shift(@_);
 
+    if(!defined $content_type) { ##
+	$content_type = "application/zip";
+    }
+	
     if (open(PIN, "<$file_path")) {
-	print STDOUT "Content-type:$content_type\n\n";
-
+	print STDOUT "Content-type:$content_type\n\n"; ## For GS3: "Content-type:application/zip\n\n";
  	my $buf;
  	my $num_bytes = 0;
@@ -966,4 +1191,17 @@
 }
 
+sub greenstone_server_version
+{	
+    my $gsdl_cgi = shift(@_);
+    my $version = $gsdl_cgi->greenstone_version();
+    $gsdl_cgi->generate_ok_message("Greenstone server version is: $version\n");
+}
+
+sub get_library_url_suffix
+{
+    my $gsdl_cgi = shift(@_);
+    my $library_url = $gsdl_cgi->library_url_suffix();
+    $gsdl_cgi->generate_ok_message("Greenstone library URL suffix is: $library_url\n");
+}
 
 &main();
