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