Location: PHPKode > projects > D4WStatsLittle - Web Stats System > d4wstatslittle-web-stats-system/Install Without Using Dreamweaver/d4wstatslittle/logger-v2.php
<?php
/*
+--------------------------------------------------------------------------
|   D4WStatsLittle v1.1.2 - Developers4Web Web Stats Software
|   ==========================================================
|   by Developers4Web.com
|	D4WStatsLittle is a Trade Mark of Developers4Web
|   Copyright Developers4Web 2005 - 2006. All rights reserved.
|   English:
|       http://stats-service.developers4web.com
|   Español:
|       http://www.developers4web.com
|   ========================================
|   optimized by Trio Solutions
|   Dreamweaver Extensions and Web Development Components 
|   English:
|       http://components.developers4web.com
|   Español:
|       http://componentes.developers4web.com
|   ========================================
|   Web: http://www.d4wstats.com
|   Date: Tuesday, February 20, 2006
|   Email: info (at) developers4web (dot) com
|	License Type: D4WSTATSLITTLE is NOT Open Source Software and Limitations Apply 
|   Licence Info: /d4wstatslittle/license.txt
+--------------------------------------------------------------------------
*/
?>
<?php

  error_reporting(7);

  $dc = __FILE__;
  if ($dc == '')
      $dc = $_SERVER["SCRIPT_FILENAME"];
  $dc = str_replace("\\","/", $dc);      
  $dc = str_replace("//","/", $dc); 
  $dc = substr($dc,0,strpos($dc, "/d4wstatslittle"));
  $documentroot  = $dc;
  
  define('DC_ROOT',$documentroot."/");
  define('SERVER_PATH',$documentroot."/d4wstatslittle/d4wfiles/logs/");

  $blocked = file(DC_ROOT."d4wstatslittle/blocked-ips.txt");


  if ( array_search($_SERVER["REMOTE_ADDR"]."\r\n", $blocked) ||
       array_search($_SERVER["REMOTE_ADDR"]."\n\r", $blocked) ||
       array_search($_SERVER["REMOTE_ADDR"]."\r", $blocked) ||
       array_search($_SERVER["REMOTE_ADDR"], $blocked) ||
       array_search($_SERVER["REMOTE_ADDR"]."\n", $blocked))
  {
      exit;  //blocked ip address
  }


  DEFINE('SE_GOOGLE',1);
  DEFINE('SE_YAHOO',2);
  DEFINE('SE_MSN',3);  

  $referer = $_SERVER["HTTP_REFERER"];
  
  if ($referer == "") $referer = $_GET["src"];
  $referer = cleanURL($referer);
  $url = $_GET["refer"];
  $browser = ckbrowser($_SERVER["HTTP_USER_AGENT"]);
  $ip = IPAddress2IPNumber($_SERVER["REMOTE_ADDR"]);
  $resolution = $_GET["w"] . "x".  $_GET["h"];
  $cookies = $_GET["ck"];

  $searchengine = 0;
  $keyword = "";
  
  // separar datos de referer
  $domain = $url;
  $path = substr($url,7);
  $pos = strpos($path, "/");
  if ($pos === false)
      $path = "";
  else
  {
      $path = substr($path,$pos+1);
      $domain = substr($domain,0,$pos+7);
  }  
  
  
  // separar datos de cliente
  $client_domain = $referer;
  $page = substr($referer,7);
  $pos = strpos($page, "/");
  if ($pos === false)
      $page = "";
  else
  {
      $page = substr($page,$pos+1);
      $client_domain = substr($client_domain,0,$pos+7);
  }
  $client_clean_domain = substr($client_domain,7);
  
  
  
  if ( (substr($url,0,strlen("http://www.google."))=="http://www.google.") && (strpos($url, "q=")>0) )
  {
      $keyword = getwords($url,"q");
      $searchengine = 1;
  }
  else if ( ((strpos($url, "bing.com")>0) || (strpos($url, "bing.es")>0) || (strpos($url, "bing.se")>0) || (strpos($url, "bing.ch")>0)) && (strpos($url, "q=")>0) )
  {
      $keyword = getwords($url,"q");
      $searchengine = 2;
  }
  else if ( (strpos($url, ".yahoo.com")>0) && (strpos($url, "p=")>0) )
  {
      $keyword = getwords($url,"p");
      $searchengine = 3;
  }

  $tablename = "today_".date("Ymd");
  
  // if not tablenames yet, then create it
  if (!file_exists(SERVER_PATH.'logger-v2-tablenames.inc.php'))
  {
      $fd = fopen(SERVER_PATH.'logger-v2-tablenames.inc.php', "w");
      flock($fd, LOCK_EX );
      $contents = "<?php\r\n?>";
      fwrite ($fd, $contents);
      flock($fd, LOCK_UN );
      fclose($fd);     
      // esto es para la fecha de inicio
      $fd = fopen(SERVER_PATH.'logger-startdate.inc.php', "w");
      flock($fd, LOCK_EX );
      $contents = "<?php\r\ndefine('D4W_LOGGER_START','".date("Y-m-d")."');\r\n?>";
      fwrite ($fd, $contents);
      flock($fd, LOCK_UN );
      fclose($fd);                
  }
  
  if (!file_exists(SERVER_PATH.'logger-v2-tablenames.inc.php'))
  {
      echo 'Please, set write permisions to folder /d4wstatslittle/d4wfiles/logs , this step is required.';
      exit;
  }

  require SERVER_PATH.'logger-v2-tablenames.inc.php';

  $fullpath = SERVER_PATH . $tablename;
  
  if (!file_exists($fullpath))
      addDefine("logger-v2-tablenames.inc.php", $tablename);

  $ftd = fopen($fullpath, "a");
  flock($ftd, LOCK_EX );    
    
  $country = getCountryByIp($ip, "1");
  if ($country == 0)
      $country = getCountryByIp($ip, "2");
      
  //        hora,cookie,resolution,ip_converted,country,domain,path,keyword,searchengine,browser,page)";
  if ($domain == 'http://'. $_SERVER["HTTP_HOST"] || $domain == 'https://'. $_SERVER["HTTP_HOST"])
  {
      $domain = '';
      $path = '';
  }
  if ($path != '')
      $path = "/".$path;
  $values = date("H").",".$cookies.",".$resolution.",".$ip.",".$country.",".$domain.",\"".rXquote($path)."\",\"".rXquote($keyword)."\",".$searchengine.",".$browser.",\"/".rXquote($page)."\"";
  fwrite($ftd, $values ."\r\n");
  flock($ftd, LOCK_UN );
  fclose($ftd);   

  // needed functions
  
  function addDefine ($filename, $constantname, $comment = "")
  {
      $filename = SERVER_PATH.$filename;
      $fd = fopen($filename, "r+");
      flock($fd, LOCK_EX );
      $contents = fread ($fd, filesize ($filename)-2);
      $pos = strpos($contents,$constantname);
      if ($pos === false)
      {
          $contents = "define('".$constantname."','1');// ".$comment."\r\n?>";
          fwrite ($fd, $contents);
      }     
      flock($fd, LOCK_UN );
      fclose($fd);      
  }

  function cleanURL ($url)
  {
      if ($url == '') return $url;
      if (strpos(strtoupper($url), "PHPSESSID"))
          $url = substr($url, 0, strpos(strtoupper($url), "PHPSESSID")-1);
      if (strpos(strtolower($url), "?") == strlen($url)-1)
          $url = substr($url, 0, strlen($url)-1);
      if (strpos(strtolower($url), "/",  strlen($url)-1) == strlen($url)-1)
          $url = substr($url, 0, strlen($url)-1);
      return $url;
  }

  function getCountryByIp ($ip, $fileid = "1")
  {
      $ip = floatval($ip);
      $ranges = file(SERVER_PATH."iptocountry".$fileid.".csv");
      $start = 0;
      $end = count($ranges) - 1;
      $found = false;
      $country = 0;
      
      while (!$found && $start <= $end) 
      {
         //echo $start." ".$end."<br />";
         $fpos = floor($start + ($end-$start) / 2);
         $arr = explode (",", $ranges[$fpos]);
         if (floatval($arr[0]) <= $ip && floatval($arr[1]) >= $ip)
         {
             $found = true;
             $country = trim($arr[2]);
         }
         else if (floatval($arr[0]) > $ip)
         {
             if ($end == $fpos)
                 $found = true;
             else
                 $end = $fpos;
         }
         else    
         {
             if ($start == $fpos)
             {
                 if ($start < $end)
                     $start = $end;
                 else    
                     $found = true;
             }    
             else          
                 $start = $fpos;
         }    
      }
      
      return $country;
  }

  function IPAddress2IPNumber($dotted)
  {
     $dotted = preg_split( "/[.]+/", $dotted);
     $ip = (double) ($dotted[0] * 16777216) + ($dotted[1] * 65536) + ($dotted[2] * 256) + ($dotted[3]);
     return $ip;
  }

  function ckbrowser($user_agent)
  {
 	  if((ereg("Netscape", $user_agent))) $browser = "Netscape";
 	  elseif(ereg("Firefox", $user_agent)) $browser = "Firefox";
      elseif(ereg("MSIE", $user_agent)) $browser = "MSIE";
      elseif(ereg("Lynx", $user_agent)) $browser = "Lynx";
      elseif(ereg("Opera", $user_agent)) $browser = "Opera";
      elseif(ereg("Gecko", $user_agent)) $browser = "Mozilla";
      elseif(ereg("WebTV", $user_agent)) $browser = "WebTV";
      elseif(ereg("Konqueror", $user_agent)) $browser = "Konqueror";
      elseif((eregi("bot", $user_agent)) || (ereg("Google", $user_agent)) || (ereg("Slurp", $user_agent)) || (ereg("Scooter", $user_agent)) || (eregi("Spider", $user_agent)) || (eregi("Infoseek", $user_agent))) $browser = "Bot";
      else $browser = "Other";

      return($browser);
  }

  function getwords($url,$param)
  {

      $pos = strpos($url, "?".$param."=");
      if ($pos === false)
      {
          $pos = strpos($url, "&".$param."=");
          if ($pos === false)
              return '';
          else
              $url = substr($url,$pos+3);
      }
      else
          $url = substr($url,$pos+3);
      $pos = strpos($url, "&".$param."=");
      if ($pos === false)
      {
          $pos = strpos($url, "&");
          if ($pos === false)
              return $url;
          else
              return substr($url,0,$pos);
      }
      else
          $url = substr($url,$pos+3);
      $pos = strpos($url, "&");
          if ($pos === false)
              return $url;
          else
              return substr($url,0,$pos);

  }

  function rXquote ($str)  
  {  	 
	  return str_replace('"','""',$str);
  }


?>
Return current item: D4WStatsLittle - Web Stats System