-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqa-online-user-widget.php
164 lines (121 loc) · 4.2 KB
/
qa-online-user-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
class qa_online_user_widget {
function allow_template($template)
{
return true;
}
function allow_region($region)
{
return true;
}
function get_tag($tag,$xml)
{
preg_match_all('/<'.$tag.'>(.*)<\/'.$tag.'>$/imU',$xml,$match);
return $match[1];
}
function is_bot()
{
/* This function will check whether the visitor is a search engine robot */
$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
"Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
"Butterfly","Twitturls","Me.dium","Twiceler");
foreach($botlist as $bot)
{
if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
return true; // Is a bot
}
return false; // Not a bot
}
function count_online()
{
if($this->is_bot()) die();
$stringIp = $_SERVER['REMOTE_ADDR'];
$intIp = ip2long($stringIp);
// Checking wheter the visitor is already marked as being online:
$inDB = qa_db_query_sub('SELECT 1 FROM ^who_is_online WHERE ip='.$intIp.'');
if(!mysql_num_rows($inDB))
{
// This user is not in the database, so we must fetch
// the geoip data and insert it into the online table:
if(!isset($_COOKIE['geoData']))
{
// Making an API call to Hostip:
$xml = file_get_contents('http://api.hostip.info/?ip='.$stringIp);
$city = $this->get_tag('gml:name',$xml);
$city = $city[1];
$countryName = $this->get_tag('countryName',$xml);
$countryName = $countryName[0];
$countryAbbrev = $this->get_tag('countryAbbrev',$xml);
$countryAbbrev = $countryAbbrev[0];
// Setting a cookie with the data, which is set to expire in a month:
setcookie('geoData',$city.'|'.$countryName.'|'.$countryAbbrev, time()+60*60*24*30,'/');
}
//else($_COOKIE['geoData'])
else
{
// A "geoData" cookie has been previously set by the script, so we will use it
// Always escape any user input, including cookies:
list($city,$countryName,$countryAbbrev) = explode('|',mysql_real_escape_string(strip_tags($_COOKIE['geoData'])));
}
$countryName = str_replace('(Unknown Country?)','UNKNOWN',$countryName);
// In case the Hostip API fails:
if (!$countryName)
{
$countryName='UNKNOWN';
$countryAbbrev='XX';
$city='(Unknown City?)';
}
qa_db_query_sub('INSERT INTO ^who_is_online (ip,city,country,country_code) VALUES(#,$,$,$)',$intIp,$city,$countryName,$countryAbbrev);
}
else
{
// If the visitor is already online, just update the dt value of the row:
qa_db_query_sub('UPDATE qa_who_is_online SET dt=NOW() WHERE ip='.$intIp.'');
}
// Removing entries not updated in the last 10 minutes:
qa_db_query_sub('DELETE FROM qa_who_is_online WHERE dt<SUBTIME(NOW(),"0 0:10:0") ');
// Counting all the online visitors:
list($totalOnline) = mysql_fetch_array(qa_db_query_sub('SELECT COUNT(*) FROM ^who_is_online'));
// Outputting the number as plain text:
echo $totalOnline;
}
function geodata()
{
if($this->is_bot()) die();
// Selecting the top 15 countries with the most visitors:
$result =
qa_db_query_sub(' SELECT country_code,country, COUNT(*) AS total
FROM qa_who_is_online
GROUP BY country_code
ORDER BY total DESC
LIMIT 15');
while($row=mysql_fetch_assoc($result))
{
echo '
<div class="geoRow">
<div class="flag"><img src="'.QA_ONLINE_USERS_DIR.'who-is-online/img/famfamfam-countryflags/'.strtolower($row['country_code']).'.gif" width="16" height="11" /></div>
<div class="country" title="'.htmlspecialchars($row['country']).'">'.$row['country'].'</div>
<div class="people">'.$row['total'].'</div>
</div>
';
}
}
function output_widget($region, $place, $themeobject, $template, $request, $qa_content)
{
?>
<div class="onlineWidget">
<div class="panel"><?php $this->geodata()?> </div>
<div class="count"><?php $this->count_online()?></div>
<div class="label">online</div>
<div class="arrow"></div>
</div><?php
}
};
/*
Omit PHP closing tag to help avoid accidental output
*/