ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/BBClone/trunk/log_processor.php
Revision: 201
Committed: Tue Jan 7 12:40:32 2014 UTC (10 years, 10 months ago) by matthys
File size: 11343 byte(s)
Log Message:
Rewrote bbc_update_host_stat($client); detect dns name <2, lenght dns name to display (last 2 or 3 parts)

File Contents

# Content
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-2014, 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'], "."))).".&nbsp;-";
72 } else {
73 // IPv6
74 $parts = explode( ":", $client['ip']);
75 $parts = array_slice( $parts, 0, 4);
76 $host = implode( ":", $parts) . ":&nbsp;-";
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 $access, $last;
130
131 $long_page = $connect['page'];
132 $over_60 = (strlen($long_page) > 60) ? 1 : 0;
133 $connect['page'] = $over_60 ? "...".substr($long_page, -57) : $long_page;
134
135 // Fix oversized page titles
136 if (($over_60) && (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
143 if (!isset($access['page'][($connect['page'])]['count'])) {
144 $access['page'][($connect['page'])]['count'] = 0;
145 }
146
147 $access['page'][($connect['page'])]['count']++;
148 $access['page'][($connect['page'])]['uri'] = $connect['uri'];
149
150 $last['pages'] = ((empty($last['pages'])) || (!is_array($last['pages']))) ? array() : $last['pages'];
151
152 if (($over_60) && (in_array($long_page, $last['pages']))) {
153 $last['pages'][bbc_get_key($last['pages'], $long_page)] = $connect['page'];
154 }
155 if (!in_array($connect['page'], $last['pages'])) $last['pages'][] = $connect['page'];
156
157 $connect['page'] = bbc_get_key($last['pages'], $connect['page']);
158
159 if (isset($connect['uri'])) unset($connect['uri']);
160
161 return $connect;
162 }
163
164 // Transfer the raw data from the main counters of var into $last.
165 // Any new data (more recent than $BBC_MAXTIME) is used in the global stats
166 function bbc_add_new_connections_to_old() {
167 global $BBC_IGNORE_AGENT, $BBC_IGNORE_BOTS, $BBC_MAXTIME, $BBC_MAXVISIBLE, $BBC_NO_DNS, $BBC_NO_HITS,
168 $BBC_PURGE_SINGLE, $access, $last;
169
170 // Checking whether we have new connections
171 if (!$new_access = bbc_counter_to_array()) return false;
172
173 // cleanup if requested
174 !empty($BBC_PURGE_SINGLE) ? bbc_purge_single() : "";
175
176 ((!empty($access['time'])) && (is_array($access['time']))) ? bbc_time_offset() : "";
177
178 // Upgrade from older versions. We need to erase the "last" data.
179 if (isset($access['last'])) unset($access['last']);
180
181 //check for broken 0.4.2 referrer counting and apply fix if necessary
182 if (isset($access) && !isset($access['bugs']['ref_fix']) && isset($access['referer']['not_specified'])) {
183 bbc_fix_refer_stat(array_sum($access['referer']));
184 }
185
186 // fix wrong browser assignments
187 foreach (array("java", "wwwc", "libwww") as $what) {
188 if (isset($access) && isset($access['stat']['browser'][$what])) {
189 $access['stat']['robot'][$what] = $access['stat']['browser'][$what];
190 $access['stat']['os']['other'] -= $access['stat']['robot'][$what];
191
192 unset($access['stat']['browser'][$what]);
193 }
194 }
195
196 $nb_new_access = (!empty($new_access) && is_array($new_access)) ? count($new_access) : 0;
197 $nb_last_access = (!empty($last['traffic']) && is_array($last['traffic'])) ? count($last['traffic']) : 0;
198
199 foreach ($new_access as $connect) {
200 $connect = bbc_update_connect($connect);
201
202 // the "last reset on" flag initialisation
203 if ((!isset($access['time'])) && (!isset($access['time']['reset']))) {
204 $access['time']['reset'] = $connect['time'];
205 }
206
207 // Stop processing if bots are completely ignored
208 if ((!empty($BBC_IGNORE_BOTS)) && ($BBC_IGNORE_BOTS == 2)) {
209 if (!empty($connect['robot'])) {
210 --$nb_new_access;
211 continue;
212 }
213 }
214 // Omit referrers coming from robots
215 $connect['referer'] = !empty($connect['robot']) ? "unknown" : $connect['referer'];
216
217 $this_connect = $connect['time'];
218 $last_connect = !empty($access['time']['last']) ? $access['time']['last'] : 0;
219
220 // Hits as base for time stats if desired
221 if (empty($BBC_NO_HITS)) bbc_update_time_stat($this_connect, $last_connect);
222
223 // The script viewed
224 $connect = isset($connect['page']) ? bbc_update_page_stats($connect) : $connect;
225 $prev_recorded = 0;
226
227 // Check if a similar connection has been recorded yet
228 for($l = $nb_last_access - 1; ($l >= 0) && (($connect['time'] - $last['traffic'][$l]['time']) < $BBC_MAXTIME);
229 $l--) {
230 if (!empty($BBC_IGNORE_AGENT) ? ($connect['ip'] == $last['traffic'][$l]['ip']) :
231 (($connect['ip'] == $last['traffic'][$l]['ip']) && ($connect['agent'] == $last['traffic'][$l]['agent']))) {
232 $last['traffic'][$l]['page'] = $connect['page'];
233 $last['traffic'][$l]['time'] = $this_connect;
234 $last['traffic'][$l]['visits']++;
235 $access['stat']['totalvisits']++;
236
237 ($BBC_MAXVISIBLE > 0) ? bbc_update_visits($connect['time'], $connect['page'], $l) : "";
238
239 $prev_recorded = 1;
240 break;
241 }
242 }
243
244 // Add new connection if it hasn't been recorded yet
245 if (!$prev_recorded) {
246 if (empty($access['stat']['totalvisits'])) $access['stat']['totalvisits'] = 0;
247 if (empty($access['stat']['totalcount'])) $access['stat']['totalcount'] = 0;
248
249 $connect['dns'] = !empty($BBC_NO_DNS) ? $connect['ip'] : bbc_clean(gethostbyaddr($connect['ip']));
250 $connect['ext'] = bbc_get_extension($connect['dns'], $connect['ip']);
251
252 $last['traffic'][$nb_last_access] = bbc_update_access($connect);
253 // Visit stats
254 $last['traffic'][$nb_last_access]['views'][] = $last['traffic'][$nb_last_access]['time']."|"
255 .$last['traffic'][$nb_last_access]['page']."|1";
256
257 // Unique visits as base for time stats if desired
258 if (!empty($BBC_NO_HITS)) bbc_update_time_stat($this_connect, $last_connect);
259
260 // Referrers collection will be updated all along with the keywords if available
261 if (isset($connect['referer'])) {
262 bbc_update_referer_stat($connect['referer']);
263 $flt_search = bbc_get_keywords($connect['referer']);
264 }
265 // The search as a whole in $last
266 $last['traffic'][$nb_last_access]['search'] = ($flt_search !== false) ? implode(" ", $flt_search) : "-";
267
268 // The host listing
269 if ((isset($connect['dns'])) && (isset($connect['ip']))) {
270 bbc_update_host_stat($last['traffic'][$nb_last_access]);
271 }
272
273 $access['stat']['totalvisits']++;
274 $access['stat']['totalcount']++;
275 $nb_last_access++;
276 }
277 }
278 return $nb_new_access;
279 }
280
281 // Remove unnecessary connections from $last, that either exceed the $BBC_MAXVISIBLE limit or are
282 // older than time() - $BBC_MAXTIME.
283 function bbc_update_last_access() {
284 global $last, $BBC_MAXTIME, $BBC_MAXVISIBLE, $BBC_TIMESTAMP, $BBC_TIME_OFFSET;
285
286 if (($BBC_MAXVISIBLE <= 0) || (empty($last['traffic'])) || (!is_array($last['traffic']))) {
287 $last['traffic'] = array();
288 return;
289 }
290 else {
291 $nb_connect = count($last['traffic']);
292 $ctime = $BBC_TIMESTAMP + ($BBC_TIME_OFFSET * 60);
293
294 for ($k = $nb_connect - 1 - $BBC_MAXVISIBLE; $k >= 0; $k--) {
295 if (($ctime - $last['traffic'][$k]['time']) > $BBC_MAXTIME) unset($last['traffic'][$k]);
296 }
297 usort($last['traffic'],"bbc_sort_time_sc");
298 }
299 }
300 ?>

Properties

Name Value
svn:keywords Id