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/include/wo_functions.inc.php

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