<?php

require_once('config.php');
require_once(
'kmltemplate.php');
require_once(
'MDB2.php');

$trackId mysql_real_escape_string($_GET['tr']);

if (
strlen($trackId) == 0) {
    die(
'Invalid track: ' $trackId);
}

$dsn "mysql://$dbUsername:$dbPassword@$dbHost/$dbDatabase";

$options = array(
    
'debug' => 2,
    
'result_buffering' => true// needed for the numRows
);

$mdb2 =& MDB2::connect($dsn$options);
if (
PEAR::isError($mdb2)) {
    die(
$mdb2->getMessage());
}

$query "SELECT longitude,lattitude,timestamp
FROM track,point
WHERE track.loguserid = point.loguserid
and point.timestamp between track.startlog and track.endlog
and track.id = '
$trackId'
and track.public = 1
order by timestamp"
;

$res =& $mdb2->query($query);

$coordinates '';

if (
$res->numRows() > 0) {
    while ((
$row $res->fetchRow())) {
        
// Assuming MDB2's default fetchmode is MDB2_FETCHMODE_ORDERED
        
$coordinates $coordinates $row[0] . "," $row[1] . "\n";
        
$lastpointTime $row[2];
    }
} else {
    die(
'Invalid track');
}

$dateTime date_create($lastpointTime);
$strDateTime $dateTime->format('D, j M Y H:i:s');

// Output the result
header("content-type:application/vnd.google-earth.kml+xml");
header("Last-Modified: " $strDateTime);
echo(
str_replace('###coordinates###',$coordinates,$kml));

$mdb2->disconnect();
?>