#!/usr/bin/perl
use strict;
use CGI::Carp qw(fatalsToBrowser);
use lib '.';
use DBI;
use XFileConfig;
use Session;
use CGI qw(param);
my $ok = "<br><b style='background:#1a1;color:#fff;padding:2px;'>OK</b>";
my $ses = Session->new;
my $f = $ses->f;
if($f->{site_settings})
{
my @fields = qw(temp_dir upload_dir htdocs_dir);
$f->{temp_dir} = "$f->{cgi_path}/temp";
$f->{upload_dir} = "$f->{cgi_path}/uploads";
$f->{htdocs_dir} = "$f->{site_path}/files";
my $conf;
open(F,"XFSConfig.pm")||$ses->message("Can't read XFSConfig");
$conf.=$_ while <F>;
close F;
for my $x (@fields)
{
my $val = $f->{$x};
$conf=~s/$x\s*=>\s*(\S+)\s*,/"$x => '$val',"/e;
}
open(F,">XFSConfig.pm")||$ses->message("Can't write XFSConfig");
print F $conf;
close F;
}
if($f->{save_sql_settings} || $f->{site_settings})
{
my @fields = $f->{save_sql_settings} ? qw(db_host db_login db_passwd db_name pasword_salt) : qw(site_url site_cgi site_path);
my $conf;
open(F,"XFileConfig.pm")||$ses->message("Can't read XFileConfig");
$conf.=$_ while <F>;
close F;
$f->{pasword_salt} = $ses->randchar(12);
$f->{dl_key} = $ses->randchar(10);
for my $x (@fields)
{
my $val = $f->{$x};
$conf=~s/$x\s*=>\s*(\S+)\s*,/"$x => '$val',"/e;
}
open(F,">XFileConfig.pm")||$ses->message("Can't write XFileConfig");
print F $conf;
close F;
$ses->redirect('install.cgi');
}
if($f->{create_sql})
{
my $db = $ses->db;
open(FILE,"install.sql")||$ses->message("Can't open create.sql");
my $sql;
$sql.=$_ while <FILE>;
$sql=~s/CREATE TABLE/CREATE TABLE IF NOT EXISTS/gis;
$db->Exec($_) for split(';',$sql);
$db->Exec("INSERT INTO Users (usr_login,usr_email,usr_password,usr_created,usr_adm) VALUES (?,?,ENCODE(?,?),NOW(),1)",$f->{usr_login},$f->{usr_email},$f->{usr_password},$c->{pasword_salt});
$ses->redirect('install.cgi');
}
print"Content-type:text/html\n\n";
print"<HTML><BODY style='font:13px Arial;'><h2>XFileSharingPro Installation Script</h2>";
print"<b>1) Permissions Check</b><br><br>";
my $perms = {
'logs.txt' => 0666,
'ipn_log.txt' => 0666,
'fs.cgi' => 0755,
'index.cgi' => 0755,
'ipn.cgi' => 0755,
'cron.pl' => 0755,
'dl.pl' => 0755,
'upload.cgi' => 0755,
'upload_status.cgi' => 0755,
'api.cgi' => 0755,
'XFileConfig.pm' => 0666,
'XFSConfig.pm' => 0666,
'temp' => 0777,
'uploads' => 0777,
"$c->{site_path}/files" => 0777,
"$c->{site_path}/captchas" => 0777,
};
my @arr;
for(keys %{$perms})
{
next unless -e $_;
chmod $perms->{$_}, $_;
my $chmod = (stat($_))[2] & 07777;
my $chmod_txt = sprintf("%04o", $chmod);
push @arr, "<b>$_</b> : $chmod_txt : ".( $chmod == $perms->{$_} ? 'OK' : "<u>ERROR: should be ".sprintf("%04o",$perms->{$_})."</u>" );
}
chmod 0666, "$c->{site_path}/.htaccess" if -f "$c->{site_path}/.htaccess";
print join '<br>', @arr;
if( grep{/ERROR/}@arr )
{
print"<br><br><u>Fix errors above and refresh this page</u>";
}
else
{
print"<br><br>All permissions are correct.$ok";
}
print"<hr>";
print"<b>2) MySQL Settings</b><br><br>";
my $dbh=DBI->connect("DBI:mysql:database=$c->{db_name};host=$c->{db_host}",$c->{db_login},$c->{db_passwd}) if $c->{db_name} && $c->{db_host};
if($dbh)
{
print"MySQL Settings are correct. Can connect to DB.$ok";
}
else
{
print<<EOP
Can't connect to DB with current settings: $DBI::errstr<br><br>
<Form method="POST">
<input type="hidden" name="save_sql_settings" value="1">
MySQL Host:<br>
<input type="text" name="db_host" value="$c->{db_host}"><br>
MySQL DB Username:<br>
<input type="text" name="db_login" value="$c->{db_login}"><br>
MySQL DB Password:<br>
<input type="text" name="db_passwd" value="$c->{db_passwd}"><br>
MySQL DB Name:<br>
<input type="text" name="db_name" value="$c->{db_name}"><br><br>
<input type="submit" value="Save MySQL Settings">
</Form>
EOP
;
}
print"<hr>";
print"<b>3) MySQL tables create & Admin account</b><br><br>";
if(!$dbh)
{
print"Fix MySQL settings above first.";
}
else
{
my $sth=$dbh->prepare("DESC Files");
my $rc=$sth->execute();
if($rc)
{
print"Tables created successfully.$ok";
}
else
{
print<<EOP
<form method="POST">
<input type="hidden" name="create_sql" value="1">
Admin login:<br><input type="text" name="usr_login"><br>
Admin password:<br><input type="text" name="usr_password"><br>
Admin E-mail:<br><input type="text" name="usr_email"><br><br>
<input type="submit" value="Create MySQL Tables & Admin Account">
</form>
EOP
;
}
}
print"<hr><b>4) Site URL / Path Settings</b><br><br>";
if($c->{site_url} && $c->{site_cgi} && -d $c->{site_path})
{
print"Settings are correct.$ok";
print"<br><br><b>Login as admin and add FileServer now: <a href='$c->{site_url}/login.html'>$c->{site_url}/login.html</a></b>";
}
else
{
my $path = $ENV{DOCUMENT_ROOT};
my ($cgipath) = $ENV{SCRIPT_FILENAME}=~/^(.+)\//;
my $url_cgi = 'http://'.$ENV{HTTP_HOST}.$ENV{REQUEST_URI};
$url_cgi=~s/\/[^\/]+$//;
my $url = 'http://'.$ENV{HTTP_HOST};
$url = $c->{site_url}||$url;
$url_cgi = $c->{site_cgi}||$url_cgi;
$path = $c->{site_path}||$path;
print<<EOP
<form method="POST">
<input type="hidden" name="site_settings" value="1">
htdocs folder URL:<br>
<input type="text" name="site_url" value="$url" size=48> <small>No trailing slash</small><br>
cgi-bin folder URL:<br>
<input type="text" name="site_cgi" value="$url_cgi" size=48> <small>No trailing slash</small><br>
cgi-bin disk path:<br>
<input type="text" name="cgi_path" value="$cgipath" size=48><br>
htdocs disk path:<br>
<input type="text" name="site_path" value="$path" size=48><br>
<br>
<input type="submit" value="Save site settings">
</form>
EOP
;
}
print"<hr><b>5) Manually Remove install files</b><br><br>install.cgi<br>install.sql<br>convert.cgi<br>convert.sql";