<?php

// fixme: what if a url outputted has a " in it

include 'rb-include.php';
rb_header(true);

$r = pg_exec($db, "select url from rdf_files where id='$id' and owner='$uid'");
if (pg_numrows($r) == 0) {
	pg_freeresult($r);
	exit(1);
}
$url = pg_result($r, 0, 0);
pg_freeresult($r);

pg_exec($db, "update rdf_files set last_output=current_timestamp where id='$id'");

// fix for IE catching or PHP bug issue
header('Pragma: public');
header('Expires: 0'); // set expiration time
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

// force download
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Type: application/download');

header('Content-Disposition: attachment; filename=' . basename($url) . ';');

echo '<?xml version="1.0"?>', "\n";
?>
<rdf:RDF xmlns="http://gnomoradio.org/gnomoradio/1.0.rdf#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:dcterms="http://purl.org/dc/elements/1.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:cc="http://web.resource.org/cc/"
    xmlns:rbow="http://gnomoradio.org/rainbow/1.0.rdf#">

<?php
$songs_res = pg_exec($db, "select id, rdf_id, title, artist, lyrics, license, length from songs where owner='$uid' and rdf_file='$id'");
for ($song = 0; $song < pg_numrows($songs_res); $song++) {
	$artists_res = pg_exec($db, "select rdf_file, rdf_id, url from artists, rdf_files where artists.id='" . pg_result($songs_res, $song, 3) . "' and rdf_file=rdf_files.id");
	$artist_url = '';
	if (pg_result($artists_res, 0, 0) != $id)
		$artist_url = pg_result($artists_res, 0, 2);
	$artist_url .= '#' . pg_result($artists_res, 0, 1);
	pg_freeresult($artists_res);
?>
<Recording rdf:ID="<?php echo pg_result($songs_res, $song, 1); ?>">
  <dc:title><?php echo htmlentities(pg_result($songs_res, $song, 2), ENT_NOQUOTES); ?></dc:title>
  <dc:creator rdf:resource="<?php echo $artist_url; ?>"/>
  <cc:license rdf:resource="<?php echo pg_result($songs_res, $song, 5); ?>"/>
  <length><?php echo pg_result($songs_res, $song, 6); ?></length>
<?php
	$afs_res = pg_exec($db, "select id, rdf_id, filesize, checksum, format from audiofiles where song='" . pg_result($songs_res, $song, 0) . "' and owner='$uid'");
	for ($af = 0; $af < pg_numrows($afs_res); $af++) {
		echo '  <audiofile rdf:resource="#', pg_result($afs_res, $af, 1), '"/>', "\n";
	}
	$lyrics = pg_result($songs_res, $song, 4);
	if ($lyrics) {
		echo '  <lyrics>', "\n", nl2br(htmlentities(lyrics, ENT_NOQUOTES)), '  </lyrics>', "\n";
	}
	echo '</Recording>', "\n";
	echo "\n";

	for ($af = 0; $af < pg_numrows($afs_res); $af++) {
		echo '<rbow:Resource rdf:ID="', pg_result($afs_res, $af, 1), '">', "\n";
		$available_res = pg_exec($db, "select url from available where owner='$uid' and audiofile='" . pg_result($afs_res, $af, 0) . "'");
		for ($available = 0; $available < pg_numrows($available_res); $available++) {
			echo '  <rbow:available rdf:resource="', pg_result($available_res, $available, 0), '"/>', "\n";
		}
		pg_freeresult($available_res);
		echo '  <representationOf rdf:resource="#', pg_result($songs_res, $song, 1), '"/>', "\n";
		echo '  <cc:license rdf:resource="', pg_result($songs_res, $song, 5), '"/>', "\n";
		echo '  <dc:format>', pg_result($afs_res, $af, 4), '</dc:format>', "\n";
		echo '  <rbow:checksum>', pg_result($afs_res, $af, 3), '</rbow:checksum>', "\n";
		echo '  <rbow:size>', pg_result($afs_res, $af, 2), '</rbow:size>', "\n";
		echo '</rbow:Resource>', "\n";
		echo "\n";
	}
	pg_freeresult($afs_res);
}
pg_freeresult($songs_res);

$artists_res = pg_exec($db, "select rdf_id, name, website from artists where owner='$uid' and rdf_file='$id'");
for ($artist = 0; $artist < pg_numrows($artists_res); $artist++) {
	echo '<cc:Agent rdf:ID="', pg_result($artists_res, $artist, 0), '">', "\n";
	echo '  <dc:title>', htmlentities(pg_result($artists_res, $artist, 1), ENT_NOQUOTES), '</dc:title>', "\n";
	if (pg_result($artists_res, $artist, 2))
		echo '  <website rdf:resource="', pg_result($artists_res, $artist, 2), '"/>', "\n";
	echo '</cc:Agent>', "\n";
	echo "\n";
}
pg_freeresult($artists_res);

$artists_res = pg_exec($db, "select rdf_id, name, website, url from artists, rdf_files where rdf_file=rdf_files.id and artists.owner='$uid' and rdf_file != '$id' and artists.id in (select artist from songs where owner='$uid' and rdf_file='$id')");
for ($artist = 0; $artist < pg_numrows($artists_res); $artist++) {
	echo '<cc:Agent rdf:about="', pg_result($artists_res, $artist, 3), '#', pg_result($artists_res, $artist, 0), '">', "\n";
	echo '  <dc:title>', htmlentities(pg_result($artists_res, $artist, 1), ENT_NOQUOTES), '</dc:title>', "\n";
	echo '  <website rdf_resource="', pg_result($artists_res, $artist, 2), '"/>', "\n";
	echo '</cc:Agent>', "\n";
	echo "\n";
}
pg_freeresult($artists_res);

$licenses_res = pg_exec($db, "select license_url, rdf from license_descriptions where license_url in (select license from songs where rdf_file='$id')");
for ($license = 0; $license < pg_numrows($licenses_res); $license++) {
	echo pg_result($licenses_res, $license, 1), "\n";
}
pg_freeresult($licenses_res);

?>
</rdf:RDF>
