/usr/share/php/Horde/Service/Weather/Metar.php is in php-horde-service-weather 2.5.4-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | <?php
/**
* Copyright 2016-2017 Horde LLC (http://www.horde.org/)
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Weather
*/
/**
* Horde_Service_Weather_Metar
*
* Responsible for parsing encoded METAR and TAF data.
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @category Horde
* @package Service_Weather
*/
class Horde_Service_Weather_Metar extends Horde_Service_Weather_Base
{
/**
* Database handle. Expects to have the following table available:
*
* @var Horde_Db_Adapter_Base
*/
protected $_db;
/**
* Name of table containing the NOAA METAR database.
*
* @var string
*/
protected $_tableName = 'horde_metar_airports';
/**
* Default paths to download weather data.
*
* @var string
*/
protected $_metar_path = 'http://tgftp.nws.noaa.gov/data/observations/metar/stations';
protected $_taf_path = 'http://tgftp.nws.noaa.gov/data/forecasts/taf/stations';
/**
* Local cache of locations.
*
* @var array
*/
protected $_locations;
/**
* Constructor.
*
* In addtion to the params for the parent class, you can also set a
* database adapter for NOAA station lookups, and if you don't want
* to use the default METAR/TAF http locations, you can set them here too.
* Note only HTTP is currently supported, but file and ftp are @todo.
*
* @param array $params Parameters:
* - cache: (Horde_Cache) Optional Horde_Cache object.
* - cache_lifetime: (integer) Lifetime of cached data, if caching.
* - http_client: (Horde_Http_Client) Required http client object.
* - db: (Horde_Db_Adapter_Base) DB Adapter for METAR DB.
* - metar_path: (string) Path or URL to METAR data.
* - taf_path: (string) Path or URL to TAF data.
*/
public function __construct(array $params = array())
{
// Mini-hack to avoid passing a http_client object if
// we really don't need it.
if (empty($params['http_client']) &&
!empty($params['metar_path']) &&
!empty($params['taf_path'])) {
$params['http_client'] = true;
}
parent::__construct($params);
if (!empty($params['db'])) {
$this->_db = $params['db'];
}
if (!empty($params['metar_path'])) {
$this->_metar_path = $params['metar_path'];
}
if (!empty($params['taf_path'])) {
$this->_taf_path = $params['taf_path'];
}
if (!empty($params['table_name'])) {
$this->_tableName = $params['table_name'];
}
}
/**
* Returns the current observations (METAR).
*
* @param string $location The location string.
*
* @return Horde_Service_Weather_Current_Base
* @throws Horde_Service_Weather_Exception
*/
public function getCurrentConditions($location)
{
$this->_station = $this->_getStation($location);
// Sniff out type of request.
$endpoint = sprintf('%s/%s.TXT', $this->_metar_path, $location);
$pathinfo = parse_url($endpoint);
if (empty($pathinfo['scheme']) && file_exists($endpoint)) {
$pathinfo['scheme'] = 'file';
} elseif (empty($pathinfo['scheme'])) {
throw new Horde_Service_Weather_Exception('Invalid path to METAR data.');
}
switch ($pathinfo['scheme']) {
case 'http':
$data = $this->_makeRequest($endpoint);
break;
case 'file':
$data = file_get_contents(realpath($endpoint));
break;
}
if (empty($data)) {
throw new Horde_Service_Weather_Exception('METAR file not found.');
}
$parser = new Horde_Service_Weather_Parser_Metar(array('units' => $this->units));
return new Horde_Service_Weather_Current_Metar(
$parser->parse($data),
$this
);
}
/**
* Performs a HTTP request.
*
* @param string $url The URL endpoint.
* @param integer $lifetime The cache lifetime.
*
* @return string The response body of the request.
* @throws Horde_Service_Weather_Exception
*/
protected function _makeRequest($url, $lifetime = 86400)
{
$cachekey = md5('hordeweather' . $url);
if ((!empty($this->_cache) && !$results = $this->_cache->get($cachekey, $lifetime)) ||
empty($this->_cache)) {
$url = new Horde_Url($url);
$response = $this->_http->get((string)$url);
if (!$response->code == '200') {
throw new Horde_Service_Weather_Exception($response->code);
}
$results = $response->getBody();
if (!empty($this->_cache)) {
$this->_cache->set($cachekey, $results);
}
}
return $results;
}
/**
* Returns the forecast for the current location.
*
* @param string $location The location code.
* @param integer $length The forecast length, a
* Horde_Service_Weather::FORECAST_* constant.
* (Ignored)
* @param integer $type The type of forecast to return, a
* Horde_Service_Weather::FORECAST_TYPE_* constant
* (Ignored)
*
* @return Horde_Service_Weather_Forecast_Base
* @throws Horde_Service_Weather_Exception
*/
public function getForecast(
$location,
$length = Horde_Service_Weather::FORECAST_3DAY,
$type = Horde_Service_Weather::FORECAST_TYPE_STANDARD)
{
$this->_station = $this->_getStation($location);
// Sniff out type of request.
$endpoint = sprintf('%s/%s.TXT', $this->_taf_path, $location);
$pathinfo = parse_url($endpoint);
if (empty($pathinfo['scheme']) && file_exists($endpoint)) {
$pathinfo['scheme'] = 'file';
} elseif (empty($pathinfo['scheme'])) {
throw new Horde_Service_Weather_Exception('Invalid path to TAF data.');
}
switch ($pathinfo['scheme']) {
case 'http':
$url = sprintf('%s/%s.TXT', $this->_taf_path, $location);
$data = $this->_makeRequest($url);
break;
case 'file':
$data = file_get_contents(realpath($pathinfo['path']));
break;
}
if (empty($data)) {
throw new Horde_Service_Weather_Exception('TAF file not found.');
}
// Parse the data.
$parser = new Horde_Service_Weather_Parser_Taf(array('units' => $this->units));
return new Horde_Service_Weather_Forecast_Taf(
$parser->parse($data),
$this
);
}
/**
* Searches locations.
*
* @param string $location The location string to search.
* @param integer $type The type of search to perform, a
* Horde_Service_Weather::SEARCHTYPE_* constant.
*
* @return Horde_Service_Weather_Station The search location suitable to use
* directly in a weather request.
* @throws Horde_Service_Weather_Exception
*/
public function searchLocations(
$location,
$type = Horde_Service_Weather::SEARCHTYPE_STANDARD)
{
try {
return $this->_getStation($location);
} catch (Horde_Exception_NotFound $e) {
return new Horde_Service_Weather_Station(array(
'code' => $location
));
}
}
/**
* Get array of supported forecast lengths.
*
* @return array The array of supported lengths.
*/
public function getSupportedForecastLengths()
{
// There are no "normal" forecast lengths in TAF data.
return array();
}
/**
* Return an array containing all available METAR locations/airports.
*
* @return array An array of station information. Each entry contains:
* - icao: The ICAO identifier of the location.
* - name: The human readable name of the station.
* - country: The country the station is located in (if available).
*/
public function getLocations()
{
if (empty($this->_locations)) {
$this->_locations = $this->_getLocations();
}
return $this->_locations;
}
/**
* Searches for locations that begin with the text in $search.
*
* @param string $search The text to search.
*
* @return array An array of stdClass objects with 'name' and 'code'
* properties.
* @throws Horde_Service_Weather_Exception
*/
public function autocompleteLocation($search)
{
if (empty($this->_db)) {
return array();
}
$sql = 'SELECT icao, name, state, municipality, country FROM ' . $this->_tableName . ' WHERE '
. 'name LIKE ? OR icao LIKE ? OR state LIKE ? OR municipality LIKE ?';
try {
$rows = $this->_db->select($sql, array_fill(0, 4, $search . '%'));
} catch (Horde_Db_Exception $e) {
throw new Horde_Service_Weather_Exception($e);
}
$results = array();
foreach ($rows as $row) {
$obj = new stdClass();
$obj->name = sprintf('%s (%s, %s, %s)', $row['name'], $row['municipality'], $row['state'], $row['country']);
$obj->code = $row['icao'];
$results[] = $obj;
}
return $results;
}
/**
* Perform DB query to obtain list of airport codes.
*
* @return array An array of station information. Each entry contains:
* - icao: The ICAO identifier of the location.
* - name: The human readable name of the station.
* - country: The country the station is located in (if available).
*
* @throws Horde_Service_Weather_Exception
*/
protected function _getLocations()
{
if (empty($this->_db)) {
return array();
}
$sql = 'SELECT icao, name, state, municipality, country FROM ' . $this->_tableName . ' ORDER BY country';
try {
return $this->_db->selectAll($sql);
} catch (Horde_Exception $e) {
throw new Horde_Service_Weather_Exception($e);
}
}
/**
* Return a station object matching $code.
*
* @param string $code The ICAO station identifier.
*
* @return Horde_Service_Weather_Station The station object.
* @throws Horde_Service_Weather_Exception
* @throws Horde_Exception_NotFound
*/
protected function _getStation($code)
{
if (empty($this->_db)) {
return new Horde_Service_Weather_Station(array(
'code' => $code,
'name' => $code
));
}
$sql = 'SELECT icao, name, country, latitude, longitude from ' . $this->_tableName . ' WHERE icao = ?';
try {
$result = $this->_db->selectOne($sql, array($code));
} catch (Horde_Db_Exception $e) {
throw new Horde_Service_Weather_Exception($e);
}
if (empty($result)) {
throw new Horde_Exception_NotFound();
}
return new Horde_Service_Weather_Station(array(
'name' => $result['name'],
'code' => $code,
'country_name' => $result['country'],
'lat' => $result['latitude'],
'lon' => $result['lon']
));
}
}
|