Просмотр файла news/libraries/web/geo.php

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