/** * Resolve an IP address to location * * @param string $ip IP Address * @param mixed $format Format for an output string, or false to output result object * @return mixed */ public static function resolveIP($ip = '', $format = "%S %n, %z %L, %C") { if (!$ip) { return false; } if (!isset(self::$geocoder)) { new ElggGeocoder(); } $geocoder = self::$geocoder; if (!$geocoder) { return false; } try { $data = $geocoder->geocode($ip); } catch (Exception $e) { elgg_log("ElggIPResolver::resolveIP failed with the following message: " . $e->getMessage(), 'WARNING'); } if ($data) { if ($format) { $formatter = new Formatter($data); return $formatter->format($format); } else { return $data; } } else { return false; } }
/** * @dataProvider dataProviderForTestFormat */ public function testFormat($data, $format, $expected) { $geocodedObject = new Geocoded(); $geocodedObject->fromArray($data); $formatter = new Formatter($geocodedObject); $result = $formatter->format($format); $this->assertTrue(is_string($result)); $this->assertEquals($expected, $result); }
/** * Resolve an IP address to location * * @param string $ip IP Address * @param mixed $format Format for an output string, or false to output result object * @return mixed */ public static function resolveIP($ip = '', $format = "%S %n, %z %L, %C") { if (!$ip) { return false; } if (!isset(self::$geocoder)) { new ElggGeocoder(); } $geocoder = self::$geocoder; if (!$geocoder) { return false; } $data = $geocoder->geocode($ip); if ($data) { if ($format) { $formatter = new Formatter($data); return $formatter->format($format); } else { return $data; } } else { return false; } }
/** * Get coordinates and location name of the current session * @return array */ function get_geopositioning() { // We have a session location set previously or by another plugin if (isset($_SESSION['geopositioning'])) { return $_SESSION['geopositioning']; } // Get an address from IP $data = ElggIPResolver::resolveIP($_SERVER['REMOTE_ADDR'], false); if ($data) { $formatter = new Formatter($data); return array('location' => $formatter->format("%S %n, %z %L, %C"), 'latitude' => $data->getLatitude(), 'longitude' => $data->getLongitude()); } else { if (elgg_is_logged_in()) { $user = elgg_get_logged_in_user_entity(); return array('location' => $user->getLocation(), 'latitude' => $user->getLatitude(), 'longitude' => $user->getLongitude()); } else { return array('location' => '', 'latitude' => 0, 'longitude' => 0); } } }
protected function execute(InputInterface $input, OutputInterface $output) { $coordinate = new Coordinate($input->getArgument('coordinate')); $geocoder = new Geocoder(); $adapter = $this->getAdapter($input->getOption('adapter')); $provider = $this->getProvider($input->getOption('provider')); if ($input->getOption('args')) { $args = is_array($input->getOption('args')) ? implode(',', $input->getOption('args')) : $input->getOption('args'); $geocoder->registerProvider(new $provider(new $adapter(), $args)); } else { $geocoder->registerProvider(new $provider(new $adapter())); } $reversed = $geocoder->reverse($coordinate->getLatitude(), $coordinate->getLongitude()); $formatter = new Formatter($reversed); if ($input->getOption('raw')) { $result = array(); $result[] = sprintf('<label>Adapter</label>: <value>%s</value>', $adapter); $result[] = sprintf('<label>Provider</label>: <value>%s</value>', $provider); if ($input->getOption('args')) { $result[] = sprintf('<label>Arguments</label>: <value>%s</value>', $args); } $result[] = '---'; $result[] = sprintf('<label>Latitude</label>: <value>%s</value>', $reversed->getLatitude()); $result[] = sprintf('<label>Longitude</label>: <value>%s</value>', $reversed->getLongitude()); if (null !== ($bounds = $reversed->getBounds())) { $result[] = '<label>Bounds</label>'; $result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']); $result[] = sprintf(' - <label>West</label>: <value>%s</value>', $bounds['west']); $result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']); $result[] = sprintf(' - <label>East</label>: <value>%s</value>', $bounds['east']); } $result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $reversed->getStreetNumber()); $result[] = sprintf('<label>Street Name</label>: <value>%s</value>', $reversed->getStreetName()); $result[] = sprintf('<label>Zipcode</label>: <value>%s</value>', $reversed->getZipcode()); $result[] = sprintf('<label>City</label>: <value>%s</value>', $reversed->getCity()); $result[] = sprintf('<label>City District</label>: <value>%s</value>', $reversed->getCityDistrict()); $result[] = sprintf('<label>County</label>: <value>%s</value>', $reversed->getCounty()); $result[] = sprintf('<label>County Code</label>: <value>%s</value>', $reversed->getCountyCode()); $result[] = sprintf('<label>Region</label>: <value>%s</value>', $reversed->getRegion()); $result[] = sprintf('<label>Region Code</label>: <value>%s</value>', $reversed->getRegionCode()); $result[] = sprintf('<label>Country</label>: <value>%s</value>', $reversed->getCountry()); $result[] = sprintf('<label>Country Code</label>: <value>%s</value>', $reversed->getCountryCode()); $result[] = sprintf('<label>Timezone</label>: <value>%s</value>', $reversed->getTimezone()); } elseif ($input->getOption('json')) { $result = sprintf('<value>%s</value>', json_encode($reversed->toArray())); } else { $result = sprintf('<value>%s</value>', $formatter->format($input->getOption('format'))); } $output->writeln($result); }