\n"; Syntax(); } if (! isset($_SERVER['argv'][1])) { echo "Specify the GPX file as the second parameter.\n"; Syntax(); } $gpxfile = $_SERVER['argv'][1]; if (! file_exists($gpxfile) || ! is_file($gpxfile)) { echo "$gpxfile is not a file.\n"; Syntax(); } $fp = fopen($gpxfile, "r"); if (! $fp) { echo "Unable to open $gpxfile\n"; exit(); } $stack = array(); $parser = xml_parser_create(); $WPList = array(); xml_set_element_handler($parser, "start_element", "stop_element"); xml_set_character_data_handler($parser, "char_data"); $buf = fgets($fp, 8192); while (! feof($fp) && xml_parse($parser, $buf, feof($fp))) { $buf = fgets($fp, 8192); } xml_parser_free($parser); fclose($fp); ProcessIndex(); function start_element($parser, $name, $attrs) { global $char_data, $stack, $Info; $Now = join(' ', $stack) . ' ' . $name; if ($Now == 'GPX WPT') { $Info = array('Name' => 'Unknown'); $Info['RealLat'] = $attrs['LAT']; $Info['RealLon'] = $attrs['LON']; $Info['Lat'] = ProcessDeg($attrs['LAT'], 'N', 'S'); $Info['Lon'] = ProcessDeg($attrs['LON'], 'E', 'W', true); } elseif ($name == 'GROUNDSPEAK:LOG') { if (! isset($Info['LogNum'])) { $Info['Logs'] = array(); $Info['LogNum'] = 0; } else $Info['LogNum'] ++; } $char_data = ''; array_push($stack, $name); } function stop_element($parser, $name) { global $char_data, $stack, $Info, $WPList; $Now = join(' ', $stack); if ($Now == 'GPX WPT') ProcessWaypoint($Info); elseif ($Now == 'GPX WPT NAME') $Info['Name'] = $char_data; elseif ($Now == 'GPX WPT URL') $Info['URL'] = $char_data; elseif ($Now == 'GPX WPT URLNAME') $Info['URLName'] = $char_data; elseif (strncmp($Now, 'GPX WPT GROUNDSPEAK:CACHE GROUNDSPEAK:LOGS ', 43) == 0) { if ($name == 'GROUNDSPEAK:DATE') $Info['Logs'][$Info['LogNum']]['Date'] = $char_data; elseif ($name == 'GROUNDSPEAK:TYPE') $Info['Logs'][$Info['LogNum']]['Type'] = $char_data; elseif ($name == 'GROUNDSPEAK:FINDER') $Info['Logs'][$Info['LogNum']]['Finder'] = $char_data; elseif ($name == 'GROUNDSPEAK:TEXT') $Info['Logs'][$Info['LogNum']]['Text'] = HTMLDecode($char_data); } elseif ($Now == 'GPX WPT GROUNDSPEAK:CACHE GROUNDSPEAK:TRAVELBUGS ' . 'GROUNDSPEAK:TRAVELBUG GROUNDSPEAK:NAME') { $Info['TravelBugs'][] = $char_data; } elseif (strncmp($Now, 'GPX WPT GROUNDSPEAK:CACHE ', 26) == 0) { if ($name == 'GROUNDSPEAK:PLACED_BY') $Info['By'] = $char_data; elseif ($name == 'GROUNDSPEAK:TYPE') $Info['Type'] = $char_data; elseif ($name == 'GROUNDSPEAK:CONTAINER') $Info['Container'] = $char_data; elseif ($name == 'GROUNDSPEAK:DIFFICULTY') $Info['Difficulty'] = $char_data; elseif ($name == 'GROUNDSPEAK:TERRAIN') $Info['Terrain'] = $char_data; elseif ($name == 'GROUNDSPEAK:SHORT_DESCRIPTION') $Info['Short Desc'] = $char_data; elseif ($name == 'GROUNDSPEAK:LONG_DESCRIPTION') $Info['Long Desc'] = $char_data; elseif ($name == 'GROUNDSPEAK:ENCODED_HINTS') $Info['Hints'][] = HTMLDecode($char_data); } $elem = array_pop($stack); } function char_data($parser, $data) { global $char_data; $char_data .= $data; } function HTMLDecode($str) { $str = str_replace(">", ">", $str); $str = str_replace("<", "<", $str); $str = str_replace("&", "&", $str); $str = str_replace("
", "\n", $str); return $str; } function ProcessDeg($deg, $pos, $neg, $three = false) { if ($deg > 0) { $str = $pos; } else { $str = $neg; $deg = - $deg; } $d = $deg; settype($d, 'integer'); $deg -= $d; if ($three && $d < 100) $str .= '0'; if ($d < 10) $str .= '0'; $str .= $d . ''; $deg *= 60; $deg *= 1000; settype($deg, 'integer'); $deg /= 1000; $str .= $deg . "'"; return $str; } function Syntax() { echo "php4-cgi gcd2html.php \n"; exit(); } function ProcessWaypoint($Info) { global $WPList, $WPToFile; $html_file = strtolower($Info['Name']); $dir = substr($html_file, -1, 1); CheckDir($dir); $html_file = $dir . '/' . $html_file; $WPList[$Info['Name']] = array( 'Name' => $Info['Name'], 'URLName' => $Info['URLName'], 'URL' => $Info['URL'], 'Lat' => $Info['RealLat'], 'Lon' => $Info['RealLon'], 'Difficulty' => $Info['Difficulty'], 'Terrain' => $Info['Terrain'], 'Type' => $Info['Type'], 'File' => $html_file, ); $fp = CreateFile($html_file . '.html'); fwrite($fp, HTMLHeader($Info['Name'])); fwrite($fp, WaypointDesc($Info)); fwrite($fp, HTMLFooter()); fclose($fp); } function CreateFile($fn) { $fp = fopen($fn, 'w'); if (! $fp) { echo "Unable to open $fn for output.\n"; exit(); } return $fp; } function HTMLHeader($title) { $s = '' . htmlspecialchars($title) . ''; $s .= '' . "\n"; return $s; } function HTMLFooter() { return ''; } function WaypointDesc($Info) { ob_start(); ?>

- ()

 

By

(Difficulty , Terrain )

Online Page

Travel Bugs:



Hints:


()

Pick Cache by Waypoint:

' . "\n"); $ByLetter = array(); foreach (array_keys($WPList) as $k) { if (! isset($ByLetter[$k[2]])) $ByLetter[$k[2]] = array(); $ByLetter[$k[2]][] = $k; } ksort($ByLetter); fwrite($fp, '

GC... :: '); CheckDir('wp'); foreach ($ByLetter as $letter => $hits) { $url = 'wp/' . $letter . '.html'; fwrite($fp, '' . $letter . ' '); WriteSubWPIndex($letter, $url, $hits); } fwrite($fp, "

\n"); fwrite($fp, "

Pick Cache by Name

\n"); $picker = array(); CheckDir('name'); foreach ($WPList as $k => $wp) { $name = $wp['URLName']; $name = strtoupper($name); $name = preg_replace('/[^A-Z0-9]/', '', $name); if (! isset($picker[$name[0]])) $picker[$name[0]] = array(); $picker[$name[0]][$name] = $wp; } ksort($picker); fwrite($fp, '

'); CheckDir('name'); foreach ($picker as $letter => $hits) { $url = 'name/' . $letter . '.html'; fwrite($fp, '' . $letter . ' '); WriteSubIndex($letter, $url, $hits); } fwrite($fp, "

\n"); fwrite($fp, HTMLFooter()); fclose($fp); } function CheckDir($dir) { if (! is_dir($dir)) { if (! mkdir($dir)) { echo "Unable to make directory $dir\n"; exit(); } } } function WriteSubIndex($letter, $url, $hits) { global $WPList; $fp = CreateFile($url); fwrite($fp, HTMLHeader("Caches : $letter")); ksort($hits); fwrite($fp, '

'); foreach ($hits as $name => $wp) { fwrite($fp, '' . htmlspecialchars($wp['URLName']) . "
\n"); } fwrite($fp, "

\n"); fwrite($fp, HTMLFooter()); fclose($fp); } function WriteSubWPIndex($letter, $url, $hits) { global $WPList; $fp = CreateFile($url); fwrite($fp, HTMLHeader("Waypoints : GC$letter...")); sort($hits); fwrite($fp, '

'); foreach ($hits as $k) { fwrite($fp, '' . $k . "
\n"); } fwrite($fp, "

\n"); fwrite($fp, HTMLFooter()); fclose($fp); }