00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class TScounter extends SupportFunctions
00030 {
00031
00032
00033
00034
00035 var $reloadRestriction = 300;
00036
00037
00038
00039
00040 var $visitorStartValue = 1;
00041
00042
00043
00044
00045 var $log_data;
00046 var $spider_library;
00047 var $ip;
00048 var $sessionID;
00049 var $pageImpressions = 1;
00050 var $visitors;
00051 var $webpage;
00052
00053 var $user_agent;
00054 var $currentIndex;
00055
00056 var $data_dir;
00057
00058 var $hour;
00059 var $day;
00060 var $month;
00061 var $year;
00062
00063
00064
00065
00066
00067
00068
00069 function TScounter($url_root, $file_path, $data_path)
00070 {
00071 $this->hour = date("G");
00072 $this->day = date("j");
00073 $this->month = date("n");
00074 $this->year = date("Y");
00075
00076 # Get the ip of the client browser.
00077 if (getenv("HTTP_X_FORWARDED_FOR"))
00078 {
00079
00080 # Client uses a proxy server.
00081 $client_ip = getenv("HTTP_X_FORWARDED_FOR");
00082 $this->ip = substr($client_ip, 0, strpos($client_ip, ','));
00083
00084 if ($this->ip == "")
00085 $this->ip = getenv("HTTP_CLIENT_IP");
00086 }
00087 else
00088 {
00089
00090 # Client does not use proxy.
00091 $this->ip = getenv("REMOTE_ADDR");
00092 }
00093
00094 # Check the php version.
00095 if ($this->_checkMinimumVersion("4.1.0"))
00096 {
00097
00098 # php version is at least 4.1.0
00099 $this->webpage = $_SERVER["PHP_SELF"];
00100 $this->user_agent = $_SERVER["HTTP_USER_AGENT"];
00101
00102 if (isset($_COOKIE["PHPSESSID"]))
00103 $this->sessionID = $_COOKIE["PHPSESSID"];
00104 }
00105 else
00106 {
00107
00108 # php version is older than 4.1.0
00109 global $HTTP_SERVER_VARS;
00110 global $HTTP_COOKIE_VARS;
00111 $this->webpage = $HTTP_SERVER_VARS["PHP_SELF"];
00112 $this->user_agent = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];
00113
00114 if (isset($HTTP_COOKIE_VARS["PHPSESSID"]))
00115 $this->sessionID = $HTTP_COOKIE_VARS["PHPSESSID"];
00116 }
00117
00118 # Get the relative path of the webpage from the document root.
00119 if (sizeof($subdirs = explode("/", $url_root)) > 0)
00120 {
00121 $webpath = explode("/", $this->webpage);
00122 $this->webpage = "";
00123
00124 for ($i = sizeof($webpath) - 1; $i >= sizeof($subdirs); $i--)
00125 $this->webpage = "/" . $webpath[$i] . $this->webpage;
00126 }
00127
00128 if ($data_path === null)
00129 {
00130 $data_path = $file_path . '/data';
00131 }
00132
00133 $this->data_dir = $data_path;
00134
00135 # Set the path to the data files.
00136 $this->log_data["track"] = $this->data_dir . "track_" . $this->month . "_" . $this->year . ".log";
00137 $this->log_data["hits"] = $this->data_dir . "hits.log";
00138
00139 $this->spider_library = $this->data_dir . "spider.lib";
00140
00141 # Place the pointer at the last user track.
00142 $this->currentIndex = sizeof(file($this->log_data["track"])) - 1;
00143
00144 if ($this->currentIndex < 0)
00145 $this->currentIndex = 0;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154 function _log($category)
00155 {
00156 switch ($category)
00157 {
00158 case "impression":
00159 $subject = $this->webpage;
00160
00161 break;
00162
00163 case "visit":
00164 $subject = $category;
00165
00166 break;
00167 }
00168
00169 # This Function checks whether there is already an entry for the webpage
00170 # in the hits_data. If there is one, it will increment the number
00171 # of page impressions. If there is none, it will initialize the number of
00172 # page impressions of that webpage with 1. Same procedure for logging the
00173 # visits, except that the start value can be modified if prefered.
00174
00175 $log_file = file($this->log_data["hits"]);
00176
00177 if ($this->_incrementIfEqual($subject, $log_file, 0, 1) != 1)
00178 {
00179 # There is no entry yet, so we have to add a new one.
00180 if (!strcmp($subject, "visit"))
00181 $start_value = $this->visitorStartValue;
00182 else
00183 $start_value = 1;
00184
00185 $line = $subject . "|" . $start_value . "|\r\n";
00186 $this->_writeLine($this->log_data["hits"], $line, "a");
00187 }
00188 else
00189 {
00190 # Write the already changed array back to the hits_data.
00191 $this->_writeArray($this->log_data["hits"], $log_file, "w");
00192 }
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202 function _getNumberOf($category)
00203 {
00204 switch ($category)
00205 {
00206 case "impression":
00207 $subject = $this->webpage;
00208
00209 break;
00210
00211 case "visit":
00212 $subject = "visit";
00213
00214 break;
00215 }
00216
00217 $log_file = file($this->log_data["hits"]);
00218
00219 for ($i = 0; $i < sizeof($log_file); $i++)
00220 {
00221 $log_line = explode("|", $log_file[$i]);
00222
00223 if (!strcmp($log_line[0], $subject))
00224 {
00225 return $log_line[1];
00226 break;
00227 }
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238 function _processPageRequest($cookie_support)
00239 {
00240 if (!$this->_identifyClientAs("spider"))
00241 {
00242 # Start the main process.
00243
00244 switch ($cookie_support)
00245 {
00246 case TRUE:
00247
00248 # User was already counted, cookies are supported.
00249 if ($this->_compare("sessionID") || $this->_compare("ip"))
00250 {
00251
00252 # Add the actual webpage to the user track and
00253 # increase the page impressions.
00254
00255 $this->_trackVisitor("old visitor");
00256 $this->_log("impression");
00257 }
00258 else
00259 {
00260
00261 # We have to search the corresponding user
00262 # track of the actual user in the logfile.
00263 # If the index is < 0, we have reached the
00264 # end of the logfile. We log the new visitor,
00265 # which has never visited us before.
00266
00267 $this->currentIndex--;
00268
00269 if ($this->currentIndex >= 0)
00270 $this->_processPageRequest(TRUE);
00271 else
00272 {
00273 $this->_log("visit");
00274 $this->_trackVisitor("new visitor");
00275 $this->_log("impression");
00276 }
00277 }
00278
00279 break;
00280
00281 case FALSE:
00282
00283 # No session id generated yet. Either this is
00284 # the first page request of the visitor or the
00285 # client browser does not support cookies.
00286
00287 if ($this->_compare("ip"))
00288 {
00289
00290 # Client browser does not support cookies but
00291 # we can track the visitor by the IP.
00292
00293 $this->_trackVisitor("old visitor");
00294 $this->_log("impression");
00295 }
00296 else if ($this->_reloadRestriction())
00297 {
00298
00299 # Another visitor has requested the website during
00300 # the period of time defined as reload restriction.
00301 # It is possible that the visitor we are actually
00302 # tracking has already been counted during that reload
00303 # restriction. To check this, we search the IP in the
00304 # older user tracks until the elapsed time is higher
00305 # than the reload restriction.
00306
00307 # If the index is < 0, we have reached the
00308 # end of the logfile. We log the new visitor,
00309 # which has never visited us before.
00310
00311 $this->currentIndex--;
00312
00313 if ($this->currentIndex >= 0)
00314 $this->_processPageRequest(FALSE);
00315 else
00316 {
00317 $this->_log("visit");
00318 $this->_trackVisitor("new visitor");
00319 $this->_log("impression");
00320 }
00321 }
00322 else
00323 {
00324
00325 # A new visitor has entered the website.
00326
00327 $this->_log("visit");
00328 $this->_trackVisitor("new visitor");
00329 $this->_log("impression");
00330 }
00331
00332 break;
00333 }
00334 }
00335 }
00336
00337
00338
00339
00340
00341
00342
00343 function _trackVisitor($mode)
00344 {
00345 $log_file = file($this->log_data["track"]);
00346
00347 switch ($mode)
00348 {
00349 case "new visitor":
00350
00351 # Start a new user track.
00352 $user_track = "|" . $this->ip . "|" . time() . "|" . time() . "|" . $this->webpage . "|\r\n";
00353
00354 # Add the new user track to the logfile.
00355 $this->_writeLine($this->log_data["track"], $user_track, "a");
00356 break;
00357
00358 case "old visitor":
00359
00360 # Add the actual webpage to the existing user track.
00361 # Try to add the session id to the user track.
00362
00363 $user_track = explode("|", $log_file[$this->currentIndex]);
00364
00365 if (isset($this->sessionID))
00366 $user_track[0] = $this->sessionID;
00367
00368 # Track the time of the actual request.
00369 $user_track[3] = time();
00370
00371 $user_track[sizeof($user_track) - 1] = $this->webpage . "|\r\n";
00372 $log_file[$this->currentIndex] = implode("|", $user_track);
00373
00374 # Write the changed user track back to the logfile.
00375 $this->_writeArray($this->log_data["track"], $log_file, "w");
00376 break;
00377 }
00378 }
00379
00380
00381
00382
00383
00384
00385
00386 function _compare($subject)
00387 {
00388 $log_file = file($this->log_data["track"]);
00389
00390 $user_track = explode("|", $log_file[$this->currentIndex]);
00391
00392 switch ($subject)
00393 {
00394 case "ip":
00395
00396 # Compare the object ip with the ip
00397 # of the user track.
00398
00399 if (isset($user_track[1]) && !strcmp($user_track[1], $this->ip))
00400 return TRUE;
00401 else
00402 return FALSE;
00403
00404 break;
00405
00406 case "sessionID":
00407
00408 # Compare the object session id with the
00409 # session id stored in the user track.
00410
00411 if (isset($this->sessionID) && !strcmp($user_track[0], $this->sessionID))
00412 return TRUE;
00413 else
00414 return FALSE;
00415
00416 break;
00417 }
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 function _reloadRestriction()
00429 {
00430 $log_file = file($this->log_data["track"]);
00431 $user_track = explode("|", $log_file[$this->currentIndex]);
00432
00433 if (isset($user_track[3]) && (time() - $user_track[3] < $this->reloadRestriction))
00434 return TRUE;
00435 else
00436 return FALSE;
00437 }
00438
00439
00440
00441
00442
00443
00444
00445 function _identifyClientAs($category)
00446 {
00447 switch ($category)
00448 {
00449 case "spider":
00450 $library_data = $this->spider_library;
00451
00452 $subject = $this->user_agent;
00453 break;
00454 }
00455
00456 $library_file = file($library_data);
00457
00458 # Compare the spider library with the user_agent.
00459 # If there are any matches, the current page has
00460 # been requested by a spider or a robot.
00461
00462 for ($i = 0; $i < sizeof($library_file); $i++)
00463 {
00464 $library_line = explode("|", $library_file[$i]);
00465
00466 if (eregi($library_line[0], $subject))
00467 return $library_line[1];
00468 }
00469
00470 return FALSE;
00471 }
00472 }
00473 ?>