// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
$publicAccess = true;
include('authenticate.php');
include('request_interface.php');
$error = '';
if ($allowDownload && $download)
{
// query file to BSs
$query = "*.*.BS.getFileBase64Content $file";
$qstate = nel_query($query, $commandResult);
//echo "$commandResult";
if ($commandResult)
{
$res_array = explode("\n", $commandResult);
$parse_start = 0;
while (true)
{
if ($parse_start >= count($res_array))
break;
$offset = 4;
list($res_shard) = sscanf($res_array[$parse_start], "----- Result from Shard %s");
list($dld_file, $num_res, $file_size, $originalMD5) = sscanf($res_array[$parse_start+$offset-1], "file %s lines %d size %d haskey %s");
$start = $parse_start+$offset;
$stop = $start+$num_res;
$parse_start += $num_res+$offset;
if ($num_res == 0)
continue;
$buffer = '';
for ($line=$start; $line<$stop; ++$line)
$buffer .= trim($res_array[$line]);
$decoded = base64_decode($buffer);
$decodedMD5 = md5($decoded);
if ($originalMD5 != '' && $originalMD5 != $decodedMD5)
{
$error = "ERROR: failed to download file '$file', MD5 signature indicates file is corrupted.";
}
else
{
header("Content-type: application/bin");
header("Content-Disposition: attachment; filename=".basename($file));
echo $decoded;
die();
}
}
}
if (!$error)
$error = "ERROR: failed to download file '$file', file may not exist on any server.";
}
if ($allowUpload && $upload)
{
$f = fopen($upld_file, "rb");
if ($f)
{
$content = base64_encode(fread($f, $upld_file_size));
$query = $shard_addr.".putFileBase64Content $file $content";
$qstate = nel_query($query, $commandResult);
}
}
htmlProlog($_SERVER['PHP_SELF'], "Backup Interface (Character Up/Download)");
// input variables :
// - $charid : character id to upload/download
// - $file : filename to upload/download
//
$query = "*.*.BS.State";
$qstate = nel_query($query, $result);
if ($qstate)
{
$arr = explode(' ', $result);
$numRes = count($arr);
$numRows = current($arr);
$numLines = ($numRes-$numRows-2)/$numRows;
next($arr);
for ($i=0; $i<$numRows; ++$i)
{
$vars[] = current($arr);
next($arr);
}
unset($shards);
for ($i=0; $i<$numLines; ++$i)
{
unset($l);
foreach($vars as $var)
{
$l[$var] = current($arr);
next($arr);
}
$shards[] = $l;
}
}
echo "
\n";
if ($error)
{
echo "$error
\n";
}
if ($allowDownload)
{
echo "
";
echo "
";
}
if ($allowUpload)
{
echo "";
}
/*
echo "\n";
echo "\n";
*/
htmlEpilog();
?>