Ignore:
Timestamp:
May 16, 2010, 3:45:47 PM (14 years ago)
Author:
vdigital
Message:

New: Piwigo 2.1 support
Fix: Flags could be based on Languages if api calls are forbidden by a provider.
Part of a code review.

File:
1 edited

Legend:

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

    r6172 r6193  
    44/* Secure Config */
    55if (!isset($conf['Whois Online']) or !isset($conf_whois['Active'])) $conf_whois = whois_online_conf();
     6
     7if (defined('IN_ADMIN') and IN_ADMIN) include_once(WHOIS_ONLINE_PATH.'include/wo_admin_functions.inc.php');
    68
    79function whois_online_conf()
     
    3436}
    3537
    36 
    37 if ( !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 }
    11638// Assume the Default display on pages
    11739function whois_default_display() {
     
    11941        $template->set_filenames(array( 'Whois_display' => dirname(__FILE__).'/../default.tpl'));
    12042        $template->pparse('Whois_display');
    121 }
    122 
    123 // Add admin links
    124 function 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
    137 function Whois_most($text, $count, $when, $format) {
    138  return sprintf(l10n($text),$count,date(l10n($format),$when));
    13943}
    14044
     
    14751  VALUES ('Whois Online','". $conf['Whois Online'] ."','Whois Online configuration');");
    14852        return;
    149 }
    150 
    151 function whois_country($trace, $bypass = false) {
    152   if (!isset($trace['country'])) {
    153                         pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . ' ADD `country` VARCHAR( 254 ) 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'])) 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                         $c['Code'] = substr($c['Name'],-8,2); # " (Private Address) (XX) City"
    166                         $c['Name'] = ucwords ( strtolower( substr($c['Name'],0,-5)));
    167                 }
    168                 else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    169         }
    170         if (stripos($c['Name'], 'Squid')!==false) $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    171         // else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
    172         if ($c['Code'] == 'Private Address') {
    173                 $c['Name'] = l10n('Private Address');
    174                 $c['City'] = l10n('N/A');
    175                 $c['Code'] = '__';
    176         }
    177         $new = htmlspecialchars(serialize($c),ENT_QUOTES,'UTF-8');
    178         if ($new == $trace['country']) return $c;
    179         pwg_query('UPDATE ' . WHOIS_ONLINE_TABLE . '
    180       SET `country` = \'' . $new . '\'
    181     WHERE `session_id` = \'' . $trace['session_id'] . '\';');
    182   return $c;
    183 }
    184 
    185 function whois_flag($trace, &$step, $limit = 10) {
    186   $flag = WHOIS_ONLINE_PATH . 'flags/' . $trace['Country']['Code'] . '.jpg';
    187         if (file_exists($flag) and $flag != '__' ) return $flag;
    188         if ( $step > $limit ) return WHOIS_ONLINE_PATH . 'flags/__.jpg';
    189         $f = fopen  ('http://api.hostip.info/flag.php?ip=' . $trace['IP'], 'r');
    190         $result='';
    191         while ($l = fgets ($f, 1024)) $result .= $l;
    192         fclose ($f);
    193                 $f = fopen($flag,"w+");
    194         fputs($f,$result);
    195         fclose($f);
    196   return $flag;
    19753}
    19854
     
    245101        }
    246102        // Write or Rewrite the dedicated record
     103        $ctry = '_' . strtoupper( substr(@$_SERVER["HTTP_ACCEPT_LANGUAGE"],6,2) );
    247104        $query = 'REPLACE INTO ' . WHOIS_ONLINE_TABLE . ' (`IP`, `hidden_IP`, `session_id`,`user_id`,`username`,`lang`, `user_agent`,
    248105        `any_previous`, `same_previous`, `permanent`,`last_access`,`last_elm_ids`, `last_cat_ids`, `last_tag_ids`, `last_sch_ids`,
     
    251108                . $dedicated['hidden_IP'] .'\', \''. $dedicated['session_id'] .'\', \''
    252109                . $dedicated['user_id'] .'\', \''. $dedicated['username'] .'\', \''
    253                 . substr($lang_info['code'],0,2) .'\', \''
     110                . substr($lang_info['code'],0,2). $ctry .'\', \''
    254111                . $dedicated['user_agent'] .'\', \''
    255112                . $dedicated['any_previous'] .'\', \''
     
    297154}
    298155
    299 /*
    300   returns (mixed): (string) 'bot agent name'  || (bool) false
    301   @param (string) HTTP_USER_AGENT
    302 */
    303 function is_a_bot($agent = '')
    304 {
    305   global $conf;
    306         if ($agent == '') $agent = $_SERVER['HTTP_USER_AGENT'];
    307         $botlist = array('Teoma', 'alexa', 'froogle', 'Gigabot', 'inktomi',
    308         'looksmart', 'URL_Spider_SQL', 'Firefly', 'NationalDirectory',
    309         'Ask Jeeves', 'TECNOSEEK', 'InfoSeek', 'WebFindBot', 'girafabot',
    310         'crawler', 'www.galaxy.com', 'Googlebot', 'Scooter', 'Slurp',
    311         'msnbot', 'appie', 'FAST', 'WebBug', 'Spade', 'ZyBorg', 'rabaz',
    312         'Baiduspider', 'Feedfetcher-Google', 'TechnoratiSnoop', 'Rankivabot',
    313         'Mediapartners-Google', 'Sogou web spider', 'WebAlta Crawler');
    314   if (isset($conf['search_agents']))
    315           $botlist = array_merge( $botlist, array_diff( $conf['search_agents'], $botlist ) );
    316   foreach($botlist as $bot) {
    317     if (stripos($agent, $bot)!==false) return $bot;
    318   }
    319   return false;
    320 }
     156// Add admin links
     157function whois_add_icon($menu) {
     158        global $conf_whois, $lang;
     159        if ($conf_whois['Add icon to History'])
     160                $lang['History'] .= '</a> <a class="external" href="' . get_admin_plugin_menu_link(WHOIS_ONLINE_PATH.'config.php') . '">
     161                        <img class="button" src="' . WHOIS_ONLINE_PATH . 'icons/Whois_tuner.gif" alt="Whois Online configuration" title="Whois Online configuration" /></a>';
     162        if ($conf_whois['Add to Plugins menu']) array_push($menu, array(
     163                                'NAME' => 'Whois Online',
     164                                'URL' => get_admin_plugin_menu_link(WHOIS_ONLINE_PATH.'config.php'),
     165                        ));
     166        return $menu;
     167}
     168
    321169?>
Note: See TracChangeset for help on using the changeset viewer.