geo.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 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']=(!empty($out['country_code']) && !empty($out['region']))
  57. ? geoip_region_name_by_code($out['country_code'],$out['region']) : '';
  58. unset($out['country_code3'],$out['region'],$out['postal_code']);
  59. return $out;
  60. }
  61. if (($req=$web->request('http://www.geoplugin.net/json.gp'.
  62. ($public?('?ip='.$ip):''))) &&
  63. $data=json_decode($req['body'],TRUE)) {
  64. $out=[];
  65. foreach ($data as $key=>$val)
  66. if (!strpos($key,'currency') && $key!=='geoplugin_status'
  67. && $key!=='geoplugin_region')
  68. $out[$fw->snakecase(substr($key, 10))]=$val;
  69. return $out;
  70. }
  71. return FALSE;
  72. }
  73. /**
  74. * Return weather data based on specified latitude/longitude
  75. * @return array|FALSE
  76. * @param $latitude float
  77. * @param $longitude float
  78. * @param $key string
  79. **/
  80. function weather($latitude,$longitude,$key) {
  81. $fw=\Base::instance();
  82. $web=\Web::instance();
  83. $query=[
  84. 'lat'=>$latitude,
  85. 'lon'=>$longitude,
  86. 'APPID'=>$key,
  87. 'units'=>'metric'
  88. ];
  89. return ($req=$web->request(
  90. 'http://api.openweathermap.org/data/2.5/weather?'.
  91. http_build_query($query)))?
  92. json_decode($req['body'],TRUE):
  93. FALSE;
  94. }
  95. }