Changeset 5810


Ignore:
Timestamp:
Apr 12, 2010, 12:00:47 AM (14 years ago)
Author:
vdigital
Message:

Fixes:

MySQL 4.1 support
Alternate reading solutions: file_get_contents / fsockopen / curl
Replace of deprecated functions as of PHP 5.3.0

Location:
extensions/whois_online
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • extensions/whois_online/Changelog.txt.php

    r3695 r5810  
    33Plugin Name: Whois online
    44** History **
     5  2010-04-11 2.0.k
     6                                                 MySQL 4.1 support
     7                                                 Alternate reading solutions: file_get_contents / fsockopen / curl
     8                                                 Replace of deprecated functions as of PHP 5.3.0
    59  2009-07-28 2.0.j
    610                                                 Minor changes in Config management
  • extensions/whois_online/main.inc.php

    r3695 r5810  
    2323/*
    2424Plugin Name: Whois online
    25 Version: 2.0.j
     25Version: 2.0.k
    2626Description: Who is online?
    2727Plugin URI: http://fr.piwigo.org/ext/extension_view.php?eid=279
     
    2929Author URI: http://www.vdigital.org
    3030*/
    31 define('WHOIS_ONLINE_VER', '2.0.j');
     31define('WHOIS_ONLINE_VER', '2.0.k');
    3232global $prefixeTable, $conf;
    3333// $conf['debug_l10n'] = true;
  • extensions/whois_online/maintain.inc.php

    r3695 r5810  
    1616  `username` varchar(100) character set utf8 collate utf8_bin NOT NULL DEFAULT '',
    1717  `lang` char(2) character set utf8 collate utf8_bin NOT NULL DEFAULT 'en',
    18   `country` VARCHAR( 256 ) NOT NULL DEFAULT '',
     18  `country` VARCHAR( 255 ) NOT NULL DEFAULT '',
    1919  `user_agent` VARCHAR( 160 ) NOT NULL DEFAULT '',
    20   `any_previous` VARCHAR( 256 ) NOT NULL DEFAULT '',
    21   `same_previous` VARCHAR( 256 ) NOT NULL DEFAULT '',
     20  `any_previous` VARCHAR( 255 ) NOT NULL DEFAULT '',
     21  `same_previous` VARCHAR( 255 ) NOT NULL DEFAULT '',
    2222  `permanent` enum('true','false') NOT NULL DEFAULT 'false',
    2323  `last_access` varchar(12) NOT NULL DEFAULT '',
  • extensions/whois_online/online.php

    r3695 r5810  
    4949}
    5050
    51 
    52 
    53 
     51if ( !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}
    54130// Assume the Default display on pages
    55131function whois_default_display() {
     
    89165function whois_country($trace, $bypass = false) {
    90166  if (!isset($trace['country'])) {
    91                         pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . ' ADD `country` VARCHAR( 256 ) NOT NULL AFTER `lang` ;');
     167                        pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . ' ADD `country` VARCHAR( 255 ) NOT NULL AFTER `lang` ;');
    92168                        $trace['country']='';
    93169        }
     
    96172        if (isset($c['Code']) and $c['Code']!='' and $c['Code']!='__') return $c;
    97173        if ($bypass and isset($c['Code']) and $c['Code']=='__') return $c;
    98   $f = @fopen ('http://api.hostip.info/get_html.php?ip=' . $trace['IP'], 'r');
    99         if ($f === FALSE) return Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    100         $result='';
    101         while ($l = fgets ($f, 1024)) $result .= $l;
    102         fclose ($f);
    103         $tokens = split ("[:\n]", $result);
    104         $c = array ('Name' => $tokens[1], 'City' => $tokens[3]);
    105         if (strpos ($c['Name'], '?') === FALSE) {
    106                 $tokens = split ("[\(\)]", $c['Name']);
    107                 $c['Code'] = isset($tokens[1]) ? $tokens[1]:'__';
    108                 $c['Name'] = ucwords ( strtolower( ereg_replace
    109                         (' \([^\)]+)', '', $c['Name'])));
     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                }
    110183        }
    111184        else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
     
    127200        if (file_exists($flag)) return $flag;
    128201        if ( $step > $limit ) return WHOIS_ONLINE_PATH . 'flags/__.jpg';
    129         $f = fopen ('http://api.hostip.info/flag.php?ip=' . $trace['IP'], 'r');
    130         $result='';
    131         while ($l = fgets ($f, 1024)) $result .= $l;
    132         fclose ($f);
    133         $f = fopen($flag,"w+");
     202        $f = fopen ('http://api.hostip.info/flag.php?ip=' . $trace['IP'], 'r');
     203        $result='';
     204        while ($l = fgets ($f, 1024)) $result .= $l;
     205        fclose ($f);
     206                $f = fopen($flag,"w+");
    134207        fputs($f,$result);
    135208        fclose($f);
     
    385458  if (!isset($global['any_previous'])) {
    386459                pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE .
    387                         ' ADD `same_previous` VARCHAR( 256 ) NOT NULL DEFAULT \'\'  AFTER `country`;');
     460                        ' ADD `same_previous` VARCHAR( 255 ) NOT NULL DEFAULT \'\'  AFTER `country`;');
    388461                pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE .
    389                         ' ADD `any_previous` VARCHAR( 256 ) NOT NULL DEFAULT \'\'  AFTER `country`;');
     462                        ' ADD `any_previous` VARCHAR( 255 ) NOT NULL DEFAULT \'\'  AFTER `country`;');
    390463                pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE .
    391464                        ' ADD `user_agent` VARCHAR( 160 ) NOT NULL DEFAULT \'\'  AFTER `country`;');
Note: See TracChangeset for help on using the changeset viewer.