1 |
<?php |
2 |
/* This file is part of BBClone (A PHP based Web Counter on Steroids) |
3 |
* |
4 |
* SVN FILE $Id$ |
5 |
* |
6 |
* Copyright (C) 2001-2015, the BBClone Team (see doc/authors.txt for details) |
7 |
* |
8 |
* This program is free software: you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation, either version 3 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* See doc/copying.txt for details |
19 |
*/ |
20 |
|
21 |
/////////////////// |
22 |
// Log Processes // |
23 |
/////////////////// |
24 |
|
25 |
// Checking where we came from |
26 |
if (!defined("_MARK_PAGE")) return; |
27 |
|
28 |
// used by usort() |
29 |
function bbc_sort_time_sc($row_a, $row_b) { |
30 |
if ($row_a['time'] == $row_b['time']) return 0; |
31 |
return ($row_a['time'] > $row_b['time'] ) ? 1 : -1; |
32 |
} |
33 |
|
34 |
// return the key of a value |
35 |
function bbc_get_key($array, $str) { |
36 |
reset($array); |
37 |
|
38 |
while (list($idx, $val) = each($array)) { |
39 |
if ($val == $str) return $idx; |
40 |
} |
41 |
return 0; |
42 |
} |
43 |
|
44 |
// purge host and referrer stats at request |
45 |
function bbc_purge_single() { |
46 |
global $access; |
47 |
|
48 |
foreach (array("host", "key", "referer") as $cat) { |
49 |
reset($access[$cat]); |
50 |
|
51 |
while (list($key, $score) = each($access[$cat])) { |
52 |
if ($score == 1) { |
53 |
unset($access[$cat][$key]); |
54 |
($cat == "referer") ? ++$access[$cat]['not_specified'] : ""; |
55 |
} |
56 |
} |
57 |
} |
58 |
} |
59 |
|
60 |
// records the hosts that visited us most. Note, that we only pick up hostnames and strip |
61 |
// any sort of prefix because else the listing would become rather useless |
62 |
function bbc_update_host_stat($client) { |
63 |
global $access, $BBC_IGNORE_BOTS; |
64 |
|
65 |
if ((empty($BBC_IGNORE_BOTS)) || (!isset($client['robot']))) { |
66 |
$is_num = ($client['dns'] == $client['ip']) ? 1 : 0; |
67 |
if ($is_num || strlen($client['dns']) < 2) { |
68 |
// Numeric |
69 |
if (strpos($client['ip'], ":") === false) { |
70 |
// IPv4 |
71 |
$host = trim(substr($client['ip'], 0, strrpos($client['ip'], "."))).". -"; |
72 |
} else { |
73 |
// IPv6 |
74 |
$parts = explode( ":", $client['ip']); |
75 |
$parts = array_slice( $parts, 0, 4); |
76 |
$host = implode( ":", $parts) . ": -"; |
77 |
} |
78 |
} else { |
79 |
// DNS name |
80 |
$parts = explode( ".", $client['dns']); |
81 |
$lastparts = implode( ".", array_slice( $parts, -2, 2) ); |
82 |
if (strlen($lastparts) <= 6) { |
83 |
// Last 2 DNS name parts <= 6; take last 3 parts |
84 |
$host = implode( ".", array_slice( $parts, -3, 3) ); |
85 |
} else { |
86 |
// Last 2 DNS name parts > 6; take last 2 parts |
87 |
$host = implode( ".", array_slice( $parts, -2, 2) ); |
88 |
} |
89 |
} |
90 |
} |
91 |
if ((empty($BBC_IGNORE_BOTS)) || (!isset($client['robot']))) { |
92 |
if (!isset($access['host'][$host])) $access['host'][$host] = 0; |
93 |
$access['host'][$host]++; |
94 |
} |
95 |
|
96 |
if (isset($access['host']['not_specified'])) unset($access['host']['not_specified']); |
97 |
} |
98 |
|
99 |
// the listing of the visited pages |
100 |
function bbc_update_visits($time, $page, $nr) { |
101 |
global $BBC_MAXVISIBLE, $last; |
102 |
|
103 |
$lv = count($last['traffic'][$nr]['views']) - 1; |
104 |
$last_time = substr($last['traffic'][$nr]['views'][$lv], 0, strpos($last['traffic'][$nr]['views'][$lv], "|")); |
105 |
$last_cnt = substr($last['traffic'][$nr]['views'][$lv], (strrpos($last['traffic'][$nr]['views'][$lv], "|") + 1)); |
106 |
$last_page = substr($last['traffic'][$nr]['views'][$lv], (strpos($last['traffic'][$nr]['views'][$lv], "|") + 1)); |
107 |
$last_page = substr($last_page, 0, strpos($last_page, "|")); |
108 |
$last['traffic'][$nr]['off'] = !empty($last['traffic'][$nr]['off']) ? $last['traffic'][$nr]['off'] : 0; |
109 |
|
110 |
if ((empty($last['traffic'][$nr]['views'])) || (!is_array($last['traffic'][$nr]['views']))) return; |
111 |
|
112 |
if (intval($last_page) === intval($page)) { |
113 |
$last['traffic'][$nr]['views'][$lv] = "$last_time|$last_page|".++$last_cnt; |
114 |
$last['traffic'][$nr]['off']++; |
115 |
} |
116 |
else $last['traffic'][$nr]['views'][] = "$time|$page|1"; |
117 |
|
118 |
sort($last['traffic'][$nr]['views']); |
119 |
|
120 |
// number of elements to be removed with array_splice() if necessary |
121 |
$lv = count($last['traffic'][$nr]['views']) - 1; |
122 |
$del = (($lv + 1) > $BBC_MAXVISIBLE) ? (($lv + 1) - $BBC_MAXVISIBLE) : false; |
123 |
$last['traffic'][$nr]['views'] = ($del !== false) ? array_splice($last['traffic'][$nr]['views'], $del) : |
124 |
$last['traffic'][$nr]['views']; |
125 |
} |
126 |
|
127 |
// The most visited pages ranking |
128 |
function bbc_update_page_stats($connect) { |
129 |
global $BBC_MAX_PAGENAME, $access, $last; |
130 |
|
131 |
$long_page = $connect['page']; |
132 |
$oversized_pagename = (strlen($long_page) > $BBC_MAX_PAGENAME) ? 1 : 0; |
133 |
$connect['page'] = $oversized_pagename ? "...".substr($long_page, -($BBC_MAX_PAGENAME-3)) : $long_page; |
134 |
|
135 |
// Fix oversized page titles |
136 |
if (($oversized_pagename) && (isset($access['page'][$long_page]['count']))) { |
137 |
$access['page'][($connect['page'])]['count'] = $access['page'][$long_page]['count']; |
138 |
$access['page'][($connect['page'])]['uri'] = $access['page'][$long_page]['uri']; |
139 |
unset($access['page'][$long_page]); |
140 |
} |
141 |
|
142 |
if (!isset($access['page'][($connect['page'])]['count'])) { |
143 |
$access['page'][($connect['page'])]['count'] = 0; |
144 |
} |
145 |
|
146 |
$access['page'][($connect['page'])]['count']++; |
147 |
$access['page'][($connect['page'])]['uri'] = $connect['uri']; |
148 |
|
149 |
$last['pages'] = ((empty($last['pages'])) || (!is_array($last['pages']))) ? array() : $last['pages']; |
150 |
|
151 |
if (($oversized_pagename) && (in_array($long_page, $last['pages']))) { |
152 |
$last['pages'][bbc_get_key($last['pages'], $long_page)] = $connect['page']; |
153 |
} |
154 |
if (!in_array($connect['page'], $last['pages'])) $last['pages'][] = $connect['page']; |
155 |
|
156 |
$connect['page'] = bbc_get_key($last['pages'], $connect['page']); |
157 |
|
158 |
if (isset($connect['uri'])) unset($connect['uri']); |
159 |
|
160 |
return $connect; |
161 |
} |
162 |
|
163 |
// Transfer the raw data from the main counters of var into $last. |
164 |
// Any new data (more recent than $BBC_MAXTIME) is used in the global stats |
165 |
function bbc_add_new_connections_to_old() { |
166 |
global $BBC_IGNORE_AGENT, $BBC_IGNORE_BOTS, $BBC_MAXTIME, $BBC_MAXVISIBLE, $BBC_NO_DNS, $BBC_NO_HITS, |
167 |
$BBC_PURGE_SINGLE, $access, $last; |
168 |
|
169 |
// Checking whether we have new connections |
170 |
if (!$new_access = bbc_counter_to_array()) return false; |
171 |
|
172 |
// cleanup if requested |
173 |
!empty($BBC_PURGE_SINGLE) ? bbc_purge_single() : ""; |
174 |
|
175 |
((!empty($access['time'])) && (is_array($access['time']))) ? bbc_time_offset() : ""; |
176 |
|
177 |
// Upgrade from older versions. We need to erase the "last" data. |
178 |
if (isset($access['last'])) unset($access['last']); |
179 |
|
180 |
//check for broken 0.4.2 referrer counting and apply fix if necessary |
181 |
if (isset($access) && !isset($access['bugs']['ref_fix']) && isset($access['referer']['not_specified'])) { |
182 |
bbc_fix_refer_stat(array_sum($access['referer'])); |
183 |
} |
184 |
|
185 |
// fix wrong browser assignments |
186 |
foreach (array("java", "wwwc", "libwww") as $what) { |
187 |
if (isset($access) && isset($access['stat']['browser'][$what])) { |
188 |
$access['stat']['robot'][$what] = $access['stat']['browser'][$what]; |
189 |
$access['stat']['os']['other'] -= $access['stat']['robot'][$what]; |
190 |
|
191 |
unset($access['stat']['browser'][$what]); |
192 |
} |
193 |
} |
194 |
|
195 |
$nb_new_access = (!empty($new_access) && is_array($new_access)) ? count($new_access) : 0; |
196 |
$nb_last_access = (!empty($last['traffic']) && is_array($last['traffic'])) ? count($last['traffic']) : 0; |
197 |
|
198 |
foreach ($new_access as $connect) { |
199 |
$connect = bbc_update_connect($connect); |
200 |
|
201 |
// the "last reset on" flag initialisation |
202 |
if ((!isset($access['time'])) && (!isset($access['time']['reset']))) { |
203 |
$access['time']['reset'] = $connect['time']; |
204 |
} |
205 |
|
206 |
// Stop processing if bots are completely ignored |
207 |
if ((!empty($BBC_IGNORE_BOTS)) && ($BBC_IGNORE_BOTS == 2)) { |
208 |
if (!empty($connect['robot'])) { |
209 |
--$nb_new_access; |
210 |
continue; |
211 |
} |
212 |
} |
213 |
// Omit referrers coming from robots |
214 |
$connect['referer'] = !empty($connect['robot']) ? "unknown" : $connect['referer']; |
215 |
|
216 |
$this_connect = $connect['time']; |
217 |
$last_connect = !empty($access['time']['last']) ? $access['time']['last'] : 0; |
218 |
|
219 |
// Hits as base for time stats if desired |
220 |
if (empty($BBC_NO_HITS)) bbc_update_time_stat($this_connect, $last_connect); |
221 |
|
222 |
// The script viewed |
223 |
$connect = isset($connect['page']) ? bbc_update_page_stats($connect) : $connect; |
224 |
$prev_recorded = 0; |
225 |
|
226 |
// Check if a similar connection has been recorded yet |
227 |
for($l = $nb_last_access - 1; ($l >= 0) && (($connect['time'] - $last['traffic'][$l]['time']) < $BBC_MAXTIME); |
228 |
$l--) { |
229 |
if (!empty($BBC_IGNORE_AGENT) ? ($connect['ip'] == $last['traffic'][$l]['ip']) : |
230 |
(($connect['ip'] == $last['traffic'][$l]['ip']) && ($connect['agent'] == $last['traffic'][$l]['agent']))) { |
231 |
$last['traffic'][$l]['page'] = $connect['page']; |
232 |
$last['traffic'][$l]['time'] = $this_connect; |
233 |
$last['traffic'][$l]['visits']++; |
234 |
$access['stat']['totalvisits']++; |
235 |
|
236 |
($BBC_MAXVISIBLE > 0) ? bbc_update_visits($connect['time'], $connect['page'], $l) : ""; |
237 |
|
238 |
$prev_recorded = 1; |
239 |
break; |
240 |
} |
241 |
} |
242 |
|
243 |
// Add new connection if it hasn't been recorded yet |
244 |
if (!$prev_recorded) { |
245 |
if (empty($access['stat']['totalvisits'])) $access['stat']['totalvisits'] = 0; |
246 |
if (empty($access['stat']['totalcount'])) $access['stat']['totalcount'] = 0; |
247 |
|
248 |
$connect['dns'] = !empty($BBC_NO_DNS) ? $connect['ip'] : bbc_clean(gethostbyaddr($connect['ip'])); |
249 |
$connect['ext'] = bbc_get_extension($connect['dns'], $connect['ip']); |
250 |
|
251 |
$last['traffic'][$nb_last_access] = bbc_update_access($connect); |
252 |
// Visit stats |
253 |
$last['traffic'][$nb_last_access]['views'][] = $last['traffic'][$nb_last_access]['time']."|" |
254 |
.$last['traffic'][$nb_last_access]['page']."|1"; |
255 |
|
256 |
// Unique visits as base for time stats if desired |
257 |
if (!empty($BBC_NO_HITS)) bbc_update_time_stat($this_connect, $last_connect); |
258 |
|
259 |
// Referrers collection will be updated all along with the keywords if available |
260 |
if (isset($connect['referer'])) { |
261 |
bbc_update_referer_stat($connect['referer']); |
262 |
$flt_search = bbc_get_keywords($connect['referer']); |
263 |
} |
264 |
// The search as a whole in $last |
265 |
$last['traffic'][$nb_last_access]['search'] = ($flt_search !== false) ? implode(" ", $flt_search) : "-"; |
266 |
|
267 |
// The host listing |
268 |
if ((isset($connect['dns'])) && (isset($connect['ip']))) { |
269 |
bbc_update_host_stat($last['traffic'][$nb_last_access]); |
270 |
} |
271 |
|
272 |
$access['stat']['totalvisits']++; |
273 |
$access['stat']['totalcount']++; |
274 |
$nb_last_access++; |
275 |
} |
276 |
} |
277 |
return $nb_new_access; |
278 |
} |
279 |
|
280 |
// Remove unnecessary connections from $last, that either exceed the $BBC_MAXVISIBLE limit or are |
281 |
// older than time() - $BBC_MAXTIME. |
282 |
function bbc_update_last_access() { |
283 |
global $last, $BBC_MAXTIME, $BBC_MAXVISIBLE, $BBC_TIMESTAMP, $BBC_TIME_OFFSET; |
284 |
|
285 |
if (($BBC_MAXVISIBLE <= 0) || (empty($last['traffic'])) || (!is_array($last['traffic']))) { |
286 |
$last['traffic'] = array(); |
287 |
return; |
288 |
} |
289 |
else { |
290 |
$nb_connect = count($last['traffic']); |
291 |
$ctime = $BBC_TIMESTAMP + ($BBC_TIME_OFFSET * 60); |
292 |
|
293 |
for ($k = $nb_connect - 1 - $BBC_MAXVISIBLE; $k >= 0; $k--) { |
294 |
if (($ctime - $last['traffic'][$k]['time']) > $BBC_MAXTIME) unset($last['traffic'][$k]); |
295 |
} |
296 |
usort($last['traffic'],"bbc_sort_time_sc"); |
297 |
} |
298 |
} |
299 |
?> |