Esempio n. 1
0
function outputXmlFile($sessionid, $filenr, $bXmlDecl, $bOcXmlTag, $bDocType, $ziptype)
{
    global $zip_basedir, $zip_wwwdir, $sDateformat, $sDateshort, $t1, $t2, $t3, $safemode_zip, $safemode_zip, $sCharset, $bAttrlist;
    global $absolute_server_URI, $bLicense, $sLanguage;
    global $ocxmlversion;
    // alle records aus tmpxml_* übertragen
    if (!mb_ereg_match('^[0-9]{1,11}', $sessionid)) {
        die('sessionid invalid');
    }
    if (!mb_ereg_match('^[0-9]{1,11}', $filenr)) {
        die('filenr invalid');
    }
    /* begin now a few dynamically loaded constants */
    $logtypes = array();
    $rs = sql('SELECT `id`, `de` FROM log_types');
    for ($i = 0; $i < mysql_num_rows($rs); $i++) {
        $r = sql_fetch_array($rs);
        $logtypes[$r['id']] = $r['de'];
    }
    mysql_free_result($rs);
    $cachetypes = array();
    $rs = sql('SELECT `id`, `short`, `de` FROM cache_type');
    for ($i = 0; $i < mysql_num_rows($rs); $i++) {
        $r = sql_fetch_array($rs);
        $cachetypes[$r['id']]['de'] = $r['de'];
        $cachetypes[$r['id']]['short'] = $r['short'];
    }
    mysql_free_result($rs);
    $cachestatus = array();
    $rs = sql('SELECT `id`, `de` FROM cache_status');
    for ($i = 0; $i < mysql_num_rows($rs); $i++) {
        $r = sql_fetch_array($rs);
        $cachestatus[$r['id']]['de'] = $r['de'];
    }
    mysql_free_result($rs);
    $counties = array();
    $rs = sql('SELECT `short`, `de` FROM countries');
    for ($i = 0; $i < mysql_num_rows($rs); $i++) {
        $r = sql_fetch_array($rs);
        $counties[$r['short']]['de'] = $r['de'];
    }
    mysql_free_result($rs);
    $cachesizes = array();
    $rs = sql('SELECT `id`, `de` FROM cache_size');
    for ($i = 0; $i < mysql_num_rows($rs); $i++) {
        $r = sql_fetch_array($rs);
        $cachesizes[$r['id']]['de'] = $r['de'];
    }
    mysql_free_result($rs);
    $languages = array();
    $rs = sql('SELECT `short`, `de` FROM languages');
    for ($i = 0; $i < mysql_num_rows($rs); $i++) {
        $r = sql_fetch_array($rs);
        $languages[$r['short']]['de'] = $r['de'];
    }
    mysql_free_result($rs);
    $objecttypes['4'] = 'user';
    $objecttypes['2'] = 'cache';
    $objecttypes['3'] = 'cachedesc';
    $objecttypes['1'] = 'cachelog';
    $objecttypes['6'] = 'picture';
    $objecttypes['8'] = 'cachelist';
    // not implemented yet
    /* end now a few dynamically loaded constants */
    // temporäre Datei erstellen
    if (!is_dir($zip_basedir . 'ocxml11/' . $sessionid)) {
        mkdir($zip_basedir . 'ocxml11/' . $sessionid);
    }
    $fileid = 1;
    while (file_exists($zip_basedir . 'ocxml11/' . $sessionid . '/' . $sessionid . '-' . $filenr . '-' . $fileid . '.xml')) {
        $fileid++;
    }
    $xmlfilename = $zip_basedir . 'ocxml11/' . $sessionid . '/' . $sessionid . '-' . $filenr . '-' . $fileid . '.xml';
    $f = fopen($xmlfilename, 'w');
    if ($bXmlDecl == '1') {
        if ($sCharset == 'iso-8859-1') {
            fwrite($f, '<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>' . "\n");
        } else {
            if ($sCharset == 'utf-8') {
                fwrite($f, '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>' . "\n");
            }
        }
    }
    if ($bDocType == '1') {
        fwrite($f, '<!DOCTYPE oc11xml PUBLIC "-//Opencaching Network//DTD OCXml V 1.' . $ocxmlversion % 10 . '//EN" "http://www.opencaching.de/xml/ocxml' . $ocxmlversion . '.dtd">' . "\n");
    }
    if ($bOcXmlTag == '1') {
        $rs = sql('SELECT `date_created`, `modified_since` FROM `xmlsession` WHERE `id`=&1', $sessionid);
        $r = sql_fetch_array($rs);
        fwrite($f, '<oc11xml version="1.' . $ocxmlversion % 10 . '" date="' . date($sDateformat, strtotime($r['date_created'])) . '" since="' . date($sDateformat, strtotime($r['modified_since'])) . '">' . "\n");
        mysql_free_result($rs);
    }
    if ($bAttrlist == '1') {
        $rs = sql("SELECT SQL_BUFFER_RESULT `id`, `name`, `icon_large`, `icon_no`, `icon_undef` FROM `cache_attrib`");
        fwrite($f, $t1 . '<attrlist>' . "\n");
        while ($r = sql_fetch_assoc($rs)) {
            fwrite($f, $t2 . '<attr id="' . $r['id'] . '" icon_large="' . xmlentities($absolute_server_URI . $r['icon_large']) . '" icon_no="' . xmlentities($absolute_server_URI . $r['icon_no']) . '" icon_undef="' . xmlentities($absolute_server_URI . $r['icon_undef']) . '">' . xmlcdata($r['name']) . '</attr>' . "\n");
        }
        fwrite($f, $t1 . '</attrlist>' . "\n");
        sql_free_result($rs);
    }
    $rs = sql('SELECT SQL_BUFFER_RESULT `user`.`user_id` `id`, `user`.`node` `node`, `user`.`uuid` `uuid`, `user`.`username` `username`, `user`.`pmr_flag` `pmr_flag`, `user`.`date_created` `date_created`, `user`.`last_modified` `last_modified` FROM `tmpxml_users`, `user` WHERE `tmpxml_users`.`id`=`user`.`user_id`');
    while ($r = sql_fetch_array($rs)) {
        fwrite($f, $t1 . '<user>' . "\n");
        fwrite($f, $t2 . '<id id="' . $r['id'] . '" node="' . $r['node'] . '">' . $r['uuid'] . '</id>' . "\n");
        fwrite($f, $t2 . '<username>' . xmlcdata($r['username']) . '</username>' . "\n");
        fwrite($f, $t2 . '<pmr>' . ($r['pmr_flag'] == 0 ? '0' : '1') . '</pmr>' . "\n");
        fwrite($f, $t2 . '<datecreated>' . date($sDateformat, strtotime($r['date_created'])) . '</datecreated>' . "\n");
        fwrite($f, $t2 . '<lastmodified>' . date($sDateformat, strtotime($r['last_modified'])) . '</lastmodified>' . "\n");
        fwrite($f, $t1 . '</user>' . "\n");
    }
    mysql_free_result($rs);
    $rs = sql('SELECT SQL_BUFFER_RESULT `caches`.`cache_id` `id`, `caches`.`uuid` `uuid`, `caches`.`user_id` `user_id`, 
	                                    `user`.`uuid` `useruuid`, `user`.`username` `username`, `caches`.`name` `name`, 
	                                    `caches`.`longitude` `longitude`, `caches`.`latitude` `latitude`, `caches`.`type` `type`, 
	                                    `caches`.`country` `country`, `caches`.`size` `size`, `caches`.`desc_languages` `desclanguages`,
	                                    `caches`.`difficulty` `difficulty`, `caches`.`terrain` `terrain`, `caches`.`way_length` `way_length`, 
	                                    `caches`.`search_time` `search_time`, `caches`.`wp_gc` `wp_gc`, `caches`.`wp_nc` `wp_nc`,
	                                    /* we deliberatly do not use gc_wp_maintained here */
	                                    `caches`.`wp_oc` `wp_oc`, `caches`.`date_hidden` `date_hidden`, `caches`.`date_created` `date_created`, `caches`.`is_publishdate` `is_publishdate`, 
	                                    `caches`.`last_modified` `last_modified`, `caches`.`status` `status`, `caches`.`node` `node`,
	                                    `caches`.`listing_last_modified` `listing_last_modified`, `cache_status`.`allow_user_view`
	                               FROM `tmpxml_caches`
	                         INNER JOIN `caches` ON `tmpxml_caches`.`id`=`caches`.`cache_id`
	                         INNER JOIN `user` ON `caches`.`user_id`=`user`.`user_id`
	                         INNER JOIN `cache_status` ON `caches`.`status`=`cache_status`.`id`');
    while ($r = sql_fetch_array($rs)) {
        $bAllowView = $r['allow_user_view'] == 1;
        if ($r['size'] == 8 && $ocxmlversion < 12) {
            $r['size'] = 2;
        }
        // return as micro in old interface version
        fwrite($f, $t1 . '<cache>' . "\n");
        fwrite($f, $t2 . '<id id="' . $r['id'] . '" node="' . $r['node'] . '">' . $r['uuid'] . '</id>' . "\n");
        fwrite($f, $t2 . '<userid id="' . $r['user_id'] . '" uuid="' . $r['useruuid'] . '">' . xmlcdata($r['username']) . '</userid>' . "\n");
        fwrite($f, $t2 . '<name>' . xmlcdata($bAllowView ? $r['name'] : '') . '</name>' . "\n");
        fwrite($f, $t2 . '<longitude>' . sprintf('%01.5f', $bAllowView ? $r['longitude'] : 0) . '</longitude>' . "\n");
        fwrite($f, $t2 . '<latitude>' . sprintf('%01.5f', $bAllowView ? $r['latitude'] : 0) . '</latitude>' . "\n");
        fwrite($f, $t2 . '<type id="' . $r['type'] . '" short="' . xmlentities($cachetypes[$r['type']]['short']) . '">' . xmlcdata($cachetypes[$r['type']]['de']) . '</type>' . "\n");
        fwrite($f, $t2 . '<status id="' . $r['status'] . '">' . xmlcdata($cachestatus[$r['status']]['de']) . '</status>' . "\n");
        fwrite($f, $t2 . '<country id="' . $r['country'] . '">' . xmlcdata($counties[$r['country']]['de']) . '</country>' . "\n");
        fwrite($f, $t2 . '<size id="' . $r['size'] . '">' . xmlcdata($cachesizes[$r['size']]['de']) . '</size>' . "\n");
        fwrite($f, $t2 . '<desclanguages>' . $r['desclanguages'] . '</desclanguages>' . "\n");
        fwrite($f, $t2 . '<difficulty>' . sprintf('%01.1f', $r['difficulty'] / 2) . '</difficulty>' . "\n");
        fwrite($f, $t2 . '<terrain>' . sprintf('%01.1f', $r['terrain'] / 2) . '</terrain>' . "\n");
        fwrite($f, $t2 . '<rating waylength="' . $r['way_length'] . '" needtime="' . $r['search_time'] . '" />' . "\n");
        fwrite($f, $t2 . '<waypoints gccom="' . xmlentities($r['wp_gc']) . '" nccom="' . xmlentities($r['wp_nc']) . '" oc="' . xmlentities($r['wp_oc']) . '" />' . "\n");
        fwrite($f, $t2 . '<datehidden>' . date($sDateformat, strtotime($r['date_hidden'])) . '</datehidden>' . "\n");
        if ($ocxmlversion >= 12) {
            $pd = ' ispublishdate="' . $r['is_publishdate'] . '"';
        } else {
            $pd = "";
        }
        fwrite($f, $t2 . '<datecreated' . $pd . '>' . date($sDateformat, strtotime($r['date_created'])) . '</datecreated>' . "\n");
        fwrite($f, $t2 . '<lastmodified>' . date($sDateformat, strtotime($r['last_modified'])) . '</lastmodified>' . "\n");
        if ($ocxmlversion >= 14) {
            fwrite($f, $t2 . '<listing_lastmodified>' . date($sDateformat, strtotime($r['listing_last_modified'])) . '</listing_lastmodified>' . "\n");
        }
        $rsAttributes = sql("SELECT `cache_attrib`.`id`, `cache_attrib`.`name`\n\t\t                       FROM `caches_attributes`\n\t\t                 INNER JOIN `cache_attrib` ON `caches_attributes`.`attrib_id`=`cache_attrib`.`id`\n\t\t                      WHERE `caches_attributes`.`cache_id`='&1'", $r['id']);
        fwrite($f, $t2 . '<attributes>' . "\n");
        while ($rAttribute = sql_fetch_assoc($rsAttributes)) {
            fwrite($f, $t3 . '<attribute id="' . ($rAttribute['id'] + 0) . '">' . xmlcdata($rAttribute['name']) . '</attribute>' . "\n");
        }
        fwrite($f, $t2 . '</attributes>' . "\n");
        sql_free_result($rsAttributes);
        if ($ocxmlversion >= 13) {
            $rsWaypoints = sql("SELECT `coordinates`.`id`, `coordinates`.`subtype` AS `type`,\n\t\t\t                           `coordinates`.`latitude`, `coordinates`.`longitude`,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t `coordinates`.`description`,\n\t\t\t                           `coordinates_type`.`name` AS `type_name`\n\t\t\t                      FROM `coordinates`\n\t\t\t                INNER JOIN `coordinates_type` ON `coordinates_type`.`id`=`coordinates`.`subtype`\n\t\t\t                     WHERE `cache_id`='&1' AND `type`=1\n\t\t\t                  ORDER BY `coordinates`.`id` ASC", $r['id']);
            fwrite($f, $t2 . '<wpts>' . "\n");
            while ($rWaypoint = sql_fetch_assoc($rsWaypoints)) {
                fwrite($f, $t3 . '<wpt id="' . ($rWaypoint['id'] + 0) . '" type="' . ($rWaypoint['type'] + 0) . '" typename="' . xmlentities($rWaypoint['type_name']) . '" longitude="' . sprintf('%01.5f', $rWaypoint['longitude']) . '" latitude="' . sprintf('%01.5f', $rWaypoint['latitude']) . '">' . xmlcdata($rWaypoint['description']) . '</wpt>' . "\n");
            }
            fwrite($f, $t2 . '</wpts>' . "\n");
            sql_free_result($rsAttributes);
        }
        fwrite($f, $t1 . '</cache>' . "\n");
    }
    mysql_free_result($rs);
    $rs = sql('SELECT SQL_BUFFER_RESULT `cache_desc`.`id` `id`, `cache_desc`.`uuid` `uuid`, `cache_desc`.`cache_id` `cache_id`, 
	                                    `cache_desc`.`language` `language`, `cache_desc`.`short_desc` `short_desc`,
	                                    `cache_desc`.`desc` `desc`, `cache_desc`.`desc_html` `desc_html`, `cache_desc`.`hint` `hint`, 
	                                    `cache_desc`.`last_modified` `last_modified`, `caches`.`uuid` `cacheuuid`, `cache_desc`.`node` `node`,
	                                    `cache_status`.`allow_user_view`,
	                                    `caches`.`user_id`, `user`.`username`, `user`.`data_license`
	                               FROM `tmpxml_cachedescs`
	                         INNER JOIN `cache_desc` ON `tmpxml_cachedescs`.`id`=`cache_desc`.`id`
	                         INNER JOIN `caches` ON `caches`.`cache_id`=`cache_desc`.`cache_id`
	                         INNER JOIN `cache_status` ON `caches`.`status`=`cache_status`.`id`
													 INNER JOIN `user` ON `user`.`user_id`=`caches`.`user_id`');
    while ($r = sql_fetch_array($rs)) {
        $bAllowView = $r['allow_user_view'] == 1;
        fwrite($f, $t1 . '<cachedesc>' . "\n");
        fwrite($f, $t2 . '<id id="' . $r['id'] . '" node="' . $r['node'] . '">' . $r['uuid'] . '</id>' . "\n");
        fwrite($f, $t2 . '<cacheid id="' . $r['cache_id'] . '">' . $r['cacheuuid'] . '</cacheid>' . "\n");
        fwrite($f, $t2 . '<language id="' . $r['language'] . '">' . xmlcdata($languages[$r['language']]['de']) . '</language>' . "\n");
        fwrite($f, $t2 . '<shortdesc>' . xmlcdata($bAllowView ? $r['short_desc'] : '') . '</shortdesc>' . "\n");
        $desc = $r['desc'];
        if ($r['desc_html'] == 0) {
            $desc = mb_ereg_replace('<br />', '', $desc);
            $desc = html_entity_decode($desc, ENT_COMPAT, 'UTF-8');
        }
        $lang = $sLanguage != "" ? $sLanguage : $r['language'];
        $disclaimer = getLicenseDisclaimer($r['user_id'], $r['username'], $r['data_license'], $r['cache_id'], $lang, true, true);
        if ($bLicense) {
            fwrite($f, $t2 . '<license>' . xmlcdata($disclaimer) . '</license>' . "\n");
        } else {
            if ($disclaimer != "") {
                $desc .= "<p><em>" . $disclaimer . "</em></p>";
            }
        }
        $desc .= get_desc_npas($r['cache_id']);
        fwrite($f, $t2 . '<desc html="' . ($r['desc_html'] == 1 ? '1' : '0') . '">' . xmlcdata($bAllowView ? $desc : '') . '</desc>' . "\n");
        $r['hint'] = mb_ereg_replace('<br />', '', $r['hint']);
        $r['hint'] = html_entity_decode($r['hint'], ENT_COMPAT, 'UTF-8');
        fwrite($f, $t2 . '<hint>' . xmlcdata($bAllowView ? $r['hint'] : '') . '</hint>' . "\n");
        fwrite($f, $t2 . '<lastmodified>' . date($sDateformat, strtotime($r['last_modified'])) . '</lastmodified>' . "\n");
        fwrite($f, $t1 . '</cachedesc>' . "\n");
    }
    mysql_free_result($rs);
    if ($ocxmlversion >= 14) {
        $rating_condition = "AND `cache_logs`.`date`=`cache_rating`.`rating_date`";
    } else {
        $rating_condition = "";
    }
    $rs = sql('SELECT SQL_BUFFER_RESULT `cache_logs`.`id` `id`, `cache_logs`.`cache_id` `cache_id`, `cache_logs`.`user_id` `user_id`, 
	                                    `cache_logs`.`type` `type`, `cache_logs`.`date` `date`, `cache_logs`.`text` `text`, `cache_logs`.`text_html` `text_html`,
	                                    `cache_logs`.`oc_team_comment`,
	                                    `cache_logs`.`date_created` `date_created`, `cache_logs`.`last_modified` `last_modified`,
	                                    `cache_logs`.`log_last_modified` `log_last_modified`, 
	                                    `cache_logs`.`uuid` `uuid`, `user`.`username` `username`, `caches`.`uuid` `cacheuuid`, 
	                                    `user`.`uuid` `useruuid`, `cache_logs`.`node` `node`, IF(NOT ISNULL(`cache_rating`.`cache_id`) AND `cache_logs`.`type` IN (1,7), 1, 0) AS `recommended`,
	                                    `cache_status`.`allow_user_view`,
	                                    `user`.`data_license`,
	                                    `caches`.`country` AS `language`  /* hack */
	                               FROM `cache_logs` 
	                         INNER JOIN `tmpxml_cachelogs` ON `cache_logs`.`id`=`tmpxml_cachelogs`.`id`
	                         INNER JOIN `user` ON `cache_logs`.`user_id`=`user`.`user_id`
	                         INNER JOIN `caches` ON `caches`.`cache_id`=`cache_logs`.`cache_id`
	                         INNER JOIN `cache_status` ON `caches`.`status`=`cache_status`.`id`
	                          LEFT JOIN `cache_rating` ON `cache_logs`.`cache_id`=`cache_rating`.`cache_id` AND `cache_logs`.`user_id`=`cache_rating`.`user_id` ' . $rating_condition);
    while ($r = sql_fetch_array($rs)) {
        $bAllowView = $r['allow_user_view'] == 1;
        $r['text'] = mb_ereg_replace('<br />', '', $r['text']);
        $r['text'] = html_entity_decode($r['text'], ENT_COMPAT, 'UTF-8');
        // locked/invisible should never be returned here - these logs are deleted before
        // reactivating the cache. Just for the case ... it is safe to return them as 'locked'.
        if ($r['type'] == 14) {
            $r['type'] = 13;
        }
        if ($ocxmlversion >= 13) {
            $teamcomment = ' teamcomment="' . $r['oc_team_comment'] . '"';
        } else {
            $teamcomment = '';
            if ($r['type'] > 8) {
                $r['type'] = 3;
            }
        }
        fwrite($f, $t1 . '<cachelog>' . "\n");
        fwrite($f, $t2 . '<id id="' . $r['id'] . '" node="' . $r['node'] . '">' . $r['uuid'] . '</id>' . "\n");
        fwrite($f, $t2 . '<cacheid id="' . $r['cache_id'] . '">' . $r['cacheuuid'] . '</cacheid>' . "\n");
        fwrite($f, $t2 . '<userid id="' . $r['user_id'] . '" uuid="' . $r['useruuid'] . '">' . xmlcdata($r['username']) . '</userid>' . "\n");
        fwrite($f, $t2 . '<logtype id="' . $r['type'] . '" recommended="' . $r['recommended'] . '"' . $teamcomment . '>' . xmlcdata($logtypes[$r['type']]) . '</logtype>' . "\n");
        fwrite($f, $t2 . '<date>' . date($ocxmlversion >= 13 ? $sDateformat : $sDateshort, strtotime($r['date'])) . '</date>' . "\n");
        fwrite($f, $t2 . '<text html="' . $r['text_html'] . '">' . xmlcdata($bAllowView ? $r['text'] : '') . '</text>' . "\n");
        fwrite($f, $t2 . '<datecreated>' . date($sDateformat, strtotime($r['date_created'])) . '</datecreated>' . "\n");
        fwrite($f, $t2 . '<lastmodified>' . date($sDateformat, strtotime($r['last_modified'])) . '</lastmodified>' . "\n");
        if ($ocxmlversion >= 14) {
            fwrite($f, $t2 . '<log_lastmodified>' . date($sDateformat, strtotime($r['log_last_modified'])) . '</log_lastmodified>' . "\n");
        }
        if ($bLicense) {
            $lang = $sLanguage != "" ? $sLanguage : $r['language'];
            $disclaimer = getLicenseDisclaimer($r['user_id'], $r['username'], $r['data_license'], $r['cache_id'], $lang, false, true);
            fwrite($f, $t2 . '<license>' . xmlcdata($disclaimer) . '</license>' . "\n");
        }
        fwrite($f, $t1 . '</cachelog>' . "\n");
    }
    mysql_free_result($rs);
    $rs = sql('SELECT SQL_BUFFER_RESULT `pictures`.`id` `id`, `pictures`.`url` `url`, `pictures`.`title` `title`, 
	                                    `pictures`.`object_id` `object_id`, `pictures`.`object_type` `object_type`, 
	                                    `pictures`.`date_created` `date_created`, `pictures`.`uuid` `uuid`, 
	                                    `pictures`.`last_modified` `last_modified`, `pictures`.`display` `display`, 
	                                    `pictures`.`spoiler` `spoiler`, `pictures`.`node` `node`,
	                                    `pictures`.`mappreview`,
	                                    IFNULL(`c1`.`cache_id`,`c2`.`cache_id`) AS `cache_id`,
	                                    IFNULL(`c1`.`country`,`c2`.`country`) AS `language`,  /* hack */
	                                    IFNULL(`cs1`.`allow_user_view`, `cs2`.`allow_user_view`) AS `auv`,
	                                    IFNULL(`u1`.`user_id`,`u2`.`user_id`) AS `user_id`,
	                                    IFNULL(`u1`.`username`,`u2`.`username`) AS `username`,
	                                    IFNULL(`u1`.`data_license`,`u2`.`data_license`) AS `data_license`
	                               FROM `tmpxml_pictures` 
	                         INNER JOIN `pictures` ON `tmpxml_pictures`.`id`=`pictures`.`id` 
	                          LEFT JOIN `caches` AS `c1` ON `pictures`.`object_type`=2 AND `pictures`.`object_id`=`c1`.`cache_id` 
	                          LEFT JOIN `cache_logs` ON `pictures`.`object_type`=1 AND `pictures`.`object_id`=`cache_logs`.`id` 
	                          LEFT JOIN `caches` AS `c2` ON `cache_logs`.`cache_id`=`c2`.`cache_id` 
	                          LEFT JOIN `cache_status` AS `cs1` ON `c1`.`status`=`cs1`.`id` 
	                          LEFT JOIN `cache_status` AS `cs2` ON `c2`.`status`=`cs2`.`id`
	                          LEFT JOIN `user` `u1` ON `u1`.`user_id`=`cache_logs`.`user_id`
	                          LEFT JOIN `user` `u2` ON `u2`.`user_id`=`c1`.`user_id`');
    while ($r = sql_fetch_array($rs)) {
        $bAllowView = $r['auv'] == 1;
        fwrite($f, $t1 . '<picture>' . "\n");
        fwrite($f, $t2 . '<id id="' . $r['id'] . '" node="' . $r['node'] . '">' . $r['uuid'] . '</id>' . "\n");
        fwrite($f, $t2 . '<url>' . xmlcdata($bAllowView ? $r['url'] : '') . '</url>' . "\n");
        fwrite($f, $t2 . '<title>' . xmlcdata($bAllowView ? $r['title'] : '') . '</title>' . "\n");
        fwrite($f, $t2 . '<object id="' . $r['object_id'] . '" type="' . $r['object_type'] . '" typename="' . xmlentities($objecttypes[$r['object_type']]) . '">' . object_id2uuid($r['object_id'], $r['object_type']) . '</object>' . "\n");
        if ($ocxmlversion >= 13) {
            fwrite($f, $t2 . '<picattr spoiler="' . $r['spoiler'] . '" display="' . $r['display'] . '" preview="' . $r['mappreview'] . '" />' . "\n");
        } else {
            fwrite($f, $t2 . '<attributes spoiler="' . $r['spoiler'] . '" display="' . $r['display'] . '" />' . "\n");
        }
        fwrite($f, $t2 . '<datecreated>' . date($sDateformat, strtotime($r['date_created'])) . '</datecreated>' . "\n");
        fwrite($f, $t2 . '<lastmodified>' . date($sDateformat, strtotime($r['last_modified'])) . '</lastmodified>' . "\n");
        if ($bLicense) {
            $lang = $sLanguage != "" ? $sLanguage : $r['language'];
            $disclaimer = getLicenseDisclaimer($r['user_id'], $r['username'], $r['data_license'], $r['cache_id'], $lang, false, true);
            fwrite($f, $t2 . '<license>' . xmlcdata($disclaimer) . '</license>' . "\n");
        }
        fwrite($f, $t1 . '</picture>' . "\n");
    }
    mysql_free_result($rs);
    $rs = sql('SELECT SQL_BUFFER_RESULT `removed_objects`.`id` `id`, `removed_objects`.`localid` `localid`, `removed_objects`.`uuid` `uuid`, 
	                                    `removed_objects`.`type` `type`, `removed_objects`.`removed_date` `removed_date`, `removed_objects`.`node` `node` 
	                               FROM `tmpxml_removedobjects`, `removed_objects` WHERE `removed_objects`.`id`=`tmpxml_removedobjects`.`id`');
    while ($r = sql_fetch_array($rs)) {
        fwrite($f, $t1 . '<removedobject>' . "\n");
        fwrite($f, $t2 . '<id id="' . $r['id'] . '" node="' . $r['node'] . '" />' . "\n");
        fwrite($f, $t2 . '<object id="' . $r['localid'] . '" type="' . $r['type'] . '" typename="' . xmlentities($objecttypes[$r['type']]) . '">' . $r['uuid'] . '</object>' . "\n");
        fwrite($f, $t2 . '<removeddate>' . date($sDateformat, strtotime($r['removed_date'])) . '</removeddate>' . "\n");
        fwrite($f, $t1 . '</removedobject>' . "\n");
    }
    mysql_free_result($rs);
    if ($bOcXmlTag == '1') {
        fwrite($f, '</oc11xml>' . "\n");
    }
    fclose($f);
    $rel_xmlfile = 'ocxml11/' . $sessionid . '/' . $sessionid . '-' . $filenr . '-' . $fileid . '.xml';
    $rel_zipfile = 'ocxml11/' . $sessionid . '/' . $sessionid . '-' . $filenr . '-' . $fileid;
    // zippen und url-redirect
    if ($ziptype == '0') {
        tpl_redirect($zip_wwwdir . $rel_xmlfile);
        exit;
    } else {
        if ($ziptype == 'zip') {
            $rel_zipfile .= '.zip';
        } else {
            if ($ziptype == 'bzip2') {
                $rel_zipfile .= '.bz2';
            } else {
                if ($ziptype == 'gzip') {
                    $rel_zipfile .= '.gz';
                } else {
                    die('unknown zip type');
                }
            }
        }
    }
    $call = $safemode_zip . ' --type=' . escapeshellcmd($ziptype) . ' --src=' . escapeshellcmd($rel_xmlfile) . ' --dst=' . escapeshellcmd($rel_zipfile);
    system($call);
    // datei vorhanden?
    if (!file_exists($zip_basedir . $rel_zipfile)) {
        die('all ok, but zip failed - internal server error');
    }
    tpl_redirect($zip_wwwdir . $rel_zipfile);
    exit;
}
Esempio n. 2
0
function search_output()
{
    global $opt, $login;
    global $cache_note_text;
    $server_address = $opt['page']['absolute_url'];
    $server_domain = parse_url($server_address, PHP_URL_HOST);
    $gpxHead = '<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0" creator="Opencaching.de - http://www.opencaching.de" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
  <name>Cache listing generated from Opencaching.de</name>
  <desc>This is a waypoint file generated from Opencaching.de{wpchildren}</desc>
  <author>Opencaching.de</author>
  <email>contact@opencaching.de</email>
  <url>http://' . $server_domain . '</url>
  <urlname>Opencaching.de - Geocaching in Deutschland, Oesterreich und der Schweiz</urlname>
  <time>{time}</time>
  <keywords>cache, geocache, opencaching, waypoint</keywords>
';
    $gpxLine = '  <wpt lat="{lat}" lon="{lon}">
    <time>{time}</time>
    <name>{waypoint}</name>
    <desc>{cachename}</desc>
    <src>' . $server_domain . '</src>
    <url>' . $server_address . 'viewcache.php?cacheid={cacheid}</url>
    <urlname>{cachename}</urlname>
    <sym>{sym}</sym>
    <type>Geocache|{type}</type>
    <groundspeak:cache id="{cacheid}" {status} xmlns:groundspeak="http://www.groundspeak.com/cache/1/0/1">
      <groundspeak:name>{cachename}</groundspeak:name>
      <groundspeak:placed_by>{owner}</groundspeak:placed_by>
      <groundspeak:owner id="{userid}">{owner}</groundspeak:owner>
      <groundspeak:type>{type}</groundspeak:type>
      <groundspeak:container>{container}</groundspeak:container>
      <groundspeak:attributes>
{attributes}      </groundspeak:attributes>
      <groundspeak:difficulty>{difficulty}</groundspeak:difficulty>
      <groundspeak:terrain>{terrain}</groundspeak:terrain>
      <groundspeak:country>{country}</groundspeak:country>
      <groundspeak:state>{state}</groundspeak:state>
      <groundspeak:short_description html="True">{shortdesc}</groundspeak:short_description>
      <groundspeak:long_description html="True">{desc}&lt;br /&gt;{images}</groundspeak:long_description>
{hints}      <groundspeak:logs>
{logs}      </groundspeak:logs>
      <groundspeak:travelbugs>
{geokrety}      </groundspeak:travelbugs>
    </groundspeak:cache>
  </wpt>
{cache_waypoints}';
    /* Ocprop:
     *    <wpt\s+lat=\"([0-9\-\+\.]+)\"\s+lon=\"([0-9\-\+\.]+)\">
     *    <time>(.*?)<\/time>
     *      (Date: ^([0-9]{4})\-([0-9]{2})\-([0-9]{2})T[0-9\:\-\.]+(Z)?$/s)
     *    <name>(.*?)<\/name>
     *    <url>http:\/\/www\.opencaching\.de\/viewcache\.php\?cacheid=([0-9]+)<\/url>
     *    <sym>(.*?)<\/sym>
     *    <groundspeak:cache\s+id=\"[0-9]+\"\s+available=\"(True|False)\"\s+archived=\"(True|False)\"
     *    <groundspeak:name>(.*?)<\/groundspeak:name>
     *    <groundspeak:placed_by>(.*?)<\/groundspeak:placed_by>
     *    <groundspeak:owner id="([0-9])+">(.*?)<\/groundspeak:owner>
     *    <groundspeak:type>(.*?)<\/groundspeak:type>
     *    <groundspeak:container>(.*?)<\/groundspeak:container>
     *    <groundspeak:difficulty>(.*?)<\/groundspeak:difficulty>
     *    <groundspeak:terrain>(.*?)<\/groundspeak:terrain>
     *    <groundspeak:country>(.*?)<\/groundspeak:country>
     *    <groundspeak:state>(.*?)<\/groundspeak:state>
     *    <groundspeak:short_description html="(.*?)".*?>(.*?)<\/groundspeak:short_description>
     *    <groundspeak:long_description html="(.*?)".*?>(.*?)<\/groundspeak:long_description>
     *    <groundspeak:encoded_hints>(.*?)<\/groundspeak:encoded_hints>
     */
    $gpxAttributes = ' 	      <groundspeak:attribute id="{attrib_id}" inc="{attrib_inc}">{attrib_name}</groundspeak:attribute>';
    $gpxLog = '      <groundspeak:log id="{id}">
        <groundspeak:date>{date}</groundspeak:date>
        <groundspeak:type>{type}</groundspeak:type>
        <groundspeak:finder id="{userid}">{username}</groundspeak:finder>
        <groundspeak:text encoded="False">{text}</groundspeak:text>
      </groundspeak:log>';
    $gpxGeokrety = '		<groundspeak:travelbug id="{gkid}" ref="{gkref}">
		  <groundspeak:name>{gkname}</groundspeak:name>
		</groundspeak:travelbug>';
    $gpxWaypoints = '  <wpt lat="{wp_lat}" lon="{wp_lon}">
    <time>{time}</time>
    <name>{name}</name>
    <cmt>{comment}</cmt>
    <desc>{desc}</desc>
    <url>' . $server_address . 'viewcache.php?cacheid={cacheid}</url>
    <urlname>{parent} {cachename}</urlname>
    <sym>{type}</sym>
    <type>Waypoint|{type}</type>
    <gsak:wptExtension xmlns:gsak="http://www.gsak.net/xmlv1/4">
      <gsak:Parent>{parent}</gsak:Parent>
    </gsak:wptExtension>
  </wpt>
';
    $gpxFoot = '</gpx>';
    $gpxTimeFormat = 'Y-m-d\\TH:i:s\\Z';
    $gpxStatus[0] = 'available="False" archived="False"';
    // other (unavailable, not archived)
    $gpxStatus[1] = 'available="True" archived="False"';
    //available, not archived
    $gpxStatus[2] = 'available="False" archived="False"';
    //unavailable, not archived
    $gpxStatus[3] = 'available="False" archived="True"';
    //unavailable, archived
    $gpxStatus[6] = 'available="False" archived="True"';
    //locked, visible
    $gpxContainer[0] = 'Other';
    $gpxContainer[2] = 'Micro';
    $gpxContainer[3] = 'Small';
    $gpxContainer[4] = 'Regular';
    $gpxContainer[5] = 'Large';
    $gpxContainer[6] = 'Large';
    $gpxContainer[7] = 'Virtual';
    $gpxContainer[8] = 'Micro';
    // cache types known by gpx
    $gpxType[0] = 'Unknown Cache';
    $gpxType[2] = 'Traditional Cache';
    $gpxType[3] = 'Multi-cache';
    $gpxType[4] = 'Virtual Cache';
    $gpxType[5] = 'Webcam Cache';
    $gpxType[6] = 'Event Cache';
    // unknown ... converted
    $gpxType[7] = 'Unknown Cache';
    $gpxType[8] = 'Unknown Cache';
    $gpxType[10] = 'Traditional Cache';
    $gpxLogType[0] = 'Other';
    $gpxLogType[1] = 'Found it';
    $gpxLogType[2] = 'Didn\'t find it';
    $gpxLogType[3] = 'Write note';
    $gpxLogType[7] = 'Attended';
    $gpxLogType[8] = 'Will attend';
    $gpxLogType[9] = 'Archive';
    $gpxLogType[10] = 'Owner Maintenance';
    $gpxLogType[11] = 'Temporarily Disable Listing';
    $gpxLogType[13] = 'Archive';
    $gpxLogType[14] = 'Archive';
    $gpxSymNormal = 'Geocache';
    $gpxSymFound = 'Geocache Found';
    $childwphandler = new ChildWp_Handler();
    $children = '';
    $rs = sql('SELECT &searchtmp.`cache_id` `cacheid` FROM &searchtmp');
    while ($r = sql_fetch_array($rs) && $children == '') {
        if (count($childwphandler->getChildWps($r['cacheid']))) {
            $children = ' (HasChildren)';
        }
    }
    mysql_free_result($rs);
    $gpxHead = mb_ereg_replace('{wpchildren}', $children, $gpxHead);
    $gpxHead = mb_ereg_replace('{time}', date($gpxTimeFormat, time()), $gpxHead);
    append_output($gpxHead);
    $user_id = $login->userid;
    $rs = sql_slave("SELECT SQL_BUFFER_RESULT &searchtmp.`cache_id` `cacheid`, &searchtmp.`longitude` `longitude`, &searchtmp.`latitude` `latitude`,\n\t\t\t\t\t\t\t`cache_location`.`adm2` `state`, `caches`.`wp_oc` `waypoint`, `caches`.`date_hidden` `date_hidden`, `caches`.`name` `name`,\n\t\t\t\t\t\t\t`caches`.`country` `country`, `countries`.`name` AS `country_name`, `caches`.`terrain` `terrain`, `caches`.`difficulty` `difficulty`, `caches`.`desc_languages` `desc_languages`,\n\t\t\t\t\t\t\t`caches`.`size` `size`, `caches`.`type` `type`, `caches`.`status` `status`, `user`.`username` `username`, `caches`.`user_id` `userid`, `user`.`data_license`,\n\t\t\t\t\t\t\t`cache_desc`.`desc` `desc`, `cache_desc`.`short_desc` `short_desc`, `cache_desc`.`hint` `hint`,\n\t\t\t\t\t\t\tIFNULL(`stat_cache_logs`.`found`, 0) AS `found`\n\t\t\t\t\t\tFROM &searchtmp\n\t\t\t\t\t\t\tINNER JOIN `caches` ON &searchtmp.`cache_id`=`caches`.`cache_id`\n\t\t\t\t\t\t\tINNER JOIN `countries` ON `caches`.`country`=`countries`.`short`\n\t\t\t\t\t\t\tINNER JOIN `user` ON &searchtmp.`user_id`=`user`.`user_id`\n\t\t\t\t\t\t\tINNER JOIN `cache_desc` ON `caches`.`cache_id`=`cache_desc`.`cache_id`AND `caches`.`default_desclang`=`cache_desc`.`language`\n\t\t\t\t\t\t\tLEFT JOIN `cache_location` ON &searchtmp.`cache_id`=`cache_location`.`cache_id`\n\t\t\t\t\t\t\tLEFT JOIN `stat_cache_logs` ON &searchtmp.`cache_id`=`stat_cache_logs`.`cache_id` AND `stat_cache_logs`.`user_id`='&1'", $user_id);
    while ($r = sql_fetch_array($rs)) {
        $thisline = $gpxLine;
        $lat = sprintf('%01.5f', $r['latitude']);
        $thisline = mb_ereg_replace('{lat}', $lat, $thisline);
        $lon = sprintf('%01.5f', $r['longitude']);
        $thisline = mb_ereg_replace('{lon}', $lon, $thisline);
        $time = date($gpxTimeFormat, strtotime($r['date_hidden']));
        $thisline = mb_ereg_replace('{time}', $time, $thisline);
        $thisline = mb_ereg_replace('{waypoint}', $r['waypoint'], $thisline);
        $thisline = mb_ereg_replace('{cacheid}', $r['cacheid'], $thisline);
        $thisline = mb_ereg_replace('{cachename}', text_xmlentities($r['name']), $thisline);
        $thisline = mb_ereg_replace('{country}', text_xmlentities($r['country_name']), $thisline);
        $thisline = mb_ereg_replace('{state}', text_xmlentities($r['state']), $thisline);
        if ($r['hint'] == '') {
            $thisline = mb_ereg_replace('{hints}', '', $thisline);
        } else {
            // Ocprop:  <groundspeak:encoded_hints>(.*?)<\/groundspeak:encoded_hints>
            $hint = html_entity_decode(strip_tags($r['hint']), ENT_COMPAT, "UTF-8");
        }
        $thisline = mb_ereg_replace('{hints}', '      <groundspeak:encoded_hints>' . text_xmlentities($hint) . '</groundspeak:encoded_hints>
', $thisline);
        $thisline = mb_ereg_replace('{shortdesc}', text_xmlentities($r['short_desc']), $thisline);
        $desc = str_replace(' src="images/uploads/', ' src="' . $server_address . 'images/uploads/', $r['desc']);
        $license = getLicenseDisclaimer($r['userid'], $r['username'], $r['data_license'], $r['cacheid'], $opt['template']['locale'], true, true);
        if ($license != "") {
            $desc .= "<p><em>{$license}</em></p>\n";
        }
        $desc .= get_desc_npas($r['cacheid']);
        $thisline = mb_ereg_replace('{desc}', text_xmlentities(decodeEntities($desc)), $thisline);
        $thisline = mb_ereg_replace('{images}', text_xmlentities(getPictures($r['cacheid'], $server_address)), $thisline);
        if (isset($gpxType[$r['type']])) {
            $thisline = mb_ereg_replace('{type}', $gpxType[$r['type']], $thisline);
        } else {
            $thisline = mb_ereg_replace('{type}', $gpxType[0], $thisline);
        }
        if (isset($gpxContainer[$r['size']])) {
            $thisline = mb_ereg_replace('{container}', $gpxContainer[$r['size']], $thisline);
        } else {
            $thisline = mb_ereg_replace('{container}', $gpxContainer[0], $thisline);
        }
        if (isset($gpxStatus[$r['status']])) {
            $thisline = mb_ereg_replace('{status}', $gpxStatus[$r['status']], $thisline);
        } else {
            $thisline = mb_ereg_replace('{status}', $gpxStatus[0], $thisline);
        }
        $sDiffDecimals = '';
        if ($r['difficulty'] % 2) {
            $sDiffDecimals = '.5';
        }
        $r['difficulty'] -= $r['difficulty'] % 2;
        $thisline = mb_ereg_replace('{difficulty}', $r['difficulty'] / 2 . $sDiffDecimals, $thisline);
        $sTerrDecimals = '';
        if ($r['terrain'] % 2) {
            $sTerrDecimals = '.5';
        }
        $r['terrain'] -= $r['terrain'] % 2;
        $thisline = mb_ereg_replace('{terrain}', $r['terrain'] / 2 . $sTerrDecimals, $thisline);
        $thisline = mb_ereg_replace('{owner}', text_xmlentities($r['username']), $thisline);
        $thisline = mb_ereg_replace('{userid}', $r['userid'], $thisline);
        if ($r['found'] > 0) {
            $thisline = mb_ereg_replace('{sym}', text_xmlentities($gpxSymFound), $thisline);
        } else {
            $thisline = mb_ereg_replace('{sym}', text_xmlentities($gpxSymNormal), $thisline);
        }
        // clear cache specific data
        $logentries = '';
        $cache_note = false;
        $attribentries = '';
        $waypoints = '';
        $gkentries = '';
        // fetch logs
        if ($user_id != 0) {
            // insert personal note
            $cacheNote = getCacheNote($user_id, $r['cacheid']);
            if ($cacheNote) {
                $thislog = $gpxLog;
                $thislog = mb_ereg_replace('{id}', 0, $thislog);
                $thislog = mb_ereg_replace('{date}', date($gpxTimeFormat), $thislog);
                $thislog = mb_ereg_replace('{userid}', $user_id, $thislog);
                $thislog = mb_ereg_replace('{username}', text_xmlentities($login->username), $thislog);
                $thislog = mb_ereg_replace('{type}', $gpxLogType[3], $thislog);
                $thislog = mb_ereg_replace('{text}', text_xmlentities($cacheNote['note']), $thislog);
                $logentries .= $thislog . "\n";
            }
            // current users logs
            $rsLogs = sql_slave("SELECT `cache_logs`.`id`, `cache_logs`.`type`, `cache_logs`.`date`, `cache_logs`.`text`, `user`.`username`, `user`.`user_id` FROM `cache_logs`, `user` WHERE `cache_logs`.`user_id`=`user`.`user_id` AND `cache_logs`.`cache_id`=&1 AND `user`.`user_id`=&2 ORDER BY `cache_logs`.`date` DESC, `cache_logs`.`date_created` DESC", $r['cacheid'], $user_id);
            while ($rLog = sql_fetch_array($rsLogs)) {
                $thislog = $gpxLog;
                $thislog = mb_ereg_replace('{id}', $rLog['id'], $thislog);
                $thislog = mb_ereg_replace('{date}', date($gpxTimeFormat, strtotime($rLog['date'])), $thislog);
                $thislog = mb_ereg_replace('{userid}', $rLog['user_id'], $thislog);
                $thislog = mb_ereg_replace('{username}', text_xmlentities($rLog['username']), $thislog);
                if (isset($gpxLogType[$rLog['type']])) {
                    $logtype = $gpxLogType[$rLog['type']];
                } else {
                    $logtype = $gpxLogType[0];
                }
                $thislog = mb_ereg_replace('{type}', $logtype, $thislog);
                $thislog = mb_ereg_replace('{text}', text_xmlentities(decodeEntities($rLog['text'])), $thislog);
                $logentries .= $thislog . "\n";
            }
            mysql_free_result($rsLogs);
        }
        // newest 20 logs (except current users)
        $rsLogs = sql_slave("SELECT `cache_logs`.`id`, `cache_logs`.`type`, `cache_logs`.`date`, `cache_logs`.`text`, `user`.`username`, `user`.`user_id` FROM `cache_logs`, `user` WHERE `cache_logs`.`user_id`=`user`.`user_id` AND `cache_logs`.`cache_id`=&1 AND `user`.`user_id`!=&2 ORDER BY `cache_logs`.`date` DESC, `cache_logs`.`date_created` DESC LIMIT 20", $r['cacheid'], $user_id);
        while ($rLog = sql_fetch_array($rsLogs)) {
            $thislog = $gpxLog;
            $thislog = mb_ereg_replace('{id}', $rLog['id'], $thislog);
            $thislog = mb_ereg_replace('{date}', date($gpxTimeFormat, strtotime($rLog['date'])), $thislog);
            $thislog = mb_ereg_replace('{userid}', $rLog['user_id'], $thislog);
            $thislog = mb_ereg_replace('{username}', text_xmlentities($rLog['username']), $thislog);
            if (isset($gpxLogType[$rLog['type']])) {
                $logtype = $gpxLogType[$rLog['type']];
            } else {
                $logtype = $gpxLogType[0];
            }
            $thislog = mb_ereg_replace('{type}', $logtype, $thislog);
            $thislog = mb_ereg_replace('{text}', text_xmlentities(decodeEntities($rLog['text'])), $thislog);
            $logentries .= $thislog . "\n";
        }
        mysql_free_result($rsLogs);
        $thisline = mb_ereg_replace('{logs}', $logentries, $thisline);
        // attributes
        $rsAttributes = sql_slave("SELECT `gc_id`, `gc_inc`, `gc_name`\n\t\t                             FROM `caches_attributes`\n\t\t                       INNER JOIN `cache_attrib` ON `cache_attrib`.`id`=`caches_attributes`.`attrib_id`\n\t\t                            WHERE `caches_attributes`.`cache_id`=&1", $r['cacheid']);
        $gc_ids = array();
        while ($rAttrib = sql_fetch_array($rsAttributes)) {
            // Multiple OC attributes can be mapped to one GC attribute, either with
            // the same "inc"s or with different. Both may disturb applications, so we
            // output each GC ID only once.
            if (!isset($gc_ids[$rAttrib['gc_id']])) {
                $thisattribute = mb_ereg_replace('{attrib_id}', $rAttrib['gc_id'], $gpxAttributes);
                $thisattribute = mb_ereg_replace('{attrib_inc}', $rAttrib['gc_inc'], $thisattribute);
                $thisattribute = mb_ereg_replace('{attrib_name}', text_xmlentities($rAttrib['gc_name']), $thisattribute);
                $attribentries .= $thisattribute . "\n";
                $gc_ids[$rAttrib['gc_id']] = true;
            }
        }
        mysql_free_result($rsAttributes);
        $thisline = mb_ereg_replace('{attributes}', $attribentries, $thisline);
        // geokrety
        $rsGeokrety = sql_slave("SELECT `gk_item`.`id`, `gk_item`.`name`,  `caches`.`wp_oc` FROM `gk_item` INNER JOIN `gk_item_waypoint` ON `gk_item`.`id`=`gk_item_waypoint`.`id` INNER JOIN `caches` ON `gk_item_waypoint`.`wp`=`caches`.`wp_oc` WHERE `caches`.`cache_id`=&1", $r['cacheid']);
        while ($rGK = sql_fetch_array($rsGeokrety)) {
            $thiskrety = $gpxGeokrety;
            $thiskrety = mb_ereg_replace('{gkid}', $rGK['id'], $thiskrety);
            $thiskrety = mb_ereg_replace('{gkref}', sprintf("GK%04X", $rGK['id']), $thiskrety);
            $thiskrety = mb_ereg_replace('{gkname}', text_xmlentities($rGK['name']), $thiskrety);
            $gkentries .= $thiskrety . "\n";
        }
        mysql_free_result($rsGeokrety);
        $thisline = mb_ereg_replace('{geokrety}', $gkentries, $thisline);
        // additional waypoints, including personal cache note
        $childWaypoints = $childwphandler->getChildWps($r['cacheid']);
        $n = 1;
        $digits = "%0" . strlen(count($childWaypoints)) . "d";
        foreach ($childWaypoints as $childWaypoint) {
            $thiswp = $gpxWaypoints;
            $thiswp = mb_ereg_replace('{wp_lat}', sprintf('%01.5f', $childWaypoint['latitude']), $thiswp);
            $thiswp = mb_ereg_replace('{wp_lon}', sprintf('%01.5f', $childWaypoint['longitude']), $thiswp);
            $thiswp = mb_ereg_replace('{time}', $time, $thiswp);
            $thiswp = mb_ereg_replace('{name}', $r['waypoint'] . '-' . sprintf($digits, $n), $thiswp);
            $thiswp = mb_ereg_replace('{cachename}', text_xmlentities($r['name']), $thiswp);
            $thiswp = mb_ereg_replace('{comment}', text_xmlentities($childWaypoint['description']), $thiswp);
            $thiswp = mb_ereg_replace('{desc}', text_xmlentities($childWaypoint['name']), $thiswp);
            switch ($childWaypoint['type']) {
                case 1:
                    $wp_typename = "Parking Area";
                    break;
                    // well-known garmin symbols
                // well-known garmin symbols
                case 2:
                    $wp_typename = "Flag, Green";
                    break;
                    // stage / ref point
                // stage / ref point
                case 3:
                    $wp_typename = "Flag, Blue";
                    break;
                    // path
                // path
                case 4:
                    $wp_typename = "Circle with X";
                    break;
                    // final
                // final
                case 5:
                    $wp_typename = "Diamond, Green";
                    break;
                    // point of interest
                // point of interest
                default:
                    $wp_typename = "Flag, Blue";
                    break;
                    // for the case new types are forgotten here ..
            }
            $thiswp = mb_ereg_replace('{type}', text_xmlentities($wp_typename), $thiswp);
            $thiswp = mb_ereg_replace('{parent}', $r['waypoint'], $thiswp);
            $thiswp = mb_ereg_replace('{cacheid}', $r['cacheid'], $thiswp);
            $waypoints .= $thiswp;
            ++$n;
        }
        if ($cacheNote && !empty($cacheNote['latitude']) && !empty($cacheNote['longitude'])) {
            $thiswp = $gpxWaypoints;
            $thiswp = mb_ereg_replace('{wp_lat}', sprintf('%01.5f', $cacheNote['latitude']), $thiswp);
            $thiswp = mb_ereg_replace('{wp_lon}', sprintf('%01.5f', $cacheNote['longitude']), $thiswp);
            $thiswp = mb_ereg_replace('{time}', $time, $thiswp);
            $thiswp = mb_ereg_replace('{name}', $r['waypoint'] . 'NOTE', $thiswp);
            $thiswp = mb_ereg_replace('{cachename}', text_xmlentities($r['name']), $thiswp);
            $thiswp = mb_ereg_replace('{comment}', text_xmlentities($cacheNote['note']), $thiswp);
            $thiswp = mb_ereg_replace('{desc}', text_xmlentities($cache_note_text), $thiswp);
            $thiswp = mb_ereg_replace('{type}', "Reference Point", $thiswp);
            $thiswp = mb_ereg_replace('{parent}', $r['waypoint'], $thiswp);
            $thiswp = mb_ereg_replace('{cacheid}', $r['cacheid'], $thiswp);
            $waypoints .= $thiswp;
        }
        $thisline = mb_ereg_replace('{cache_waypoints}', $waypoints, $thisline);
        append_output($thisline);
    }
    mysql_free_result($rs);
    append_output($gpxFoot);
}