#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Archive::Tar;
use File::Basename;
use File::Path;
use File::Find;
use File::Copy;
my $cgi = new CGI;
my $sOutput;
my $sFileName;
my $sDirName;
my $sFileWebName;
#----------------------------------
sub fnFolderSelect
{
my $sFolderName = $_[0];
my $sReturn;
my $sFolderPath;
my $sShortFolderPath;
$sReturn .= "";
return $sReturn;
} #-- fn
#----------------------------------
sub fnFileSelect
{
my $sFolderName = $_[0];
my $sFilter = $_[1];
my $sReturn;
my $sFilePath;
my $sShortFilePath;
$sReturn .= "";
return $sReturn;
} #-- fn
#----------------------------------
sub fnErrorMessage
{
my $sMessage = $_[0];
my $sReturn;
$sReturn .= "
Installation problem
";
$sReturn .= $sMessage;
$sReturn .= <try again
ENDF
return $sReturn;
}
#-- where the site content will go
my $sContentDirectory = $cgi->param('contentdir');
#-- if existing content should be overwritten
my $sOverWriteContent = $cgi->param('overwrite');
#-- where the perl scripts will go
my $sScriptDirectory = $cgi->param('scriptdir');
#-- a displayable name for the site
my $sSiteAlias = $cgi->param('sitealias');
#-- the language for site messages
my $sLanguage = $cgi->param('language');
my $sArchiveFile = $cgi->param('archive');
my $sInstall = $cgi->param('install');
my $sMode = $cgi->param('mode');
my $sFileContents = $cgi->param('filecontents');
#my $sContentDirectoryWebName =~ s/%2F/\//;
my $sDocumentRoot = $ENV{'DOCUMENT_ROOT'};
#$sContentDirName = $ENV{'DOCUMENT_ROOT'} . '/' . $sContentDirWebName;
my $sContentPath = "$sDocumentRoot/$sContentDirectory";
my $sScriptPath = "$sDocumentRoot/$sScriptDirectory";
if ($sArchiveFile =~ /^\//)
{
$sArchiveFile = $sDocumentRoot.$sArchiveFile;
}
print $cgi->header();
my $sCss .= <
ENDCSS
$sOutput .=
$cgi->start_html(
-title=>'install site',
-author=>'bumble.sf.net',
-base=>'true',
-style=>{-code=>$sCss},
-meta=>{'keywords'=>'wiki perl apache html',
'copyright'=>'gpl'});
$sOutput .= <
END
#-------------------------------------------------
if (!$sMode)
{
my $sFolderSelect = fnFolderSelect($sDocumentRoot);
my $sFileSelect = fnFileSelect($sDocumentRoot, '\.tar');
$sOutput .= <
install the web site
help
General help
The webweb system is designed to run on an Apache webserver.
The system will probably not work on another server because
it makes use of specific features of Apache. The system uses
perl scripts to generate pages and create an automatic interface
to mysql databases.
The script directory
This is the directory where the cgi perl scripts will be placed.
Consult your web-hosting documentation to find out where the
scripts should be placed.
The content directory
This is the name of the directory where the site will be
created. The site will accessible via the url
http://www.server.com/dir/ where 'dir' is the name of
the directory specified. This name should not contain any
spaces.
The overwrite content option
This checkbox specifies if the installation script
will overwrite existing content on the server. It
is someone risky to use this option.
The website name or alias
This is the name which will appear for the site on webpages,
and will be included in various messages. This does not affect
the location of the website
The website user language
This is the language in which messages will be written.
The archive file
This is the file which contains all the scripts and templates
which are used to create and maintain the site. This file
should be uploaded to the web host. Without this file the site cannot
be installed. The file either should be in the same folder
as the installation script or should be specified relative to
the document root (eg /path/to/file.tar )
delete the entire site
If this box is checked the site will be deleted,
both the content and the perl scripts and htaccess
rules.
ENDA
$sOutput .= $cgi->end_html();
print $sOutput;
exit (0);
} #-- if not install
#------------------------------------------------------
if ($sMode eq 'install')
{
$sOutput .=
$cgi->start_html(
-title=>'install site',
-author=>'bumble.sf.net',
-base=>'true',
-meta=>{'keywords'=>'wiki perl apache html',
'copyright'=>'gpl'});
$sOutput .=
" \n" .
" \n" .
" \n" .
" \n" .
" \n" .
" \n" .
" \n";
$sOutput .= <
\$sScriptDirectory=$sScriptDirectory
\$sScriptPath=$sScriptPath
\$sContentDirectory=$sContentDirectory
\$sContentPath=$sContentPath
\$sSiteAlias=$sSiteAlias
\$sLanguage=$sLanguage
\$sArchiveFile=$sArchiveFile
\$sInstall=$sInstall
\$sMode=$sMode
END1
#------------------------------------------------
my $sMessage;
if (!$sContentDirectory)
{
$sOutput .= fnErrorMessage(
"No directory was specified for the site content.");
$sOutput .= $cgi->end_html();
print $sOutput;
exit (0);
}
#------------------------------------------------
if ((-e $sContentPath) && (!$sOverWriteContent))
{
$sMessage = <already exists on the server. The site cannot be
installed to this folder because all current content in that folder
would be overwritten (lost). Please choose a directory name that
does not exist.
ENDF
$sOutput .= fnErrorMessage($sMessage);
$sOutput .= $cgi->end_html();
print $sOutput;
exit (0);
}
#------------------------------------------------
if (!$sScriptDirectory)
{
$sMessage = <end_html();
print $sOutput;
exit (0);
}
#------------------------------------------------
if (!-e $sScriptPath)
{
$sMessage = <end_html();
print $sOutput;
exit (0);
}
#------------------------------------------------
if (!-d $sScriptPath)
{
$sMessage= <end_html();
print $sOutput;
exit (0);
}
#------------------------------------------------
if (!$sArchiveFile)
{
$sMessage = <end_html();
print $sOutput;
exit (0);
}
#------------------------------------------------
if (!-e $sArchiveFile)
{
$sMessage = <end_html();
print $sOutput;
exit (0);
}
#-- a directory which is unlikely to exist
my $sTempFolder = $sDocumentRoot.'/tmpAAA123BBB123xxx999';
rmtree([$sTempFolder]);
my $bResult = mkdir($sTempFolder, 0777);
if (!$bResult)
{
$sMessage .= <end_html();
print $sOutput;
exit (0);
}
$bResult = copy($sArchiveFile, "$sTempFolder");
if (!$bResult)
{
$sMessage .= <end_html();
print $sOutput;
exit (0);
}
chdir($sTempFolder);
my $tar = Archive::Tar->new;
$bResult = $tar->read("$sArchiveFile");
if (!$bResult)
{
$sMessage .= <end_html();
print $sOutput;
exit (0);
}
my @aaFiles = $tar->list_files();
my $sFileName;
my $sNewFileName;
my $sOldContentDirectory;
my $sOldScriptDirectory;
foreach $sFileName (@aaFiles)
{
if ($sFileName =~ m@/([^/]+)/css/scripts/data.css@)
{
$sOldContentDirectory = $1;
}
if ($sFileName =~ m@/([^/]+)/parse-page-description.cgi@)
{
$sOldScriptDirectory = $1;
}
$sNewFileName = $sFileName;
$sNewFileName =~ s/.*\/htdocs//;
$sOutput .= "$sNewFileName ";
$bResult = $tar->rename($sFileName, $sNewFileName);
if (!$bResult)
{
die "couldnt rename file $sFileName to $sNewFileName";
}
}
if (!$sOldScriptDirectory)
{
die "file missing parse-page-description.cgi, cant install";
}
$sOutput .= <
\$sOldScriptDirectory=$sOldScriptDirectory
\$sOldContentDirectory=$sOldContentDirectory
END2
my @aaExtractedFiles = $tar->extract();
if (!@aaExtractedFiles)
{
die "couldnt extract files from archive $sArchiveFile";
}
chdir($sDocumentRoot);
if ($sOverWriteContent)
{
rmtree("$sDocumentRoot/$sContentDirectory");
}
$bResult =
rename("$sTempFolder/$sOldContentDirectory",
"$sDocumentRoot/$sContentDirectory");
if (!$bResult)
{
die "couldnt rename $sTempFolder/$sOldContentDirectory to $sDocumentRoot/$sContentDirectory. $!";
}
my $file;
my $sFileBaseName;
my @directory = <$sTempFolder/$sOldScriptDirectory/*>;
foreach $file (@directory)
{
$sFileBaseName = basename($file);
print "$sDocumentRoot/$sScriptDirectory/$sFileBaseName ";
if (!-e "$sDocumentRoot/$sScriptDirectory/$sFileBaseName")
{}
}
rmtree($sTempFolder);
$sOutput .= $cgi->end_html();
print $sOutput;
exit (0);
} #-- if mode 'install'
#--------------------------------------------------
if ($sMode eq 'edit')
{
open(FILE, 'install-site.cgi') || die "Could not open install-site.cgi to edit !";
$sOutput .= <
Editing script: install-site.cgi