Location: PHPKode > projects > IPTables log analyzer > iptablelog/utils/Misc.php
<?php

/////////////////////////////////////////////////////////////////////////////////////
// IPTable log analyzer
// Copyright (C) 2002 Gerald GARCIA
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Plac<B2>e - Suite 330, Boston, MA  02111-1307, USA.
//
// Contact author : hide@address.com
/////////////////////////////////////////////////////////////////////////////////////

// $Id: Misc.php,v 1.4 2007/02/23 22:42:47 tarbuck Exp $

$dateToString[1]="1 day";
$dateToString[2]="2 days";
$dateToString[7]="1 week";
$dateToString[14]="2 weeks";
$dateToString[31]="1 month";
$dateToString[10000]="any";

$port_names = array();
$port_descriptions = array();
$port_protos = array();

function findPortNameFromNumber($nb) {
  global $port_names;
  global $port_descriptions;
  global $port_protos;
  
  if (!isset($port_names[$nb])) {
    $query = "SELECT name,proto,description FROM ports WHERE port=$nb ORDER BY custom LIMIT 1";
    $result = db_query ($query);
    if ($port_desc=db_nextobject($result)) {
      $port_names[$nb] = $port_desc->name;
      $port_descriptions[$nb] = $port_desc->description;
      $port_protos[$nb] = $port_desc->proto;
    } else {
      $port_names[$nb] = "";
      $port_descriptions[$nb] = "";
      $port_protos[$nb] = "";
    }
  }

  return $port_names[$nb];
}

function findPortDescFromNumber($nb) {
  global $port_names;
  global $port_descriptions;
  global $port_protos;

  if (!isset($port_names[$nb])) {
    $query = "SELECT name,proto,description FROM ports WHERE port=$nb ORDER BY custom LIMIT 1";
    $result = db_query ($query);
    if ($port_desc=db_nextobject($result)) {
      $port_names[$nb] = $port_desc->name;
      $port_descriptions[$nb] = $port_desc->description;
      $port_protos[$nb] = $port_desc->proto;
    } else {
      $port_names[$nb] = "";
      $port_descriptions[$nb] = "";
      $port_protos[$nb] = "";
    }
  }

  return $port_descriptions[$nb];
}

function findPortProtoFromNumber($nb) {
  global $port_names;
  global $port_descriptions;
  global $port_protos;

  if (!isset($port_names[$nb])) {
    $query = "SELECT name,proto,description FROM ports WHERE port=$nb ORDER BY custom LIMIT 1";
    $result = db_query ($query);
    if ($port_desc=db_nextobject($result)) {
      $port_names[$nb] = $port_desc->name;
      $port_descriptions[$nb] = $port_desc->description;
      $port_protos[$nb] = $port_desc->proto;
    } else {
      $port_names[$nb] = "";
      $port_descriptions[$nb] = "";
      $port_protos[$nb] = "";
    }
  }

  return $port_protos[$nb];
}

function getmicrotime()
{
  list($usec, $sec) = explode(" ",microtime());
   return ((float)$usec + (float)$sec);
}

function parsesubnet($subnet,$returntype=0) {
# takes subnet for the form net/bitmakx (0 <= bitmask <= 32) and returns
# the net and the subnetmask in an array
# the format of the return vars is dependent on $returntype:
#    $returntype=0   returns int type, suitable for manipulation (DEFAULT)
#    $returntype=1   returns unsigned ints as strings (for inclusion in a db query)
#    $returntype=2   returns dotted quad, suitable for display
    list($net,$mask) = split("/",$subnet);
    if (is_numeric($mask)) {
    	$mask = 0xffffffff << (32 - $mask);
    } else {
     	$mask = 0xffffffff;
    }

    switch ($returntype) {
    	case 0:
    		$net = ip2long($net);
    	        $mask = ip2long($mask);
		break;
    	case 1:
    		$net = sprintf("%u",ip2long($net));
    	        $mask = sprintf("%u",$mask);
		break;
	case 2:
		# no change to net is required
		$mask = long2ip($mask);
		break;
    }
		
		
    return array($net,$mask);
}

$hostname_cache = array();

function getHostnameFromCache($ip,$number=0) {
    global $hostname_cache;

    if ($number == 0) { $ip = ip2long($ip); }
    #$ip = sprintf("%u",$ip);
    if (!isset($hostname_cache[$ip])) {
	$query = sprintf("%s %u %s","SELECT host FROM host_cache WHERE ip=",$ip," LIMIT 1");
	$result=db_query($query);
	if(!($hostname_cache[$ip] = db_nextvalue($result))) {
		$hostname_cache[$ip] = "";
	}
    }
    return $hostname_cache[$ip];
}

?>
Return current item: IPTables log analyzer