| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the wp-forecast plugin for WordPress |
| 4 |
* |
| 5 |
* Copyright 2018-2022 Hans Matzen (email : webmaster at tuxlog dot de) |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
* |
| 21 |
* @package wp-forecast |
| 22 |
*/ |
| 23 |
|
| 24 |
if ( ! function_exists( 'ipstack_get_data' ) ) { |
| 25 |
/** |
| 26 |
* Function to get data from ipstack |
| 27 |
* |
| 28 |
* @param string $apikey The ipstack apikey. |
| 29 |
* @param string $ip The ip adress to get the coordinates for. |
| 30 |
*/ |
| 31 |
function ipstack_get_data( $apikey, $ip ) { |
| 32 |
// check parms. |
| 33 |
if ( trim( $apikey ) == '' || trim( $ip ) == '' ) { |
| 34 |
return array(); |
| 35 |
} |
| 36 |
|
| 37 |
$url = "http://api.ipstack.com/$ip?access_key=$apikey"; |
| 38 |
|
| 39 |
// get the location data for the ip. |
| 40 |
// $file = file_get_contents($url, false);. |
| 41 |
$file = wp_remote_get( $url ); |
| 42 |
$data = json_decode( $file['body'], true ); |
| 43 |
|
| 44 |
return $data; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! function_exists( 'ipstack_get_visitor_ip' ) ) { |
| 49 |
/** |
| 50 |
* Function to determine the ip of the visotor of the site. |
| 51 |
*/ |
| 52 |
function ipstack_get_visitor_ip() { |
| 53 |
$ip = ''; |
| 54 |
if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 55 |
$ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ) ); |
| 56 |
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 57 |
$ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ); |
| 58 |
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) { |
| 59 |
$ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED'] ) ); |
| 60 |
} elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) { |
| 61 |
$ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_FORWARDED_FOR'] ) ); |
| 62 |
} elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) { |
| 63 |
$ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_FORWARDED'] ) ); |
| 64 |
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 65 |
$ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
| 66 |
} else { |
| 67 |
$ip = 'none'; |
| 68 |
} |
| 69 |
|
| 70 |
return $ip; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if ( ! function_exists( 'distance_calculation' ) ) { |
| 77 |
/** |
| 78 |
* Description: Distance calculation from the latitude/longitude of 2 points |
| 79 |
* Author: Michaël Niessen (2014) |
| 80 |
* Website: http://AssemblySys.com |
| 81 |
* |
| 82 |
* If you find this script useful, you can show your |
| 83 |
* appreciation by getting Michaël a cup of coffee ;) |
| 84 |
* PayPal: MichaelNiessen |
| 85 |
* |
| 86 |
* As long as this notice (including author name and details) is included and |
| 87 |
* UNALTERED, this code can be freely used and distributed. |
| 88 |
* |
| 89 |
* @param string $point1_lat the latitude of the first point. |
| 90 |
* @param string $point1_long the longitude of the first point. |
| 91 |
* @param string $point2_lat the latitude of the second point. |
| 92 |
* @param string $point2_long the longitude of the second point. |
| 93 |
* @param string $unit the unit. |
| 94 |
* @param int $decimals the number of decimals. |
| 95 |
*/ |
| 96 |
function distance_calculation( $point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2 ) { |
| 97 |
// Calculate the distance in degrees. |
| 98 |
$degrees = rad2deg( acos( ( sin( deg2rad( $point1_lat ) ) * sin( deg2rad( $point2_lat ) ) ) + ( cos( deg2rad( $point1_lat ) ) * cos( deg2rad( $point2_lat ) ) * cos( deg2rad( $point1_long - $point2_long ) ) ) ) ); |
| 99 |
|
| 100 |
// Convert the distance in degrees to the chosen unit (kilometres, miles or nautical miles). |
| 101 |
switch ( $unit ) { |
| 102 |
case 'km': |
| 103 |
$distance = $degrees * 111.13384; // 1 degree = 111.13384 km, based on the average diameter of the Earth (12,735 km). |
| 104 |
break; |
| 105 |
case 'mi': |
| 106 |
$distance = $degrees * 69.05482; // 1 degree = 69.05482 miles, based on the average diameter of the Earth (7,913.1 miles). |
| 107 |
break; |
| 108 |
case 'nmi': |
| 109 |
$distance = $degrees * 59.97662; // 1 degree = 59.97662 nautic miles, based on the average diameter of the Earth (6,876.3 nautical miles). |
| 110 |
} |
| 111 |
|
| 112 |
return round( $distance, $decimals ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
if ( ! function_exists( 'everything_in_tags' ) ) { |
| 118 |
/** |
| 119 |
* Returns the content of a tag in an xml text string. |
| 120 |
* |
| 121 |
* @param string $string String to convert. |
| 122 |
* @param string $tagname The tagname to use. |
| 123 |
*/ |
| 124 |
function everything_in_tags( $string, $tagname ) { |
| 125 |
$pattern = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s"; |
| 126 |
preg_match( $pattern, $string, $matches ); |
| 127 |
return $matches[1]; |
| 128 |
} |
| 129 |
} |
| 130 |
|