-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.php
478 lines (373 loc) · 12.8 KB
/
counter.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
class Counter
{
function count($article, $tag = "")
{
global $editor;
# räknar inte mig när jag är inloggad
if($editor->IsLoggedIn())
return;
$time = time();
$ip = $_SERVER["REMOTE_ADDR"];
$agent = mysql_escape_string($_SERVER["HTTP_USER_AGENT"]);
$referrer = mysql_escape_string($_SERVER["HTTP_REFERER"]);
$article = mysql_escape_string($article);
$tag = mysql_escape_string($tag);
query("INSERT INTO log (ip,time,article,tag,referrer,agent) VALUES ('$ip',$time,'$article','$tag','$referrer','$agent')");
}
}
function dumbfilter($a)
{
return ($a != "1");
}
class CounterStatistics
{
var $log;
var $data;
var $human_agents;
var $robot_agents_with_mozilla;
var $robot_agents;
var $os;
var $browser;
function CounterStatistics($time_begin = 0, $time_end = null)
{
$this->log = array();
if($time_end)
$result = query("SELECT * FROM log WHERE time >= $time_begin AND time < $time_end ORDER BY time");
else
$result = query("SELECT * FROM log ORDER BY time");
while($row = fetch_array($result))
array_push($this->log, $row);
$this->human_agents = array(
"Mozilla", "MSIE", "Opera", "Netscape", "Links", "Dillo", "EPOC32", "UPG1", "curl",
"amaya", "Mosaic", "IBrowse", "Lynx", "iCab", "DocZilla", "ELinks", "ICE Browser",
"OmniWeb", "Bison", "W3CLineMode", "w3m", "Xiino", "BlackBerry", "Nokia", "AU-MIC",
"SHARP", "SIE-", "LG-", "ReqwirelessWeb", "SonyEricsson", "Slimbrowser", "Enigma Browser",
"Netsurf", "T-Online Browser", "LG", "SAGEM", "SHARP", "DoCoMo",
);
$this->robot_agents_with_mozilla = array(
"Ask Jeeves", "FAST", "Firefly", "Googlebot", "grub-client", "Yahoo! Slurp", "MSIECrawler",
"ZyBorg", "kulturarw3", "heritrix", "Zyte", "HTTrack", "SpeedySpider", "Cerberian Drtrs",
"GPU p2p crawler", "HallBot", "Larbin",
);
$this->robot_agents = array_merge($this->robot_agents_with_mozilla,
array("Bloglines", "FeedOnFeeds", "NetNewsWire", "PHP", "Java", "msnbot", "Filangy",
"ccubee", "aipbot", "yacy", "OmniExplorer", "RssBandit", "MagpieRSS",
"Liferea", "Wget", "updated.com", "LWP::Simple", "lmspider"));
$this->os = array(
"Windows", "Linux", "Macintosh", "FreeBSD", "OpenBSD", "AmigaOS", "SunOS", "OS/2", "BeOS",
"Nokia", "SonyEricsson", "LG-", "SIE-",
);
# måste kontrollera i rätt ordning eftersom vissa webbläsare har flera id-strängar
$this->browser = array("Opera", "MSIE 3", "MSIE 4", "MSIE 5", "MSIE 6", "MSIE 7", "Safari", "KHTML", "Firefox",
"Thunderbird", "Netscape", "Gecko", "Lynx", "Links", "ELinks", "w3m", "W3CLineMode", "Google WAP Proxy",
"Konqueror", "NAVIO", "AvantGo");
$this->feedreader = array("Google Desktop", "Liferea", "Bloglines", "Sage", "Firefox");
$this->data = array();
$this->analyse();
}
function analyse()
{
$ip_humans = array();
$ip_spiders = array();
foreach($this->log as $entry)
{
if($this->is_probably_human($entry["agent"]))
{
$this->data["requests"][$entry["tag"]."/".$entry["article"]]++;
$this->data["humans"]++;
if($entry["tag"] == "post")
$this->data["posts"][$entry["article"]]++;
if($this->is_unique(&$ip_humans, $entry["ip"], $entry["time"]))
{
if(string_endswith($entry["tag"], "rss"))
{
# räknar alla åtkomster till flödena separat
$this->data["unique_feedreaders"]++;
$this->data["feedreaders"][$this->get_feedreader($entry["agent"])]++;
}
else
{
$this->data["unique_humans"]++;
$this->data["oses"][$this->get_os($entry["agent"])]++;
$this->data["browsers"][$this->get_browser($entry["agent"])]++;
# $this->data["referrer_full"][$entry["referrer"]]++;
$this->data["days"][mb_convert_encoding(strftime("%A %e %B", $entry["time"]), "UTF-8", "ISO-8859-1")]++;
}
$this->data["tags"][$entry["tag"]]++;
$server = get_server_from_url($entry["referrer"]);
# alla google-domäner klumpas ihop till en
if(string_startswith($server, "google."))
$server = "google.*";
# alla statcounter-subdomäner klumpas ihop till en
if(string_endswith($server, ".statcounter.com"))
$server = "*.statcounter.com";
$this->data["referrer_sites"][$server]++;
$this->interpret_referrer($entry["referrer"]);
}
if($entry["tag"] == "search")
{
$query = mb_eregi_replace("[^a-zåäö0-9]", " ", mb_strtolower($entry["article"]));
$query = preg_replace("/\s+/", " ", trim($query));
$this->data["search_internal"][$query]++;
}
}
else
{
$this->data["robots"]++;
if($this->is_unique(&$ip_spiders, $entry["ip"], $entry["time"]))
{
if(string_endswith($entry["tag"], "rss"))
{
# räknar alla åtkomster till flödena separat
$this->data["unique_feedreaders"]++;
$this->data["feedreaders"][$this->get_feedreader($entry["agent"])]++;
}
else
{
$this->data["unique_robots"]++;
$this->data["robot_agents"][$this->get_robot($entry["agent"])]++;
}
}
}
}
}
function interpret_referrer($referrer)
{
$refpatterns = array(
'/.*google(\.[a-z]{2,3})+.*(\?|&)q=([^&]+).*/i'=> array('Google', 3),
'/lycossvar\.spray\.se.*(\?|&)query=([^&]+).*/i'=> array('Spray', 2),
'/(www\.)?eniro\.[se|no].*(\?|&)q=([^&]+).*/i'=> array('Eniro', 3),
'/search\.msn(\.[a-z]{2,3})+.*(\?|&)q=([^&]+).*/i'=> array('MSN', 3),
'/(se\.)?search\.yahoo\.com.*(\?|&)p=([^&]+).*/i'=>array('Yahoo', 3),
'/([a-z]{2-3}\.)?altavista.com.*(\?|&)q=([^&]+).*/i' => array('AltaVista', 3),
'/alltheweb\\.com.*(\?|&)q=([^&]+).*/i'=>array('AllTheWeb', 2),
'/web\.ask\.com.*(\?|&)ask=([^&]+).*/i'=>array('Ask Jeeves', 2),
'/aolsearch\.aol(\\.[a-z]{2,3})+.*(\?|&)query=([^&]+).*/i'=>array('AOL', 3),
'/www\.dogpile\.com.*\/([^\/]+)/i'=>array('Dogpile', 1),
'/www\\.mywebsearch\\.com.*(\\?|&)searchfor=(.+?)(&|$).*/i'=>array('MyWay', 2),
'/search\\.wanadoo(\\.[a-z]{2,3})+.*(\\?|&)q=(.+?)(&|$).*/i'=>array('Wanadoo', 3),
'/.*a9\.com\/([^&?]+).*/i'=>array('A9', 1),
);
if(!isset($this->data["searches"]))
$this->data["searches"] = array();
if(!isset($this->data["search_queries"]))
$this->data["search_queries"] = array();
global $stoplist;
foreach($refpatterns as $keng => $eng)
if(preg_match($keng, $referrer, $matches))
{
$query = urldecode($matches[$eng[1]]);
$query = mb_strtolower(mb_convert_encoding($query, "UTF-8", mb_detect_encoding($query, "UTF-8, iso-8859-1")));
$query = str_replace(array(" and ", " or "), " ", $query);
array_push($this->data["searches"], "$eng[0]: $query");
$query = mb_eregi_replace("[^a-zåäöìíïéèëñàáü0-9]", " ", $query);
$query = preg_replace("/\s+/", " ", trim($query));
$this->data["search_queries"][$query]++;
break;
}
}
function is_probably_human($agent)
{
foreach($this->human_agents as $human)
if(strpos($agent, $human) !== false)
{
if($human == "Mozilla" && $this->is_robot_disguised_as_mozilla($agent))
return false;
else
return true;
}
return false;
}
function is_robot_disguised_as_mozilla($agent)
{
foreach($this->robot_agents_with_mozilla as $robot)
if(strpos($agent, $robot) !== false)
return true;
return false;
}
function get_os($agent)
{
foreach($this->os as $os)
if(strpos($agent, $os) !== false)
return $os;
if(strpos($agent, "Win") !== false)
return "Windows";
elseif(strpos($agent, "Mac") !== false)
return "Macintosh";
return $agent;
}
function get_robot($agent)
{
foreach($this->robot_agents as $robot)
if(strpos($agent, $robot) !== false)
return $robot;
return $agent;
}
function get_browser($agent)
{
foreach($this->browser as $browser)
if(strpos($agent, $browser) !== false)
return $browser;
return $agent;
}
function get_feedreader($agent)
{
foreach($this->feedreader as $browser)
if(strpos($agent, $browser) !== false)
return $browser;
return $agent;
}
function is_unique(&$table, $ip, $timestamp)
{
$unique = false;
if(!isset($table[$ip]) or $table[$ip] + 1800 < $timestamp)
$unique = true;
$table[$ip] = $timestamp;
return $unique;
}
function generate_list($list, $count, $urlprefix = "")
{
arsort($list);
$i = 0;
#$total = array_sum($list);
$html = "<table>";
foreach($list as $id => $hits)
{
if($id == "") $id = "-";
$id = tidy_up_text($id);
#$percent = round(100 * $hits / $total, 1);
if($urlprefix !== "")
$html .= "<tr><td>$hits</td><td><em><a href='$urlprefix$id'>$id</a></em></td></tr>";
else
$html .= "<tr><td>$hits</td><td><em>$id</em></td></tr>";
$i++;
if($i == $count) break;
}
$html .= "</table>";
return $html;
}
function merge_list($list)
{
$list = array_filter($list, "dumbfilter");
arsort($list);
$already = array();
$xlist = array();
$queries = array_keys($list);
for($i = 0; $i < count($queries); $i++)
{
$queryi = $queries[$i];
$query = $queryi;
$count = (int)$list[$query];
if($already[$query])
continue;
for($j = $i + 1; $j < count($queries); $j++)
{
$queryj = $queries[$j];
if(levenshtein($queryi, $queryj) <= 2)
{
$count += (int)$list[$queryj];
$query .= ":$queryj";
$already[$queryj] = true;
}
}
$xlist[$query] = $count;
}
arsort($xlist);
return $xlist;
}
function generate_xlist($list, $count)
{
$list = $this->merge_list($list);
$i = 0;
$html = "<table>";
foreach($list as $id => $hits)
{
$id = tidy_up_text($id);
$id = str_replace(":", ", ", $id);
$html .= "<tr><td>$hits</td><td><em>$id</em></td></tr>";
$i++;
if($i == $count) break;
}
$html .= "</table>";
return $html;
}
function generate_simple_list($list, $count)
{
$i = 0;
$html = "<ol>";
foreach($list as $id)
{
if($id == "") continue;
$id = tidy_up_text($id);
$html .= "<li>$id</li>";
$i++;
if($i == $count) break;
}
$html .= "</ol>";
return $html;
}
function generate_post_list($list, $count)
{
arsort($list);
$i = 0;
$html = "<ol>";
foreach($list as $id => $hits)
{
if($id == "") continue;
$post = get_post($id);
$title = tidy_up_text($post["title"]);
$html .= "<li><a href='/blogwalk/post/$id'>$title</a></li>";
$i++;
if($i == $count) break;
}
$html .= "</ol>";
return $html;
}
function generate_statistics()
{
global $self;
$output = "<style type='text/css'>td,th { text-align: left; }</style>
<h2>Räknarstatistik</h2>";
$cs = new CounterStatistics(time() - 24 * 3600, time());
$humanslast24h = number_to_string($cs->data["unique_humans"]);
$humans = number_to_string($this->data["unique_humans"]);
$spiders = number_to_string($this->data["unique_robots"]);
$feedreaders = number_to_string($this->data["unique_feedreaders"]);
$hits = number_to_string($this->data["humans"]);
$spiderhits = number_to_string($this->data["robots"]);
$uptime = (time() - $this->log[0]["time"]) / 3600 / 24;
$humansperday = number_to_string($this->data["unique_humans"] / $uptime);
$hitspervisit = number_to_string($this->data["humans"] / $this->data["unique_humans"], 1);
$categories = array("main", "locations", "location", "blogurl", "blog", "about", "post", "search", "contact");
$output .= "<p><strong>{$this->data[unique_humans]}</strong> besök (<strong>$humansperday</strong> per dygn, men <strong>$humanslast24h</strong> det senaste dygnet). "
."Totalt $hits sidvisningar, eller $hitspervisit per besök.</p>"
."<p><strong>{$this->data[unique_robots]}</strong> unika bots/spindlar. Totalt $spiderhits sidvisningar.</p>";
$output .= "<h3>Dagar med flest unika besök</h3>"
.$this->generate_list($this->data["days"], 10);
$output .= "<h3>De vanligaste referenserna</h3>"
.$this->generate_list($this->data["referrer_sites"], 40);
$output .= "<h3>De mest besökta inläggen (ej unika träffar)</h3>"
.$this->generate_post_list($this->data["posts"], 15);
$output .= "<h3>De vanligaste sidorna (ej unika träffar)</h3>"
.$this->generate_list($this->data["requests"], 20);
$output .= "<h3>De vanligaste sökfraserna</h3>"
.$this->generate_xlist($this->data["search_queries"], 15);
$output .= "<h3>De vanligaste sidtyperna för 'ingång'</h3>"
.$this->generate_list($this->data["tags"], 20);
$output .= "<h3>De vanligaste robotarna</h3>"
.$this->generate_list($this->data["robot_agents"], 10);
$output .= "<h3>De vanligaste operativsystemen</h3>"
.$this->generate_list($this->data["oses"], 20);
$output .= "<h3>De vanligaste webbläsarna</h3>"
.$this->generate_list($this->data["browsers"], 20);
$output .= "<h3>De vanligaste flödesläsarna</h3>"
.$this->generate_list($this->data["feedreaders"], 20);
$output .= "<h3>De vanligaste interna sökfraserna</h3>"
.$this->generate_list($this->data["search_internal"], 10);
$output .= "<h3>De senaste sökningarna</h3>"
.$this->generate_simple_list(array_reverse($this->data["searches"]), 60);
return $output;
}
}
?>