/usr/share/php/Horde/Service/Weather/Wwo.php is in php-horde-service-weather 2.1.4-1.
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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | <?php
/**
* This file contains the Horde_Service_Weather class for communicating with
* the World Weather Online API.
*
* Copyright 2011-2014 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_Wwo
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @category Horde
* @package Service_Weather
*/
class Horde_Service_Weather_Wwo extends Horde_Service_Weather_Base
{
const API_URL = 'http://api.worldweatheronline.com/free/v1/weather.ashx';
const SEARCH_URL = 'http://api.worldweatheronline.com/free/v1/search.ashx';
/**
* @see Horde_Service_Weather_Base::$title
* @var string
*/
public $title = 'World Weather Online';
/**
* @see Horde_Service_Weather_Base::$link
* @var string
*/
public $link = 'http://worldweatheronline.com';
/**
* @see Horde_Service_Weather::$iconMap
*/
public $iconMap = array(
'wsymbol_0001_sunny' => '32.png',
'wsymbol_0002_sunny_intervals' => '30.png',
'wsymbol_0003_white_cloud' => '26.png',
'wsymbol_0004_black_low_cloud' => '26.png',
'wsymbol_0006_mist' => '34.png',
'wsymbol_0007_fog' => '20.png',
'wsymbol_0008_clear_sky_night' => '33.png',
'wsymbol_0009_light_rain_showers' => '11.png',
'wsymbol_0010_heavy_rain_showers' => '12.png',
'wsymbol_0011_light_snow_showers' => '14.png',
'wsymbol_0012_heavy_snow_showers' => '16.png',
'wsymbol_0013_sleet_showers' => '7.png',
'wsymbol_0016_thundery_showers' => '0.png',
'wsymbol_0017_cloudy_with_light_rain' => '11.png',
'wsymbol_0018_cloudy_with_heavy_rain' => '12.png',
'wsymbol_0019_cloudy_with_light_snow' => '13.png',
'wsymbol_0020_cloudy_with_heavy_snow' => '16.png',
'wsymbol_0021_cloudy_with_sleet' => '8.png',
'wsymbol_0024_thunderstorms' => '0.png',
'wsymbol_0025_light_rain_showers_night' => '40.png',
'wsymbol_0026_heavy_rain_showers_night' => '30.png',
'wsymbol_0027_light_snow_showers_night' => '41.png',
'wsymbol_0028_heavy_snow_showers_night' => '42.png',
'wsymbol_0029_sleet_showers_night' => '7.png',
'wsymbol_0032_thundery_showers_night' => '47.png',
'wsymbol_0033_cloudy_with_light_rain_night' => '45.png',
'wsymbol_0034_cloudy_with_heavy_rain_night' => '45.png',
'wsymbol_0035_cloudy_with_light_snow_night' => '46.png',
'wsymbol_0036_cloudy_with_heavy_snow_night' => '46.png',
'wsymbol_0037_cloudy_with_sleet_night' => '8.png',
'wsymbol_0040_thunderstorms_night' => '47.png'
);
/**
* Wwo API key.
*
* @var string
*/
protected $_key;
/**
* Constructor.
*
* @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.
* - apikey: (string) Require api key for Wwo.
*
* @return Horde_Service_Weather_Wwo
*/
public function __construct(array $params = array())
{
// Check required api key parameters here...
if (empty($params['apikey'])) {
throw new InvalidArgumentException('Missing required API Key parameter.');
}
$this->_key = $params['apikey'];
unset($params['apikey']);
parent::__construct($params);
}
/**
* Obtain the current observations.
*
* @see Horde_Service_Weather_Base::getCurrentConditions
*
* @return Horde_Service_Weather_Current_Wwo
*/
public function getCurrentConditions($location)
{
$this->_getCommonElements($location);
return $this->_current;
}
/**
* Obtain the forecast for the current location.
*
* @see Horde_Service_Weather_Base::getForecast
*/
public function getForecast(
$location,
$length = Horde_Service_Weather::FORECAST_3DAY,
$type = Horde_Service_Weather::FORECAST_TYPE_STANDARD)
{
$this->_getCommonElements($location);
return $this->_forecast;
}
/**
* Search for a valid location code.
*
* @see Horde_Service_Weather_Base::searchLocations
*/
public function searchLocations($location, $type = Horde_Service_Weather::SEARCHTYPE_STANDARD)
{
switch ($type) {
case Horde_Service_Weather::SEARCHTYPE_STANDARD:
case Horde_Service_Weather::SEARCHTYPE_IP:
return $this->_parseSearchLocations($this->_searchLocations($location));
}
}
/**
* Return an autocomplete request result.
*
* @see Horde_Service_Weather_Base::autocompleteLocation
*/
public function autocompleteLocation($search)
{
$url = new Horde_Url(self::SEARCH_URL);
$url->add(array(
'q' => $search,
'format' => 'json',
'num_of_results' => 25));
return $this->_parseAutocomplete($this->_makeRequest($url));
}
/**
* Return the supported forecast lengths.
*
* @see Horde_Service_Weather_Base::getSupportedForecastLengths
*/
public function getSupportedForecastLengths()
{
return array(
3 => Horde_Service_Weather::FORECAST_3DAY,
5 => Horde_Service_Weather::FORECAST_5DAY
);
}
/**
* Populates some common data used by forecasts and current conditions.
* Weather Underground allows requesting multiple features per request,
* and only counts it as a single request against your API key. So we trade
* a bit of request time/traffic for a smaller number of requests to obtain
* information for e.g., a typical weather portal display.
*
* @param string $location The location identifier.
* @param integer $length The forecast length.
*/
protected function _getCommonElements($location, $length = Horde_Service_Weather::FORECAST_5DAY)
{
if (!empty($this->_current) && $location == $this->_lastLocation
&& $this->_lastLength == $length) {
return;
}
$this->_lastLength = $length;
$this->_lastLocation = $location;
$url = new Horde_Url(self::API_URL);
// Not sure why, but Wwo chokes if we urlencode the location?
$url->add(array(
'q' => $location,
'num_of_days' => $length,
'includeLocation' => 'yes',
'timezone' => 'yes',
'extra' => 'localObsTime'));
$results = $this->_makeRequest($url);
// Use the minimum station data provided by forecast request to
// fetch the full station data.
$station = $this->_parseStation($results->data->nearest_area[0]);
$station = $this->searchLocations($station->lat . ',' . $station->lon);
// Hack some data to allow UTC observation time to be returned.
$results->data->current_condition[0]->date = new Horde_Date($results->data->current_condition[0]->localObsDateTime);
$results->data->current_condition[0]->date->hour += -$station->getOffset();
// Parse it.
$this->_current = $this->_parseCurrent($results->data->current_condition);
// Sunrise/Sunset
$date = $this->_current->time;
$station->sunset = new Horde_Date(
date_sunset(
$date->timestamp(),
SUNFUNCS_RET_TIMESTAMP,
$station->lat,
$station->lon)
);
$station->sunrise = new Horde_Date(
date_sunrise(
$date->timestamp(),
SUNFUNCS_RET_TIMESTAMP,
$station->lat,
$station->lon)
);
$station->time = (string)$date;
$this->_station = $station;
$this->_forecast = $this->_parseForecast($results->data->weather);
}
/**
* Parses the JSON response for a location request into a station object.
*
* @param StdClass $station The response from a Location request.
*
* @return Horde_Service_Weather_Station
*/
protected function _parseStation($station)
{
$properties = array(
'name' => $station->areaName[0]->value . ', ' . $station->region[0]->value,
'city' => $station->areaName[0]->value,
'state' => $station->region[0]->value,
'country' => $station->country[0]->value,
'country_name' => '',
'lat' => $station->latitude,
'lon' => $station->longitude,
'zip' => '',
'code' => $station->latitude . ',' . $station->longitude
);
if (isset($station->timezone)) {
// Only the *current* UTC offset is provided, with no indication
// if we are in DST or not.
$properties['tz'] = $station->timezone[0]->offset;
}
return new Horde_Service_Weather_Station($properties);
}
/**
* Parses the forecast data.
*
* @param stdClass $forecast The result of the forecast request.
*
* @return Horde_Service_Weather_Forecast_Wwo The forecast.
*/
protected function _parseForecast($forecast)
{
$forecast = new Horde_Service_Weather_Forecast_Wwo($forecast, $this);
return $forecast;
}
/**
* Parse the current_conditions response.
*
* @param stdClass $current The current_condition request response object
*
* @return Horde_Service_Weather_Current
*/
protected function _parseCurrent($current)
{
// The Current object takes care of the parsing/mapping.
$current = new Horde_Service_Weather_Current_Wwo($current[0], $this);
return $current;
}
protected function _parseAutocomplete($results)
{
$return = array();
if (!empty($results->search_api->result)) {
foreach($results->search_api->result as $result) {
if (!empty($result->region[0]->value)) {
$new = new stdClass();
$new->name = $result->areaName[0]->value . ', ' . $result->region[0]->value;
$new->code = $result->latitude . ',' . $result->longitude;
$return[] = $new;
}
}
}
return $return;
}
/**
* Execute a location search.
*
* @param string $location The location text to search.
*
* @return string The location code result(s).
*/
protected function _searchLocations($location)
{
$url = new Horde_Url(self::SEARCH_URL);
$url = $url->add(array(
'timezone' => 'yes',
'q' => $location,
'num_of_results' => 10));
return $this->_makeRequest($url);
}
protected function _parseSearchLocations($response)
{
if (!empty($response->results->error)) {
throw new Horde_Service_Weather_Exception($response->results->error->message);
}
// Wwo's location search is pretty useless. It *always* returns multiple
// matches, even if you pass an explicit identifier. We need to ignore
// these, and hope for the best.
if (!empty($response->search_api->result)) {
return $this->_parseStation($response->search_api->result[0]);
}
return array();
}
/**
* Make the remote API call.
*
* @param Horde_Url $url The endpoint.
*
* @return mixed The unserialized results form the remote API call.
* @throws Horde_Service_Weather_Exception
*/
protected function _makeRequest(Horde_Url $url)
{
$url->add(array(
'format' => 'json',
'key' => $this->_key
))->setRaw(true);
$cachekey = md5('hordeweather' . $url);
if ((!empty($this->_cache) &&
!($results = $this->_cache->get($cachekey, $this->_cache_lifetime))) ||
empty($this->_cache)) {
$response = $this->_http->get($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);
}
}
$results = Horde_Serialize::unserialize($results, Horde_Serialize::JSON);
if (!($results instanceof StdClass)) {
throw new Horde_Service_Weather_Exception(sprintf(
'Error, unable to decode response: %s',
$results));
}
return $results;
}
}
|