<?php
/**
 * KML Joiner
 * by Adam Franco
 *
 * http://www.adamfranco.com/?p=60
 *
 *
 * This KML combiner script will take an array of input URLs with optional titles 
 * and output a KML document that contains netlinks to each of those files. This
 * allows the user of the script to easily join together several KML data sets
 * without hand coding any KML markup.
 *
 *
 * @since 8/28/07
 * @package com.adamfranco.kml
 * 
 * @copyright Copyright &copy; 2007, Adam Franco
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
 *
 * @version $Id$
 */ 
 
ini_set('display_errors'true);
ini_set('magic_quotes_gpc'false);

 
// Generate the KML for later use.
$urlsFound false;
if (isset(
$_REQUEST['urls'])) {
    
ob_start();
    
    print 
"<"."?xml version='1.0' encoding='utf-8'?".">";
    print 
'
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>'
;
    if (
$_REQUEST['title'])
        print 
"\n\t<name>".htmlspecialchars($_REQUEST['title'])."</name>";
    else
        print 
"\n\t<name>Untitled KML Collection</name>";
    
    if (
$_REQUEST['description'])
        print 
"\n\t<description><![CDATA[".$_REQUEST['description']."]]></description>";
    
    foreach (
$_REQUEST['urls'] as $key => $url) {
        if (
$url && $url != 'http://') {
            print 
"\n\t<NetworkLink>";
            if (
$_REQUEST['titles'][$key])
                print 
"\n\t\t<name>".htmlspecialchars($_REQUEST['titles'][$key])."</name>";
            else
                print 
"\n\t\t<name>Untitled KML Document</name>";
                
            print 
"\n\t\t<Url>";
            print 
"\n\t\t\t<href>".htmlspecialchars($url)."</href>";
            if (
intval($_REQUEST['refresh'][$key])) {
                print 
"\n\t\t\t<refreshMode>onInterval</refreshMode>";
                print 
"\n\t\t\t<refreshInterval>".intval($_REQUEST['refresh'][$key])."</refreshInterval>";
            }
            print 
"\n\t\t</Url>";
            print 
"\n\t</NetworkLink>";
            
            
$urlsFound true;
        }
    }
    print 
'
</Document>
</kml>
'
;
    
$kml ob_get_clean();
    
    
// If the KML document is asked for, output it and exit;
    
if (!isset($_REQUEST['output']) || $_REQUEST['output'] == 'kml') {
//         header("Content-type: text/plain; charset=utf-8");
        
header("Content-Type: application/vnd.google-earth.kml+xml;");
        
header('Content-Disposition: attachment; filename="'.$photoset->title.'.kml"');
        print 
$kml;
        exit;
    } else {
        
$kmlUrl "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
        
$kmlUrl .= '?';
        
$kmlUrl .= preg_replace('/(&)?output=show_[a-z]+/i'''$_SERVER['QUERY_STRING']);
    }
    
    
// Open google maps if requested.
    
if (!isset($_REQUEST['output']) || $_REQUEST['output'] == 'show_map') {
        
$url "http://maps.google.com/maps?f=q&hl=en&ie=UTF8&om=1&t=h&q=".rawurlencode($kmlUrl);
        
header("Location: $url");
        exit;
    }
}

header("Content-type: text/html; charset=utf-8");
print 
"<"."?xml version='1.0' encoding='utf-8'?".">";
?>
<!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' lang='en' xml:lang='en'>
<head>
    <title>KML Joiner by Adam Franco</title>
    <style type='text/css'>
        .about {
            font-size: small;
            margin-top: 50px;
        }
        
        form div {
            margin-top: 10px;
            margin-bottom: 10px;
        }
        
        .new_url {
            background-color: #ccc;
            padding: 10px;
        }
        
        form div.urls {
            margin-left: 20px;
        }
        
        form div.urls span.input {
            white-space: nowrap;
        }
        .notes {
            margin-left: 30px;
            font-size: small;
        }
        
        form div.submit {
            margin-top: 20px;
        }
    
    </style>
    
    <script type='text/javascript'>
    // <![CDATA[
    
        /**
         * Add a set of form fields to the document
         * 
         * @param DomElement containerElement
         * @return void
         * @access public
         * @since 8/28/07
         */
        function addUrl (containerElement) {
            var newNode = containerElement.firstChild.nextSibling.cloneNode(true);
            var inputs = newNode.getElementsByTagName('input');
            inputs[0].value = 'http://';
            inputs[1].value = '';
            inputs[2].value = '';
            inputs[3].style.display = 'inline';
            containerElement.insertBefore(newNode, containerElement.lastChild.previousSibling);
        }
    
    // ]]>
    </script>
</head>
<body>
    <h1>KML Joiner</h1>
<?php
    
if ($urlsFound && isset($_REQUEST['output'])) {
//         print "\n<pre>"; print_r($_SERVER); print "</pre>";
        
print "\n\t<p class='new_url'>The url of your new document: <br/><input type='text' size='80' value='".$kmlUrl."'/>";
        
        if (
$_REQUEST['output'] =='show_kml' && isset($kml)) {
            print 
"\n\t\t<br/>";
            print 
"\n\t\t<textarea cols='80' rows='10'>".htmlentities($kml)."</textarea>";
        }
        print 
"</p>";
    }
?>
    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='get'>
        <div>
            <input type='radio' name='output' value='show_url' <?php 
    
if (!isset($_REQUEST['output']) || $_REQUEST['output'] == 'show_url') {
        print 
" checked='checked'"
    } 
            
?> /> Display URL
            
            <br/><input type='radio' name='output' value='show_kml' <?php 
    
if (isset($_REQUEST['output']) && $_REQUEST['output'] == 'show_kml') { 
        print 
" checked='checked'"
    } 
            
?>/> Display KML
            
            <br/><input type='radio' name='output' value='show_map' <?php 
    
if (isset($_REQUEST['output']) && $_REQUEST['output'] == 'show_map') { 
        print 
" checked='checked'"
    } 
            
?>/> Open in Google Maps
            
            <br/><input type='radio' name='output' value='kml' <?php 
    
if (isset($_REQUEST['output']) && $_REQUEST['output'] == 'kml') { 
        print 
" checked='checked'"
    } 
            
?>/> Download KML
        </div>
        <div>New document title: <br/><input type='text' name='title' size='40' value='<?php echo $_REQUEST['title']; ?>'/></div>        
        <div>New document description: <br/><textarea name='description' cols='40' rows='6'><?php echo $_REQUEST['description']; ?></textarea></div>
        
        <div class='urls'>
        
<?php
    
    
for ($i 0$i || $i count($_REQUEST['urls']); $i++) {
        print 
"\n\t\t\t<div>";
        
        print 
"\n\t\t\t\t<span class='input'>Document URL: <input type='text' name='urls[]' value='";
        if (isset(
$_REQUEST['urls'][$i])) 
            print 
$_REQUEST['urls'][$i];
        else
            print 
"http://";
        print 
"' size='40'/></span> ";
        
        print 
"\n\t\t\t\t<span class='input'>Document Title (optional): <input type='text' name='titles[]' value='";
        if (isset(
$_REQUEST['titles'][$i])) 
            print 
$_REQUEST['titles'][$i];
        print 
"'/></span> ";
        
        print 
"\n\t\t\t\t<span class='input'>Refresh every (optional): <input type='text' name='refresh[]' size='5' value='";
        if (isset(
$_REQUEST['refresh'][$i])) 
            print 
$_REQUEST['refresh'][$i];
        print 
"'/></span> seconds</span> ";
        
        print 
"\n\t\t\t\t<input type='button' value='-' onclick='this.parentNode.parentNode.removeChild(this.parentNode);' ";
        if (
$i 2
            print 
"style='display: none;'";
        print 
"/>";
        
        print 
"\n\t\t\t</div>";
    }
?>
            
            <input type='button' name='add' onclick='addUrl(this.parentNode);' value='Add Document URL'/>
        </div>
        
        <div class='submit'><input type='submit'/> <input type='button' name='clear' onclick='window.location="<?php echo $_SERVER['PHP_SELF']; ?>";' value='Clear Form'/></div>
    </form>
    <p class='about'>KML Joiner was written by <a href='http://www.adamfranco.com'>Adam Franco</a> and is licensed under the <a href='http://www.gnu.org/copyleft/gpl.html'>GNU General Public License (GPL)</a> version 3 or later.
    <br/><br/><a href='http://www.adamfranco.com/?p=60'>More information</a>.</p>
</body>
</html>