#!/usr/bin/perl
#------------------------------------------------------------------------------
# $Id$
# Download client application
#------------------------------------------------------------------------------

use strict;
use utf8;
use CGI;
use TF::CommonUtils;
use URI::Escape;
use XML::DOM;
use TF::CommonUtils;
use TF::XMLUtils;
use Encode;

#
# global
#
my $BRAND         = $ENV{'TF_BRANDNAME'} || '';
my $LOCATION      = $ENV{'TF_LOCATION'} || '';
my $LANGPARAM     = TF::CommonUtils::getLangPriority();
my $CONF_ROOT     = $ENV{'TF_CONFIG_ROOT'} || '/usr/local/teamfile/www/conf/conf.d';
my $SETTINGS_DIR  = $CONF_ROOT . '/clientdata';

my $MSGTABLE_ja  = {
					CGIError        => "CGIエラーです",
					MissingFileName => "ファイル名が指定されておりません",
					MissingModule   => "クライアントソフトウェアがサーバ上に存在しませんでした",
					InvalidFile     => "指定されたファイルをダウンロードすることはできません",
					};

my $MSGTABLE_en  = {
					CGIError        => "CGI error found.",
					MissingFileName => "The filename is missing.",
					MissingModule   => "The client software is missing.",
					InvalidFile     => "You could not download this file.",
					};
my $MSGTABLE     = ($LANGPARAM eq 'ja') ? $MSGTABLE_ja : $MSGTABLE_en;
my @ALLOWED_FILE = ('TeamFileSite.exe', 'TeamFile.exe', 'SmartFileSite.exe', 'SmartFile.exe');

if (!$LOCATION) {
	TF::CommonUtils::showAccessDeny();
	die '';
}

main();

#
# main 
#
sub main {
	my $cgi = new CGI;
	if( ! $cgi ) {
		binmode(STDOUT, ":utf8");
		TF::CommonUtils::showErrorPage($MSGTABLE->{CGIError}, $LANGPARAM);
		print STDERR "CGI Object error";
		return;
	}

	if( $cgi->cgi_error ) {
		binmode(STDOUT, ":utf8");
		TF::CommonUtils::showErrorPage($MSGTABLE->{CGIError} . $cgi->cgi_error, $LANGPARAM);
		print STDERR "CGI Error (" . $cgi->cgi->error . ")";
		return;
	}

	my $enc_filename = $cgi->param('file') || '';
	if (!$enc_filename) {
		binmode(STDOUT, ":utf8");
		TF::CommonUtils::showErrorPage($MSGTABLE->{MissingFileName}, $LANGPARAM);
		return;
	}
	my $filename = TF::CommonUtils::decodeUTF8($enc_filename);
	$filename =~ s/\n//g;
	$filename =~ s/\///g;
	my $available = 0;
	foreach my $f (@ALLOWED_FILE) {
		if ($filename eq $f) {
			$available = 1;
			last;
		}
	}
	if (!$available) {
		binmode(STDOUT, ":utf8");
		TF::CommonUtils::showErrorPage($MSGTABLE->{InvalidFile}, $LANGPARAM);
		return;
	}

	my $filepath = $SETTINGS_DIR . '/' . $filename;

	#
	# write to stream
	#
	downloadfile($filename, $filepath);
}

#
# Read template file
#
sub downloadfile {
	my $filename = shift || undef;
	my $filepath = shift || undef;

	if (!$filename || !$filepath) {
		return;
	}

	if (! -f $filepath) {
		binmode(STDOUT, ":utf8");
		TF::CommonUtils::showErrorPage($MSGTABLE->{MissingModule}, $LANGPARAM);
		return;
	}
	my $clength = (stat($filepath))[7];

	if (open F_CL, "<", $filepath) {
		print <<"_EOF_";
Content-type: application/octet-stream\r
Content-Length: $clength\r
Content-Disposition:attachment; filename=$filename\r\n
_EOF_
		my $buff;
		while (read F_CL, $buff, 8192) {
			print $buff;
		}
		close F_CL;
	}
}

