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

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

New: Piwigo 2.1 support
Fix: Non active menu => Icon is active
Fix: Active the right menu (Tools or Plugins) depending on the default access.
Change: CSS review.
Bug bypass on language.
Part of a code review.

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