geo.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfree.sf.net).
  5. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. PURPOSE.
  9. Please see the license.txt file for more information.
  10. */
  11. namespace Web;
  12. //! Geo plug-in
  13. class Geo extends \Prefab {
  14. /**
  15. * Return information about specified Unix time zone
  16. * @return array
  17. * @param $zone string
  18. **/
  19. function tzinfo($zone) {
  20. $ref=new \DateTimeZone($zone);
  21. $loc=$ref->getLocation();
  22. $trn=$ref->getTransitions($now=time(),$now);
  23. $out=array(
  24. 'offset'=>$ref->
  25. getOffset(new \DateTime('now',new \DateTimeZone('GMT')))/3600,
  26. 'country'=>$loc['country_code'],
  27. 'latitude'=>$loc['latitude'],
  28. 'longitude'=>$loc['longitude'],
  29. 'dst'=>$trn[0]['isdst']
  30. );
  31. unset($ref);
  32. return $out;
  33. }
  34. /**
  35. * Return geolocation data based on specified/auto-detected IP address
  36. * @return array|FALSE
  37. * @param $ip string
  38. **/
  39. function location($ip=NULL) {
  40. $fw=\Base::instance();
  41. $web=\Web::instance();
  42. if (!$ip)
  43. $ip=$fw->get('IP');
  44. $public=filter_var($ip,FILTER_VALIDATE_IP,
  45. FILTER_FLAG_IPV4|FILTER_FLAG_IPV6|
  46. FILTER_FLAG_NO_RES_RANGE|FILTER_FLAG_NO_PRIV_RANGE);
  47. if (function_exists('geoip_db_avail') &&
  48. geoip_db_avail(GEOIP_CITY_EDITION_REV1) &&
  49. $out=@geoip_record_by_name($ip)) {
  50. $out['request']=$ip;
  51. $out['region_code']=$out['region'];
  52. $out['region_name']=geoip_region_name_by_code(
  53. $out['country_code'],$out['region']);
  54. unset($out['country_code3'],$out['region'],$out['postal_code']);
  55. return $out;
  56. }
  57. if (($req=$web->request('http://www.geoplugin.net/json.gp'.
  58. ($public?('?ip='.$ip):''))) &&
  59. $data=json_decode($req['body'],TRUE)) {
  60. $out=array();
  61. foreach ($data as $key=>$val)
  62. if (!strpos($key,'currency') && $key!=='geoplugin_status'
  63. && $key!=='geoplugin_region')
  64. $out[$fw->snakecase(substr($key, 10))]=$val;
  65. return $out;
  66. }
  67. return FALSE;
  68. }
  69. /**
  70. * Return weather data based on specified latitude/longitude
  71. * @return array|FALSE
  72. * @param $latitude float
  73. * @param $longitude float
  74. **/
  75. function weather($latitude,$longitude) {
  76. $fw=\Base::instance();
  77. $web=\Web::instance();
  78. $query=array(
  79. 'lat'=>$latitude,
  80. 'lng'=>$longitude,
  81. 'username'=>$fw->hash($fw->get('IP'))
  82. );
  83. return ($req=$web->request(
  84. 'http://ws.geonames.org/findNearByWeatherJSON?'.
  85. http_build_query($query))) &&
  86. ($data=json_decode($req['body'],TRUE)) &&
  87. isset($data['weatherObservation'])?
  88. $data['weatherObservation']:
  89. FALSE;
  90. }
  91. }