geo.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace Web;
  16. //! Geo plug-in
  17. class Geo extends \Prefab {
  18. /**
  19. * Return information about specified Unix time zone
  20. * @return array
  21. * @param $zone string
  22. **/
  23. function tzinfo($zone) {
  24. $ref=new \DateTimeZone($zone);
  25. $loc=$ref->getLocation();
  26. $trn=$ref->getTransitions($now=time(),$now);
  27. $out=[
  28. 'offset'=>$ref->
  29. getOffset(new \DateTime('now',new \DateTimeZone('UTC')))/3600,
  30. 'country'=>$loc['country_code'],
  31. 'latitude'=>$loc['latitude'],
  32. 'longitude'=>$loc['longitude'],
  33. 'dst'=>$trn[0]['isdst']
  34. ];
  35. unset($ref);
  36. return $out;
  37. }
  38. /**
  39. * Return geolocation data based on specified/auto-detected IP address
  40. * @return array|FALSE
  41. * @param $ip string
  42. **/
  43. function location($ip=NULL) {
  44. $fw=\Base::instance();
  45. $web=\Web::instance();
  46. if (!$ip)
  47. $ip=$fw->IP;
  48. $public=filter_var($ip,FILTER_VALIDATE_IP,
  49. FILTER_FLAG_IPV4|FILTER_FLAG_IPV6|
  50. FILTER_FLAG_NO_RES_RANGE|FILTER_FLAG_NO_PRIV_RANGE);
  51. if (function_exists('geoip_db_avail') &&
  52. geoip_db_avail(GEOIP_CITY_EDITION_REV1) &&
  53. $out=@geoip_record_by_name($ip)) {
  54. $out['request']=$ip;
  55. $out['region_code']=$out['region'];
  56. $out['region_name']='';
  57. if (!empty($out['country_code']) && !empty($out['region']))
  58. $out['region_name']=geoip_region_name_by_code(
  59. $out['country_code'],$out['region']
  60. );
  61. unset($out['country_code3'],$out['region'],$out['postal_code']);
  62. return $out;
  63. }
  64. if (($req=$web->request('http://www.geoplugin.net/json.gp'.
  65. ($public?('?ip='.$ip):''))) &&
  66. $data=json_decode($req['body'],TRUE)) {
  67. $out=[];
  68. foreach ($data as $key=>$val)
  69. if (!strpos($key,'currency') && $key!=='geoplugin_status'
  70. && $key!=='geoplugin_region')
  71. $out[$fw->snakecase(substr($key, 10))]=$val;
  72. return $out;
  73. }
  74. return FALSE;
  75. }
  76. /**
  77. * Return weather data based on specified latitude/longitude
  78. * @return array|FALSE
  79. * @param $latitude float
  80. * @param $longitude float
  81. * @param $key string
  82. **/
  83. function weather($latitude,$longitude,$key) {
  84. $fw=\Base::instance();
  85. $web=\Web::instance();
  86. $query=[
  87. 'lat'=>$latitude,
  88. 'lon'=>$longitude,
  89. 'APPID'=>$key,
  90. 'units'=>'metric'
  91. ];
  92. return ($req=$web->request(
  93. 'http://api.openweathermap.org/data/2.5/weather?'.
  94. http_build_query($query)))?
  95. json_decode($req['body'],TRUE):
  96. FALSE;
  97. }
  98. }