<?php

// A simple PHP script that transforms a wgs84_pos enabled rss2 feed into KML
// http://www.w3.org/2003/01/geo/
// http://www.keyhole.com/kml/kml_doc.html
// 20051025 zoran@kovacevic.nl: initial version
// 20060925 zoran@kovacevic.nl: Simple GeoRSS Point support.

// TODO:
// - make it possible to reverse the sorting (which makes sense if you 
//   geotagged your trip :)
// - link: get 'description' and 'name' from the rss feed, reuse xsl?

function setKmlContentTypeResponse($filename) {
    
// We have the XML content, so set the content-type for kml
    
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
    
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    
header("Content-type:  application/vnd.google-earth.kml+xml");
    
header("Content-Disposition: attachment; filename=" $filename);
    
header("Content-Transfer-Encoding: binary");
}

// The mode parameter defines if a networklink file should be sent or the data file
$mode $_GET["mode"];

// The rss parameter defines the location of the wgs84pos enabled rss2 feed
$rss $_GET["rss"];

$range $_GET["range"];
if (
$range == ""$range 4000;
$tilt $_GET["tilt"];
if (
$tilt == ""$tilt 45;
$heading $_GET["heading"];
if (
$heading == ""$heading 0;

if (
$mode == "" || $rss == "") {

    
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>RSS2 Google Earth</title>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-83520-1";
urchinTracker();
</script>
</head>
<body>

<div style="font-family: verdana; width: 800px;">

<h1>RSS2 Google Earth</h1>

<h2>Fly to RSS locations using Google Earth!</h2> 

<p>This script will generate a Google Earth <a href="http://www.keyhole.com/kml/kml_doc.html#NetworkLink">network link</a>. Open it with <a href="http://earth.google.com">Google Earth</a>. The network link will then call this script again for the geotagged RSS items. Click 'Play Tour' to fly from location to location.</p>

<p>Please enter a <a href="http://www.georss.org/simple.html">Simple GeoRSS</a> or <a href="http://www.w3.org/2003/01/geo/">wgs84_pos</a> enabled rss2 feed.</p>

<p><form method="get">
http://<input type="text" size="80" value="<?= $rss ?>" name="rss"/><input type="hidden" name="mode" value="link"/><input type="submit" value="go!"/><input type="hidden" name="range" value="4000"/><input type="hidden" name="tilt" value="45"/><input type="hidden" name="heading" value="0"/>
</form></p>

<p><small>e.g. <a href="?mode=link&rss=feeds.feedburner.com/zorankovacevic&range=200&tilt=45&heading=0">feeds.feedburner.com/zorankovacevic</a> (please omit http://)</small></p>

<p><small>note: range (4000), tilt (45) and heading (0) are querystring parameters</small></p>

Thanks:
<ul>
<li>Mikel Maron's Google Maps example: <a href="http://brainoff.com/gmaps/rss.html">http://brainoff.com/gmaps/rss.html</a></li>
<li>Geobloggers networklink example: <a href="http://geobloggers.com/feeds/">http://geobloggers.com/feeds/</a></li>
</ul>

Download:
<ul><li><a href="georss2kml.xsl">georss2kml.xsl</a> (GPL licensed)</li><li><a href="rss2kml.phps">rss2kml.php</li></ul>

<p><a href="http://www.kovacevic.nl/blog">Zoran Kovacevic</a></p>

</div>

</body>

</html>
    <?

} else if ($mode == "data") {

    
// Prefix with http:// in order to prevent fopen misuse
    
$rss_file fopen"http://" $rss"r" ) ;
    
$xml_file "";

    
// Read the feed
    
if ( $rss_file ) {
        while (!
feof($rss_file)) {
            
$xml_file .= fgets($rss_file4096);
        }
        
fclose($rss_file);
    } else {
        die (
"Failed to load $rss");
    }

    
// Use this tranformation stylesheet
    
$xsl_file "georss2kml.xsl";

    
// Allocate a new XSLT processor
    
$xh xslt_create();

    
// Read the XML from a string instead of a file
    
$arguments = array(
         
'/_xml' => $xml_file,
    );

    
// Pass parameters
    
$parameters = array(
        
'range' => $range,
        
'tilt' => $tilt,
        
'heading' => $heading
    
);

    
// Process the document
    
setKmlContentTypeResponse("rss2data");
    echo 
xslt_process($xh'arg:/_xml'$xsl_fileNULL$arguments$parameters);

    
xslt_free($xh);

} else if (
$mode == "link") {

    
setKmlContentTypeResponse("rss2link");

    echo 
'<?xml version="1.0" encoding="UTF-8"?>';
?>
<kml xmlns="http://earth.google.com/kml/2.0">
    <NetworkLink>
        <description><?= $rss ?></description>
        <name>rss2kml</name>
        <Url>
            <href><![CDATA[http://<?= $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'?>?mode=data&rss=<?= $rss ?>&range=<?= $range ?>&tilt=<?= $tilt ?>&heading=<?= $heading ?>]]></href>
            <refreshMode>onInterval</refreshMode>
            <refreshInverval>3600</refreshInverval>
        </Url>
    </NetworkLink>
</kml>
<?

}

?>