} else { $upload = import_fetch_uri($url, $more); } } if (!$upload['ok']) { $GLOBALS['error']['upload_by_url_error'] = 1; $GLOBALS['error']['details'] = $upload['error']; $GLOBALS['smarty']->display('page_upload3.txt'); exit; } # # Okay, now process the file # if ($is_flickr) { // not entirely sure about this, there are many flickr cases (seanc | 07072011) $upload = import_preprocess_address_fields($upload); $pre_process = $upload; } else { $more = array('dots_index_on' => $dots_index_on); $pre_process = import_process_file($upload, $more); } # dumper($pre_process); # convert any errors from a bag of arrays in to a hash # where the key maps to record number (assuming the count # starts at 1. if (count($pre_process['errors'])) { $_errors = array(); foreach ($pre_process['errors'] as $e) { $_errors[$e['record']] = $e; } $pre_process['errors'] = $_errors;
function import_process_file(&$file) { # Is this something that we are going to try parsing with # ogre (assuming it's been enabled). If it has then it will # return GeoJSON and we'll just carry on pretending that's # what the file is. $use_ogre = 0; if ($GLOBALS['cfg']['enable_feature_ogre']) { $ogre_map = formats_valid_ogre_import_map(); if (isset($ogre_map[$file['type']])) { $use_ogre = 1; } } if ($use_ogre) { $new_path = "{$file['path']}.{$file['extension']}"; rename($file['path'], $new_path); $file['path'] = $new_path; loadlib("geo_ogre"); $rsp = geo_ogre_convert_file($file['path']); if (!$rsp['ok']) { $rsp['details'] = $rsp['error']; $rsp['error'] = 'ogre_fail'; return $rsp; } $fh = fopen($file['path'], 'w'); fwrite($fh, json_encode($rsp['data'])); fclose($fh); $map = formats_valid_import_map("key by extension"); $old_path = $file['path']; $new_path = str_replace(".{$file['extension']}", ".json", $file['path']); rename($old_path, $new_path); $file = array('path' => $new_path, 'name' => basename($new_path), 'size' => filesize($new_path), 'extension' => 'json', 'type' => $map['json']); } # # Basic setup stuff # $rsp = array('ok' => 0); $more = array(); if ($max = $GLOBALS['cfg']['import_max_records']) { $more['max_records'] = $max; } # # CAN HAZ FILE? # $fh = fopen($file['path'], 'r'); if (!$fh) { return array('ok' => 0, 'error' => 'failed to open file'); } # # Store the $file hash we're passing around. It may be the case # that some import related libraries do not have functions for # working with filehandles (DOMDocument for example...wtf?) # $more['file'] = $file; # # Okay, now figure what we need to load and call. We # do this by asking the import map for an extension # corresponding to the file's mime-type (note: at some # point we may need to make this a bit more fine-grained # but today we don't) and then load lib_EXTENSION and # call that library's 'parse_fh' function. # $map = formats_valid_import_map(); $type = $map[$file['type']]; $func = "{$type}_parse_fh"; # # HEY LOOK! THIS PART IS IMPORTANT!! It is left to the # format specific libraries to sanitize both field names # and values (using lib_sanitize). This is *not* a # question of validating the data (checking lat/lon # ranges etc.) but just making sure that the user isn't # passing in pure crap. Take a look at the parse_fh function # in lib_csv for an example of how/what to do. # loadlib($type); $rsp = call_user_func_array($func, array($fh, $more)); # sudo put me in a function? (20110610/straup) # made it a function (20110707 | seanc) if ($rsp['ok']) { $rsp = import_preprocess_address_fields($rsp); /* loadlib("dots_address"); $needs_geocoding = 0; # note the pass-by-ref on $row foreach ($rsp['data'] as &$row){ if (($row['latitude']) && ($row['longitude'])){ $row['_has_latlon'] = 1; continue; } $row['_has_latlon'] = 0; $row['_address'] = dots_address_parse_for_geocoding($row); $needs_geocoding += 1; } $rsp['needs_geocoding'] = $needs_geocoding; */ } # TO DO: check $GLOBALS['cfg'] to see whether we should # store a permanent copy of $file['tmp_name'] somewhere # on disk. It would be nice to store it with the sheet # ID the data has been associated which we don't have # yet so maybe this isn't the best place to do the storing... # (2010107/straup) return $rsp; }