api.whois.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Allows to receive some data about IPs and domains
  4. */
  5. class UbillingWhois {
  6. /**
  7. * Current IP for lookup
  8. *
  9. * @var string
  10. */
  11. protected $ip = '';
  12. /**
  13. * System cache object placeholder
  14. *
  15. * @var object
  16. */
  17. protected $cache = '';
  18. /**
  19. * Contains current IP data as ip=>data
  20. *
  21. * @var array
  22. */
  23. protected $ipData = array();
  24. /**
  25. * Data caching timeout
  26. */
  27. const CACHE_TIMEOUT = 2592000;
  28. /**
  29. * Some predefined routes, URLs etc..
  30. */
  31. const CACHE_KEY = 'WHOISDATA';
  32. const API_IPINFO = 'http://ip-api.com/json/';
  33. const API_PARAMS = '?fields=status,message,country,countryCode,region,regionName,city,lat,lon,isp,org,as,asname,reverse,query';
  34. const URL_ASINFO = 'http://bgp.he.net/';
  35. /**
  36. * Creates new Whois object instance
  37. *
  38. * @param string $ip
  39. * @throws Exception
  40. */
  41. public function __construct($ip) {
  42. if (!empty($ip)) {
  43. $this->setIp($ip);
  44. } else {
  45. throw new Exception('EX_EMPTY_IP');
  46. }
  47. $this->initCache();
  48. $this->loadIpData();
  49. }
  50. /**
  51. * Sets current IP
  52. *
  53. * @param string $ip
  54. *
  55. * @return void
  56. */
  57. protected function setIp($ip) {
  58. $this->ip = $ip;
  59. }
  60. /**
  61. * Creates new cache instance for further usage
  62. *
  63. * @return void
  64. */
  65. protected function initCache() {
  66. $this->cache = new UbillingCache();
  67. }
  68. /**
  69. * Runs data loaders for current IP
  70. *
  71. * @return void
  72. */
  73. protected function loadIpData() {
  74. $cachedData = $this->cache->get(self::CACHE_KEY, self::CACHE_TIMEOUT);
  75. if (empty($cachedData)) {
  76. $cachedData = array();
  77. }
  78. $this->ipData = $cachedData;
  79. $updateCache = false;
  80. if (empty($cachedData)) {
  81. $updateCache = true;
  82. } else {
  83. if (!isset($cachedData[$this->ip])) {
  84. $updateCache = true;
  85. }
  86. }
  87. //cache needs to be updated
  88. if ($updateCache) {
  89. $queryUrl = self::API_IPINFO . $this->ip . self::API_PARAMS;
  90. $api = new OmaeUrl($queryUrl);
  91. $apiResponse = $api->response();
  92. if (!empty($apiResponse)) {
  93. $ipInfoData = json_decode($apiResponse, true);
  94. $this->ipData[$this->ip] = $ipInfoData;
  95. $this->cache->set(self::CACHE_KEY, $this->ipData, self::CACHE_TIMEOUT);
  96. }
  97. }
  98. }
  99. /**
  100. * Renders minimap if long/lat is present
  101. *
  102. * @global object $ubillingConfig
  103. *
  104. * @return string
  105. */
  106. protected function renderMinimap() {
  107. $result = '';
  108. if (!empty($this->ipData[$this->ip])) {
  109. global $ubillingConfig;
  110. $ipData = $this->ipData[$this->ip];
  111. $ymconf = $ubillingConfig->getYmaps();
  112. $result = generic_MapContainer('100%', '400px', 'whoismap');
  113. $placemarks = generic_MapAddMark($ipData['lat'] . ',' . $ipData['lon'], @$ipData['city'], @$ipData['city'] . ' ' . @$ipData['isp']);
  114. $result .= generic_MapInit($ipData['lat'] . ',' . $ipData['lon'], 8, $ymconf['TYPE'], $placemarks, '', $ymconf['LANG'], 'whoismap');
  115. }
  116. return ($result);
  117. }
  118. /**
  119. * Renders IP ISP/Geo data in human readable view
  120. *
  121. * @return string
  122. */
  123. public function renderData() {
  124. $result = '';
  125. $rows = '';
  126. $miniMap = '';
  127. if (!empty($this->ipData[$this->ip])) {
  128. $ipData = $this->ipData[$this->ip];
  129. if ($ipData['status'] != 'fail') {
  130. $asData = explode(' ', $ipData['as']);
  131. $asNum = (!empty($asData[0])) ? $asData[0] : '';
  132. $asLink = (!empty($asNum)) ? wf_Link((self::URL_ASINFO . $asNum), $asNum) : '-';
  133. $cells = wf_TableCell(__('IP'), '', 'row2');
  134. $cells .= wf_TableCell($ipData['query']);
  135. $rows .= wf_TableRow($cells, 'row3');
  136. $cells = wf_TableCell(__('Reverse DNS'), '', 'row2');
  137. $cells .= wf_TableCell($ipData['reverse']);
  138. $rows .= wf_TableRow($cells, 'row3');
  139. $cells = wf_TableCell(__('ISP name'), '', 'row2');
  140. $cells .= wf_TableCell($ipData['isp']);
  141. $rows .= wf_TableRow($cells, 'row3');
  142. $cells = wf_TableCell(__('AS Name'), '', 'row2');
  143. $cells .= wf_TableCell($ipData['asname']);
  144. $rows .= wf_TableRow($cells, 'row3');
  145. $cells = wf_TableCell(__('AS'), '', 'row2');
  146. $cells .= wf_TableCell($asLink);
  147. $rows .= wf_TableRow($cells, 'row3');
  148. $cells = wf_TableCell(__('ORG'), '', 'row2');
  149. $cells .= wf_TableCell($ipData['org']);
  150. $rows .= wf_TableRow($cells, 'row3');
  151. $cells = wf_TableCell(__('Country'), '', 'row2');
  152. $cells .= wf_TableCell($ipData['country']);
  153. $rows .= wf_TableRow($cells, 'row3');
  154. $cells = wf_TableCell(__('Region'), '', 'row2');
  155. $cells .= wf_TableCell($ipData['regionName']);
  156. $rows .= wf_TableRow($cells, 'row3');
  157. $cells = wf_TableCell(__('City'), '', 'row2');
  158. $cells .= wf_TableCell($ipData['city']);
  159. $rows .= wf_TableRow($cells, 'row3');
  160. $miniMap = $this->renderMinimap();
  161. } else {
  162. $cells = wf_TableCell(__('IP'), '', 'row2');
  163. $cells .= wf_TableCell($ipData['query']);
  164. $rows .= wf_TableRow($cells, 'row3');
  165. $cells = wf_TableCell(__('Message'), '', 'row2');
  166. $cells .= wf_TableCell($ipData['message']);
  167. $rows .= wf_TableRow($cells, 'row3');
  168. }
  169. }
  170. if (!empty($rows)) {
  171. $result = wf_TableBody($rows, '100%', 0, '');
  172. $result .= $miniMap;
  173. }
  174. return ($result);
  175. }
  176. /**
  177. * Public resolved IP geo data getter
  178. *
  179. * @return array
  180. */
  181. public function getIpData() {
  182. $result = array();
  183. if (!empty($this->ip) AND ! empty($this->ipData)) {
  184. $result = $this->ipData[$this->ip];
  185. }
  186. return($result);
  187. }
  188. }