MIOLO20
Tudo Estruturas de dados Namespaces Ficheiros Funções Variáveis
class_counter.php
Ir para a documentação deste ficheiro.
1<?php
2
3/************************************************
4* *
5* TSstatistics *
6* *
7* by Thomas Schuster *
8* http://www.TSinter.net *
9* *
10* file: class_counter.php *
11* version: 2.6 *
12* license: GNU General Public License *
13* created: 13.04.2002 *
14* email: admin@TSinter.net *
15* *
16* *
17* Object oriented traffic analyser. Needs no *
18* database. Tracks the visitors of a website. *
19* Filters out over 100 robots. Reload restric- *
20* tion. Displays hits per hour/day/month, *
21* various toplists, all graphical. Auto back- *
22* up. Administration center. *
23* *
24* Copyright (c) 2001-2002 TSinter.net *
25* All rights reserved. *
26* *
27************************************************/
28
30{
31
32 //-----------------------------------------//
33 // Set the reload restriction (in seconds) //
34 //-----------------------------------------//
36
37 //-----------------------------------------//
38 // Set the start value of the visitors //
39 //-----------------------------------------//
41
42 //-----------------------------------------//
43 // Do not change anything below! //
44 //-----------------------------------------//
47 var $ip;
52
55
57
58 var $hour;
59 var $day;
60 var $month;
61 var $year;
62
63 /*
64 ** Function: TScounter
65 ** Input: STRING class_path
66 ** Output: VOID
67 ** Description: This is the constructor of the class.
68 */
69 function TScounter($url_root, $file_path, $data_path)
70 {
71 self::__construct($url_root, $file_path, $data_path);
72 }
73
74 function __construct($url_root, $file_path, $data_path)
75 {
76 $this->hour = date("G");
77 $this->day = date("j");
78 $this->month = date("n");
79 $this->year = date("Y");
80
81 # Get the ip of the client browser.
82 if (getenv("HTTP_X_FORWARDED_FOR"))
83 {
84
85 # Client uses a proxy server.
86 $client_ip = getenv("HTTP_X_FORWARDED_FOR");
87 $this->ip = substr($client_ip, 0, strpos($client_ip, ','));
88
89 if ($this->ip == "")
90 $this->ip = getenv("HTTP_CLIENT_IP");
91 }
92 else
93 {
94
95 # Client does not use proxy.
96 $this->ip = getenv("REMOTE_ADDR");
97 }
98
99 # Check the php version.
100 if ($this->_checkMinimumVersion("4.1.0"))
101 {
102
103 # php version is at least 4.1.0
104 $this->webpage = $_SERVER["PHP_SELF"];
105 $this->user_agent = $_SERVER["HTTP_USER_AGENT"];
106
107 if (isset($_COOKIE["PHPSESSID"]))
108 $this->sessionID = $_COOKIE["PHPSESSID"];
109 }
110 else
111 {
112
113 # php version is older than 4.1.0
114 global $HTTP_SERVER_VARS;
115 global $HTTP_COOKIE_VARS;
116 $this->webpage = $HTTP_SERVER_VARS["PHP_SELF"];
117 $this->user_agent = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];
118
119 if (isset($HTTP_COOKIE_VARS["PHPSESSID"]))
120 $this->sessionID = $HTTP_COOKIE_VARS["PHPSESSID"];
121 }
122
123 # Get the relative path of the webpage from the document root.
124 if (sizeof($subdirs = explode("/", $url_root)) > 0)
125 {
126 $webpath = explode("/", $this->webpage);
127 $this->webpage = "";
128
129 for ($i = sizeof($webpath) - 1; $i >= sizeof($subdirs); $i--)
130 $this->webpage = "/" . $webpath[$i] . $this->webpage;
131 }
132
133 if ($data_path === null)
134 {
135 $data_path = $file_path . '/data';
136 }
137
138 $this->data_dir = $data_path;
139
140 # Set the path to the data files.
141 $this->log_data["track"] = $this->data_dir . "track_" . $this->month . "_" . $this->year . ".log";
142 $this->log_data["hits"] = $this->data_dir . "hits.log";
143
144 $this->spider_library = $this->data_dir . "spider.lib";
145
146 # Place the pointer at the last user track.
147 $this->currentIndex = sizeof(file($this->log_data["track"])) - 1;
148
149 if ($this->currentIndex < 0)
150 $this->currentIndex = 0;
151 }
152
153 /*
154 ** Function: _log
155 ** Input: VOID
156 ** Output: VOID
157 ** Description: Log the page impressions and visits.
158 */
159 function _log($category)
160 {
161 switch ($category)
162 {
163 case "impression":
164 $subject = $this->webpage;
165
166 break;
167
168 case "visit":
169 $subject = $category;
170
171 break;
172 }
173
174 # This Function checks whether there is already an entry for the webpage
175 # in the hits_data. If there is one, it will increment the number
176 # of page impressions. If there is none, it will initialize the number of
177 # page impressions of that webpage with 1. Same procedure for logging the
178 # visits, except that the start value can be modified if prefered.
179
180 $log_file = file($this->log_data["hits"]);
181
182 if ($this->_incrementIfEqual($subject, $log_file, 0, 1) != 1)
183 {
184 # There is no entry yet, so we have to add a new one.
185 if (!strcmp($subject, "visit"))
186 $start_value = $this->visitorStartValue;
187 else
188 $start_value = 1;
189
190 $line = $subject . "|" . $start_value . "|\r\n";
191 $this->_writeLine($this->log_data["hits"], $line, "a");
192 }
193 else
194 {
195 # Write the already changed array back to the hits_data.
196 $this->_writeArray($this->log_data["hits"], $log_file, "w");
197 }
198 }
199
200 /*
201 ** Function: _getNumberOf
202 ** Input: VOID
203 ** Output: INTEGER
204 ** Description: Get the number of page impressions of actual webpage or
205 ** the number of visitors of the website.
206 */
207 function _getNumberOf($category)
208 {
209 switch ($category)
210 {
211 case "impression":
212 $subject = $this->webpage;
213
214 break;
215
216 case "visit":
217 $subject = "visit";
218
219 break;
220 }
221
222 $log_file = file($this->log_data["hits"]);
223
224 for ($i = 0; $i < sizeof($log_file); $i++)
225 {
226 $log_line = explode("|", $log_file[$i]);
227
228 if (!strcmp($log_line[0], $subject))
229 {
230 return $log_line[1];
231 break;
232 }
233 }
234 }
235
236 /*
237 ** Function: _processPageRequest
238 ** Input: BOOLEAN cookie_support
239 ** Output: VOID
240 ** Description: Does all necessary work to count visitors,
241 page impressions and stores the user track.
242 */
243 function _processPageRequest($cookie_support)
244 {
245 if (!$this->_identifyClientAs("spider"))
246 {
247 # Start the main process.
248
249 switch ($cookie_support)
250 {
251 case TRUE:
252
253 # User was already counted, cookies are supported.
254 if ($this->_compare("sessionID") || $this->_compare("ip"))
255 {
256
257 # Add the actual webpage to the user track and
258 # increase the page impressions.
259
260 $this->_trackVisitor("old visitor");
261 $this->_log("impression");
262 }
263 else
264 {
265
266 # We have to search the corresponding user
267 # track of the actual user in the logfile.
268 # If the index is < 0, we have reached the
269 # end of the logfile. We log the new visitor,
270 # which has never visited us before.
271
272 $this->currentIndex--;
273
274 if ($this->currentIndex >= 0)
275 $this->_processPageRequest(TRUE);
276 else
277 {
278 $this->_log("visit");
279 $this->_trackVisitor("new visitor");
280 $this->_log("impression");
281 }
282 }
283
284 break;
285
286 case FALSE:
287
288 # No session id generated yet. Either this is
289 # the first page request of the visitor or the
290 # client browser does not support cookies.
291
292 if ($this->_compare("ip"))
293 {
294
295 # Client browser does not support cookies but
296 # we can track the visitor by the IP.
297
298 $this->_trackVisitor("old visitor");
299 $this->_log("impression");
300 }
301 else if ($this->_reloadRestriction())
302 {
303
304 # Another visitor has requested the website during
305 # the period of time defined as reload restriction.
306 # It is possible that the visitor we are actually
307 # tracking has already been counted during that reload
308 # restriction. To check this, we search the IP in the
309 # older user tracks until the elapsed time is higher
310 # than the reload restriction.
311
312 # If the index is < 0, we have reached the
313 # end of the logfile. We log the new visitor,
314 # which has never visited us before.
315
316 $this->currentIndex--;
317
318 if ($this->currentIndex >= 0)
319 $this->_processPageRequest(FALSE);
320 else
321 {
322 $this->_log("visit");
323 $this->_trackVisitor("new visitor");
324 $this->_log("impression");
325 }
326 }
327 else
328 {
329
330 # A new visitor has entered the website.
331
332 $this->_log("visit");
333 $this->_trackVisitor("new visitor");
334 $this->_log("impression");
335 }
336
337 break;
338 }
339 }
340 }
341
342 /*
343 ** Function: _trackVisitor
344 ** Input: STRING mode
345 ** Output: VOID
346 ** Description: Track the user through the website.
347 */
348 function _trackVisitor($mode)
349 {
350 $log_file = file($this->log_data["track"]);
351
352 switch ($mode)
353 {
354 case "new visitor":
355
356 # Start a new user track.
357 $user_track = "|" . $this->ip . "|" . time() . "|" . time() . "|" . $this->webpage . "|\r\n";
358
359 # Add the new user track to the logfile.
360 $this->_writeLine($this->log_data["track"], $user_track, "a");
361 break;
362
363 case "old visitor":
364
365 # Add the actual webpage to the existing user track.
366 # Try to add the session id to the user track.
367
368 $user_track = explode("|", $log_file[$this->currentIndex]);
369
370 if (isset($this->sessionID))
371 $user_track[0] = $this->sessionID;
372
373 # Track the time of the actual request.
374 $user_track[3] = time();
375
376 $user_track[sizeof($user_track) - 1] = $this->webpage . "|\r\n";
377 $log_file[$this->currentIndex] = implode("|", $user_track);
378
379 # Write the changed user track back to the logfile.
380 $this->_writeArray($this->log_data["track"], $log_file, "w");
381 break;
382 }
383 }
384
385 /*
386 ** Function: _compare
387 ** Input: STRING subject
388 ** Output: BOOLEAN
389 ** Description: Returns TRUE if two compared subjects are equal.
390 */
391 function _compare($subject)
392 {
393 $log_file = file($this->log_data["track"]);
394
395 $user_track = explode("|", $log_file[$this->currentIndex]);
396
397 switch ($subject)
398 {
399 case "ip":
400
401 # Compare the object ip with the ip
402 # of the user track.
403
404 if (isset($user_track[1]) && !strcmp($user_track[1], $this->ip))
405 return TRUE;
406 else
407 return FALSE;
408
409 break;
410
411 case "sessionID":
412
413 # Compare the object session id with the
414 # session id stored in the user track.
415
416 if (isset($this->sessionID) && !strcmp($user_track[0], $this->sessionID))
417 return TRUE;
418 else
419 return FALSE;
420
421 break;
422 }
423 }
424
425 /*
426 ** Function: _reloadRestriction
427 ** Input: VOID
428 ** Output: BOOLEAN
429 ** Description: Returns TRUE if the time elapsed between the previous
430 page request and the actual page request is smaller
431 than the period of time defined as reloadRestriction.
432 */
434 {
435 $log_file = file($this->log_data["track"]);
436 $user_track = explode("|", $log_file[$this->currentIndex]);
437
438 if (isset($user_track[3]) && (time() - $user_track[3] < $this->reloadRestriction))
439 return TRUE;
440 else
441 return FALSE;
442 }
443
444 /*
445 ** Function: _identifyClientAs
446 ** Input: VOID
447 ** Output: STRING spiderline[1] or FALSE
448 ** Description: Check if the current visitor is a member of a category.
449 */
450 function _identifyClientAs($category)
451 {
452 switch ($category)
453 {
454 case "spider":
455 $library_data = $this->spider_library;
456
457 $subject = $this->user_agent;
458 break;
459 }
460
461 $library_file = file($library_data);
462
463 # Compare the spider library with the user_agent.
464 # If there are any matches, the current page has
465 # been requested by a spider or a robot.
466
467 for ($i = 0; $i < sizeof($library_file); $i++)
468 {
469 $library_line = explode("|", $library_file[$i]);
470
471 if (preg_match('/'.$library_line[0].'/i', $subject))
472 return $library_line[1];
473 }
474
475 return FALSE;
476 }
477}
478?>
_incrementIfEqual($string, &$array, $index_equal, $index_increment)
_writeLine($file, $line, $mode)
_checkMinimumVersion($version)
_writeArray($file, $array, $mode)
_log($category)
_identifyClientAs($category)
__construct($url_root, $file_path, $data_path)
TScounter($url_root, $file_path, $data_path)
_trackVisitor($mode)
_getNumberOf($category)
_compare($subject)
_processPageRequest($cookie_support)