source: extensions/whois_online/include/wo_admin_functions.inc.php @ 6193

Last change on this file since 6193 was 6193, checked in by vdigital, 14 years ago

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 size: 6.2 KB
Line 
1<?php
2/* Functions */
3
4if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
5if (!defined('IN_ADMIN') or !IN_ADMIN) die('Hacking attempt!');
6
7
8if ( !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
88// Template function
89function Whois_most($text, $count, $when, $format) {
90 return sprintf(l10n($text),$count,date(l10n($format),$when));
91}
92
93function whois_country($trace, $bypass = false) {
94  if (!isset($trace['country'])) {
95                        pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . ' ADD `country` VARCHAR( 254 ) NOT NULL AFTER `lang` ;');
96                        $trace['country']='';
97        }
98        $c = array();
99        if ($trace['country']!='') $c = @unserialize(htmlspecialchars_decode($trace['country']));
100        if (isset($c['Code']) and $c['Code']!='' and $c['Code']!='__') return $c; 
101        if ($bypass and isset($c['Code'])) return $c;
102    $result = pwg_get_contents ('http://api.hostip.info/get_html.php?ip=' . $trace['IP'], 'r');
103        if ( $result !== false ) { 
104                $tokens = preg_split("/[:]+/", $result);
105                $c = array ('Name' => $tokens[1], 'City' => substr($tokens[3],0,-3));
106                if (strpos ($c['Name'], '?') === FALSE) {
107                        $c['Code'] = substr($c['Name'],-8,2); # " (Private Address) (XX) City"
108                        $c['Name'] = ucwords ( strtolower( substr($c['Name'],0,-5)));
109                }
110                else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
111        }
112        if (stripos($c['Name'], 'Squid')!==false or $c['Code'] =='XX') 
113                $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',);
114        $new = htmlspecialchars(serialize($c),ENT_QUOTES,'UTF-8');
115        if ($new == $trace['country']) return $c;
116        pwg_query('UPDATE ' . WHOIS_ONLINE_TABLE . '
117      SET `country` = \'' . $new . '\'
118    WHERE `session_id` = \'' . $trace['session_id'] . '\';');
119  return $c;
120}
121
122function whois_flag($trace, &$step, $limit = 10) {
123        $flag = WHOIS_ONLINE_PATH . 'flags/' . $trace['Country']['Code'] . '.jpg';
124        if (file_exists($flag) and  $trace['Country']['Code'] != '__' ) return $flag;
125        if ($trace['Country']['Code'] == '__' ) {
126            $flag = WHOIS_ONLINE_PATH . 'flags/' . substr($trace['lang'],-2, 2) . '.jpg';
127                if (file_exists($flag)) return $flag;
128                return WHOIS_ONLINE_PATH . 'flags/__.jpg';
129        }
130        if ( $step > $limit ) return WHOIS_ONLINE_PATH . 'flags/.jpg';
131        $f = fopen  ('http://api.hostip.info/flag.php?ip=' . $trace['IP'], 'r');
132        $result='';
133        while ($l = fgets ($f, 1024)) $result .= $l;
134        fclose ($f);
135        $f = fopen($flag,"w+");
136        fputs($f,$result);
137        fclose($f);
138        return $flag;
139}
140
141/*
142  returns (mixed): (string) 'bot agent name'  || (bool) false
143  @param (string) HTTP_USER_AGENT
144*/
145function is_a_bot($agent = '') 
146{
147  global $conf;
148        if ($agent == '') $agent = $_SERVER['HTTP_USER_AGENT'];
149        $botlist = array('Teoma', 'alexa', 'froogle', 'Gigabot', 'inktomi',
150        'looksmart', 'URL_Spider_SQL', 'Firefly', 'NationalDirectory', 
151        'Ask Jeeves', 'TECNOSEEK', 'InfoSeek', 'WebFindBot', 'girafabot',
152        'crawler', 'www.galaxy.com', 'Googlebot', 'Scooter', 'Slurp',
153        'msnbot', 'appie', 'FAST', 'WebBug', 'Spade', 'ZyBorg', 'rabaz',
154        'Baiduspider', 'Feedfetcher-Google', 'TechnoratiSnoop', 'Rankivabot',
155        'Mediapartners-Google', 'Sogou web spider', 'WebAlta Crawler');
156  if (isset($conf['search_agents']))
157          $botlist = array_merge( $botlist, array_diff( $conf['search_agents'], $botlist ) );
158  foreach($botlist as $bot) {
159    if (stripos($agent, $bot)!==false) return $bot; 
160  }
161  return false;
162} 
163?>
Note: See TracBrowser for help on using the repository browser.