Browse Source

Add IP geolocation for weather, fix icon alignment

master
Skylar Ittner 3 years ago
parent
commit
41e54846f0
  1. 3
      .gitignore
  2. 3
      composer.json
  3. 212
      composer.lock
  4. 47
      lib/Weather.lib.php
  5. 14
      pages/home.php
  6. 14
      pages/weather.php
  7. 4
      settings.template.php

3
.gitignore

@ -3,4 +3,5 @@ settings.php
nbproject/private
*.sync-conflict*
/database.mwb.bak
/cache/thumb/*.jpg
/cache/thumb/*.jpg
GeoLite2-City.mmdb

3
composer.json

@ -4,7 +4,8 @@
"type": "project",
"require": {
"catfan/medoo": "^1.5",
"guzzlehttp/guzzle": "^6.2"
"guzzlehttp/guzzle": "^6.2",
"geoip2/geoip2": "^2.9"
},
"license": "MPL-2.0",
"authors": [

212
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "5642e5d15688c9510f4b0d1b4ceed67e",
"content-hash": "b3a3d4cc1ba1c7371c6d652342d2c7af",
"packages": [
{
"name": "catfan/medoo",
@ -65,6 +65,114 @@
],
"time": "2018-12-08T20:24:23+00:00"
},
{
"name": "composer/ca-bundle",
"version": "1.1.4",
"source": {
"type": "git",
"url": "https://github.com/composer/ca-bundle.git",
"reference": "558f321c52faeb4828c03e7dc0cfe39a09e09a2d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/composer/ca-bundle/zipball/558f321c52faeb4828c03e7dc0cfe39a09e09a2d",
"reference": "558f321c52faeb4828c03e7dc0cfe39a09e09a2d",
"shasum": ""
},
"require": {
"ext-openssl": "*",
"ext-pcre": "*",
"php": "^5.3.2 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5",
"psr/log": "^1.0",
"symfony/process": "^2.5 || ^3.0 || ^4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Composer\\CaBundle\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
}
],
"description": "Lets you find a path to the system CA bundle, and includes a fallback to the Mozilla CA bundle.",
"keywords": [
"cabundle",
"cacert",
"certificate",
"ssl",
"tls"
],
"time": "2019-01-28T09:30:10+00:00"
},
{
"name": "geoip2/geoip2",
"version": "v2.9.0",
"source": {
"type": "git",
"url": "https://github.com/maxmind/GeoIP2-php.git",
"reference": "a807fbf65212eef5d8d2db1a1b31082b53633d77"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maxmind/GeoIP2-php/zipball/a807fbf65212eef5d8d2db1a1b31082b53633d77",
"reference": "a807fbf65212eef5d8d2db1a1b31082b53633d77",
"shasum": ""
},
"require": {
"maxmind-db/reader": "~1.0",
"maxmind/web-service-common": "~0.5",
"php": ">=5.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "2.*",
"phpunit/phpunit": "4.*",
"squizlabs/php_codesniffer": "3.*"
},
"type": "library",
"autoload": {
"psr-4": {
"GeoIp2\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Gregory J. Oschwald",
"email": "goschwald@maxmind.com",
"homepage": "http://www.maxmind.com/"
}
],
"description": "MaxMind GeoIP2 PHP API",
"homepage": "https://github.com/maxmind/GeoIP2-php",
"keywords": [
"IP",
"geoip",
"geoip2",
"geolocation",
"maxmind"
],
"time": "2018-04-10T15:32:59+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.3.3",
@ -248,6 +356,108 @@
],
"time": "2018-12-04T20:46:45+00:00"
},
{
"name": "maxmind-db/reader",
"version": "v1.4.1",
"source": {
"type": "git",
"url": "https://github.com/maxmind/MaxMind-DB-Reader-php.git",
"reference": "eb83d0ee1c1f9b8a340206302136bc81ee02ae74"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maxmind/MaxMind-DB-Reader-php/zipball/eb83d0ee1c1f9b8a340206302136bc81ee02ae74",
"reference": "eb83d0ee1c1f9b8a340206302136bc81ee02ae74",
"shasum": ""
},
"require": {
"php": ">=5.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "2.*",
"phpunit/phpunit": "4.* || 5.*",
"satooshi/php-coveralls": "1.0.*",
"squizlabs/php_codesniffer": "3.*"
},
"suggest": {
"ext-bcmath": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
"ext-gmp": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
"ext-maxminddb": "A C-based database decoder that provides significantly faster lookups"
},
"type": "library",
"autoload": {
"psr-4": {
"MaxMind\\Db\\": "src/MaxMind/Db"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Gregory J. Oschwald",
"email": "goschwald@maxmind.com",
"homepage": "http://www.maxmind.com/"
}
],
"description": "MaxMind DB Reader API",
"homepage": "https://github.com/maxmind/MaxMind-DB-Reader-php",
"keywords": [
"database",
"geoip",
"geoip2",
"geolocation",
"maxmind"
],
"time": "2019-01-04T19:55:56+00:00"
},
{
"name": "maxmind/web-service-common",
"version": "v0.5.0",
"source": {
"type": "git",
"url": "https://github.com/maxmind/web-service-common-php.git",
"reference": "61a9836fa3bb1743ab89752bae5005d71e78c73b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maxmind/web-service-common-php/zipball/61a9836fa3bb1743ab89752bae5005d71e78c73b",
"reference": "61a9836fa3bb1743ab89752bae5005d71e78c73b",
"shasum": ""
},
"require": {
"composer/ca-bundle": "^1.0.3",
"ext-curl": "*",
"ext-json": "*",
"php": ">=5.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "2.*",
"phpunit/phpunit": "4.*",
"squizlabs/php_codesniffer": "3.*"
},
"type": "library",
"autoload": {
"psr-4": {
"MaxMind\\Exception\\": "src/Exception",
"MaxMind\\WebService\\": "src/WebService"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Gregory Oschwald",
"email": "goschwald@maxmind.com"
}
],
"description": "Internal MaxMind Web Service API",
"homepage": "https://github.com/maxmind/web-service-common-php",
"time": "2018-02-12T22:31:54+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",

47
lib/Weather.lib.php

@ -6,6 +6,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
use GeoIp2\Database\Reader;
abstract class Weather {
protected $conditions = [];
@ -14,12 +16,49 @@ abstract class Weather {
protected $high;
protected $lat = 0.0;
protected $lng = 0.0;
protected $locationname = "";
public function __construct($latitude, $longitude) {
$this->lat = $latitude;
$this->lng = $longitude;
}
/**
* Attempt to find and set the user's location based on the client IP address.
* @global type $SETTINGS
* @return boolean true if successful, false if not
*/
public function setLocationByUserIP() {
global $SETTINGS;
// Make sure we'll have a valid IP when testing on localhost
if ($SETTINGS['debug'] && $_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
// This should geolocate to Helena, Montana, United States
$_SERVER['REMOTE_ADDR'] = "206.127.90.1";
}
try {
$reader = new Reader($SETTINGS['geoip_db']);
// Get the user's IP address
$clientip = $_SERVER['REMOTE_ADDR'];
// Check if we're behind CloudFlare and adjust accordingly
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$clientip = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$record = $reader->city($clientip);
$country = $record->country->name;
$region = $record->mostSpecificSubdivision->name;
$this->locationname = $record->city->name . ", $region";
$this->lat = $record->location->latitude;
$this->lng = $record->location->longitude;
return true;
} catch (GeoIp2\Exception\AddressNotFoundException $ex) {
return false;
}
}
abstract protected function loadForecast();
// Getters
@ -32,6 +71,10 @@ abstract class Weather {
return $this->lng;
}
public function getLocationName(): string {
return $this->locationname;
}
public function getForecast(): array {
return $this->conditions;
}
@ -50,6 +93,10 @@ abstract class Weather {
// Setters
public function setLocationName(string $name) {
$this->locationname = $name;
}
public function setForecast(array $conditions) {
$this->conditions = $conditions;
}

14
pages/home.php

@ -9,6 +9,7 @@ header("Link: <static/img/news-placeholder.svg>; rel=preload; as=image", false);
$weatherclass = "Weather_" . $SETTINGS['sources']['weather'];
$weather = new $weatherclass(46.595, -112.027); // TODO: get user location
$weather->setLocationByUserIP();
$weather->loadForecast();
$tempunits = "C";
@ -35,13 +36,13 @@ foreach ($newsitems as $item) {
<div class="card mb-4">
<div class="card-body">
<div class="d-flex flex-wrap justify-content-around">
<div class="mr-4 mb-2 text-center">
<div class="mx-4 mb-2 text-center">
<?php
$currently = $weather->getCurrently();
$forecast = $weather->getForecast();
?>
<div class="d-flex flex-wrap">
<div class="mr-4 display-4">
<div class="d-flex flex-wrap justify-content-center">
<div class="mx-4 display-4">
<i class="wi wi-fw <?php echo $currently->getIcon(); ?>"></i>
</div>
<div>
@ -112,7 +113,12 @@ foreach ($newsitems as $item) {
</div>
<div class="text-muted">
<i class="fas fa-map-marker-alt"></i> <?php echo round($weather->getLatitude(), 2) . ", " . round($weather->getLongitude(), 2); ?>
<i class="fas fa-map-marker-alt"></i> <?php
if (!empty($weather->getLocationName())) {
echo htmlentities($weather->getLocationName()) . " | ";
}
echo round($weather->getLatitude(), 2) . ", " . round($weather->getLongitude(), 2);
?>
</div>
</div>
</div>

14
pages/weather.php

@ -7,6 +7,7 @@
$weatherclass = "Weather_" . $SETTINGS['sources']['weather'];
$weather = new $weatherclass(46.595, -112.027); // TODO: get user location
$weather->setLocationByUserIP();
$weather->loadForecast();
$tempunits = "C";
@ -24,13 +25,13 @@ if (!empty($_COOKIE['TemperatureUnitsPref']) && preg_match("/[FCK]/", $_COOKIE['
<div class="card mb-4">
<div class="card-body">
<div class="d-flex flex-wrap justify-content-around">
<div class="mr-4 mb-2 text-center">
<div class="mx-2 mb-2 text-center">
<?php
$currently = $weather->getCurrently();
$forecast = $weather->getForecast();
?>
<div class="d-flex flex-wrap">
<div class="mr-4 display-4">
<div class="d-flex flex-wrap justify-content-center">
<div class="mx-4 display-4">
<i class="wi wi-fw <?php echo $currently->getIcon(); ?>"></i>
</div>
<div>
@ -112,7 +113,12 @@ if (!empty($_COOKIE['TemperatureUnitsPref']) && preg_match("/[FCK]/", $_COOKIE['
</div>
<div class="text-muted">
<i class="fas fa-map-marker-alt"></i> <?php echo round($weather->getLatitude(), 2) . ", " . round($weather->getLongitude(), 2); ?>
<i class="fas fa-map-marker-alt"></i> <?php
if (!empty($weather->getLocationName())) {
echo htmlentities($weather->getLocationName()) . " | ";
}
echo round($weather->getLatitude(), 2) . ", " . round($weather->getLongitude(), 2);
?>
</div>
</div>
</div>

4
settings.template.php

@ -53,6 +53,10 @@ $SETTINGS = [
],
"weather" => "DarkSky"
],
// Location of MaxMind GeoIP database
// Required attribution: This product includes GeoLite2 data created by
// MaxMind, available from http://www.maxmind.com
"geoip_db" => __DIR__ . "/GeoLite2-City.mmdb",
// List of required user permissions to access this app.
"permissions" => [
],

Loading…
Cancel
Save