Exemplo n.º 1
0
function rss_parse_fh($fh, $more = array())
{
    $xml = fread($fh, filesize($more['file']['path']));
    fclose($fh);
    $xml = trim($xml);
    $rss = new MagpieRSS($xml, 'utf-8', 'utf-8', true);
    if (!$rss) {
        return array('ok' => 0, 'error' => 'Failed to parse the RSS. Perhaps it is incorrect or squirrel-y XML?');
    }
    $data = array();
    $record = 1;
    foreach ($rss->items as $item) {
        $record++;
        if ($more['max_records'] && $record > $more['max_records']) {
            break;
        }
        $has_latlon = 0;
        if ($geo = $item['geo']) {
            $lat = $geo['lat'];
            $lon = isset($geo['long']) ? $geo['long'] : $geo['lon'];
            list($lat, $lon) = import_ensure_valid_latlon($lat, $lon);
            $has_latlon = $lat && $lon ? 1 : 0;
        }
        if (!$has_latlon && ($geo = $item['georss'])) {
            $point = trim($geo['point']);
            list($lat, $lon) = explode(" ", $point, 2);
            list($lat, $lon) = import_ensure_valid_latlon($lat, $lon);
            $has_latlon = $lat && $lon ? 1 : 0;
        }
        # What now? Maybe throw the description in to Placemaker ?
        if (!$lat) {
            $errors[] = array('record' => $record, 'error' => 'invalid or missing latitude', 'column' => 'latitude');
            continue;
        }
        if (!$lon) {
            $errors[] = array('record' => $record, 'error' => 'invalid or missing longitude', 'column' => 'longitude');
            continue;
        }
        #
        $tmp = array('guid' => filter_strict(sanitize($item['guid'], 'str')), 'title' => filter_strict(sanitize($item['title'], 'str')), 'link' => filter_strict(sanitize($item['link'], 'str')), 'created' => filter_strict(sanitize($item['pubdate'], 'str')), 'author' => filter_strict(sanitize($item['author'], 'str')), 'description' => filter_strict(sanitize($item['description'], 'str')), 'latitude' => $lat, 'longitude' => $lon);
        # what to do about 'description' and other tags?
        if (preg_match("/^tag:flickr.com,2004:\\/photo\\/(\\d+)\$/", $tmp['guid'], $m)) {
            # remove 'foo posted a photo:' stuff here
            $tmp['flickr:id'] = $m[1];
            # Why did we (Flickr) ever do this kind of thing...
            # (20101215/straup)
            $author = str_replace("nobody@flickr.com (", "", $tmp['author']);
            $author = rtrim($author, ")");
            $tmp['author'] = $author;
            if ($woe = $item['woe']) {
                $tmp['yahoo:woeid'] = filter_strict(sanitize($woe['woeid'], 'str'));
            }
            if (isset($item['media']) && isset($item['media']['category'])) {
                $tmp['tags'] = filter_strict(sanitize($item['media']['category'], 'str'));
            }
        }
        $data[] = $tmp;
    }
    return array('ok' => 1, 'data' => &$data, 'errors' => &$errors);
}
Exemplo n.º 2
0
function gpx_parse_fh($fh, $more = array())
{
    $data = fread($fh, filesize($more['file']['path']));
    fclose($fh);
    libxml_use_internal_errors(true);
    $gpx = simplexml_load_string($data);
    if (!$gpx) {
        $errors = array();
        foreach (libxml_get_errors() as $error) {
            $errors[] = $error->message;
        }
        return array('ok' => 0, 'error' => 'failed to parse XML: ' . implode(";", $errors));
    }
    $label = (string) $gpx->trk->name;
    $label = import_scrub($label);
    $simplify = $GLOBALS['cfg']['import_do_simplification']['gpx'] && $more['simplify'] ? 1 : 0;
    $data = array();
    $errors = array();
    $record = 1;
    foreach ($gpx->trk->trkseg->trkpt as $pt) {
        $record++;
        if ($more['max_records'] && $record > $more['max_records'] && !$simplify) {
            break;
        }
        $attrs = $pt->attributes();
        $lat = (string) $attrs['lat'];
        $lon = (string) $attrs['lon'];
        list($lat, $lon) = import_ensure_valid_latlon($lat, $lon);
        if (!$lat) {
            $errors[] = array('record' => $record, 'error' => 'invalid latitude', 'column' => 'latitude');
            continue;
        }
        if (!$lon) {
            $errors[] = array('record' => $record, 'error' => 'invalid longitude', 'column' => 'longitude');
            continue;
        }
        $tmp = array();
        $tmp['latitude'] = $lat;
        $tmp['longitude'] = $lon;
        $created = (string) $pt->time;
        $elevation = (string) $pt->ele;
        $tmp['created'] = import_scrub($created);
        $tmp['elevation'] = import_scrub($elevation);
        $data[] = $tmp;
    }
    if ($simplify) {
        $data = _gpx_simplify($data);
        if ($more['max_records'] && count($data) > $more['max_records']) {
            $data = array_slice($data, 0, $more['max_records']);
        }
    }
    return array('ok' => 1, 'label' => $label, 'data' => &$data, 'errors' => &$errors, 'simplified' => $simplify);
}
Exemplo n.º 3
0
function kml_parse_fh($fh, $more = array())
{
    $fsize = filesize($more['file']['path']);
    $data = fread($fh, $fsize);
    fclose($fh);
    libxml_use_internal_errors(true);
    $xml = simplexml_load_string($data);
    if (!$xml) {
        $errors = array();
        foreach (libxml_get_errors() as $error) {
            $errors[] = $error->message;
        }
        return array('ok' => 0, 'error' => 'failed to parse XML: ' . implode(";", $errors));
    }
    # Please KML, die in a fire...
    $nl = null;
    if ($xml->NetworkLink) {
        $nl = $xml->NetworkLink;
    } else {
        if ($xml->Document->NetworkLink) {
            $nl = $xml->Document->NetworkLink;
        } else {
        }
    }
    #
    if ($nl) {
        if (!$GLOBALS['cfg']['import_kml_resolve_network_links']) {
            return array('ok' => 0, 'error' => 'Network linked KML files are not currently supported.');
        }
        $url = $nl->Url;
        $link = (string) $url->href;
        $rsp = http_get($link);
        if (!$rsp['ok']) {
            return $rsp;
        }
        $xml = new SimpleXMLElement($rsp['body']);
    }
    $ctx = $xml->Document ? $xml->Document : $xml->Folder;
    if (!$ctx) {
        return array('ok' => 0, 'error' => 'Failed to locate any placemarks');
    }
    if ($ctx->Folder) {
        $ctx = $ctx->Folder;
    }
    $label = (string) $ctx->name;
    $label = import_scrub($label);
    $data = array();
    $errors = array();
    $record = 1;
    # I hate KML...
    $placemarks = array();
    if ($ctx->Folder) {
        foreach ($ctx->Folder as $f) {
            foreach ($f->Placemark as $p) {
                $placemarks[] = $p;
            }
        }
    } else {
        $placemarks = $ctx->Placemark;
    }
    #
    foreach ($placemarks as $p) {
        $record++;
        if ($more['max_records'] && $record > $more['max_records']) {
            break;
        }
        $tmp = array();
        # do everything but the geo bits first in case we run in to a big
        # bag of points like we might find in a KML file from Google's
        # mytracks (20110111/straup)
        $title = (string) $p->name;
        $desc = (string) $p->description;
        # foursquare-isms
        if (preg_match("/^foursquare /", $label) && ($a = $p->description->a)) {
            $attrs = $a->attributes();
            $href = (string) $attrs['href'];
            if (preg_match("/venue\\/(\\d+)\\/?\$/", $href, $m)) {
                $tmp['foursquare:venue'] = $m[1];
                $desc = (string) $a;
            }
        }
        # random other stuff
        if ($pub = $p->published) {
            $pub = (string) $pub;
            $pub = import_scrub($pub);
            $tmp['created'] = $pub;
        }
        if ($vis = $p->visibility) {
            if ($vis = (string) $vis) {
                $tmp['perms'] = 'private';
            }
        }
        $title = import_scrub($title);
        $desc = import_scrub($desc);
        $tmp['title'] = $title;
        $tmp['description'] = $desc;
        # sigh...
        if ($coords = (string) $p->Point->coordinates) {
            $coords = trim($coords);
            list($lon, $lat, $altitude) = explode(",", $coords, 3);
            list($lat, $lon) = import_ensure_valid_latlon($lat, $lon);
            if (!$lat) {
                $errors[] = array('record' => $record, 'error' => 'invalid latitude', 'column' => 'latitude');
                continue;
            }
            if (!$lon) {
                $errors[] = array('record' => $record, 'error' => 'invalid longitude', 'column' => 'longitude');
                continue;
            }
            $tmp['altitude'] = import_scrub($altitude);
            $tmp['latitude'] = $lat;
            $tmp['longitude'] = $lon;
        } else {
            if (($coords = $p->MultiGeometry->LineString->coordinates) || ($coords = $p->LineString->coordinates)) {
                $simplify = $GLOBALS['cfg']['import_do_simplification']['kml'] && $more['simplify'] ? 1 : 0;
                # We're going to keep our own counter below
                $record -= 2;
                $coords = (string) $coords;
                $coords = trim($coords);
                $coords = preg_split("/[\\s]+/", $coords);
                #
                foreach ($coords as $coord) {
                    $record++;
                    if ($more['max_records'] && $record > $more['max_records'] && !$simplify) {
                        break;
                    }
                    list($lon, $lat, $altitude) = explode(",", $coord, 3);
                    list($lat, $lon) = import_ensure_valid_latlon($lat, $lon);
                    if (!$lat) {
                        $errors[] = array('record' => $record, 'error' => "invalid latitude", 'column' => 'latitude');
                        continue;
                    }
                    if (!$lon) {
                        $errors[] = array('record' => $record, 'error' => "invalid longitude", 'column' => 'longitude');
                        continue;
                    }
                    $data[] = array('latitude' => $lat, 'longitude' => $lon, 'altitude' => import_scrub($altitude));
                }
                #
                if ($simplify) {
                    $data = _kml_simplify($data);
                    if ($more['max_records'] && count($data) > $more['max_records']) {
                        $data = array_slice($data, 0, $more['max_records']);
                    }
                }
                continue;
            } else {
                # to be revisited maybe; this really just makes things
                # more complicated than not (20110415/straup)
                continue;
                $errors[] = array('record' => $record, 'error' => 'Unable to find any location information (that Dotspotting knows about).');
            }
        }
        $data[] = $tmp;
    }
    return array('ok' => 1, 'label' => $label, 'data' => &$data, 'errors' => &$errors, 'simplified' => $simplify);
}
Exemplo n.º 4
0
function json_parse_fh($fh, $more = array())
{
    $raw = fread($fh, filesize($more['file']['path']));
    $json = json_decode($raw, "as a hash");
    if (!isset($json['features'])) {
        return array('ok' => 0, 'error' => 'Missing features');
    }
    $data = array();
    $errors = array();
    $record = 1;
    foreach ($json['features'] as $f) {
        $record++;
        if ($more['max_records'] && $record > $more['max_records']) {
            break;
        }
        if (!isset($f['geometry'])) {
            $errors[] = array('error' => 'missing geometry', 'record' => $record);
            continue;
        }
        if (!preg_match("/^(?:Multi)?Point\$/", $f['geometry']['type'])) {
            $errors[] = array('error' => 'not a supported geometry', 'record' => $record);
            continue;
        }
        $tmp = array();
        if (isset($f['properties'])) {
            foreach ($f['properties'] as $key => $value) {
                $key = import_scrub($key);
                $value = import_scrub($value);
                $tmp[$key] = $value;
            }
        }
        # MultiPoints (get their own counter)
        if ($f['geometry']['type'] == 'MultiPoint') {
            $counter = $record - 1;
            foreach ($f['geometry']['coordinates'] as $coords) {
                $counter++;
                if ($more['max_records'] && $counter > $more['max_records']) {
                    break;
                }
                list($lon, $lat) = $coords;
                list($lat, $lon) = import_ensure_valid_latlon($lat, $lon);
                if (!$lat) {
                    $errors[] = array('error' => 'invalid latitude', 'record' => $record, 'column' => 'latitude');
                }
                if (!$lon) {
                    $errors[] = array('error' => 'longitude', 'record' => $record, 'column' => 'longitude');
                }
                $tmp['latitude'] = $lat;
                $tmp['longitude'] = $lon;
                $data[] = $tmp;
            }
            continue;
        }
        # plain old Points
        list($lon, $lat) = $f['geometry']['coordinates'];
        list($lat, $lon) = import_ensure_valid_latlon($lat, $lon);
        if (!$lat) {
            $errors[] = array('error' => 'invalid latitude', 'record' => $record, 'column' => 'latitude');
            continue;
        }
        if (!$lon) {
            $errors[] = array('error' => 'invalid longitude', 'record' => $record, 'column' => 'longitude');
            continue;
        }
        $tmp['latitude'] = $lat;
        $tmp['longitude'] = $lon;
        $data[] = $tmp;
    }
    return array('ok' => 1, 'data' => &$data, 'errors' => &$errors);
}