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
|
<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
global $UTIL_DIR;
include_once($UTIL_DIR . "/convert.php");
class SiteStats {
public $statsdir;
public $stats = array();
private $referers = array();
private $pages = array();
private $visitors = array();
// Admin config
public $admin_title = "Site Stats";
public $admin_submodules = array("Overview" => "overview");
public function admin_overview($action, $vars)
{
switch($action) {
default:
$this->readSingle(time());
break;
}
}
public function admin($sub, $action, $vars)
{
switch($sub) {
case "overview":
$this->admin_overview($action, $vars);
$hits = 0;
foreach($this->visitors as $k => $v) {
$hits += $v;
}
echo "<p>".$hits." hits.</p>\n";
echo "<p><strong>Referers:</strong></p>\n";
echo "<ul>\n";
foreach($this->referers as $k => $v) {
echo " <li>(".$v.") <a href=\"" . $k . "\">".$k."</a></li>\n";
}
echo "</ul>\n";
echo "<p><strong>Pages:</strong></p>\n";
echo "<ul>\n";
foreach($this->pages as $k => $v) {
echo " <li>(".$v.") <a href=\"?page=" . $k . "\">".$k."</a></li>\n";
}
echo "</ul>\n";
echo "<p><strong>Visitors:</strong></p>\n";
echo "<p>".sizeof($this->visitors)." unique visitors.</p>\n";
echo "<ul>\n";
foreach($this->visitors as $k => $v) {
echo " <li>(".$v.") ".$k."</li>\n";
}
echo "</ul>\n";
break;
}
}
private function read($file)
{
$xml = file_get_contents($file);
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stats>\n" . $xml . "\n</stats>\n";
$dom = new DomDocument;
$dom->preserveWhiteSpace = TRUE;
$dom->loadXML($xml);
$entries = $dom->getElementsByTagName('entry');
foreach($entries as $entry) {
$skip = false;
$remoteaddrs = $entry->getElementsByTagName('remoteaddr');
foreach($remoteaddrs as $remoteaddr) {
$addr = $remoteaddr->textContent;
if($addr == "87.63.201.210") $skip = true;
}
$agents = $entry->getElementsByTagName('agent');
foreach($agents as $agent) {
$ag = $agent->textContent;
if(stripos($ag, "bot") > 0) $skip = true;
}
if($skip == true) continue;
$this->visitors[$addr]++;
$refs = $entry->getElementsByTagName('referer');
foreach($refs as $ref) {
$url = $ref->textContent;
if(strpos($url, "executionroom.com") > 0 || $url == "") {} else $this->referers[$url]++;
}
$pages = $entry->getElementsByTagName('page');
foreach($pages as $page) {
$pg = $page->textContent;
if($pg != "") $this->pages[$pg]++;
}
}
arsort($this->referers);
arsort($this->pages);
arsort($this->visitors);
}
public function log($loadtime)
{
$now = time();
$str = "<entry>".
"<time>".xmlenc($now)."</time>". // Time
"<remoteaddr>".xmlenc($_SERVER['REMOTE_ADDR'])."</remoteaddr>". // remote ip
"<remotehost>".xmlenc($_SERVER['REMOTE_HOST'])."</remotehost>". // remote hostname
"<loadtime>".xmlenc($loadtime)."</loadtime>". // Loadtime
"<page>".xmlenc($GLOBALS['page'])."</page>". // Page
// $_SERVER['REMOTE_PORT'] . // current port
// $_SERVER['SCRIPT_FILENAME'] . // script name
"<agent>".xmlenc($_SERVER['HTTP_USER_AGENT'])."</agent>". // User agent (browser)
"<referer>".xmlenc($_SERVER['HTTP_REFERER'])."</referer>". // referer (link)
"<uri>".xmlenc($_SERVER['REQUEST_URI'])."</uri>". // URI
// GeoIP ??
"</entry>\n";
$file = $this->getFilename($now);
if(!file_exists(dirname($file))) mkdir(dirname($file), 0777, true);
$fp = fopen($file, "a");
fwrite($fp, $str);
fclose($fp);
}
private function getFilename($timestamp)
{
$year = "2010";
$month = "03";
$file = $this->statsdir . "/" . $year . "/" . $month . ".xml";
return $file;
}
private function readSingle($timestamp)
{
$file = $this->getFilename($timestamp);
if(file_exists($file)) $this->read($file);
}
private function readAll()
{
}
public function SiteStats($statsdir, $readall = false)
{
$this->statsdir = $statsdir;
if(file_exists($statsdir) && is_dir($statsdir)) {
if($readall) {
$this->readAll();
} else {
// $this->readSingle(time());
}
}
}
}
function sitestats_init()
{
global $DATA_DIR;
return new SiteStats($DATA_DIR."/sitestats", true);
}
?>
|