<?php
session_start
();
/* {{{ The HTML part */
// The Title of what you are writing
$TITLE="Tomtom and Google";

// The text. <h2></h2> is the first paragraph.
// Escape all caracters
$TEXT='<h2>Google has added a new feature with wich you can drag around your route. How this works is explained on this page in the movie below. Basicaly you just make your iternarary with a great interface. Very easy and much easier then doing it on your Tomtom. Added fun is that you can do it from any machine in the world.</h2>
<p>I turned <a href="http://maps.google.com/maps?f=d&saddr=Rotselaarsebaan&daddr=Dutselstraat+to:Leempoelstraat+to:Streekweg+to:Alsbergweg+to:Heuvelstraat+to:Hoensberg+to:50.993122,4.942818+to:Testeltsesteenweg+to:Vennestraat+to:Madestraat+to:Moorsemsestraat+to:Tremelosesteenweg+to:Beemdenstraat+to:Steenweg+op+Gelrode+to:Aarschotsesteenweg%2FN19&hl=en&geocode=FV4eCQMdaG5IAA%3BFX4PCQMd-jhJAA%3BFeDwCAMdrIdJAA%3BFT4nCQMdWxVKAA%3BFY4aCQMdRKNKAA%3BFTZKCQMd8GRLAA%3BFSCyCQMdR1dLAA%3B%3BFS5CCgMdKsxKAA%3BFaNKCgMdoktKAA%3BFc5-CgMduJlJAA%3BFTonCgMd_ppIAA%3BFTYXCgMdUOlIAA%3BFRTgCQMdSydJAA%3BFeynCQMd5DdIAA%3BFWTbCAMdGPBHAA&mra=dpe&mrcr=0&mrsp=7&sz=13&via=1,2,3,4,5,6,7,8,9,10,11,12,13,14&sll=50.974751,4.880333&sspn=0.083985,0.253029&ie=UTF8&ll=50.954318,4.837761&spn=0.168044,0.506058&z=12">this link</a> into <a href="rotselaarsebaan_to_aarschotsesteenweg_n19.itn">this file</a>.</p>    
<p><h2>This is the <a href="http://maps.google.com/maps?f=d" TARGET="Google">Google Maps</a> to Tomtom iternerary converter</h2>
<p>More explanation <a href="tomtom.php">on this page</a></p>
<p><form method="post">
<input name="url" size="100" /><br>
<input name="submit" type="submit" />
</form></p>
'
;
include(
"../main_text2.inc");
/* }}} */ 
/* {{{ function to retrieve the file and save it */
function cURLcheckBasicFunctions()
{
  if( !
function_exists("curl_init") &&
      !
function_exists("curl_setopt") &&
      !
function_exists("curl_exec") &&
      !
function_exists("curl_close") ) return false;
  else return 
true;
}

/*
 * Returns string status information.
 * Can be changed to int or bool return types.
 */
function cURLdownload($url$file)
{
  if( !
cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
  
$ch curl_init();
  if(
$ch)
  {
    
$fp fopen($file"w");
    if(
$fp)
    {
      if( !
curl_setopt($chCURLOPT_URL$url) ) return "FAIL: curl_setopt(CURLOPT_URL)";
      if( !
curl_setopt($chCURLOPT_FILE$fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
      if( !
curl_setopt($chCURLOPT_HEADER0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
      if( !
curl_exec($ch) ) return "FAIL: curl_exec()";
      
curl_close($ch);
      
fclose($fp);
//      return "SUCCESS: $file [$url]";
    
}
    else return 
"FAIL: fopen()";
  }
  else return 
"FAIL: curl_init()";
}
/* }}} */ 
/* {{{ Actual download */
if (!isset($_POST['submit'])) die();
$URL $_POST['url']."&output=kml";
echo 
cURLdownload("$URL""upload/maps.xml");
/* }}} */ 
/* {{{ Opening of the XML file */
include_once ("xmlize.php");
$myFile "upload/maps.xml";
$fh fopen($myFile'r');
$data fread($fhfilesize($myFile));
fclose($fh);

$xml xmlize($data); # where $data is the xml in the above section.
/* }}} */ 
/* {{{ Show the filename */
$name $xml["kml"]["#"]["Document"];
for(
$i 0$i sizeof($name); $i++) {
    
$file $name[$i];
    
$filename $file["#"]["name"][0]["#"];
    
$filename strtolower(str_replace(" ","_",$filename));
//    print "Filename will be : $filename.itn<br>\n<hr>\n";
}
/* }}} */ 
/* {{{  The iternerary */
/* {{{  Next line shortens it for the rest */
$itinerary $xml["kml"]["#"]["Document"][0]["#"]["Placemark"];
/* }}} */ 
/* {{{ The first line */
$Placemark $itinerary[0];
$name        $Placemark["#"]["name"][0]["#"];
preg_match("/(?<=\040)([^\s]+?)$/",trim($name),$matches);
$name $matches[0];
$longitude $Placemark["#"]["LookAt"][0]["#"]["longitude"][0]["#"];
$longitude $longitude 100000;
$latitude  $Placemark["#"]["LookAt"][0]["#"]["latitude"][0]["#"];
$latitude  $latitude  100000;
$LINE $longitude."|".$latitude."|".$name."|4|\n";
/* }}} */ 
/* {{{ The second line till almost the end */
for($i 1$i sizeof($itinerary)-3$i++) {

    
$Placemark $itinerary[$i];
    
$name        $Placemark["#"]["name"][0]["#"];
    
preg_match("/(?<=\040)([^\s]+?)$/",trim($name),$matches);
    
$name $matches[0];
    
$longitude $Placemark["#"]["LookAt"][0]["#"]["longitude"][0]["#"];
    
$longitude $longitude 100000;
    
$latitude  $Placemark["#"]["LookAt"][0]["#"]["latitude"][0]["#"];
    
$latitude  $latitude  100000;
    
$NEWLINE $longitude."|".$latitude."|".$name."|1|\n";
    
$LINE $LINE.$NEWLINE;
}
/* }}} */ 
/* {{{ The last line (well the last two need to be deleted) */
$i sizeof($itinerary)-3;
$Placemark $itinerary[$i];
$name        $Placemark["#"]["name"][0]["#"];
preg_match("/(?<=\040)([^\s]+?)$/",trim($name),$matches);
$name $matches[0];
$longitude $Placemark["#"]["LookAt"][0]["#"]["longitude"][0]["#"];
$longitude $longitude 100000;
$latitude  $Placemark["#"]["LookAt"][0]["#"]["latitude"][0]["#"];
$latitude  $latitude  100000;
$NEWLINE $longitude."|".$latitude."|".$name."|3|\n";
$LINE $LINE.$NEWLINE;
/* }}} */ 
/* {{{ Opening the file to save it */

@ob_end_clean();
// Show all the info on a page
$_SESSION['LINE'] = $LINE;
$_SESSION['filename'] = $filename;
$TEXT $TEXT.'
    <iframe src="file.php" width="0" height="0" FRAMEBORDER="0">
    Copy the content in the box below into '
.$filename.'.itn 
    </iframe>'
;
$TEXT $TEXT.'
<p>If you did not save the file, you can safe the content in the box below into '
.$filename.'.itn and put that on your Tomtom. You can download the xml file called maps.kml <a href="'.$URL.'">right here</a>.</p>
<pre>'
.$LINE.'</pre>';
include(
"../main_text2.inc");    
/* }}} */ 
/* }}} */ 
/* {{{ Traverse to see what the parameters are */
//print "<hr>\n";
//print "All the rest<br>\n";
//traverse_xmlize($xml, 'xml');
//print "<pre>".implode("", $traverse_array)."</pre>";
/* }}} */ 
?>