Ignore:
Timestamp:
Apr 24, 2010, 10:18:27 PM (14 years ago)
Author:
vdigital
Message:

[ 2.0.m ]

Fix: IE8 User agent could be too long
Fix: Failure on localisation (a French internet provider blocks external get_files)
Fix: Loss of previous collected data


File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/whois_online/online.php

    r5916 r5954  
    44global $conf, $conf_whois, $prefixeTable;
    55include_once(WHOIS_ONLINE_PATH.'include/wo_functions.inc.php');
    6 /* Secure Config */
    7 if (!isset($conf_whois['Active'])) $conf_whois = whois_online_conf();
    8 
    9 function whois_online_conf()
    10 {
    11         global $conf;
    12         $default = array(
    13           'Active' => true,
    14           'Delete level' => 20,
    15           'Radar limit' => 25,
    16           'Webmasters' => 2, // Normal
    17           'Administrators' => 2,
    18           'Obsolete limit' => 20,
    19                 'Default display' => true,
    20           'Add to Plugins menu' => false,
    21           'Add icon to History' => true,
    22           'Keep data' => true,
    23           'Search id' => 0,
    24           'Users' => Array('max' => 0, 'When' => date('Y-m-d'), 'count' => 0),
    25         );
    26         $conf_whois = array_merge($default, unserialize($conf['Whois Online']));
    27         if ((!isset($conf_whois['Version'])) or $conf_whois['Version'] != WHOIS_ONLINE_VER
    28                 or $conf['Whois Online'] != serialize($conf_whois)) {
    29                 $conf_whois['Version'] = WHOIS_ONLINE_VER;
    30                 $conf['Whois Online'] = serialize($conf_whois);
    31                 pwg_query('REPLACE INTO ' . CONFIG_TABLE . " (param,value,comment)
    32           VALUES ('Whois Online','". $conf['Whois Online'] ."','Whois Online configuration');");
    33         }
    34         return $conf_whois;
    35 }
    366
    377define('ONLINE_LEVEL', (100+$conf_whois['Delete level'])/100);
     
    4919}
    5020
    51 if ( !function_exists('pwg_get_contents') ) {
    52         function pwg_get_contents($url, $mode='') {
    53 
    54                 global $pwg_mode, $pwg_prev_host;
    55                 $timeout = 5; // will be a parameter (only for the socket)
    56 
    57                 $host = (strtolower(substr($url,0,7)) == 'http://') ? substr($url,7) : $url;
    58                 $host = (strtolower(substr($host,0,8)) == 'https://') ? substr($host,8) : $host;
    59                 $doc = substr($host, strpos($host, '/'));
    60                 $host = substr($host, 0, strpos($host, '/'));
    61 
    62                 if ($pwg_prev_host != $host) $pwg_mode = ''; // What was possible with one website could be different with another
    63                 $pwg_prev_host = $host;
    64                 if (isset($pwg_mode)) $mode = $pwg_mode;
    65                 if ($mode == 'r') $mode = '';
    66                 // $mode = 'ch'; // Forcing a test '' all, 'fs' fsockopen, 'ch' cURL
    67 
    68         // 1 - The simplest solution: file_get_contents
    69         // Contraint: php.ini
    70         //      ; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
    71         //      allow_url_fopen = On
    72                 if ( $mode == '' ) {
    73                   if ( true === (bool) ini_get('allow_url_fopen') ) {
    74                                 $value = file_get_contents($url);
    75                                 if ( $value !== false and substr($value,0,21) != '<!DOCTYPE HTML PUBLIC') {
    76                                         return $value;
    77                                 }
    78                         }
    79                 }
    80                 if ( $mode == '' ) $mode = 'fs';
    81                 if ( $pwg_mode == '' ) $pwg_mode = 'fs'; // Remind it
    82         // 2 - Often accepted access: fsockopen
    83                 if ($mode == 'fs') {
    84                         $fs = fsockopen($host, 80, $errno, $errstr, $timeout);
    85                         if ( $fs !== false ) {
    86                                 fwrite($fs, 'GET ' . $doc . " HTTP/1.1\r\n");
    87                                 fwrite($fs, 'Host: ' . $host . "\r\n");
    88                                 fwrite($fs, "Connection: Close\r\n\r\n");
    89                                 stream_set_blocking($fs, TRUE);
    90                                 stream_set_timeout($fs,$timeout); // Again the $timeout on the get
    91                                 $info = stream_get_meta_data($fs);
    92                                 $value = '';
    93                                 while ((!feof($fs)) && (!$info['timed_out'])) {
    94                                                                 $value .= fgets($fs, 4096);
    95                                                                 $info = stream_get_meta_data($fs);
    96                                                                 flush();
    97                                 }
    98                                 if ( $info['timed_out'] === false  and substr($value,0,21) != '<!DOCTYPE HTML PUBLIC') return $value;
    99                         }
    100                 }
    101 
    102                 if ( $pwg_mode == 'fs' ) $pwg_mode = 'ch'; // Remind it
    103         // 3 - Sometime another solution: curl_exec
    104         // See http://fr2.php.net/manual/en/curl.installation.php
    105           if (function_exists('curl_init') and $pwg_mode == 'ch') {
    106                         $ch = @curl_init();
    107                         @curl_setopt($ch, CURLOPT_URL, $url);
    108                         @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    109                         @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    110                         @curl_setopt($ch, CURLOPT_HEADER, 1);
    111                         @curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
    112                         @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    113                         @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    114                         $value = @curl_exec($ch);
    115                         $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    116                         $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
    117                         @curl_close($value);
    118                         if ($value !== false and $status >= 200 and $status < 400) {
    119                                 $value = substr($value, $header_length);
    120                                 // echo '<br/>-ch- ('. $value . ') <br/>';
    121                                 return $value;
    122                         }
    123                         else $pwg_mode = 'failed'; // Sorry but remind it as well
    124                 }
    125 
    126                 // No other solutions
    127                 return false;
    128         }
    129 }
    130 // Assume the Default display on pages
    131 function whois_default_display() {
    132         global $template;
    133         $template->set_filenames(array( 'Whois_display' => dirname(__FILE__).'/default.tpl'));
    134         $template->pparse('Whois_display');
    135 }
    136 
    137 // Add admin links
    138 function whois_add_icon($menu) {
    139         global $conf_whois, $lang;
    140         if ($conf_whois['Add icon to History'])
    141                 $lang['History'] .= '</a> <a class="external" href="' . get_admin_plugin_menu_link(WHOIS_ONLINE_PATH.'config.php') . '">
    142                         <img class="button" src="' . WHOIS_ONLINE_PATH . 'icons/Whois_tuner.gif" alt="Whois Online configuration" title="Whois Online configuration" /></a>';
    143         if ($conf_whois['Add to Plugins menu']) array_push($menu, array(
    144                                 'NAME' => 'Whois Online',
    145                                 'URL' => get_admin_plugin_menu_link(WHOIS_ONLINE_PATH.'config.php'),
    146                         ));
    147         return $menu;
    148 }
    149 
    150 // Template function
    151 function Whois_most($text, $count, $when, $format) {
    152  return sprintf(l10n($text),$count,date(l10n($format),$when));
    153 }
    154 
    155 // New member
    156 function whois_online_register($who) {
    157         global $conf, $conf_whois;
    158         $conf_whois['Users']['count'] = $who['id'];
    159         $conf['Whois Online'] = serialize($conf_whois);
    160         pwg_query('REPLACE INTO ' . CONFIG_TABLE . " (param,value,comment)
    161   VALUES ('Whois Online','". $conf['Whois Online'] ."','Whois Online configuration');");
    162         return;
    163 }
    164 
    165 function whois_country($trace, $bypass = false) {
    166   if (!isset($trace['country'])) {
    167                         pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . ' ADD `country` VARCHAR( 255 ) NOT NULL AFTER `lang` ;');
    168                         $trace['country']='';
    169         }
    170         $c = array();
    171         if ($trace['country']!='') $c = @unserialize(htmlspecialchars_decode($trace['country']));
    172         if (isset($c['Code']) and $c['Code']!='' and $c['Code']!='__') return $c;
    173         if ($bypass and isset($c['Code']) and $c['Code']=='__') return $c;
    174     $result = pwg_get_contents ('http://api.hostip.info/get_html.php?ip=' . $trace['IP'], 'r');
    175         if ( $result !== false ) {
    176                 $tokens = preg_split("/[:]+/", $result);
    177                 $c = array ('Name' => $tokens[1], 'City' => substr($tokens[3],0,-3));
    178                 if (strpos ($c['Name'], '?') === FALSE) {
    179                         $tokens = preg_split ("/[\(\)]/", $c['Name']);
    180                         $c['Code'] = isset($tokens[1]) ? $tokens[1]:'__';
    181                         $c['Name'] = ucwords ( strtolower( substr($c['Name'],0,-5)));
    182                 }
    183                 else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    184         }
    185         else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    186         if ($c['Code'] == 'Private Address') {
    187                 $c['Name'] = l10n('Private Address');
    188                 $c['City'] = l10n('N/A');
    189                 $c['Code'] = '__';
    190         }
    191         $new = htmlspecialchars(serialize($c),ENT_QUOTES,'UTF-8');
    192         if ($new == $trace['country']) return $c;
    193         pwg_query('UPDATE ' . WHOIS_ONLINE_TABLE . '
    194       SET `country` = \'' . $new . '\'
    195     WHERE `session_id` = \'' . $trace['session_id'] . '\';');
    196   return $c;
    197 }
    198 
    199 function whois_flag($trace, &$step, $limit = 10) {
    200   $flag = WHOIS_ONLINE_PATH . 'flags/' . $trace['Country']['Code'] . '.jpg';
    201         if (file_exists($flag)) return $flag;
    202         if ( $step > $limit ) return WHOIS_ONLINE_PATH . 'flags/__.jpg';
    203         $f = fopen  ('http://api.hostip.info/flag.php?ip=' . $trace['IP'], 'r');
    204         $result='';
    205         while ($l = fgets ($f, 1024)) $result .= $l;
    206         fclose ($f);
    207                 $f = fopen($flag,"w+");
    208         fputs($f,$result);
    209         fclose($f);
    210   return $flag;
    211 }
    212 
    213 // Read all data
    214 function whois_online_get_data($order='') {
    215         $remove = "''"; // Nothing to remove ( = an empty session_id)
    216         $ctr = 0; $Online[0] = '';
    217         $result = pwg_query('SELECT * FROM ' . WHOIS_ONLINE_TABLE . $order . ';');
    218         while($row=mysql_fetch_array($result)) {
    219                 $row['delay'] = (int) time() - $row['last_access'];
    220                 $row['elm_ids'] = explode(' ', $row['last_elm_ids']);
    221                 $row['cat_ids'] = explode(' ', $row['last_cat_ids']);
    222                 $row['tag_ids'] = explode(' ', $row['last_tag_ids']);
    223                 $row['sch_ids'] = explode(' ', $row['last_sch_ids']);
    224                 $row['dates'] = explode(' ', $row['last_dates']);
    225                 $ctr++;
    226                 if ( $row['IP'] != 'global' and $row['permanent'] == 'false'
    227                         and $row['delay'] > (float)( 60 * 60 * 24 * 3 )) $remove .= ", '" . $row['session_id'] . "'";
    228                 elseif ($row['IP'] == 'global') $Online[0] = $row;
    229                 else $Online[] = $row;
    230         }
    231         // Out of 7 visits: Reduce registered visits for 3 days without access
    232         if ($remove != "''" and $ctr > (count($Online) * ONLINE_LEVEL)
    233                 and ($ctr-count($Online)) > ONLINE_LIMIT and ($Online[0]['pag_hits']%7)==0) {
    234                 pwg_query('DELETE FROM ' . WHOIS_ONLINE_TABLE . ' WHERE `session_id` IN ('. $remove .')
    235                                 AND `permanent` = \'false\' AND `IP` <> \'global\';');
    236                 if (($Online[0]['pag_hits']%13)==0) @pwg_query('OPTIMIZE TABLE ' . WHOIS_ONLINE_TABLE . ';');
    237         }
    238         return $Online;
    239 }
    240 
    241 // Update global and update/create current session_id
    242 function whois_online_update(&$global, &$dedicated, &$gtrace)
    243 {
    244         global $lang_info;
    245         // Rewrite the global record
    246         if ( $gtrace == 2 ) {
    247                 $query = 'REPLACE INTO ' . WHOIS_ONLINE_TABLE . ' (`IP`, `hidden_IP`, `session_id`,`user_id`,`username`,`lang`,
    248                 `permanent`,`last_access`,`last_elm_ids`, `last_cat_ids`, `last_tag_ids`, `last_sch_ids`,
    249                 `first_access_date`, `last_dates`, `elm_hits`, `pag_hits`)
    250                         VALUES (\'global\', \'true\',\'global\', 0, \''. $global['username'] .'\', \'--\', \'true\', \''
    251                         . time()  .'\',  \''
    252                         . implode(' ',$global['elm_ids']) . '\', \''
    253                         . implode(' ',$global['cat_ids']) . '\', \''
    254                         . implode(' ',$global['tag_ids']) . '\', \''
    255                         . implode(' ',$global['sch_ids']) . '\', \''
    256                         . $global['first_access_date'] . '\', \'\', \''
    257                         . $global['elm_hits'] . '\', \'' . $global['pag_hits'] . '\');';
    258                 pwg_query($query);
    259         }
    260         // Write or Rewrite the dedicated record
    261         $query = 'REPLACE INTO ' . WHOIS_ONLINE_TABLE . ' (`IP`, `hidden_IP`, `session_id`,`user_id`,`username`,`lang`, `user_agent`,
    262         `any_previous`, `same_previous`, `permanent`,`last_access`,`last_elm_ids`, `last_cat_ids`, `last_tag_ids`, `last_sch_ids`,
    263         `first_access_date`, `last_dates`, `elm_hits`, `pag_hits`)
    264                 VALUES (\''. $dedicated['IP'] .'\', \''
    265                 . $dedicated['hidden_IP'] .'\', \''. $dedicated['session_id'] .'\', \''
    266                 . $dedicated['user_id'] .'\', \''. $dedicated['username'] .'\', \''
    267                 . substr($lang_info['code'],0,2) .'\', \''
    268                 . $dedicated['user_agent'] .'\', \''
    269                 . $dedicated['any_previous'] .'\', \''
    270                 . $dedicated['same_previous'] .'\', \''
    271                 . $dedicated['permanent'] . '\', \''. time() .'\',  \''
    272                 . implode(' ',$dedicated['elm_ids']) . '\', \''
    273                 . implode(' ',$dedicated['cat_ids']) . '\', \''
    274                 . implode(' ',$dedicated['tag_ids']) . '\', \''
    275                 . implode(' ',$dedicated['sch_ids']) . '\', \''
    276                 . $dedicated['first_access_date'] . '\', \''
    277                 . implode(' ',$dedicated['dates']) . '\', \''
    278                 . $dedicated['elm_hits'] . '\', \''
    279                 . $dedicated['pag_hits'] . '\');';
    280         pwg_query($query);
    281 }
    282 
    283 // Data tracking
    284 // Parameters:
    285 //  - Array of Ids
    286 //  - New ID
    287 //  - Array of dates
    288 // => Add the ID if needed, Add Today if needed
    289 function whois_online_track(&$key, $id, &$date) {
    290         if ($id != '') array_unshift($key, $id);
    291         $key = array_unique($key);
    292         if (count($key)>10) array_pop($key);
    293         array_unshift($date, date('Y-m-d'));
    294         $date = array_unique($date);
    295         if (count($date)>5) array_pop($date);
    296         return;
    297 }
    298 
    299 // Antiaspi delay conversion in seconds
    300 // delay in "HH:ii:ss" or "d :HH:ii:ss"
    301 // return delay in seconds
    302 function whois_online_duration($date_string)
    303 {
    304  list($s, $i, $H, $d, $more) =
    305    array_merge(array_reverse(
    306            explode(" ",str_ireplace(':',' ', $date_string))),
    307                  array(0,0,0,0,0));
    308  $t = time();
    309  return strtotime(sprintf("+%s days %s hours %s minutes %s seconds",
    310    $d, $H, $i, $s), $t) - $t;
    311 }
    31221
    31322/*
     
    523232        $ctr_same = count(explode(' ',$visit['same_previous']));
    524233       
    525         $visit['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
     234        $visit['user_agent'] = substr($_SERVER['HTTP_USER_AGENT'],0,160);
    526235        $Vip =& $visit['IP'];
    527236        $visit['Allowed_SE'] = false;
Note: See TracChangeset for help on using the changeset viewer.