[Box Backup] running bbackupquery in a browser (problem solved)

Jarkko Haapalainen boxbackup@fluffy.co.uk
Thu, 28 Feb 2008 11:27:54 +0200


--Apple-Mail-9-397892027
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed;
	delsp=yes
Content-Transfer-Encoding: 7bit

Hi,

Tried to sent this some time ago, but here again.

I made quick version of mine for browsing and restoring files from  
browser so look at the attached file. It only took ~1,5 hours so there  
is probably bugs, but is working nicely for me in my mac ;)

Best regards, Jarkko


--Apple-Mail-9-397892027
Content-Disposition: attachment;
	filename=bbackupquery.php
Content-Type: text/php;
	x-unix-mode=0644;
	name="bbackupquery.php"
Content-Transfer-Encoding: 7bit

<?php
$bquery = 'sudo /usr/local/bin/bbackupquery';
$chown  = 'sudo /usr/sbin/chown _www';
$tmpdir = '/tmp';

function path_to_arr($bquery, $path = null) {
	$cmd = sprintf('%s \'list -t "%s"\' quit', $bquery, rawurldecode($path));
	$out = shell_exec($cmd);
	preg_match_all('/^(\w{8})\s(\D{6})\s(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})\s([^\n]*)\n/m', $out, $matches, PREG_SET_ORDER);
	return array('path' => $path, 'files' => $matches);
}

function arr_to_html($m) {
	$html = '';
	$path = $m['path'];
	foreach ($m['files'] as $object) {
		list ($line, $id, $flags, $datetime, $name) = $object;
		if (strpos($flags, 'd') === 1) {
			$link = sprintf('<a href="?cwd=%s">%s</a>', "$path/$name", $name);
		} else {
			$link = sprintf('<a href="?cwd=%s&amp;get=%s&amp;name=%s">%s</a>', "$path", $id, $name, $name);
		}
		$html .= "<tr><td>$id</td><td>$flags</td><td>$datetime</td><td>$link</td></tr>";
	}
	return $html;
}

function get_file($bquery, $chown, $tmpdir, $path, $file, $filename) {
	$new = "$tmpdir/".md5($file);
	$cmd = sprintf('%s \'cd "%s"\' \'get -i %s %s\' quit', $bquery, rawurldecode($path), rawurldecode($file), $new);
	$out = shell_exec($cmd);
	if (file_exists($new)) {
		exec("$chown '$new'");
		$fp = fopen($new, 'rb');
		header("Pragma: public");
		header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
		header("Cache-Control: private",false);
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-Type: application/octet-stream");
		header("Content-Disposition: attachment; filename=\"".rawurldecode(utf8_decode($filename))."\"");
		header("Content-Length: " . filesize($new));
		fpassthru($fp);
		unlink($new);
		exit;
	} else {
		echo "<pre>ERROR: file restore failed\n";
		var_dump($cmd, $path, $file);
		echo "</pre>";
	}
}

if (!empty($_GET['get'])) {
	get_file($bquery, $chown, $tmpdir, $_GET['cwd'], $_GET['get'], $_GET['name']);
	exit;
}
?>

<html>
<head>
	<title>bbackupquery wrapper</title>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<style>
	html, body {
		font-family: verdana;
	}
	table.list thead td {
		border-bottom: 1px solid #aaa;
	}
	table.list tbody td {
		padding: 4px 6px 4px 6px;
		border-right: 1px solid #aaa;
		border-bottom: 1px solid #aaa;
	}
	</style>
</head>
<body>
<table class="list">
<thead>
	<tr>
		<td>id</td>
		<td>flags</td>
		<td>datetime</td>
		<td>name</td>
	</tr>
</thead>
<tbody>
<?= arr_to_html( path_to_arr($bquery, $_GET['cwd']) ); ?>
</tbody>
</table>
</body>
</html>
--Apple-Mail-9-397892027
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed
Content-Transfer-Encoding: 7bit






--Apple-Mail-9-397892027--