Changeset 31341


Ignore:
Timestamp:
Jan 22, 2016, 7:04:13 PM (8 years ago)
Author:
plg
Message:

api.hostip.info no longer works, replaced by freegeoip.net

use the standard (in Piwigo) fetchRemote instead of a specific function for whois_online

speed: use geoiplookup if possible

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/whois_online/include/wo_admin_functions.inc.php

    r9145 r31341  
    44if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
    55if (!defined('IN_ADMIN') or !IN_ADMIN) die('Hacking attempt!');
    6 
    7 
    8 if ( !function_exists('pwg_get_contents') ) {
    9         function pwg_get_contents($url, $mode='') {
    10 
    11                 global $pwg_mode, $pwg_prev_host;
    12                 $timeout = 5; // will be a parameter (only for the socket)
    13 
    14                 $host = (strtolower(substr($url,0,7)) == 'http://') ? substr($url,7) : $url;
    15                 $host = (strtolower(substr($host,0,8)) == 'https://') ? substr($host,8) : $host;
    16                 $doc = substr($host, strpos($host, '/'));
    17                 $host = substr($host, 0, strpos($host, '/'));
    18 
    19                 if ($pwg_prev_host != $host) $pwg_mode = ''; // What was possible with one website could be different with another
    20                 $pwg_prev_host = $host;
    21                 if (isset($pwg_mode)) $mode = $pwg_mode;
    22                 if ($mode == 'r') $mode = '';
    23                 // $mode = 'ch'; // Forcing a test '' all, 'fs' fsockopen, 'ch' cURL
    24 
    25         // 1 - The simplest solution: file_get_contents
    26         // Contraint: php.ini
    27         //      ; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
    28         //      allow_url_fopen = On
    29                 if ( $mode == '' ) {
    30                   if ( true === (bool) ini_get('allow_url_fopen') ) {
    31                                 $value = file_get_contents($url);
    32                                 if ( $value !== false and substr($value,0,21) != '<!DOCTYPE HTML PUBLIC') {
    33                                         return $value;
    34                                 }
    35                         }
    36                 }
    37                 if ( $mode == '' ) $mode = 'fs';
    38                 if ( $pwg_mode == '' ) $pwg_mode = 'fs'; // Remind it
    39         // 2 - Often accepted access: fsockopen
    40                 if ($mode == 'fs') {
    41                         $fs = fsockopen($host, 80, $errno, $errstr, $timeout);
    42                         if ( $fs !== false ) {
    43                                 fwrite($fs, 'GET ' . $doc . " HTTP/1.1\r\n");
    44                                 fwrite($fs, 'Host: ' . $host . "\r\n");
    45                                 fwrite($fs, "Connection: Close\r\n\r\n");
    46                                 stream_set_blocking($fs, TRUE);
    47                                 stream_set_timeout($fs,$timeout); // Again the $timeout on the get
    48                                 $info = stream_get_meta_data($fs);
    49                                 $value = '';
    50                                 while ((!feof($fs)) && (!$info['timed_out'])) {
    51                                                                 $value .= fgets($fs, 4096);
    52                                                                 $info = stream_get_meta_data($fs);
    53                                                                 flush();
    54                                 }
    55                                 if ( $info['timed_out'] === false  and substr($value,0,21) != '<!DOCTYPE HTML PUBLIC') return $value;
    56                         }
    57                 }
    58 
    59                 if ( $pwg_mode == 'fs' ) $pwg_mode = 'ch'; // Remind it
    60         // 3 - Sometime another solution: curl_exec
    61         // See http://fr2.php.net/manual/en/curl.installation.php
    62           if (function_exists('curl_init') and $pwg_mode == 'ch') {
    63                         $ch = @curl_init();
    64                         @curl_setopt($ch, CURLOPT_URL, $url);
    65                         @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    66                         @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    67                         @curl_setopt($ch, CURLOPT_HEADER, 1);
    68                         @curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
    69                         @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    70                         @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    71                         $value = @curl_exec($ch);
    72                         $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    73                         $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
    74                         @curl_close($value);
    75                         if ($value !== false and $status >= 200 and $status < 400) {
    76                                 $value = substr($value, $header_length);
    77                                 // echo '<br/>-ch- ('. $value . ') <br/>';
    78                                 return $value;
    79                         }
    80                         else $pwg_mode = 'failed'; // Sorry but remind it as well
    81                 }
    82 
    83                 // No other solutions
    84                 return false;
    85         }
    86 }
    87 
    886
    897// Select the correct menu on loc_end_admin
     
    10018}
    10119
    102 function whois_country($trace, $bypass = false) {
     20function whois_country($trace, $bypass = false)
     21{
     22  global $conf;
     23 
    10324  if (!isset($trace['country'])) {
    10425                        pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . ' ADD `country` VARCHAR( 254 ) NOT NULL AFTER `lang` ;');
     
    10930        if (isset($c['Code']) and $c['Code']!='' and $c['Code']!='__') return $c;
    11031        if ($bypass and isset($c['Code'])) return $c;
    111     $result = pwg_get_contents ('http://api.hostip.info/get_html.php?ip=' . $trace['IP'], 'r');
    112         if ( $result !== false ) {
    113                 $tokens = preg_split("/[:]+/", $result);
    114                 $c = array ('Name' => $tokens[1], 'City' => substr($tokens[3],0,-3));
    115                 if (strpos ($c['Name'], '?') === FALSE) {
    116                         $c['Code'] = substr($c['Name'],-8,2); # " (Private Address) (XX) City"
    117                         $c['Name'] = ucwords ( strtolower( substr($c['Name'],0,-5)));
    118                 }
    119                 else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    120         }
    121         if (stripos($c['Name'], 'Squid')!==false or $c['Code'] =='XX')
    122                 $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    123         $new = htmlspecialchars(serialize($c),ENT_QUOTES,'UTF-8');
     32
     33  $c = array(
     34    'Code' => '__',
     35    'Name' => l10n('Unknown country'),
     36    'City' => 'N/A',
     37    );
     38
     39  if (!filter_var($trace['IP'], FILTER_VALIDATE_IP))
     40  {
     41    return $c;
     42  }
     43     
     44  if (isset($conf['whois_online_use_geoiplookup']) and $conf['whois_online_use_geoiplookup'])
     45  {
     46    $geoiplookup_output = exec('geoiplookup '.$trace['IP']);
     47
     48    if (!preg_match('/IP Address not found/', $geoiplookup_output))
     49    {
     50      list(,$geoiplookup_output) = explode(':', $geoiplookup_output);
     51      list($country_code, $country_name) = explode(',', $geoiplookup_output);
     52
     53      $c['Name'] = trim($country_name);
     54      $c['Code'] = trim($country_code);
     55    }
     56  }
     57  else
     58  {
     59    $url = 'http://freegeoip.net/json/' . $trace['IP'];
     60 
     61    if (fetchRemote($url, $result) and $geo_data = @json_decode($result, true))
     62    {
     63      // echo '<pre>'; print_r($geo_data); echo '</pre>';
     64      $c['Name'] = $geo_data['country_name'];
     65      $c['Code'] = $geo_data['country_code'];
     66    }
     67  }
     68
     69  $new = htmlspecialchars(serialize($c),ENT_QUOTES,'UTF-8');
     70 
    12471        if ($new == $trace['country']) return $c;
    125         pwg_query('UPDATE ' . WHOIS_ONLINE_TABLE . '
    126       SET `country` = \'' . $new . '\'
    127     WHERE `session_id` = \'' . $trace['session_id'] . '\';');
     72 
     73        pwg_query('
     74UPDATE ' . WHOIS_ONLINE_TABLE . '
     75  SET `country` = \'' . $new . '\'
     76  WHERE `session_id` = \'' . $trace['session_id'] . '\'
     77;');
     78 
    12879  return $c;
    12980}
Note: See TracChangeset for help on using the changeset viewer.